o .&aD@sddlZddlZddlZddlZddlmZmZddlmZddl m Z ddl m Z m Z ddlmZdZdZd ZGd d d eZGd d d ZGdddZGdddZdS)N)compatconfig) ClientError) compat_open) exceptions filehelpers) BasicCommand ihc @seZdZdZdZddddddddd d ddd d d dddd dddd dgZdZdZdZdZ ddZ ddZ ddZ ddZ ddZd d!Zd"d#Zd$S)% ECSDeploydeployaDeploys a new task definition to the specified ECS service. Only services that use CodeDeploy for deployments are supported. This command will register a new task definition, update the CodeDeploy appspec with the new task definition revision, create a CodeDeploy deployment, and wait for the deployment to successfully complete. This command will exit with a return code of 255 if the deployment does not succeed within 30 minutes by default or up to 10 minutes more than your deployment group's configured wait time (max of 6 hours).servicezJThe short name or full Amazon Resource Name (ARN) of the service to updateT)name help_textrequiredztask-definitionzThe file path where your task definition file is located. The format of the file must be the same as the JSON output of: aws ecs register-task-definition --generate-cli-skeletoncodedeploy-appspecaThe file path where your AWS CodeDeploy appspec file is located. The appspec file may be in JSON or YAML format. The TaskDefinition property will be updated within the appspec with the newly registered task definition ARN, overwriting any placeholder values in the file.clusterzThe short name or full Amazon Resource Name (ARN) of the cluster that your service is running within. If you do not specify a cluster, the "default" cluster is assumed.Fzcodedeploy-applicationzThe name of the AWS CodeDeploy application to use for the deployment. The specified application must use the 'ECS' compute platform. If you do not specify an application, the application name AppECS-[CLUSTER_NAME]-[SERVICE_NAME] is assumed.zcodedeploy-deployment-groupa*The name of the AWS CodeDeploy deployment group to use for the deployment. The specified deployment group must be associated with the specified ECS service and cluster. If you do not specify a deployment group, the deployment group name DgpECS-[CLUSTER_NAME]-[SERVICE_NAME] is assumed.z6Successfully registered new ECS task definition {arn} z%Successfully created deployment {id} z8Successfully deployed {task_def} to service '{service}' zcustomization/ecs-deploycCs||j|j\}}t|j|||j}||||_|jjd|j |j t j |jdd}| ||j|_||||_|||dS)N codedeployuser_agent_extra) region_nameverifyr)_load_file_argstask_definitioncodedeploy_appspec ECSClient_sessionUSER_AGENT_EXTRA_get_resource_names resources create_clientregion verify_sslrConfig_validate_code_deploy_resources _cd_validatorget_deployment_wait_time wait_time_register_task_def task_def_arn_create_and_wait_for_deployment)self parsed_argsparsed_globalsregister_task_def_kwargs appspec_objecs_client_wrappercodedeploy_clientr2B/usr/lib/python3/dist-packages/awscli/customizations/ecs/deploy.py _run_mainrs,    zECSDeploy._run_maincCst||}||j||jd|jd}tj|jj |d| ||j |jd}tj|j j |j|dtj dS)Napp_namedeployment_group_name)idr )task_defr ) CodeDeployerupdate_task_def_arnr)create_deploymentrsysstdoutwriteMSG_CREATED_DEPLOYMENTformatwait_for_deploy_successr' MSG_SUCCESSflush)r+clientappspecdeployer deployment_id service_namer2r2r3r*s"    z)ECSDeploy._create_and_wait_for_deploymentc Csztjtj|}zt| }|WdWS1swYWdStttfy<}zt j ||dd}~ww)N) file_patherror) ospath expandvars expanduserrreadOSErrorIOErrorUnicodeDecodeErrorr FileLoadError)r+rI full_pathfer2r2r3_get_file_contentss (zECSDeploy._get_file_contentscCsR|}|d}|d}t|||j}t|||j}||d||d||dS)NrH cluster_name service_arn cluster_arn)r rYrrZr5r6)get_service_detailsr get_app_namecodedeploy_applicationget_deploy_group_namecodedeploy_deployment_group)r+args ecs_clientservice_detailsrHrXapplication_namer6r2r2r3rs zECSDeploy._get_resource_namescCs0||}t|}||}t|}||fSN)rWjsonloadsr parse_appspec)r+ task_def_arg appspec_argtask_def_stringr.appspec_stringr/r2r2r3rs    zECSDeploy._load_file_argscCs:||}|dd}tj|jj|dtj|S)NtaskDefinitiontaskDefinitionArn)arn)register_task_definitionr<r=r>MSG_TASK_DEF_REGISTEREDr@rC)r+task_def_kwargsraresponser)r2r2r3r(s    zECSDeploy._register_task_defcCs&t||j}||||_dSrd)CodeDeployValidatorrdescribe_cd_resources validate_allr%)r+rD validatorr2r2r3r$s  z)ECSDeploy._validate_code_deploy_resourcesN)__name__ __module__ __qualname__NAME DESCRIPTION ARG_TABLErpr?rBrr4r*rWrrr(r$r2r2r2r3r sR    <  r c@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS)r9zKWaiting for {deployment_id} to succeed (will wait up to {wait} minutes)... cC||_||_dSrd)_client _appspec_dict)r+ cd_client appspec_dictr2r2r3__init__ zCodeDeployer.__init__c CsP|||}z|jjdi|}W|dSty'}ztjd|dd}~ww)Nzcreate deploymentactionrJ deploymentIdr2)_get_create_deploy_requestr~r;rrServiceClientError)r+r5deploy_grp_name request_objrrrVr2r2r3r;szCodeDeployer.create_deploymentcCs$t|j}t|}t|Srd)redumpsrr ensure_byteshashlibsha256 hexdigest)r+ appspec_strappspec_encodedr2r2r3_get_appspec_hashs  zCodeDeployer._get_appspec_hashcCs$||dt|j|dddS)NAppSpecContent)contentr) revisionTypeappSpecContent)applicationNamedeploymentGroupNamerevision)rerrr)r+r5rr2r2r3rs z'CodeDeployer._get_create_deploy_requestc Cs|j}td|d}g}||D]&}|D]}||}t||d}||} t|| d} || | <q||q|||<||_dS)a Inserts the ARN of the previously created ECS task definition into the provided appspec. Expected format of ECS appspec (YAML) is: version: 0.0 resources: - : type: AWS::ECS::Service properties: taskDefinition: # replace this loadBalancerInfo: containerName: containerPort: rr propertiesrlN)rrfind_required_keyappend) r+new_arnr/ resources_keyupdated_resourcesresourcerresource_contentproperties_keyproperties_content task_def_keyr2r2r3r:s&    z CodeDeployer.update_task_def_arncCsn|jd}|dur|tkrt}n |dus|dkrd}t}|d|}||d}||||j||ddS)Ndeployment_successful<)Delay MaxAttempts)r WaiterConfig)r~ get_waiter MAX_WAIT_MINDEFAULT_DELAY_SEC_show_deploy_wait_msgwait)r+r7wait_minwaiter delay_sec max_attemptsrr2r2r3rA(s   z$CodeDeployer.wait_for_deploy_successcCs&tj|jj||dtjdS)N)rGr)r<r=r> MSG_WAITINGr@rC)r+r7rr2r2r3r;s z"CodeDeployer._show_deploy_wait_msgN) rwrxryrrr;rrr:rArr2r2r2r3r9s  + r9c@s<eZdZddZddZddZddZd d Zd d Zd S)rscCr}rd)r~_resource_names)r+rrr2r2r3rCrzCodeDeployValidator.__init__c Csz |jj|jdd|_Wnty }ztjd|dd}~wwz|jd}|jd}|jj||d|_WdStyJ}ztjd|dd}~ww)Nr5)rz describe Code Deploy applicationrr6)rrz%describe Code Deploy deployment group) r~get_applicationr app_detailsrrrget_deployment_groupdeployment_group_details)r+rVdgpappr2r2r3rtGs,   z)CodeDeployValidator.describe_cd_resourcescCsVt|dr |jdur dS|jd}|d}|dd}d}||d}||}|tS)NrdeploymentGroupInfo blueGreenDeploymentConfigurationdeploymentReadyOptionwaitTimeInMinutes)terminateBlueInstancesOnDeploymentSuccessterminationWaitTimeInMinutes)hasattrrTIMEOUT_BUFFER_MIN)r+dgp_infoblue_green_infodeploy_ready_wait_min terminate_keytermination_wait_minconfigured_waitr2r2r3r&Xs     z,CodeDeployValidator.get_deployment_wait_timecCs||dSrd)validate_applicationvalidate_deployment_group)r+r2r2r3ruls z CodeDeployValidator.validate_allcCs.|jd}|jdddkrtjd|ddS)Nr5 applicationcomputePlatformECS Applicationrr)rrrInvalidPlatformError)r+r5r2r2r3rps z(CodeDeployValidator.validate_applicationc Cs|jd}|jd}|jd}|jd}|jd}|jd}|d}|dkr-tjd |d |jdd }|D]*} | d } | |krL| |krLtj|d|d | d} | |kr`| |kr`tj|d|d q6dS)Nr6r rYrrZrrrzDeployment Groupr ecsServices serviceName)dg_namer resource_name clusterName)rrrrInvalidProperyError) r+rr rYrrZgrp_infocompute_platformtarget_servicestarget target_servtarget_clusterr2r2r3rvs4       z-CodeDeployValidator.validate_deployment_groupN) rwrxryrrtr&rurrr2r2r2r3rsBs rsc@s$eZdZddZddZddZdS)rcCs6||_tj|d|_|jd|j|j|j|jd|_dS)Nrecs)r endpoint_urlrr) _argsrr#_custom_configr r!rr"r~)r+sessionr,r-rr2r2r3rs zECSClient.__init__c Cs|jj}|dus nd}z |jj||jjgd}Wnty+}ztjd|dd}~wwt|ddkr=tj |jj|d|dd}t |d }|d |d |d |d S) Ndefault)rserviceszdescribe ECS servicerrr)r r clusterArn serviceArnr)rYrHrZrX) rrr~describe_servicesr rrrlenInvalidServiceErrorrget_cluster_name_from_arn)r+rservice_responserVrbrXr2r2r3r[s6    zECSClient.get_service_detailsc Cs@z |jjdi|}W|Sty}ztjd|dd}~ww)Nzregister ECS task definitionrr2)r~rorrr)r+kwargsrrrVr2r2r3rosz"ECSClient.register_task_definitionN)rwrxryrr[ror2r2r2r3rs r)rrerKr<botocorerrbotocore.exceptionsr awscli.compatrawscli.customizations.ecsrrawscli.customizations.commandsrrrrr r9rsrr2r2r2r3s"    9lR