Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/cli.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import functools | # WARNING: do not import unnecessary things here to keep cli startup time under | ||||
# control | |||||
import logging | import logging | ||||
import os | import os | ||||
from typing import Dict, Optional | from typing import Dict, Optional | ||||
import click | import click | ||||
from swh.core import config | |||||
from swh.core.cli import CONTEXT_SETTINGS | from swh.core.cli import CONTEXT_SETTINGS | ||||
from swh.journal.client import get_journal_client | |||||
from swh.storage import get_storage | |||||
from swh.storage.api.server import app | |||||
try: | try: | ||||
from systemd.daemon import notify | from systemd.daemon import notify | ||||
except ImportError: | except ImportError: | ||||
notify = None | notify = None | ||||
@click.group(name="storage", context_settings=CONTEXT_SETTINGS) | @click.group(name="storage", context_settings=CONTEXT_SETTINGS) | ||||
Show All 13 Lines | help=( | ||||
"if set, override the value present in the configuration file if any. " | "if set, override the value present in the configuration file if any. " | ||||
"Defaults to 'read' for the 'backfill' command, and 'write' for 'rpc-server' " | "Defaults to 'read' for the 'backfill' command, and 'write' for 'rpc-server' " | ||||
"and 'replay' commands." | "and 'replay' commands." | ||||
), | ), | ||||
) | ) | ||||
@click.pass_context | @click.pass_context | ||||
def storage(ctx, config_file, check_config): | def storage(ctx, config_file, check_config): | ||||
"""Software Heritage Storage tools.""" | """Software Heritage Storage tools.""" | ||||
from swh.core import config | |||||
if not config_file: | if not config_file: | ||||
config_file = os.environ.get("SWH_CONFIG_FILENAME") | config_file = os.environ.get("SWH_CONFIG_FILENAME") | ||||
if config_file: | if config_file: | ||||
if not os.path.exists(config_file): | if not os.path.exists(config_file): | ||||
raise ValueError("%s does not exist" % config_file) | raise ValueError("%s does not exist" % config_file) | ||||
conf = config.read(config_file) | conf = config.read(config_file) | ||||
else: | else: | ||||
Show All 29 Lines | @click.option( | ||||
help="Indicates if the server should run in debug mode", | help="Indicates if the server should run in debug mode", | ||||
) | ) | ||||
@click.pass_context | @click.pass_context | ||||
def serve(ctx, host, port, debug): | def serve(ctx, host, port, debug): | ||||
"""Software Heritage Storage RPC server. | """Software Heritage Storage RPC server. | ||||
Do NOT use this in a production environment. | Do NOT use this in a production environment. | ||||
""" | """ | ||||
from swh.storage.api.server import app | |||||
if "log_level" in ctx.obj: | if "log_level" in ctx.obj: | ||||
logging.getLogger("werkzeug").setLevel(ctx.obj["log_level"]) | logging.getLogger("werkzeug").setLevel(ctx.obj["log_level"]) | ||||
ensure_check_config(ctx.obj["config"], ctx.obj["check_config"], "write") | ensure_check_config(ctx.obj["config"], ctx.obj["check_config"], "write") | ||||
app.config.update(ctx.obj["config"]) | app.config.update(ctx.obj["config"]) | ||||
app.run(host, port=int(port), debug=bool(debug)) | app.run(host, port=int(port), debug=bool(debug)) | ||||
@storage.command() | @storage.command() | ||||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | |||||
) | ) | ||||
@click.pass_context | @click.pass_context | ||||
def replay(ctx, stop_after_objects): | def replay(ctx, stop_after_objects): | ||||
"""Fill a Storage by reading a Journal. | """Fill a Storage by reading a Journal. | ||||
There can be several 'replayers' filling a Storage as long as they use | There can be several 'replayers' filling a Storage as long as they use | ||||
the same `group-id`. | the same `group-id`. | ||||
""" | """ | ||||
import functools | |||||
from swh.journal.client import get_journal_client | |||||
from swh.storage import get_storage | |||||
from swh.storage.replay import process_replay_objects | from swh.storage.replay import process_replay_objects | ||||
ensure_check_config(ctx.obj["config"], ctx.obj["check_config"], "write") | ensure_check_config(ctx.obj["config"], ctx.obj["check_config"], "write") | ||||
conf = ctx.obj["config"] | conf = ctx.obj["config"] | ||||
storage = get_storage(**conf.pop("storage")) | storage = get_storage(**conf.pop("storage")) | ||||
client_cfg = conf.pop("journal_client") | client_cfg = conf.pop("journal_client") | ||||
▲ Show 20 Lines • Show All 47 Lines • Show Last 20 Lines |