feat(bootstrap): rewrite bootstrap script and docs for standalone deployment
This commit is contained in:
parent
dc9b7a30d9
commit
2b533f4375
2 changed files with 110 additions and 97 deletions
131
BOOTSTRAP.md
131
BOOTSTRAP.md
|
|
@ -1,113 +1,50 @@
|
||||||
# Bootstrap Guide
|
# NixOS GitOps Bootstrap Guide
|
||||||
|
|
||||||
This document outlines the manual steps required to initialize the NixOS GitOps environment on a fresh Proxmox host. You must perform these steps before the CI/CD pipeline or any automated staging environments can function.
|
This repository is designed to be fully automated once bootstrapped, but if you are adapting this codebase for **your own infrastructure**, you must modify several deployment-specific variables before running the bootstrap script on a fresh Proxmox host.
|
||||||
|
|
||||||
## Secret Management Initialization
|
## Adapt the Codebase
|
||||||
|
|
||||||
We use `sops-nix` to manage secrets, separating between Production and Staging.
|
Before bootstrapping your host, fork or clone this repository and make the following changes to match your environment:
|
||||||
|
|
||||||
**Prerequisites:** Install `age` ([age documentation](https://github.com/FiloSottile/age)).
|
|
||||||
|
|
||||||
1. **Generate the Production Master Key**
|
|
||||||
|
|
||||||
```bash
|
|
||||||
age-keygen -o prod-master.age
|
|
||||||
```
|
|
||||||
|
|
||||||
It is recommended to generate this key on a secure offline workstation. The private key should be stored in a secure offline location, such as a USB drive or printed on paper. Avoid storing this private key on any server.
|
|
||||||
|
|
||||||
2. **Generate the Staging Master Key**
|
|
||||||
|
|
||||||
```bash
|
|
||||||
age-keygen -o staging-master.age
|
|
||||||
```
|
|
||||||
|
|
||||||
3. **Update Configuration**
|
|
||||||
|
|
||||||
Replace both placeholders in `.sops.yaml` with the newly generated **public keys**. Commit and push this change.
|
|
||||||
|
|
||||||
## Proxmox Host Initialization & Authentication
|
|
||||||
|
|
||||||
The CI/CD pipeline needs restricted API access to Proxmox to provision Virtual Machines. We use OpenTofu to provision the bare-metal host.
|
|
||||||
|
|
||||||
### Find your Raw Disk ID
|
|
||||||
|
|
||||||
The current OpenTofu configuration assumes that your system has a 2TB disk attached to the Proxmox host. This disk will be formatted and used for VM storage.
|
|
||||||
|
|
||||||
1. Determine the persistent hardware ID of your 2TB disk, using the following command:
|
|
||||||
|
|
||||||
|
### Hardware Identifiers
|
||||||
|
- **Find your NVMe/Disk UUID**: Log into your fresh Proxmox host and run:
|
||||||
```bash
|
```bash
|
||||||
ls -l /dev/disk/by-id/
|
ls -l /dev/disk/by-id/
|
||||||
```
|
```
|
||||||
|
Identify your primary data disk (e.g. `nvme-eui...` or `wwn-0x...`).
|
||||||
|
- **Update OpenTofu Config**: Open `opentofu/nodes/mikoshi/main.tf` (you may want to rename `mikoshi` to your host's name) and replace the `disk` ID inside the `zpool` resource with your hardware UUID.
|
||||||
|
|
||||||
It is recommended to use the persistent hardware ID (e.g., `wwn-0x...`, `nvme-eui...`, or `ata-...`) instead of `/dev/sdb` because the latter can change between reboots.
|
### Identity & Access
|
||||||
|
- **SSH Keys**: The GitOps Control Center needs an SSH key for disaster recovery.
|
||||||
|
- Update the Cloud-Init SSH key in `opentofu/nodes/mikoshi/main.tf` under the `user_account` block.
|
||||||
|
- Update the permanent NixOS SSH key in `nixos/users/admin/default.nix`.
|
||||||
|
- **Secrets (SOPS)**: Replace the placeholder tokens in the Control Center host config (e.g., `nixos/hosts/izanagi/secrets.yaml` if you haven't renamed it) with your actual Proxmox API token and Forgejo token. Encrypt this file with your own `sops` Age key.
|
||||||
|
|
||||||
2. **Action Required:** Provide this ID to the system or update the `opentofu/nodes/<hostname>/main.tf` configuration with this ID.
|
### Hostname & Naming Schema
|
||||||
|
If your Proxmox host or your Control Center has a different name:
|
||||||
|
- Rename the folders in `opentofu/nodes/` and `nixos/hosts/`.
|
||||||
|
- Update the `node_name` inside your OpenTofu `main.tf`.
|
||||||
|
- Update `nixos/flake.nix` to reflect your new host names.
|
||||||
|
|
||||||
### Apply the OpenTofu Host State
|
## Execute the Bootstrap
|
||||||
|
|
||||||
**Prerequisites:** Install `opentofu` ([OpenTofu installation](https://opentofu.org/docs/intro/install/)).
|
Once you have pushed your adapted codebase to your Git server, SSH into your fresh Proxmox node as `root` and run the bootstrap script:
|
||||||
|
|
||||||
1. Initialize and apply the state. You will be prompted to enter your `root@pam` Proxmox credentials.
|
```bash
|
||||||
|
curl -fsSL https://git.your-server.com/your-repo/raw/branch/main/scripts/bootstrap.sh | bash
|
||||||
|
```
|
||||||
|
|
||||||
```bash
|
### What this script does automatically:
|
||||||
# Navigate to the OpenTofu host directory
|
- **Fixes APT Repositories**: Disables enterprise repositories and adds community repositories.
|
||||||
cd opentofu/host
|
- **Fixes NIC Offloading**: Installs a systemd service to safely disable TSO/GSO/GRO on physical interfaces to prevent network drops.
|
||||||
# Initialize and apply the OpenTofu configuration
|
- **Installs OpenTofu**: Pulls the official binaries.
|
||||||
tofu init
|
- **Applies Host State**: Runs `tofu apply` which:
|
||||||
tofu apply
|
- Formats your specified disk into the `data` ZFS pool.
|
||||||
```
|
- Sets laptop lid switch to ignore (if applicable).
|
||||||
|
- Spins up the GitOps Control Center VM.
|
||||||
|
|
||||||
2. Upon successful completion, OpenTofu will output a secure **API Token**. Copy this token securely.
|
## Post-Bootstrap
|
||||||
|
|
||||||
## Forgejo Secrets Configuration
|
Once the bootstrap script completes, the Control Center VM will boot, initialize via Cloud-Init, and automatically start pulling this git repository.
|
||||||
|
|
||||||
The CI/CD actions require access to the Proxmox token and the staging secret key.
|
From this point on, **you no longer need to log into the Proxmox host.** All future changes to VMs, networks, or applications should be done declaratively via Pull Requests to your repository!
|
||||||
|
|
||||||
1. Navigate to your Forgejo Web UI.
|
|
||||||
2. Go to **Settings > Actions > Secrets** for this repository.
|
|
||||||
3. Add the following repository secrets:
|
|
||||||
* `PROXMOX_TOKEN_SECRET`: Paste the token generated from Step 2.
|
|
||||||
* `RENOVATE_TOKEN`: Create a Personal Access Token (PAT) for your user in Forgejo with read/write access to code and pull requests, and paste it here.
|
|
||||||
|
|
||||||
## Staging Golden Key Provisioning (Proxmox Snippet)
|
|
||||||
|
|
||||||
Instead of relying on Forgejo CI/CD to store the staging private key, we use a secure hypervisor-level Cloud-Init snippet.
|
|
||||||
|
|
||||||
1. SSH into your Proxmox server (`pve`).
|
|
||||||
2. Create the Cloud-Init snippet file:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
cat << 'EOF' > /var/lib/vz/snippets/staging-key.yaml
|
|
||||||
#cloud-config
|
|
||||||
write_files:
|
|
||||||
- path: /var/lib/sops-nix/key.txt
|
|
||||||
permissions: '0600'
|
|
||||||
content: |
|
|
||||||
AGE-SECRET-KEY-1... (paste your staging-master private key here)
|
|
||||||
runcmd:
|
|
||||||
- echo "Staging age key injected successfully."
|
|
||||||
EOF
|
|
||||||
```
|
|
||||||
|
|
||||||
3. This completely removes the secret from Forgejo. When OpenTofu spins up a staging VM, it simply tells Proxmox to attach this local snippet!
|
|
||||||
|
|
||||||
## TrueNAS API Security (RBAC)
|
|
||||||
|
|
||||||
To prevent the CI/CD pipeline from having `root` access to your TrueNAS server, you must run the RBAC bootstrap script to create a restricted user (`forgejo-ci`) that can *only* clone datasets for staging, not destroy production data.
|
|
||||||
|
|
||||||
1. Ensure you have network access to your TrueNAS host.
|
|
||||||
2. Execute the RBAC setup script:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
./scripts/truenas-rbac-setup.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
3. Provide your TrueNAS IP and the `root` Admin API Token when prompted.
|
|
||||||
4. The script will automatically create the custom `ci-runner-role` and the `forgejo-ci` user.
|
|
||||||
5. Follow the terminal output instructions to log into the TrueNAS Web UI as the new user and generate the restricted API token.
|
|
||||||
6. Use this restricted token for the `TRUENAS_API_KEY` secret in Forgejo.
|
|
||||||
|
|
||||||
## Next Steps
|
|
||||||
|
|
||||||
Once these bootstrap steps are complete, the foundational authentication is in place. The Forgejo CI actions will now have the necessary permissions to build images, provision VMs, and test staging environments autonomously and securely.
|
|
||||||
|
|
|
||||||
76
scripts/bootstrap.sh
Normal file
76
scripts/bootstrap.sh
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
BANNER="==========================================================="
|
||||||
|
printf "%s\n Proxmox Bootstrap\n%s\n\n" "$BANNER" "$BANNER"
|
||||||
|
|
||||||
|
echo "Applying post-pve-install fixes (fixing repos)..."
|
||||||
|
# Remove enterprise repos and add non-subscription repos safely
|
||||||
|
rm -f /etc/apt/sources.list.d/pve-enterprise.list
|
||||||
|
|
||||||
|
echo "deb http://download.proxmox.com/debian/pve bookworm pve-no-subscription" > /etc/apt/sources.list.d/pve-no-subscription.list
|
||||||
|
|
||||||
|
# Disable the "No Valid Subscription" nag screen
|
||||||
|
sed -i.bak "s/data.status !== 'Active'/false/g" /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Applying NIC offloading fixes..."
|
||||||
|
# The community script disables TSO, GSO, and GRO on physical interfaces.
|
||||||
|
# We create a systemd service to ensure this applies on boot.
|
||||||
|
cat <<'EOF' > /etc/systemd/system/nic-offload-fix.service
|
||||||
|
[Unit]
|
||||||
|
Description=Disable NIC offloading (TSO/GRO/GSO) for physical interfaces
|
||||||
|
After=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=oneshot
|
||||||
|
# Iterate over all physical interfaces (excluding lo, bridges, veth, etc.)
|
||||||
|
ExecStart=/bin/bash -c 'for dev in /sys/class/net/*; do if [ "$(basename "$dev")" != "lo" ] && [[ ! "$(basename "$dev")" =~ ^(vmbr|veth|fwbr|tap|bonding) ]]; then /usr/sbin/ethtool -K "$(basename "$dev")" tso off gso off gro off || true; fi; done'
|
||||||
|
RemainAfterExit=yes
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
EOF
|
||||||
|
systemctl enable --now nic-offload-fix.service || true
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Updating system and installing OpenTofu..."
|
||||||
|
apt-get update
|
||||||
|
# Install curl, git, gnupg, ethtool, and required apt dependencies
|
||||||
|
apt-get install -y apt-transport-https ca-certificates curl git gnupg ethtool
|
||||||
|
|
||||||
|
# Install OpenTofu repository and binary
|
||||||
|
install -m 0755 -d /etc/apt/keyrings
|
||||||
|
curl -fsSL https://get.opentofu.org/opentofu.gpg | tee /etc/apt/keyrings/opentofu.gpg >/dev/null
|
||||||
|
curl -fsSL https://packages.opentofu.org/opentofu/tofu/gpgkey | gpg --no-tty --batch --dearmor -o /etc/apt/keyrings/opentofu-repo.gpg >/dev/null
|
||||||
|
chmod a+r /etc/apt/keyrings/opentofu.gpg /etc/apt/keyrings/opentofu-repo.gpg
|
||||||
|
|
||||||
|
printf "deb [signed-by=/etc/apt/keyrings/opentofu.gpg,/etc/apt/keyrings/opentofu-repo.gpg] https://packages.opentofu.org/opentofu/tofu/any/ any main\ndeb-src [signed-by=/etc/apt/keyrings/opentofu.gpg,/etc/apt/keyrings/opentofu-repo.gpg] https://packages.opentofu.org/opentofu/tofu/any/ any main\n" > /etc/apt/sources.list.d/opentofu.list
|
||||||
|
chmod a+r /etc/apt/sources.list.d/opentofu.list
|
||||||
|
|
||||||
|
apt-get update
|
||||||
|
apt-get install -y tofu
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Cloning the infrastructure repository..."
|
||||||
|
cd /root
|
||||||
|
if [ -d "nix-config" ]; then
|
||||||
|
echo "Repository already exists. Pulling latest..."
|
||||||
|
cd nix-config
|
||||||
|
git pull
|
||||||
|
else
|
||||||
|
git clone https://git.depeuter.dev/Bos55/nix-config.git
|
||||||
|
cd nix-config
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Bootstrapping Proxmox Host State..."
|
||||||
|
cd opentofu/nodes/mikoshi
|
||||||
|
|
||||||
|
echo "Initializing OpenTofu..."
|
||||||
|
tofu init -upgrade
|
||||||
|
|
||||||
|
echo "Applying bare-metal state..."
|
||||||
|
tofu apply -auto-approve
|
||||||
|
|
||||||
|
printf "\n%s\n Bootstrap Complete!\n The ZFS pool, Resource Pools, and the Control Center\n VM have been provisioned.\n\n The Control Center is booting now. Once online, it will automatically\n pull this repository and provision the rest of your VMs!\n%s\n" "$BANNER" "$BANNER"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue