def finish(exp_id): print("Finish experiments") user, passwd = auth.get_user_credentials() api = rest.Api(user, passwd) result = iotlabcli.experiment.stop_experiment(api, exp_id.value) print(result)
def get_info_cached(): now = datetime.datetime.now() data = {} try: with open(NODE_CACHE_FILENAME) as f: data = json.load(f) cache_time = datetime.datetime.strptime(data["time"], TIME_FORMAT) if now - cache_time > datetime.timedelta(hours=REFRESH_PERIOD_HOURS): raise Exception("Too old") except: print("Reload node data") try: user, passwd = auth.get_user_credentials() api = rest.Api(user, passwd) newdata = iotlabcli.experiment.info_experiment(api) newdata["time"] = now.strftime(TIME_FORMAT) with open(NODE_CACHE_FILENAME, 'w') as outfile: json.dump(newdata, outfile, sort_keys=True, indent=4, separators=(',', ': ')) data = newdata except: pass # use cached data anyway print("Node data loaded") return data
def submit_experiment(resources, max_duration, exp_id_result, start_time=None, power_average=None): print(resources) print("Started") user, passwd = auth.get_user_credentials() api = rest.Api(user, passwd) if power_average: m3_prof = profile.ProfileM3(PROFILE_NAME, 'dc') m3_prof.set_consumption(140, power_average, True, True, True) m3_prof.set_radio('sniffer', [20]) api.add_profile(PROFILE_NAME, m3_prof) result = iotlabcli.experiment.submit_experiment(api, EXPERIMENT_NAME, max_duration, [resources], start_time=start_time) print(result) exp_id_result.value = result['id'] print(exp_id_result.value) print("Waiting") result = iotlabcli.experiment.wait_experiment(api, exp_id_result.value) print(result)
def robot_parse_and_run(opts): # noqa # Too complex but straightforward """ Parse namespace 'opts' object and execute requested command """ user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) command = opts.command if command == 'status': exp_id = helpers.get_current_experiment(api, opts.experiment_id) nodes = common.list_nodes(api, exp_id, opts.nodes_list, opts.exclude_nodes_list) ret = iotlabcli.robot.robot_command(api, 'status', exp_id, nodes) elif command == 'update': exp_id = helpers.get_current_experiment(api, opts.experiment_id) nodes = common.list_nodes(api, exp_id, opts.nodes_list, opts.exclude_nodes_list) name, site = opts.update_name_site ret = iotlabcli.robot.robot_update_mobility(api, exp_id, name, site, nodes) elif command == 'get' and opts.get_list: ret = iotlabcli.robot.mobility_command(api, 'list') elif command == 'get' and opts.get_name_site is not None: ret = iotlabcli.robot.mobility_command(api, 'get', opts.get_name_site) else: # pragma: no cover raise ValueError('Unknown command') return ret
def get_experiment_parser(opts): """ Parse namespace 'opts' object and execute requested 'get' command """ user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) # pylint:disable=no-else-return if opts.get_cmd == 'experiment_list': return experiment.get_experiments_list(api, opts.state, opts.limit, opts.offset) elif opts.get_cmd == 'start': exp_id = helpers.get_current_experiment(api, opts.experiment_id, running_only=False) ret = experiment.get_experiment(api, exp_id, opts.get_cmd) # Add a 'date' field timestamp = ret['start_time'] ret['local_date'] = time.ctime(timestamp) if timestamp else 'Unknown' return ret elif opts.get_cmd == 'experiments': return experiment.get_active_experiments(api, running_only=not opts.active) else: exp_id = helpers.get_current_experiment(api, opts.experiment_id) return experiment.get_experiment(api, exp_id, opts.get_cmd)
def stop_experiment_parser(opts): """ Parse namespace 'opts' object and execute requested 'stop' command """ user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) exp_id = helpers.get_current_experiment(api, opts.experiment_id) return experiment.stop_experiment(api, exp_id)
def info_experiment_parser(opts): """ Parse namespace 'opts' object and execute requested 'info' command """ user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) selection = dict(opts.info_selection or ()) return experiment.info_experiment(api, opts.list_id, **selection)
def __init__(self, nodename, profile, timeexpe): user, passwd = auth.get_user_credentials() self.api = rest.Api(user, passwd) self.idexpe = 0 self.nodename = nodename self.profile = profile self.time = timeexpe
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_parse_and_run(opts): """ Parse namespace 'opts' object and execute requested command """ user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) exp_id = helpers.get_current_experiment(api, opts.experiment_id) command = opts.command if opts.command != 'with_argument': # opts.command has a real value command = opts.command cmd_opt = None elif opts.firmware_path is not None: # opts.command has default value command = 'update' cmd_opt = opts.firmware_path elif opts.profile_name is not None: # opts.command has default value command = 'profile' cmd_opt = opts.profile_name else: # pragma: no cover assert False, "Unknown command %r" % opts.command nodes = common.list_nodes(api, exp_id, opts.nodes_list, opts.exclude_nodes_list) return iotlabcli.node.node_command(api, command, exp_id, nodes, cmd_opt)
def load_experiment_parser(opts): """ Parse namespace 'opts' object and execute requested 'load' command """ user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) files = helpers.flatten_list_list(opts.files) return experiment.load_experiment(api, opts.path_file, files)
def submit_experiment_parser(opts): """ Parse namespace 'opts' and execute requested 'submit' command """ user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) return experiment.submit_experiment(api, opts.name, opts.duration, opts.nodes_list, opts.reservation, opts.print_json, opts.site_association)
def test_get_circuits_selection(self, _method): """Test get_circuits selection.""" _method.return_value = {'circuit': 'test'} api = rest.Api(None, None) api.get_circuits(site='grenoble', type='predefined') _method.assert_called_with('mobilities/circuits' '?site=grenoble&type=predefined')
def open_a8_parse_and_run(opts): """Parse namespace 'opts' object and execute M3 fw update action.""" user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) exp_id = helpers.get_current_experiment(api, opts.experiment_id) config_ssh = {'user': user, 'exp_id': exp_id} nodes = common.list_nodes(api, exp_id, opts.nodes_list, opts.exclude_nodes_list) # Only if nodes_list or exclude_nodes_list is not specify (nodes = []) if not nodes: nodes = _get_experiment_nodes_list(api, exp_id) # Only keep A8 nodes nodes = [ "node-{0}".format(node) for node in nodes if node.startswith('a8') ] command = opts.command res = None if command == 'reset-m3': res = iotlabsshcli.open_a8.reset_m3(config_ssh, nodes, verbose=opts.verbose) elif command == 'flash-m3': res = iotlabsshcli.open_a8.flash_m3(config_ssh, nodes, opts.firmware, verbose=opts.verbose) elif command == 'wait-for-boot': res = iotlabsshcli.open_a8.wait_for_boot(config_ssh, nodes, max_wait=opts.max_wait, verbose=opts.verbose) elif command == 'run-script': res = iotlabsshcli.open_a8.run_script(config_ssh, nodes, opts.script, opts.frontend, verbose=opts.verbose) elif command == 'run-cmd': res = iotlabsshcli.open_a8.run_cmd(config_ssh, nodes, opts.cmd, opts.frontend, verbose=opts.verbose) elif command == 'copy-file': res = iotlabsshcli.open_a8.copy_file(config_ssh, nodes, opts.file_path, verbose=opts.verbose) if res is None: raise ValueError('Unknown command {0}'.format(command)) return res
def script_parser(opts): """Parse namespace 'opts' and execute requestes 'run' command.""" user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) exp_id = helpers.get_current_experiment(api, opts.experiment_id) command, options = _script_command_options(opts) return experiment.script_experiment(api, exp_id, command, *options)
def blocking_experiments(site,node_type,nodes): user, passwd = auth.get_user_credentials() api = rest.Api(user, passwd) experiments = api.get_experiments('Waiting,Running')['items'] #experiments = api.get_experiments('Error')['items'] experiments = [x for x in experiments if any(("%s-%i.%s.iot-lab.info"%(node_type,node,site) in x['resources']) for node in nodes)] return experiments
def __init__(self, expServer=None): self.expServer = expServer name, password = getCredentialsOrFail(self) if expServer == None: serverUrl = getServerRestUrl() else: serverUrl = "https://{0}/rest/" .format(expServer) self.request = rest.Api(url = serverUrl, username=name, password=password, parser=self) self.userName = name
def test_get_experiment(self, w_exp_archive): """ Test experiment.get_experiment """ arch_content = '\x42\x69' ret_val = RequestRet(content=arch_content, status_code=200) patch('requests.get', return_value=ret_val).start() api = rest.Api('user', 'password') ret = experiment.get_experiment(api, 123, option='data') self.assertEquals(ret, 'Written') w_exp_archive.assert_called_with(123, arch_content)
def setUpClass(cls): cls.request = rest.Api() cls.exp_id = iotlabcli.helpers.check_experiments_running( json.loads(cls.request.get_experiments(_RUNNING_EXP)), parser=None) ressources = cls.request.get_experiment_resources(cls.exp_id) json_dict = serial_aggregator.extract_json(ressources) nodes_list = serial_aggregator.extract_nodes(json_dict, HOSTNAME) cls.nodes_aggregator = serial_aggregator.NodeAggregator(nodes_list)
def test_get_nodes_selection(self, _method): """Test get_nodes selection.""" _method.return_value = {'state': 'Running'} api = rest.Api(None, None) api.get_nodes(False, 'grenoble', archi='m3', state='Alive') _method.assert_called_with('nodes' '?archi=m3&site=grenoble&state=Alive') api.get_nodes(True, archi='a8', state='Busy', site='lille') _method.assert_called_with('nodes/ids' '?archi=a8&site=lille&state=Busy')
def get_experiment_parser(opts): """ Parse namespace 'opts' object and execute requested 'get' command """ user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) if opts.get_cmd == 'experiment_list': return experiment.get_experiments_list(api, opts.state, opts.limit, opts.offset) else: exp_id = helpers.get_current_experiment(api, opts.experiment_id) return experiment.get_experiment(api, exp_id, opts.get_cmd)
def main(args=sys.argv[1:]): """ Main command-line execution loop." """ try: parser = parse_options() parser_options = parser.parse_args(args) request = rest.Api(username=parser_options.username, password=parser_options.password, parser=parser) command_node(parser_options, request, parser) except KeyboardInterrupt: print >> sys.stderr, "\nStopped." sys.exit()
def flash_single_a8(args): (run,site,image,exp_id_value,node) = args user, passwd = auth.get_user_credentials() api = rest.Api(user, passwd) logfile = os.path.join(run['logdir'],run['name']+"_flash_"+str(node)+".log") ssh_options = " -o ConnectTimeout=4 -o StrictHostKeyChecking=no -o LogLevel=ERROR -o UserKnownHostsFile=/dev/null " redir_cmd = "ssh -t "+user+"@"+site+".iot-lab.info ssh root@node-"+str(node)+ssh_options+"'/etc/init.d/serial_redirection start'" cpy_cmd = "scp -oProxyCommand=\"ssh -W %h:%p "+user+"@"+site+".iot-lab.info\""+ssh_options+image+" root@node-"+str(node)+":~/firmware.elf" flash_cmd = "ssh -t "+user+"@"+site+".iot-lab.info ssh root@node-"+str(node)+ssh_options+"'PATH=/usr/local/bin:/usr/bin flash_a8_m3 /home/root/firmware.elf'" result = 0 with open(logfile,"w") as log: # Try to establish ssh connection and start serial redirection total_timeout = 60*60 interval = 10 last_reset = 0 reset_interval = 60 for i in range(0,int(total_timeout/interval)+1): for cmd in [redir_cmd,cpy_cmd,flash_cmd]: log.write("> "+cmd+"\n") log.flush() result = subprocess.call(cmd, stdout=log, stderr=log, shell=True) log.flush() if result != 0: break if result == 0 or i >= total_timeout/interval-1: break else: if i*interval > last_reset+reset_interval: log.write(">>> STOP\n") result = api.node_command('stop',exp_id_value,[node]) log.write("Result %s\n"%str(result)) time.sleep(6) log.write(">>> START\n") result = api.node_command('start',exp_id_value,[node]) log.write("Result %s\n"%str(result)) log.flush() last_reset = i*interval reset_interval *= 2 # exponential increase time.sleep(interval) if result == 0: os.remove(logfile) return result
def node_parse_and_run(opts): """ Parse namespace 'opts' object and execute requested command """ user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) exp_id = helpers.get_current_experiment(api, opts.experiment_id) if opts.command == 'with_argument': command, cmd_opt = _node_parse_command_and_opt(**vars(opts)) else: # opts.command has a real value command, cmd_opt = (opts.command, None) nodes = common.list_nodes(api, exp_id, opts.nodes_list, opts.exclude_nodes_list) return iotlabcli.node.node_command(api, command, exp_id, nodes, cmd_opt)
def wait_experiment_parser(opts): """ Parse namespace 'opts' object and execute requested 'wait' command """ user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) exp_id = helpers.get_current_experiment(api, opts.experiment_id, running_only=False) sys.stderr.write("Waiting that experiment {} gets in state {}\n".format( exp_id, opts.state)) return experiment.wait_experiment(api, exp_id, opts.state, opts.step, opts.timeout)
def profile_parse_and_run(opts): """ Parse namespace 'opts' object and execute requested command """ user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) fct_parser = { 'addwsn430': add_profile_parser, 'addm3': add_profile_parser, 'adda8': add_profile_parser, 'load': load_profile_parser, 'get': get_profile_parser, 'del': del_profile_parser, }[opts.subparser_name] return fct_parser(api, opts)
def stop_all(site, node_type, nodes): user, passwd = auth.get_user_credentials() api = rest.Api(user, passwd) blocking = blocking_experiments(site, node_type, nodes) for exp in blocking: eid = exp['id'] print("Stopping " + str(eid)) result = api.stop_experiment(eid) print(result) print("Waiting for stopping " + str(eid)) result = iotlabcli.experiment.wait_experiment( api, eid, states='Terminated,Error') print(result)
def get_experiment_parser(opts): """ Parse namespace 'opts' object and execute requested 'get' command """ user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) # pylint:disable=no-else-return if opts.get_cmd == 'experiment_list': return experiment.get_experiments_list(api, opts.state, opts.limit, opts.offset) elif opts.get_cmd in ('start_date', 'state'): return _get_experiment_attr(api, opts) elif opts.get_cmd == 'experiments': return experiment.get_active_experiments(api, running_only=not opts.active) else: exp_id = helpers.get_current_experiment(api, opts.experiment_id) return experiment.get_experiment(api, exp_id, _deprecate_cmd(opts.get_cmd))
def states(site,node_type,nodes): user, passwd = auth.get_user_credentials() api = rest.Api(user, passwd) resources = api.get_resources(True,site) resources = resources['items'][0][site][node_type] print(resources) for k in resources: resources[k] = nodes_from_string(resources[k]) result = {} for k in resources: for n in resources[k]: if n in nodes: result[n] = k return result
def parse_and_run(opts): """Parse namespace 'opts' object and execute M3 fw update action.""" user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) exp_id = helpers.get_current_experiment(api, opts.experiment_id) # Fetch token from new API host = urlparse(api.url).netloc api_url = 'https://{}/api/experiments/{}/token'.format(host, exp_id) request_kwargs = {'auth_username': user, 'auth_password': passwd} request = tornado.httpclient.HTTPRequest(api_url, **request_kwargs) request.headers["Content-Type"] = "application/json" client = tornado.httpclient.HTTPClient() try: token_response = client.fetch(request).buffer.read() except tornado.httpclient.HTTPClientError as exc: # pylint:disable=superfluous-parens print("Failed to fetch token from API: {}".format(exc)) return 1 token = json.loads(token_response.decode())['token'] nodes = common.list_nodes(api, exp_id, opts.nodes_list, opts.exclude_nodes_list) # Only if nodes_list or exclude_nodes_list is not specify (nodes = []) if not nodes: nodes = _get_experiment_nodes_list(api, exp_id) # Drop A8 nodes nodes = ["{}".format(node) for node in nodes if not node.startswith('a8')] if not nodes: return 1 if _check_nodes_list(nodes) > 0: return 1 return start(Session(host, exp_id, user, token), nodes)