1616FEATURES_TEMPLATES_FILEPATH = "/app/"
1717FEATURES_BASEPATH_TERRAFORM = "/root/.asdf/shims/terraform"
1818
19- def check (result : subprocess .CompletedProcess ) -> None :
19+
20+ def check (result : subprocess .Popen ) -> None :
2021 """
2122 Checks the result of a subprocess execution. If the return code is non-zero,
2223 it logs an error message and exits the program.
2324
2425 Args:
25- result (subprocess.CompletedProcess ): The result of the subprocess execution.
26+ result (subprocess.Popen ): The result of the subprocess execution.
2627 """
28+ result .wait () # Wait for the process to complete
2729 if result .returncode != 0 :
2830 logging .error (f"Failed to execute: { result .args } " )
29- logging .error (f"Error output: { result .stderr } " )
31+ logging .error (f"Error output: { result .stderr . read () } " )
3032 sys .exit (1 )
3133
32- def run_command (command : List [str ]) -> subprocess .CompletedProcess :
34+
35+ def run_command (command : List [str ]) -> subprocess .Popen :
3336 """
34- Runs a command using subprocess and returns the result.
37+ Runs a command using subprocess.Popen and returns the result.
3538
3639 Args:
3740 command (List[str]): The command to be executed as a list of strings.
3841
3942 Returns:
40- subprocess.CompletedProcess : The result of the command execution.
43+ subprocess.Popen : The result of the command execution.
4144 """
4245 try :
4346 logging .info (f"Running command: { ' ' .join (command )} " )
44- result = subprocess .run (command , capture_output = True , text = True )
45- check (result )
46- return result
47+ # Start the process
48+ process = subprocess .Popen (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
49+
50+ # Read and print output in real-time
51+ for line in process .stdout :
52+ print (line , end = "" ) # Print each line as it is produced
53+
54+ # Check the result after the process completes
55+ check (process )
56+ return process
4757 except Exception as e :
4858 logging .error (f"Exception occurred while running command: { command } " )
4959 logging .error (str (e ))
5060 sys .exit (1 )
5161
5262
53-
54- def run (metadata ):
55- inputs : dict = metadata .inputs
56- container_url : str = 'stackspot/runtime-job-deploy:latest'
57- run_task_id : str = inputs ["run_task_id" ]
58- output_file : str = inputs .get ("output_file" ) or "deploy-output.json"
59- path_to_mount : str = inputs .get ("path_to_mount" ) or "."
60- path_to_mount = f"{ path_to_mount } :/app-volume"
61-
63+ def build_flags (inputs : dict ) -> list :
64+
6265 docker_flags : dict = dict (
6366 FEATURES_LEVEL_LOG = inputs .get ("features_level_log" ) or "info" ,
6467 FEATURES_TERRAFORM_LOGPROVIDER = inputs .get ("tf_log_provider" ) or "info" ,
@@ -81,20 +84,28 @@ def run(metadata):
8184 FEATURES_TEMPLATES_FILEPATH = FEATURES_TEMPLATES_FILEPATH ,
8285 FEATURES_BASEPATH_TERRAFORM = FEATURES_BASEPATH_TERRAFORM
8386 )
87+ flags = []
88+ for k , v in docker_flags .items ():
89+ flags += ["-e" , f"{ k } ={ v } " ]
90+
91+ return flags
92+
8493
85- cmd = [
86- "docker" ,
87- "run" , "--rm" ,
94+ def run (metadata ):
95+ inputs : dict = metadata .inputs
96+ container_url : str = 'stackspot/runtime-job-deploy:latest'
97+ run_task_id : str = inputs ["run_task_id" ]
98+ output_file : str = inputs .get ("output_file" ) or "deploy-output.json"
99+ path_to_mount : str = inputs .get ("path_to_mount" ) or "."
100+ path_to_mount = f"{ path_to_mount } :/app-volume"
101+
102+ flags = build_flags (inputs )
103+ cmd = ["docker" , "run" , "--rm" , "-v" , path_to_mount ] + flags + [
88104 "--entrypoint=/app/stackspot-runtime-job-deploy" ,
89105 container_url ,
90106 "start" ,
91107 f"--run-task-id={ run_task_id } " ,
92108 f"--output-file={ output_file } " ,
93- "-v" , path_to_mount
94109 ]
95- flags = []
96- for k , v in docker_flags .items ():
97- flags += ["-e" , f"{ k } ={ v } " ]
98- docker_run = cmd + flags
99110
100- run_command (docker_run )
111+ run_command (cmd )
0 commit comments