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 test_list_nodes(self, g_nodes_list): """ Run the different list_nodes cases """ api = api_mock() g_nodes_list.return_value = [ "m3-1.grenoble.iot-lab.info", "m3-2.grenoble.iot-lab.info", "m3-3.grenoble.iot-lab.info", "m3-1.strasbourg.iot-lab.info", "m3-2.strasbourg.iot-lab.info", "m3-3.strasbourg.iot-lab.info" ] nodes_ll = [ ["m3-1.grenoble.iot-lab.info", "m3-2.grenoble.iot-lab.info"], ["m3-1.strasbourg.iot-lab.info", "m3-2.strasbourg.iot-lab.info"], ] # No nodes provided => all nodes, no external requests res = common.list_nodes(api, 123) self.assertEqual(res, []) self.assertFalse(g_nodes_list.called) # Normal case, no external requests, only list of all provided nodes res = common.list_nodes(api, 123, nodes_ll=nodes_ll) self.assertEqual(res, ["m3-1.grenoble.iot-lab.info", "m3-2.grenoble.iot-lab.info", "m3-1.strasbourg.iot-lab.info", "m3-2.strasbourg.iot-lab.info"]) self.assertFalse(g_nodes_list.called) res = common.list_nodes(api, 123, excl_nodes_ll=nodes_ll) self.assertEqual(res, ["m3-3.grenoble.iot-lab.info", "m3-3.strasbourg.iot-lab.info"]) self.assertTrue(g_nodes_list.called)
def test_list_nodes(self, g_nodes_list): """ Run the different list_nodes cases """ api = api_mock() g_nodes_list.return_value = [ "m3-1.grenoble.iot-lab.info", "m3-2.grenoble.iot-lab.info", "m3-3.grenoble.iot-lab.info", "m3-1.strasbourg.iot-lab.info", "m3-2.strasbourg.iot-lab.info", "m3-3.strasbourg.iot-lab.info" ] nodes_ll = [ ["m3-1.grenoble.iot-lab.info", "m3-2.grenoble.iot-lab.info"], ["m3-1.strasbourg.iot-lab.info", "m3-2.strasbourg.iot-lab.info"], ] # No nodes provided => all nodes, no external requests res = common.list_nodes(api, 123) self.assertEqual(res, []) self.assertFalse(g_nodes_list.called) # Normal case, no external requests, only list of all provided nodes res = common.list_nodes(api, 123, nodes_ll=nodes_ll) self.assertEqual(res, [ "m3-1.grenoble.iot-lab.info", "m3-2.grenoble.iot-lab.info", "m3-1.strasbourg.iot-lab.info", "m3-2.strasbourg.iot-lab.info" ]) self.assertFalse(g_nodes_list.called) res = common.list_nodes(api, 123, excl_nodes_ll=nodes_ll) self.assertEqual( res, ["m3-3.grenoble.iot-lab.info", "m3-3.strasbourg.iot-lab.info"]) self.assertTrue(g_nodes_list.called)
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 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 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 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