U 'Dg6@sLUdZddlZddlZddlZddlmZmZddlmZddl m Z m Z m Z ddl mZddlmZddlmZdd lmZed d gZd d gedgeedZeed<eeZdZdZdZe dddZ!e dddZ"d+edddZ#d,ddZ$eddd Z%e e&d!d"d#Z'e dd$d%Z(e dd&d'Z)e*eeedd(d)d*Z+dS)-z1ubuntu_pro: Configure Ubuntu Pro support servicesN)AnyList)urlparse) performancesubputil)Cloud)Config) MetaSchema) PER_INSTANCEzubuntu-advantageZubuntu_advantageZ cc_ubuntu_proZubuntu ubuntu_pro)idZdistrosZ frequencyZactivate_by_schema_keysmetaREDACTEDzUUnable to determine if this is an Ubuntu Pro instance. Fallback to normal Pro attach.)Z http_proxyZ https_proxyZglobal_apt_http_proxyZglobal_apt_https_proxyZua_apt_http_proxyZua_apt_https_proxy) pro_sectioncCsd|kr dS|d}t|ts@dt|j}t|t|d|krLdS|d}t|tsdt|j}t|t|dS)Nfeaturesz.'ubuntu_pro.features' should be a dict, not a disable_auto_attachzB'ubuntu_pro.features.disable_auto_attach' should be a bool, not a ) isinstancedicttype__name__LOGerror RuntimeErrorbool)rrmsgrr@/usr/lib/python3/dist-packages/cloudinit/config/cc_ubuntu_pro.pyvalidate_schema_features*s     r) pro_configc Csg}t|D]z\}}|tkr0td|qn |dkr:qz&t|}|jdkr^|d|Wqtt fk r|d|YqXq|rt d d |dS)aValidate user-provided ua:config option values. This function supplements flexible jsonschema validation with specific value checks to aid in triage of invalid user-provided configuration. Note: It does not log/raise config values as they could be urls containing sensitive auth info. @param pro_config: Dictionary of config value under 'ubuntu_pro'. @raises: ValueError describing invalid values provided. z4Not validating unknown ubuntu_pro.config.%s propertyN)ZhttpZhttpsz-Expected URL scheme http/https for ua:config:zExpected a URL for ua:config:z$Invalid ubuntu_pro configuration: {} ) sorteditemsKNOWN_PRO_CONFIG_PROPSrwarningrZschemeappendAttributeError ValueErrorformatjoin)rerrorskeyvalueZ parsed_urlrrrsupplemental_schema_validationEs,  r-c Cs|dkr dSt|ts,tdt|jdt|g}t|D]\}}d}i}|dkrxt d|ddd|g}nh|d}t d|t d |r|d t |}n|d |}ddd |g}d |dd |gi}zt j |f|WqDt jk rD}z0t|} |dk r&| |t} ||| fW5d}~XYqDXqD|r|D]\} } td| | qRtdddd|DdS)Nz+ubuntu_pro: config should be a dict, not a z%; skipping enabling config parameterszDisabling Pro config for %sproconfigZunsetz =REDACTEDzEnabling Pro config %sz\s=set logstringz#Failure enabling/disabling "%s": %sz3Failure enabling/disabling Ubuntu Pro config(s): {}, css|]\}}d|VqdS)z"{}"N)r().0param_rrr sz!set_pro_config..)rrrrrr-r!r"rdebugresearchescaperProcessExecutionErrorstrreplacerr%r$r(r)) r enable_errorsr+r,Zredacted_key_valueZ subp_kwargsZ config_cmd key_valueeerr_msgr6rrrrset_pro_configlsH        " rDc Csz|dkrg}nrr$listrrrr9r)rr=r?rlogexcrrIloadsZJSONDecodeErrorgetr%)tokenrHZ attach_cmdZ redacted_cmdrBerrrcmdZ enable_stdoutr7Z enable_respr@Zerror_servicesrKrrr configure_pros         rTcloudcCsztdrdSz|jWn"tk r>ttdYnXz|jdgWn"tk rtttdYnXdS)z.Install ubuntu-advantage-tools if not present.r.NzPackage update failedzubuntu-advantage-toolsz(Failed to install ubuntu-advantage-tools) rZwhichZdistroZupdate_package_sources ExceptionrrNrZinstall_packagesrUrrrmaybe_install_ua_toolss   rX)rreturnc Cst|didd}|r dSddlm}ddlm}z$tjddd  |}W5QRXWn>|k r}z t d |t t WYdSd}~XYnX|jS) NrrFr)UserFacingError)should_auto_attachz6Checking if the instance can be attached to Ubuntu ProalwaysZlog_modez%Error during `should_auto_attach`: %s) rrPuaclient.api.exceptionsrZZ4uaclient.api.u.pro.attach.auto.should_auto_attach.v1r[rTimedrr9r$ERROR_MSG_SHOULD_AUTO_ATTACH)rrrZr[resultexrrr_should_auto_attach s$    rccCsZ|d}|s$d}t|t||d}|rDtdd|t||dddS)NrQz8`ubuntu_pro.token` required in non-Pro Ubuntu instances. enable_betazrrrrrrs<         ', d"