3 ܓi!@sbddlZddlZddlZddlmZddlmZddlmZd ddZddZ Gd d d eZ dS) N)Cluster)sos_parse_version)sos_get_command_outputcCs|sgSdd|jdDS)zParse comma separated string.cSsg|] }|jqS)strip).0stringrr/usr/lib/python3.6/juju.py sz(_parse_option_string..,)split)Zstringsrrr _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|_tjd|_dS)NZsos) model_nameappsunitsmachinesloggingZ getLoggerui_log)selfrrrr __init__.s z"_get_index..Index.__init__c Ssx|djD]r\}}g}|jdi}xN|jD]B\}}|d}|jd|} | g|j|<| g|j|<|j| q0W||j|<qWdS)zAdds principal units to index. applicationsrmachine:N)itemsgetrrrappendr) r juju_statusappapp_infonodesrZunit unit_infornoderrr add_principals5s   z(_get_index..Index.add_principalsc Ss x|djD]\}}|jdg}x|D]}|jj|sP|jjd|dq*|j|j|j|d|d|kr|jjd|dq*|d|d}x`|jD]T\}}|jd|d} x6|jd ijD]"\} }| j|d r| g|j| <qWqWq*WqWd 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) rrrrZsubordinate_toparentr_r r!Zsub_keyrrr add_subordinatesCs&   z*_get_index..Index.add_subordinatescSs6x0|djD] }|jd|}|g|j|<qWdS)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 !r/r)rr/rrr _get_indexs Ir0c@sdeZdZdZdZdZdddd gZd d ZddZddZ ddZ ddZ 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 Cloudsr1Filter node list by apps (comma separated regex).r3Filter node list by units (comma separated string).models4Filter node list by models (comma separated string).r6Filter node list by machines (comma separated string).cCstjdd|dtjdS)z#Remove leading characters before {.z (^[^{]*)(.*)z\2r)countflags)resub MULTILINE)routputrrr _cleanup_juju_outputszjuju._cleanup_juju_outputcCs6|j|}t|d}|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_statusr0r"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}|j}t|tdkr6|d7}|jd|d|}|j|}|dd ksztd |d |d|j|d }d}tj|}|S) Nz-m r2z --format json3z --no-colorz status  Zstatusr'z' returned error: r=)rCrcmdZexec_primary_cmd Exceptionr>jsonloads) rrZ model_optionZ format_optionZ juju_versionZ status_cmdrBZjuju_json_outputrrrr r?s   zjuju._execute_juju_statuscCsNt}xB|D]:}x4t||jD]"\}}tj||r |j|p>gq Wq W|S)zFilter with regex match.)setgetattrrr:matchupdate)rkeypatterns model_inforpatternparamvaluerrr _filter_by_patterns   zjuju._filter_by_patterncCsJt}x>|D]6}x0t||jD]\}}||kr |j|p:gq Wq W|S)zFilter with fixed match.)rKrLrrN)rrOrPrQrrRrSrTrrr _filter_by_fixeds  zjuju._filter_by_fixedcCsdS)z'Dynamically change transport to 'juju'.r1r)rrrr set_transport_typeszjuju.set_transport_typec Cst|jd}t|jd}t|jd}t|jd}|||d}t|j r\| r\gS|sfdg}t}x\|D]T}|j|}xD|jD]8\} } | dkr|j| | |} n|j| | |} |j | qWqrWt |S)z+Get the machine numbers from `juju status`.r5rrr)rrrr2) r Z get_optionanyvaluesrKrArrUrVrNlist) rr5rrrfiltersrZmodelrQrOZresourceZ_nodesrrr get_nodess$   zjuju.get_nodesN)rr2r3)rr2r4)r5r2r6)rr2r7)r+r,r-r.rGZ cluster_nameZ option_listr>rArCr?rUrVrWr\rrrr r1qs"  r1)N) rrIr:Zsos.collector.clustersrZ sos.utilitiesrrr r0r1rrrr  s    V