Skip to content

Commit 25e46d6

Browse files
committed
feat: migrating and adjusting build steps to work with sourcing files
from nix
1 parent 08b6fb2 commit 25e46d6

File tree

9 files changed

+261
-112
lines changed

9 files changed

+261
-112
lines changed

amazon-arm64.pkr.hcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ source "amazon-ebssurrogate" "source" {
106106
profile = "${var.profile}"
107107
#access_key = "${var.aws_access_key}"
108108
#ami_name = "${var.ami_name}-arm64-${formatdate("YYYY-MM-DD-hhmm", timestamp())}"
109-
ami_name = "${var.ami_name}-${var.postgres-version}"
109+
ami_name = "${var.ami_name}-${var.postgres-version}-stage-1"
110110
ami_virtualization_type = "hvm"
111111
ami_architecture = "arm64"
112112
ami_regions = "${var.ami_regions}"

ansible/tasks/finalize-ami.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
rule: allow
3434
port: https
3535
tags:
36-
- install-supabase-internal
36+
- install-supabase-internal
3737

3838
- name: UFW - Deny all other incoming traffic by default
3939
ufw:
@@ -71,10 +71,11 @@
7171
systemctl reenable logrotate.timer
7272
become: yes
7373

74-
- name: import pgsodium_getkey script
75-
template:
76-
src: files/pgsodium_getkey_readonly.sh.j2
77-
dest: "{{ pg_bindir }}/pgsodium_getkey.sh"
78-
owner: postgres
79-
group: postgres
80-
mode: 0700
74+
# - name: import pgsodium_getkey script
75+
# template:
76+
# src: files/pgsodium_getkey_readonly.sh.j2
77+
# dest: "{{ pg_bindir }}/pgsodium_getkey.sh"
78+
# owner: postgres
79+
# group: postgres
80+
# mode: 0700
81+
# TODO bring into 2nd phase

ansible/tasks/setup-gotrue.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939

4040
# libpq is a C library that enables user programs to communicate with
4141
# the PostgreSQL database server.
42-
# - name: gotrue - system dependencies
43-
# apt:
44-
# pkg:
45-
# - libpq-dev
42+
- name: gotrue - system dependencies
43+
apt:
44+
pkg:
45+
- libpq-dev
4646

4747
- name: gotrue - create service file
4848
template:

ansible/tasks/setup-postgres.yml

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
# - name: Postgres - copy package
2-
# copy:
3-
# src: files/postgres/
4-
# dest: /tmp/build/
5-
6-
# - name: Postgres - add PPA
7-
# apt_repository:
8-
# repo: "deb [ trusted=yes ] file:///tmp/build ./"
9-
# state: present
10-
111
# - name: Postgres - install commons
122
# apt:
133
# name: postgresql-common
@@ -16,26 +6,9 @@
166
# - name: Do not create main cluster
177
# shell:
188
# cmd: sed -ri 's/#(create_main_cluster) .*$/\1 = false/' /etc/postgresql-common/createcluster.conf
19-
20-
# - name: Postgres - install server
21-
# apt:
22-
# name: postgresql-{{ postgresql_major }}={{ postgresql_release }}-1.pgdg20.04+1
23-
# install_recommends: no
24-
25-
# - name: Postgres - remove PPA
26-
# apt_repository:
27-
# repo: "deb [ trusted=yes ] file:///tmp/build ./"
28-
# state: absent
29-
30-
# - name: Postgres - cleanup package
31-
# file:
32-
# path: /tmp/build
33-
# state: absent
34-
35-
# - name: Create symlink to /usr/lib/postgresql/bin
36-
# shell:
37-
# cmd: ln -s /usr/lib/postgresql/{{ postgresql_major }}/bin /usr/lib/postgresql/bin
38-
#TODO resolve in new build
9+
#
10+
# TODO These lines appear to be installing and configuring https://launchpad.net/ubuntu/+source/postgresql-common
11+
# as far as I can see, we don't need this now.
3912

4013
- name: create postgres group
4114
group:
@@ -133,38 +106,3 @@
133106
mode: 0750
134107
with_items:
135108
- "/data/pgdata"
136-
137-
# - name: Link database data_dir to data volume directory
138-
# file:
139-
# src: "/data/pgdata"
140-
# path: "/var/lib/postgresql/data"
141-
# state: link
142-
# force: yes
143-
144-
# - name: Initialize the database
145-
# become: yes
146-
# become_user: postgres
147-
# shell:
148-
# cmd: /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data initdb -o "--allow-group-access"
149-
# vars:
150-
# ansible_command_timeout: 60
151-
# # Circumvents the following error:
152-
# # "Timeout (12s) waiting for privilege escalation prompt"
153-
154-
# - name: copy PG systemd unit
155-
# template:
156-
# src: files/postgresql_config/postgresql.service.j2
157-
# dest: /etc/systemd/system/postgresql.service
158-
159-
# - name: copy optimizations systemd unit
160-
# template:
161-
# src: files/database-optimizations.service.j2
162-
# dest: /etc/systemd/system/database-optimizations.service
163-
164-
# # Reload
165-
# - name: System - systemd reload
166-
# systemd:
167-
# enabled: yes
168-
# name: postgresql
169-
# daemon_reload: yes
170-
# TODO resolve in new build

ansible/tasks/stage2/playbook.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- hosts: localhost
2+
become: yes
3+
4+
5+
tasks:
6+
- set_fact:
7+
supabase_internal: true
8+
tags:
9+
- install-supabase-internal
10+
11+
- set_fact:
12+
parallel_jobs: 16
13+
14+
- name: Install Postgres from nix binary cache
15+
import_tasks: stage2-setup-postgres.yml
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
- name: Install Postgres from nix binary cache
2+
become: yes
3+
shell: |
4+
sudo -u postgres bash -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && nix profile install github:supabase/postgres#psql_15/bin"
5+
6+
- name: Ensure /usr/lib/postgresql/bin directory exists
7+
file:
8+
path: /usr/lib/postgresql/bin
9+
state: directory
10+
owner: postgres
11+
group: postgres
12+
13+
14+
- name: Ensure /usr/lib/postgresql/share directory exists
15+
file:
16+
path: /usr/lib/postgresql/share/postgresql
17+
state: directory
18+
owner: postgres
19+
group: postgres
20+
21+
- name: Ensure /usr/lib/postgresql/share/contrib directory exists
22+
file:
23+
path: /usr/lib/postgresql/share/postgresql/contrib
24+
state: directory
25+
owner: postgres
26+
group: postgres
27+
28+
- name: Ensure /usr/lib/postgresql/share/timezonesets directory exists
29+
file:
30+
path: /usr/lib/postgresql/share/postgresql/timezonesets
31+
state: directory
32+
owner: postgres
33+
group: postgres
34+
35+
- name: Ensure /usr/lib/postgresql/share/tsearch_data directory exists
36+
file:
37+
path: /usr/lib/postgresql/share/postgresql/tsearch_data
38+
state: directory
39+
owner: postgres
40+
group: postgres
41+
42+
- name: Ensure /usr/lib/postgresql/share/extension directory exists
43+
file:
44+
path: /usr/lib/postgresql/share/postgresql/extension
45+
state: directory
46+
owner: postgres
47+
group: postgres
48+
49+
- name: Create symbolic links from /home/postgres/.nix-profile/bin to /usr/lib/postgresql/bin
50+
file:
51+
src: "{{ item }}"
52+
dest: "/usr/lib/postgresql/bin/{{ item | basename }}"
53+
state: link
54+
with_fileglob:
55+
- "/home/postgres/.nix-profile/bin/*"
56+
become: yes
57+
58+
59+
- name: Create symbolic links from /home/postgres/.nix-profile/share/postgresql to /usr/lib/postgresql/share/postgresql
60+
file:
61+
src: "{{ item }}"
62+
dest: "/usr/lib/postgresql/share/postgresql/{{ item | basename }}"
63+
state: link
64+
with_fileglob:
65+
- "/home/postgres/.nix-profile/share/postgresql/*"
66+
become: yes
67+
68+
- name: Create symbolic links from /home/postgres/.nix-profile/share/postgresql/extension to /usr/lib/postgresql/share/postgresql/extension
69+
file:
70+
src: "{{ item }}"
71+
dest: "/usr/lib/postgresql/share/postgresql/extension/{{ item | basename }}"
72+
state: link
73+
with_fileglob:
74+
- "/home/postgres/.nix-profile/share/postgresql/extension/*"
75+
become: yes
76+
77+
78+
# - name: Create directories recursively
79+
# file:
80+
# path: "/usr/lib/postgresql/share/postgresql/contrib/{{ item | dirname }}"
81+
# state: directory
82+
# with_fileglob:
83+
# - "/home/postgres/.nix-profile/share/postgresql/contrib/*"
84+
85+
# - name: Create symbolic links for files
86+
# file:
87+
# src: "{{ item }}"
88+
# dest: "/usr/lib/postgresql/share/postgresql/contrib/{{ item | regex_replace('^/home/postgres/.nix-profile/share/postgresql/contrib/', '') }}"
89+
# state: link
90+
# with_find:
91+
# paths: "/home/postgres/.nix-profile/share/postgresql/contrib/"
92+
# recurse: yes
93+
# patterns: "*"
94+
# file_type: any
95+
96+
- name: Create symbolic links from /home/postgres/.nix-profile/share/postgresql/timezonesets to /usr/lib/postgresql/share/postgresql/timeszonesets
97+
file:
98+
src: "{{ item }}"
99+
dest: "/usr/lib/postgresql/share/postgresql/timezonesets/{{ item | basename }}"
100+
state: link
101+
with_fileglob:
102+
- "/home/postgres/.nix-profile/share/postgresql/timezonesets/*"
103+
become: yes
104+
105+
- name: Create symbolic links from /home/postgres/.nix-profile/share/postgresql/tsearch_data to /usr/lib/postgresql/share/postgresql/tsearch_data
106+
file:
107+
src: "{{ item }}"
108+
dest: "/usr/lib/postgresql/share/postgresql/tsearch_data/{{ item | basename }}"
109+
state: link
110+
with_fileglob:
111+
- "/home/postgres/.nix-profile/share/postgresql/tsearch_data/*"
112+
become: yes
113+
114+
115+
# Install extensions before init
116+
# - name: Install Postgres extensions
117+
# import_tasks: tasks/setup-docker.yml
118+
# TODO resolve in new build
119+
120+
- name: Link database data_dir to data volume directory
121+
file:
122+
src: "/data/pgdata"
123+
path: "/var/lib/postgresql/data"
124+
state: link
125+
force: yes
126+
owner: postgres
127+
group: postgres
128+
129+
- name: Initialize the database
130+
become: yes
131+
become_user: postgres
132+
shell:
133+
cmd: /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data initdb -o "--allow-group-access"
134+
vars:
135+
ansible_command_timeout: 60
136+
# # Circumvents the following error:
137+
# # "Timeout (12s) waiting for privilege escalation prompt"
138+
139+
- name: copy PG systemd unit
140+
template:
141+
src: files/postgresql_config/postgresql.service.j2
142+
dest: /etc/systemd/system/postgresql.service
143+
144+
- name: copy optimizations systemd unit
145+
template:
146+
src: files/database-optimizations.service.j2
147+
dest: /etc/systemd/system/database-optimizations.service
148+
149+
# # # Reload
150+
- name: System - systemd reload
151+
systemd:
152+
enabled: yes
153+
name: postgresql
154+
daemon_reload: yes

flake.nix

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
# it also serves as a base for importing the orioldb/postgres overlay to
3030
#build the orioledb postgres patched version of postgresql16
3131
oriole_pkgs = import nixpkgs {
32+
config = { allowUnfree = true; };
3233
inherit system;
3334
overlays = [
3435
# NOTE (aseipp): add any needed overlays here. in theory we could
@@ -44,6 +45,7 @@
4445
#This variable works the same as 'oriole_pkgs' but builds using the upstream
4546
#nixpkgs builds of postgresql 15 and 16 + the overlays listed below
4647
pkgs = import nixpkgs {
48+
config = { allowUnfree = true; };
4749
inherit system;
4850
overlays = [
4951
# NOTE (aseipp): add any needed overlays here. in theory we could
@@ -450,7 +452,7 @@
450452
mkdir -p $out/bin
451453
substitute ${./nix/tools/run-replica.sh.in} $out/bin/start-postgres-replica \
452454
--subst-var-by 'PGSQL_SUPERUSER' '${pgsqlSuperuser}' \
453-
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'\
455+
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'
454456
chmod +x $out/bin/start-postgres-replica
455457
'';
456458
sync-exts-versions = pkgs.runCommand "sync-exts-versions" { } ''
@@ -545,7 +547,11 @@
545547
nix-update
546548
pg_prove
547549
shellcheck
548-
550+
ansible
551+
ansible-lint
552+
(packer.overrideAttrs (oldAttrs: {
553+
version = "1.7.8";
554+
}))
549555
basePackages.start-server
550556
basePackages.start-client
551557
basePackages.start-replica

scripts/nix-provision.sh

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,39 @@ set -o errexit
55
set -o pipefail
66
set -o xtrace
77

8+
function install_packages {
9+
# Setup Ansible on host VM
10+
sudo apt-get update && sudo apt-get install software-properties-common -y
11+
sudo add-apt-repository --yes --update ppa:ansible/ansible && sudo apt-get install ansible -y
12+
ansible-galaxy collection install community.general
13+
14+
}
15+
16+
817

918
function install_nix() {
1019
sudo su -c "curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install --no-confirm \
1120
--extra-conf \"substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com\" \
1221
--extra-conf \"trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI=% cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=\" " -s /bin/bash root
1322
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
14-
sudo -u postgres bash -c '. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && nix profile install github:supabase/postgres#psql_15/bin'
1523

1624
}
1725

26+
27+
function execute_stage2_playbook {
28+
29+
sudo tee /etc/ansible/ansible.cfg <<EOF
30+
[defaults]
31+
callbacks_enabled = timer, profile_tasks, profile_roles
32+
EOF
33+
# Run Ansible playbook
34+
#export ANSIBLE_LOG_PATH=/tmp/ansible.log && export ANSIBLE_DEBUG=True && export ANSIBLE_REMOTE_TEMP=/tmp
35+
export ANSIBLE_LOG_PATH=/tmp/ansible.log && export ANSIBLE_REMOTE_TEMP=/tmp
36+
ansible-playbook /tmp/ansible-playbook/stage2/playbook.yml $ARGS
37+
}
38+
39+
40+
install_packages
1841
install_nix
42+
execute_stage2_playbook
43+

0 commit comments

Comments
 (0)