Skip to content

Commit ca3bd10

Browse files
Lakshmipathidarora
authored andcommitted
fastboot: Build arm64 AMI image
- Supports arm64 ext4fs AMI creation using EBS-Surrogate - Reduce VM boot-time between 10 to 15 seconds - Shrink rootfs size from 16GB to 11GB - Cleanup unused files and packages Signed-off-by: Lakshmipathi Ganapathi <[email protected]> Update amazon-arm64.pkr.hcl Update amazon-arm64.pkr.hcl
1 parent 331f8f3 commit ca3bd10

22 files changed

+1210
-17
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Run CI checks
33
on:
44
push:
55
branches:
6-
- develop
6+
- wip/fastboot
77

88
jobs:
99
build:
@@ -16,4 +16,4 @@ jobs:
1616

1717
- name: Build AMI
1818
run: |
19-
packer build -timestamp-ui -color=false -on-error=cleanup -var-file common.vars.json -var-file development-arm.vars.json amazon.json
19+
packer build -var-file="development-arm.vars.pkr.hcl" -var-file="common.vars.pkr.hcl" amazon-arm64.pkr.hcl

amazon-arm64.pkr.hcl

+218
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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,--skip-tags,install-pgbouncer,--skip-tags,install-supabase-internal,ebssurrogate_mode='true'"
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 "region" {
42+
type = string
43+
}
44+
45+
variable "build-vol" {
46+
type = string
47+
default = "xvdc"
48+
}
49+
50+
# ccache docker image details
51+
variable "docker_user" {
52+
type = string
53+
default = ""
54+
}
55+
56+
variable "docker_passwd" {
57+
type = string
58+
default = ""
59+
}
60+
61+
variable "docker_image" {
62+
type = string
63+
default = ""
64+
}
65+
66+
variable "docker_image_tag" {
67+
type = string
68+
default = "latest"
69+
}
70+
71+
locals {
72+
creator = "packer"
73+
}
74+
75+
variable "postgres-version" {
76+
type = string
77+
default = ""
78+
}
79+
80+
# source block
81+
source "amazon-ebssurrogate" "source" {
82+
profile = "${var.profile}"
83+
#access_key = "${var.aws_access_key}"
84+
#ami_name = "${var.ami_name}-arm64-${formatdate("YYYY-MM-DD-hhmm", timestamp())}"
85+
ami_name = "${var.ami_name}-${var.postgres-version}"
86+
ami_virtualization_type = "hvm"
87+
ami_architecture = "arm64"
88+
ami_regions = "${var.ami_regions}"
89+
instance_type = "t4g.2xlarge"
90+
region = "${var.region}"
91+
#secret_key = "${var.aws_secret_key}"
92+
93+
# Use latest official ubuntu focal ami owned by Canonical.
94+
source_ami_filter {
95+
filters = {
96+
virtualization-type = "hvm"
97+
name = "${var.ami}"
98+
root-device-type = "ebs"
99+
}
100+
owners = [ "099720109477" ]
101+
most_recent = true
102+
}
103+
ena_support = true
104+
launch_block_device_mappings {
105+
device_name = "/dev/xvdf"
106+
delete_on_termination = true
107+
volume_size = 10
108+
volume_type = "gp3"
109+
}
110+
111+
launch_block_device_mappings {
112+
device_name = "/dev/${var.build-vol}"
113+
delete_on_termination = true
114+
volume_size = 16
115+
volume_type = "gp2"
116+
omit_from_artifact = true
117+
}
118+
119+
run_tags = {
120+
creator = "packer"
121+
appType = "postgres"
122+
}
123+
run_volume_tags = {
124+
creator = "packer"
125+
appType = "postgres"
126+
}
127+
snapshot_tags = {
128+
creator = "packer"
129+
appType = "postgres"
130+
}
131+
tags = {
132+
creator = "packer"
133+
appType = "postgres"
134+
}
135+
136+
communicator = "ssh"
137+
ssh_pty = true
138+
ssh_username = "ubuntu"
139+
ssh_timeout = "5m"
140+
141+
ami_root_device {
142+
source_device_name = "/dev/xvdf"
143+
device_name = "/dev/xvda"
144+
delete_on_termination = true
145+
volume_size = 10
146+
volume_type = "gp2"
147+
}
148+
}
149+
150+
# a build block invokes sources and runs provisioning steps on them.
151+
build {
152+
sources = ["source.amazon-ebssurrogate.source"]
153+
154+
provisioner "file" {
155+
source = "ebssurrogate/files/sources-arm64.cfg"
156+
destination = "/tmp/sources.list"
157+
}
158+
159+
provisioner "file" {
160+
source = "ebssurrogate/files/ebsnvme-id"
161+
destination = "/tmp/ebsnvme-id"
162+
}
163+
164+
provisioner "file" {
165+
source = "ebssurrogate/files/70-ec2-nvme-devices.rules"
166+
destination = "/tmp/70-ec2-nvme-devices.rules"
167+
}
168+
169+
provisioner "file" {
170+
source = "ebssurrogate/scripts/chroot-bootstrap.sh"
171+
destination = "/tmp/chroot-bootstrap.sh"
172+
}
173+
174+
provisioner "file" {
175+
source = "ebssurrogate/files/cloud.cfg"
176+
destination = "/tmp/cloud.cfg"
177+
}
178+
179+
provisioner "file" {
180+
source = "ebssurrogate/files/vector.timer"
181+
destination = "/tmp/vector.timer"
182+
}
183+
184+
# Copy ansible playbook
185+
provisioner "shell" {
186+
inline = ["mkdir /tmp/ansible-playbook"]
187+
}
188+
189+
provisioner "file" {
190+
source = "ansible"
191+
destination = "/tmp/ansible-playbook"
192+
}
193+
194+
provisioner "file" {
195+
source = "scripts"
196+
destination = "/tmp/ansible-playbook"
197+
}
198+
199+
provisioner "shell" {
200+
environment_vars = [
201+
"ARGS=${var.ansible_arguments}",
202+
"DOCKER_USER=${var.docker_user}",
203+
"DOCKER_PASSWD=${var.docker_passwd}",
204+
"DOCKER_IMAGE=${var.docker_image}",
205+
"DOCKER_IMAGE_TAG=${var.docker_image_tag}"
206+
]
207+
script = "ebssurrogate/scripts/surrogate-bootstrap.sh"
208+
execute_command = "sudo -S sh -c '{{ .Vars }} {{ .Path }}'"
209+
start_retry_timeout = "5m"
210+
skip_clean = true
211+
}
212+
213+
provisioner "file" {
214+
source = "/tmp/ansible.log"
215+
destination = "/tmp/ansible.log"
216+
direction = "download"
217+
}
218+
}

ansible/playbook.yml

+31-13
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@
4747
systemd:
4848
name: postgresql
4949
state: started
50+
when: not ebssurrogate_mode
51+
52+
- name: Start Postgres Database without Systemd
53+
become: yes
54+
become_user: postgres
55+
shell:
56+
cmd: /usr/bin/pg_ctl -D /var/lib/postgresql/data start
57+
when: ebssurrogate_mode
58+
59+
- name: Install WAL-G
60+
import_tasks: tasks/setup-wal-g.yml
61+
62+
- name: Install PostgREST
63+
import_tasks: tasks/setup-postgrest.yml
64+
tags:
65+
- install-postgrest
66+
67+
- name: Adjust APT update intervals
68+
copy:
69+
src: files/apt_periodic
70+
dest: /etc/apt/apt.conf.d/10periodic
5071

5172
- name: Transfer init SQL files
5273
copy:
@@ -67,19 +88,6 @@
6788
state: absent
6889
loop: "{{ sql_files }}"
6990

70-
- name: Install PostgREST
71-
import_tasks: tasks/setup-postgrest.yml
72-
tags:
73-
- install-postgrest
74-
75-
- name: Clean out build dependencies
76-
import_tasks: tasks/clean-build-dependencies.yml
77-
78-
- name: Adjust APT update intervals
79-
copy:
80-
src: files/apt_periodic
81-
dest: /etc/apt/apt.conf.d/10periodic
82-
8391
- name: UFW - Allow SSH connections
8492
ufw:
8593
rule: allow
@@ -160,7 +168,17 @@
160168
paths: /usr/lib/postgresql/bin
161169
register: postgresql_bin
162170

171+
- name: Clean out build dependencies
172+
import_tasks: tasks/clean-build-dependencies.yml
173+
163174
- name: Create symbolic links for Postgres binaries to /usr/bin/
164175
become: yes
165176
shell:
166177
cmd: "for fl in /usr/lib/postgresql/bin/* ; do ln -sf $fl /usr/bin/$(basename $fl) ; done"
178+
179+
- name: Stop Postgres Database without Systemd
180+
become: yes
181+
become_user: postgres
182+
shell:
183+
cmd: /usr/bin/pg_ctl -D /var/lib/postgresql/data stop
184+
when: ebssurrogate_mode

ansible/tasks/internal/optimizations.yml

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
community.general.snap:
33
name: amazon-ssm-agent
44
state: absent
5+
failed_when: not ebssurrogate_mode
56

67
- name: ensure services are stopped and disabled for first boot
78
systemd:
@@ -15,12 +16,14 @@
1516
- fail2ban
1617
- motd-news
1718
- vector
19+
failed_when: not ebssurrogate_mode
1820

1921
- name: Remove snapd
2022
apt:
2123
state: absent
2224
pkg:
2325
- snapd
26+
failed_when: not ebssurrogate_mode
2427

2528
- name: ensure services are stopped and disabled for first boot
2629
systemd:
@@ -30,6 +33,7 @@
3033
masked: yes
3134
with_items:
3235
- lvm2-monitor
36+
failed_when: not ebssurrogate_mode
3337

3438
- name: disable man-db
3539
become: yes
@@ -40,3 +44,4 @@
4044
- man-db
4145
- popularity-contest
4246
- ubuntu-advantage-tools
47+
failed_when: not ebssurrogate_mode

ansible/tasks/setup-osquery.yml

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
- name: filter out audit logs from journald # c.f. https://osquery.readthedocs.io/en/stable/installation/install-linux/
1111
shell: |
1212
systemctl mask --now systemd-journald-audit.socket
13+
when: not ebssurrogate_mode
14+
15+
- name: filter out audit logs from journald without Systemd
16+
ansible.builtin.file:
17+
src: /dev/null
18+
dest: /etc/systemd/system/systemd-journald-audit.socket
19+
state: link
20+
become: yes
21+
when: ebssurrogate_mode
1322

1423
- name: install systemd service
1524
template:

ansible/tasks/setup-postgres.yml

+24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- libxslt-dev
1212
- libssl-dev
1313
- libsystemd-dev
14+
- libpq-dev
1415
- libxml2-utils
1516
- uuid-dev
1617
- xsltproc
@@ -32,6 +33,15 @@
3233
shell:
3334
cmd: update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10
3435

36+
# Setup permissions
37+
- name: Update permissions for /var/tmp directory
38+
file:
39+
path: /var/tmp/
40+
owner: root
41+
group: root
42+
mode: '1777'
43+
become: yes
44+
3545
# Building Postgres from source
3646
- name: Postgres - download latest release
3747
get_url:
@@ -142,6 +152,20 @@
142152
dest: /etc/postgresql/pg_ident.conf
143153
group: postgres
144154

155+
- name: Find all files in /usr/lib/postgresql/bin
156+
find:
157+
paths: /usr/lib/postgresql/bin
158+
register: postgresql_bin
159+
160+
- name: Create symbolic links for Postgres binaries to /usr/bin/
161+
become: yes
162+
file:
163+
src: "{{ item.path }}"
164+
path: "/usr/bin/{{ item.path | basename }}"
165+
state: link
166+
force: yes
167+
with_items: "{{ postgresql_bin.files }}"
168+
145169
# init DB
146170
- name: Initialize the database
147171
become: yes

0 commit comments

Comments
 (0)