o
    !d                     @   sn   d dl Z d dlZd dlZdZG dd dZG dd deZG dd deZG d	d
 d
eZG dd deZdS )    Nzcodedeploy.onpremises.ymlc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )SystemzgOnly Ubuntu Server, Red Hat Enterprise Linux Server and Windows Server operating systems are supported.c                 C   s    |j | _ | j jd|jd| _d S )Ns3)Zregion_name)sessionZcreate_clientregionr   selfparams r	   Lusr/lib/python3.10/site-packages/awscli/customizations/codedeploy/systems.py__init__   s
   zSystem.__init__c                 C      t d)Nvalidate_administratorNotImplementedErrorr   r	   r	   r
   r   "      zSystem.validate_administratorc                 C   r   )Ninstallr   r   r	   r	   r
   r   %   r   zSystem.installc                 C   r   )N	uninstallr   r   r	   r	   r
   r   (   r   zSystem.uninstallN)__name__
__module____qualname__ZUNSUPPORTED_SYSTEM_MSGr   r   r   r   r	   r	   r	   r
   r      s    r   c                   @   sD   e Zd ZdZdZdeeZdZdd Zdd Z	d	d
 Z
dd ZdS )Windowsz C:\ProgramData\Amazon\CodeDeployzconf.onpremises.ymlz{0}\{1}zcodedeploy-agent.msic                 C   s   t jj s
tdd S )Nz.You must run this command as an Administrator.)ctypeswindllshell32ZIsUserAnAdminRuntimeErrorr   r	   r	   r
   r   2   s
   zWindows.validate_administratorc                 C   s  d|v r|j | _tjg dtjtjd}| \}}d}|jdkr,||vr,td|| j	j
|j|jd}t| jd}||d	   W d    n1 sPw   Y  tjd
| jdddgdd tg d tjg dtjtjd}| \}}d|vrtdd S )N	installerpowershell.exe-CommandzStop-Service-Namecodedeployagentstdoutstderr;Cannot find any service with service name 'codedeployagent'r   ,Failed to stop the AWS CodeDeploy Agent:
{0}ZBucketKeywbBodyz.\{0}z/quietz/lz".\codedeploy-agent-install-log.txtT)shell)r   r   zRestart-Servicer    r!   )r   r   zGet-Servicer    r!   ZRunningz:The AWS CodeDeploy Agent did not start after installation.)r   	INSTALLER
subprocessPopenPIPEcommunicate
returncoder   formatr   
get_objectbucketkeyopenwriteread
check_call)r   r   processoutputerror	not_foundresponsefr	   r	   r
   r   8   sJ   	
	zWindows.installc                 C   sX   t jg dt jt jd}| \}}d}|jdkr|   d S ||vr*td|d S )Nr   r"   r%   r   r&   )r-   r.   r/   r0   r1   _remove_agentr   r2   )r   r   r:   r;   r<   r=   r	   r	   r
   r   o   s   	
zWindows.uninstallc                 C   s@   t jg dt jt jd}| \}}|jdkrtd|d S )N)Zwmicproductwherezname="CodeDeploy Host Agent"callr   z/nointeractiver"   r   z1Failed to uninstall the AWS CodeDeploy Agent:
{0})r-   r.   r/   r0   r1   r   r2   )r   r:   r;   r<   r	   r	   r
   r@      s   	
zWindows._remove_agentN)r   r   r   
CONFIG_DIRCONFIG_FILEr2   CONFIG_PATHr,   r   r   r   r@   r	   r	   r	   r
   r   ,   s    7r   c                   @   sT   e Zd ZdZeZdeeZdZdd Z	dd Z
dd	 Zd
d Zdd Zdd ZdS )Linuxz/etc/codedeploy-agent/confz{0}/{1}r   c                 C   s   t  dkr
tdd S )Nr   z"You must run this command as sudo.)osgeteuidr   r   r	   r	   r
   r      s   zLinux.validate_administratorc                 C   s   d|v r|j | _| | | | | jj|j|jd}t| jd}|	|d 
  W d    n1 s6w   Y  tddd| jg | j }tj }|j|d< |j|d	< |j|d
< |jd urj|j|d< tjd| jdg|d d S )Nr   r'   r)   r*   chmodz+xz./{0}Z
AWS_REGIONZAWS_ACCESS_KEY_IDZAWS_SECRET_ACCESS_KEYZAWS_SESSION_TOKENauto)env)r   r,   _update_system_stop_agentr   r3   r4   r5   r6   r7   r8   r-   r9   r2   r   Zget_credentialsrH   environcopyr   Z
access_keyZ
secret_keytoken)r   r   r>   r?   Zcredentialsenvironmentr	   r	   r
   r      s,   









zLinux.installc                 C   s&   |  |}|jdkr| | d S d S )Nr   )rN   r1   r@   )r   r   r:   r	   r	   r
   r      s   

zLinux.uninstallc                 C   r   )NZ
preinstallr   r   r	   r	   r
   rM      r   zLinux._update_systemc                 C   r   )NZremove_agentr   r   r	   r	   r
   r@      r   zLinux._remove_agentc                 C   sJ   t jg dt jt jd}| \}}|jdkr#|j|vr#td||S )N)Zservicecodedeploy-agentstopr"   r   r&   )r-   r.   r/   r0   r1   not_found_msgr   r2   )r   r   r:   r;   r<   r	   r	   r
   rN      s   zLinux._stop_agentN)r   r   r   rD   DEFAULT_CONFIG_FILErE   r2   rF   r,   r   r   r   rM   r@   rN   r	   r	   r	   r
   rG      s    rG   c                   @   $   e Zd Zdd Zdd Zdd ZdS )Ubuntuc                 C   s    t g d t g d d S )N)apt-get-yupdate)rY   rZ   r   zruby2.0r-   r9   r   r	   r	   r
   rM      s   zUbuntu._update_systemc                 C      t g d d S )N)Zdpkgz-rrS   r\   r   r	   r	   r
   r@         zUbuntu._remove_agentc                 C      d|_ t| |S )Nz&codedeploy-agent: unrecognized servicerU   rG   rN   r   r	   r	   r
   rN         zUbuntu._stop_agentNr   r   r   rM   r@   rN   r	   r	   r	   r
   rX      s    rX   c                   @   rW   )RHELc                 C   r]   )N)yumrZ   r   Zrubyr\   r   r	   r	   r
   rM      r^   zRHEL._update_systemc                 C   r]   )N)rd   rZ   ZeraserS   r\   r   r	   r	   r
   r@      r^   zRHEL._remove_agentc                 C   r_   )Nz<Redirecting to /bin/systemctl stop  codedeploy-agent.servicer`   r   r	   r	   r
   rN      ra   zRHEL._stop_agentNrb   r	   r	   r	   r
   rc      s    rc   )	r   rH   r-   rV   r   r   rG   rX   rc   r	   r	   r	   r
   <module>   s   k>