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