-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathst2bootstrap-deb.template.sh
417 lines (347 loc) · 14.6 KB
/
st2bootstrap-deb.template.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
set -eu
HUBOT_ADAPTER='slack'
HUBOT_SLACK_TOKEN=${HUBOT_SLACK_TOKEN:-''}
VERSION=''
RELEASE='stable'
REPO_TYPE=''
REPO_PREFIX=''
ST2_PKG_VERSION=''
ST2WEB_PKG_VERSION=''
ST2CHATOPS_PKG_VERSION=''
DEV_BUILD=''
USERNAME=''
PASSWORD=''
U16_ADD_INSECURE_PY3_PPA=0
SUBTYPE=`lsb_release -cs`
if [[ "$SUBTYPE" != 'xenial' && "$SUBTYPE" != 'focal' && "$SUBTYPE" != 'bionic' ]]; then
echo "Unsupported ubuntu codename ${SUBTYPE}. Please use 16.04 (xenial) or Ubuntu 18.04 (bionic) or Ubuntu 20.04 (focal) as base system!"
exit 2
fi
setup_args() {
for i in "$@"
do
case $i in
-v=*|--version=*)
VERSION="${i#*=}"
shift
;;
-s|--stable)
RELEASE=stable
shift
;;
-u|--unstable)
RELEASE=unstable
shift
;;
--staging)
REPO_TYPE='staging'
shift
;;
--dev=*)
DEV_BUILD="${i#*=}"
shift
;;
--user=*)
USERNAME="${i#*=}"
shift
;;
--password=*)
PASSWORD="${i#*=}"
shift
;;
# Provide flag to enable installing Python3 from 3rd party insecure PPA for Ubuntu Xenial
# TODO: Remove once Ubuntu Xenial is dropped
--u16-add-insecure-py3-ppa)
U16_ADD_INSECURE_PY3_PPA=1
shift
;;
*)
# unknown option
;;
esac
done
if [[ "$REPO_TYPE" != '' ]]; then
REPO_PREFIX="${REPO_TYPE}-"
fi
if [[ "$VERSION" != '' ]]; then
if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] && [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+dev$ ]]; then
echo "$VERSION does not match supported formats x.y.z or x.ydev"
exit 1
fi
if [[ "$VERSION" =~ ^[0-9]+\.[0-9]+dev$ ]]; then
echo "You're requesting a dev version! Switching to unstable!"
RELEASE='unstable'
fi
fi
echo "########################################################"
echo " Installing StackStorm $RELEASE $VERSION "
echo "########################################################"
if [ "$REPO_TYPE" == "staging" ]; then
printf "\n\n"
echo "################################################################"
echo "### Installing from staging repos!!! USE AT YOUR OWN RISK!!! ###"
echo "################################################################"
fi
if [ "$DEV_BUILD" != '' ]; then
printf "\n\n"
echo "###############################################################################"
echo "### Installing from dev build artifacts!!! REALLY, ANYTHING COULD HAPPEN!!! ###"
echo "###############################################################################"
fi
if [[ "$USERNAME" = '' || "$PASSWORD" = '' ]]; then
echo "Let's set StackStorm admin credentials."
echo "You can also use \"--user\" and \"--password\" for unattended installation."
echo "Press \"ENTER\" to continue or \"CTRL+C\" to exit/abort"
read -e -p "Admin username: " -i "st2admin" USERNAME
read -e -s -p "Password: " PASSWORD
if [ "${PASSWORD}" = '' ]; then
echo "Password cannot be empty."
exit 1
fi
fi
# Python 3.6 package is not available in Ubuntu Xenial
# Installer can add it via 3rd party PPA based on user agreement
if [[ "$SUBTYPE" = 'xenial' ]]; then
sudo apt-get update > /dev/null 2>/dev/null
# check if python3.6 is available
if (! apt-cache show python3.6 2> /dev/null | grep 'Package:' > /dev/null); then
if [[ "$U16_ADD_INSECURE_PY3_PPA" = "0" ]]; then
echo ""
echo "WARNING!"
echo "The python3.6 package is a required dependency for the StackStorm st2 package but that is not installable from any of the default Ubuntu 16.04 repositories."
echo "We recommend switching to Ubuntu 18.04 LTS (Bionic) as a base OS. Support for Ubuntu 16.04 will be removed with future StackStorm versions."
echo ""
echo "Alternatively we'll try to add python3.6 from the 3rd party 'deadsnakes' repository: https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa."
echo ""
echo "You can provide the following flag os use python3.6 from the 'deadsnakes'repository: --u16-add-insecure-py3-ppa"
echo ""
echo "By using the flag you are aware of the support and security risks associated with using unofficial 3rd party PPA repository, and you understand that StackStorm does NOT provide ANY support for python3.6 packages on Ubuntu 16.04."
echo ""
exit 1
else
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F23C5A6CF475977595C89F51BA6932366A755776
echo "deb http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial main" | sudo tee /etc/apt/sources.list.d/deadsnakes-ubuntu-ppa-xenial.list
fi
fi
fi
}
# include:includes/common.sh
install_st2_dependencies() {
# Silence debconf prompt, raised during some dep installations. This will be passed to sudo via 'env_keep'.
export DEBIAN_FRONTEND=noninteractive
sudo apt-get update
# Note: gnupg-curl is needed to be able to use https transport when fetching keys
if [[ "$SUBTYPE" = 'xenial' ]]; then
sudo apt-get install -y gnupg-curl
fi
sudo apt-get install -y curl
# Various other dependencies needed by st2 and installer script
sudo apt-get install -y crudini
}
install_rabbitmq() {
# install RabbitMQ
sudo apt-get install -y rabbitmq-server
sudo rabbitmqctl add_user stackstorm "${ST2_RABBITMQ_PASSWORD}"
sudo rabbitmqctl delete_user guest
sudo rabbitmqctl set_user_tags stackstorm administrator
sudo rabbitmqctl set_permissions -p / stackstorm ".*" ".*" ".*"
# Configure RabbitMQ to listen on localhost only
sudo sh -c 'echo "RABBITMQ_NODE_IP_ADDRESS=127.0.0.1" >> /etc/rabbitmq/rabbitmq-env.conf'
sudo systemctl restart rabbitmq-server
}
install_mongodb() {
# Add key and repo for the latest stable MongoDB 4.0
wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -
echo "deb http://repo.mongodb.org/apt/ubuntu ${SUBTYPE}/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
# Install MongoDB 4.0
sudo apt-get update
sudo apt-get install -y mongodb-org
# Configure MongoDB to listen on localhost only
sudo sed -i -e "s#bindIp:.*#bindIp: 127.0.0.1#g" /etc/mongod.conf
# Enable and restart
sudo systemctl enable mongod
sudo systemctl start mongod
sleep 5
# Create admin user and user used by StackStorm (MongoDB needs to be running)
# NOTE: mongo shell will automatically exit when piping from stdin. There is
# no need to put quit(); at the end. This way last command exit code will be
# correctly preserved and install script will correctly fail and abort if this
# command fails.
mongo <<EOF
use admin;
db.createUser({
user: "admin",
pwd: "${ST2_MONGODB_PASSWORD}",
roles: [
{ role: "userAdminAnyDatabase", db: "admin" }
]
});
EOF
mongo <<EOF
use st2;
db.createUser({
user: "stackstorm",
pwd: "${ST2_MONGODB_PASSWORD}",
roles: [
{ role: "readWrite", db: "st2" }
]
});
EOF
# Require authentication to be able to acccess the database
sudo sh -c 'printf "security:\n authorization: enabled\n" >> /etc/mongod.conf'
# MongoDB needs to be restarted after enabling auth
sudo systemctl restart mongod
}
install_redis() {
# Install Redis Server. By default, redis only listen on localhost only.
sudo apt-get install -y redis-server
}
get_full_pkg_versions() {
if [[ "$VERSION" != '' ]];
then
local ST2_VER=$(apt-cache show st2 | grep Version | awk '{print $2}' | grep ^${VERSION//./\\.} | sort --version-sort | tail -n 1)
if [[ -z "$ST2_VER" ]]; then
echo "Could not find requested version of StackStorm!!!"
sudo apt-cache policy st2
exit 3
fi
local ST2WEB_VER=$(apt-cache show st2web | grep Version | awk '{print $2}' | grep ^${VERSION//./\\.} | sort --version-sort | tail -n 1)
if [[ -z "$ST2WEB_VER" ]]; then
echo "Could not find requested version of st2web."
sudo apt-cache policy st2web
exit 3
fi
local ST2CHATOPS_VER=$(apt-cache show st2chatops | grep Version | awk '{print $2}' | grep ^${VERSION//./\\.} | sort --version-sort | tail -n 1)
if [[ -z "$ST2CHATOPS_VER" ]]; then
echo "Could not find requested version of st2chatops."
sudo apt-cache policy st2chatops
exit 3
fi
ST2_PKG_VERSION="=${ST2_VER}"
ST2WEB_PKG_VERSION="=${ST2WEB_VER}"
ST2CHATOPS_PKG_VERSION="=${ST2CHATOPS_VER}"
echo "##########################################################"
echo "#### Following versions of packages will be installed ####"
echo "st2${ST2_PKG_VERSION}"
echo "st2web${ST2WEB_PKG_VERSION}"
echo "st2chatops${ST2CHATOPS_PKG_VERSION}"
echo "##########################################################"
fi
}
install_st2() {
# Following script adds a repo file, registers gpg key and runs apt-get update
curl -sL https://packagecloud.io/install/repositories/StackStorm/${REPO_PREFIX}${RELEASE}/script.deb.sh | sudo bash
if [[ "$DEV_BUILD" = '' ]]; then
STEP="Get package versions" && get_full_pkg_versions && STEP="Install st2"
sudo apt-get install -y st2${ST2_PKG_VERSION}
else
sudo apt-get install -y jq
PACKAGE_URL=$(get_package_url "${DEV_BUILD}" "${SUBTYPE}" "st2_.*.deb")
PACKAGE_FILENAME="$(basename ${PACKAGE_URL})"
curl -sSL -k -o ${PACKAGE_FILENAME} ${PACKAGE_URL}
sudo dpkg -i --force-depends ${PACKAGE_FILENAME}
sudo apt-get install -yf
rm ${PACKAGE_FILENAME}
fi
# Configure [database] section in st2.conf (username password for MongoDB access)
sudo crudini --set /etc/st2/st2.conf database username "stackstorm"
sudo crudini --set /etc/st2/st2.conf database password "${ST2_MONGODB_PASSWORD}"
# Configure [messaging] section in st2.conf (username password for RabbitMQ access)
AMQP="amqp://stackstorm:[email protected]:5672"
sudo crudini --set /etc/st2/st2.conf messaging url "${AMQP}"
# Configure [coordination] section in st2.conf (url for Redis access)
sudo crudini --set /etc/st2/st2.conf coordination url "redis://127.0.0.1:6379"
sudo st2ctl start
sudo st2ctl reload --register-all
}
configure_st2_authentication() {
# Install htpasswd tool for editing ini files
sudo apt-get install -y apache2-utils
# Create a user record in a password file.
sudo echo "${PASSWORD}" | sudo htpasswd -i /etc/st2/htpasswd $USERNAME
# Configure [auth] section in st2.conf
sudo crudini --set /etc/st2/st2.conf auth enable 'True'
sudo crudini --set /etc/st2/st2.conf auth backend 'flat_file'
sudo crudini --set /etc/st2/st2.conf auth backend_kwargs '{"file_path": "/etc/st2/htpasswd"}'
sudo st2ctl restart-component st2auth
sudo st2ctl restart-component st2api
sudo st2ctl restart-component st2stream
}
install_st2web() {
# Add key and repo for the latest stable nginx
sudo apt-key adv --fetch-keys http://nginx.org/keys/nginx_signing.key
sudo sh -c "cat <<EOT > /etc/apt/sources.list.d/nginx.list
deb http://nginx.org/packages/ubuntu/ ${SUBTYPE} nginx
deb-src http://nginx.org/packages/ubuntu/ ${SUBTYPE} nginx
EOT"
sudo apt-get update
# Install st2web and nginx
sudo apt-get install -y st2web${ST2WEB_PKG_VERSION} nginx
# Generate self-signed certificate or place your existing certificate under /etc/ssl/st2
sudo mkdir -p /etc/ssl/st2
sudo openssl req -x509 -newkey rsa:2048 -keyout /etc/ssl/st2/st2.key -out /etc/ssl/st2/st2.crt \
-days 365 -nodes -subj "/C=US/ST=California/L=Palo Alto/O=StackStorm/OU=Information \
Technology/CN=$(hostname)"
# Remove default site, if present
sudo rm -f /etc/nginx/conf.d/default.conf
# Copy and enable StackStorm's supplied config file
sudo cp /usr/share/doc/st2/conf/nginx/st2.conf /etc/nginx/conf.d/
sudo service nginx restart
}
install_st2chatops() {
# Add NodeJS 10 repo
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
# Install st2chatops
sudo apt-get install -y st2chatops${ST2CHATOPS_PKG_VERSION}
}
configure_st2chatops() {
# set API keys. This should work since CLI is configuered already.
ST2_API_KEY=`st2 apikey create -k`
sudo sed -i -r "s/^(export ST2_API_KEY.).*/\1$ST2_API_KEY/" /opt/stackstorm/chatops/st2chatops.env
sudo sed -i -r "s/^(export ST2_AUTH_URL.).*/# &/" /opt/stackstorm/chatops/st2chatops.env
sudo sed -i -r "s/^(export ST2_AUTH_USERNAME.).*/# &/" /opt/stackstorm/chatops/st2chatops.env
sudo sed -i -r "s/^(export ST2_AUTH_PASSWORD.).*/# &/" /opt/stackstorm/chatops/st2chatops.env
# Setup adapter
if [[ "$HUBOT_ADAPTER"="slack" ]] && [[ ! -z "$HUBOT_SLACK_TOKEN" ]]
then
sudo sed -i -r "s/^# (export HUBOT_ADAPTER=slack)/\1/" /opt/stackstorm/chatops/st2chatops.env
sudo sed -i -r "s/^# (export HUBOT_SLACK_TOKEN.).*/\1/" /opt/stackstorm/chatops/st2chatops.env
sudo sed -i -r "s/^(export HUBOT_ADAPTER.).*/\1$HUBOT_ADAPTER/" /opt/stackstorm/chatops/st2chatops.env
sudo sed -i -r "s/^(export HUBOT_SLACK_TOKEN.).*/\1$HUBOT_SLACK_TOKEN/" /opt/stackstorm/chatops/st2chatops.env
sudo service st2chatops restart
else
echo "####################### WARNING ########################"
echo "######## Chatops requires manual configuration #########"
echo "Edit /opt/stackstorm/chatops/st2chatops.env to specify "
echo "the adapter and settings hubot should use to connect to "
echo "the chat you're using. Don't forget to start the service"
echo "afterwards:"
echo ""
echo " $ sudo service st2chatops restart"
echo ""
echo "For more information, please refer to documentation at "
echo "https://docs.stackstorm.com/install/deb.html#setup-chatops"
echo "########################################################"
fi
}
## Let's do this!
trap 'fail' EXIT
STEP="Setup args" && setup_args $@
STEP="Check TCP ports and MongoDB storage requirements" && check_st2_host_dependencies
STEP="Generate random password" && generate_random_passwords
STEP="Configure Proxy" && configure_proxy
STEP="Install st2 dependencies" && install_st2_dependencies
STEP="Install st2 dependencies (RabbitMQ)" && install_rabbitmq
STEP="Install st2 dependencies (MongoDB)" && install_mongodb
STEP="Install st2 dependencies (Redis)" && install_redis
STEP="Install st2" && install_st2
STEP="Configure st2 user" && configure_st2_user
STEP="Configure st2 auth" && configure_st2_authentication
STEP="Configure st2 CLI config" && configure_st2_cli_config
STEP="Generate symmetric crypto key for datastore" && generate_symmetric_crypto_key_for_datastore
STEP="Verify st2" && verify_st2
STEP="Install st2web" && install_st2web
STEP="Install st2chatops" && install_st2chatops
STEP="Configure st2chatops" && configure_st2chatops
trap - EXIT
ok_message