bootstrap/bootstrap.sh
2022-12-01 22:05:59 -05:00

123 lines
3.2 KiB
Bash

#!/bin/bash
# Hardcode base URL for your raw repository files.
# Set the value to your own fork, replacing the `authorized_keys` file with your own
# i.e., "https://raw.githubusercontent.com/<GH_USER>/<GH_REPO>/<GH_BRANCH>"
REPO_RAW_URL="https://git.krislamo.org/kris/bootstrap/raw/branch/main"
AUTH_KEY_FILE="/authorized_keys"
# Optional debianzfs install script download
DEBIANZFS="https://git.krislamo.org/kris/debianzfs/raw/branch/main/debianzfs.sh"
DEBIANZFS_BIN="/usr/local/bin/debianzfs"
##############################
######## STOP EDITING ########
##############################
# Root required
if [ $EUID -ne 0 ]; then
echo "You must run this script as root"
exit 1
fi
# Options
while getopts ':bceg:h:i:r:suz' OPTION; do
case "$OPTION" in
b) BOOT_CYCLE=1;;
c) CDROM_REMOVE=1;;
e) ENABLE_SSH=1;;
g) GATEWAY="$OPTARG";;
h) HOST="$OPTARG";;
i) IP="$OPTARG";;
r) REPO="$OPTARG";;
s) SSH=1;;
u) UPDATE=1;;
z) ZFSINSTALL=1;;
?)
usage
exit 1;;
esac
done
# Allow override but use default repo if not set
[ -z "$REPO" ] && REPO="$REPO_RAW_URL"
# Get current date, hostname, create a temporary directory,
DATE=$(date '+%Y%m%d')
CUR_HOSTNAME=$(hostname)
# Remove CD sources
if [ "$CDROM_REMOVE" -eq 1 ]; then
echo "NOTICE: Backing up /etc/apt/sources.list => /etc/apt/sources.list.$DATE"
sed -i."$DATE" '/deb cdrom/d' /etc/apt/sources.list
fi
# Upgrade software
if [ "$UPDATE" -eq 1 ]; then
echo "NOTICE: Upgrading system"
apt-get update
apt-get upgrade -y
fi
# Install personal SSH keys under root and install the OpenSSH server
if [ "$SSH" -eq 1 ]; then
# Does authorized_keys file already exist?
if [ -f /root/.ssh/authorized_keys ]; then
echo "ERROR: /root/.ssh/authorized_keys file already exists"
exit 1
fi
echo "NOTICE: Installing root's authorized_keys and the OpenSSH server"
mkdir -p /root/.ssh/
chmod 700 /root/.ssh/
wget "${REPO}${AUTH_KEY_FILE}" -O /root/.ssh/authorized_keys
chmod 644 /root/.ssh/authorized_keys
apt-get install openssh-server -y
if [ "$ENABLE_SSH" -eq 1 ]; then
echo "NOTICE: Enabling the OpenSSH server"
systemctl start ssh
fi
fi
# If IP is set, backup interfaces and configure static IP
if [ -n "$IP" ]; then
if [ -z "$GATEWAY" ]; then
echo "ERROR: IP set without a GATEWAY address. See option -g"
exit 1
fi
echo "NOTICE: Backing up network interfaces file and installing a new static one"
sed -i."$DATE" "s/dhcp/static/g" /etc/network/interfaces
if ! grep -q "address" /etc/network/interfaces; then
echo " address $IP" >> /etc/network/interfaces
echo " gateway $GATEWAY_IP" >> /etc/network/interfaces
else
echo "ERROR: Address already set"
exit 1
fi
fi
# Download DebianZFS script
if [ "$ZFSINSTALL" -eq 1 ]; then
echo "NOTICE: Installing DebianZFS installation script"
wget "$DEBIANZFS" -O "$DEBIANZFS_BIN"
chmod u+x "$DEBIANZFS_BIN"
fi
# If NEW_HOSTNAME is set, configure new hostname and backup /etc/hosts
if [ -n "$HOST" ]; then
hostnamectl set-hostname "$HOST"
echo "NOTICE: Backing up /etc/hosts and setting new hostname to '$HOST'"
sed -i."$DATE" "s/$CUR_HOSTNAME/$HOST/g" /etc/hosts
fi
# Restart
if [ "$BOOT_CYCLE" -eq 1 ]; then
echo "NOTICE: Restarting the machine in 10 seconds..."
sleep 9
echo "NOTICE: Restarting!"
sleep 1
systemctl reboot
fi