summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorLinnnus <[email protected]>2024-01-16 17:46:52 +0100
committerLinnnus <[email protected]>2024-01-16 17:46:52 +0100
commitd8e387ae0e06db2ff3665e0c267dd0ac48215071 (patch)
treefd3825d665f8f0fd8770077fb624f1a4b85a0642 /modules
parent0b4080777d9cd1bceb89a41b6acf4234420ff1c4 (diff)
hosts/ahmed: Add forsvarsarper
Diffstat (limited to 'modules')
-rw-r--r--modules/nixos/default.nix1
-rw-r--r--modules/nixos/forsvarsarper/default.nix67
-rw-r--r--modules/nixos/forsvarsarper/script.py28
3 files changed, 96 insertions, 0 deletions
diff --git a/modules/nixos/default.nix b/modules/nixos/default.nix
index 0bce684..4651c56 100644
--- a/modules/nixos/default.nix
+++ b/modules/nixos/default.nix
@@ -13,5 +13,6 @@
"notifications.linus.onl" = import ./nofitications.linus.onl;
"git.linus.onl" = import ./git.linus.onl;
"hellohtml.linus.onl" = import ./hellohtml.linus.onl;
+ forsvarsarper = import ./forsvarsarper;
};
}
diff --git a/modules/nixos/forsvarsarper/default.nix b/modules/nixos/forsvarsarper/default.nix
new file mode 100644
index 0000000..c7dfc90
--- /dev/null
+++ b/modules/nixos/forsvarsarper/default.nix
@@ -0,0 +1,67 @@
+# This module defines an on-demand minecraft server service which turns off the
+# server when it's not being used.
+{
+ config,
+ lib,
+ pkgs,
+ ...
+}: let
+ inherit (lib) mkIf mkEnableOption;
+
+ cfg = config.services.forsvarsarper;
+in {
+ options.services.forsvarsarper.enable = mkEnableOption "daily scan for tests";
+
+ config = mkIf cfg.enable {
+ # Create a user to run the server under.
+ users.users.forsvarsarper = {
+ description = "Runs daily scan for tests";
+ group = "forsvarsarper";
+ isSystemUser = true;
+ home = "/srv/forsvarsarper";
+ createHome = true;
+ };
+ users.groups.forsvarsarper = {};
+
+ age.secrets.forsvarsarper-env = {
+ file = ../../../secrets/forsvarsarper.env.age;
+ owner = config.users.users.forsvarsarper.name;
+ group = config.users.users.forsvarsarper.group;
+ mode = "0440";
+ };
+
+ # Create a service which simply runs script. This will be invoked by our timer.
+ systemd.services.forsvarsarper = {
+ serviceConfig = {
+ # We only want to run this once every time the timer triggers it.
+ Type = "oneshot";
+ # Run as the user we created above.
+ User = "forsvarsarper";
+ Group = "forsvarsarper";
+ WorkingDirectory = config.users.users.forsvarsarper.home;
+ };
+ script =
+ let
+ python3' = pkgs.python3.withPackages (ps: [ps.requests]);
+ in
+ ''
+ # Load the secret environment variables.
+ export $(grep -v '^#' ${config.age.secrets.forsvarsarper-env.path} | xargs)
+ # Kick off.
+ exec ${python3'}/bin/python3 ${./script.py}
+ '';
+ };
+
+ # Create a timer to activate our oneshot service.
+ systemd.timers.forsvarsarper = {
+ wantedBy = ["timers.target"];
+ partOf = ["forsvarsarper.service"];
+ after = ["network-online.target"];
+ wants = ["network-online.target"];
+ timerConfig = {
+ OnCalendar = "*-*-* 8:00:00";
+ Unit = "forsvarsarper.service";
+ };
+ };
+ };
+}
diff --git a/modules/nixos/forsvarsarper/script.py b/modules/nixos/forsvarsarper/script.py
new file mode 100644
index 0000000..7f12508
--- /dev/null
+++ b/modules/nixos/forsvarsarper/script.py
@@ -0,0 +1,28 @@
+import requests
+import os
+
+URL = "https://karriere.forsvaret.dk/varnepligt/varnepligten/cybervarnepligt/"
+TARGET_PHRASE = "Der er p&aring; nuv&aelig;rende tidspunkt ikke planlagt nogen afpr&oslash;vninger."
+
+try:
+ response = requests.get(URL);
+ print(f"Forespørgsel til {URL} gav status {response.status_code}")
+except:
+ message = "nejj den er ødelagt"
+else:
+ if TARGET_PHRASE in response.text:
+ message = "der er stadig ikke planlagt nogle afprøvninger"
+ else:
+ message = "noget har ændret sig på siden!!"
+ print(response.text)
+
+token = os.getenv("TOKEN")
+data = {
+ "title": "forsvaret status",
+ "message": message,
+ "url": URL,
+}
+response = requests.post(f"https://notifications.linus.onl/api/send-notification/{token}", json=data)
+print(f"Forespørgsel til at sende notifikation gav status {response.status_code}")
+response.raise_for_status()
+