-
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
Francesco Faraone
committed
Feb 28, 2024
1 parent
1b64602
commit 65f2506
Showing
19 changed files
with
1,745 additions
and
738 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ jobs: | |
poetry install | ||
- name: Linting | ||
run: | | ||
flake8 | ||
ruff check . | ||
- name: Testing | ||
run: | | ||
pytest | ||
|
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 |
---|---|---|
|
@@ -29,7 +29,7 @@ jobs: | |
poetry install | ||
- name: Linting | ||
run: | | ||
flake8 | ||
ruff check . | ||
- name: Unit tests | ||
run: | | ||
pytest | ||
|
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
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
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,4 +1,4 @@ | ||
from sud.cli import main | ||
from sud.cli import app | ||
|
||
if __name__ == "__main__": | ||
main() | ||
app() |
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,82 +1,133 @@ | ||
import logging | ||
from datetime import timedelta | ||
from pathlib import Path | ||
from typing import Annotated, Optional, Tuple | ||
|
||
from rich.console import Console | ||
import typer | ||
from rich import print | ||
from rich.logging import RichHandler | ||
|
||
from sud import click as click | ||
from sud import config, get_version | ||
from sud import get_version | ||
from sud.config import Config | ||
from sud.prompt import APISecretPrompt, HostnamePrompt | ||
from sud.updater import Updater | ||
|
||
console = Console() | ||
app = typer.Typer( | ||
add_completion=False, | ||
rich_markup_mode="rich", | ||
) | ||
|
||
|
||
@click.group() | ||
@click.option( | ||
"-c", | ||
"--config-file", | ||
type=click.Path( | ||
dir_okay=False, | ||
readable=True, | ||
writable=True, | ||
), | ||
default="/etc/sud/sud-config.yml", | ||
help="Load the configuration file from a specific location.", | ||
) | ||
@click.version_option(get_version()) | ||
@config.pass_config | ||
def cli(ctx, config_file): | ||
""" | ||
SUD the Python Scaleway DNS Updater utility. | ||
""" | ||
def version_callback(value: bool): | ||
if value: | ||
print(f"[bold dark_orange]SUD[/] version: {get_version()}") | ||
raise typer.Exit() | ||
|
||
|
||
@cli.command(load_config=False) | ||
# @click.option( | ||
# "-H", | ||
# "--hostname", | ||
# required=True, | ||
# prompt="[cyan]Hostname[/cyan]", | ||
# ) | ||
# @click.option( | ||
# "-s", | ||
# "--api-secret", | ||
# required=True, | ||
# prompt="[cyan]Scaleway API secret[/cyan]", | ||
# hide_input=True, | ||
# confirmation_prompt=True, | ||
# ) | ||
@click.option( | ||
"-s", | ||
"--frequency", | ||
required=True, | ||
prompt="Check frequency", | ||
default=300, | ||
show_default=True, | ||
) | ||
@config.pass_config | ||
def init(config, frequency): | ||
pass | ||
def validate_frequency_callback(value: int): | ||
if value < 60: | ||
raise typer.BadParameter("Minimum frequency is once per minutes (60 seconds).") | ||
return value | ||
|
||
|
||
@app.command() | ||
def init( | ||
ctx: typer.Context, | ||
hostname: Annotated[ | ||
Optional[str], | ||
typer.Option( | ||
"--hostname", | ||
"-H", | ||
show_default=False, | ||
help="Hostname that must be created/updated.", | ||
), | ||
] = None, | ||
api_secret: Annotated[ | ||
Optional[str], | ||
typer.Option( | ||
"--api-secret", | ||
"-s", | ||
show_default=False, | ||
help="Scaleway API secret.", | ||
), | ||
] = None, | ||
frequency: Annotated[ | ||
Optional[int], | ||
typer.Option( | ||
"--frequency", | ||
"-f", | ||
callback=validate_frequency_callback, | ||
help="Number of seconds between checks.", | ||
), | ||
] = 300, | ||
telegram: Annotated[ | ||
Optional[Tuple[int, str]], | ||
typer.Option( | ||
"--telegram-notifications", | ||
"-t", | ||
metavar="CHAT_ID TOKEN", | ||
help="Add telegram configuration for notifications.", | ||
), | ||
] = (None, None), | ||
): | ||
"""Generate a SUD configuration file.""" | ||
if not hostname: | ||
hostname = HostnamePrompt.ask( | ||
"Enter the hostname that must be created or update", | ||
) | ||
|
||
@cli.command() | ||
@config.pass_config | ||
def run(config): | ||
if not api_secret: | ||
api_secret = APISecretPrompt.ask( | ||
"Enter the Scaleway API secret", | ||
password=True, | ||
) | ||
|
||
config: Config = ctx.obj | ||
config.hostname = hostname | ||
config.api_secret = api_secret | ||
config.frequency = timedelta(seconds=frequency) | ||
config.store() | ||
|
||
|
||
@app.command() | ||
def run(ctx: typer.Context): | ||
"""Run the [magenta][link=https://scaleway.com]Scaleway[/link][/magenta] \ | ||
DNS Updater.""" | ||
logging.basicConfig( | ||
level=logging.INFO, | ||
format="%(message)s", | ||
datefmt="[%X]", | ||
handlers=[RichHandler()], | ||
) | ||
updater = Updater(config) | ||
updater = Updater(ctx.obj) | ||
updater.run() | ||
|
||
|
||
def main(): | ||
try: | ||
cli(standalone_mode=False) | ||
except click.ClickException as e: | ||
console.print(f"[bold red]Error:[/bold red] {str(e)}") | ||
except click.Abort: | ||
pass | ||
except Exception: | ||
console.print_exception() | ||
@app.callback() | ||
def main( | ||
ctx: typer.Context, | ||
config_file: Annotated[ | ||
Optional[Path], | ||
typer.Option( | ||
"--config-file", | ||
"-c", | ||
help="Load the configuration file from a specific location.", | ||
), | ||
] = "/etc/sud/sud-config.yml", | ||
version: Annotated[ | ||
Optional[bool], | ||
typer.Option("--version", callback=version_callback, is_eager=True), | ||
] = None, | ||
): | ||
""" | ||
[bold dark_orange]SUD[/] is a small DDNS updater utility for the | ||
Scaleway DNS service. | ||
It periodically (default: 300 seconds) check for the IP public IP | ||
address from through the utility goes to internet | ||
and create or update a [cyan]Host (A)[/] record in the DNS zone of the | ||
specified domain name. | ||
""" | ||
config = Config(config_file) | ||
ctx.obj = config | ||
if ctx.invoked_subcommand != "init": | ||
config.load() |
Oops, something went wrong.