def logs(obj, plugins): """Print the latest install logs for given plugins.""" api = lib.get_api(**obj) # Despite the name, this only fetches the latest log for each plugin, not all! raw_info = lib.resilient_call( api.run_view, "pluginsmanager", "all_plugin_logs", description="api.run_view('pluginsmanager', 'all_plugin_logs')", idempotent=True, ) if plugins: # Filter to specified plugins. raw_info = [i for i in raw_info if i["name"] in plugins] for i in raw_info: for f in ( "name", "version", "description", "action", "server", "success", "when", ): lib.log(f"{f.capitalize()}: {i[f]}") lib.log("Logs:") if i["success"]: lib.log_output(i["output"].strip()) else: lib.log_error(i["output"].strip()) lib.log("") if len(raw_info) == 0 and len(plugins) > 0: lib.log( "No logs found. Note that any plugins must be exact name matches without version info." )
def create_fixture(obj, name): """Create new fixture with given name.""" api = lib.get_api(**obj) lib.log_output( lib.dbctl_action( api, "create_fixture", dict(name=name), f"Creating fixture {name}" ) )
def delete_fixture(obj, name, yes): """Delete fixture with given name.""" if not yes: click.confirm(f'Are you sure you want to delete fixture "{name}"?', abort=True) api = lib.get_api(**obj) lib.log_output( lib.dbctl_action( api, "delete_fixture", dict(name=name), f"Deleting fixture {name}" ) )
def info(obj, logs): """Provide information about installed plugins.""" api = lib.get_api(**obj) raw_info = api.run_view("pluginsmanager", "installed_plugins_with_tags") headers = ["name", "version", "description", "when", "plugin_tags"] info = ([p[h] for h in headers] for p in raw_info) lib.log(tabulate(info, headers=headers)) if logs: for i in raw_info: name, output = i["name"], i["output"] lib.log(f"\n[Install log for {name}]") lib.log_output(output.strip())
def _log_result(result): """Pretty-print log the result from running a task, job, or view.""" if isinstance(result, FileDownloadResponse): lib.log( f"Response saved to: {result.filename} (mime_type={result.mime_type})" ) else: try: # Try to pretty print if it converts to JSON. lib.pretty_print(result, "json") except json.decoder.JSONDecodeError: # Otherwise print normally. lib.log_output(str(result))
def import_users_and_roles(obj, filename): """Import users (and roles) from given TOML file.""" filename = Path(filename) api = lib.get_api(**obj) import_data = lib.read_toml(filename) users = import_data.get("users", []) roles = import_data.get("roles", []) if users: api.post("users", json=users) if roles: api.post("roles", json=roles) lib.log_output( f"Imported {len(users)} users and {len(roles)} roles from {filename}")
def export_users_and_roles(obj, filename, with_roles): """Export users (and roles) to given TOML file.""" filename = Path(filename) api = lib.get_api(**obj) export_data = {} export_data["users"] = api.get_all_users() if with_roles: export_data["roles"] = api.get_all_roles() with filename.open(mode="w") as f: lib.pretty_print(export_data, "toml", f) lib.log_output( f"Exported {len(export_data['users'])} users and {len(export_data.get('roles', []))} roles to {filename}" )
def list_fixtures(obj): """List available fixtures.""" api = lib.get_api(**obj) lib.log_output( lib.dbctl_action(api, "list_fixtures", dict(), f"Fetching list of fixtures") )
def freeze(obj): """Print currently installed plugins as versions TOML.""" versions = PluginSpecs.make_from_plugininfos( PluginInfos.make_from_encapsia(obj["host"])).as_version_dict() lib.log_output(toml.dumps(versions))
def list_users(obj, super_users, system_users, all_users): """List out information about users.""" api = lib.get_api(**obj) if not (super_users or system_users or all_users): # If no specific type of user specified then assume all-users was intended. all_users = True if super_users: lib.log_output("[Super users]") users = api.get_super_users() headers = ["email", "first_name", "last_name"] lib.log_output( tabulate.tabulate( [[getattr(row, header) for header in headers] for row in users], headers=headers, )) lib.log_output() if system_users: lib.log_output("[System users]") users = api.get_system_users() headers = ["email", "description", "capabilities"] lib.log_output( tabulate.tabulate( [[getattr(row, header) for header in headers] for row in users], headers=headers, )) lib.log_output() if all_users: lib.log_output("[All users]") users = api.get_all_users() headers = [ "email", "first_name", "last_name", "role", "enabled", "is_site_user", ] lib.log_output( tabulate.tabulate([[row[header] for header in headers] for row in users], headers=headers)) lib.log_output()
def freeze(obj): """Print currently installed plugins as versions TOML.""" api = lib.get_api(**obj) raw_info = api.run_view("pluginsmanager", "installed_plugins_with_tags") info = {i["name"]: i["version"] for i in raw_info} lib.log_output(toml.dumps(info))