nixcfg/deploy.sh

57 lines
1.4 KiB
Bash
Raw Normal View History

2021-11-02 09:15:37 +00:00
#!/usr/bin/env bash
2021-11-16 18:00:15 +00:00
cd $(dirname $0)
2021-11-18 14:40:06 +00:00
if [ -z "$@" ]; then
2021-11-18 15:40:19 +00:00
NIXHOSTLIST=$(ls node | grep -v '\.site\.')
2021-11-18 14:40:06 +00:00
else
NIXHOSTLIST="$@"
fi
TMP_PATH=/tmp/tmp-deploy-$(date +%s)
2021-12-30 18:27:32 +00:00
YEAR=$(date +%Y)
2021-11-18 14:40:06 +00:00
for NIXHOST in $NIXHOSTLIST; do
2021-11-16 18:00:15 +00:00
NIXHOST=${NIXHOST%.*}
if [ -z "$SSH_USER" ]; then
SSH_DEST=$NIXHOST
else
SSH_DEST=$SSH_USER@$NIXHOST
fi
2021-11-02 09:15:37 +00:00
echo "==== DOING $NIXHOST ===="
2021-11-16 18:00:15 +00:00
2021-12-30 12:27:39 +00:00
echo "Sending NixOS config files"
2021-12-30 18:27:32 +00:00
ssh -F ssh_config $SSH_DEST mkdir -p $TMP_PATH $TMP_PATH/pki
cat configuration.nix | ssh -F ssh_config $SSH_DEST tee $TMP_PATH/configuration.nix > /dev/null
cat node/$NIXHOST.nix | ssh -F ssh_config $SSH_DEST tee $TMP_PATH/node.nix > /dev/null
cat node/$NIXHOST.site.nix | ssh -F ssh_config $SSH_DEST tee $TMP_PATH/site.nix > /dev/null
2021-12-30 12:27:39 +00:00
echo "Sending secret files"
2021-12-30 18:27:32 +00:00
for SECRET in rclone.conf pki/nomad-ca.crt pki/nomad$YEAR.crt pki/nomad$YEAR.key; do
test -f secrets/$SECRET && (cat secrets/$SECRET | ssh -F ssh_config $SSH_DEST tee $TMP_PATH/$SECRET > /dev/null)
done
2021-12-30 12:27:39 +00:00
echo "Rebuilding NixOS"
ssh -F ssh_config $SSH_DEST tee $TMP_PATH/deploy.sh > /dev/null <<EOF
set -ex
cd $TMP_PATH
mv configuration.nix node.nix site.nix /etc/nixos
2021-12-30 18:27:32 +00:00
test -f rclone.conf && (mv rclone.conf /root; chmod 600 /root/rclone.conf)
2021-12-30 18:27:32 +00:00
mkdir -p /var/lib/nomad/pki
test -f pki/nomad-ca.crt && mv -v pki/nomad* /var/lib/nomad/pki
nixos-rebuild switch
EOF
ssh -t -F ssh_config $SSH_DEST sudo sh $TMP_PATH/deploy.sh
ssh -F ssh_config $SSH_DEST rm -rv $TMP_PATH
2021-11-02 09:15:37 +00:00
done