
    d,                     >   d Z ddlZddlZddlZddlZddlmZ ddlmc mZ ddl	mc m
Z
 ddlmc mc mZ ddlmc mc mZ ddlmZ ddlmZmZmZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZ dd
l m!Z! ddl"m#Z# dZ$dZ% ejL                  e%      Z'dZ( G d de)      Z*y)z
Provision handler
    N)datetime)ustr)	add_eventWALAEventOperationelapsed_milliseconds)ProvisionErrorProtocolErrorOSUtilError)
get_osutil)ProvisionStatus)get_protocol_util)
AGENT_NAME)cloud_init_is_enabled
CustomDatas   .*/bin/cloud-init.*provisionedc                       e Zd Zd Zd Zed        Zd ZddZed        Z	ed        Z
d Zd	 Zed
        Zd Zd Zd Zd Zd Zd Zddej*                  fdZd Zd Zy)ProvisionHandlerc                 @    t               | _        t               | _        y N)r   osutilr   protocol_utilselfs    F/usr/lib/python3/dist-packages/azurelinuxagent/pa/provision/default.py__init__zProvisionHandler.__init__5   s     l.0    c                    t        j                         s6t        j                  d       | j	                          | j                          y 	 t        j                         }d }| j                         rt        j                  d       y t        j                  d       t               rt        d      t        j                  d       | j                  j                         }| j                  j                          | j                  dd       t        j                  d       | j                  |       | j!                         }| j"                  j%                          | j	                          | j'                  d	j)                  | j+                               d
t-        |             | j/                  |j0                         | j                          t        j                  d       y # t2        t        f$ rv}dj)                  t5        |      | j+                               }t        j6                  |       | j                  dt5        |             | j'                  |d       Y d }~y d }~ww xY w)Nz#Provisioning is disabled, skipping.z)Provisioning already completed, skipping.z$Running default provisioning handlerzUcloud-init appears to be installed and enabled, this is not expected, cannot continuezCopying ovf-env.xmlProvisioningStartingzStarting provisioningzProvisioning succeeded ({0}s)T)
is_successdurationzProvisioning completezProvisioning failed: {0} ({1}s)ProvisioningFailedF)r    )confget_provision_enabledloggerinfowrite_provisionedreport_readyr   utcnowcheck_provisioned_filer   r   r   copy_ovf_envget_protocolreport_not_ready	provisionreg_ssh_host_keyr   restart_ssh_servicereport_eventformat_get_uptime_secondsr   handle_provision_guest_agentprovision_guest_agentr	   r   error)r   	utc_start
thumbprintovf_envemsgs         r   runzProvisionHandler.run9   s   ))+KK=>""$*	 )IJ**,GHKK>?$&$ &P Q Q KK-.((557G++-!!.*=KK/0NN7#..0JKK++-""$=DDTE]E]E_`-i8  : --g.K.KLKK/0~. 	3::47DD\D\D^_CLL!!"6Q@ce4	s    ;G( EG( (I-7A,I((I-c                      	 t        d      5 } | j                         j                         \  }}|cd d d        S # 1 sw Y   y xY w#  Y yxY w)Nz/proc/uptimer   )openreadlinesplit)fhuptime_s      r   r3   z$ProvisionHandler._get_uptime_secondsl   sJ    	n% KKM//1	  	s$   A #:	A AA A A
c                    t        j                         }t        j                         rt        j                  t        j
                                t        j                         dk(  r&	 t        j                  d       | j                         S d}t        j                  |j                  |t        j                                      | j                         S )Nautozssh-keygen -Azssh-keygen -N '' -t {0} -f {1})r#   get_ssh_host_keypair_typeget_regenerate_ssh_host_keyfileutilrm_filesget_ssh_key_globget_ssh_host_keypair_mode	shellutilr<   r2   get_ssh_key_private_pathget_ssh_host_key_thumbprint)r   keypair_type
keygen_cmds      r   r/   z!ProvisionHandler.reg_ssh_host_keyu   s    557++-d3356--/69
 o. //11	 >
j$f\%)%B%B%DFG //11r   c                 0   dj                  t        j                               }t        j                  ||      }|d   dk(  r4|d   j                         j                         d   j                  dd      S t        dj                  |d   |d               )Nzssh-keygen -lf {0})chk_errr      : z2Failed to generate ssh host key: ret={0}, out= {1})	r2   r#   get_ssh_key_public_pathrL   run_get_outputrstripr@   replacer   )r   rR   cmdrets       r   rN   z,ProvisionHandler.get_ssh_host_key_thumbprint   s    "))$*F*F*HI&&sG<q6Q;q6==?((*1-55c2>>  #67=vc!fc!f7MO Or   c                  n    t         j                  j                  t        j                         t
              S r   )ospathjoinr#   get_lib_dirPROVISIONED_FILE r   r   provisioned_file_pathz&ProvisionHandler.provisioned_file_path   s!    ww||D,,.0@AAr   c                  d    t         j                  j                  t        j	                               S )z
        A VM is considered provisioned *anytime* the provisioning
        sentinel file exists and not provisioned *anytime* the file
        is absent.
        )r]   r^   isfiler   rc   rb   r   r   is_provisionedzProvisionHandler.is_provisioned   s!     ww~~.DDFGGr   c                    t         j                         syt        j                  t         j	                               j                         }| j                  j                  |      s`t        |      dkD  r2t        j                  d       ddlm}  |       }|j                          | j                          | j                          y)a`  
        If the VM was provisioned using an agent that did not record
        the VM unique identifier, the provisioning file will be re-written
        to include the identifier.

        A warning is logged *if* the VM unique identifier has changed
        since VM was provisioned.

        Returns False if the VM has not been provisioned.
        Fr   zTVM is provisioned, but the VM unique identifier has changed -- clearing cached state)get_deprovision_handlerT)r   rf   rH   	read_filerc   stripr   is_current_instance_idlenr%   warnazurelinuxagent.pa.deprovisionrh   run_changed_unique_idr'   r(   )r   srh   deprovision_handlers       r   r*   z'ProvisionHandler.check_provisioned_file   s      ..0/EEGHNNP{{11!41vz 4 53&=&?##99;""$r   c                     t        j                  t        j                         t	               j                                y r   )rH   
write_filer   rc   r   get_instance_idr   s    r   r'   z"ProvisionHandler.write_provisioned   s*    224L((*	,r   c                  ~    t        j                  d       t        j                  t	        j
                         d       y )Nz4Disabling guest agent in accordance with ovf-env.xmlrU   )r%   rm   rH   rs   r#   get_disable_agent_file_pathrb   r   r   write_agent_disabledz%ProvisionHandler.write_agent_disabled   s(    JKD<<>Cr   c                     | j                  |ddt        j                         |r%|j                         dk(  r| j	                          y y y )NTr   )messager    r!   	operationfalse)r1   r   ProvisionGuestAgentlowerrw   )r   r5   s     r   r4   z-ProvisionHandler.handle_provision_guest_agent   sS    "7%)#$$6$J$J 	 	L !%:%@%@%Bg%M%%' &N r   c                    t        j                  d       	 t        j                  dj                  |j                               | j                  j                  |j                         t        j                  dj                  |j                               | j                  j                  |j                         | j                  |       | j                  |       t        j                         r| j                  j                          y y # t        $ r(}t        dj                  t        |                  d }~ww xY w)NzHandle ovf-env.xml.zSet hostname [{0}]zPublish hostname [{0}]zFailed to provision: {0})r%   r&   r2   hostnamer   set_hostnamepublish_hostnameconfig_user_accountsave_customdatar#   get_delete_root_passworddel_root_passwordr
   r   r   )r   ovfenvr:   s      r   r.   zProvisionHandler.provision   s    )*	MKK,33FOODEKK$$V__5KK077HIKK((9$$V,  (,,.--/ /  	M !;!B!B47!KLL	Ms   C6D 	E #D;;E c                    t        j                  d       | j                  j                  |j                         |j
                  pt        j                  d       t        j                         }t        j                         }| j                  j                  |j                  |j
                  ||       t        j                  d       | j                  j                  |j                  |j
                  d u        t        j                  d       | j                  j                  |j                         | j                  |       | j                  |       y )Nz!Create user account if not existszSet user password.)crypt_idsalt_lenzConfigure sudoer)nopasswdzConfigure sshd)r%   r&   r   useraddusernameuser_passwordr#   get_password_cryptidget_password_crypt_salt_lenchpasswdconf_sudoer	conf_sshddisable_ssh_password_authdeploy_ssh_pubkeysdeploy_ssh_keypairs)r   r   r   r   s       r   r   z$ProvisionHandler.config_user_account   s   78FOO,+KK,-002H779HKK  &2F2F*2X ! G 	&')/)=)=)E 	  	G 	$%f>>?'  (r   c                    |j                   }|y t        j                         }t        j                         st        j                         r0t        j                  d       | j                  j                  |      }t        j                  d       t        j                  j                  |t              }t        j                  ||       t        j                         rt        j                         }t        j                  d       t        j                   |d       t#        j$                  |       t'        t(        t+        t        j                         |z
        dt,        j.                         y y )NzDecode custom datazSave custom datazExecute custom datai  T)namer!   r    op)
customdatar#   r`   get_decode_customdataget_execute_customdatar%   r&   r   decode_customdatar]   r^   r_   CUSTOM_DATA_FILErH   rs   timechmodrL   r<   r   r   intr   r   )r   r   r   lib_dircustomdata_filestarts         r   r   z ProvisionHandler.save_customdata   s    &&
""$%%'4+F+F+HKK,-66zBJ&''',,w0@AOZ8&&(IIKEKK-.HH_e,MM/*:!$TYY[5%8!9#'-88: )r   c                     |j                   D ]=  }t        j                  d       | j                  j	                  |j
                  |       ? y )NzDeploy ssh public key.)ssh_pubkeysr%   r&   r   deploy_ssh_pubkeyr   )r   r   pubkeys      r   r   z#ProvisionHandler.deploy_ssh_pubkeys  s>    (( 	CFKK01KK))&//6B	Cr   c                     |j                   D ]=  }t        j                  d       | j                  j	                  |j
                  |       ? y )NzDeploy ssh key pairs.)ssh_keypairsr%   r&   r   deploy_ssh_keypairr   )r   r   keypairs      r   r   z$ProvisionHandler.deploy_ssh_keypairs  s>    ** 	EGKK/0KK**6??GD	Er   Fr   c                 ,    t        t        ||||       y )N)r   ry   r!   r    r   )r   r   )r   ry   r    r!   rz   s        r   r1   zProvisionHandler.report_event  s    z#%) 		"r   c                    t        d||      }	 | j                  j                         }|j                  |       y # t        $ r:}t        j                  d|       | j                  t        |             Y d }~y d }~ww xY w)NNotReady)status	subStatusdescriptionzReporting NotReady failed: {0}	r   r   r,   report_provision_statusr	   r%   r6   r1   r   )r   
sub_statusr   r   protocolr:   s         r   r-   z!ProvisionHandler.report_not_ready   sn     
j-8:	'))668H,,V4 	'LL91=d1g&&	's   +< 	A?0A::A?c                     t        d      }	 | j                  j                         }|j                  |       y # t        $ r:}t        j                  d|       | j                  t        |             Y d }~y d }~ww xY w)NReady)r   zReporting Ready failed: {0}r   )r   r   r   r:   s       r   r(   zProvisionHandler.report_ready*  sg     0	'))668H,,V4 	'LL6:d1g&&	's   +: 	A=0A88A=N)T)__name__
__module____qualname__r   r<   staticmethodr3   r/   rN   rc   rf   r*   r'   rw   r4   r.   r   r   r   r   r   	Provisionr1   r-   r(   rb   r   r   r   r   4   s    11f  2&O B B H H<,
 D D(M&)*:0C
E
 05q1;;"''r   r   )+__doc__r]   os.pathrer   r   azurelinuxagent.common.confcommonr#   azurelinuxagent.common.loggerr%   &azurelinuxagent.common.utils.shellutilutilsrL   %azurelinuxagent.common.utils.fileutilrH   azurelinuxagent.common.futurer   azurelinuxagent.common.eventr   r   r    azurelinuxagent.common.exceptionr   r	   r
   azurelinuxagent.common.osutilr   'azurelinuxagent.common.protocol.restapir   $azurelinuxagent.common.protocol.utilr   azurelinuxagent.common.versionr   ,azurelinuxagent.pa.provision.cloudinitdetectr   r   CLOUD_INIT_PATTERNcompileCLOUD_INIT_REGEXra   objectr   rb   r   r   <module>r      s   $ 
  	   * * . . : : 8 8 .   4 C B 5 N + 2::01   }'v }'r   