-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,569 additions
and
288 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
-c main.txt | ||
|
||
# Make tox faster | ||
tox-uv | ||
|
||
# Type checking | ||
mypy | ||
types-pyyaml | ||
|
||
# Linting | ||
ruff | ||
pre-commit | ||
|
||
# Testing | ||
pytest | ||
pytest-asyncio | ||
pytest-cov | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .purger import Purger | ||
|
||
__all__ = ["Purger"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,103 @@ | ||
"""Command-line interface for Google Filestore tools.""" | ||
"""CLI for the filesystem purger.""" | ||
|
||
import argparse | ||
import asyncio | ||
import os | ||
from pathlib import Path | ||
|
||
import yaml | ||
from pydantic import ValidationError | ||
from safir.logging import LogLevel, Profile | ||
|
||
from .constants import ENV_PREFIX | ||
from .models.config import Config | ||
from .models.v1.policy import Policy | ||
from .purger import Purger | ||
from .constants import POLICY_FILE, ENV_PREFIX | ||
|
||
|
||
def _add_options() -> argparse.ArgumentParser: | ||
"""Add options applicable to any filestore tool.""" | ||
parser = argparse.ArgumentParser() | ||
def _add_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: | ||
parser.add_argument( | ||
"-f", | ||
"--file", | ||
"-c", | ||
"--config-file", | ||
"--config", | ||
help="Application configuration file", | ||
) | ||
parser.add_argument( | ||
"-p", | ||
"--policy-file", | ||
help="Policy file for purger", | ||
default=os.environ.get(f"{ENV_PREFIX}FILE", POLICY_FILE), | ||
type=Path, | ||
required=True, | ||
"--policy", | ||
help="Purger policy configuration file", | ||
) | ||
parser.add_argument( | ||
"-x", | ||
"--dry-run", | ||
help="Do not perform actions, but print what would be done", | ||
type=bool, | ||
default=bool(os.environ.get(f"{ENV_PREFIX}DRY_RUN", "")), | ||
"-d", "--debug", action="store_true", help="Enable debug logging" | ||
) | ||
parser.add_argument( | ||
"-d", | ||
"--debug", | ||
"--verbose", | ||
default=bool(os.environ.get(f"{ENV_PREFIX}DEBUG", "")), | ||
type=bool, | ||
help="Verbose debugging output", | ||
"-x", | ||
"--dry-run", | ||
action="store_true", | ||
help="Do not act, but report what would be done", | ||
) | ||
|
||
return parser | ||
|
||
def purge() -> None: | ||
"""Purge the target filesystems.""" | ||
args = _get_options().parse_args() | ||
purger = Purger( | ||
policy_file=args.policy, | ||
dry_run=args.dry_run, | ||
debug=args.debug | ||
|
||
def _postprocess_args_to_config(raw_args: argparse.Namespace) -> Config: | ||
config: Config | None = None | ||
override_cf = raw_args.config_file or os.getenv( | ||
ENV_PREFIX + "CONFIG_FILE", "" | ||
) | ||
if override_cf: | ||
config_file = Path(override_cf) | ||
try: | ||
config_obj = yaml.safe_load(config_file.read_text()) | ||
config = Config.model_validate(config_obj) | ||
except (FileNotFoundError, UnicodeDecodeError, ValidationError): | ||
config = Config() | ||
else: | ||
config = Config() | ||
# Validate policy. If the file is specified, use that; if not, use | ||
# defaults from config. | ||
override_pf = raw_args.policy_file or os.getenv( | ||
ENV_PREFIX + "POLICY_FILE", "" | ||
) | ||
policy_file = Path(override_pf) if override_pf else config.policy_file | ||
policy_obj = yaml.safe_load(policy_file.read_text()) | ||
Policy.model_validate(policy_obj) | ||
# If we get this far, it's a legal policy file. | ||
config.policy_file = policy_file | ||
# For dry-run and debug, if specified, use that, and if not, do whatever | ||
# the config says. | ||
if raw_args.debug is not None: | ||
if raw_args.debug: | ||
config.logging.log_level = LogLevel.DEBUG | ||
config.logging.profile = Profile.development | ||
else: | ||
# User asked for no debug, so let's override the config. | ||
# I guess? | ||
config.logging.log_level = LogLevel.INFO | ||
config.logging.profile = Profile.production | ||
if raw_args.dry_run is not None: | ||
config.dry_run = raw_args.dry_run | ||
return config | ||
|
||
|
||
def _get_executor(desc: str) -> Purger: | ||
parser = argparse.ArgumentParser(description=desc) | ||
parser = _add_args(parser) | ||
args = parser.parse_args() | ||
config = _postprocess_args_to_config(args) | ||
return Purger(config=config) | ||
|
||
|
||
def report() -> None: | ||
"""Report what files would be purged.""" | ||
reporter = _get_executor("Report what files would be purged.") | ||
asyncio.run(reporter.plan()) | ||
asyncio.run(reporter.report()) | ||
|
||
|
||
def purge() -> None: | ||
"""Purge files.""" | ||
purger = _get_executor("Purge files.") | ||
asyncio.run(purger.plan()) | ||
asyncio.run(purger.purge()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""Exceptions for the purger.""" | ||
|
||
from safir.slack.blockkit import SlackException | ||
|
||
|
||
class PlanNotReadyError(SlackException): | ||
"""An operation needing a Plan was requested, but no Plan is ready.""" | ||
|
||
|
||
class PolicyNotFoundError(SlackException): | ||
"""No Policy matching the given directory was found.""" |
Oops, something went wrong.