add: better upload and build scripts

This commit is contained in:
valerie 2024-07-14 21:25:14 -04:00
parent 3f56dde55c
commit af5e1f5432
Signed by: valnyx
GPG key ID: CC10324DD962CB7E
4 changed files with 92 additions and 51 deletions

View file

@ -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!

64
nixinfra/build.sh Executable file → Normal file
View file

@ -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
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

View file

@ -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."

View file

@ -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')