|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import subprocess |
| 4 | +import logging |
| 5 | +from typing import List |
| 6 | + |
| 7 | +# Configure logging |
| 8 | +logging.basicConfig( |
| 9 | + level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +STK_IAM_DOMAIN = os.getenv("STK_IAM_DOMAIN", "https://idm.stackspot.com") |
| 14 | +STK_RUNTIME_MANAGER_DOMAIN = os.getenv( |
| 15 | + "STK_RUNTIME_MANAGER_DOMAIN", "https://runtime-manager.v1.stackspot.com" |
| 16 | +) |
| 17 | +CONTAINER_UNIFIED_URL = os.getenv( |
| 18 | + "CONTAINER_UNIFIED_URL", "stackspot/runtime-job-unified:latest" |
| 19 | +) |
| 20 | + |
| 21 | +FEATURES_BASEPATH_TMP = "/tmp/runtime/deploys" |
| 22 | +FEATURES_BASEPATH_EBS = "/opt/runtime" |
| 23 | +FEATURES_TEMPLATES_FILEPATH = "/app/" |
| 24 | +FEATURES_BASEPATH_TERRAFORM = "/root/.asdf/shims/terraform" |
| 25 | + |
| 26 | + |
| 27 | +def check(result: subprocess.Popen) -> None: |
| 28 | + """ |
| 29 | + Checks the result of a subprocess execution. If the return code is non-zero, |
| 30 | + it logs an error message and exits the program. |
| 31 | +
|
| 32 | + Args: |
| 33 | + result (subprocess.Popen): The result of the subprocess execution. |
| 34 | + """ |
| 35 | + result.wait() # Wait for the process to complete |
| 36 | + if result.returncode != 0: |
| 37 | + logging.error(f"Failed to execute: {result.args}") |
| 38 | + logging.error(f"Error output: {result.stderr.read()}") |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + |
| 42 | +def run_command(command: List[str]) -> subprocess.Popen: |
| 43 | + """ |
| 44 | + Runs a command using subprocess.Popen and returns the result. |
| 45 | +
|
| 46 | + Args: |
| 47 | + command (List[str]): The command to be executed as a list of strings. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + subprocess.Popen: The result of the command execution. |
| 51 | + """ |
| 52 | + try: |
| 53 | + logging.info(f"Running command: {' '.join(command)}") |
| 54 | + # Start the process |
| 55 | + process = subprocess.Popen( |
| 56 | + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True |
| 57 | + ) |
| 58 | + |
| 59 | + # Read and print output in real-time |
| 60 | + for line in process.stdout: |
| 61 | + print(line, end="") # Print each line as it is produced |
| 62 | + |
| 63 | + # Check the result after the process completes |
| 64 | + check(process) |
| 65 | + return process |
| 66 | + except Exception as e: |
| 67 | + logging.error(f"Exception occurred while running command: {command}") |
| 68 | + logging.error(str(e)) |
| 69 | + sys.exit(1) |
| 70 | + |
| 71 | + |
| 72 | +def build_flags(inputs: dict) -> list: |
| 73 | + |
| 74 | + TF_PARALLELISM=f"-parallelism={inputs.get('tf_parallelism') or '10'}" |
| 75 | + |
| 76 | + docker_flags: dict = dict( |
| 77 | + FEATURES_LEVEL_LOG=inputs.get("features_level_log") or "info", |
| 78 | + FEATURES_TERRAFORM_LOGPROVIDER=inputs.get("tf_log_provider") or "info", |
| 79 | + FEATURES_RELEASE_LOCALEXEC=inputs.get("localexec_enabled") or "False", |
| 80 | + FEATURES_TERRAFORM_MODULES=inputs.get("features_terraform_modules") or "[]", |
| 81 | + AWS_ACCESS_KEY_ID=inputs["aws_access_key_id"], |
| 82 | + AWS_SECRET_ACCESS_KEY=inputs["aws_secret_access_key"], |
| 83 | + AWS_SESSION_TOKEN=inputs["aws_session_token"], |
| 84 | + AUTHENTICATE_CLIENT_ID=inputs["client_id"], |
| 85 | + AUTHENTICATE_CLIENT_SECRET=inputs["client_key"], |
| 86 | + AUTHENTICATE_CLIENT_REALMS=inputs["client_realm"], |
| 87 | + REPOSITORY_NAME=inputs["repository_name"], |
| 88 | + AWS_REGION=inputs["aws_region"], |
| 89 | + AUTHENTICATE_URL=STK_IAM_DOMAIN, |
| 90 | + FEATURES_API_MANAGER=STK_RUNTIME_MANAGER_DOMAIN, |
| 91 | + FEATURES_BASEPATH_TMP=FEATURES_BASEPATH_TMP, |
| 92 | + FEATURES_BASEPATH_EBS=FEATURES_BASEPATH_EBS, |
| 93 | + FEATURES_TEMPLATES_FILEPATH=FEATURES_TEMPLATES_FILEPATH, |
| 94 | + FEATURES_BASEPATH_TERRAFORM=FEATURES_BASEPATH_TERRAFORM, |
| 95 | + TF_CLI_ARGS_apply=TF_PARALLELISM, |
| 96 | + TF_CLI_ARGS_plan=TF_PARALLELISM, |
| 97 | + TF_CLI_ARGS_destroy=TF_PARALLELISM |
| 98 | + |
| 99 | + ) |
| 100 | + flags = [] |
| 101 | + for k, v in docker_flags.items(): |
| 102 | + flags += ["-e", f"{k}={v}"] |
| 103 | + |
| 104 | + return flags |
1 | 105 |
|
2 | 106 |
|
3 | 107 | def run(metadata): |
4 | | - print(f"Hello {metadata.inputs.get('user_name')}!") |
| 108 | + inputs: dict = metadata.inputs |
| 109 | + run_id: str = inputs["run_id"] |
| 110 | + base_path_output: str = inputs.get("base_path_output") or "." |
| 111 | + path_to_mount: str = inputs.get("path_to_mount") or "." |
| 112 | + path_to_mount = f"{path_to_mount}:/app-volume" |
| 113 | + |
| 114 | + flags = build_flags(inputs) |
| 115 | + cmd = ( |
| 116 | + ["docker", "run", "--rm", "-v", path_to_mount] |
| 117 | + + flags |
| 118 | + + [ |
| 119 | + "--entrypoint=/app/stackspot-runtime-job", |
| 120 | + CONTAINER_UNIFIED_URL, |
| 121 | + "start", |
| 122 | + f"--run-id={run_id}", |
| 123 | + f"--base-path-output={base_path_output}", |
| 124 | + ] |
| 125 | + ) |
| 126 | + |
| 127 | + run_command(cmd) |
0 commit comments