o x[h,@sddlZddlZddlZddlmZddlmZmZmZddl m Z ddl m Z m Z ddl mZddl mZddlmZdd lmZeeZGd d d ZGd d d e jZddZddZddZee je jffgZ ddZ!dS)N)suppress)gaierror getaddrinfo inet_ntoa)pack)sourcessubp) url_helper)util)dhcp)ec2c@s(eZdZdZddZddZddZdS) CloudStackPasswordServerClienta Implements password fetching from the CloudStack password server. http://cloudstack-administration.readthedocs.org/ en/latest/templates.html#adding-password-management-to-your-templates has documentation about the system. This implementation is following that found at https://github.com/shankerbalan/cloudstack-scripts/ blob/master/cloud-set-guest-password-debian cCs ||_dSN)virtual_router_address)selfrrH/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceCloudStack.py__init__+ z'CloudStackPasswordServerClient.__init__cCs:tddddddddd d |d |jg \}}|S) Nwgetz--quietz--tries3z --timeout20z--output-document-z--headerzDomU_Request: {0}z{0}:8080)rformatrstrip)r domu_requestoutput_rrr _do_request.s z*CloudStackPasswordServerClient._do_requestcCs4|d}|dvr dS|dkrtd|d|S)Nsend_my_password)saved_password bad_requestz-Error when attempting to fetch root password.r!)r RuntimeError)rpasswordrrr get_passwordCs  z+CloudStackPasswordServerClient.get_passwordN)__name__ __module__ __qualname____doc__rrr%rrrrr s   r csleZdZdZdZdZddZddZ   dfd d Zd d Z d dZ ddZ ddZ e ddZZS)DataSourceCloudStack CloudStackx2cCsdtj||||tj|jd|_d|_||_t |j|_ |j s%t dd|j d|_ i|_ dS)NcslatestzNo virtual router found!zhttp:///)r DataSourcerospathjoinseed_dirapi_verdistroget_vr_addressvr_addrr#metadata_addresscfg)rsys_cfgr7pathsrrrrUs  zDataSourceCloudStack.__init__cCstdtd}|r|Stdttjt|jd}|r-|WdSWdn1s7wYtd|jj j tt |jj |jj }|dpYd}|WdS1sfwYtddS)z Try obtaining a "domain-name" DHCP lease parameter: - From systemd-networkd lease - From dhclient lease z.Try obtaining domain name from networkd leases DOMAINNAMEzHCould not obtain FQDN from networkd leases. Falling back to ISC dhclientz domain-nameNzBCould not obtain FQDN from ISC dhclient leases. Falling back to %szNo dhcp leases found)LOGdebugr networkd_get_option_from_leasesrNoDHCPLeaseMissingDhclientError IscDhclientget_key_from_latest_leaser7 dhcp_client client_nameFileNotFoundErrorget_newest_leasefallback_interfaceget)r domainname domain_name latest_leaserrr_get_domainnamecs:     z$DataSourceCloudStack._get_domainnameFcsnt|||}|r5d|jvr5td|}|r/|jd|}td|t||jStd||S)z Returns instance's hostname / fqdn First probes the parent class method. If fqdn is requested, and the parent method didn't return it, then attach the domain-name from DHCP response. .zFQDN requestedzObtained the following FQDN: %szNCould not determine domain name for FQDN. Fall back to hostname as an FQDN: %s) super get_hostnamehostnamer?r@rNrDataSourceHostname is_default)rfqdn resolve_ip metadata_onlyrRrK __class__rrrQs   z!DataSourceCloudStack.get_hostnamecCs|}|jdkr dSt|jdg}t}tj||j|jt j d\}}|r1t d|t|St d|t t|t|S)NrFzlatest/meta-data/instance-id)urlsmax_waittimeout status_cbzUsing metadata source: '%s'z>Giving up on waiting for the metadata from %s after %s seconds)get_url_paramsmax_wait_secondsuhelp combine_urlr:time monotonic wait_for_urltimeout_secondsr?warningr@criticalintbool)r url_paramsrZ start_timeurl _responserrrwait_for_metadata_services.    z.DataSourceCloudStack.wait_for_metadata_servicecCs|jSr)r;rrrrget_config_objsz#DataSourceCloudStack.get_config_objcCsi}tj||jddr|d|_|d|_td|jdSzV|s'WdSt }t |j |j |_t |j |j |_tdtt |t|j}z|}Wntygttd |jYWdSw|rsd|d did |_WdStyttd |j YdSw) Nr0)basez user-dataz meta-dataz%Using seeded cloudstack data from: %sTFz)Crawl of metadata service took %s secondsz/Failed to fetch password from virtual router %sexpire) ssh_pwauthr$chpasswdz(Failed fetching from metadata service %s)r read_optional_seedr5 userdata_rawmetadatar?r@rnrbrcr get_instance_userdatar6r:get_instance_metadatarhr r9r% Exceptionlogexcr;)rseed_retrkpassword_client set_passwordrrr _get_datas\      zDataSourceCloudStack._get_datacC |jdS)Nz instance-idrwrorrrget_instance_idrz$DataSourceCloudStack.get_instance_idcCr)Nzavailability-zonerrorrravailability_zones z&DataSourceCloudStack.availability_zone)FFF)r&r'r(dsname url_max_wait url_timeoutrrNrQrnrprrpropertyr __classcell__rrrXrr*Ms ,0r*cCs>ztdd}WntytdYdSw|dddS)Nz data-serverPzDNS Entry data-server not foundr)rrr?r@)addrinforrrget_data_servers  rcCs^td}|D]#}|d}|ddkr,ttdt|dd}td||Sq dS) Nz/proc/net/route 00000000zs*       .7  2