populate_chroot: set of scripts to make bento

This commit is contained in:
Solene Rapenne 2022-09-04 02:29:38 +02:00
parent fb038b6b58
commit 01d9952717
2 changed files with 111 additions and 0 deletions

2
config.sh.sample Normal file
View File

@ -0,0 +1,2 @@
CHROOT_DIR=/home/chroot
REMOTE_IP=myserver.example

109
populate_chroot.sh Executable file
View File

@ -0,0 +1,109 @@
#!/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
echo "Copying $i"
# we only want directories
if [ -d "$i" ]
then
# create the script that will check for updates
cat > "$i/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}:/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 > "$i/bootstrap.sh" <<EOF
#!/bin/sh
set -e
install -d -o root -g root -m 700 /var/bento
cd /var/bento
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
if test -f flake.nix
then
nixos-rebuild build --flake .#bento-machine
if [ ! "\${RESULT}" = "\$(readlink -f result)" ]
then
nixos-rebuild switch --flake .#bento-machine
fi
else
nixos-rebuild build --no-flake -I nixos-config=./configuration.nix --upgrade
if [ ! "\${RESULT}" = "\$(readlink -f result)" ]
then
nixos-rebuild switch --no-flake -I nixos-config=./configuration.nix --upgrade
fi
fi
EOF
# to make flakes using caching, we must avoid repositories to change everytime
# we must ignore files that change everytime
cat > "$i/.gitignore" <<EOF
bootstrap.sh
update.sh
.state
result
last_change_date
EOF
# copy files in the chroot
rsync --delete -avL "$i/" "${CHROOT_DIR}/${i}/"
# sftp chroot requires the home directory to be owned by root
install -d -o root -g root -m 755 "${CHROOT_DIR}/${i}"
touch "${CHROOT_DIR}/${i}/last_change_date"
fi
done