|
| 1 | +# Apache License Version 2.0 |
| 2 | +# |
| 3 | +# Copyright (c) 2021., Redis Labs Modules |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +import json |
| 7 | +import logging |
| 8 | +import os |
| 9 | + |
| 10 | +from redisbench_admin.run.git import git_vars_crosscheck |
| 11 | +from redisbench_admin.run.ssh import ssh_pem_check |
| 12 | +from redisbench_admin.utils.remote import ( |
| 13 | + fetch_remote_setup_git_url, |
| 14 | + setup_remote_environment, |
| 15 | +) |
| 16 | +from python_terraform import Terraform |
| 17 | + |
| 18 | +from redisbench_admin.utils.utils import EC2_PRIVATE_PEM |
| 19 | + |
| 20 | + |
| 21 | +def deploy_command_logic(args, project_name, project_version): |
| 22 | + logging.info( |
| 23 | + "Using: {project_name} {project_version}".format( |
| 24 | + project_name=project_name, project_version=project_version |
| 25 | + ) |
| 26 | + ) |
| 27 | + terraform_bin_path = args.terraform_bin_path |
| 28 | + tf_github_org = args.github_org |
| 29 | + tf_github_actor = args.github_actor |
| 30 | + tf_github_repo = args.github_repo |
| 31 | + tf_github_sha = args.github_sha |
| 32 | + tf_github_branch = args.github_branch |
| 33 | + infra_timeout_secs = args.infra_timeout_secs |
| 34 | + |
| 35 | + ( |
| 36 | + tf_github_actor, |
| 37 | + tf_github_branch, |
| 38 | + tf_github_org, |
| 39 | + tf_github_repo, |
| 40 | + tf_github_sha, |
| 41 | + ) = git_vars_crosscheck( |
| 42 | + tf_github_actor, tf_github_branch, tf_github_org, tf_github_repo, tf_github_sha |
| 43 | + ) |
| 44 | + |
| 45 | + private_key = args.private_key |
| 46 | + ssh_pem_check(EC2_PRIVATE_PEM, private_key) |
| 47 | + inventory_git = args.inventory_git |
| 48 | + inventory_local_dir = args.inventory_local_dir |
| 49 | + destroy = args.destroy |
| 50 | + if inventory_local_dir is not None: |
| 51 | + if os.path.isdir(inventory_local_dir) is False: |
| 52 | + os.mkdir(inventory_local_dir) |
| 53 | + ( |
| 54 | + remote_setup, |
| 55 | + deployment_type, |
| 56 | + remote_id, |
| 57 | + ) = fetch_remote_setup_git_url(inventory_git, inventory_local_dir, destroy) |
| 58 | + tf = Terraform( |
| 59 | + working_dir=remote_setup, |
| 60 | + terraform_bin_path=terraform_bin_path, |
| 61 | + ) |
| 62 | + tf_setup_name_sufix = "{}-{}".format(args.setup_name_sufix, tf_github_sha) |
| 63 | + tf_setup_name = "{}{}".format(remote_setup, tf_setup_name_sufix) |
| 64 | + terraform_backend_key = "benchmarks/infrastructure/{}.tfstate".format(tf_setup_name) |
| 65 | + tf_triggering_env = "redisbench-admin-deploy" |
| 66 | + logging.info("Setting an infra timeout of {} secs".format(infra_timeout_secs)) |
| 67 | + if args.destroy is False: |
| 68 | + (tf_return_code, _, _, _, _, _, _,) = setup_remote_environment( |
| 69 | + tf, |
| 70 | + tf_github_sha, |
| 71 | + tf_github_actor, |
| 72 | + tf_setup_name, |
| 73 | + tf_github_org, |
| 74 | + tf_github_repo, |
| 75 | + tf_triggering_env, |
| 76 | + infra_timeout_secs, |
| 77 | + ) |
| 78 | + logging.info("Deploy terraform return code {}".format(tf_return_code)) |
| 79 | + env_json = tf.output() |
| 80 | + output_dict = {} |
| 81 | + for k, v in env_json.items(): |
| 82 | + k = k.upper() |
| 83 | + if type(v["value"]) == list: |
| 84 | + output_dict[k] = v["value"][0] |
| 85 | + else: |
| 86 | + output_dict[k] = v["value"] |
| 87 | + |
| 88 | + logging.info("JSON env variables {}".format(output_dict)) |
| 89 | + if args.set_env_vars_json != "": |
| 90 | + with open(args.set_env_vars_json, "w") as json_fd: |
| 91 | + json.dump(output_dict, json_fd) |
| 92 | + else: |
| 93 | + _, _, _ = tf.init( |
| 94 | + capture_output=True, |
| 95 | + backend_config={"key": terraform_backend_key}, |
| 96 | + ) |
| 97 | + logging.info("Refreshing remote state") |
| 98 | + _, _, _ = tf.refresh() |
| 99 | + logging.info("Triggering destroy") |
| 100 | + output = tf.destroy(capture_output=True) |
| 101 | + logging.info("Finished destroying the remote env. Output:") |
| 102 | + for line in output[1].split("\n"): |
| 103 | + print(line) |
0 commit comments