def dev_update(obj, directory): """Update plugin parts which have changed since previous update. Optionally pass in the DIRECTORY of the plugin (defaults to cwd). """ directory = Path(directory) plugin_toml_path = directory / "plugin.toml" if not plugin_toml_path.exists(): lib.log_error("Not in a plugin directory.") sys.exit(1) with get_modified_plugin_directories( directory, reset=obj["force"]) as modified_plugin_directories: if modified_plugin_directories: with lib.temp_directory() as temp_directory: shutil.copy(plugin_toml_path, temp_directory) for modified_directory in modified_plugin_directories: lib.log(f"Including: {modified_directory}") shutil.copytree( directory / modified_directory, temp_directory / modified_directory, ) api = lib.get_api(**obj) lib.run_plugins_task( api, "dev_update_plugin", dict(), "Uploading to server", data=lib.create_targz_as_bytes(temp_directory), ) else: lib.log("Nothing to do.")
def _install_plugin(api, filename): """Use the API to install plugin directly from a file.""" if not filename.exists(): lib.log_error(f"Cannot find plugin: {filename}", abort=True) blob_id = api.upload_file_as_blob(filename.as_posix()) lib.log(f"Uploaded {filename} to blob: {blob_id}") lib.run_plugins_task(api, "install_plugin", dict(blob_id=blob_id), "Installing")
def dev_create(obj, namespace, n_task_workers): """Create namespace of given name. Only useful during developmment.""" api = lib.get_api(**obj) lib.run_plugins_task( api, "dev_create_namespace", dict(namespace=namespace, n_task_workers=n_task_workers), "Creating namespace", )
def remove_task(obj, scheduled_task_id): """Remove scheduled task by id.""" api = lib.get_api(**obj) lib.run_plugins_task( api, "remove_scheduled_task", dict(scheduled_task_id=scheduled_task_id), "Removing scheduled tasks", )
def remove_tasks_in_namespace(obj, namespace): """Remove all scheduled tasks in given namespace.""" api = lib.get_api(**obj) lib.run_plugins_task( api, "remove_scheduled_tasks_in_namespace", dict(namespace=namespace), "Removing scheduled tasks", )
def uninstall(obj, namespace): """Uninstall named plugin.""" if not obj["force"]: click.confirm( f"Are you sure you want to uninstall the plugin " + f"(delete all!) from namespace {namespace}", abort=True, ) api = lib.get_api(**obj) lib.run_plugins_task(api, "uninstall_plugin", dict(namespace=namespace), f"Uninstalling {namespace}")
def uninstall(obj, show_logs, namespaces): """Uninstall named plugin(s).""" if namespaces and not obj["plugins_force"]: lib.log("Preparing to uninstall: " + ", ".join(namespaces)) click.confirm( "Are you sure?", abort=True, ) api = lib.get_api(**obj) for namespace in namespaces: lib.run_plugins_task( api, "uninstall_plugin", dict(namespace=namespace), f"Uninstalling {namespace}", print_output=show_logs, )
def add_task(obj, description, task_host, task_token, namespace, task, params, cron, jitter): """Add new scheduled task.""" api = lib.get_api(**obj) lib.run_plugins_task( api, "add_scheduled_task", dict( description=description, host=task_host, token=task_token, namespace=namespace, task=task, params=params, cron=cron, jitter=jitter, ), "Adding scheduled task", )
def dev_destroy(obj, all, namespaces): """Destroy namespace(s) of given name. Only useful during development""" api = lib.get_api(**obj) if all: click.confirm( "Are you sure you want to destroy all namespaces?", abort=True, ) lib.run_plugins_task( api, "dev_wipe", dict(), "Destroying all namespaces", ) else: for namespace in namespaces: lib.run_plugins_task( api, "dev_destroy_namespace", dict(namespace=namespace), f"Destroying namespace: {namespace}", )
def _install_plugin(api, filename: Path, print_output: bool = False): """Use the API to install plugin directly from a file.""" if not filename.is_file(): lib.log_error(f"Cannot find plugin: {filename}", abort=True) blob_id = lib.resilient_call( api.upload_file_as_blob, filename.as_posix(), description=f"api.upload_file_as_blob({filename})", idempotent=True, # re-uploading a blob is safe ) lib.log(f"Uploaded {filename} to blob: {blob_id}") return lib.run_plugins_task( api, "install_plugin", dict(blob_id=blob_id), "Installing", print_output=print_output, idempotent=True, # re-installing a plugin should be safe )
def dev_list(obj): """Print information about the namespace usage of installed plugins.""" api = lib.get_api(**obj) lib.run_plugins_task(api, "list_namespaces", dict(), "Fetching list of namespaces")
def dev_destroy_namespace(obj, namespace): """Destroy namespace of given name. Only useful during development""" api = lib.get_api(**obj) lib.run_plugins_task(api, "dev_destroy_namespace", dict(namespace=namespace), "Destroying namespace")
def list_tasks(obj): """List all scheduled tasks.""" api = lib.get_api(**obj) lib.run_plugins_task(api, "list_scheduled_tasks", {}, "Fetching list of scheduled tasks")