o id @sJddlZddlZddlZddlmZd ddZddZGdddeZdS) N)ClustercCs|sgSdd|dDS)zParse comma separated string.cSsg|]}|qS)strip).0stringrr=/usr/lib/python3/dist-packages/sos/collector/clusters/juju.py sz(_parse_option_string..,)split)stringsrrr_parse_option_stringsr cCsGddd}||S)aRHelper function to get Index. The reason why we need Index defined in function is because currently the collector.__init__ will load all the classes in this module and also Index. This will cause bug because it think Index is Cluster type. Also We don't want to provide a customized filter to remove Index class. c@s0eZdZdZddZddZddZdd Zd S) z_get_index..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}. cSs(||_i|_i|_i|_td|_dS)Nsos) model_nameappsunitsmachineslogging getLoggerui_log)selfrrrr__init__,s z"_get_index..Index.__init__c Ss|dD]7\}}g}|di}|D]!\}}|d}|jd|} | g|j|<| g|j|<|| q||j|<qdS)zAdds principal units to index. applicationsrmachine:N)itemsgetrrrappendr) r juju_statusappapp_infonodesrunit unit_infornoderrradd_principals3s     z(_get_index..Index.add_principalsc Ss|dD]s\}}|dg}|D]f}|j|s%|jd|dq|j||j|d|d|vrC|jd|dq|d|d}|D](\}}|jd|d} |d iD]\} } | |d rv| g|j| <qeqOqqd S) zAdd subordinates to index. Since subordinates does not have units they need to be manually added. rzsubordinate-tozPrincipal charm z is missingrz is missing unitsrr subordinates/N) rrrrwarningextendr startswithr) rrrrsubordinate_toparentrr!r"r#sub_key sub_valuerrradd_subordinatesAs:      z*_get_index..Index.add_subordinatescSs2|dD]}|jd|}|g|j|<qdS)zAdd machines to index. If model does not have any applications it needs to be manually added. rrN)keysrr)rrrr#rrr add_machinesbsz&_get_index..Index.add_machinesN)__name__ __module__ __qualname____doc__rr$r.r0rrrrIndex#s  !r5r)rr5rrr _get_indexs Ir6c@sXeZdZdZdZdZgdZddZddZdd Z d d Z d d Z ddZ ddZ dS)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).)rr8z3Filter node list by units (comma separated string).)modelsr8z4Filter node list by models (comma separated string).)rr8z6Filter node list by machines (comma separated string).cCstdd|dtjS)z#Remove leading characters before {.z (^[^{]*)(.*)z\2r)resub MULTILINE)routputrrr_cleanup_juju_outputszjuju._cleanup_juju_outputcCs6||}t|d}|||||||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_statusr6r$r.r0)rrrindexrrr_get_model_infos     zjuju._get_model_infocCs|rd|nd}d}|jd|d|}||}|ddks-td|d |d||d }d}zt|}W|StjyMtd |w) Nz-m r8z --format jsonz status  statusr'z' returned error: r=z-Juju output is not valid json format.Output: )cmdexec_primary_cmd Exceptionr>jsonloadsJSONDecodeError)rr model_option format_option status_cmdresjuju_json_outputrrrrr?s$   zjuju._execute_juju_statuscCsFt}|D]}t||D]\}}t||r||pgqq|S)zFilter with regex match.)setgetattrrr:matchupdaterkeypatterns model_infor patternparamvaluerrr_filter_by_patterns zjuju._filter_by_patterncCsBt}|D]}t||D]\}}||kr||pgqq|S)zFilter with fixed match.)rPrQrrSrTrrr_filter_by_fixedszjuju._filter_by_fixedcCsdS)z'Dynamically change transport to 'juju'.r7r)rrrrset_transport_typeszjuju.set_transport_typec Cst|d}t|d}t|d}t|d}|||d}t|s,|s,gS|s1dg}t}|D](}||}|D]\} } | dkrQ|| | |} n|| | |} | | qAq6t |S)z+Get the machine numbers from `juju status`.r9rrr)rrrr8) r get_optionanyvaluesrPrArr[r\rSlist) rr9rrrfiltersr modelrWrUresource_nodesrrr get_nodess&   zjuju.get_nodesN)r1r2r3r4rE cluster_name option_listr>rAr?r[r\r]rfrrrrr7os   r7)N)rrHr:sos.collector.clustersrr r6r7rrrrs   V