id LddlZddlZddlZddlmZddZdZGddeZy)N)Clustercn|sgS|jdDcgc]}|jc}Scc}w)zParse comma separated string.,)splitstrip)stringsstrings =/usr/lib/python3/dist-packages/sos/collector/clusters/juju.py_parse_option_stringr s-  )0s); .IndexzIndex structure to help parse juju status output. Attributes apps, units and machines are dict which key is the app/unit/machine name and the value is list of targets which format are {model_name}:{machine_id}. cp||_i|_i|_i|_t j d|_y)Nsos) model_nameappsunitsmachineslogging getLoggerui_log)selfrs r __init__z"_get_index..Index.__init__,s0#-DODIDJDM!++E2DKc>|djD]\}}g}|jdi}|jD]L\}}|d}|jd|} | g|j|<| g|j|<|j | N||j |<y)zAdds principal units to index. applicationsrmachine:N)itemsgetrrrappendr) r juju_statusappapp_infonodesrunit unit_infornodes r add_principalsz(_get_index..Index.add_principals3s!,^!.Index.add_subordinatesAsd "-^!!C,=F99==0 ++.vhkB!IIcN))$))F*;<k.&A&&II ++.vh6GH!'7?HE+0;;==i"&//!2!Ii4H3IJ2;--*B3%'=.GY '11#)<7;f 7 3 ==#= =rcv|djD]#}|jd|}|g|j|<%y)zAdd machines to index. If model does not have any applications it needs to be manually added. rrN)keysrr)rr"rr(s r add_machinesz&_get_index..Index.add_machinesbsF 'z2779 0//*!G95*. g& 0rN)__name__ __module__ __qualname____doc__rr)r4r7rr Indexr#s  3 ' =B 0rr=r<)rr=s r _get_indexr>sG0G0R  rcJeZdZdZdZdZgdZdZdZdZ dZ dZ d Z d Z y ) jujua! The juju cluster profile is intended to be used on juju managed clouds. It"s assumed that `juju` is installed on the machine where `sos` is called, and that the juju user has superuser privilege to the current controller. By default, the sos reports will be collected from all the applications in the current model. If necessary, you can filter the nodes by models / applications / units / machines with cluster options. Example: sos collect --cluster-type juju -c "juju.models=sos" -c "juju.apps=a,b,c" zJuju Managed Clouds))rz1Filter node list by apps (comma separated regex).)rrAz3Filter node list by units (comma separated string).)modelsrAz4Filter node list by models (comma separated string).)rrAz6Filter node list by machines (comma separated string).cPtjdd|dtjS)z#Remove leading characters before {.z (^[^{]*)(.*)z\2r)resub MULTILINE)routputs r _cleanup_juju_outputzjuju._cleanup_juju_outputsvvoufaFFrc|j|}t|}|j||j||j ||S)zParse juju status output and return target dict. Here are couple helper functions to parse the juju principals units, subordinate units and machines. )r)_execute_juju_statusr>r)r4r7)rrr"indexs r _get_model_infozjuju._get_model_infosM // ; j1 [) {+ ;' rc@|rd|nd}d}|jd|d|}|j|}|ddk(std|d |d|j|d }d} t j |}|S#tj $rtd |wxYw) Nz-m rAz --format jsonz status  statusr'z' returned error: rGz-Juju output is not valid json format.Output: )cmdexec_primary_cmd ExceptionrHjsonloadsJSONDecodeError)rr model_option format_option status_cmdresjuju_json_outputr"s r rJzjuju._execute_juju_statuss-7ZL)R ' z,qH ##J/8}!a |+=c(m_MN N44c(mE  **%56K  ## +,.  s $A;;"Bct}|D]P}t||jD]1\}}tj||s|j |xsg3R|S)zFilter with regex match.)setgetattrrrDmatchupdaterkeypatterns model_infor%patternparamvalues r _filter_by_patternzjuju._filter_by_patterns` .G ' C 8 > > @ . u88GU+LL"- . . rct}|D]?}t||jD] \}}||k(s |j|xsg"A|S)zFilter with fixed match.)r]r^rr`ras r _filter_by_fixedzjuju._filter_by_fixedsZ .G ' C 8 > > @ . ue#LL"- . . rcy)z'Dynamically change transport to 'juju'.r@r<)rs r set_transport_typezjuju.set_transport_typesrc&t|jd}t|jd}t|jd}t|jd}|||d}t|js|sgS|sdg}t }|D]h}|j |}|j D]B\} } | dk(r|j| | |} n|j| | |} |j| Djt|S)z+Get the machine numbers from `juju status`.rBrrr)rrrrA) r get_optionanyvaluesr]rLrrhrjr`list) rrBrrrfiltersr%modelrdrbresource_nodess r get_nodeszjuju.get_nodess%dooh&?@#DOOF$;<$T__W%=>' (CD%XF7>>#$VITF %E--e4J!( % X&=!44S(JOF!223*MF V$  % %E{rN)r8r9r:r;rQ cluster_name option_listrHrLrJrhrjrlrvr<rr r@r@os>  C(L KG &rr@)N)rrTrDsos.collector.clustersrr r>r@r<rr rzs- *=Slt7tr