33import json
44import time
55
6- from os .path import join , dirname , relpath , basename
6+ from os .path import join , relpath , basename , exists , abspath
77from cloudharness_model import ApplicationTestConfig , HarnessMainConfig
88
99from cloudharness_utils .constants import APPS_PATH , DEPLOYMENT_CONFIGURATION_PATH , \
@@ -51,7 +51,7 @@ def build_artifact(image_name, context_path, requirements=None, dockerfile_path=
5151 in requirements ]
5252 return artifact_spec
5353
54-
54+
5555 base_images = set ()
5656
5757 def process_build_dockerfile (dockerfile_path , root_path , global_context = False , requirements = None , app_name = None ):
@@ -78,21 +78,21 @@ def process_build_dockerfile(dockerfile_path, root_path, global_context=False, r
7878
7979 for dockerfile_path in base_dockerfiles :
8080 process_build_dockerfile (dockerfile_path , root_path , global_context = True )
81-
82-
81+
82+
8383 release_config = skaffold_conf ['deploy' ]['helm' ]['releases' ][0 ]
8484 release_config ['name' ] = helm_values .namespace
8585 release_config ['namespace' ] = helm_values .namespace
8686 release_config ['artifactOverrides' ][KEY_APPS ] = {}
87-
87+
8888 static_images = set ()
8989 for root_path in root_paths :
9090 static_dockerfiles = find_dockerfiles_paths (
9191 join (root_path , STATIC_IMAGES_PATH ))
9292
9393 for dockerfile_path in static_dockerfiles :
9494 process_build_dockerfile (dockerfile_path , root_path )
95-
95+
9696
9797 for root_path in root_paths :
9898 apps_path = join (root_path , APPS_PATH )
@@ -138,22 +138,45 @@ def process_build_dockerfile(dockerfile_path, root_path, global_context=False, r
138138 }
139139 }
140140
141- flask_main = find_file_paths (context_path , '__main__.py' )
142-
143- if flask_main :
141+ mains_candidates = find_file_paths (context_path , '__main__.py' )
142+
143+ def identify_unicorn_based_main (candidates ):
144+ import re
145+ gunicorn_pattern = re .compile (r"gunicorn" )
146+ for candidate in candidates :
147+ dockerfile_path = f"{ candidate } /.."
148+ while not exists (f"{ dockerfile_path } /Dockerfile" ) and abspath (dockerfile_path ) != abspath (root_path ):
149+ dockerfile_path += "/.."
150+ dockerfile = f"{ dockerfile_path } /Dockerfile"
151+ if not exists (dockerfile ):
152+ continue
153+ with open (dockerfile , 'r' ) as file :
154+ if re .search (gunicorn_pattern , file .read ()):
155+ return candidate
156+ requirements = f"{ candidate } /../requirements.txt"
157+ if not exists (requirements ):
158+ continue
159+ with open (requirements , 'r' ) as file :
160+ if re .search (gunicorn_pattern , file .read ()):
161+ return candidate
162+ return None
163+
164+ task_main_file = identify_unicorn_based_main (mains_candidates )
165+
166+ if task_main_file :
144167 release_config ['overrides' ]['apps' ][app_key ] = \
145168 {
146169 'harness' : {
147170 'deployment' : {
148171 'command' : ['python' ],
149- 'args' : [f'/usr/src/app/{ os .path .basename (flask_main [ 0 ] )} /__main__.py' ]
172+ 'args' : [f'/usr/src/app/{ os .path .basename (task_main_file )} /__main__.py' ]
150173 }
151174 }
152175 }
153-
176+
154177 test_config : ApplicationTestConfig = helm_values .apps [app_key ].harness .test
155178 if test_config .unit .enabled and test_config .unit .commands :
156-
179+
157180 skaffold_conf ['test' ].append (dict (
158181 image = get_image_tag (app_name ),
159182 custom = [dict (command = "docker run $IMAGE " + cmd ) for cmd in test_config .unit .commands ]
@@ -209,7 +232,7 @@ def get_image_tag(name):
209232 "/usr/src/app" ),
210233 }
211234 })
212-
235+
213236
214237 if not os .path .exists (os .path .dirname (vscode_launch_path )):
215238 os .makedirs (os .path .dirname (vscode_launch_path ))
0 commit comments