Skip to content

Commit 5da1b41

Browse files
committed
Add common base box support and update documentation
1 parent 3a534a6 commit 5da1b41

10 files changed

+174
-20
lines changed

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,19 @@ vagrant box add generic/debian12 --provider=libvirt
4444

4545
### Build Base Box with Packer
4646

47-
Choose which Kubernetes distribution you want to use:
47+
First, build the common base box:
48+
49+
```bash
50+
# Build the common base box
51+
./build-base-box.sh common
52+
```
53+
54+
This command will:
55+
1. Download the Debian 12 base box
56+
2. Install system updates and required packages
57+
3. Package the resulting VM into a new Vagrant box called "common-base"
58+
59+
Then, choose which Kubernetes distribution you want to use:
4860

4961
```bash
5062
# For standard Kubernetes (k8s)
@@ -62,11 +74,10 @@ Choose which Kubernetes distribution you want to use:
6274

6375
This command will:
6476

65-
1. Download the Debian 12 base box
66-
2. Install system updates and required packages
67-
3. Install containerd container runtime
68-
4. Install the selected Kubernetes distribution
69-
5. Package the resulting VM into a new Vagrant box
77+
1. Use the common base box as a starting point
78+
2. Install containerd container runtime
79+
3. Install the selected Kubernetes distribution
80+
4. Package the resulting VM into a new Vagrant box
7081

7182
### Create the Virtual Machines and Provision the Kubernetes Cluster
7283

@@ -137,6 +148,10 @@ When you're done experimenting, you can destroy all VMs:
137148
### Remove Base Boxes
138149

139150
```bash
151+
# Remove common base box
152+
vagrant box remove common-base
153+
rm -rf output-common-base
154+
140155
# For standard Kubernetes (k8s)
141156
vagrant box remove k8s-base
142157
rm -rf output-k8s-base

build-base-box.sh

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,46 @@ set -e # Exit immediately if a command exits with non-zero status
44
# Check if distribution is provided
55
if [ -z "$1" ]; then
66
echo "Usage: $0 <distribution> [version]"
7-
echo " distribution: k8s, k3s, or k0s"
8-
echo " version: Optional version override (default: uses version in packer config)"
7+
echo " distribution: common, k8s, k3s, or k0s"
8+
echo " version: Optional version override for k8s/k3s/k0s (default: uses version in packer config)"
99
exit 1
1010
fi
1111

1212
DISTRIBUTION=$1
1313
VERSION=$2
1414

1515
# Validate distribution
16-
if [[ ! "$DISTRIBUTION" =~ ^(k8s|k3s|k0s)$ ]]; then
17-
echo "Error: Distribution must be k8s, k3s, or k0s"
16+
if [[ ! "$DISTRIBUTION" =~ ^(common|k8s|k3s|k0s)$ ]]; then
17+
echo "Error: Distribution must be common, k8s, k3s, or k0s"
1818
exit 1
1919
fi
2020

21+
# Check if building a distribution-specific box and common box exists
22+
if [[ "$DISTRIBUTION" != "common" ]]; then
23+
# Check if common-base box exists
24+
if ! vagrant box list | grep -q "common-base"; then
25+
echo "Error: common-base box not found. Please build it first with: $0 common"
26+
exit 1
27+
fi
28+
fi
29+
2130
echo "Building $DISTRIBUTION base box..."
2231

2332
# Init packer
24-
packer init "packer/$DISTRIBUTION-base-box.pkr.hcl"
25-
26-
# Build base box with optional version override
27-
if [ -z "$VERSION" ]; then
28-
packer build -force "packer/$DISTRIBUTION-base-box.pkr.hcl"
33+
if [[ "$DISTRIBUTION" == "common" ]]; then
34+
packer init "packer/common-base-box.pkr.hcl"
35+
36+
# Build common base box (no version parameter needed)
37+
packer build -force "packer/common-base-box.pkr.hcl"
2938
else
30-
packer build -force -var "version=$VERSION" "packer/$DISTRIBUTION-base-box.pkr.hcl"
39+
packer init "packer/$DISTRIBUTION-base-box.pkr.hcl"
40+
41+
# Build distribution-specific base box with optional version override
42+
if [ -z "$VERSION" ]; then
43+
packer build -force "packer/$DISTRIBUTION-base-box.pkr.hcl"
44+
else
45+
packer build -force -var "version=$VERSION" "packer/$DISTRIBUTION-base-box.pkr.hcl"
46+
fi
3147
fi
3248

3349
echo "$DISTRIBUTION base box built successfully!"

common/common-base-playbook.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- hosts: all
3+
become: true
4+
tasks:
5+
- name: "Update, upgrade, and restart instance"
6+
ansible.builtin.include_tasks: "{{ playbook_dir }}/includes/base-update-upgrade-reboot.yml"

generate-packer-configs.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,21 @@ generate_packer_config() {
4747
# Replace placeholders in template
4848
sed -e "s/{{ distribution }}/$dist/g" \
4949
-e "s/{{ version }}/$version/g" \
50-
packer/base-box.template.pkr.hcl > "packer/$dist-base-box.pkr.hcl"
50+
packer/distribution-base-box.template.pkr.hcl > "packer/$dist-base-box.pkr.hcl"
5151

5252
echo "Generated packer/$dist-base-box.pkr.hcl"
5353
}
5454

55+
# Function to generate common base box configuration
56+
generate_common_base_config() {
57+
echo "Generating Packer configuration for common base box..."
58+
59+
# Copy the template directly (no replacements needed)
60+
cp packer/common-base-box.template.pkr.hcl packer/common-base-box.pkr.hcl
61+
62+
echo "Generated packer/common-base-box.pkr.hcl"
63+
}
64+
5565
# Get latest stable versions for each distribution
5666
K8S_VERSION=$(fetch_latest_k8s_version)
5767
K3S_VERSION=$(fetch_latest_k3s_version)
@@ -62,6 +72,9 @@ echo "- k8s: $K8S_VERSION"
6272
echo "- k3s: $K3S_VERSION"
6373
echo "- k0s: $K0S_VERSION"
6474

75+
# Generate common base box configuration
76+
generate_common_base_config
77+
6578
# Generate Packer configurations for each distribution
6679
generate_packer_config "k8s" "$K8S_VERSION"
6780
generate_packer_config "k3s" "$K3S_VERSION"

packer/common-base-box.pkr.hcl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
packer {
2+
required_plugins {
3+
vagrant = {
4+
version = "~> 1"
5+
source = "github.com/hashicorp/vagrant"
6+
}
7+
ansible = {
8+
version = "~> 1"
9+
source = "github.com/hashicorp/ansible"
10+
}
11+
}
12+
}
13+
14+
source "vagrant" "common-base" {
15+
provider = "libvirt"
16+
communicator = "ssh"
17+
source_path = "generic/debian12"
18+
add_force = true
19+
box_name = "common-base"
20+
}
21+
22+
build {
23+
sources = ["source.vagrant.common-base"]
24+
25+
provisioner "ansible" {
26+
playbook_file = "./common/common-base-playbook.yml"
27+
user = "vagrant"
28+
use_proxy = false
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
packer {
2+
required_plugins {
3+
vagrant = {
4+
version = "~> 1"
5+
source = "github.com/hashicorp/vagrant"
6+
}
7+
ansible = {
8+
version = "~> 1"
9+
source = "github.com/hashicorp/ansible"
10+
}
11+
}
12+
}
13+
14+
source "vagrant" "common-base" {
15+
provider = "libvirt"
16+
communicator = "ssh"
17+
source_path = "generic/debian12"
18+
add_force = true
19+
box_name = "common-base"
20+
}
21+
22+
build {
23+
sources = ["source.vagrant.common-base"]
24+
25+
provisioner "ansible" {
26+
playbook_file = "./common/common-base-playbook.yml"
27+
user = "vagrant"
28+
use_proxy = false
29+
}
30+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
packer {
2+
required_plugins {
3+
vagrant = {
4+
version = "~> 1"
5+
source = "github.com/hashicorp/vagrant"
6+
}
7+
ansible = {
8+
version = "~> 1"
9+
source = "github.com/hashicorp/ansible"
10+
}
11+
}
12+
}
13+
14+
variable "distribution" {
15+
type = string
16+
default = "{{ distribution }}"
17+
}
18+
19+
variable "version" {
20+
type = string
21+
default = "{{ version }}"
22+
}
23+
24+
source "vagrant" "{{ distribution }}-base" {
25+
provider = "libvirt"
26+
communicator = "ssh"
27+
source_path = "common-base" # Use common-base box instead of generic/debian12
28+
add_force = true
29+
box_name = "{{ distribution }}-base"
30+
}
31+
32+
build {
33+
sources = ["source.vagrant.{{ distribution }}-base"]
34+
35+
provisioner "ansible" {
36+
playbook_file = "./{{ distribution }}/base-playbook.yml"
37+
user = "vagrant"
38+
use_proxy = false
39+
40+
extra_arguments = [
41+
"--extra-vars", "{{ distribution }}_version=${var.version}"
42+
]
43+
}
44+
}

packer/k0s-base-box.pkr.hcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ variable "version" {
2424
source "vagrant" "k0s-base" {
2525
provider = "libvirt"
2626
communicator = "ssh"
27-
source_path = "generic/debian12"
27+
source_path = "common-base" # Use common-base box instead of generic/debian12
2828
add_force = true
2929
box_name = "k0s-base"
3030
}

packer/k3s-base-box.pkr.hcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ variable "version" {
2424
source "vagrant" "k3s-base" {
2525
provider = "libvirt"
2626
communicator = "ssh"
27-
source_path = "generic/debian12"
27+
source_path = "common-base" # Use common-base box instead of generic/debian12
2828
add_force = true
2929
box_name = "k3s-base"
3030
}

packer/k8s-base-box.pkr.hcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ variable "version" {
2424
source "vagrant" "k8s-base" {
2525
provider = "libvirt"
2626
communicator = "ssh"
27-
source_path = "generic/debian12"
27+
source_path = "common-base" # Use common-base box instead of generic/debian12
2828
add_force = true
2929
box_name = "k8s-base"
3030
}

0 commit comments

Comments
 (0)