o
    .&ßaõ7  ã                   @   s–   d dl Z d dlZd dl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 dlmZ d dlmZ d d	lmZ e e¡ZG d
d„ deƒZdS )é    N)ÚConfig)Ú
exceptions)ÚDeployer)Ú
S3Uploader)Ú
yaml_parse)ÚBasicCommand)Úget_stdout_text_writer)Úwrite_exceptionc                   @   s4  e Zd ZdZdZdZdZdZe 	dd¡Z
dd	d
dœddd	ddœddddœddddœdddœdddœedddddidœg d d!œd"ddddd#d$gd%œdœg d&d!œd'd(d)dd*d+œd,dd-dœd.ddddidœd/d0œd1ddd1d2d	d3d4œd5dd(d1d2d	d6d4œedddddidœg d7d!œgZd8d9„ Z		dAd:d;„Zd<d=„ Zd>d?„ Zd@S )BÚDeployCommandz•Changeset created successfully. Run the following command to review changes:
aws cloudformation describe-change-set --change-set-name {changeset_id}
z2Successfully created/updated stack - {stack_name}
zparameter-overridesÚtagsÚdeployÚcloudformationz_deploy_description.rstztemplate-fileTz;The path where your AWS CloudFormation template is located.)ÚnameÚrequiredÚ	help_textz
stack-nameÚstorez²The name of the AWS CloudFormation stack you're deploying to. If you specify an existing stack, the command updates the stack. If you specify a new stack, the command creates it.)r   Úactionr   r   z	s3-bucketFz The name of the S3 bucket where this command uploads your CloudFormation template. This is required the deployments of templates sized greater than 51,200 byteszforce-uploadÚ
store_truez�Indicates whether to override existing files in the S3 bucket. Specify this flag to upload artifacts even if they  match existing artifacts in the S3 bucket.)r   r   r   z	s3-prefixz¡A prefix name that the command adds to the artifacts' name when it uploads them to the S3 bucket. The prefix name is a path name (folder name) for the S3 bucket.)r   r   z
kms-key-idzfThe ID of an AWS KMS key that the command uses to encrypt artifacts that are at rest in the S3 bucket.ÚarrayÚtypeÚstring)r   ÚitemsaU  A list of parameter structures that specify input parameters for your stack template. If you're updating a stack and you don't specify a parameter, the command uses the stack's existing value. For new stacks, you must specify parameters that don't have a default value. Syntax: ParameterKey1=ParameterValue1 ParameterKey2=ParameterValue2 ...)r   r   r   ÚschemaÚdefaultr   ÚcapabilitiesÚCAPABILITY_IAMÚCAPABILITY_NAMED_IAM)r   Úenuma¡  A list of capabilities that you must specify before AWS Cloudformation can create certain stacks. Some stack templates might include resources that can affect permissions in your AWS account, for example, by creating new AWS Identity and Access Management (IAM) users. For those stacks, you must explicitly acknowledge their capabilities by specifying this parameter.  The only valid values are CAPABILITY_IAM and CAPABILITY_NAMED_IAM. If you have IAM resources, you can specify either capability. If you have IAM resources with custom names, you must specify CAPABILITY_NAMED_IAM. If you don't specify this parameter, this action returns an InsufficientCapabilities error.zno-execute-changesetÚstore_falseÚexecute_changeseta1  Indicates whether to execute the change set. Specify this flag if you want to view your stack changes before executing the change set. The command creates an AWS CloudFormation change set and then exits without executing the change set. After you view the change set, execute it to implement your changes.)r   r   Údestr   r   zrole-arnz‘The Amazon Resource Name (ARN) of an AWS Identity and Access Management (IAM) role that AWS CloudFormation assumes when executing the change set.znotification-arnszxAmazon Simple Notification Service topic Amazon Resource Names (ARNs) that AWS CloudFormation associates with the stack.)r   r   r   r   zfail-on-empty-changesetÚfail_on_empty_changesetzžSpecify if the CLI should return a non-zero exit code if there are no changes to be made to the stack. The default behavior is to return a non-zero exit code.)r   r   r   Ú
group_namer    r   r   zno-fail-on-empty-changesetz[Causes the CLI to return an exit code of 0 if there are no changes to be made to the stack.zÛA list of tags to associate with the stack that is created or updated. AWS CloudFormation also propagates these tags to resources in the stack if the resource supports it. Syntax: TagKey1=TagValue1 TagKey2=TagValue2 ...c                 C   sN  | j jd|j|j|jd�}|j}tj |¡st	j
|d�‚t|dƒ�}| ¡ }W d   ƒ n1 s0w   Y  |j}|  |j| j¡}|  |j| j¡}	dd„ |	 ¡ D ƒ}
t|ƒ}|  ||¡}tj |j¡}|dkrm|jsmt	 ¡ ‚|j}|r�| j jdtd	d
�|j|jd�}t|||j|j|jƒ}nd }t|ƒ}|  |||||j |j!|j"|j#||
|j$¡S )Nr   )Úregion_nameÚendpoint_urlÚverify)Útemplate_pathÚrc                 S   s   g | ]	\}}||d œ‘qS ))ÚKeyÚValue© )Ú.0ÚkeyÚvaluer*   r*   úM/usr/lib/python3/dist-packages/awscli/customizations/cloudformation/deploy.pyÚ
<listcomp>  s    ÿz+DeployCommand._run_main.<locals>.<listcomp>i È  Ús3Ús3v4)Úsignature_version)Úconfigr#   r%   )%Ú_sessionÚcreate_clientÚregionr$   Ú
verify_sslÚtemplate_fileÚosÚpathÚisfiler   ÚInvalidTemplatePathErrorÚopenÚreadÚ
stack_nameÚparse_key_value_argÚparameter_overridesÚPARAMETER_OVERRIDE_CMDr   ÚTAGS_CMDr   r   Úmerge_parametersÚgetsizeÚ	s3_bucketÚDeployBucketRequiredErrorr   r   Ú	s3_prefixÚ
kms_key_idÚforce_uploadr   r   r   r   Úrole_arnÚnotification_arnsr!   )ÚselfÚparsed_argsÚparsed_globalsÚcloudformation_clientr&   ÚhandleÚtemplate_strr?   rA   Ú	tags_dictr   Útemplate_dictÚ
parametersÚtemplate_sizeÚbucketÚ	s3_clientÚs3_uploaderÚdeployerr*   r*   r.   Ú	_run_mainð   sf   ýÿÿ
ÿþÿüü
ûzDeployCommand._run_mainc              
   C   sº   z|j |||||||	|
d�}W n tjy- } z|r‚ t|tƒ d� W Y d }~dS d }~ww |rJ| |j|¡ | ||j¡ t	j
 | jj|d�¡ nt	j
 | jj|jd�¡ t	j
 ¡  dS )N)r?   Úcfn_templateÚparameter_valuesr   rK   rL   rY   r   )Úoutfiler   )r?   )Úchangeset_id)Úcreate_and_wait_for_changesetr   ÚChangeEmptyErrorr	   r   r   r_   Úwait_for_executeÚchangeset_typeÚsysÚstdoutÚwriteÚMSG_EXECUTE_SUCCESSÚformatÚMSG_NO_EXECUTE_CHANGESETÚflush)rM   rZ   r?   rR   rU   r   r   rK   rL   rY   r   r!   ÚresultÚexr*   r*   r.   r   )  s:   
ø
€ü
ÿÿ
zDeployCommand.deployc                 C   sd   g }t | dd¡tƒs|S |d  ¡ D ]\}}d|i}||v r&|| |d< nd|d< | |¡ q|S )aƒ  
        CloudFormation CreateChangeset requires a value for every parameter
        from the template, either specifying a new value or use previous value.
        For convenience, this method will accept new parameter values and
        generates a dict of all parameters in a format that ChangeSet API
        will accept

        :param parameter_overrides:
        :return:
        Ú
ParametersNÚParameterKeyÚParameterValueTÚUsePreviousValue)Ú
isinstanceÚgetÚdictr   Úappend)rM   rT   rA   r]   r,   r-   Úobjr*   r*   r.   rD   J  s   ÿzDeployCommand.merge_parametersc                 C   sH   i }|D ]}|  dd¡}t|ƒdkrtj||d�‚|d ||d < q|S )af  
        Converts arguments that are passed as list of "Key=Value" strings
        into a real dictionary.

        :param arg_value list: Array of strings, where each string is of
            form Key=Value
        :param argname string: Name of the argument that contains the value
        :return dict: Dictionary representing the key/value pairs
        ú=é   é   )Úargnamer-   r   )ÚsplitÚlenr   Ú InvalidKeyValuePairArgumentError)rM   Ú	arg_valuery   rk   ÚdataÚkey_value_pairr*   r*   r.   r@   i  s   
þz!DeployCommand.parse_key_value_argN)T)Ú__name__Ú
__module__Ú__qualname__ri   rg   rB   rC   ÚNAMEr   Ú	FROM_FILEÚDESCRIPTIONÚ	ARG_TABLEr[   r   rD   r@   r*   r*   r*   r.   r
       sØ    ÿÿü	ûü
ü
ýýÿþôþþþ
ð!úü
ÿþöøøÿþô �Ö ><
ý!r
   )r9   rd   ÚloggingÚbotocore.clientr   Ú$awscli.customizations.cloudformationr   Ú-awscli.customizations.cloudformation.deployerr   Ú awscli.customizations.s3uploaderr   Ú/awscli.customizations.cloudformation.yamlhelperr   Úawscli.customizations.commandsr   Úawscli.compatr   Úawscli.utilsr	   Ú	getLoggerr€   ÚLOGr
   r*   r*   r*   r.   Ú<module>   s   
