forked from itsmechlark/features
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
141 lines (115 loc) · 4.81 KB
/
install.sh
File metadata and controls
141 lines (115 loc) · 4.81 KB
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
#!/usr/bin/env bash
PG_VERSION=${VERSION:-"latest"}
PG_ARCHIVE_ARCHITECTURES="amd64 arm64 i386 ppc64el"
PG_ARCHIVE_VERSION_CODENAMES="bookworm bullseye buster sid bionic focal jammy kinetic noble"
USERNAME="${USERNAME:-"${_REMOTE_USER:-"automatic"}"}"
# Default: Exit on any failure.
set -e
# Clean up
rm -rf /var/lib/apt/lists/*
# Setup STDERR.
err() {
echo "(!) $*" >&2
}
if [ "$(id -u)" -ne 0 ]; then
err 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
exit 1
fi
# Determine the appropriate non-root user
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
USERNAME=""
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
if id -u ${CURRENT_USER} > /dev/null 2>&1; then
USERNAME=${CURRENT_USER}
break
fi
done
if [ "${USERNAME}" = "" ]; then
USERNAME=root
fi
elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then
USERNAME=root
fi
apt_get_update()
{
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update -y
fi
}
# Checks if packages are installed and installs them if not
check_packages() {
if ! dpkg -s "$@" > /dev/null 2>&1; then
apt_get_update
apt-get -y install --no-install-recommends "$@"
fi
}
setup_pq() {
tee /usr/local/share/pq-init.sh << 'EOF'
#!/bin/sh
set -e
version_major=$(psql --version | sed -z "s/psql (PostgreSQL) //g" | grep -Eo -m 1 "^([0-9]+)" | sed -z "s/-//g")
echo "listen_addresses = '*'" >> /etc/postgresql/${version_major}/main/postgresql.conf \
&& echo "data_directory = '$PGDATA'" >> /etc/postgresql/${version_major}/main/postgresql.conf \
&& echo "host all all 0.0.0.0/0 trust" > /etc/postgresql/${version_major}/main/pg_hba.conf \
&& echo "host all all ::/0 trust" >> /etc/postgresql/${version_major}/main/pg_hba.conf \
&& echo "host all all ::1/128 trust" >> /etc/postgresql/${version_major}/main/pg_hba.conf
if [ ! -f "$PGDATA/PG_VERSION" ]; then
echo "Initializing PostgreSQL database..."
chown -R postgres:postgres $PGDATA \
&& chmod 0750 $PGDATA \
&& sudo -H -u postgres sh -c "/usr/lib/postgresql/${version_major}/bin/initdb -D $PGDATA --auth-local trust --auth-host scram-sha-256"
else
echo "PostgreSQL database already initialized, skipping initialization"
fi
echo "Starting PostgreSQL..."
sudo /etc/init.d/postgresql start \
&& pg_isready -t 60
set +e
# Execute whatever commands were passed in (if any). This allows us
# to set this script to ENTRYPOINT while still executing the default CMD.
exec "$@"
EOF
chmod +x /usr/local/share/pq-init.sh \
&& chown ${USERNAME}:root /usr/local/share/pq-init.sh
}
install_using_apt() {
# Install dependencies
check_packages apt-transport-https curl ca-certificates gnupg2 dirmngr sudo
# Import the repository signing key
curl -sS https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor --output /usr/share/keyrings/pgdg-archive-keyring.gpg
# Create the file repository configuration
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/pgdg-archive-keyring.gpg] http://apt.postgresql.org/pub/repos/apt ${VERSION_CODENAME}-pgdg main" > /etc/apt/sources.list.d/pgdg.list
# Update lists
apt-get update -yq
# Soft version matching for CLI
if [ "${PG_VERSION}" = "latest" ] || [ "${PG_VERSION}" = "lts" ] || [ "${PG_VERSION}" = "stable" ]; then
# Empty, meaning grab whatever "latest" is in apt repo
version_major=""
version_suffix=""
else
version_major="-$(echo "${PG_VERSION}" | grep -oE -m 1 "^([0-9]+)")"
version_suffix="=$(apt-cache show postgresql${version_major} | awk -F"Version: " '{print $2}' | grep -E -m 1 "^(${PG_VERSION})(\.|$|\+.*|-.*)")"
if [ -z ${version_suffix} ] || [ ${version_suffix} = "=" ]; then
echo "Provided PG_VERSION (${PG_VERSION}) was not found in the apt-cache for this package+distribution combo";
return 1
fi
echo "version_major ${version_major}"
echo "version_suffix ${version_suffix}"
fi
(apt-get install -yq postgresql${version_major}${version_suffix} postgresql-client${version_major} \
&& setup_pq) || return 1
}
export DEBIAN_FRONTEND=noninteractive
# Source /etc/os-release to get OS info
. /etc/os-release
architecture="$(dpkg --print-architecture)"
if [[ "${PG_ARCHIVE_ARCHITECTURES}" = *"${architecture}"* ]] && [[ "${PG_ARCHIVE_VERSION_CODENAMES}" = *"${VERSION_CODENAME}"* ]]; then
install_using_apt || use_zip="true"
else
use_zip="true"
fi
# Clean up
rm -rf /var/lib/apt/lists/*
echo "Done!"