a >h!@sbddlZddlZddlZddlmZddlmZddlmZd ddZddZ Gd d d eZ dS) N)Cluster)sos_parse_versionsos_get_command_outputcCs|sgSdd|dDS)zParse comma separated string.cSsg|] }|qS)strip).0stringrr?/usr/lib/python3.9/site-packages/sos/collector/clusters/juju.py z(_parse_option_string..,)split)stringsrrr _parse_option_stringsrcCsGddd}||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)NZsos) model_nameappsunitsmachinesloggingZ getLoggerui_log)selfrrrr __init__.s z"_get_index..Index.__init__c Ss|dD]n\}}g}|di}|D]B\}}|d}|jd|} | g|j|<| g|j|<|| q,||j|<q dS)zAdds principal units to index. applicationsrmachine:N)itemsgetrrrappendr) r juju_statusappapp_infonodesrZunit unit_infornoderrr add_principals5s    z(_get_index..Index.add_principalsc Ss|dD]\}}|dg}|D]}|j|sJ|jd|dq$|j||j|d|d|vr|jd|dq$|d|d}|D]P\}}|jd|d} |d iD]"\} }| |d r| g|j| <qqq$q d 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 unitsrrZ subordinates/N) rrrrZwarningextendr startswithr) rrr r!Zsubordinate_toparentr_r#r$Zsub_keyrrr add_subordinatesCs,    z*_get_index..Index.add_subordinatescSs2|dD] }|jd|}|g|j|<q dS)zAdd machines to index. If model does not have any applications it needs to be manually added. rrN)keysrr)rrrr$rrr add_machinesdsz&_get_index..Index.add_machinesN)__name__ __module__ __qualname____doc__rr%r+r-rrrr Index%s !r2r)rr2rrr _get_indexs Ir3c@s`eZdZdZdZdZgdZddZddZdd Z d d Z d d Z ddZ ddZ ddZdS)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).)rr5z3Filter node list by units (comma separated string).)modelsr5z4Filter node list by models (comma separated string).)rr5z6Filter node list by machines (comma separated string).cCstjdd|dtjdS)z#Remove leading characters before {.z (^[^{]*)(.*)z\2r)countflags)resub MULTILINE)routputrrr _cleanup_juju_outputs zjuju._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_statusr3r%r+r-)rrrindexrrr _get_model_infos      zjuju._get_model_infocCstd}|dS)zGrab the version of jujuz juju versionr<r)rresrrr _get_juju_versionszjuju._get_juju_versionc Cs|rd|nd}d}|}t|tdkr6|d7}|jd|d|}||}|dd ksztd |d |d||d }d}t|}|S) Nz-m r5z --format json3z --no-colorz status  statusr'z' returned error: r<)rBrcmdZexec_primary_cmd Exceptionr=jsonloads) rrZ model_optionZ format_optionZ juju_versionZ status_cmdrAZjuju_json_outputrrrr r>s   zjuju._execute_juju_statuscCsFt}|D]6}t||D]"\}}t||r||p:gqq |S)zFilter with regex match.)setgetattrrr9matchupdaterkeypatterns model_infor"patternZparamvaluerrr _filter_by_patterns  zjuju._filter_by_patterncCsBt}|D]2}t||D]\}}||kr||p6gqq |S)zFilter with fixed match.)rKrLrrNrOrrr _filter_by_fixeds zjuju._filter_by_fixedcCsdS)z'Dynamically change transport to 'juju'.r4r)rrrr set_transport_typeszjuju.set_transport_typec Cst|d}t|d}t|d}t|d}|||d}t|sX|sXgS|sbdg}t}|D]P}||}|D]8\} } | dkr|| | |} n|| | |} | | qqlt |S)z+Get the machine numbers from `juju status`.r6rrr)rrrr5) rZ get_optionanyvaluesrKr@rrUrVrNlist) rr6rrrfiltersr"ZmodelrRrPresourceZ_nodesrrr get_nodess$  zjuju.get_nodesN)r.r/r0r1rGZ cluster_nameZ option_listr=r@rBr>rUrVrWr]rrrr r4qs   r4)N) rrIr9Zsos.collector.clustersrZ sos.utilitiesrrrr3r4rrrr  s    V