@@ -116,8 +116,11 @@ def __init__(self,
116
116
self ._sci = {'R_d' : None , 'R' : 0 }
117
117
self ._job_id = job_id
118
118
self ._arguments = locals ()
119
+ self ._folder = f"{ self ._tmp_folder } /repo" # default if not changed in checkout_repository
120
+ self ._run_id = None
119
121
self ._commit_hash = None
120
122
self ._commit_timestamp = None
123
+
121
124
del self ._arguments ['self' ] # self is not needed and also cannot be serialzed. We remove it
122
125
123
126
@@ -137,8 +140,6 @@ def __init__(self,
137
140
self .__services_to_pause_phase = {}
138
141
self .__join_default_network = False
139
142
self .__docker_params = []
140
- self .__folder = f"{ self ._tmp_folder } /repo" # default if not changed in checkout_repository
141
- self .__run_id = None
142
143
143
144
# we currently do not use this variable
144
145
# self.__filename = self._original_filename # this can be changed later if working directory changes
@@ -152,21 +153,20 @@ def initialize_run(self):
152
153
# We issue a fetch_one() instead of a query() here, cause we want to get the RUN_ID
153
154
154
155
# we also update the branch here again, as this might not be main in case of local filesystem
155
-
156
- self .__run_id = DB ().fetch_one ("""
156
+ self ._run_id = DB ().fetch_one ("""
157
157
INSERT INTO runs (job_id, name, uri, email, branch, filename, commit_hash, commit_timestamp, runner_arguments, created_at)
158
158
VALUES (%s, %s, %s, 'manual', %s, %s, %s, %s, %s, NOW())
159
159
RETURNING id
160
160
""" , params = (self ._job_id , self ._name , self ._uri , self ._branch , self ._original_filename , self ._commit_hash , self ._commit_timestamp , json .dumps (self ._arguments )))[0 ]
161
- return self .__run_id
161
+ return self ._run_id
162
162
163
163
def initialize_folder (self , path ):
164
164
shutil .rmtree (path , ignore_errors = True )
165
165
Path (path ).mkdir (parents = True , exist_ok = True )
166
166
167
167
def save_notes_runner (self ):
168
168
print (TerminalColors .HEADER , '\n Saving notes: ' , TerminalColors .ENDC , self .__notes_helper .get_notes ())
169
- self .__notes_helper .save_to_db (self .__run_id )
169
+ self .__notes_helper .save_to_db (self ._run_id )
170
170
171
171
def check_system (self , mode = 'start' ):
172
172
if self ._skip_system_checks :
@@ -198,7 +198,7 @@ def checkout_repository(self):
198
198
'--recurse-submodules' ,
199
199
'--shallow-submodules' ,
200
200
self ._uri ,
201
- self .__folder
201
+ self ._folder
202
202
],
203
203
check = True ,
204
204
capture_output = True ,
@@ -214,7 +214,7 @@ def checkout_repository(self):
214
214
'--recurse-submodules' ,
215
215
'--shallow-submodules' ,
216
216
self ._uri ,
217
- self .__folder
217
+ self ._folder
218
218
],
219
219
check = True ,
220
220
capture_output = True ,
@@ -225,9 +225,9 @@ def checkout_repository(self):
225
225
if self ._branch :
226
226
# we never want to checkout a local directory to a different branch as this might also be the GMT directory itself and might confuse the tool
227
227
raise RuntimeError ('Specified --branch but using local URI. Did you mean to specify a github url?' )
228
- self .__folder = self ._uri
228
+ self ._folder = self ._uri
229
229
230
- self ._branch = subprocess .check_output (['git' , 'branch' , '--show-current' ], cwd = self .__folder , encoding = 'UTF-8' ).strip ()
230
+ self ._branch = subprocess .check_output (['git' , 'branch' , '--show-current' ], cwd = self ._folder , encoding = 'UTF-8' ).strip ()
231
231
232
232
# we can safely do this, even with problematic folders, as the folder can only be a local unsafe one when
233
233
# running in CLI mode
@@ -236,7 +236,7 @@ def checkout_repository(self):
236
236
check = True ,
237
237
capture_output = True ,
238
238
encoding = 'UTF-8' ,
239
- cwd = self .__folder
239
+ cwd = self ._folder
240
240
)
241
241
self ._commit_hash = self ._commit_hash .stdout .strip ("\n " )
242
242
@@ -245,8 +245,9 @@ def checkout_repository(self):
245
245
check = True ,
246
246
capture_output = True ,
247
247
encoding = 'UTF-8' ,
248
- cwd = self .__folder
248
+ cwd = self ._folder
249
249
)
250
+
250
251
self ._commit_timestamp = self ._commit_timestamp .stdout .strip ("\n " )
251
252
self ._commit_timestamp = datetime .strptime (self ._commit_timestamp , "%Y-%m-%d %H:%M:%S %z" )
252
253
@@ -295,13 +296,13 @@ def recursive_lookup(k, d):
295
296
296
297
Loader .add_constructor ('!include' , Loader .include )
297
298
298
- usage_scenario_file = join_paths (self .__folder , self ._original_filename , 'file' )
299
+ usage_scenario_file = join_paths (self ._folder , self ._original_filename , 'file' )
299
300
300
301
# We set the working folder now to the actual location of the usage_scenario
301
302
if '/' in self ._original_filename :
302
- self .__folder = usage_scenario_file .rsplit ('/' , 1 )[0 ]
303
+ self ._folder = usage_scenario_file .rsplit ('/' , 1 )[0 ]
303
304
#self.__filename = usage_scenario_file.rsplit('/', 1)[1] # we currently do not use this variable
304
- print ("Working folder changed to " , self .__folder )
305
+ print ("Working folder changed to " , self ._folder )
305
306
306
307
307
308
with open (usage_scenario_file , 'r' , encoding = 'utf-8' ) as fp :
@@ -472,7 +473,7 @@ def update_and_insert_specs(self):
472
473
json .dumps (measurement_config ),
473
474
escape (json .dumps (self ._usage_scenario ), quote = False ),
474
475
gmt_hash ,
475
- self .__run_id )
476
+ self ._run_id )
476
477
)
477
478
478
479
def import_metric_providers (self ):
@@ -573,11 +574,11 @@ def build_docker_images(self):
573
574
self .__notes_helper .add_note ({'note' : f"Building { service ['image' ]} " , 'detail_name' : '[NOTES]' , 'timestamp' : int (time .time_ns () / 1_000 )})
574
575
575
576
# Make sure the context docker file exists and is not trying to escape some root. We don't need the returns
576
- context_path = join_paths (self .__folder , context , 'dir' )
577
+ context_path = join_paths (self ._folder , context , 'dir' )
577
578
join_paths (context_path , dockerfile , 'file' )
578
579
579
580
docker_build_command = ['docker' , 'run' , '--rm' ,
580
- '-v' , f"{ self .__folder } :/workspace:ro" , # this is the folder where the usage_scenario is!
581
+ '-v' , f"{ self ._folder } :/workspace:ro" , # this is the folder where the usage_scenario is!
581
582
'-v' , f"{ temp_dir } :/output" ,
582
583
'gcr.io/kaniko-project/executor:latest' ,
583
584
f"--dockerfile=/workspace/{ context } /{ dockerfile } " ,
@@ -706,9 +707,9 @@ def setup_services(self):
706
707
707
708
docker_run_string .append ('-v' )
708
709
if 'folder-destination' in service :
709
- docker_run_string .append (f"{ self .__folder } :{ service ['folder-destination' ]} :ro" )
710
+ docker_run_string .append (f"{ self ._folder } :{ service ['folder-destination' ]} :ro" )
710
711
else :
711
- docker_run_string .append (f"{ self .__folder } :/tmp/repo:ro" )
712
+ docker_run_string .append (f"{ self ._folder } :/tmp/repo:ro" )
712
713
713
714
if self .__docker_params :
714
715
docker_run_string [2 :2 ] = self .__docker_params
@@ -727,7 +728,7 @@ def setup_services(self):
727
728
docker_run_string .append ('-v' )
728
729
if volume .startswith ('./' ): # we have a bind-mount with relative path
729
730
vol = volume .split (':' ,1 ) # there might be an :ro etc at the end, so only split once
730
- path = os .path .realpath (os .path .join (self .__folder , vol [0 ]))
731
+ path = os .path .realpath (os .path .join (self ._folder , vol [0 ]))
731
732
if not os .path .exists (path ):
732
733
raise RuntimeError (f"Service '{ service_name } ' volume path does not exist: { path } " )
733
734
docker_run_string .append (f"{ path } :{ vol [1 ]} " )
@@ -740,16 +741,16 @@ def setup_services(self):
740
741
vol = volume .split (':' )
741
742
# We always assume the format to be ./dir:dir:[flag] as if we allow none bind mounts people
742
743
# could create volumes that would linger on our system.
743
- path = os .path .realpath (os .path .join (self .__folder , vol [0 ]))
744
+ path = os .path .realpath (os .path .join (self ._folder , vol [0 ]))
744
745
if not os .path .exists (path ):
745
746
raise RuntimeError (f"Service '{ service_name } ' volume path does not exist: { path } " )
746
747
747
- # Check that the path starts with self.__folder
748
- if not path .startswith (self .__folder ):
748
+ # Check that the path starts with self._folder
749
+ if not path .startswith (self ._folder ):
749
750
raise RuntimeError (f"Service '{ service_name } ' trying to escape folder: { path } " )
750
751
751
752
# To double check we also check if it is in the files allow list
752
- if path not in [str (item ) for item in Path (self .__folder ).rglob ("*" )]:
753
+ if path not in [str (item ) for item in Path (self ._folder ).rglob ("*" )]:
753
754
raise RuntimeError (f"Service '{ service_name } ' volume '{ path } ' not in allowed file list" )
754
755
755
756
if len (vol ) == 3 :
@@ -1100,7 +1101,7 @@ def stop_metric_providers(self):
1100
1101
1101
1102
metric_provider .stop_profiling ()
1102
1103
1103
- df = metric_provider .read_metrics (self .__run_id , self .__containers )
1104
+ df = metric_provider .read_metrics (self ._run_id , self .__containers )
1104
1105
if isinstance (df , int ):
1105
1106
print ('Imported' , TerminalColors .HEADER , df , TerminalColors .ENDC , 'metrics from ' , metric_provider .__class__ .__name__ )
1106
1107
# If df returns an int the data has already been committed to the db
@@ -1169,7 +1170,7 @@ def update_start_and_end_times(self):
1169
1170
UPDATE runs
1170
1171
SET start_measurement=%s, end_measurement=%s
1171
1172
WHERE id = %s
1172
- """ , params = (self .__start_measurement , self .__end_measurement , self .__run_id ))
1173
+ """ , params = (self .__start_measurement , self .__end_measurement , self ._run_id ))
1173
1174
1174
1175
def store_phases (self ):
1175
1176
print (TerminalColors .HEADER , '\n Updating phases in DB' , TerminalColors .ENDC )
@@ -1181,7 +1182,7 @@ def store_phases(self):
1181
1182
UPDATE runs
1182
1183
SET phases=%s
1183
1184
WHERE id = %s
1184
- """ , params = (json .dumps (self .__phases ), self .__run_id ))
1185
+ """ , params = (json .dumps (self .__phases ), self ._run_id ))
1185
1186
1186
1187
def read_container_logs (self ):
1187
1188
print (TerminalColors .HEADER , '\n Capturing container logs' , TerminalColors .ENDC )
@@ -1226,7 +1227,7 @@ def save_stdout_logs(self):
1226
1227
UPDATE runs
1227
1228
SET logs=%s
1228
1229
WHERE id = %s
1229
- """ , params = (logs_as_str , self .__run_id ))
1230
+ """ , params = (logs_as_str , self ._run_id ))
1230
1231
1231
1232
1232
1233
def cleanup (self ):
@@ -1266,8 +1267,6 @@ def cleanup(self):
1266
1267
self .__end_measurement = None
1267
1268
self .__join_default_network = False
1268
1269
#self.__filename = self._original_filename # # we currently do not use this variable
1269
- self .__folder = f"{ self ._tmp_folder } /repo"
1270
- self .__run_id = None
1271
1270
1272
1271
def run (self ):
1273
1272
'''
@@ -1280,13 +1279,12 @@ def run(self):
1280
1279
Methods thus will behave differently given the runner was instantiated with different arguments.
1281
1280
1282
1281
'''
1283
- return_run_id = None
1284
1282
try :
1285
1283
config = GlobalConfig ().config
1286
1284
self .check_system ('start' )
1287
1285
self .initialize_folder (self ._tmp_folder )
1288
1286
self .checkout_repository ()
1289
- return_run_id = self .initialize_run ()
1287
+ self .initialize_run ()
1290
1288
self .initial_parse ()
1291
1289
self .import_metric_providers ()
1292
1290
self .populate_image_names ()
@@ -1399,7 +1397,7 @@ def run(self):
1399
1397
1400
1398
print (TerminalColors .OKGREEN , arrows ('MEASUREMENT SUCCESSFULLY COMPLETED' ), TerminalColors .ENDC )
1401
1399
1402
- return return_run_id # we cannot return self.__run_id as this is reset in cleanup()
1400
+ return self ._run_id
1403
1401
1404
1402
if __name__ == '__main__' :
1405
1403
import argparse
@@ -1475,7 +1473,6 @@ def run(self):
1475
1473
sys .exit (1 )
1476
1474
GlobalConfig (config_name = args .config_override )
1477
1475
1478
- successful_run_id = None
1479
1476
runner = Runner (name = args .name , uri = args .uri , uri_type = run_type , filename = args .filename ,
1480
1477
branch = args .branch , debug_mode = args .debug , allow_unsafe = args .allow_unsafe ,
1481
1478
no_file_cleanup = args .no_file_cleanup , skip_system_checks = args .skip_system_checks ,
@@ -1486,7 +1483,7 @@ def run(self):
1486
1483
# Using a very broad exception makes sense in this case as we have excepted all the specific ones before
1487
1484
#pylint: disable=broad-except
1488
1485
try :
1489
- successful_run_id = runner .run () # Start main code
1486
+ runner .run () # Start main code
1490
1487
1491
1488
# this code should live at a different position.
1492
1489
# From a user perspective it makes perfect sense to run both jobs directly after each other
@@ -1499,24 +1496,24 @@ def run(self):
1499
1496
# loop over them issueing separate queries to the DB
1500
1497
from tools .phase_stats import build_and_store_phase_stats
1501
1498
1502
- print ("Run id is" , successful_run_id )
1503
- build_and_store_phase_stats (successful_run_id , runner ._sci )
1499
+ print ("Run id is" , runner . _run_id )
1500
+ build_and_store_phase_stats (runner . _run_id , runner ._sci )
1504
1501
1505
1502
1506
1503
print (TerminalColors .OKGREEN ,'\n \n ####################################################################################' )
1507
- print (f"Please access your report on the URL { GlobalConfig ().config ['cluster' ]['metrics_url' ]} /stats.html?id={ successful_run_id } " )
1504
+ print (f"Please access your report on the URL { GlobalConfig ().config ['cluster' ]['metrics_url' ]} /stats.html?id={ runner . _run_id } " )
1508
1505
print ('####################################################################################\n \n ' , TerminalColors .ENDC )
1509
1506
1510
1507
except FileNotFoundError as e :
1511
- error_helpers .log_error ('Docker command failed.' , e , successful_run_id )
1508
+ error_helpers .log_error ('Docker command failed.' , e , runner . _run_id )
1512
1509
except subprocess .CalledProcessError as e :
1513
- error_helpers .log_error ('Docker command failed' , 'Stdout:' , e .stdout , 'Stderr:' , e .stderr , successful_run_id )
1510
+ error_helpers .log_error ('Docker command failed' , 'Stdout:' , e .stdout , 'Stderr:' , e .stderr , runner . _run_id )
1514
1511
except KeyError as e :
1515
- error_helpers .log_error ('Was expecting a value inside the usage_scenario.yml file, but value was missing: ' , e , successful_run_id )
1512
+ error_helpers .log_error ('Was expecting a value inside the usage_scenario.yml file, but value was missing: ' , e , runner . _run_id )
1516
1513
except RuntimeError as e :
1517
- error_helpers .log_error ('RuntimeError occured in runner.py: ' , e , successful_run_id )
1514
+ error_helpers .log_error ('RuntimeError occured in runner.py: ' , e , runner . _run_id )
1518
1515
except BaseException as e :
1519
- error_helpers .log_error ('Base exception occured in runner.py: ' , e , successful_run_id )
1516
+ error_helpers .log_error ('Base exception occured in runner.py: ' , e , runner . _run_id )
1520
1517
finally :
1521
1518
if args .print_logs :
1522
1519
for container_id_outer , std_out in runner .get_logs ().items ():
0 commit comments