def deploy_env(env_name: str, manifest_dir: str) -> None: docker.login(context=context) _logger.debug("DockerHub and ECR Logged in") cdk_toolkit.deploy(context=context) _logger.debug("CDK Toolkit Stack deployed") env.deploy( context=context, eks_system_masters_roles_changes=changeset.eks_system_masters_roles_changeset if changeset else None, ) _logger.debug("Env Stack deployed") eksctl.deploy_env( context=context, changeset=changeset, ) _logger.debug("EKS Environment Stack deployed") kubectl.deploy_env(context=context) _logger.debug("Kubernetes Environment components deployed") helm.deploy_env(context=context) _logger.debug("Helm Charts installed") k8s_context = utils.get_k8s_context(context=context) kubectl.fetch_kubectl_data(context=context, k8s_context=k8s_context) ContextSerDe.dump_context_to_ssm(context=context) _logger.debug("Updating userpool redirect") _update_userpool_client(context=context) _update_userpool(context=context)
def deploy_foundation(env_name: str) -> None: docker.login(context=context) _logger.debug("DockerHub and ECR Logged in") cdk_toolkit.deploy(context=context) _logger.debug("CDK Toolkit Stack deployed") foundation.deploy(context=context) _logger.debug("Demo Stack deployed")
def deploy_foundation(args: Tuple[str, ...]) -> None: _logger.debug("args: %s", args) if len(args) != 1: raise ValueError("Unexpected number of values in args") env_name: str = args[0] context: "FoundationContext" = ContextSerDe.load_context_from_ssm(env_name=env_name, type=FoundationContext) _logger.debug("Context loaded.") docker.login(context=context) _logger.debug("DockerHub and ECR Logged in") cdk_toolkit.deploy(context=context) _logger.debug("CDK Toolkit Stack deployed") foundation.deploy(context=context) _logger.debug("Demo Stack deployed")
def destroy(context: "Context") -> None: _logger.debug("Stack name: %s", context.env_stack_name) if cfn.does_stack_exist(stack_name=context.env_stack_name): docker.login(context=context) _logger.debug("DockerHub and ECR Logged in") ecr.cleanup_remaining_repos(env_name=context.name) args = [context.name] cdk.destroy( context=context, stack_name=context.env_stack_name, app_filename=os.path.join(ORBIT_CLI_ROOT, "remote_files", "cdk", "env.py"), args=args, ) ssm.cleanup_context(env_name=context.name)
def destroy(context: "Context") -> None: _logger.debug("Stack name: %s", context.env_stack_name) if cfn.does_stack_exist(stack_name=context.env_stack_name): docker.login(context=context) _logger.debug("DockerHub and ECR Logged in") _cleanup_remaining_resources(env_name=context.name) add_images_str, remove_images_str = _concat_images_into_args( context=context, add_images=[], remove_images=[]) args = [context.name, add_images_str, remove_images_str] cdk.destroy( context=context, stack_name=context.env_stack_name, app_filename=os.path.join(ORBIT_CLI_ROOT, "remote_files", "cdk", "env.py"), args=args, ) ssm.cleanup_context(env_name=context.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 deploy_env(args: Tuple[str, ...]) -> None: _logger.debug("args: %s", args) if len(args) == 2: env_name: str = args[0] skip_images_remote_flag: str = str(args[1]) else: raise ValueError("Unexpected number of values in args") context: "Context" = ContextSerDe.load_context_from_ssm(env_name=env_name, type=Context) _logger.debug("Context loaded.") changeset: Optional["Changeset"] = load_changeset_from_ssm( env_name=env_name) _logger.debug("Changeset loaded.") docker.login(context=context) _logger.debug("DockerHub and ECR Logged in") cdk_toolkit.deploy(context=context) _logger.debug("CDK Toolkit Stack deployed") env.deploy( context=context, add_images=[], remove_images=[], eks_system_masters_roles_changes=changeset. eks_system_masters_roles_changeset if changeset else None, ) _logger.debug("Env Stack deployed") if skip_images_remote_flag == "skip-images": _logger.debug("Docker images build skipped") else: deploy_images_remotely(context=context) _logger.debug("Docker Images deployed") eksctl.deploy_env( context=context, changeset=changeset, ) _logger.debug("EKS Environment Stack deployed") kubectl.deploy_env(context=context) _logger.debug("Kubernetes Environment components deployed")
def deploy_env(args: Tuple[str, ...]) -> None: _logger.debug("args: %s", args) if len(args) == 2: env_name: str = args[0] skip_images_remote_flag: str = str(args[1]) else: raise ValueError("Unexpected number of values in args") context: "Context" = ContextSerDe.load_context_from_ssm(env_name=env_name, type=Context) _logger.debug("Context loaded.") changeset: Optional["Changeset"] = load_changeset_from_ssm(env_name=env_name) _logger.debug("Changeset loaded.") docker.login(context=context) _logger.debug("DockerHub and ECR Logged in") cdk_toolkit.deploy(context=context) _logger.debug("CDK Toolkit Stack deployed") env.deploy( context=context, eks_system_masters_roles_changes=changeset.eks_system_masters_roles_changeset if changeset else None, ) _logger.debug("Env Stack deployed") deploy_images_remotely(context=context, skip_images=skip_images_remote_flag == "skip-images") _logger.debug("Docker Images deployed") eksctl.deploy_env( context=context, changeset=changeset, ) _logger.debug("EKS Environment Stack deployed") kubectl.deploy_env(context=context) _logger.debug("Kubernetes Environment components deployed") helm.deploy_env(context=context) _logger.debug("Helm Charts installed") k8s_context = utils.get_k8s_context(context=context) kubectl.fetch_kubectl_data(context=context, k8s_context=k8s_context, include_teams=False) ContextSerDe.dump_context_to_ssm(context=context)
def deploy_teams(args: Tuple[str, ...]) -> None: _logger.debug("args: %s", args) if len(args) == 1: env_name: str = args[0] else: raise ValueError("Unexpected number of values in args") context: "Context" = ContextSerDe.load_context_from_ssm(env_name=env_name, type=Context) _logger.debug("Context loaded.") changeset: Optional["Changeset"] = load_changeset_from_ssm(env_name=env_name) _logger.debug("Changeset loaded.") if changeset: plugins.PLUGINS_REGISTRIES.load_plugins( context=context, plugin_changesets=changeset.plugin_changesets, teams_changeset=changeset.teams_changeset ) _logger.debug("Plugins loaded") docker.login(context=context) _logger.debug("DockerHub and ECR Logged in") if changeset and changeset.teams_changeset and changeset.teams_changeset.removed_teams_names: kubectl.write_kubeconfig(context=context) for team_name in changeset.teams_changeset.removed_teams_names: team_context: Optional["TeamContext"] = context.get_team_by_name(name=team_name) if team_context is None: raise RuntimeError(f"TeamContext {team_name} not found!") _logger.debug("Destroying team %s", team_name) plugins.PLUGINS_REGISTRIES.destroy_team_plugins(context=context, team_context=team_context) _logger.debug("Team Plugins destroyed") helm.destroy_team(context=context, team_context=team_context) _logger.debug("Team Helm Charts uninstalled") kubectl.destroy_team(context=context, team_context=team_context) _logger.debug("Kubernetes Team components destroyed") eksctl.destroy_team(context=context, team_context=team_context) _logger.debug("EKS Team Stack destroyed") teams.destroy_team(context=context, team_context=team_context) _logger.debug("Team %s destroyed", team_name) context.remove_team_by_name(name=team_name) ContextSerDe.dump_context_to_ssm(context=context) team_names = [t.name for t in context.teams] if changeset and changeset.teams_changeset and changeset.teams_changeset.added_teams_names: team_names.extend(changeset.teams_changeset.added_teams_names) manifest: Optional["Manifest"] = ManifestSerDe.load_manifest_from_ssm(env_name=context.name, type=Manifest) if manifest is None: raise RuntimeError(f"Manifest {context.name} not found!") kubectl.write_kubeconfig(context=context) for team_name in team_names: team_manifest = manifest.get_team_by_name(name=team_name) if team_manifest is None: raise RuntimeError(f"TeamManifest {team_name} not found!") teams.deploy_team(context=context, manifest=manifest, team_manifest=team_manifest) _logger.debug("Team Stacks deployed") team_context = context.get_team_by_name(name=team_name) if team_context is None: raise RuntimeError(f"TeamContext {team_name} not found!") eksctl.deploy_team(context=context, team_context=team_context) _logger.debug("EKS Team Stack deployed") kubectl.deploy_team(context=context, team_context=team_context) _logger.debug("Kubernetes Team components deployed") helm.deploy_team(context=context, team_context=team_context) _logger.debug("Team Helm Charts installed") plugins.PLUGINS_REGISTRIES.deploy_team_plugins( context=context, team_context=team_context, changes=changeset.plugin_changesets if changeset else [] ) team_context.plugins = team_manifest.plugins ContextSerDe.dump_context_to_ssm(context=context) _logger.debug("Team Plugins deployed") k8s_context = utils.get_k8s_context(context=context) kubectl.fetch_kubectl_data(context=context, k8s_context=k8s_context, include_teams=True) _logger.debug("Teams deployed")
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")
def deploy_teams(env_name: str, manifest_dir: str) -> None: if changeset: plugins.PLUGINS_REGISTRIES.load_plugins( context=context, plugin_changesets=changeset.plugin_changesets, teams_changeset=changeset.teams_changeset, ) _logger.debug("Plugins loaded") docker.login(context=context) _logger.debug("DockerHub and ECR Logged in") if changeset and changeset.teams_changeset and changeset.teams_changeset.removed_teams_names: kubectl.write_kubeconfig(context=context) for team_name in changeset.teams_changeset.removed_teams_names: team_context: Optional["TeamContext"] = context.get_team_by_name(name=team_name) if team_context is None: raise RuntimeError(f"TeamContext {team_name} not found!") _logger.debug("Destory all user namespaces for %s", team_context.name) sh.run(f"kubectl delete namespaces -l orbit/team={team_context.name},orbit/space=user --wait=true") _logger.debug("Destroying team %s", team_name) plugins.PLUGINS_REGISTRIES.destroy_team_plugins(context=context, team_context=team_context) _logger.debug("Team Plugins destroyed") helm.destroy_team(context=context, team_context=team_context) _logger.debug("Team Helm Charts uninstalled") kubectl.destroy_team(context=context, team_context=team_context) _logger.debug("Kubernetes Team components destroyed") eksctl.destroy_team(context=context, team_context=team_context) _logger.debug("EKS Team Stack destroyed") teams.destroy_team(context=context, team_context=team_context) _logger.debug("Team %s destroyed", team_name) context.remove_team_by_name(name=team_name) ContextSerDe.dump_context_to_ssm(context=context) team_names = [t.name for t in context.teams] if changeset and changeset.teams_changeset and changeset.teams_changeset.added_teams_names: team_names.extend(changeset.teams_changeset.added_teams_names) manifest: Optional["Manifest"] = ManifestSerDe.load_manifest_from_ssm(env_name=context.name, type=Manifest) if manifest is None: raise RuntimeError(f"Manifest {context.name} not found!") kubectl.write_kubeconfig(context=context) for team_name in team_names: team_manifest = manifest.get_team_by_name(name=team_name) if team_manifest is None: raise RuntimeError(f"TeamManifest {team_name} not found!") teams.deploy_team(context=context, manifest=manifest, team_manifest=team_manifest) _logger.debug("Team Stacks deployed") team_context = context.get_team_by_name(name=team_name) if team_context is None: raise RuntimeError(f"TeamContext {team_name} not found!") eksctl.deploy_team(context=context, team_context=team_context) _logger.debug("EKS Team Stack deployed") kubectl.deploy_team(context=context, team_context=team_context) _logger.debug("Kubernetes Team components deployed") helm.deploy_team(context=context, team_context=team_context) _logger.debug("Team Helm Charts installed") plugins.PLUGINS_REGISTRIES.deploy_team_plugins( context=context, team_context=team_context, changes=changeset.plugin_changesets if changeset else [] ) team_context.plugins = team_manifest.plugins ContextSerDe.dump_context_to_ssm(context=context) _logger.debug("Team Plugins deployed") _logger.debug("Teams deployed")