-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CLI] New CLI tool for node operators
We now provide a CLI tool that integrates all the operations commonly performed by node operators. Currently, this CLI allows to: * generate private keys for the node, replacing a functionality that was implemented in the CCN main app directly. * run migrations, replacing the config updater script.
- Loading branch information
1 parent
d10cdd0
commit fdf844f
Showing
16 changed files
with
371 additions
and
215 deletions.
There are no files selected for viewing
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 was deleted.
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
File renamed without changes.
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,14 @@ | ||
""" | ||
Global configuration object for the CLI. Use the `get_cli_config()` method | ||
to access and modify the configuration. | ||
""" | ||
|
||
from dataclasses import dataclass | ||
from pathlib import Path | ||
|
||
|
||
@dataclass | ||
class CliConfig: | ||
config_file_path: Path | ||
key_dir: Path | ||
verbose: bool |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import typer | ||
|
||
from aleph.ccn_cli.cli_config import CliConfig | ||
from typing import cast | ||
from aleph.ccn_cli.services.keys import generate_keypair, save_keys | ||
|
||
keys_ns = typer.Typer() | ||
|
||
|
||
@keys_ns.command() | ||
def generate(ctx: typer.Context): | ||
""" | ||
Generates a new set of private/public keys for the Core Channel Node. | ||
The keys will be created in the key directory. You can modify the destination | ||
by using the --key-dir option. | ||
""" | ||
cli_config = cast(CliConfig, ctx.obj) | ||
print(cli_config) | ||
|
||
typer.echo(f"Generating a key pair in {cli_config.key_dir.absolute()}...") | ||
key_pair = generate_keypair() | ||
save_keys(key_pair, str(cli_config.key_dir)) | ||
typer.echo("Done.") | ||
|
||
|
||
@keys_ns.command() | ||
def show(ctx: typer.Context): | ||
""" | ||
Prints the private key of the node. | ||
""" | ||
cli_config = cast(CliConfig, ctx.obj) | ||
|
||
key_path = cli_config.key_dir / "node-secret.key" | ||
if not key_path.exists(): | ||
typer.echo( | ||
f"'{key_path.absolute()}' does not exist. Did you run 'keys generate'?", | ||
err=True, | ||
) | ||
raise typer.Exit(code=1) | ||
|
||
if not key_path.is_file(): | ||
typer.echo(f"'{key_path}' is not a file.", err=True) | ||
raise typer.Exit(code=1) | ||
|
||
with key_path.open() as f: | ||
typer.echo(f.read()) |
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,87 @@ | ||
import asyncio | ||
from pathlib import Path | ||
from traceback import format_exc | ||
from typing import Optional | ||
from typing import cast | ||
|
||
import typer | ||
|
||
from aleph.ccn_cli.cli_config import CliConfig | ||
from .migration_runner import run_migrations | ||
|
||
migrations_ns = typer.Typer() | ||
|
||
|
||
FILTER_SCRIPTS_HELP = ( | ||
"A filter for migration scripts. If specified, only the files " | ||
"matching the provided glob expression will be run." | ||
) | ||
|
||
KEY_FILE_HELP = ( | ||
"Path to the private key file, if any. " | ||
"Only used to upgrade the key to the latest format." | ||
) | ||
|
||
|
||
def run_migration_command( | ||
cli_config: CliConfig, | ||
command: str, | ||
filter_scripts: Optional[str], | ||
key_file: Optional[Path], | ||
): | ||
try: | ||
asyncio.run( | ||
run_migrations( | ||
cli_config=cli_config, | ||
command=command, | ||
filter_scripts=filter_scripts, | ||
key_file=key_file, | ||
) | ||
) | ||
except Exception as e: | ||
typer.echo(f"{command} failed: {e}.", err=True) | ||
if cli_config.verbose: | ||
typer.echo(format_exc()) | ||
raise typer.Exit(code=1) | ||
|
||
|
||
@migrations_ns.command() | ||
def upgrade( | ||
ctx: typer.Context, | ||
filter_scripts: Optional[str] = typer.Option( | ||
None, | ||
help=FILTER_SCRIPTS_HELP, | ||
), | ||
key_file: Optional[Path] = typer.Option( | ||
None, | ||
help=KEY_FILE_HELP, | ||
), | ||
): | ||
cli_config = cast(CliConfig, ctx.obj) | ||
run_migration_command( | ||
cli_config=cli_config, | ||
command="upgrade", | ||
filter_scripts=filter_scripts, | ||
key_file=key_file, | ||
) | ||
|
||
|
||
@migrations_ns.command() | ||
def downgrade( | ||
ctx: typer.Context, | ||
filter_scripts: Optional[str] = typer.Option( | ||
None, | ||
help=FILTER_SCRIPTS_HELP, | ||
), | ||
key_file: Optional[Path] = typer.Option( | ||
None, | ||
help=KEY_FILE_HELP, | ||
), | ||
): | ||
cli_config = cast(CliConfig, ctx.obj) | ||
run_migration_command( | ||
cli_config=cli_config, | ||
command="downgrade", | ||
filter_scripts=filter_scripts, | ||
key_file=key_file, | ||
) |
Oops, something went wrong.