Skip to content

Commit 41a4f14

Browse files
committed
preserve AMI build process
1 parent ea0abae commit 41a4f14

File tree

5 files changed

+415
-23
lines changed

5 files changed

+415
-23
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
UPSTREAM_NIX_GIT_SHA := $(shell git rev-parse origin/release/15.6)
33
GIT_SHA := $(shell git describe --tags --always --dirty)
44

5-
init: amazon-arm64-nix.pkr.hcl
6-
packer init amazon-arm64-nix.pkr.hcl
5+
init: qemu-arm64-nix.pkr.hcl
6+
packer init qemu-arm64-nix.pkr.hcl
77

8-
output-cloudimg/packer-cloudimg: ansible amazon-arm64-nix.pkr.hcl
9-
packer build -var "git_sha=$(UPSTREAM_NIX_GIT_SHA)" amazon-arm64-nix.pkr.hcl
8+
output-cloudimg/packer-cloudimg: ansible qemu-arm64-nix.pkr.hcl
9+
packer build -var "git_sha=$(UPSTREAM_NIX_GIT_SHA)" qemu-arm64-nix.pkr.hcl
1010

1111
disk/focal-raw.img: output-cloudimg/packer-cloudimg
1212
mkdir -p disk
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This script creates filesystem and setups up chrooted
4+
# enviroment for further processing. It also runs
5+
# ansible playbook and finally does system cleanup.
6+
#
7+
# Adapted from: https://github.com/jen20/packer-ubuntu-zfs
8+
9+
set -o errexit
10+
set -o pipefail
11+
set -o xtrace
12+
13+
if [ $(dpkg --print-architecture) = "amd64" ];
14+
then
15+
ARCH="amd64";
16+
else
17+
ARCH="arm64";
18+
fi
19+
20+
function waitfor_boot_finished {
21+
export DEBIAN_FRONTEND=noninteractive
22+
23+
echo "args: ${ARGS}"
24+
# Wait for cloudinit on the surrogate to complete before making progress
25+
while [[ ! -f /var/lib/cloud/instance/boot-finished ]]; do
26+
echo 'Waiting for cloud-init...'
27+
sleep 1
28+
done
29+
}
30+
31+
function install_packages {
32+
apt-get update && sudo apt-get install software-properties-common e2fsprogs -y
33+
add-apt-repository --yes --update ppa:ansible/ansible && sudo apt-get install ansible -y
34+
ansible-galaxy collection install community.general
35+
}
36+
37+
function execute_playbook {
38+
39+
tee /etc/ansible/ansible.cfg <<EOF
40+
[defaults]
41+
callbacks_enabled = timer, profile_tasks, profile_roles
42+
EOF
43+
# Run Ansible playbook
44+
#export ANSIBLE_LOG_PATH=/tmp/ansible.log && export ANSIBLE_DEBUG=True && export ANSIBLE_REMOTE_TEMP=/mnt/tmp
45+
export ANSIBLE_LOG_PATH=/tmp/ansible.log && export ANSIBLE_REMOTE_TEMP=/mnt/tmp
46+
ansible-playbook ./ansible/playbook.yml --extra-vars '{"nixpkg_mode": true, "debpkg_mode": false, "stage2_nix": false}' # $ARGS - I think this is being not passed in correctly
47+
}
48+
49+
function setup_postgesql_env {
50+
# Create the directory if it doesn't exist
51+
sudo mkdir -p /etc/environment.d
52+
53+
# Define the contents of the PostgreSQL environment file
54+
cat <<EOF | sudo tee /etc/environment.d/postgresql.env >/dev/null
55+
LOCALE_ARCHIVE=/usr/lib/locale/locale-archive
56+
LANG="en_US.UTF-8"
57+
LANGUAGE="en_US.UTF-8"
58+
LC_ALL="en_US.UTF-8"
59+
LC_CTYPE="en_US.UTF-8"
60+
EOF
61+
}
62+
63+
function setup_locale {
64+
cat << EOF >> /etc/locale.gen
65+
en_US.UTF-8 UTF-8
66+
EOF
67+
68+
cat << EOF > /etc/default/locale
69+
LANG="C.UTF-8"
70+
LC_CTYPE="C.UTF-8"
71+
EOF
72+
locale-gen en_US.UTF-8
73+
}
74+
75+
waitfor_boot_finished
76+
install_packages
77+
setup_postgesql_env
78+
setup_locale
79+
execute_playbook
80+
81+
# stage 2 things
82+
function install_nix() {
83+
sudo su -c "curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install --no-confirm \
84+
--extra-conf \"substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com\" \
85+
--extra-conf \"trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI=% cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=\" " -s /bin/bash root
86+
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
87+
88+
}
89+
90+
function execute_stage2_playbook {
91+
sudo tee /etc/ansible/ansible.cfg <<EOF
92+
[defaults]
93+
callbacks_enabled = timer, profile_tasks, profile_roles
94+
EOF
95+
# sed -i 's/- hosts: all/- hosts: localhost/' /tmp/ansible-playbook/ansible/playbook.yml
96+
# Run Ansible playbook
97+
export ANSIBLE_LOG_PATH=/tmp/ansible.log && export ANSIBLE_REMOTE_TEMP=/tmp
98+
ansible-playbook ./ansible/playbook.yml \
99+
--extra-vars '{"nixpkg_mode": false, "stage2_nix": true, "debpkg_mode": false}' \
100+
--extra-vars "git_commit_sha=${GIT_SHA}"
101+
}
102+
103+
function clean_system {
104+
# Copy cleanup scripts
105+
chmod +x /tmp/ansible-playbook/scripts/90-cleanup-qemu.sh
106+
/tmp/ansible-playbook/scripts/90-cleanup-qemu.sh
107+
108+
# # Cleanup logs
109+
rm -rf /var/log/*
110+
# # https://github.com/fail2ban/fail2ban/issues/1593
111+
touch /var/log/auth.log
112+
113+
touch /var/log/pgbouncer.log
114+
chown pgbouncer:postgres /var/log/pgbouncer.log
115+
116+
# # Setup postgresql logs
117+
mkdir -p /var/log/postgresql
118+
chown postgres:postgres /var/log/postgresql
119+
# # Setup wal-g logs
120+
mkdir /var/log/wal-g
121+
touch /var/log/wal-g/{backup-push.log,backup-fetch.log,wal-push.log,wal-fetch.log,pitr.log}
122+
123+
# #Creatre Sysstat directory for SAR
124+
mkdir /var/log/sysstat
125+
126+
chown -R postgres:postgres /var/log/wal-g
127+
chmod -R 0300 /var/log/wal-g
128+
129+
# # audit logs directory for apparmor
130+
mkdir /var/log/audit
131+
132+
# # unwanted files
133+
rm -rf /var/lib/apt/lists/*
134+
rm -rf /root/.cache
135+
rm -rf /root/.vpython*
136+
rm -rf /root/go
137+
rm -rf /mnt/usr/share/doc
138+
}
139+
140+
install_nix
141+
execute_stage2_playbook
142+
cloud-init clean --logs

qemu-arm64-nix.pkr.hcl

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
variable "ami" {
2+
type = string
3+
default = "ubuntu/images/hvm-ssd/ubuntu-focal-20.04-arm64-server-*"
4+
}
5+
6+
variable "profile" {
7+
type = string
8+
default = "${env("AWS_PROFILE")}"
9+
}
10+
11+
variable "ami_name" {
12+
type = string
13+
default = "supabase-postgres"
14+
}
15+
16+
variable "ami_regions" {
17+
type = list(string)
18+
default = ["ap-southeast-2"]
19+
}
20+
21+
variable "ansible_arguments" {
22+
type = string
23+
default = "--skip-tags install-postgrest,install-pgbouncer,install-supabase-internal"
24+
}
25+
26+
variable "aws_access_key" {
27+
type = string
28+
default = ""
29+
}
30+
31+
variable "aws_secret_key" {
32+
type = string
33+
default = ""
34+
}
35+
36+
variable "environment" {
37+
type = string
38+
default = "prod"
39+
}
40+
41+
variable "git_sha" {
42+
type = string
43+
}
44+
45+
# variable "region" {
46+
# type = string
47+
# }
48+
49+
variable "build-vol" {
50+
type = string
51+
default = "xvdc"
52+
}
53+
54+
# ccache docker image details
55+
variable "docker_user" {
56+
type = string
57+
default = ""
58+
}
59+
60+
variable "docker_passwd" {
61+
type = string
62+
default = ""
63+
}
64+
65+
variable "docker_image" {
66+
type = string
67+
default = ""
68+
}
69+
70+
variable "docker_image_tag" {
71+
type = string
72+
default = "latest"
73+
}
74+
75+
locals {
76+
creator = "packer"
77+
}
78+
79+
variable "postgres-version" {
80+
type = string
81+
default = ""
82+
}
83+
84+
variable "git-head-version" {
85+
type = string
86+
default = "unknown"
87+
}
88+
89+
variable "packer-execution-id" {
90+
type = string
91+
default = "unknown"
92+
}
93+
94+
variable "force-deregister" {
95+
type = bool
96+
default = false
97+
}
98+
99+
packer {
100+
required_plugins {
101+
amazon = {
102+
source = "github.com/hashicorp/amazon"
103+
version = "~> 1"
104+
}
105+
qemu = {
106+
version = "~> 1.0"
107+
source = "github.com/hashicorp/qemu"
108+
}
109+
}
110+
}
111+
112+
source "null" "dependencies" {
113+
communicator = "none"
114+
}
115+
116+
build {
117+
name = "cloudimg.deps"
118+
sources = ["source.null.dependencies"]
119+
120+
provisioner "shell-local" {
121+
inline = [
122+
"cp /usr/share/AAVMF/AAVMF_VARS.fd AAVMF_VARS.fd",
123+
"cloud-localds seeds-cloudimg.iso user-data-cloudimg meta-data"
124+
]
125+
inline_shebang = "/bin/bash -e"
126+
}
127+
}
128+
129+
source "qemu" "cloudimg" {
130+
boot_wait = "2s"
131+
cpus = 12
132+
disk_image = true
133+
disk_size = "15G"
134+
format = "qcow2"
135+
# TODO (darora): disable backing image for qcow2
136+
headless = true
137+
http_directory = "http"
138+
iso_checksum = "file:https://cloud-images.ubuntu.com/focal/current/SHA256SUMS"
139+
iso_url = "https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-arm64.img"
140+
memory = 20000
141+
qemu_binary = "qemu-system-aarch64"
142+
qemu_img_args {
143+
create = ["-F", "qcow2"]
144+
}
145+
qemuargs = [
146+
["-machine", "virt"],
147+
["-cpu", "host"],
148+
["-device", "virtio-gpu-pci"],
149+
["-drive", "if=pflash,format=raw,id=ovmf_code,readonly=on,file=/usr/share/AAVMF/AAVMF_CODE.fd"],
150+
["-drive", "if=pflash,format=raw,id=ovmf_vars,file=AAVMF_VARS.fd"],
151+
["-drive", "file=output-cloudimg/packer-cloudimg,format=qcow2"],
152+
["-drive", "file=seeds-cloudimg.iso,format=raw"],
153+
["--enable-kvm"]
154+
]
155+
shutdown_command = "sudo -S shutdown -P now"
156+
ssh_handshake_attempts = 500
157+
ssh_password = "ubuntu"
158+
ssh_timeout = "1h"
159+
ssh_username = "ubuntu"
160+
ssh_wait_timeout = "1h"
161+
use_backing_file = true
162+
accelerator = "kvm"
163+
}
164+
165+
build {
166+
name = "cloudimg.image"
167+
sources = ["source.qemu.cloudimg"]
168+
169+
# Copy ansible playbook
170+
provisioner "shell" {
171+
inline = ["mkdir /tmp/ansible-playbook"]
172+
}
173+
174+
provisioner "file" {
175+
source = "ansible"
176+
destination = "/tmp/ansible-playbook"
177+
}
178+
179+
provisioner "file" {
180+
source = "scripts"
181+
destination = "/tmp/ansible-playbook"
182+
}
183+
184+
provisioner "file" {
185+
source = "migrations"
186+
destination = "/tmp"
187+
}
188+
189+
provisioner "file" {
190+
source = "ebssurrogate/files/unit-tests"
191+
destination = "/tmp"
192+
}
193+
194+
provisioner "shell" {
195+
environment_vars = [
196+
"POSTGRES_SUPABASE_VERSION=${var.postgres-version}",
197+
"GIT_SHA=${var.git_sha}"
198+
]
199+
use_env_var_file = true
200+
script = "ebssurrogate/scripts/qemu-bootstrap-nix.sh"
201+
execute_command = "sudo -S sh -c '. {{.EnvVarFile}} && cd /tmp/ansible-playbook && {{.Path}}'"
202+
start_retry_timeout = "5m"
203+
skip_clean = true
204+
}
205+
}

0 commit comments

Comments
 (0)