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