-
Notifications
You must be signed in to change notification settings - Fork 41
/
stellar-horizon.postinst
77 lines (66 loc) · 3.04 KB
/
stellar-horizon.postinst
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
#!/bin/sh
# postinst script for stellar-core-postgres
#
# see: dh_installdeb(1)
set -e
# summary of how this script can be called:
# * <postinst> `configure' <most-recently-configured-version>
# * <old-postinst> `abort-upgrade' <new version>
# * <conflictor's-postinst> `abort-remove' `in-favour' <package>
# <new-version>
# * <postinst> `abort-remove'
# * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
# <failed-install-package> <version> `removing'
# <conflicting-package> <version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package
ROLE=stellar;
DBNAME=stellar;
case "$1" in
configure)
# check stellar user exists
if ! getent passwd stellar > /dev/null; then
adduser --system --group --quiet --home /var/lib/stellar \
--no-create-home --disabled-password --shell /bin/bash $ROLE;
fi
chown -R stellar:stellar /var/lib/stellar/ /var/log/stellar/
# run db migrations
# Confirm the stellar role exists in Postgres before proceeding. If it's not there then no DB migration should proceed
if [ -x /usr/bin/stellar-horizon ]; then
if [ -f /etc/default/stellar-horizon ]; then
# extract --db_url from /etc/default/stellar-horizon
db_connection_string=$(grep -r '^DATABASE_URL' /etc/default/stellar-horizon | sed 's/DATABASE_URL=//g' | sed 's/"//g');
# extract INGEST flag from /etc/default/stellar-horizon. Default
ingest_flag=$(grep -r '^INGEST=' /etc/default/stellar-horizon||echo "INGEST=true");
ingest_flag=$(echo $ingest_flag | sed 's/INGEST=//g' | sed 's/"//g');
# check `stellar` role and DATABASE_URL
if echo "SELECT 'role_exists' FROM pg_roles WHERE rolname='${ROLE}'" | runuser -l ${ROLE} -c "psql '${db_connection_string}' -tA" 2>&1 | grep 'role_exists' > /dev/null; then
# check `horizon` database has been initialised
if echo "SELECT 'horizon' FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'history_ledgers';" | runuser -l ${ROLE} -c "psql '${db_connection_string}' -tA" 2>&1 | grep 'horizon' > /dev/null; then
export DATABASE_URL="$db_connection_string"
export INGEST="$ingest_flag"
sudo -E -u ${ROLE} stellar-horizon db migrate up
echo 'info: migrated database'
else
echo "warning: the horizon database is not yet initialised, try running 'stellar-horizon-cmd db init'"
echo "warning: migrations have not been run; won't attempt to start horizon"
exit 0
fi
else
echo "warning: either the stellar postgresql role and/or the horizon database doesn't exist yet; won't attempt to start horizon"
exit 0
fi
fi
fi
;;
abort-upgrade|abort-remove|abort-deconfigure)
;;
*)
echo "postinst called with unknown argument \`$1'" >&2
exit 1
;;
esac
# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.
#DEBHELPER#
exit 0