diff --git a/.gitignore b/.gitignore index 59d3f8e..0057595 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ out build.log secrets.nix -meta \ No newline at end of file +meta +hardware-configuration.nix \ No newline at end of file diff --git a/nixinfra/build.sh b/nixinfra/build.sh deleted file mode 100755 index 0e5a3da..0000000 --- a/nixinfra/build.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env bash -set -e - -echo "Building '$1'..." -nix --extra-experimental-features nix-command run github:nix-community/nixos-generators -- --format proxmox --configuration "$1.nix" | tee build.log - -if [ ! -d "out/" ]; then - mkdir out/ -fi - -echo "Copying file to the output directory..." - -# Hacky! -mkdir -p out/$1 -rm -rf out/$1 out/$1.vma.zst -OUT_FILE="$(sed -n '$p' build.log)" -cp -r $OUT_FILE out/$1.vma.zst \ No newline at end of file diff --git a/nixinfra/buildall.sh b/nixinfra/buildall.sh deleted file mode 100755 index 3b4814f..0000000 --- a/nixinfra/buildall.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash -mkdir meta > /dev/null 2> /dev/null -touch meta/tagged_for_upload - -for FILE in kitteh-node-*/*; do - FILE_NO_EXTENSION="${FILE/".nix"/""}" - - # Hacky! - mkdir -p meta/$FILE - rm -rf meta/$FILE - - sha512sum $FILE > /tmp/kt-clusterbuild_sha512sum - - if [ ! -f "meta/$FILE.sha" ] || ! diff -q "/tmp/kt-clusterbuild_sha512sum" "meta/$FILE.sha" > /dev/null; then - ./build.sh $FILE_NO_EXTENSION - - if [ $? -ne 0 ]; then - echo "Failed to build, skipping..." - continue - fi - - if ! grep -q "out/$FILE_NO_EXTENSION.vma.zst" meta/tagged_for_upload; then - echo "out/$FILE_NO_EXTENSION.vma.zst" >> meta/tagged_for_upload - fi - else - echo "Not building '$FILE_NO_EXTENSION'." - fi - - mv "/tmp/kt-clusterbuild_sha512sum" "meta/$FILE.sha" -done - -echo "Done building." \ No newline at end of file diff --git a/nixinfra/commons.agent.nix b/nixinfra/commons.agent.nix index 207c8da..a829c9e 100644 --- a/nixinfra/commons.agent.nix +++ b/nixinfra/commons.agent.nix @@ -5,13 +5,6 @@ in { ./commons.nix ]; - # This is intentionally defined like this (not using braces) for updating. DO NOT CHANGE THIS. - # - greysoh - proxmox.qemuConf.memory = 8192; - proxmox.qemuConf.cores = 4; - proxmox.qemuConf.name = "k3s-agent"; - proxmox.qemuConf.diskSize = pkgs.lib.mkForce "131072"; - services.k3s = { enable = true; role = "agent"; diff --git a/nixinfra/commons.nix b/nixinfra/commons.nix index ecad457..d57158f 100644 --- a/nixinfra/commons.nix +++ b/nixinfra/commons.nix @@ -4,6 +4,7 @@ let in { imports = [ ./secrets.nix + ./hardware-configuration.nix ]; swapDevices = [ @@ -13,6 +14,9 @@ in { } ]; + boot.loader.grub.enable = true; + boot.loader.grub.device = "/dev/vda"; + systemd.services.kittehclean = { enable = true; description = "Cleans up this Kitteh node & runs init tasks"; diff --git a/nixinfra/commons.server.nix b/nixinfra/commons.server.nix index 31c86a4..9300b0a 100644 --- a/nixinfra/commons.server.nix +++ b/nixinfra/commons.server.nix @@ -5,13 +5,6 @@ in { ./commons.nix ]; - # This is intentionally defined like this (not using braces) for updating. DO NOT CHANGE THIS. - # - greysoh - proxmox.qemuConf.memory = 4096; - proxmox.qemuConf.cores = 1; - proxmox.qemuConf.name = "k3s-server"; - proxmox.qemuConf.diskSize = pkgs.lib.mkForce "32768"; - services.k3s = { enable = true; role = "server"; diff --git a/nixinfra/install-script.sh b/nixinfra/install-script.sh new file mode 100644 index 0000000..9f1fec8 --- /dev/null +++ b/nixinfra/install-script.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +export TERM="xterm-256color" +clear + +echo "KittehCluster installer" +echo "Codename 'tundra'" +echo + +sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | sudo fdisk /dev/vda + o # dos disk label + n # new partition + p # primary partition + 1 # setup boot partition + 2048 # align first sector (performance reasons?) + +500M # boot partition size + n # new partition + p # primary partition + 2 # partion number 2 + # default, start immediately after preceding partition + # default, extend partition to end of disk + a # make a partition bootable + 1 # bootable partition is partition 1 -- /dev/vda1 + w # write the partition table + q # and we're done +EOF + +sudo mkfs.fat -F 32 /dev/vda1 +sudo fatlabel /dev/vda1 BOOT +sudo mkfs.ext4 /dev/vda2 -L ROOT + +sudo mount /dev/vda2 /mnt +sudo mkdir -p /mnt/boot +sudo mount /dev/vda1 /mnt/boot + +sudo nixos-generate-config --root /mnt + +sudo mv /mnt/etc/nixos/hardware-configuration.nix /tmp/hardware-configuration.nix + +sudo rm -rf /mnt/etc/nixos/* /mnt/etc/nixos/.* +sudo nix-shell -p git --command "git clone $GIT_REPO /mnt/etc/nixos" + +if [ ! -f "/mnt/etc/nixos/install-script.sh" ]; then + echo "DEBUG: checking out 'tundra' branch..." + sudo nix-shell -p git --command "cd /mnt/etc/nixos; git checkout tundra" +fi + +sudo mv /tmp/hardware-configuration.nix /mnt/etc/nixos/nixinfra/ +sudo nixos-install -I /mnt/etc/nixos/nixinfra/$NIX_INSTALL_PATH + +sudo umount /mnt/boot +sudo umount /mnt \ No newline at end of file diff --git a/nixinfra/install.sh b/nixinfra/install.sh new file mode 100755 index 0000000..46135eb --- /dev/null +++ b/nixinfra/install.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +if [ "$GIT_REPO" == "" ]; then + export GIT_REPO="https://git.hofers.cloud/greysoh/kittehcluster" +fi + +if [ "$NIX_INSTALL_PATH" == "" ]; then + echo "ERROR: the environment variable 'NIX_INSTALL_PATH' is not set!" + echo "This can be fixed by setting it to the path of the nix file, i.e:" + echo "$ NIX_INSTALL_PATH=kitteh-node-1/server.nix ./install.sh" + exit 1 +fi + +echo "Initializing..." +FILE_ENCODED="$(cat install-script.sh | base64)" +ssh -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" nixos@$1 bash -c "'echo -n $FILE_ENCODED | base64 -d > /tmp/install.sh; GIT_REPO=$GIT_REPO NIX_INSTALL_PATH=$NIX_INSTALL_PATH bash /tmp/install.sh'" \ No newline at end of file diff --git a/nixinfra/kitteh-node-1/server.nix b/nixinfra/kitteh-node-1/server.nix index 0d40188..3187ea9 100644 --- a/nixinfra/kitteh-node-1/server.nix +++ b/nixinfra/kitteh-node-1/server.nix @@ -8,13 +8,6 @@ in { ../commons.nix ]; - # This is intentionally defined like this (not using braces) for updating. DO NOT CHANGE THIS. - # - greysoh - proxmox.qemuConf.memory = 4096; - proxmox.qemuConf.cores = 1; - proxmox.qemuConf.name = "k3s-server"; - proxmox.qemuConf.diskSize = pkgs.lib.mkForce "32768"; - networking.hostName = "kitteh-node-1-k3s-server"; services.k3s = { diff --git a/nixinfra/upload.sh b/nixinfra/upload.sh deleted file mode 100755 index bd84add..0000000 --- a/nixinfra/upload.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env bash -if [ "$BASE_IP" = "" ]; then - BASE_IP=192.168.0.20 -fi - -IP_LAST_OCTET="${BASE_IP##*.}" -IP_MAIN_OCTET="${BASE_IP%.*}" - -IP_LAST_OCTET=$((IP_LAST_OCTET-1)) - -BASE_ID=100 - -cp meta/tagged_for_upload /tmp/upload_cache - -while IFS= read -r LINE; do - UPLOAD_PATH="/var/lib/vz/dump/vzdump-qemu-$(basename $LINE .vma.zst)-$(date +"%Y_%m_%d-%H_%M_%S").vma.zst" - echo "Uploading VM dump '$LINE'..." - - CURRENT_NODE="$(dirname $LINE)" - CURRENT_NODE="${CURRENT_NODE##*-}" - IP="$IP_MAIN_OCTET.$((IP_LAST_OCTET+CURRENT_NODE))" - - rsync --info=progress2 $LINE root@$IP:$UPLOAD_PATH - - if [[ "$@" == *"--install"* ]] || [[ "$@" == *"-i"* ]]; then - echo "Installing VM dump '$LINE'..." - - ssh -n root@$IP "qmrestore $UPLOAD_PATH $BASE_ID --force --unique" - BASE_ID=$((BASE_ID+1)) - fi - - if [[ "$@" == *"--delete"* ]] || [[ "$@" == *"-d"* ]]; then - echo "Deleting VM dump '$LINE'..." - ssh -n root@$IP "rm -rf $UPLOAD_PATH" - fi - - ESCAPED_LINE=$(printf '%s\n' "$LINE" | sed -e 's/[\/&]/\\&/g') - sed -i "/$ESCAPED_LINE/d" meta/tagged_for_upload -done < /tmp/upload_cache - -echo "Done." \ No newline at end of file