|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +mkdir /var/run/grafana && chown grafana /var/run/grafana |
| 4 | + |
| 5 | +if [ ! -f /pgwatch2/persistent-config/self-signed-ssl.key -o ! -f /pgwatch2/persistent-config/self-signed-ssl.pem ] ; then |
| 6 | + openssl req -x509 -newkey rsa:4096 -keyout /pgwatch2/persistent-config/self-signed-ssl.key -out /pgwatch2/persistent-config/self-signed-ssl.pem -days 3650 -nodes -sha256 -subj '/CN=pw2' |
| 7 | + cp /pgwatch2/persistent-config/self-signed-ssl.pem /etc/ssl/certs/ssl-cert-snakeoil.pem |
| 8 | + cp /pgwatch2/persistent-config/self-signed-ssl.key /etc/ssl/private/ssl-cert-snakeoil.key |
| 9 | + chown postgres /etc/ssl/certs/ssl-cert-snakeoil.pem /etc/ssl/private/ssl-cert-snakeoil.key |
| 10 | + chmod -R 0600 /etc/ssl/certs/ssl-cert-snakeoil.pem /etc/ssl/private/ssl-cert-snakeoil.key |
| 11 | + chmod -R o+rx /pgwatch2/persistent-config |
| 12 | +fi |
| 13 | + |
| 14 | +# enable password encryption by default from v1.8.0 |
| 15 | +if [ ! -f /pgwatch2/persistent-config/default-password-encryption-key.txt ]; then |
| 16 | + echo -n "${RANDOM}${RANDOM}${RANDOM}${RANDOM}" > /pgwatch2/persistent-config/default-password-encryption-key.txt |
| 17 | + chmod 0600 /pgwatch2/persistent-config/default-password-encryption-key.txt |
| 18 | +fi |
| 19 | + |
| 20 | +GRAFANASSL="${PW2_GRAFANASSL,,}" # to lowercase |
| 21 | +if [ "$GRAFANASSL" == "1" ] || [ "${GRAFANASSL:0:1}" == "t" ]; then |
| 22 | + $(grep -q 'protocol = http$' /etc/grafana/grafana.ini) |
| 23 | + if [ "$?" -eq 0 ] ; then |
| 24 | + sed -i 's/protocol = http.*/protocol = https/' /etc/grafana/grafana.ini |
| 25 | + fi |
| 26 | +fi |
| 27 | + |
| 28 | +if [ -n "$PW2_GRAFANAUSER" ] ; then |
| 29 | + sed -i "s/admin_user =.*/admin_user = ${PW2_GRAFANAUSER}/" /etc/grafana/grafana.ini |
| 30 | +fi |
| 31 | + |
| 32 | +if [ -n "$PW2_GRAFANAPASSWORD" ] ; then |
| 33 | + sed -i "s/admin_password =.*/admin_password = ${PW2_GRAFANAPASSWORD}/" /etc/grafana/grafana.ini |
| 34 | +fi |
| 35 | + |
| 36 | +if [ -n "$PW2_GRAFANANOANONYMOUS" ] ; then |
| 37 | +CFG=$(cat <<-'HERE' |
| 38 | +[auth.anonymous] |
| 39 | +enabled = false |
| 40 | +HERE |
| 41 | +) |
| 42 | +echo "$CFG" >> /etc/grafana/grafana.ini |
| 43 | +fi |
| 44 | + |
| 45 | +if [ ! -f /pgwatch2/persistent-config/db-bootstrap-done-marker ] ; then |
| 46 | + |
| 47 | +if [ ! -d /var/lib/postgresql/15 ]; then |
| 48 | + mkdir /var/lib/postgresql/15 && chown -R postgres:postgres /var/lib/postgresql/15 |
| 49 | + pg_dropcluster 15 main |
| 50 | + pg_createcluster --locale en_US.UTF-8 15 main |
| 51 | + echo "include = 'pgwatch_postgresql.conf'" >> /etc/postgresql/15/main/postgresql.conf |
| 52 | + cp /pgwatch2/postgresql_timescale.conf /etc/postgresql/15/main/pgwatch_postgresql.conf |
| 53 | + cp /pgwatch2/pg_hba.conf /etc/postgresql/15/main/pg_hba.conf |
| 54 | +fi |
| 55 | + |
| 56 | +pg_ctlcluster 15 main start -- --wait |
| 57 | + |
| 58 | +su -c "psql -d postgres -f /pgwatch2/bootstrap/change_pw.sql" postgres |
| 59 | +su -c "psql -d postgres -f /pgwatch2/bootstrap/grant_monitor_to_pgwatch2.sql" postgres |
| 60 | +su -c "psql -d postgres -f /pgwatch2/bootstrap/create_db_pgwatch.sql" postgres |
| 61 | +su -c "psql -d pgwatch2 -f /pgwatch2/bootstrap/revoke_public_create.sql" postgres |
| 62 | +su -c "psql -d postgres -f /pgwatch2/bootstrap/create_db_grafana.sql" postgres |
| 63 | +su -c "psql -d postgres -f /pgwatch2/bootstrap/create_db_metric_store.sql" postgres |
| 64 | +su -c "psql -d pgwatch2 -f /pgwatch2/sql/config_store/config_store.sql" postgres |
| 65 | +su -c "psql -d pgwatch2 -f /pgwatch2/sql/config_store/metric_definitions.sql" postgres |
| 66 | +su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/00_schema_base.sql" postgres |
| 67 | +su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/01_old_metrics_cleanup_procedure.sql" postgres |
| 68 | + |
| 69 | +su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/timescale/ensure_partition_timescale.sql" postgres |
| 70 | +su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/timescale/change_compression_interval.sql" postgres |
| 71 | +su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/timescale/metric_store_timescale.sql" postgres |
| 72 | +su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/timescale/change_chunk_interval.sql" postgres |
| 73 | +su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/metric-time/ensure_partition_metric_time.sql" postgres |
| 74 | + |
| 75 | +su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_load_average/9.1/metric.sql" postgres |
| 76 | +su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_stat_statements/9.4/metric.sql" postgres |
| 77 | +su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_stat_activity/9.2/metric.sql" postgres |
| 78 | +su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_stat_replication/9.2/metric.sql" postgres |
| 79 | +su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_table_bloat_approx/9.5/metric.sql" postgres |
| 80 | +su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_table_bloat_approx_sql/12/metric.sql" postgres |
| 81 | +su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_wal_size/10/metric.sql" postgres |
| 82 | +su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_psutil_cpu/9.1/metric.sql" postgres |
| 83 | +su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_psutil_mem/9.1/metric.sql" postgres |
| 84 | +su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_psutil_disk/9.1/metric.sql" postgres |
| 85 | +su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_psutil_disk_io_total/9.1/metric.sql" postgres |
| 86 | +su -c "psql -d pgwatch2 -c 'create extension pg_qualstats'" postgres |
| 87 | + |
| 88 | +if [ -n "$PW2_TESTDB" ] ; then |
| 89 | + su -c "psql -d pgwatch2 -f /pgwatch2/bootstrap/insert_test_monitored_db.sql" postgres |
| 90 | +fi |
| 91 | + |
| 92 | +touch /pgwatch2/persistent-config/db-bootstrap-done-marker |
| 93 | + |
| 94 | +pg_ctlcluster 15 main stop -- --wait |
| 95 | + |
| 96 | +fi |
| 97 | + |
| 98 | +sleep 1 |
| 99 | + |
| 100 | +exec /usr/local/bin/supervisord --configuration=/etc/supervisor/supervisord.conf --nodaemon |
0 commit comments