def deploy_configuration(config): logger.info(f'All qhub endpoints will be under *.{config["domain"]}') jupyterhub_endpoint = f'jupyter.{config["domain"]}' if ( "client_id" not in config["authentication"]["config"] or "client_secret" not in config["authentication"]["config"] ): logger.info( "client_id and client_secret were not specified - dynamically creating oauth client" ) with timer(logger, "creating oauth client"): config["authentication"]["config"] = auth0.create_client( jupyterhub_endpoint ) with timer(logger, "rendering template"): tmp_config = pathlib.Path("./config.yaml") with tmp_config.open("w") as f: yaml.dump(config, f) render_default_template(".", tmp_config) infrastructure_dir = pathlib.Path(config["project_name"]) / "infrastructure" terraform.init(str(infrastructure_dir)) # ========= boostrap infrastructure ======== terraform.apply( str(infrastructure_dir), targets=[ "module.kubernetes", "module.kubernetes-initialization", "module.kubernetes-ingress", ], ) # ============= update dns ================ output = terraform.output(str(infrastructure_dir)) for key in output: if key.startswith("ingress"): endpoint = f'{key.split("_")[1]}.{config["domain"]}' address = output[key]["value"] if re.fullmatch(r"\d+\.\d+\.\d+\.\d+", address): cloudflare.update_record("qhub.dev", endpoint, "A", address) else: cloudflare.update_record("qhub.dev", endpoint, "CNAME", address) # ======= apply entire infrastructure ======== terraform.apply(str(infrastructure_dir))
def output(directory=None): logger.info(f"terraform output directory={directory}") with timer(logger, "terraform output"): output = subprocess.check_output("terraform output -json", shell=True, cwd=directory).decode("utf8") return json.loads(output)
def apply(directory=None, targets=None): targets = targets or [] logger.info(f"terraform apply directory={directory} targets={targets}") with timer(logger, "terraform apply"): command = " ".join(["terraform", "apply", "-auto-approve"] + ["-target=" + _ for _ in targets]) subprocess.check_output(command, shell=True, cwd=directory)
def init(directory=None): logger.info(f"terraform init directory={directory}") with timer(logger, "terraform init"): subprocess.check_output("terraform init", shell=True, cwd=directory)
def destroy(directory=None): logger.info(f"terraform destroy directory={directory}") with timer(logger, "terraform destroy"): command = "terraform destroy -auto-approve" subprocess.check_output(command, shell=True, cwd=directory)