2022-09-04 00:29:38 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
if test -f config.sh
|
|
|
|
then
|
|
|
|
. ./config.sh
|
|
|
|
else
|
|
|
|
echo "You are missing config.sh"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$(id -u)" -ne 0 ]
|
|
|
|
then
|
|
|
|
echo "you need to be root to run this script"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
cd hosts
|
|
|
|
# load all hosts or the one defined in environment variable NAME
|
|
|
|
if [ -z "$NAME" ]
|
|
|
|
then
|
|
|
|
NAME=*
|
|
|
|
fi
|
|
|
|
|
|
|
|
for i in $NAME
|
|
|
|
do
|
2022-09-05 16:38:13 +00:00
|
|
|
printf "Copying $i: "
|
2022-09-04 00:29:38 +00:00
|
|
|
|
|
|
|
# we only want directories
|
|
|
|
if [ -d "$i" ]
|
|
|
|
then
|
|
|
|
|
2022-09-05 16:38:13 +00:00
|
|
|
STAGING_DIR="$(mktemp -d /tmp/bento-staging-dispatch.XXXXXXXXXXXXXX)"
|
2022-09-04 17:06:33 +00:00
|
|
|
|
2022-09-05 16:38:13 +00:00
|
|
|
# sftp chroot requires the home directory to be owned by root
|
|
|
|
install -d -o root -g sftp_users -m 755 "${STAGING_DIR}"
|
|
|
|
install -d -o root -g sftp_users -m 755 "${STAGING_DIR}/${i}"
|
|
|
|
install -d -o root -g sftp_users -m 755 "${STAGING_DIR}/${i}/config"
|
|
|
|
install -d -o ${i} -g sftp_users -m 755 "${STAGING_DIR}/${i}/logs"
|
2022-09-04 17:06:33 +00:00
|
|
|
|
2022-09-05 16:38:13 +00:00
|
|
|
# copy files in the chroot
|
|
|
|
rsync --delete -rltgoDL "$i/" "${STAGING_DIR}/${i}/config/"
|
2022-09-04 17:06:33 +00:00
|
|
|
|
2022-09-05 16:38:13 +00:00
|
|
|
# create the script that will check for updates
|
|
|
|
cat > "${STAGING_DIR}/${i}/config/update.sh" <<EOF
|
2022-09-04 00:29:38 +00:00
|
|
|
#!/bin/sh
|
|
|
|
set -e
|
|
|
|
|
|
|
|
install -d -o root -g root -m 700 /var/bento
|
|
|
|
cd /var/bento
|
|
|
|
touch .state
|
|
|
|
|
|
|
|
STATE="\$(echo "ls -l last_change_date" | sftp ${i}@${REMOTE_IP})"
|
|
|
|
CURRENT_STATE="\$(cat /var/bento/.state)"
|
|
|
|
|
|
|
|
if [ "\$STATE" = "\$CURRENT_STATE" ]
|
|
|
|
then
|
|
|
|
echo "no update required"
|
|
|
|
else
|
|
|
|
echo "update required"
|
2022-09-04 17:06:33 +00:00
|
|
|
sftp ${i}@${REMOTE_IP}:/config/bootstrap.sh .
|
2022-09-04 00:29:38 +00:00
|
|
|
/bin/sh bootstrap.sh
|
|
|
|
echo "\$STATE" > /var/bento/.state
|
|
|
|
fi
|
|
|
|
EOF
|
|
|
|
|
2022-09-05 16:38:13 +00:00
|
|
|
# script used to download changes and rebuild
|
|
|
|
# also used to run it manually the first time to configure the system
|
|
|
|
cat > "${STAGING_DIR}/${i}/config/bootstrap.sh" <<EOF
|
2022-09-04 00:29:38 +00:00
|
|
|
#!/bin/sh
|
|
|
|
set -e
|
|
|
|
|
2022-09-04 09:07:38 +00:00
|
|
|
# accept the remote ssh fingerprint if not already known
|
2022-09-05 16:38:13 +00:00
|
|
|
ssh-keygen -F "${REMOTE_IP}" >/dev/null || ssh-keyscan "${REMOTE_IP}" >> /root/.ssh/known_hosts
|
2022-09-04 09:07:38 +00:00
|
|
|
|
2022-09-04 00:29:38 +00:00
|
|
|
install -d -o root -g root -m 700 /var/bento
|
|
|
|
cd /var/bento
|
|
|
|
|
2022-09-04 17:06:33 +00:00
|
|
|
printf "%s\n" "cd config" "get -R ." | sftp -r ${i}@${REMOTE_IP}:
|
2022-09-04 00:29:38 +00:00
|
|
|
|
|
|
|
# for flakes
|
|
|
|
test -d .git || git init
|
|
|
|
git add .
|
|
|
|
|
|
|
|
# check the current build if it exists
|
|
|
|
if test -L result
|
|
|
|
then
|
|
|
|
RESULT="\$(readlink -f result)"
|
|
|
|
fi
|
|
|
|
|
2022-09-04 17:06:33 +00:00
|
|
|
LOGFILE=\$(mktemp /tmp/build-log.XXXXXXXXXXXXXXXXXXXX)
|
|
|
|
|
|
|
|
SUCCESS=2
|
2022-09-04 00:29:38 +00:00
|
|
|
if test -f flake.nix
|
|
|
|
then
|
|
|
|
nixos-rebuild build --flake .#bento-machine
|
|
|
|
if [ ! "\${RESULT}" = "\$(readlink -f result)" ]
|
|
|
|
then
|
2022-09-04 17:06:33 +00:00
|
|
|
nixos-rebuild switch --flake .#bento-machine 2>&1 | tee \$LOGFILE
|
|
|
|
SUCCESS=\$?
|
|
|
|
else
|
|
|
|
SUCCESS=nothing
|
2022-09-04 00:29:38 +00:00
|
|
|
fi
|
|
|
|
else
|
2022-09-04 09:55:36 +00:00
|
|
|
export NIX_PATH=/root/.nix-defexpr/channels:nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/var/bento/configuration.nix:/nix/var/nix/profiles/per-user/root/channels
|
2022-09-04 17:06:33 +00:00
|
|
|
nixos-rebuild build --no-flake --upgrade 2>&1 | tee \$LOGFILE
|
2022-09-04 00:29:38 +00:00
|
|
|
if [ ! "\${RESULT}" = "\$(readlink -f result)" ]
|
|
|
|
then
|
2022-09-04 17:06:33 +00:00
|
|
|
nixos-rebuild switch --no-flake --upgrade 2>&1 | tee -a \$LOGFILE
|
|
|
|
SUCCESS=\$?
|
|
|
|
else
|
|
|
|
SUCCESS=nothing
|
2022-09-04 00:29:38 +00:00
|
|
|
fi
|
|
|
|
fi
|
2022-09-04 17:06:33 +00:00
|
|
|
|
|
|
|
gzip -9 \$LOGFILE
|
|
|
|
#mv \$LOGFILE \$LOGFILE.gz
|
|
|
|
if [ ! "\$SUCCESS" = "nothing" ]
|
|
|
|
then
|
|
|
|
if [ "\$SUCCESS" -eq 0 ]
|
|
|
|
then
|
|
|
|
echo "put \${LOGFILE}.gz /logs/\$(date +%Y%m%d-%H%M)-success.log.gz" | sftp ${i}@${REMOTE_IP}:
|
|
|
|
else
|
|
|
|
echo "put \${LOGFILE}.gz /logs/\$(date +%Y%m%d-%H%M)-failure.log.gz" | sftp ${i}@${REMOTE_IP}:
|
|
|
|
fi
|
|
|
|
fi
|
2022-09-04 00:29:38 +00:00
|
|
|
EOF
|
|
|
|
|
2022-09-05 16:38:13 +00:00
|
|
|
# to make flakes using caching, we must avoid repositories to change everytime
|
|
|
|
# we must ignore files that change everytime
|
|
|
|
cat > "${STAGING_DIR}/${i}/config/.gitignore" <<EOF
|
2022-09-04 00:29:38 +00:00
|
|
|
bootstrap.sh
|
|
|
|
update.sh
|
|
|
|
.state
|
|
|
|
result
|
|
|
|
last_change_date
|
|
|
|
EOF
|
2022-09-05 16:38:13 +00:00
|
|
|
|
|
|
|
# only distribute changes if they changed
|
|
|
|
# this avoids bumping the time and trigger a rebuild for nothing
|
|
|
|
diff -r "${STAGING_DIR}/${i}/config/" "${CHROOT_DIR}/${i}/config/" >/dev/null
|
|
|
|
CHANGES=$?
|
|
|
|
|
|
|
|
if [ "$CHANGES" -ne 0 ]
|
|
|
|
then
|
|
|
|
echo " update"
|
|
|
|
# copy files in the chroot
|
|
|
|
install -d -o root -g sftp_users -m 755 "${CHROOT_DIR}"
|
|
|
|
install -d -o root -g sftp_users -m 755 "${CHROOT_DIR}/${i}"
|
|
|
|
install -d -o root -g sftp_users -m 755 "${CHROOT_DIR}/${i}/config"
|
|
|
|
install -d -o ${i} -g sftp_users -m 755 "${CHROOT_DIR}/${i}/logs"
|
|
|
|
rsync --delete -rltgoDvL "${STAGING_DIR}/${i}/config/" "${CHROOT_DIR}/${i}/config/"
|
|
|
|
touch "${CHROOT_DIR}/${i}/last_change_date"
|
|
|
|
else
|
|
|
|
echo " no changes"
|
|
|
|
fi
|
|
|
|
|
|
|
|
rm -fr "${STAGING_DIR}"
|
2022-09-04 00:29:38 +00:00
|
|
|
fi
|
|
|
|
done
|