def destroy_env(env: str, debug: bool) -> None: with MessagesContext("Destroying", debug=debug) as msg_ctx: ssm.cleanup_changeset(env_name=env) ssm.cleanup_manifest(env_name=env) if ssm.does_parameter_exist(name=f"/orbit/{env}/context") is False: msg_ctx.info(f"Environment {env} not found. Destroying only possible remaining resources.") elb.delete_load_balancers(env_name=env) destroy_remaining_resources(env_name=env, top_level="orbit") msg_ctx.progress(100) return context: "Context" = ContextSerDe.load_context_from_ssm(env_name=env, type=Context) msg_ctx.info("Context loaded") msg_ctx.info(f"Teams: {','.join([t.name for t in context.teams])}") msg_ctx.progress(2) if any(cfn.does_stack_exist(stack_name=t.stack_name) for t in context.teams): msg_ctx.error("Found Teams dependent on the Envrionment.") return if ( cfn.does_stack_exist(stack_name=context.env_stack_name) or cfn.does_stack_exist(stack_name=context.toolkit.stack_name) or cfn.does_stack_exist(stack_name=context.cdk_toolkit.stack_name) ): bundle_path = bundle.generate_bundle(command_name="destroy", context=context) msg_ctx.progress(5) buildspec = codebuild.generate_spec( context=context, plugins=True, cmds_build=[f"orbit remote --command destroy_env {env}"], changeset=None, ) remote.run( command_name="destroy", context=context, bundle_path=bundle_path, buildspec=buildspec, codebuild_log_callback=msg_ctx.progress_bar_callback, timeout=45, ) msg_ctx.info("Env destroyed") msg_ctx.progress(95) try: destroy_toolkit(env_name=context.name) except botocore.exceptions.ClientError as ex: error = ex.response["Error"] if "does not exist" not in error["Message"]: raise _logger.debug(f"Skipping toolkit destroy: {error['Message']}") msg_ctx.info("Toolkit destroyed") ssm.cleanup_env(env_name=context.name) msg_ctx.progress(100)
def destroy_foundation(env: str, debug: bool) -> None: with MessagesContext("Destroying", debug=debug) as msg_ctx: ssm.cleanup_changeset(env_name=env, top_level="orbit-foundation") ssm.cleanup_manifest(env_name=env, top_level="orbit-foundation") if ssm.does_parameter_exist(name=f"/orbit-foundation/{env}/context") is False: msg_ctx.info(f"Foundation {env} not found. Destroying only possible remaining resources.") destroy_remaining_resources(env_name=env, top_level="orbit-foundation") msg_ctx.progress(100) return context: "FoundationContext" = ContextSerDe.load_context_from_ssm(env_name=env, type=FoundationContext) msg_ctx.info("Context loaded") msg_ctx.progress(2) msg_ctx.progress(4) if ( cfn.does_stack_exist(stack_name=cast(str, context.stack_name)) or cfn.does_stack_exist(stack_name=context.toolkit.stack_name) or cfn.does_stack_exist(stack_name=context.cdk_toolkit.stack_name) ): bundle_path = bundle.generate_bundle(command_name="destroy", context=cast(Context, context)) msg_ctx.progress(5) buildspec = codebuild.generate_spec( context=cast(Context, context), plugins=False, cmds_build=[f"orbit remote --command destroy_foundation {env}"], changeset=None, ) remote.run( command_name="destroy", context=cast(Context, context), bundle_path=bundle_path, buildspec=buildspec, codebuild_log_callback=msg_ctx.progress_bar_callback, timeout=45, ) msg_ctx.info("Foundation destroyed") msg_ctx.progress(95) try: destroy_toolkit(env_name=context.name, top_level="orbit-foundation") except botocore.exceptions.ClientError as ex: error = ex.response["Error"] if "does not exist" not in error["Message"]: raise _logger.debug(f"Skipping toolkit destroy: {error['Message']}") msg_ctx.info("Toolkit destroyed") ssm.cleanup_env(env_name=context.name, top_level="orbit-foundation") msg_ctx.progress(100)
def destroy_env(env: str, preserve_credentials: bool, debug: bool) -> None: with MessagesContext("Destroying", debug=debug) as msg_ctx: ssm.cleanup_changeset(env_name=env) ssm.cleanup_manifest(env_name=env) if ssm.does_parameter_exist(name=f"/orbit/{env}/context") is False: msg_ctx.info( f"Environment {env} not found. Destroying only possible remaining resources." ) elb.delete_load_balancers(env_name=env) destroy_remaining_resources(env_name=env, top_level="orbit") msg_ctx.progress(100) return msg_ctx.progress(15) context: "Context" = ContextSerDe.load_context_from_ssm(env_name=env, type=Context) msg_ctx.info("Context loaded") msg_ctx.info(f"Teams: {','.join([t.name for t in context.teams])}") if any( cfn.does_stack_exist(stack_name=t.stack_name) for t in context.teams): raise click.ClickException( "Found Teams dependent on the Envrionment.") if (cfn.does_stack_exist(stack_name=context.env_stack_name) or cfn.does_stack_exist(stack_name=context.toolkit.stack_name) or cfn.does_stack_exist( stack_name=context.cdk_toolkit.stack_name)): msg_ctx.progress(50) destroy.destroy_env(env_name=context.name) if not preserve_credentials: secretsmanager.delete_docker_credentials( secret_id=f"orbit-{context.name}-docker-credentials") _logger.info("Removed docker credentials from SecretsManager") try: if context.cdk_toolkit.s3_bucket: s3.delete_bucket(bucket=context.cdk_toolkit.s3_bucket) except Exception as ex: _logger.debug( "Skipping Environment CDK Toolkit bucket deletion. Cause: %s", ex) msg_ctx.info("Env destroyed leaving the Env toolkit") msg_ctx.progress(100)
def destroy_foundation(env: str, debug: bool) -> None: with MessagesContext("Destroying", debug=debug) as msg_ctx: ssm.cleanup_changeset(env_name=env, top_level="orbit-f") ssm.cleanup_manifest(env_name=env, top_level="orbit-f") if ssm.does_parameter_exist(name=f"/orbit-f/{env}/context") is False: msg_ctx.info( f"Foundation {env} not found. Destroying only possible remaining resources." ) destroy_remaining_resources(env_name=env, top_level="orbit-f") msg_ctx.progress(100) return context: "FoundationContext" = ContextSerDe.load_context_from_ssm( env_name=env, type=FoundationContext) msg_ctx.info("Context loaded") msg_ctx.progress(25) if (cfn.does_stack_exist(stack_name=cast(str, context.stack_name)) or cfn.does_stack_exist(stack_name=context.toolkit.stack_name) or cfn.does_stack_exist( stack_name=context.cdk_toolkit.stack_name)): destroy.destroy_foundation(env_name=context.name) msg_ctx.info("Foundation destroyed") msg_ctx.progress(75) try: _destroy_toolkit( env_name=context.name, top_level="orbit-f", cdk_toolkit_bucket=context.cdk_toolkit.s3_bucket, ) except botocore.exceptions.ClientError as ex: error = ex.response["Error"] if "does not exist" not in error["Message"]: raise _logger.debug(f"Skipping toolkit destroy: {error['Message']}") msg_ctx.info("Toolkit destroyed") ssm.cleanup_env(env_name=context.name, top_level="orbit-f") msg_ctx.progress(100)
def delete_image(env: str, name: str, debug: bool) -> None: with MessagesContext("Destroying Docker Image", debug=debug) as msg_ctx: if not name: raise ValueError("Image name required to delete") ssm.cleanup_changeset(env_name=env) ssm.cleanup_manifest(env_name=env) context: "Context" = ContextSerDe.load_context_from_ssm(env_name=env, type=Context) msg_ctx.info("Manifest loaded") if cfn.does_stack_exist(stack_name=f"orbit-{context.name}") is False: msg_ctx.error( "Please, deploy your environment before deploy/destroy any docker image" ) return msg_ctx.progress(3) delete.delete_image(env_name=env, image_name=name) msg_ctx.info("Docker Image destroyed from ECR") msg_ctx.progress(100)
def dump_manifest_to_ssm(manifest: T) -> None: _logger.debug("Writing manifest to SSM parameter.") if isinstance(manifest, Manifest): _logger.debug("Teams: %s", [t.name for t in manifest.teams]) content: Dict[str, Any] = cast(Dict[str, Any], Manifest.Schema().dump(manifest)) ssm.cleanup_manifest(env_name=manifest.name) if "Teams" in content: for team in content["Teams"]: team_parameter_name: str = f"/orbit/{manifest.name}/teams/{team['Name']}/manifest" ssm.put_parameter(name=team_parameter_name, obj=team) del content["Teams"] manifest_parameter_name = manifest.ssm_parameter_name elif isinstance(manifest, FoundationManifest): content = cast(Dict[str, Any], FoundationManifest.Schema().dump(manifest)) ssm.cleanup_manifest(env_name=manifest.name, top_level="orbit-f") manifest_parameter_name = manifest.ssm_parameter_name else: raise ValueError("Unknown 'manifest' Type") ssm.put_parameter(name=cast(str, manifest_parameter_name), obj=content)
def delete_image(env: str, name: str, debug: bool) -> None: with MessagesContext("Destroying Docker Image", debug=debug) as msg_ctx: ssm.cleanup_changeset(env_name=env) ssm.cleanup_manifest(env_name=env) context: "Context" = ContextSerDe.load_context_from_ssm(env_name=env, type=Context) msg_ctx.info("Manifest loaded") if cfn.does_stack_exist(stack_name=f"orbit-{context.name}") is False: msg_ctx.error("Please, deploy your environment before deploy/destroy any docker image") return plugins.PLUGINS_REGISTRIES.load_plugins( context=context, msg_ctx=msg_ctx, plugin_changesets=[], teams_changeset=None, ) msg_ctx.progress(3) bundle_path = bundle.generate_bundle(command_name=f"delete_image-{name}", context=context, dirs=[]) msg_ctx.progress(4) buildspec = codebuild.generate_spec( context=context, plugins=False, cmds_build=[f"orbit remote --command delete_image {env} {name}"], changeset=None, ) remote.run( command_name=f"delete_image-{name}", context=context, bundle_path=bundle_path, buildspec=buildspec, codebuild_log_callback=msg_ctx.progress_bar_callback, timeout=10, ) msg_ctx.info("Docker Image destroyed from ECR") msg_ctx.progress(100)