blob: 594dd445474ed08f2645595852fa7d522be1f0e8 [file] [log] [blame]
pceicicd34a7a722023-10-03 21:01:37 +00001#!/usr/bin/env python3
2import logging
3import mmap
4import multiprocessing as mp
5import powder.experiment as pexp
6import random
7import re
8import string
9import sys
10import time
11import os
12
13logging.basicConfig(
14 level=logging.DEBUG,
15 format="[%(asctime)s] %(name)s:%(levelname)s: %(message)s"
16)
17
18
19class PowderProfile:
20 """Instantiates a Powder experiment based on the provided Powder profile
21 by default the project is 'osc' and the profile is 'ubuntu-20'
22
23 """
24
25 # default Powder experiment credentials
26 PROJECT_NAME = 'osc'
27 PROFILE_NAME = 'ubuntu-20'
28 EXPERIMENT_NAME_PREFIX = 'osctest-'
29
30 SUCCEEDED = 0 # all steps succeeded
31 FAILED = 1 # on of the steps failed
32
33 def __init__(self, experiment_name=None):
34 if experiment_name is not None:
35 self.experiment_name = experiment_name
36 else:
37 self.experiment_name = self.EXPERIMENT_NAME_PREFIX + self._random_string()
38
39 try:
40 self.project_name = os.environ['PROJECT']
41 except KeyError:
42 self.project_name = self.PROJECT_NAME
43
44 try:
45 self.profile_name = os.environ['PROFILE']
46 except KeyError:
47 self.profile_name = self.PROFILE_NAME
48
49 def run(self):
50 if not self._start_powder_experiment():
51 self._finish(self.FAILED)
52 else:
53 self._finish(self.SUCCEEDED)
54
55 def _random_string(self, strlen=7):
56 characters = string.ascii_lowercase + string.digits
57 return ''.join(random.choice(characters) for i in range(strlen))
58
59 def _start_powder_experiment(self):
60 logging.info('Instantiating Powder experiment...')
61 self.exp = pexp.PowderExperiment(experiment_name=self.experiment_name,
62 project_name=self.project_name,
63 profile_name=self.profile_name)
64
65 exp_status = self.exp.start_and_wait()
66 if exp_status != self.exp.EXPERIMENT_READY:
67 logging.error('Failed to start experiment.')
68 return False
69 else:
70 return True
71
72 def _finish(self, test_status):
73 if test_status == self.FAILED:
74 logging.info('The experiment could not be started... maybe the resources were unavailable.')
75 elif test_status == self.SUCCEEDED:
76 logging.info('The experiment successfully started.')
77
78 sys.exit(test_status)
79
80
81if __name__ == '__main__':
82 xtesting_host = PowderProfile()
83 xtesting_host.run()