diff --git a/README.md b/README.md index 15a4b77..d4e68ea 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Currently, I cannot recommend that you use this setup in production yet. I have 2. Copy `secrets.example.nix` to `secrets.nix`. 3. Change `services.k3s.token` to be a unique token (i.e. using `uuidgen`, `head -c 500 /dev/random | sha1sum | cut -d " " -f 1`, etc) 4. Change `users.users.clusteradm.openssh.authorizedKeys.keys` to have your SSH key(s) in there. -5. Then, run `./buildall.sh`, to build all the virtual machines. This may take a long time, depending on your hardware! On a 2015 MacBook Air, this took 30 minutes. Make some tea while you wait! +5. Then, run `./build.sh all`, to build all the virtual machines. This may take a long time, depending on your hardware! On a 2015 MacBook Air, this took 30 minutes. Make some tea while you wait! 6. Finally, run `BASE_IP=your_base_ip_here ./upload.sh -i -d`, with `BASE_IP` being the first IP for your Proxmox cluster. 7. Set all VMs to auto-start, then turn them all on, starting with the first node's `k3s-server`. 8. You can now connect using your SSH key to any of the nodes with the user `clusteradm`. The default password is `1234`. Be sure to change this! diff --git a/nixinfra/build.sh b/nixinfra/build.sh old mode 100755 new mode 100644 index 0e5a3da..b2f27a0 --- a/nixinfra/build.sh +++ b/nixinfra/build.sh @@ -1,17 +1,59 @@ #!/usr/bin/env bash -set -e +# e - script stops on error (return != 0) +# u - error if undefined variable +# o pipefail - script fails if one of piped commands fails +# x - output each line (debug) +set -euo pipefail -echo "Building '$1'..." -nix --extra-experimental-features nix-command run github:nix-community/nixos-generators -- --format proxmox --configuration "$1.nix" | tee build.log +if [ "$1" = "all" ]; then + BUILT=() + for file in kitteh-node-*/*; do + FILE_NO_EXTENSION="${file/".nix"/""}" + + # checksum modification checking + if [ ! -d "meta/$(dirname "$file")" ]; then + mkdir -p "meta/$(dirname "$file")" + fi + sha512sum "$file" > /tmp/kt-clusterbuild_sha512sum -if [ ! -d "out/" ]; then - mkdir out/ + if [ ! -f "meta/$file.sha" ] || ! diff -q "/tmp/kt-clusterbuild_sha512sum" "meta/$file.sha" > /dev/null; then + ./"${0}" "$FILE_NO_EXTENSION" + + # shellcheck disable=SC2181 + 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'." + continue + fi + + BUILT+=("$FILE_NO_EXTENSION") + + mv "/tmp/kt-clusterbuild_sha512sum" "meta/$file.sha" + done + + echo "Done building:" + declare -p BUILT fi -echo "Copying file to the output directory..." +if [ "$1" != "all" ]; then + echo "Building '$1'..." + nix --extra-experimental-features nix-command run github:nix-community/nixos-generators -- --format proxmox --configuration "$1.nix" | tee build.log -# 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 + if [ ! -d "out/" ]; then + mkdir out/ + fi + + echo "Copying file to the output directory..." + + mkdir -p "out/$(dirname "$1")" + rm -rf out/"$1".vma.zst + OUT_FILE="$(sed -n '$p' build.log)" + cp -r "$OUT_FILE" out/"$1".vma.zst +fi \ 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/upload.sh b/nixinfra/upload.sh index bd84add..f2fd4b4 100755 --- a/nixinfra/upload.sh +++ b/nixinfra/upload.sh @@ -1,4 +1,35 @@ #!/usr/bin/env bash +# e - script stops on error (return != 0) +# u - error if undefined variable +# o pipefail - script fails if one of piped commands fails +# x - output each line (debug) +set -euo pipefail +function usage() { + echo "Usage: $0 [options] [--] + + Options: + -h|help Display this message + -i|install Install VM Dumps + -d|delete Delete VM Dumps" +} + +# default values +INSTALL=0 +DELETE=0 + +# handle commandline arguments +while getopts ":hid" opt +do + # shellcheck disable=SC2214 + case $opt in + h|help) usage; exit 0;; + i|install) INSTALL=1;; + d|delete) DELETE=1;; + *) echo -e "\n Option does not exist: $OPTARG\n"; usage; exit 1;; + esac +done +shift $((OPTIND-1)) + if [ "$BASE_IP" = "" ]; then BASE_IP=192.168.0.20 fi @@ -13,25 +44,25 @@ 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" + 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="$(dirname "$LINE")" CURRENT_NODE="${CURRENT_NODE##*-}" IP="$IP_MAIN_OCTET.$((IP_LAST_OCTET+CURRENT_NODE))" - rsync --info=progress2 $LINE root@$IP:$UPLOAD_PATH + rsync --info=progress2 "$LINE" root@"$IP":"$UPLOAD_PATH" - if [[ "$@" == *"--install"* ]] || [[ "$@" == *"-i"* ]]; then + if [[ $INSTALL -eq 1 ]]; then echo "Installing VM dump '$LINE'..." - ssh -n root@$IP "qmrestore $UPLOAD_PATH $BASE_ID --force --unique" + ssh -n root@"$IP" "qmrestore $UPLOAD_PATH $BASE_ID --force --unique" BASE_ID=$((BASE_ID+1)) fi - if [[ "$@" == *"--delete"* ]] || [[ "$@" == *"-d"* ]]; then + if [[ $DELETE -eq 1 ]]; then echo "Deleting VM dump '$LINE'..." - ssh -n root@$IP "rm -rf $UPLOAD_PATH" + ssh -n root@"$IP" "rm -rf $UPLOAD_PATH" fi ESCAPED_LINE=$(printf '%s\n' "$LINE" | sed -e 's/[\/&]/\\&/g')