Compare commits

...

5 Commits

6 changed files with 21 additions and 91 deletions

15
.gitignore vendored
View File

@ -1,7 +1,10 @@
result /result
config.json
log.txt
__pycache__/ __pycache__/
.vscode/ /.vscode/
.direnv/ /.direnv/
.mypy_cache/ /.mypy_cache/
/config.json
/log.txt
/api.key

View File

@ -13,7 +13,7 @@ def loadSettings(path: str | None) -> dict:
Load the settings file. Load the settings file.
`path` Path to a settings file. `path` Path to a settings file.
By default the program will look for `$HOME/dyn-gandi/config.json` and then `/etc/dyn-gandi.json`. By default the program will look for `$HOME/.config/dyn-gandi/config.json` and then `/etc/dyn-gandi.json`.
''' '''
if path is None: if path is None:
@ -22,6 +22,11 @@ def loadSettings(path: str | None) -> dict:
if not os.path.exists(path): if not os.path.exists(path):
path = '/etc/dyn-gandi.json' path = '/etc/dyn-gandi.json'
if not os.path.exists(path):
print(
f"[ERROR] No config file found at '~/.config/dyn-gandi/config.json' or '/etc/dyn-gandi.json'")
quit()
if not os.path.exists(path): if not os.path.exists(path):
print(f"[ERROR] '{path}' does not exist.") print(f"[ERROR] '{path}' does not exist.")
quit() quit()
@ -52,7 +57,7 @@ def loadSettings(path: str | None) -> dict:
def log(logline: str, path: str = './log.txt', stdout: bool = False) -> None: def log(logline: str, path: str = './log.txt', stdout: bool = False) -> None:
''' '''
Appends logline at the end of the file file with a timestamp Appends `logline` at the end of the file file with a timestamp.
`logline` The line to log. `logline` The line to log.
@ -125,8 +130,8 @@ def checkRecords(api: dict[str, dict[str, list[str]]], current_ip: str, log: Cal
records_to_renew[key][domain] = list(set(records_to_renew[key][domain]) - set(record['rrset_name'] for record in ( records_to_renew[key][domain] = list(set(records_to_renew[key][domain]) - set(record['rrset_name'] for record in (
filter(lambda x: current_ip in x['rrset_values'], response.json())))) filter(lambda x: current_ip in x['rrset_values'], response.json()))))
if records_to_renew[key][domain] != []: if (records := records_to_renew[key][domain]) != []:
log(f"[INFO][{','.join(records_to_renew)}] Record need to be renewed") log(f"[INFO][{','.join(records)}] Records need to be renewed")
else: else:
log(f"[ERROR][{domain}][{response.status_code}] {response.json()}") log(f"[ERROR][{domain}][{response.status_code}] {response.json()}")

View File

@ -21,14 +21,6 @@
default = pkgs.${system}.poetry2nix.mkPoetryApplication {projectDir = self;}; default = pkgs.${system}.poetry2nix.mkPoetryApplication {projectDir = self;};
}); });
# `nix run`
apps = forAllSystems (system: {
default = {
program = "${self.packages.${system}.default}/bin/dyn_gandi";
type = "app";
};
});
# `nix develop` # `nix develop`
devShells = forAllSystems (system: { devShells = forAllSystems (system: {
default = pkgs.${system}.mkShellNoCC { default = pkgs.${system}.mkShellNoCC {

View File

@ -1,70 +0,0 @@
self: {
config,
lib,
pkgs,
...
}: let
cfg = config.dyn-gandi;
package = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
inherit (lib) mkIf mkEnableOption mkOption types;
format = pkgs.formats.json {};
configFile = format.generate "config.json" cfg.settings;
in {
options.dyn-gandi = {
enable = mkEnableOption "dyn-gandi";
timer = lib.mkOption {
type = types.nullOr types.int;
default = null;
description = lib.mdDoc ''
The time intervall in seconds the script should be repeated.
'';
};
settings = mkOption {
type = with types; let
valueType = nullOr (oneOf [
# TODO: restrict type to actual config file structure
bool
int
float
str
path
(attrsOf valueType)
(listOf valueType)
]);
in
valueType;
default = throw "Please specify dyn-gandi.settings";
};
};
config = mkIf cfg.enable {
home.packages = [package]; # TODO: make this architecture independent
systemd.user.services.dyn-gandi = mkIf (cfg.timer
!= null) {
Unit = {
After = ["network.target"];
};
Install = {WantedBy = ["default.target"];};
Service = {
Type = "oneshot";
ExecStart = "${package}/bin/dyn_gandi --config ${configFile}";
};
};
systemd.user.timers.dyn-gandi = mkIf (cfg.timer
!= null) {
Install = {WantedBy = ["timers.target"];};
Timer = {
OnBootSec = "0s";
OnUnitActiveSec = "${toString cfg.timer}s";
Unit = "dyn-gandi.service";
};
};
};
}

View File

@ -46,7 +46,7 @@ in {
systemd.services.dyn-gandi = mkIf (cfg.timer systemd.services.dyn-gandi = mkIf (cfg.timer
!= null) { != null) {
script = "${package}/bin/dyn_gandi --config ${configFile}"; script = "${package}/bin/dyn-gandi --config ${configFile}";
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";

View File

@ -11,7 +11,7 @@ python = "^3.10"
requests = "2.29.0" requests = "2.29.0"
[tool.poetry.scripts] [tool.poetry.scripts]
dyn_gandi = "dyn_gandi.main:main" dyn-gandi = "dyn_gandi.main:main"
[build-system] [build-system]
requires = ["poetry-core"] requires = ["poetry-core"]