def stop(self, **kwargs): self.task_controller = TaskController() input_dict = {} mutually_exclusive(["id", "all"], kwargs, input_dict) if "id" in input_dict: self.cli_helper.echo(__("info", "cli.run.stop", input_dict['id'])) elif "all" in input_dict: self.cli_helper.echo(__("info", "cli.run.stop.all")) else: raise RequiredArgumentMissing() try: if "id" in input_dict: result = self.task_controller.stop(task_id=input_dict['id']) if not result: self.cli_helper.echo( __("error", "cli.run.stop", input_dict['id'])) else: self.cli_helper.echo( __("info", "cli.run.stop.success", input_dict['id'])) if "all" in input_dict: result = self.task_controller.stop(all=input_dict['all']) if not result: self.cli_helper.echo(__("error", "cli.run.stop.all")) else: self.cli_helper.echo(__("info", "cli.run.stop.all.success")) return result except Exception: if "id" in input_dict: self.cli_helper.echo( __("error", "cli.run.stop", input_dict['id'])) if "all" in input_dict: self.cli_helper.echo(__("error", "cli.run.stop.all")) return False
def rstudio(self, **kwargs): self.cli_helper.echo(__("info", "cli.workspace.rstudio")) # Creating input dictionaries snapshot_dict = {} # Environment if kwargs.get("environment_id", None) or kwargs.get( "environment_paths", None): mutually_exclusive_args = ["environment_id", "environment_paths"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) task_dict = { "ports": ["8787:8787"], "command_list": [ "/usr/lib/rstudio-server/bin/rserver", "--server-daemonize=0", "--server-app-armor-enabled=0" ], "mem_limit": kwargs["mem_limit"], "workspace": "rstudio" } self.cli_helper.echo(__("info", "cli.workspace.run.rstudio")) # Run task and return Task object result return self.task_run_helper(task_dict, snapshot_dict, "cli.workspace.rstudio")
def run(self, **kwargs): self.cli_helper.echo(__("info", "cli.run.run")) # Create input dictionaries snapshot_dict = {} # Environment if kwargs.get("environment_id", None) or kwargs.get( "environment_paths", None): mutually_exclusive_args = ["environment_id", "environment_paths"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) task_dict = { "ports": kwargs['ports'], "interactive": kwargs['interactive'], "mem_limit": kwargs['mem_limit'] } if not isinstance(kwargs['cmd'], list): if platform.system() == "Windows": task_dict['command'] = kwargs['cmd'] elif isinstance(kwargs['cmd'], basestring): task_dict['command_list'] = shlex.split(kwargs['cmd']) else: task_dict['command_list'] = kwargs['cmd'] data_paths = kwargs['data'] # Run task and return Task object result task_obj = self.task_run_helper(task_dict, snapshot_dict, "cli.run.run", data_paths=data_paths) if not task_obj: return False # Creating the run object run_obj = Run(task_obj) return run_obj
def create(self, **kwargs): self.cli_helper.echo(__("info", "cli.snapshot.create")) snapshot_dict = {} # Code mutually_exclusive_args = ["code_id", "commit_id"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) # Environment mutually_exclusive_args = ["environment_id", "environment_def_path"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) # File mutually_exclusive_args = ["file_collection_id", "filepaths"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) # Config mutually_exclusive_args = ["config_filepath", "config_filename"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) # Stats mutually_exclusive_args = ["stats_filepath", "stats_filename"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) optional_args = ["session_id", "task_id", "message", "label"] for arg in optional_args: if arg in kwargs and kwargs[arg] is not None: snapshot_dict[arg] = kwargs[arg] snapshot_obj = self.snapshot_controller.create(snapshot_dict) return snapshot_obj.id
def test_mutually_exclusive(self): mutually_exclusive_args = ["code_id", "commit_id"] arguments_dictionary = { 'code_id': 'test_code_id', 'environment_id': 'test_environment_id' } dictionary = {} mutually_exclusive(mutually_exclusive_args, arguments_dictionary, dictionary) assert dictionary assert dictionary['code_id'] == arguments_dictionary['code_id'] update_dictionary_failed = False try: mutually_exclusive_args = ["code_id", "commit_id"] arguments_dictionary = { 'code_id': 'test_code_id', 'commit_id': 'test_environment_id' } dictionary = {} mutually_exclusive(mutually_exclusive_args, arguments_dictionary, dictionary) except MutuallyExclusiveArguments: update_dictionary_failed = True assert update_dictionary_failed
def run(self, **kwargs): self.task_controller = TaskController() self.cli_helper.echo(__("info", "cli.task.run")) # Create input dictionaries snapshot_dict = {} # Environment if kwargs.get("environment_id", None) or kwargs.get( "environment_paths", None): mutually_exclusive_args = ["environment_id", "environment_paths"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) task_dict = { "ports": kwargs['ports'], "interactive": kwargs['interactive'], "mem_limit": kwargs['mem_limit'] } if not isinstance(kwargs['cmd'], list): if platform.system() == "Windows": task_dict['command'] = kwargs['cmd'] elif isinstance(kwargs['cmd'], basestring): task_dict['command_list'] = shlex.split(kwargs['cmd']) else: task_dict['command_list'] = kwargs['cmd'] # Create the task object task_obj = self.task_controller.create() updated_task_obj = task_obj try: # Pass in the task updated_task_obj = self.task_controller.run( task_obj.id, snapshot_dict=snapshot_dict, task_dict=task_dict) except Exception as e: self.logger.error("%s %s" % (e, task_dict)) self.cli_helper.echo("%s" % e) self.cli_helper.echo(__("error", "cli.task.run", task_obj.id)) return False finally: self.cli_helper.echo(__("info", "cli.task.run.stop")) self.task_controller.stop(updated_task_obj.id) self.cli_helper.echo( __("info", "cli.task.run.complete", updated_task_obj.id)) return updated_task_obj
def rstudio(self, **kwargs): self.task_controller = TaskController() self.cli_helper.echo(__("info", "cli.workspace.rstudio")) # Creating input dictionaries snapshot_dict = {} # Environment if kwargs.get("environment_id", None) or kwargs.get( "environment_paths", None): mutually_exclusive_args = ["environment_id", "environment_paths"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) task_dict = { "ports": ["8787:8787"], "command_list": [ "/usr/lib/rstudio-server/bin/rserver", "--server-daemonize=0", "--server-app-armor-enabled=0" ], "mem_limit": kwargs["mem_limit"] } # Create the task object task_obj = self.task_controller.create() updated_task_obj = task_obj # Pass in the task try: updated_task_obj = self.task_controller.run( task_obj.id, snapshot_dict=snapshot_dict, task_dict=task_dict) except Exception as e: self.logger.error("%s %s" % (e, task_dict)) self.cli_helper.echo( __("error", "cli.workspace.rstudio", task_obj.id)) return False finally: self.cli_helper.echo(__("info", "cli.task.run.stop")) self.task_controller.stop(updated_task_obj.id) self.cli_helper.echo( __("info", "cli.task.run.complete", updated_task_obj.id)) return updated_task_obj
def terminal(self, **kwargs): self.cli_helper.echo(__("info", "cli.workspace.terminal")) # Creating input dictionaries snapshot_dict = {} # Environment if kwargs.get("environment_id", None) or kwargs.get( "environment_paths", None): mutually_exclusive_args = ["environment_id", "environment_paths"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) task_dict = { "interactive": True, "mem_limit": kwargs["mem_limit"], "command_list": ["/bin/bash"] } # Run task and return Task object result return self.task_run_helper(task_dict, snapshot_dict, "cli.workspace.terminal")
def jupyterlab(self, **kwargs): self.cli_helper.echo(__("info", "cli.workspace.jupyterlab")) # Creating input dictionaries snapshot_dict = {} # Environment if kwargs.get("environment_id", None) or kwargs.get( "environment_paths", None): mutually_exclusive_args = ["environment_id", "environment_paths"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) task_dict = { "ports": ["8888:8888"], "command_list": ["jupyter", "lab", "--allow-root"], "mem_limit": kwargs["mem_limit"], "workspace": "jupyterlab" } # Run task and return Task object result return self.task_run_helper(task_dict, snapshot_dict, "cli.workspace.jupyterlab")
def notebook(self, **kwargs): self.task_controller = TaskController() self.cli_helper.echo(__("info", "cli.workspace.notebook")) # Creating input dictionaries snapshot_dict = {} # Environment if kwargs.get("environment_id", None) or kwargs.get( "environment_paths", None): mutually_exclusive_args = ["environment_id", "environment_paths"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) task_dict = { "ports": ["8888:8888"], "command_list": ["jupyter", "notebook"], "mem_limit": kwargs["mem_limit"] } # Create the task object task_obj = self.task_controller.create() updated_task_obj = task_obj # Pass in the task try: updated_task_obj = self.task_controller.run( task_obj.id, snapshot_dict=snapshot_dict, task_dict=task_dict) except Exception as e: self.logger.error("%s %s" % (e, task_dict)) self.cli_helper.echo( __("error", "cli.workspace.notebook", task_obj.id)) return False finally: self.cli_helper.echo(__("info", "cli.task.run.stop")) self.task_controller.stop(updated_task_obj.id) self.cli_helper.echo( __("info", "cli.task.run.complete", updated_task_obj.id)) return updated_task_obj
def test_mutually_exclusive(self): mutually_exclusive_args = ["code_id", "commit_id"] arguments_dictionary = { "code_id": "test_code_id", "environment_id": "test_environment_id" } dictionary = {} mutually_exclusive(mutually_exclusive_args, arguments_dictionary, dictionary) assert dictionary assert dictionary['code_id'] == arguments_dictionary['code_id'] failed = False try: mutually_exclusive_args = ["code_id", "commit_id"] arguments_dictionary = {"code_id": None, "environment_id": None} dictionary = {} mutually_exclusive(mutually_exclusive_args, arguments_dictionary, dictionary) except RequiredArgumentMissing: failed = True assert failed failed = False try: mutually_exclusive_args = ["code_id", "commit_id"] arguments_dictionary = {"code_id": None, "commit_id": None} dictionary = {} mutually_exclusive(mutually_exclusive_args, arguments_dictionary, dictionary) except RequiredArgumentMissing: failed = True assert failed update_dictionary_failed = False try: mutually_exclusive_args = ["code_id", "commit_id"] arguments_dictionary = { 'code_id': "test_code_id", 'commit_id': "test_environment_id" } dictionary = {} mutually_exclusive(mutually_exclusive_args, arguments_dictionary, dictionary) except MutuallyExclusiveArguments: update_dictionary_failed = True assert update_dictionary_failed
def create(self, **kwargs): self.snapshot_controller = SnapshotController() self.cli_helper.echo(__("info", "cli.snapshot.create")) run_id = kwargs.get("run_id", None) # creating snapshot with task id if it exists if run_id is not None: excluded_args = [ "environment_id", "environment_paths", "paths", "config_filepath", "config_filename", "stats_filepath", "stats_filename" ] for arg in excluded_args: if arg in kwargs and kwargs[arg] is not None: raise SnapshotCreateFromTaskArgs( "error", "cli.snapshot.create.run.args", arg) message = kwargs.get("message", None) label = kwargs.get("label", None) # Create a new core snapshot object snapshot_task_obj = self.snapshot_controller.create_from_task( message, run_id, label=label) self.cli_helper.echo( "Created snapshot id: %s" % snapshot_task_obj.id) return snapshot_task_obj else: # creating snapshot without task id snapshot_dict = {"visible": True} # Environment if kwargs.get("environment_id", None) or kwargs.get( "environment_paths", None): mutually_exclusive_args = [ "environment_id", "environment_paths" ] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) # File if kwargs.get("paths", None): snapshot_dict['paths'] = kwargs['paths'] # Config if kwargs.get("config_filepath", None) or kwargs.get( "config_filename", None) or kwargs.get("config", None): mutually_exclusive_args = [ "config_filepath", "config_filename", "config" ] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) # parsing config if "config" in snapshot_dict: config = {} config_list = snapshot_dict["config"] for item in config_list: item_parsed_dict = parse_cli_key_value(item, 'config') config.update(item_parsed_dict) snapshot_dict["config"] = config # Stats if kwargs.get("stats_filepath", None) or kwargs.get( "stats_filename", None) or kwargs.get("config", None): mutually_exclusive_args = [ "stats_filepath", "stats_filename", "stats" ] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) # parsing stats if "stats" in snapshot_dict: stats = {} stats_list = snapshot_dict["stats"] for item in stats_list: item_parsed_dict = parse_cli_key_value(item, 'stats') stats.update(item_parsed_dict) snapshot_dict["stats"] = stats optional_args = ["message", "label"] for arg in optional_args: if arg in kwargs and kwargs[arg] is not None: snapshot_dict[arg] = kwargs[arg] snapshot_obj = self.snapshot_controller.create(snapshot_dict) # Because snapshots may be invisible to the user, this function ensures that by the end # the user can monitor the snapshot on the CLI, but making it visible snapshot_obj = self.snapshot_controller.update( snapshot_obj.id, visible=True) self.cli_helper.echo( __("info", "cli.snapshot.create.success", snapshot_obj.id)) return snapshot_obj
def create(self, **kwargs): self.cli_helper.echo(__("info", "cli.snapshot.create")) task_id = kwargs.get("task_id", None) # creating snapshot with task id if it exists if task_id is not None: excluded_args = [ "code_id", "commit_id", "environment_id", "environment_definition_filepath", "file_collection_id", "filepaths", "config_filepath", "config_filename", "stats_filepath", "stats_filename" ] for arg in excluded_args: if arg in kwargs and kwargs[arg] is not None: raise SnapshotCreateFromTaskArgs( "error", "cli.snapshot.create.task.args", arg) message = kwargs.get("message", None) label = kwargs.get("label", None) # Create a new core snapshot object snapshot_task_obj = self.snapshot_controller.create_from_task( message, task_id, label=label) self.cli_helper.echo("Created snapshot id: %s" % snapshot_task_obj.id) return snapshot_task_obj.id else: # creating snapshot without task id snapshot_dict = {"visible": True} # Code if kwargs.get("code_id", None) or kwargs.get("commit_id", None): mutually_exclusive_args = ["code_id", "commit_id"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) # Environment if kwargs.get("environment_id", None) or kwargs.get( "environment_definition_filepath", None): mutually_exclusive_args = [ "environment_id", "environment_definition_filepath" ] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) # File if kwargs.get("file_collection_id", None) or kwargs.get( "filepaths", None): mutually_exclusive_args = ["file_collection_id", "filepaths"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) # Config if kwargs.get("config_filepath", None) or kwargs.get( "config_filename", None): mutually_exclusive_args = [ "config_filepath", "config_filename" ] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) # Stats if kwargs.get("stats_filepath", None) or kwargs.get( "stats_filename", None): mutually_exclusive_args = ["stats_filepath", "stats_filename"] mutually_exclusive(mutually_exclusive_args, kwargs, snapshot_dict) optional_args = ["session_id", "message", "label"] for arg in optional_args: if arg in kwargs and kwargs[arg] is not None: snapshot_dict[arg] = kwargs[arg] snapshot_obj = self.snapshot_controller.create(snapshot_dict) self.cli_helper.echo( __("info", "cli.snapshot.create.success", snapshot_obj.id)) return snapshot_obj.id