about summary refs log tree commit diff
path: root/colmena/cobalt/services
diff options
context:
space:
mode:
authorsefidel <contact@sefidel.net>2023-02-05 02:23:01 +0900
committersefidel <contact@sefidel.net>2023-02-05 02:23:01 +0900
commitb90c4685335246ffb4fef6fd6caaf4e12dfd0e17 (patch)
treed6a9c48fd30d500cdfed74fe8143ab6473a48463 /colmena/cobalt/services
parent84b6973c35b17ea37a48e461e5f3d44173f5a23f (diff)
downloadnixrc-b90c4685335246ffb4fef6fd6caaf4e12dfd0e17.tar.gz
nixrc-b90c4685335246ffb4fef6fd6caaf4e12dfd0e17.zip
feat(colmena/services): gitolite: regularly clean trashed repositories
Diffstat (limited to 'colmena/cobalt/services')
-rw-r--r--colmena/cobalt/services/gitolite.nix51
1 files changed, 51 insertions, 0 deletions
diff --git a/colmena/cobalt/services/gitolite.nix b/colmena/cobalt/services/gitolite.nix
index 845c026..4461a30 100644
--- a/colmena/cobalt/services/gitolite.nix
+++ b/colmena/cobalt/services/gitolite.nix
@@ -1,3 +1,5 @@
+{ pkgs, ... }:
+
 {
   services.gitolite = {
     enable = true;
@@ -20,4 +22,53 @@
   environment.persistence."/persist".directories = [
     "/var/lib/gitolite"
   ];
+
+
+  systemd.timers."gitolite-trash-cleanup" = {
+    wantedBy = [ "timers.target" ];
+    timerConfig = {
+      OnCalendar = "*-*-* 00:00:00";
+      Unit = "gitolite-trash-cleanup.service";
+    };
+  };
+
+  systemd.services."gitolite-trash-cleanup" = {
+    script = ''
+      set -euo pipefail
+      if [ ! -d "Trash" ] ; then
+        echo Trash directory is nonexistent!
+        echo No operations to perform. Exiting.
+        exit 0
+      fi
+
+      match=$(find Trash -type d -regextype posix-extended -regex ".*/[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}:[0-9]{2}:[0-9]{2}$")
+      processed_entry=0
+      removed_entry=0
+
+      for dir in $match
+      do
+        system_timestamp=$(date +%s)
+        trash_timestamp=$(basename $dir | sed -e "s/_/ /g" | date -f - +%s)
+        age=$(( $system_timestamp - $trash_timestamp ))
+        # Wipe trashes older than 2w
+        if [[ age -gt 1209600 ]] ; then
+          target_dir=$(rev <<< $dir | cut -d'/' -f2- | rev)
+          echo "Removing '$target_dir' (age $age)"
+          rm -rf $target_dir
+          ((removed_entry+=1))
+        fi
+        ((processed_entry+=1))
+      done
+
+      echo "Done! Removed $removed_entry/$processed_entry"
+    '';
+
+    path = with pkgs; [ bash util-linux coreutils ];
+
+    serviceConfig = {
+      Type = "oneshot";
+      User = "git";
+      WorkingDirectory = "/var/lib/gitolite/repositories";
+    };
+  };
 }