U !sc@sddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z dZ difddZdifddZdifd d Zd d Zd"d dZddZddZddZddZejd#ddZd$ddZddZd d!ZdS)%)absolute_importNz?{account}.dkr.ecr.{region}.amazonaws.com/{image_name}:{version} DockerfilecCs t|}t|t|||dS)Builds a docker image from the specified dockerfile, and pushes it to ECR. Handles things like ECR login, creating the repository. Returns the name of the created docker image in ECR N)_find_base_image_in_dockerfile_ecr_login_if_needed_build_from_dockerfile)repository_name dockerfile build_args base_imager c/home/ec2-user/SageMaker/aws-deepracer-workshops/Advanced workshops/workshop/common/docker_utils.pybuild_docker_images rcCs(t|}t|t|||t|}|S)r)rrrpush)rr r r ecr_tagr r r build_and_push_docker_image*s  rcCs`ddd|d|dg}|D]\}}|dd||fg7}qtd||ft|td |dS) Ndockerbuildz-tz-f.z --build-argz%s=%sz Building docker image %s from %szDone building docker image %s)itemsprint_execute)rr r Z build_cmdkvr r r r7s rcCs6t|}ttdd|d}|dd}|S)NcSs |dS)NzFROM ) startswith)liner r r Cz0_find_base_image_in_dockerfile..r)open readlineslistfilterrstrip)r Zdockerfile_lines from_liner r r r rAs  rcCst}|p|dd}|p&|j}z|d\}}Wntk rV|}d}YnX|jd|d}t||t||t |||}|S)a  Push the builded tag to ECR. Args: tag (string): tag which you named your algo aws_account (string): aws account of the ECR repo aws_region (string): aws region where the repo is located Returns: (string): ECR repo image that was pushed stsAccount:latestecr) region_name) boto3Sessionclientget_caller_identityr*split ValueError_create_ecr_repo _ecr_login_push)tag aws_account aws_regionsessionrversion ecr_clientrr r r rHs      rcCsVd||f}d||f}tdd||gtd||ftdd|gtd||S)Nz%s.dkr.ecr.%s.amazonaws.comz%s/%srr4z-Pushing docker image to ECR repository %s/%s rzDone pushing %s)rr)r5r6r4Zecr_reporr r r r3es   r3cCsFz|j|dtd|Wn$|jjk r@td|YnXdS)z6 Create the repo if it doesn't already exist. )ZrepositoryNamezCreated new ECR repository: %sz!ECR repository already exists: %sN)Zcreate_repositoryr exceptionsZ RepositoryAlreadyExistsException)r9rr r r r1os  r1cCsr|j|gd}|dd}t|d}|dd}|ddd}dd d d d ||g}t|d dtddS)N) registryIdsauthorizationDatarauthorizationTokenutf-8zAWS: proxyEndpointrloginz-uAWSz-pT)quietzLogged into ECR)get_authorization_tokenbase64 b64decodedecodestriprr)r9r5authauthorization_data raw_tokentokenecr_urlcmdr r r r2zs  r2cCsNtd}d|krd|ksdStd|r2dS|dd}t||dS)Nr)zdkr.ecrz amazonaws.comzdocker images -q %srr)r+r- _check_outputrGr/r2)imager9r5r r r rs rtmpccs$tj|||d}|Vt|dS)aCreate a temporary directory with a context manager. The file is deleted when the context exits. The prefix, suffix, and dir arguments are the same as for mkstemp(). Args: suffix (str): If suffix is specified, the file name will end with that suffix, otherwise there will be no suffix. prefix (str): If prefix is specified, the file name will begin with that prefix; otherwise, a default prefix is used. dir (str): If dir is specified, the file will be created in that directory; otherwise, a default directory is used. Returns: str: path to the directory )suffixprefixdirN)tempfilemkdtempshutilrmtree)rRrSrTrQr r r _tmpdirsrYFc Csv|stdd|tj|tjtjd}z t|Wn:tk rp}zd|t|f}t|W5d}~XYnXdS)Nz$ %s )stdoutstderrzFailed to run: %s, %s) rjoin subprocessPopenPIPESTDOUT_stream_output RuntimeErrorstr)commandrBprocessemsgr r r rs rcCsJd}|dkr2|jd}tj||}q|dkrFtd|dS)aTStream the output of a process to stdout This function takes an existing process that will be polled for output. Only stdout will be polled and sent to sys.stdout. Args: process(subprocess.Popen): a process that has been started with stdout=PIPE and stderr=STDOUT Returns (int): process exit code Nr>rzProcess exited with code: %s)r[readlinerFsyswritepollrc)rf exit_coder[r r r rbs   rbc Ost|trt|}d}ztj|f||}Wn.tjk r\}z|j}d}W5d}~XYnX|d}|st d|t dd ||S)NTFr>zCommand output: %szFailed to run %s,) isinstancerdshlexr/r^ check_outputCalledProcessErroroutputrFr Exceptionr])rM popenargskwargssuccessrsrgr r r rNs    rN)NN)rPrQN)F) __future__rrD contextlibostimerprWr^rjrUr+jsonZIMAGE_TEMPLATErrrrrr3r1r2rcontextmanagerrYrrbrNr r r r s4