2025-09-06 19:03:55 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
2025-10-14 15:58:09 +01:00
|
|
|
# Redirect all output to a log file for reliability
|
|
|
|
|
exec > /tmp/startup.log 2>&1
|
|
|
|
|
|
2025-09-06 19:03:55 +01:00
|
|
|
INFCTL_GIT_REPO="https://codeberg.org/headshed/infctl-cli.git"
|
2025-10-08 13:59:03 +01:00
|
|
|
INFCTL_GIT_REPO_BRANCH="main"
|
2025-09-06 19:03:55 +01:00
|
|
|
INFCTL_INSTALL_DIR="/opt/src"
|
|
|
|
|
|
|
|
|
|
# ensure only run once
|
|
|
|
|
if [[ -f /etc/startup_was_launched ]]; then exit 0; fi
|
|
|
|
|
|
|
|
|
|
touch /etc/startup_was_launched
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Format the k3s disk if not already formatted
|
|
|
|
|
|
|
|
|
|
# This creates an ext4 filesystem on the specified
|
|
|
|
|
# disk with no reserved space for root, forces the operation,
|
|
|
|
|
# fully initializes inode tables and the journal, and enables
|
|
|
|
|
# discard/TRIM for better performance on SSDs or
|
|
|
|
|
# thin-provisioned storage.
|
|
|
|
|
if ! lsblk | grep -q "/var/lib/rancher/k3s"; then
|
|
|
|
|
mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/google-k3s-disk
|
|
|
|
|
mkdir -p /var/lib/rancher/k3s
|
|
|
|
|
mount -o discard,defaults /dev/disk/by-id/google-k3s-disk /var/lib/rancher/k3s
|
|
|
|
|
chmod a+w /var/lib/rancher/k3s
|
|
|
|
|
fi
|
|
|
|
|
# A disk named k3s-disk in your Terraform configuration will
|
|
|
|
|
# appear as /dev/disk/by-id/google-k3s-disk.
|
|
|
|
|
|
|
|
|
|
# Format the app-data-disk if not already formatted
|
|
|
|
|
if ! lsblk | grep -q "/mnt/disks/app-data"; then
|
|
|
|
|
mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/google-app-data-disk
|
|
|
|
|
mkdir -p /mnt/disks/app-data
|
|
|
|
|
mount -o discard,defaults /dev/disk/by-id/google-app-data-disk /mnt/disks/app-data
|
|
|
|
|
chmod a+w /mnt/disks/app-data
|
|
|
|
|
fi
|
|
|
|
|
# Similarly, a disk named app-data-disk will appear as /dev/
|
|
|
|
|
# disk/by-id/google-app-data-disk.
|
|
|
|
|
|
|
|
|
|
# Add to /etc/fstab for persistence (only if not already present)
|
|
|
|
|
if ! grep -q "/var/lib/rancher/k3s" /etc/fstab; then
|
|
|
|
|
echo "/dev/disk/by-id/google-k3s-disk /var/lib/rancher/k3s ext4 defaults,discard 0 0" >> /etc/fstab
|
|
|
|
|
fi
|
|
|
|
|
if ! grep -q "/mnt/disks/app-data" /etc/fstab; then
|
|
|
|
|
echo "/dev/disk/by-id/google-app-data-disk /mnt/disks/app-data ext4 defaults,discard 0 0" >> /etc/fstab
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# apt install
|
|
|
|
|
apt update
|
|
|
|
|
apt install -y ncdu htop git curl
|
|
|
|
|
|
|
|
|
|
# helm install
|
|
|
|
|
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
|
|
|
|
|
chmod 700 get_helm.sh
|
|
|
|
|
/bin/bash get_helm.sh
|
|
|
|
|
|
|
|
|
|
# user bashrc config
|
|
|
|
|
rc=/home/user/.bashrc
|
|
|
|
|
{
|
|
|
|
|
echo "export KUBECONFIG=~/.kube/config"
|
|
|
|
|
echo "alias l='ls -lah'"
|
|
|
|
|
echo "alias ll='ls -lh'"
|
|
|
|
|
echo "alias k=kubectl"
|
|
|
|
|
echo "export dry='--dry-run=client'"
|
|
|
|
|
echo "export o='-oyaml'"
|
|
|
|
|
echo "alias kcd='kubectl config use-context'"
|
|
|
|
|
echo "source <(kubectl completion bash)"
|
|
|
|
|
echo "complete -F __start_kubectl k"
|
|
|
|
|
echo "alias k='kubectl'"
|
|
|
|
|
} >> $rc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Install k3s
|
|
|
|
|
k3s_version="v1.32.8+k3s1"
|
|
|
|
|
curl -sfL https://get.k3s.io \
|
|
|
|
|
| \
|
|
|
|
|
INSTALL_K3S_VERSION="$k3s_version" sh -s - server \
|
|
|
|
|
--cluster-init \
|
|
|
|
|
--disable traefik \
|
|
|
|
|
--disable servicelb
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Set up kubeconfig for the 'user' user
|
|
|
|
|
mkdir -p /home/user/.kube
|
|
|
|
|
chown user:user /home/user/.kube
|
|
|
|
|
chmod 700 /home/user/.kube
|
|
|
|
|
# Copy the kubeconfig file to the user's home directory
|
|
|
|
|
# for easier access
|
|
|
|
|
cp /etc/rancher/k3s/k3s.yaml /home/user/.kube/config
|
|
|
|
|
chown user:user /home/user/.kube/config
|
|
|
|
|
|
|
|
|
|
# install infctl
|
|
|
|
|
curl -L https://codeberg.org/headshed/infctl-cli/raw/branch/main/install.sh | bash
|
|
|
|
|
|
|
|
|
|
# clone infctl repo if not already present
|
|
|
|
|
if [[ ! -d "$INFCTL_INSTALL_DIR" ]]; then
|
|
|
|
|
mkdir -p "$INFCTL_INSTALL_DIR"
|
|
|
|
|
cd ${INFCTL_INSTALL_DIR} || "echo 'Failed to change directory to $INFCTL_INSTALL_DIR' ; exit 1"
|
|
|
|
|
git clone --branch "$INFCTL_GIT_REPO_BRANCH" "$INFCTL_GIT_REPO" || "echo 'Failed to clone $INFCTL_GIT_REPO' ; exit 1"
|
|
|
|
|
chown -R user:user "$INFCTL_INSTALL_DIR"
|
|
|
|
|
fi
|
|
|
|
|
|
2025-10-14 15:58:09 +01:00
|
|
|
for i in {1..100}; do
|
|
|
|
|
if [[ -f /opt/src/infctl-cli/.env ]]; then
|
|
|
|
|
echo ".env file found."
|
|
|
|
|
break
|
|
|
|
|
else
|
|
|
|
|
echo ".env file not found. Attempt $i/100. Waiting 5 seconds..."
|
|
|
|
|
sleep 5
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Final check after loop
|
|
|
|
|
if [[ ! -f /opt/src/infctl-cli/.env ]]; then
|
|
|
|
|
echo "ERROR: .env file not found after 10 attempts. Exiting."
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# load .env file
|
|
|
|
|
source /opt/src/infctl-cli/.env
|
|
|
|
|
|
|
|
|
|
# check to see if INSTALL_FORGEJO is set to "true"
|
|
|
|
|
if [[ "$INSTALL_FORGEJO" == "true" ]]; then
|
|
|
|
|
# install forgejo using infctl
|
|
|
|
|
# ....
|
|
|
|
|
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
|
|
|
|
|
LOG_FORMAT=none infctl -f "${INFCTL_INSTALL_DIR}/infctl-cli/gcloud/tf/scripts/install-forgejo-pipeline.json"
|
|
|
|
|
touch /etc/forgejo_was_installed
|
|
|
|
|
|
|
|
|
|
fi
|