def _deploy_user_image(image_name: str, path: str, env: str, build_args: Optional[List[str]]) -> None: region = get_region() account_id = get_account_id() _logger.debug(f"_deploy_user_image args: {account_id}, {region}, {image_name}, {env}") docker.login_v2(account_id=account_id, region=region) _logger.debug("DockerHub and ECR Logged in") _logger.debug("Deploying the %s Docker image", image_name) ecr_repo = f"orbit-{env}/users/{image_name}" if not ecr.describe_repositories(repository_names=[ecr_repo]): ecr.create_repository(repository_name=ecr_repo) _logger.debug("Building and deploy docker image from source...") path = os.path.join(os.getcwd(), image_name) _logger.debug("path: %s", path) docker.deploy_image_from_source( dir=path, name=ecr_repo, build_args=build_args, tag="latest", env=env, ) _logger.debug("Docker Image Deployed to ECR")
def _deploy_team_image(context: "Context", team_context: "TeamContext", image: str) -> None: image_dir: str = _create_dockerfile(context=context, team_context=team_context, image_name=image) image_name: str = f"orbit-{context.name}-{team_context.name}-{image}" _logger.debug("Deploying the %s Docker image", image_name) docker.deploy_image_from_source(context=context, dir=image_dir, name=image_name) _logger.debug("Docker Image Deployed to ECR (%s).", image_name)
def _deploy_image(args: Tuple[str, ...]) -> None: _logger.debug("_deploy_image args: %s", args) if len(args) < 4: raise ValueError("Unexpected number of values in args.") env: str = args[0] image_name: str = args[1] dir: str = args[2] script: Optional[str] = args[3] if args[3] != "NO_SCRIPT" else None build_args = args[4:] context: "Context" = ContextSerDe.load_context_from_ssm(env_name=env, type=Context) _logger.debug("manifest.name: %s", context.name) docker.login(context=context) _logger.debug("DockerHub and ECR Logged in") _logger.debug("Deploying the %s Docker image", image_name) image_def: ImageManifest = getattr(context.images, image_name.replace("-", "_")) if image_def.source == "code": _logger.debug("Building and deploy docker image from source...") path = os.path.join(os.getcwd(), "bundle", dir) _logger.debug("path: %s", path) if script is not None: sh.run(f"sh {script}", cwd=path) tag = cast(str, image_def.version) docker.deploy_image_from_source( context=context, dir=path, name=f"orbit-{context.name}-{image_name}", build_args=cast(Optional[List[str]], build_args), tag=tag, ) else: _logger.debug("Replicating docker image to ECR...") docker.replicate_image( context=context, image_name=image_name, deployed_name=f"orbit-{context.name}-{image_name}") _logger.debug("Docker Image Deployed to ECR")
def build_image(args: Tuple[str, ...]) -> None: if len(args) < 4: raise ValueError("Unexpected number of values in args.") env: str = args[0] image_name: str = args[1] script: Optional[str] = args[2] if args[2] != "NO_SCRIPT" else None source_registry: Optional[str] if args[3] != "NO_REPO": if len(args) < 6: raise Exception( "Source registry is defined without 'source_repository' or 'source_version' " ) source_registry = args[3] source_repository: str = args[4] source_version: str = args[5] build_args = args[6:] _logger.info("replicating image %s: %s %s:%s", image_name, source_registry, source_repository, source_version) else: _logger.info("building image %s: %s", image_name, script) build_args = args[4:] source_registry = None _logger.debug("args: %s", args) context: "Context" = ContextSerDe.load_context_from_ssm(env_name=env, type=Context) docker.login(context=context) _logger.debug("DockerHub and ECR Logged in") ecr_repo = (f"orbit-{context.name}/{image_name}" if image_name in [ n.replace("_", "-") for n in context.images.names ] else f"orbit-{context.name}/users/{image_name}") if not ecr.describe_repositories(repository_names=[ecr_repo]): ecr.create_repository(repository_name=ecr_repo, env_name=context.name) image_def: Optional["ImageManifest"] = getattr( context.images, image_name.replace("-", "_"), None) _logger.debug("image def: %s", image_def) if source_registry: docker.replicate_image( context=context, image_name=image_name, deployed_name=ecr_repo, source=source_registry, source_repository=source_repository, source_version=source_version, ) elif image_def is None or image_def.get_source( account_id=context.account_id, region=context.region) == "code": path = os.path.join(os.getcwd(), image_name) if not os.path.exists(path): bundle_dir = os.path.join(os.getcwd(), "bundle", image_name) if os.path.exists(bundle_dir): path = bundle_dir else: raise RuntimeError( f"Unable to locate source in {path} or {bundle_dir}") _logger.debug("path: %s", path) if script is not None: sh.run(f"sh {script}", cwd=path) tag = cast(str, image_def.version if image_def else "latest") docker.deploy_image_from_source(context=context, dir=path, name=ecr_repo, tag=tag, build_args=cast( Optional[List[str]], build_args)) else: docker.replicate_image(context=context, image_name=image_name, deployed_name=ecr_repo) _logger.debug("Docker Image Deployed to ECR")