-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall
executable file
·299 lines (245 loc) · 8.58 KB
/
install
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
#!/usr/bin/env bash
set -e
function exists() {
which $1 > /dev/null 2>&1
}
function fail() {
echo $1
echo "Please contact Lamassu support ([email protected]), including "
echo "command output and lamassu-debug.log file"
exit 4
}
function upstart() {
package="$1"
cat > "/etc/init/$package"".conf" <<EOF
exec $package --key /root/$package\.key --cert /root/$package\.crt
respawn
start on startup
env DATABASE_URL=postgres://lamassu:$password@localhost/lamassu
env NODE_ENV=production
chdir $(npm -g explore $package pwd)
EOF
}
function initscript() {
package="$1"
cat > "/etc/init.d/$package" <<EOF
#!/bin/sh
### BEGIN INIT INFO
# Provides: $package
# Required-Start: \$remote_fs \$network postgresql
# Required-Stop: \$remote_fs \$network postgresql
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: $package
# Description: Lamassu.is Operator Server
### END INIT INFO
export DATABASE_URL=postgres://lamassu:$password@localhost/lamassu
export NODE_ENV=production
export LAMASSU_ENV=production
#For debugging, uncomment the following lines:
#export NODE_ENV=debug
#export LAMASSU_ENV=debug
#CLOSE=--no-close
PIDFILE=/var/run/$package.pid
PATH=\$PATH:/usr/local/bin
DAEMON=/usr/local/bin/$package
DAEMON_OPTS="--key $userhome/$package.key --cert $userhome/$package.crt"
NAME=node
USER=lamassu
GROUP=lamassu
[ -x \$DAEMON ] || exit 0
case "\$1" in
start)
echo -n "Starting $package: "
start-stop-daemon --start --quiet --pidfile \$PIDFILE --name \$NAME --user \$USER \$CLOSE --chuid \$USER:\$GROUP --background --make-pidfile --chdir $(npm -g explore $package pwd) --exec \$DAEMON -- \$DAEMON_OPTS
echo "done."
;;
stop)
echo -n "Stopping $package: "
start-stop-daemon --stop --quiet --pidfile \$PIDFILE --name \$NAME --user \$USER
echo "done."
;;
restart|reload|force-reload)
\$0 stop
\$0 start
;;
esac
EOF
chmod 0700 "/etc/init.d/$package"
}
function prompt() {
question="$1"
default="$2"
silent="$3"
var="$4"
echo -n "$question: "
if [ ! -z "$default" ]; then
echo -n "($default) "
fi
if [ "$silent" = true ]; then
read -s result
echo
else
read result
fi
if [ -z "$result" ]; then
export "$var=$default"
else
export "$var=$result"
fi
}
echo
echo "Hello! This script will install Lamassu Remote Server."
echo
if [ "$(whoami)" != "root" ]; then
echo "This script has to be run as \`root\`"
exit 3
fi
installuser="root"
debug="lamassu-debug.log"
prefix="/usr/local"
arch=$(uname -m)
case $arch in
x86_64)
arch=x64;;
i686)
arch=x86;;
esac
node="http://nodejs.org/dist/v0.10.32/node-v0.10.32-linux-$arch.tar.gz"
# First detect our package manager. Fail early if we don't support it.
update=""
install=""
rm -f lamassu-debug.log
if exists apt-get; then
update="apt-get update -y"
upgrade="apt-get upgrade -y"
install="
apt-get update -y &&
apt-get install -y postgresql postgresql-contrib libpq-dev git build-essential python curl &&
/etc/init.d/postgresql start
"
fi
if [ -z "$install" ]; then
echo "Your package manager isn't supported"
echo "This script currently supports apt-get"
exit 1
fi
# Then detect our service management system. Also fail early if we don't
# support it.
# Remark: `upstart` appears to be going away from Ubuntu in lieu of `systemd`.
service=""
if exists start && [ -d "/etc/init" ]; then
service="upstart"
fi
if exists update-rc.d; then
service="debian_sysv-rc"
fi
if [ -z "$service" ]; then
echo "Your service manager isn't supported"
echo "This script currently supports upstart and debian_sysv-rc"
exit 2
fi
echo "We'll now ask you a couple of questions in order to configure "
echo "this installation."
echo
echo "Pick your admin panel credentials:"
prompt "Username" "admin" false admin_username
prompt "Password" "" true admin_password
if [ -z "$admin_password" ]; then
fail "Admin password cannot be blank"
fi
echo
echo "Updating your system (this might take a while)..."
locale-gen en_US.UTF-8 >> $debug 2>&1
export LANGUAGE="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
echo 'LANGUAGE="en_US.UTF-8"' >> /etc/default/locale
echo 'LC_ALL="en_US.UTF-8"' >> /etc/default/locale
eval $update >> $debug 2>&1
eval $upgrade >> $debug 2>&1
[ $? -ne 0 ] && fail "Updating your system failed"
# Install PostgreSQL with the package manager we found.
echo
echo "Installing PostgreSQL (this might take a while)..."
eval $install >> $debug 2>&1
[ $? -ne 0 ] && fail "PostgreSQL installation failed"
# Set up users and databases in Postgres.
# Remark: do we want lamassu to be a super user?
password=$(openssl rand -hex 32)
su - postgres >> $debug 2>&1 <<EOF
dropdb lamassu
dropuser lamassu
psql -c "CREATE ROLE lamassu WITH LOGIN SUPERUSER PASSWORD '$password';"
createdb -O lamassu lamassu
EOF
echo "{\"postgresql\":\"postgres://lamassu:$password@localhost/lamassu\"}" > /etc/lamassu.json
echo "Fetching and installing node.js..."
# Install node in $prefix
curl "$node" | tar -C"$prefix" --strip-components=1 -zxf-
[ $? -ne 0 ] && fail "Node.js installation failed"
echo "Installing Lamassu stack (this might take a while)..."
# Install Lamassu stack
npm -g install lamassu-server@two-way lamassu-admin [email protected] migrate bunyan >> $debug 2>&1
[ $? -ne 0 ] && fail "Lamassu stack installation failed"
# Bootstrap lamassu database
# We also have to use `-h 127.0.0.1` because the default type of authentication
# for `local` connection is `peer`, which checks username of the user running
# `psql` command.
echo "Bootstrapping PostgreSQL..."
SQL_FILE="$(npm -g root)/lamassu-scripts/database/lamassu.sql"
if [ -x /usr/sbin/adduser ]; then
# Add lamassu system account to run daemon and connect to DB as
echo "Adding lamassu user..."
/usr/sbin/adduser lamassu --disabled-password --disabled-login >> $debug 2>&1 < /dev/null || true
installuser="lamassu"
PGPASSWORD="$password" su -c "psql -h 127.0.0.1 lamassu lamassu" $installuser >> $debug 2>&1 < $SQL_FILE
[ $? -ne 0 ] && fail "Bootstrapping PostgreSQL failed"
else
PGPASSWORD="$password" psql -h 127.0.0.1 lamassu lamassu >> $debug 2>&1 < $SQL_FILE ||
( echo "Could not connect to PostgreSQL as lamassu user.\nTry adding the following line to the beginning of your pg_hba.conf:"
echo "host lamassu lamassu 127.0.0.1 255.255.255.255 trust"
fail "Bootstrapping PostgreSQL failed" )
fi
echo "Creating a Lamassu user account"
DATABASE_URL="postgres://lamassu:$password@localhost/lamassu" lamassu-useradd "$admin_username" "$admin_password"
[ $? -ne 0 ] && fail "Adding a user failed"
ip=$(ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}')
userhome=$(su - -c pwd $installuser)
# Generate SSL certificates for lamassu-server and lamassu-admin.
echo "Generating SSL certificates..."
openssl req -new -newkey rsa:8092 -days 9999 -nodes -x509 -subj "/C=US/ST=/L=/O=/CN=$ip:8081" -keyout $userhome/lamassu-admin.key -out $userhome/lamassu-admin.crt >> $debug 2>&1
[ $? -ne 0 ] && fail "Generating a certificate for lamassu-admin failed"
openssl req -new -newkey rsa:8092 -days 9999 -nodes -x509 -subj "/C=US/ST=/L=/O=/CN=$ip:3000" -keyout $userhome/lamassu-server.key -out $userhome/lamassu-server.crt >> $debug 2>&1
[ $? -ne 0 ] && fail "Generating a certificate for lamassu-server failed"
chown $installuser $userhome/{*.key,*.crt}
chmod 600 $userhome/{*.key,*.crt}
echo "Installing Lamassu services..."
if [ "$service" == "upstart" ]; then
upstart "lamassu-server"
upstart "lamassu-admin"
elif [ "$service" == "debian_sysv-rc" ]; then
initscript "lamassu-server"
initscript "lamassu-admin"
update-rc.d lamassu-server defaults || fail "Initscript enabling failed"
update-rc.d lamassu-admin defaults || fail "Initscript enabling failed"
fi
ORIG_PWD=$(pwd)
cd /usr/local/lib/node_modules/lamassu-server
/usr/local/bin/migrate
cd $ORIG_PWD
echo "Starting Lamassu services..."
if [ "$service" == "upstart" ]; then
start lamassu-admin >> $debug 2>&1
[ $? -ne 0 ] && fail "Starting lamassu-admin via $service failed"
start lamassu-server >> $debug 2>&1
[ $? -ne 0 ] && fail "Starting lamassu-server via $service failed"
elif [ "$service" == "debian_sysv-rc" ]; then
invoke-rc.d lamassu-admin start >> $debug 2>&1
[ $? -ne 0 ] && fail "Starting lamassu-admin via $service failed"
invoke-rc.d lamassu-server start >> $debug 2>&1
[ $? -ne 0 ] && fail "Starting lamassu-server via $service failed"
fi
echo
echo "Done! Now it's time to configure Lamassu stack."
echo "Open https://$ip:8081 in your browser to access "
echo "your admin panel."