def test_get_user_credentials(self, m_read): """ Test auth.get_user_credentials """ m_read.return_value = ('super_user', 'super_passwd') # passwords given self.assertEqual(('user', 'passwd'), auth.get_user_credentials('user', 'passwd')) self.passwords = ['password_prompt'] self.assertEqual(('user', 'password_prompt'), auth.get_user_credentials('user')) self.assertEqual(('super_user', 'super_passwd'), auth.get_user_credentials())
def test_get_user_credentials(self, m_read): """ Test auth.get_user_credentials """ m_read.return_value = ('super_user', 'super_passwd') # passwords given self.assertEquals(('user', 'passwd'), auth.get_user_credentials('user', 'passwd')) self.passwords = ['password_prompt'] self.assertEquals(('user', 'password_prompt'), auth.get_user_credentials('user')) self.assertEquals(('super_user', 'super_passwd'), auth.get_user_credentials())
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 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 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 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 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 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 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 __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 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 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)
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 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 robot_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 nodes = common.list_nodes(api, exp_id, opts.nodes_list, opts.exclude_nodes_list) return iotlabcli.robot.robot_command(api, command, exp_id, nodes)
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 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 {0} gets in state {1}\n".format(exp_id, opts.state)) return experiment.wait_experiment(api, exp_id, opts.state, opts.step, opts.timeout)
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 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 firmware = opts.firmware_path # None if command != 'update' nodes = list_nodes(api, exp_id, opts.nodes_list, opts.exclude_nodes_list) return iotlabcli.node.node_command(api, command, exp_id, nodes, firmware)
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 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 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 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 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 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) 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 else: exp_id = helpers.get_current_experiment(api, opts.experiment_id) return experiment.get_experiment(api, exp_id, opts.get_cmd)
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 main(): """ Launch serial aggregator and aggregate serial links of all nodes. """ parser = opts_parser() opts = parser.parse_args() api = Api(*get_user_credentials()) opts.with_a8 = True try: nodes = SerialAggregator.select_nodes(opts) except RuntimeError as err: print(err) exit(1) if opts.experiment_id: exp_id = opts.experiment_id else: exp_id = iotlabcli.get_current_experiment(api) print("Running radio logger ...") run_radio_logger(exp_id, opts, nodes)
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)
def states(site,node_type,nodes): user, passwd = auth.get_user_credentials() api = rest.Api(user, passwd) resources = api.get_nodes(True,site) node_strings = {} for node_types in resources['items'][0]['archis']: if node_type in node_types['archi']: for states in node_types['states']: node_strings[states['state']] = states['ids'] resources = node_strings 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
#!/usr/bin/env python import random from math import sqrt, inf from iotlabcli import auth, rest, experiment user, passwd = auth.get_user_credentials() api = rest.Api(user, passwd) number_nodes = 64 nodes = experiment.info_experiment(api, site='lille', archi='m3', state='Alive')['items'] for node in list(nodes): try: node['x'] = float(node['x']) node['y'] = float(node['y']) node['z'] = float(node['z']) except ValueError: nodes.remove(node) node_idx = random.randint(0, len(nodes) - 1) selected_nodes = [nodes.pop(node_idx)] def dist(node1, node2): return sqrt(pow(node1['x'] - node2['x'],2)\ + pow(node1['y'] - node2['y'], 2)\ + pow(node1['z'] - node2['z'], 2))
def test_write_then_read(self): """ Test writing auth then reading it back """ # pylint: disable=protected-access auth.write_password_file('username', 'password') self.assertEqual(('username', 'password'), auth.get_user_credentials())
def reload_experiment_parser(opts): """Parse namespace 'opts' object and execute requested 'reload' command.""" user, passwd = auth.get_user_credentials(opts.username, opts.password) api = rest.Api(user, passwd) return experiment.reload_experiment(api, opts.experiment_id, opts.duration, opts.reservation)
def status_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) selection = dict(opts.nodes_selection or ()) return iotlabcli.status.status_command(api, opts.command, **selection)
def __init__(self): user, passwd = auth.get_user_credentials() self.api = rest.Api(user, passwd)