autorestart rclone sync jobs on failure

This commit is contained in:
Chaos 2023-03-10 11:09:11 +00:00
parent 7fd04a7551
commit d0583ddb89
No known key found for this signature in database
4 changed files with 40 additions and 17 deletions

View file

@ -141,7 +141,6 @@ in {
{
source = "/home/misskey/misskey-files";
dest = "Storage-Media-Crypt:";
serviceConfig = {};
timerConfig = {
OnStartupSec = "60";
OnCalendar = "4h";

View file

@ -20,5 +20,6 @@
compressImage = false;
squashfsCompression = "zstd -Xcompression-level 1";
};
config.services.openssh.permitRootLogin = lib.mkForce "yes";
}

View file

@ -19,16 +19,13 @@
./secrets.nix
./profiles/wireguard.nix
./profiles/harry-vpn.nix
./profiles/misskey-dev.nix
];
environment.systemPackages = with pkgs; [teamviewer];
home-manager.users.root = {
imports = with tree; [home.base];
home.stateVersion = "22.05";
};
home-manager.users.chaos = {
imports = with tree; [
home.base

View file

@ -9,22 +9,26 @@ with lib; let
makeNameSafe = name: builtins.replaceStrings ["/" ":"] ["-" "-"] name;
daemonService = sync_config:
lib.mkMerge [
daemonService = sync_config: {
serviceConfig = lib.mkMerge [
{
serviceConfig = {
Type = "oneshot";
Type = "oneshot";
User =
if cfg.user != null
then "${cfg.user}"
else "root";
User =
if cfg.user != null
then "${cfg.user}"
else "root";
ExecStart = "${pkgs.rclone}/bin/rclone sync ${sync_config.source} ${sync_config.dest} -P";
};
ExecStart = "${pkgs.rclone}/bin/rclone sync ${sync_config.source} ${sync_config.dest} -P";
}
(lib.mkIf sync_config.autoRestart {
TimeoutSec = 60;
Restart = "on-failure";
})
sync_config.serviceConfig
];
};
in {
options = {
services.rclone-sync = {
@ -43,9 +47,22 @@ in {
options = {
source = mkOption {type = types.str;};
dest = mkOption {type = types.str;};
autoRestart = mkOption {
type = types.bool;
default = true;
};
timerConfig = mkOption {type = types.attrs;};
serviceConfig = mkOption {type = types.attrs;};
timerConfig = mkOption {
type = types.attrs;
default = {
OnStartupSec = "60";
OnCalendar = "4h";
};
};
serviceConfig = mkOption {
type = types.attrs;
default = {};
};
};
});
default = [];
@ -55,6 +72,15 @@ in {
config = mkMerge [
(mkIf (cfg.enable && cfg.sync_jobs != []) {
envionment.systemPackages = let
allServices = lib.concatStringsSep " " (map (job: "rclone-sync-${makeNameSafe job.source}-${makeNameSafe job.dest}"));
in [
(pkgs.writeShellScriptBin "rclone-sync-all" ''
systemctl stop ${allServices}
systemctl start --wait ${allServices}
'')
];
systemd.services = listToAttrs (map (job: {
name = "rclone-sync-${makeNameSafe job.source}-${makeNameSafe job.dest}";
value = daemonService job;