o
    .&ßa  ã                   @   sT   d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
mZ G dd„ deƒZdS )é    N)ÚBasicCommand)ÚDEFAULT_CONFIG_FILE)Úvalidate_regionÚvalidate_instance_nameÚvalidate_tagsÚvalidate_iam_user_arnÚINSTANCE_NAME_ARGÚIAM_USER_ARN_ARGc                   @   sŠ   e Zd ZdZdZddddddœd	dddœd
œdœdœZeddddeddœegZdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!S )"ÚRegisterÚregistera   Creates an IAM user for the on-premises instance, if not provided, and saves the user's credentials to an on-premises instance configuration file; registers the on-premises instance with AWS CodeDeploy; and optionally adds tags to the on-premises instance.ÚarrayÚobjectzThe tag key.ÚstringT)ÚdescriptionÚtypeÚrequiredzThe tag value.)ÚKeyÚValue)r   Ú
properties)r   ÚitemsÚtagsz--tags <value>Fú+zFOptional. The list of key/value pairs to tag the on-premises instance.)ÚnameÚsynopsisr   ÚnargsÚschemaÚ	help_textc              
   C   s  |}| j |_t||ƒ t|ƒ t|ƒ t|ƒ | j jd|j|j|j	d�| _
| j jd|jd�| _z0|jsG|  |¡ |  |¡ |  |¡ |  |¡ |  |¡ |jrT|  |¡ tj d t¡¡ W d S  ty€ } ztj ¡  tj d |¡¡ W Y d }~d S d }~ww )NÚ
codedeploy)Úregion_nameÚendpoint_urlÚverifyÚiam)r   záCopy the on-premises configuration file named {0} to the on-premises instance, and run the following command on the on-premises instance to install and configure the AWS CodeDeploy Agent:
aws deploy install --config-file {0}
z²ERROR
{0}
Register the on-premises instance by following the instructions in "Configure Existing On-Premises Instances by Using AWS CodeDeploy" in the AWS CodeDeploy User Guide.
)Ú_sessionÚsessionr   r   r   r   Úcreate_clientÚregionr   Ú
verify_sslr   r!   Úiam_user_arnÚ_create_iam_userÚ_create_access_keyÚ_create_user_policyÚ_create_configÚ_register_instancer   Ú	_add_tagsÚsysÚstdoutÚwriteÚformatr   Ú	ExceptionÚflushÚstderr)ÚselfÚparsed_argsÚparsed_globalsÚparamsÚe© r:   úK/usr/lib/python3/dist-packages/awscli/customizations/codedeploy/register.pyÚ	_run_mainD   sN   
üþ





ÿ
û	
ú€þzRegister._run_mainc                 C   sL   t j d¡ |j|_| jjd|jd�}|d d |_t j d |j¡¡ d S )NzCreating the IAM user... z/AWS/CodeDeploy/)ÚPathÚUserNameÚUserÚArnzDONE
IamUserArn: {0}
)	r.   r/   r0   Úinstance_nameÚ	user_namer!   Úcreate_userr'   r1   ©r5   r8   Úresponser:   r:   r;   r(   t   s   þÿþzRegister._create_iam_userc                 C   sT   t j d¡ | jj|jd�}|d d |_|d d |_t j d |j|j¡¡ d S )Nz$Creating the IAM user access key... )r>   Ú	AccessKeyÚAccessKeyIdÚSecretAccessKeyz+DONE
AccessKeyId: {0}
SecretAccessKey: {1}
)	r.   r/   r0   r!   Úcreate_access_keyrB   Úaccess_key_idÚsecret_access_keyr1   rD   r:   r:   r;   r)   ƒ   s   ÿþýzRegister._create_access_keyc                 C   sL   t j d¡ d|_d|_| jj|j|j|jd� t j d |j|j¡¡ d S )Nz Creating the IAM user policy... zcodedeploy-agentz�{
    "Version": "2012-10-17",
    "Statement": [ {
        "Action": [ "s3:Get*", "s3:List*" ],
        "Effect": "Allow",
        "Resource": "*"
    } ]
})r>   Ú
PolicyNameÚPolicyDocumentz)DONE
PolicyName: {0}
PolicyDocument: {1}
)	r.   r/   r0   Úpolicy_nameÚpolicy_documentr!   Úput_user_policyrB   r1   ©r5   r8   r:   r:   r;   r*   “   s    ÿ
ýþýzRegister._create_user_policyc              	   C   sj   t j d t¡¡ ttdƒ�}| d |j|j|j|j	¡¡ W d   ƒ n1 s(w   Y  t j d¡ d S )NzACreating the on-premises instance configuration file named {0}...ÚwzT---
region: {0}
iam_user_arn: {1}
aws_access_key_id: {2}
aws_secret_access_key: {3}
úDONE
)
r.   r/   r0   r1   r   Úopenr%   r'   rJ   rK   )r5   r8   Úfr:   r:   r;   r+   ®   s    þüûÿzRegister._create_configc                 C   s0   t j d¡ | jj|j|jd� t j d¡ d S )Nz(Registering the on-premises instance... )ÚinstanceNameÚ
iamUserArnrS   )r.   r/   r0   r   Úregister_on_premises_instancerA   r'   rQ   r:   r:   r;   r,   Â   s   þzRegister._register_instancec                 C   s2   t j d¡ | jj|j|jgd� t j d¡ d S )Nz+Adding tags to the on-premises instance... )r   ÚinstanceNamesrS   )r.   r/   r0   r   Ú!add_tags_to_on_premises_instancesr   rA   rQ   r:   r:   r;   r-   Ê   s   þzRegister._add_tagsN)Ú__name__Ú
__module__Ú__qualname__ÚNAMEÚDESCRIPTIONÚTAGS_SCHEMAr   r	   Ú	ARG_TABLEr<   r(   r)   r*   r+   r,   r-   r:   r:   r:   r;   r
      sD    ÿýýúþþùó0r
   )r.   Úawscli.customizations.commandsr   Ú(awscli.customizations.codedeploy.systemsr   Ú&awscli.customizations.codedeploy.utilsr   r   r   r   r   r	   r
   r:   r:   r:   r;   Ú<module>   s
    