|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import subprocess |
| 4 | +import logging |
| 5 | + |
| 6 | +# Configure logging |
| 7 | +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
| 8 | + |
| 9 | + |
| 10 | +STK_IAM_DOMAIN = os.getenv("STK_IAM_DOMAIN", "https://idm.stackspot.com") |
| 11 | +STK_RUNTIME_MANAGER_DOMAIN = os.getenv("STK_RUNTIME_MANAGER_DOMAIN", "https://runtime-manager.v1.stackspot.com") |
| 12 | + |
| 13 | +FEATURES_BASEPATH_TMP = "/tmp/runtime/deploys" |
| 14 | +FEATURES_BASEPATH_EBS = "/opt/runtime" |
| 15 | +FEATURES_TEMPLATES_FILEPATH = "/app/" |
| 16 | +FEATURES_BASEPATH_TERRAFORM = "/root/.asdf/shims/terraform" |
| 17 | + |
| 18 | +def check(result: subprocess.CompletedProcess) -> None: |
| 19 | + """ |
| 20 | + Checks the result of a subprocess execution. If the return code is non-zero, |
| 21 | + it logs an error message and exits the program. |
| 22 | +
|
| 23 | + Args: |
| 24 | + result (subprocess.CompletedProcess): The result of the subprocess execution. |
| 25 | + """ |
| 26 | + if result.returncode != 0: |
| 27 | + logging.error(f"Failed to execute: {result.args}") |
| 28 | + logging.error(f"Error output: {result.stderr}") |
| 29 | + sys.exit(1) |
| 30 | + |
| 31 | +def run_command(command: List[str]) -> subprocess.CompletedProcess: |
| 32 | + """ |
| 33 | + Runs a command using subprocess and returns the result. |
| 34 | +
|
| 35 | + Args: |
| 36 | + command (List[str]): The command to be executed as a list of strings. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + subprocess.CompletedProcess: The result of the command execution. |
| 40 | + """ |
| 41 | + try: |
| 42 | + logging.info(f"Running command: {' '.join(command)}") |
| 43 | + result = subprocess.run(command, capture_output=True, text=True) |
| 44 | + check(result) |
| 45 | + return result |
| 46 | + except Exception as e: |
| 47 | + logging.error(f"Exception occurred while running command: {command}") |
| 48 | + logging.error(str(e)) |
| 49 | + sys.exit(1) |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +def run(metadata): |
| 54 | + inputs: dict = metadata.inputs |
| 55 | + container_url: str = 'stackspot/runtime-job-deploy:latest' |
| 56 | + run_task_id: str = inputs["run_task_id"] |
| 57 | + output_file: str = inputs.get("output_file") or "deploy-output.json" |
| 58 | + path_to_mount: str = inputs.get("path_to_mount") or "." |
| 59 | + path_to_mount = f"{path_to_mount}:/app-volume" |
| 60 | + |
| 61 | + docker_flags: dict = dict( |
| 62 | + FEATURES_LEVEL_LOG=inputs.get("features_level_log") or "info", |
| 63 | + FEATURES_TERRAFORM_LOGPROVIDER=inputs.get("tf_log_provider") or "info", |
| 64 | + FEATURES_RELEASE_LOCALEXEC=inputs.get("localexec_enabled") or "False", |
| 65 | + FEATURES_TERRAFORM_MODULES=inputs.get("features_terraform_modules") or '[]', |
| 66 | + |
| 67 | + AWS_ACCESS_KEY_ID=inputs['aws_access_key_id'], |
| 68 | + AWS_SECRET_ACCESS_KEY=inputs['aws_secret_access_key'], |
| 69 | + AWS_SESSION_TOKEN=inputs['aws_session_token'], |
| 70 | + AUTHENTICATE_CLIENT_ID=inputs["client_id"], |
| 71 | + AUTHENTICATE_CLIENT_SECRET=inputs["client_key"], |
| 72 | + AUTHENTICATE_CLIENT_REALMS=inputs["client_realm"], |
| 73 | + REPOSITORY_NAME=inputs["repository_name"], |
| 74 | + AWS_REGION=inputs["aws_region"], |
| 75 | + |
| 76 | + AUTHENTICATE_URL=STK_IAM_DOMAIN, |
| 77 | + FEATURES_API_MANAGER=STK_RUNTIME_MANAGER_DOMAIN, |
| 78 | + FEATURES_BASEPATH_TMP=FEATURES_BASEPATH_TMP, |
| 79 | + FEATURES_BASEPATH_EBS=FEATURES_BASEPATH_EBS, |
| 80 | + FEATURES_TEMPLATES_FILEPATH=FEATURES_TEMPLATES_FILEPATH, |
| 81 | + FEATURES_BASEPATH_TERRAFORM=FEATURES_BASEPATH_TERRAFORM |
| 82 | + ) |
| 83 | + |
| 84 | + cmd = [ |
| 85 | + "docker", |
| 86 | + "run", "--rm", |
| 87 | + "--entrypoint=/app/stackspot-runtime-job-deploy" |
| 88 | + container_url, |
| 89 | + "start", |
| 90 | + f"--run-task-id={run_task_id}", |
| 91 | + f"--output-file={output_file}", |
| 92 | + "-v", path_to_mount |
| 93 | + ] |
| 94 | + flags = [] |
| 95 | + for k, v in docker_flags.items(): |
| 96 | + flags += ["-e", f"{k}={v}"] |
| 97 | + docker_run = cmd + flags |
| 98 | + |
| 99 | + run_command(docker_run) |
0 commit comments