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 test_get_current_experiment(self): """ Test get_current_experiment """ api = None with patch('iotlabcli.helpers.exps_by_states_dict') as exps_m: exps_m.return_value = {'Running': [234]} self.assertEqual(123, helpers.get_current_experiment(api, 123)) self.assertEqual(234, helpers.get_current_experiment(api, None)) # also return 'active' experiments exps_m.return_value = {'Waiting': [234]} self.assertEqual(234, helpers.get_current_experiment( api, None, running_only=False))
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 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 _get_exp_id(iotlab_api, exp_id): """ Get experiment id """ try: return helpers.get_current_experiment(iotlab_api, exp_id) except ValueError, err: print(err) sys.exit(1)
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 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 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 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 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 _get_experiment_attr(api, opts): """ Return start_time or state experiment attribute with old api format""" assert opts.get_cmd in ( 'state', 'start_date', ) exp_id = helpers.get_current_experiment(api, opts.experiment_id, running_only=False) ret = experiment.get_experiment(api, exp_id, '') if opts.get_cmd == 'state': helpers.deprecate_warn_cmd('exp-state', 'print', 8) return {opts.get_cmd: ret[opts.get_cmd]} helpers.deprecate_warn_cmd('start-time', 'print', 8) # start_date option utc_date = datetime.strptime(ret[opts.get_cmd], '%Y-%m-%dT%H:%M:%SZ') timestamp = (utc_date - datetime(1970, 1, 1)).total_seconds() local_date = time.ctime(timestamp) if timestamp else 'Unknown' return {'start_time': int(timestamp), 'local_date': local_date}
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 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 open_linux_parse_and_run(opts): """Parse namespace 'opts' object.""" 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 Linux nodes nodes = [ f'node-{node}' for node in nodes if node.startswith('a8') or node.startswith('rpi3') ] command = opts.command res = None if command == 'reset': res = iotlabsshcli.open_linux.reset(config_ssh, nodes, verbose=opts.verbose) elif command == 'flash': res = iotlabsshcli.open_linux.flash(config_ssh, nodes, opts.firmware, verbose=opts.verbose) elif command == 'wait-for-boot': res = iotlabsshcli.open_linux.wait_for_boot(config_ssh, nodes, max_wait=opts.max_wait, verbose=opts.verbose) elif command == 'run-script': res = iotlabsshcli.open_linux.run_script(config_ssh, nodes, opts.script, opts.frontend, verbose=opts.verbose) elif command == 'run-cmd': res = iotlabsshcli.open_linux.run_cmd(config_ssh, nodes, opts.cmd, opts.frontend, verbose=opts.verbose) elif command == 'copy-file': res = iotlabsshcli.open_linux.copy_file(config_ssh, nodes, opts.file_path, verbose=opts.verbose) if command == 'reset-m3': deprecate_warn_cmd('reset-m3', 'reset', 7) res = iotlabsshcli.open_linux.reset(config_ssh, nodes, verbose=opts.verbose) elif command == 'flash-m3': deprecate_warn_cmd('flash-m3', 'flash', 7) res = iotlabsshcli.open_linux.flash(config_ssh, nodes, opts.firmware, verbose=opts.verbose) if res is None: raise ValueError(f'Unknown command {command}') return res