summary refs log tree commit diff
path: root/modules/auto-redeploy.nix
blob: f4366793f5379536c43b6344a5d552db8a0bd28b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{ config, pkgs, lib, secrets, ... }:
{
  systemd.timers = {
    "auto-redeploy" = {
      enable = false;
      wantedBy = [ "timers.target" ];
      after = [ "network.target" ];
      timerConfig = {
        #every 10 seconds
        OnCalendar = "*:*:0/10";
        Unit = "auto-redeploy.service";
      };
    };
  };
  #Emma - Auto-redeploy service  
  systemd.services = {
    "auto-redeploy" = {
      enable = false;
      stopIfChanged = false;
      restartIfChanged = false;
      #wantedBy = [ "multi-user.target" ];
      #after = [ "network.target" ];
      path = with pkgs; [ git curl nix nixos-install-tools nixos-rebuild ];
      serviceConfig = {
          ExecStart = pkgs.writeShellScript "auto-redeploy" ''
            cd /Spacebar-Open-Architecture
            #store current commit hash
            echo "Current commit: $(git rev-parse HEAD)"
            currentCommit=$(git rev-parse HEAD)
            git fetch --all
            #check if there are any new commits
            echo "Checking for new commits..."
            echo "Local: $(git rev-parse HEAD)"
            echo "Remote: $(git rev-parse @{u})"
            if [ $(git rev-parse HEAD) = $(git rev-parse @{u}) ]; then
              echo "Already up-to-date"
              exit 0
            fi
            curl -S -H "Content-Type: application/json" -d '{"username": "${config.networking.hostName} - redeploy", "content": "System started redeploy."}' ${secrets.webhooks.discord.deploy}
            #pull new commits
            git pull
            #send commit log to discord
            #curl -S -F "file=@<(git log --pretty=format:'%h - %s (%an, %ar)' $currentCommit..HEAD)" -F "filename=commit.log" -F "content=Adding commits" -H "Content-Type: multipart/form-data" ${secrets.webhooks.discord.deploy}
            
            
            
            #call ./update.sh and store output (including stderr) in a file
            (
              nix flake update
              nixos-generate-config --show-hardware-config > hardware-configuration.nix
              git add -f hardware-configuration.nix
              nixos-rebuild switch --flake ".#${config.networking.hostName}" -j`nproc` --upgrade-all
              git rm --cached hardware-configuration.nix
            ) 2>&1 | tee /tmp/update.log
            #send the output to discord as a file, with hostname as username, use paload_json
            curl -S -F "file=@/tmp/update.log" -F "filename=update.log" -F 'payload_json={"username": "${config.networking.hostName} - redeploy", "content": "Redeploy finished! Logs:"}' -H "Content-Type: multipart/form-data" ${secrets.webhooks.discord.deploy}
            


            
            #store current commit in file
            echo $(git rev-parse HEAD) > currentCommit
            '';
          #Restart = "always";
          #RestartSec = 60;
          User = "root";
      };
    };
  };
}