# PoWA for PostgreSQL
# https://powa.readthedocs.io

##############################################################################
# Install on PostgreSQL Servers
# Using upstream postresql's repos, not Debian's repos.
apt install postgresql-10-powa postgresql-10-pg-qualstats postgresql-10-pg-stat-kcache postgresql-10-hypopg

# Add permissions for PoWA node

# Configure postgres:
vim /etc/postgresql/10/main/postgresql.conf

# Change:
# shared_preload_libraries to (keeping citus):
shared_preload_libraries='citus,pg_stat_statements,powa,pg_stat_kcache,pg_qualstats'
# Enable:
track_io_timing = on

# Create databases, run on each pg1 pg2 pg3 pg4 server:
psql
CREATE DATABASE powa ;
\c powa
CREATE EXTENSION pg_stat_statements;
CREATE EXTENSION btree_gist;
CREATE EXTENSION powa;
CREATE EXTENSION pg_qualstats;
CREATE EXTENSION pg_stat_kcache;
CREATE EXTENSION hypopg;
CREATE ROLE powa SUPERUSER LOGIN PASSWORD 'password' ;

##############################################################################
# Install on Web Node

# Set up Debian Stretch node
# Install postgres repos:
echo 'deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main' > /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
apt update
apt dist-upgrade
apt install postgresql-client-10

# There isn't a package of powa-web for Debian.
# Install dependencies:
apt install python python-psycopg2 python-sqlalchemy python-tornado

wget -O powa-web-3.1.4.tar.gz https://github.com/powa-team/powa-web/archive/3.1.4.tar.gz
tar -zxvf powa-web-3.1.4.tar.gz
cd powa-web-3.1.4
# cp ./powa-web.conf-dist ./powa-web.conf
cat > powa-web.conf <<EOF
servers={
  'pg1': {
    'host': '10.52.1.91',
    'port': '5432',
    'database': 'powa',
    'query': {'client_encoding': 'utf8'}
  },
  'pg2': {
    'host': '10.52.1.92',
    'port': '5432',
    'database': 'powa',
    'query': {'client_encoding': 'utf8'}
  },
  'pg3': {
    'host': '10.52.1.93',
    'port': '5432',
    'database': 'powa',
    'query': {'client_encoding': 'utf8'}
  },
  'pg4': {
    'host': '10.52.1.94',
    'port': '5432',
    'database': 'powa',
    'query': {'client_encoding': 'utf8'}
  }
}
cookie_secret="password"
EOF

./powa-web

# XXX setup user, startup scripts, etc.


# From git version: XXX fail:
# Unable to connect: powa-archivist version 3.1.X does not match powa-web version 3.2.X
git clone https://github.com/powa-team/powa-web
# Last version from git to work with 3.1.X:
cd powa-web
git checkout a14a85b3b29ae1d68c20e7394e4214d318a948f3
./powa-web

##############################################################################
# Admin Web Client

# On workstation create tunnel:
ssh -N -C -L 58888:localhost:8888 powa

# On workstation, in browser, login user 'powa' with powa's postgres password.
http://localhost:58888/
##############################################################################
