Skip to content

Commit

Permalink
qhub destroy using targets (nebari-dev#948)
Browse files Browse the repository at this point in the history
  • Loading branch information
danlester authored Nov 30, 2021
1 parent c0d08bb commit a5d0190
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
6 changes: 6 additions & 0 deletions qhub/cli/destroy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def create_destroy_subcommand(subparser):
action="store_true",
help="Disable auto-rendering before destroy",
)
subparser.add_argument(
"--full-only",
action="store_true",
help="Only carry out one full pass instead of targeted sections",
)
subparser.set_defaults(func=handle_destroy)


Expand All @@ -43,4 +48,5 @@ def handle_destroy(args):
destroy_configuration(
config,
args.skip_remote_state_provision,
args.full_only,
)
83 changes: 79 additions & 4 deletions qhub/destroy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
logger = logging.getLogger(__name__)


def destroy_configuration(config, skip_remote_state_provision=False):
def destroy_configuration(config, skip_remote_state_provision=False, full_only=False):
logger.info(
"""Removing all infrastructure, your local files will still remain, \n
you can use 'qhub deploy' to re - install infrastructure using same config file"""
"""Removing all infrastructure, your local files will still remain,
you can use 'qhub deploy' to re-install infrastructure using same config file\n"""
)

with timer(logger, "destroying QHub"):
Expand All @@ -20,7 +20,82 @@ def destroy_configuration(config, skip_remote_state_provision=False):
# 02 Remove all infrastructure
terraform.init(directory="infrastructure")
terraform.refresh(directory="infrastructure")
terraform.destroy(directory="infrastructure")

if not full_only:
stages = (
{
"name": "General cluster software",
"targets": [
"module.kubernetes-nfs-mount",
"module.kubernetes-nfs-server",
"module.kubernetes-nfs-mount",
"module.kubernetes-conda-store-server",
"module.kubernetes-conda-store-mount",
"module.kubernetes-autoscaling",
"module.qhub",
"module.prefect",
"module.monitoring",
"module.clearml",
"module.forwardauth",
"random_password.jupyterhub-jhsecret",
"random_password.forwardauth-jhsecret",
"kubernetes_secret.qhub_yaml_secret",
]
+ [
f"module.{helmext['name']}-extension"
for helmext in config.get("helm_extensions", [])
]
+ [
f"module.ext-{ext['name']}"
for ext in config.get("extensions", [])
],
},
{
"name": "Keycloak Config",
"targets": [
"module.kubernetes-keycloak-config",
"random_password.keycloak-qhub-bot-password",
],
},
{
"name": "Keycloak Helm installation",
"targets": ["module.kubernetes-keycloak-helm"],
},
{
"name": "Kubernetes Ingress",
"targets": ["module.kubernetes-ingress"],
},
{
"name": "Kubernetes Cluster",
"targets": [
"module.kubernetes",
"module.kubernetes-initialization",
],
},
{
"name": "Cloud Infrastructure",
"targets": [
"module.registry-jupyterhub", # GCP
"module.efs", # AWS
"module.registry-jupyterlab", # AWS
"module.network", # AWS
"module.accounting", # AWS
"module.registry", # Azure
],
},
)

for stageinfo in stages:
logger.info(
f"Running Terraform Stage: {stageinfo['name']} {stageinfo['targets']}"
)
terraform.destroy(
directory="infrastructure", targets=stageinfo["targets"]
)

else:
logger.info("Running Terraform Stage: FULL")
terraform.destroy(directory="infrastructure")

# 03 Remove terraform backend remote state bucket
# backwards compatible with `qhub-config.yaml` which
Expand Down
8 changes: 5 additions & 3 deletions qhub/provider/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,14 @@ def refresh(directory=None):
run_terraform_subprocess(command, cwd=directory, prefix="terraform")


def destroy(directory=None):
logger.info(f"terraform destroy directory={directory}")
def destroy(directory=None, targets=None):
targets = targets or []

logger.info(f"terraform destroy directory={directory} targets={targets}")
command = [
"destroy",
"-auto-approve",
]
] + ["-target=" + _ for _ in targets]

with timer(logger, "terraform destroy"):
run_terraform_subprocess(command, cwd=directory, prefix="terraform")
Expand Down

0 comments on commit a5d0190

Please sign in to comment.