o
    !d;                     @   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                   @   sX  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/d0dd(d,d-d	d1d/d2dd3dd4ddddidd5d6d7ddd7d8d	d9d/d:dd(d7d8d	d;d/edddddidg d<d!gZd=d> Z	dFd?d@ZdAdB ZdCdD ZdES )GDeployCommandzChangeset 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storezThe 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-bucketFzThe 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_truezIndicates 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-prefixzA 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   capabilitiesZCAPABILITY_IAMZ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disable-rollbackdisable_rollbackzcPreserve the state of previously provisioned resources when the execute-change-set operation fails.)r   r   r   Z
group_namer   r   r   zno-disable-rollbackzKRoll back all resource changes when the execute-change-set operation fails.zrole-arnzThe 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_changesetzSpecify 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.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.zA 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   sR  | 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$|j%S )Nr   )region_nameendpoint_urlverify)template_pathrc                 S   s   g | ]	\}}||d qS ))KeyValue ).0keyvaluer(   r(   Ousr/lib/python3.10/site-packages/awscli/customizations/cloudformation/deploy.py
<listcomp>  s    z+DeployCommand._run_main.<locals>.<listcomp>i   Zs3Zs3v4)Zsignature_version)configr!   r#   )&Z_sessionZcreate_clientZregionr"   Z
verify_sslZtemplate_fileospathisfiler   ZInvalidTemplatePathErroropenread
stack_nameparse_key_value_argparameter_overridesPARAMETER_OVERRIDE_CMDr   TAGS_CMDr   r   merge_parametersgetsizeZ	s3_bucketZDeployBucketRequiredErrorr   r   Z	s3_prefixZ
kms_key_idZforce_uploadr   r   r   r   role_arnnotification_arnsr    r   )selfZparsed_argsZparsed_globalsZcloudformation_clientr$   handletemplate_strr4   r6   Z	tags_dictr   template_dict
parametersZtemplate_sizebucketZ	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 |rK||j|| |||j t	j
| jj|d nt	j
| jj|jd t	j
  dS )N)r4   Zcfn_templateparameter_valuesr   r;   r<   rC   r   )outfiler   )r4   )changeset_id)Zcreate_and_wait_for_changesetr   ZChangeEmptyErrorr	   r   r   rH   Zwait_for_executeZchangeset_typesysstdoutwriteMSG_EXECUTE_SUCCESSformatMSG_NO_EXECUTE_CHANGESETflush)r=   rD   r4   r?   rA   r   r   r;   r<   rC   r   r    r   resultexr(   r(   r,   r   A  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:
        Z
ParametersNZParameterKeyZParameterValueTZUsePreviousValue)
isinstancegetdictr   append)r=   r@   r6   rF   r*   r+   objr(   r(   r,   r9   c  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   Z InvalidKeyValuePairArgumentError)r=   	arg_valuerZ   rP   dataZkey_value_pairr(   r(   r,   r5     s   
z!DeployCommand.parse_key_value_argN)TF)__name__
__module____qualname__rN   rL   r7   r8   NAMEr   Z	FROM_FILEZDESCRIPTIONZ	ARG_TABLErE   r   r9   r5   r(   r(   r(   r,   r
       s    	


!
  V<
"r
   )r/   rI   loggingZbotocore.clientr   Z$awscli.customizations.cloudformationr   Z-awscli.customizations.cloudformation.deployerr   Z awscli.customizations.s3uploaderr   Z/awscli.customizations.cloudformation.yamlhelperr   Zawscli.customizations.commandsr   Zawscli.compatr   Zawscli.utilsr	   	getLoggerr_   ZLOGr
   r(   r(   r(   r,   <module>   s   
