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