def node_command(api, command, exp_id, nodes_list=(), cmd_opt=None): """ Launch commands (start, stop, reset, update) on resources (JSONArray) user experiment :param api: API Rest api object :param command: command that should be run :param exp_id: Target experiment id :param nodes_list: List of nodes where to run command. Empty list runs on all nodes :param cmd_opt: Firmware path for update, profile name for profile """ assert command in ('update', 'profile', 'start', 'stop', 'reset', 'debug-start', 'debug-stop') result = None if command == 'update': assert cmd_opt is not None, '`cmd_opt` required for update' files = helpers.FilesDict() files.add_firmware(cmd_opt) files[NODE_FILENAME] = json.dumps(nodes_list) result = api.node_update(exp_id, files) elif command == 'profile': cmd_opt = '&name={0}'.format(cmd_opt) result = api.node_command(command, exp_id, nodes_list, cmd_opt) else: result = api.node_command(command, exp_id, nodes_list) return result
def _script_run_files_dict(*site_associations): """Return script start files dict. Returns dict with format { <RUN_FILENAME>: json({'script': [<scripts_associations>], ...}), 'scriptname': b'scriptcontent', } """ if not site_associations: raise ValueError('Got empty site_associations %r', site_associations) _check_sites_uniq(*site_associations) files_dict = helpers.FilesDict() # Save association and files associations = {} for sites, assocs in site_associations: for assoctype, assocname in assocs.items(): _add_siteassoc_to_dict(associations, sites, assoctype, assocname) files_dict.add_files_from_dict(SITE_ASSOCIATIONS_FILE_ASSOCS, assocs) # Add scrit sites association to files_dict files_dict[RUN_FILENAME] = helpers.json_dumps(associations) return files_dict
def test_add_file_method(self, read_file): """Test FilesDict add_file methods.""" def _read_file(name, *_): """Read file mock.""" files = { '1.elf': b'ELF32_1', '2.elf': b'ELF32_2', 'prof.json': b'{}', } return files[name] read_file.side_effect = _read_file file_dict = helpers.FilesDict() file_dict.add_file('1.elf') self.assertEqual(file_dict['1.elf'], b'ELF32_1') # Add some files input_files_dict = {'firmware': '1.elf', 'profile': 'prof.json'} file_dict.add_files_from_dict(['firmware', 'profile', 'script'], input_files_dict) self.assertEqual(file_dict, {'1.elf': b'ELF32_1', 'prof.json': b'{}'}) # Add some other files input_files_dict = {'firmware': '2.elf', 'scriptconfig': 'otherfile'} file_dict.add_files_from_dict(['firmware', 'profile', 'script'], input_files_dict) self.assertEqual(file_dict, {'1.elf': b'ELF32_1', '2.elf': b'ELF32_2', 'prof.json': b'{}', })
def submit_experiment( api, name, duration, # pylint:disable=too-many-arguments resources, start_time=None, print_json=False): """ Submit user experiment with JSON Encoder serialization object Experiment and firmware(s). If submission is accepted by scheduler OAR we print JSONObject response with id submission. :param api: API Rest api object :param name: experiment name :param duration: experiment duration in seconds :param resources: list of 'exp_resources' which :param print_json: select if experiment should be printed as json instead of submitted """ assert resources, 'Empty resources: %r' % resources experiment = _Experiment(name, duration, start_time) exp_files = helpers.FilesDict() for res_dict in resources: experiment.add_exp_resources(res_dict) exp_files.add_firmware(res_dict.get('firmware', None)) # firmware if print_json: # output experiment description return experiment # submit experiment exp_files[EXP_FILENAME] = helpers.json_dumps(experiment) # exp description return api.submit_experiment(exp_files)
def flash(run,site,node_type,image,exp_id,nodes): if node_type == 'm3': user, passwd = auth.get_user_credentials() api = rest.Api(user, passwd) # Flash nodes files = helpers.FilesDict() files.add_firmware(image) files[NODE_FILENAME] = json.dumps(nodes) result = api.node_update(exp_id.value,files) print(result) print("Nodes "+",".join(nodes)+" flashed ") return True elif node_type == 'a8': p = Pool(len(nodes)) results = p.map(flash_single_a8,[(run,site,image,exp_id.value,node) for node in nodes]) allok = all(r == 0 for r in results) if allok: print("All nodes flash successfully") else: print("Flashing failed for ", end=' ') print([nodes[x] for x in [i for i in range(0,len(results)) if results[i] != 0]]) return allok else: assert(false);
def _node_command_profile_load(api, exp_id, nodes_list, cmd_opt): assert cmd_opt is not None, '`cmd_opt` required for update' files = helpers.FilesDict() files.add_file(cmd_opt) files[NODE_FILENAME] = json.dumps(nodes_list) return api.node_profile_load(exp_id, files)
def node_command(api, command, exp_id, nodes_list=(), firmware_path=None): """ Launch commands (start, stop, reset, update) on resources (JSONArray) user experiment :param api: API Rest api object :param command: command that should be run :param exp_id: Target experiment id :param nodes_list: List of nodes where to run command. Empty list runs on all nodes :param firmware_path: Firmware path for update command """ assert command in ('update', 'start', 'stop', 'reset') result = None if 'update' == command: assert firmware_path is not None, '`firmware_path` required for update' files = helpers.FilesDict() files.add_firmware(firmware_path) files[NODE_FILENAME] = json.dumps(nodes_list) result = api.node_update(exp_id, files) else: result = api.node_command(command, exp_id, nodes_list) return result
def _node_command_flash(api, exp_id, nodes_list, cmd_opt): assert cmd_opt is not None, '`cmd_opt` required for update' files = helpers.FilesDict() files.add_file(cmd_opt) if cmd_opt.endswith('.bin'): files[EXPERIMENT] = json.dumps({'nodes': nodes_list, 'offset': 0}) return api.node_update(exp_id, files, binary=True) files[NODE_FILENAME] = json.dumps(nodes_list) return api.node_update(exp_id, files)
def test_no_overwrite(self): """Test FilesDict values overriding.""" file_dict = helpers.FilesDict() file_dict['a'] = 1 # Can re-add same value file_dict['a'] = 1 # Cannot add a different valu file_dict['b'] = 2 with self.assertRaises(ValueError): file_dict['b'] = 3 # Check dict self.assertEqual(file_dict, {'a': 1, 'b': 2})
def test_no_overwrite(self): """Test FilesDict values overriding.""" file_dict = helpers.FilesDict() file_dict['a'] = 1 # Can re-add same value file_dict['a'] = 1 # Cannot add a different valu file_dict['b'] = 2 try: file_dict['b'] = 3 except ValueError: pass # different value else: self.fail('No ValueError on different values') # Check dict self.assertEqual(file_dict, {'a': 1, 'b': 2})
def submit_experiment( api, name, duration, # pylint:disable=too-many-arguments resources, start_time=None, print_json=False, sites_assocs=None): """ Submit user experiment with JSON Encoder serialization object Experiment and firmware(s). If submission is accepted by scheduler OAR we print JSONObject response with id submission. :param api: API Rest api object :param name: experiment name :param duration: experiment duration in minutes :param resources: list of 'exp_resources' :param print_json: select if experiment should be printed as json instead of submitted :param sites_assocs: list of 'site_association' """ assert resources, 'Empty resources: %r' % resources experiment = _Experiment(name, duration, start_time) exp_files = helpers.FilesDict() for res_dict in resources: experiment.add_exp_resources(res_dict) exp_files.add_files_from_dict(NODES_ASSOCIATIONS_FILE_ASSOCS, res_dict) sites_assocs = sites_assocs or () for site_assoc in sites_assocs: experiment.add_site_association(site_assoc) assocs = site_assoc.associations exp_files.add_files_from_dict(SITE_ASSOCIATIONS_FILE_ASSOCS, assocs) if print_json: # output experiment description return experiment # submit experiment exp_files[EXP_FILENAME] = helpers.json_dumps(experiment) # exp description return api.submit_experiment(exp_files)
def load_experiment(api, exp_desc_path, firmware_list=()): """ Load and submit user experiment description with firmware(s) Firmwares required for experiment will be loaded from current directory, except if their path is given in firmware_list :param api: API Rest api object :param exp_desc_path: path to experiment json description file :param firmware_list: list of firmware path """ # 1. load experiment description exp_dict = json.loads(helpers.read_file(exp_desc_path)) exp_files = helpers.FilesDict() # 2. Add experiment description exp_files[EXP_FILENAME] = helpers.json_dumps(exp_dict) # 3. Add firmwares files to the experiment files using # firmware_list and experiment firmwareassociations # extract firmwares names _fw_association = exp_dict['firmwareassociations'] or [] firmwares = set([assoc['firmwarename'] for assoc in _fw_association]) try: # replace firwmare name by firmware_path from firmware_list for _fw_path in firmware_list: firmwares.remove(basename(_fw_path)) firmwares.add(_fw_path) except KeyError as err: raise ValueError("Firmware {!s} is not in experiment: {}".format( err, exp_desc_path)) else: # Add all firmwares to the experiment files for _fw_path in firmwares: exp_files.add_firmware(_fw_path) return api.submit_experiment(exp_files)
def load_experiment(api, exp_desc_path, files_list=()): """ Load and submit user experiment description with firmware(s) Firmwares and scripts required for experiment will be loaded from current directory, except if their path is given in files_list :param api: API Rest api object :param exp_desc_path: path to experiment json description file :param files_list: list of files path """ # 1. load experiment description exp_dict = json.loads(helpers.read_file(exp_desc_path)) experiment = _Experiment.from_dict(exp_dict) # 2. List files and update path with provided path files = _files_with_filespath(experiment.filenames(), files_list) # Construct experiment files exp_files = helpers.FilesDict() exp_files[EXP_FILENAME] = helpers.json_dumps(experiment) for exp_file in files: exp_files.add_file(exp_file) return api.submit_experiment(exp_files)
def file_dict(): """fixture to provide an empty FilesDict""" return helpers.FilesDict()