a 'Dg,@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)selfrrJ/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceCloudStack.py__init__+sz'CloudStackPasswordServerClient.__init__cCs:tddddddddd d |d |jg \}}|S) NZwgetz--quietz--tries3z --timeoutZ20z--output-document-z--headerzDomU_Request: {0}z{0}:8080)rformatrstrip)rZ domu_requestoutput_rrr _do_request.s z*CloudStackPasswordServerClient._do_requestcCs4|d}|dvrdS|dkr&td|d|S)NZsend_my_password)saved_passwordZ bad_requestz-Error when attempting to fetch root password.r)r RuntimeError)rpasswordrrr get_passwordCs  z+CloudStackPasswordServerClient.get_passwordN)__name__ __module__ __qualname____doc__rrrrrrrr s r csfeZdZdZdZdZddZddZdfd d Zd d Z d dZ ddZ ddZ e ddZZS)DataSourceCloudStackZ CloudStackx2cCsdtj||||tj|jd|_d|_||_t |j|_ |j sJt dd|j d|_ i|_ dS)NcsZlatestzNo virtual router found!zhttp:///)r DataSourcerospathjoinseed_dirapi_verdistroget_vr_addressvr_addrrmetadata_addresscfg)rZsys_cfgr/pathsrrrrUs zDataSourceCloudStack.__init__cCstdtd}|r|Stdttj8t|jd}|rZ|WdSWdn1sn0Ytd|jj j tt 4|jj |jj }|dpd}|WdS1s0YtddS)z Try obtaining a "domain-name" DHCP lease parameter: - From systemd-networkd lease - From dhclient lease z.Try obtaining domain name from networkd leasesZ 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_leaser/ dhcp_client client_nameFileNotFoundErrorget_newest_leaseZfallback_interfaceget)r domainnameZ domain_name latest_leaserrr_get_domainnamecs2   0 " z$DataSourceCloudStack._get_domainnameFcsnt|||}|rjd|jvrjtd|}|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_hostnamehostnamer5r6rBrZDataSourceHostnameZ is_default)rZfqdnZ resolve_ipZ metadata_onlyrFr@ __class__rrrEs   z!DataSourceCloudStack.get_hostnamecCs~|}|jdkrdSt|jdg}t}tj||j|jt j d\}}|r\t d|nt d|t t|t|S)NrFzlatest/meta-data/instance-id)urlsZmax_waittimeoutZ status_cbzUsing metadata source: '%s'z>Giving up on waiting for the metadata from %s after %s seconds)Zget_url_paramsZmax_wait_secondsuhelpZ combine_urlr2time monotonicZ wait_for_urlZtimeout_secondsr5Zwarningr6Zcriticalintbool)rZ url_paramsrI start_timeurlZ _responserrrwait_for_metadata_services,  z.DataSourceCloudStack.wait_for_metadata_servicecCs|jSr)r3rrrrget_config_objsz#DataSourceCloudStack.get_config_objcCsi}tj||jddr>|d|_|d|_td|jdSz|sNWdSt }t |j |j |_t |j |j |_tdtt |t|j}z |}Wn"tyttd |jYn0|rd|d did |_WdSty ttd |j YdS0dS) Nr()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 %sZexpire)Z ssh_pwauthrZchpasswdz(Failed fetching from metadata service %s)r Zread_optional_seedr-Z userdata_rawmetadatar5r6rRrLrMr Zget_instance_userdatar.r2Zget_instance_metadatarNr r1r ExceptionZlogexcr3)rZseed_retrPZpassword_clientZ set_passwordrrr _get_datasV      zDataSourceCloudStack._get_datacCs |jdS)Nz instance-idrVrSrrrget_instance_idsz$DataSourceCloudStack.get_instance_idcCs |jdS)Nzavailability-zonerYrSrrravailability_zonesz&DataSourceCloudStack.availability_zone)FFF)r r!r"ZdsnameZ url_max_waitZ url_timeoutrrBrErRrTrXrZpropertyr[ __classcell__rrrGrr$Ms,0r$cCsBztdd}Wnty,tdYdS0|dddSdS)Nz data-serverPzDNS Entry data-server not foundr)rrr5r6)Zaddrinforrrget_data_servers   r`cCs^td}|D]F}|d}|ddkrttdt|dd}td||SqdS) Nz/proc/net/route Z00000000zr<rj)r/Zlatest_addressrArrrr0s>   0  0 r0cCs t|tSr)rZlist_from_depends datasources)Zdependsrrrget_datasource_listRsrl)"Zloggingr*rL contextlibrZsocketrrrstructrZ cloudinitrrr rKr Z cloudinit.netr Zcloudinit.sources.helpersr Z getLoggerr r5r r)r$r`rjr0ZDEP_FILESYSTEMZ DEP_NETWORKrkrlrrrrs(       .7  2