pceicicd | 34a7a72 | 2023-10-03 21:01:37 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | import logging |
| 3 | import mmap |
| 4 | import multiprocessing as mp |
| 5 | import powder.experiment as pexp |
| 6 | import random |
| 7 | import re |
| 8 | import string |
| 9 | import sys |
| 10 | import time |
| 11 | import os |
| 12 | |
| 13 | logging.basicConfig( |
| 14 | level=logging.DEBUG, |
| 15 | format="[%(asctime)s] %(name)s:%(levelname)s: %(message)s" |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | class 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 | |
| 81 | if __name__ == '__main__': |
| 82 | xtesting_host = PowderProfile() |
| 83 | xtesting_host.run() |