bento/populate_chroot.sh

160 lines
4.3 KiB
Bash
Raw Normal View History

#!/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
printf "Copying $i: "
# we only want directories
if [ -d "$i" ]
then
STAGING_DIR="$(mktemp -d /tmp/bento-staging-dispatch.XXXXXXXXXXXXXX)"
# 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"
# copy files in the chroot
rsync --delete -rltgoDL "$i/" "${STAGING_DIR}/${i}/config/"
# create the script that will check for updates
cat > "${STAGING_DIR}/${i}/config/update.sh" <<EOF
#!/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"
sftp ${i}@${REMOTE_IP}:/config/bootstrap.sh .
/bin/sh bootstrap.sh
echo "\$STATE" > /var/bento/.state
fi
EOF
# 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
#!/bin/sh
set -e
# accept the remote ssh fingerprint if not already known
ssh-keygen -F "${REMOTE_IP}" >/dev/null || ssh-keyscan "${REMOTE_IP}" >> /root/.ssh/known_hosts
install -d -o root -g root -m 700 /var/bento
cd /var/bento
printf "%s\n" "cd config" "get -R ." | sftp -r ${i}@${REMOTE_IP}:
# 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
LOGFILE=\$(mktemp /tmp/build-log.XXXXXXXXXXXXXXXXXXXX)
SUCCESS=2
if test -f flake.nix
then
nixos-rebuild build --flake .#bento-machine
if [ ! "\${RESULT}" = "\$(readlink -f result)" ]
then
nixos-rebuild switch --flake .#bento-machine 2>&1 | tee \$LOGFILE
SUCCESS=\$?
else
SUCCESS=nothing
fi
else
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
nixos-rebuild build --no-flake --upgrade 2>&1 | tee \$LOGFILE
if [ ! "\${RESULT}" = "\$(readlink -f result)" ]
then
nixos-rebuild switch --no-flake --upgrade 2>&1 | tee -a \$LOGFILE
SUCCESS=\$?
else
SUCCESS=nothing
fi
fi
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
EOF
# 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
bootstrap.sh
update.sh
.state
result
last_change_date
EOF
# 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}"
fi
done