forked from aquametalabs/aquameta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·352 lines (273 loc) · 12.1 KB
/
install.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/bin/bash
# exit on failure
set -e
set -o pipefail
#############################################################################
#
# Aquameta Installer Script
#
# Does the following:
# - install apt packages
# - install python packages
# - install postgresql extensions
#
#############################################################################
# prompting and sanity checking
echo " __ "
echo "_____ ________ _______ _____ _____/ |______ "
echo "\__ \ / ____/ | \__ \ / \_/ __ \ __\__ \ "
echo " / __ \< <_| | | // __ \| Y Y \ ___/| | / __ \_"
echo "(____ /\__ |____/(____ /__|_| /\___ >__| (____ /"
echo " \/ |__| \/ \/ \/ \/ "
echo " [ version 0.2.0 - base install ]"
echo ""
echo " OBLIGATORY WARNING:"
echo ""
echo " This code is highly experimental and should "
echo " NOT be run in a production environment."
echo " You have been warned."
echo " ❤ MGMT."
read -p "Continue? [y/N]" -n 1 -r
echo # (optional) move to a new line
if ! [[ $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
# set working directory and destination directory
SRC="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# make sure we're running as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
#############################################################################
# prompt for $DEST location
#############################################################################
read -p "Installation directory [$SRC]: " DEST
DEST=${DEST:-$SRC}
#############################################################################
# apt packages
#############################################################################
echo "Installing dependencies via apt..."
echo "Please choose:"
echo " a) Debian 9 apt repository"
echo " b) Ubuntu 18 apt repository"
REPLY=
while ! [[ $REPLY =~ ^[aAbB]$ ]]
do
echo
read -p "[a/b] " -n 1 -r
done
if [[ $REPLY =~ ^[aA]$ ]]
then
apt-get install -y software-properties-common
add-apt-repository "deb http://deb.debian.org/debian/ stretch main contrib non-free"
add-apt-repository "deb http://security.debian.org/ stretch/updates main contrib non-free"
add-apt-repository "deb http://deb.debian.org/debian/ stretch-updates main contrib non-free"
else
apt-get install -y software-properties-common
add-apt-repository universe
fi
# add postgresql official repository
sudo apt-get install -y wget ca-certificates
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list
# update
apt-get update -y
# install required packages
DEBIAN_FRONTEND=nointeractive \
apt-get install -y postgresql-11 postgresql-11-python-multicorn \
postgresql-server-dev-11 postgresql-plpython-11 python-pip \
python-werkzeug python-psycopg2 nginx sudo sendmail \
fuse dnsutils \
libssl-dev libpcre3 libpcre3-dev \
git vim tmux sudo
#############################################################################
# plv8 binaries
#############################################################################
DEBIAN_FRONTEND=nointeractive \
apt install -y libc++-dev
cd $SRC
git clone https://github.com/aquametalabs/plv8-binaries.git
cd plv8-binaries/postgresql-11/plv8-2.3.11
./install-binaries-huzzah.sh
cd $SRC
rm -rf plv8-binaries
ldconfig
#############################################################################
# sendmail
#############################################################################
# locale-gen "en_US.UTF-8" && dpkg-reconfigure locales
# echo `tail -1 /etc/hosts`.localdomain >> /etc/hosts
#############################################################################
# python packages
#############################################################################
echo "Installing core python packages..."
pip install requests fusepy
# filesystem_fdw
cd $SRC/src/py-package/filesystem_fdw
sudo -H pip install --upgrade --force-reinstall .
# aquameta-endpoint
cd $SRC/src/py-package/uwsgi-endpoint
sudo -H pip install --upgrade --force-reinstall .
#############################################################################
# aquameta postgresql extensions
#############################################################################
# install extensions into PostgreSQL's extensions/ directory
echo "Building core PostgreSQL extensions..."
cd $SRC/src/pg-extension/pg_catalog_get_defs && make && make install
cd $SRC/src/pg-extension/meta && make && make install
cd $SRC/src/pg-extension/bundle && make && make install
cd $SRC/src/pg-extension/filesystem && make && make install
cd $SRC/src/pg-extension/email && make && make install
cd $SRC/src/pg-extension/event && make && make install
cd $SRC/src/pg-extension/endpoint && make && make install
cd $SRC/src/pg-extension/widget && make && make install
cd $SRC/src/pg-extension/semantics && make && make install
#############################################################################
# build the aquameta database
#############################################################################
echo "Configuring PostgreSQL..."
# enable peer authentication
sudo sed -i "s/^local all.*$/local all all trust/" /etc/postgresql/11/main/pg_hba.conf
systemctl restart postgresql.service
# create aquameta database
sudo -u postgres createdb aquameta
sudo -u postgres psql -c "alter database aquameta set bytea_output to 'hex'" aquameta
# create dependency extensions required by aquameta
echo "Installing dependency extensions..."
sudo -u postgres psql -c "create extension if not exists plpythonu" aquameta
sudo -u postgres psql -c "create extension if not exists multicorn schema public" aquameta
sudo -u postgres psql -c "create extension if not exists hstore schema public" aquameta
sudo -u postgres psql -c "create extension if not exists hstore_plpythonu schema public" aquameta
sudo -u postgres psql -c "create extension if not exists dblink schema public" aquameta
sudo -u postgres psql -c "create extension if not exists \"uuid-ossp\"" aquameta
sudo -u postgres psql -c "create extension if not exists pgcrypto schema public" aquameta
sudo -u postgres psql -c "create extension if not exists postgres_fdw" aquameta
sudo -u postgres psql -c "create extension if not exists plv8" aquameta
# create aquameta core extensions
echo "Installing core Aquameta extensions..."
sudo -u postgres psql -c "create extension pg_catalog_get_defs schema pg_catalog" aquameta
sudo -u postgres psql -c "create extension meta" aquameta
sudo -u postgres psql -c "create extension bundle" aquameta
sudo -u postgres psql -c "create extension filesystem" aquameta
sudo -u postgres psql -c "create extension email" aquameta
sudo -u postgres psql -c "create extension event" aquameta
sudo -u postgres psql -c "create extension endpoint" aquameta
sudo -u postgres psql -c "create extension widget" aquameta
sudo -u postgres psql -c "create extension semantics" aquameta
sudo -u postgres psql -f $SRC/src/sql/ide/000-ide.sql aquameta
#############################################################################
# install and checkout enabled bundles
#############################################################################
echo "Installing core bundles..."
echo "Please choose:"
echo " a) Hub installl -- download bundles (and future updates)"
echo " from the Aquameta bundle hub."
echo " b) Offline install -- do not connect to the hub, install"
echo " from source only."
REPLY=
while ! [[ $REPLY =~ ^[aAbB]$ ]]
do
echo
read -p "[A/b] " -n 1 -r
done
if [[ $REPLY =~ ^[bB]$ ]]
then
if [ "$DEST" != "$SRC" ]; then
mkdir --parents $DEST
cp -R $SRC/bundles-available $DEST
cp -R $SRC/bundles-enabled $DEST
fi
chown -R postgres:postgres $DEST/bundles-available
chown -R postgres:postgres $DEST/bundles-enabled
echo "Loading bundles-enabled/*/*.csv ..."
for D in `find $DEST/bundles-enabled/* \( -type l -o -type d \)`
do
sudo -u postgres psql -c "select bundle.bundle_import_csv('$D')" aquameta
done
echo "Checking out head commit of every bundle ..."
sudo -u postgres psql -c "select bundle.checkout(c.id) from bundle.commit c join bundle.bundle b on b.head_commit_id = c.id;" aquameta
else
for REMOTE in `find $SRC/src/remotes/*.sql -type f`
do
sudo -u postgres psql aquameta -f $REMOTE
done
sudo -u postgres psql aquameta -c "select bundle.remote_mount(id) from bundle.remote_database"
sudo -u postgres psql aquameta -c "select bundle.remote_pull_bundle(r.id, b.id) from bundle.remote_database r, hub.bundle b where b.name != 'org.aquameta.core.bundle'"
echo "Checking out head commit of every bundle ..."
sudo -u postgres psql aquameta -c "select bundle.checkout(c.id) from bundle.commit c join bundle.bundle b on b.head_commit_id = c.id;"
fi
#############################################################################
# configure uwsgi and start the service
#############################################################################
echo "Setting up uWSGI service..."
mkdir -p /etc/aquameta
# copy service file into /etc/systemd/system
cp $SRC/src/py-package/uwsgi-endpoint/aquameta.emperor.uwsgi.service /etc/systemd/system
# copy uwsgi .ini file into /etc/uwsgi/uwsgi-emperor.ini
cp $SRC/src/py-package/uwsgi-endpoint/uwsgi-emperor.ini /etc/aquameta
systemctl enable aquameta.emperor.uwsgi.service
systemctl restart aquameta.emperor.uwsgi.service
#############################################################################
# configure nginx and restart the service
#############################################################################
echo "Setting up nginx..."
cp $SRC/src/py-package/uwsgi-endpoint/nginx/aquameta_endpoint.conf /etc/nginx/sites-available
cd /etc/nginx/sites-enabled
rm -f ./default
ln -sf ../sites-available/aquameta_endpoint.conf
systemctl restart nginx
#############################################################################
# grant default permissions for 'anonymous' and 'user' roles
#############################################################################
echo ""
echo "New User Registration Scheme"
echo "----------------------------"
echo "Please select a security scheme:"
echo "a) PRIVATE - No anonymous access, no anonymous user registration"
echo "b) OPEN REGISTRATION - Anonymous users may register for an account and read limited data"
REPLY=
until [[ $REPLY =~ ^[AaBb]$ ]]; do
read -p "Choice? [a/B] " -n 1 -r
echo
done
sudo -u postgres psql -f $SRC/src/privileges/000-general.sql aquameta
if [[ $REPLY =~ ^[Aa]$ ]]; then
echo "Installing PRIVATE security scheme..."
sudo -u postgres psql -f $SRC/src/privileges/001-anonymous.sql aquameta
else if [[ $REPLY =~ ^[Bb]$ ]]; then
echo "Installing OPEN REGISTRATION scheme..."
sudo -u postgres psql -f $SRC/src/privileges/001-anonymous-register.sql aquameta
fi
fi
sudo -u postgres psql -f $SRC/src/privileges/002-user.sql aquameta
#############################################################################
# setup aquameta superuser
#############################################################################
echo "Superuser Registration"
echo "----------------------"
echo "Enter the name, email and password of the user you'd like to setup as superuser:"
read -p "Full Name: " NAME
read -p "Email Address: " EMAIL
read -p "PostgreSQL Username [$(logname)]: " ROLE
if [[ $ROLE = '' ]]
then
ROLE=$(logname)
fi
read -s -p "Password: " PASSWORD
echo ""
echo "Creating superuser...."
REG_COMMAND="select endpoint.register_superuser('$EMAIL', '$PASSWORD', '$NAME', '$ROLE')"
sudo -u postgres psql -c "$REG_COMMAND" aquameta
#############################################################################
# finished!
#############################################################################
MACHINE_IP=`hostname --ip-address|cut -d ' ' -f1`
EXTERNAL_IP=`dig +short myip.opendns.com @resolver1.opendns.com`
echo ""
echo "Aquameta was successfully installed. Next, login and configure your installation:"
echo ""
echo "Localhost link: http://localhost/"
echo "Machine link: http://$MACHINE_IP/"
echo "External link: http://$EXTERNAL_IP/"