From 09065695512aa05530e038d9b589104958639bcb Mon Sep 17 00:00:00 2001 From: Astro Date: Mon, 12 Dec 2022 21:57:00 +0100 Subject: [PATCH] owncast: add archiver --- hosts/owncast/default.nix | 14 +++++-- hosts/owncast/owncast-archiver.nix | 61 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 hosts/owncast/owncast-archiver.nix diff --git a/hosts/owncast/default.nix b/hosts/owncast/default.nix index cb599069..7d7a3759 100644 --- a/hosts/owncast/default.nix +++ b/hosts/owncast/default.nix @@ -1,13 +1,17 @@ { config, pkgs, ... }: { + imports = [ + ./owncast-archiver.nix + ]; + deployment = { vcpu = 8; mem = 2048; persistedShares = [ "/etc" "/home" "/var" ]; extraShares = [ { source = "/glusterfs/big/microvms/c3d2/config/owncast/archive"; - mountPoint = "/mnt/archive"; + mountPoint = config.services.owncast-archiver.targetDir; } ]; }; c3d2.hq.statistics.enable = true; @@ -38,7 +42,7 @@ proxyWebsockets = true; }; locations."/archive/" = { - alias = "/mnt/archive/"; + alias = "${config.services.owncast-archiver.targetDir}/"; extraConfig = '' fancyindex on; fancyindex_exact_size off; @@ -46,5 +50,9 @@ }; }; }; - systemd.services.nginx.serviceConfig.ReadWritePaths = [ "/mnt/archive" ]; + systemd.services.nginx.serviceConfig.ReadWritePaths = [ + config.services.owncast-archiver.targetDir + ]; + + services.owncast-archiver.enable = true; } diff --git a/hosts/owncast/owncast-archiver.nix b/hosts/owncast/owncast-archiver.nix new file mode 100644 index 00000000..15938a04 --- /dev/null +++ b/hosts/owncast/owncast-archiver.nix @@ -0,0 +1,61 @@ +{ config, lib, pkgs, ... }: + +let + + owncastArchiver = with pkgs; writeScript "owncast-archiver.sh" '' + #! ${runtimeShell} -e + + PATH=${lib.makeBinPath [ coreutils curl jq ffmpeg ]} + + while true; do + + STATUS="$(curl -s https://owncast.c3d2.de/api/status)" + ONLINE="$(echo "$STATUS" | jq -r .online)" + if [ "$ONLINE" = true ]; then + TITLE="$(echo "$STATUS" | jq -r .streamTitle)" + ffmpeg -i https://owncast.c3d2.de/hls/0/stream.m3u8 -c copy "$(echo "$(date -Iseconds)_$TITLE.mkv"|tr " +<>:/" "____\\-\\-")" + fi + + sleep ${toString cfg.pollInterval} + done + ''; + + cfg = config.services.owncast-archiver; + +in + +{ + options.services.owncast-archiver = with lib; { + enable = mkEnableOption "owncast archiver"; + + targetDir = mkOption { + type = types.str; + default = "/mnt/archive"; + }; + + pollInterval = mkOption { + type = types.int; + default = 10; + }; + }; + + config = lib.mkIf cfg.enable { + users.users.archiver = { + isSystemUser = true; + group = "nginx"; + }; + + systemd.services.owncast-archiver = { + wantedBy = [ "multi-user.target" ]; + after = [ "owncast.service" ]; + serviceConfig = { + ReadWritePaths = cfg.targetDir; + WorkingDirectory = cfg.targetDir; + User = "archiver"; + ExecStart = owncastArchiver; + Restart = "always"; + RestartSec = 60; + }; + }; + }; +}