Skip to content

Commit aed885e

Browse files
committed
...
1 parent 18b038e commit aed885e

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

runtime-deploy-action/script.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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)

runtime-iac-action/script.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
14+
def check(result: subprocess.CompletedProcess) -> None:
15+
"""
16+
Checks the result of a subprocess execution. If the return code is non-zero,
17+
it logs an error message and exits the program.
18+
19+
Args:
20+
result (subprocess.CompletedProcess): The result of the subprocess execution.
21+
"""
22+
if result.returncode != 0:
23+
logging.error(f"Failed to execute: {result.args}")
24+
logging.error(f"Error output: {result.stderr}")
25+
sys.exit(1)
26+
27+
def run_command(command: List[str]) -> subprocess.CompletedProcess:
28+
"""
29+
Runs a command using subprocess and returns the result.
30+
31+
Args:
32+
command (List[str]): The command to be executed as a list of strings.
33+
34+
Returns:
35+
subprocess.CompletedProcess: The result of the command execution.
36+
"""
37+
try:
38+
logging.info(f"Running command: {' '.join(command)}")
39+
result = subprocess.run(command, capture_output=True, text=True)
40+
check(result)
41+
return result
42+
except Exception as e:
43+
logging.error(f"Exception occurred while running command: {command}")
44+
logging.error(str(e))
45+
sys.exit(1)
46+
47+
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+
57+
docker_flags: dict = dict(
58+
FEATURES_LEVEL_LOG=inputs.get("features_level_log") or "info",
59+
60+
AUTHENTICATE_CLIENT_ID=inputs["client_id"],
61+
AUTHENTICATE_CLIENT_SECRET=inputs["client_key"],
62+
AUTHENTICATE_CLIENT_REALMS=inputs["client_realm"],
63+
REPOSITORY_NAME=inputs["repository_name"],
64+
AWS_REGION=inputs["aws_region"],
65+
66+
AUTHENTICATE_URL=STK_IAM_DOMAIN,
67+
FEATURES_API_MANAGER=STK_RUNTIME_MANAGER_DOMAIN,
68+
)
69+
70+
cmd = [
71+
"docker",
72+
"run", "--rm",
73+
"--entrypoint=/app/stackspot-runtime-job-iac",
74+
container_url,
75+
"start",
76+
f"--run-task-id={run_task_id}",
77+
f"--base-path-output={base_path_output}",
78+
"-v", path_to_mount
79+
]
80+
flags = []
81+
for k, v in docker_flags.items():
82+
flags += ["-e", f"{k}={v}"]
83+
docker_run = cmd + flags
84+
85+
run_command(docker_run)

0 commit comments

Comments
 (0)