-
Notifications
You must be signed in to change notification settings - Fork 5
/
bootstrap.sh
executable file
·466 lines (401 loc) · 16.5 KB
/
bootstrap.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/bin/bash
# Set this to 0 if you don't have (or don't want to use) sudo permissions
HAVESUDO=${HAVESUDO:=1}
# Setup a development environment for conductor, tim
# and aeolus-cli. Configure conductor to use an external
# imagefactory/deltacloud by setting env variables and
# oauth.json, below. Startup conductor on port 3000
export WORKDIR=${WORKDIR:=~/aeolus-workdir}
# Where the aeolus projects (conductor, aeolus-cli and tim)
# get checked out to
export FACTER_AEOLUS_WORKDIR=${FACTER_AEOLUS_WORKDIR:=$WORKDIR}
# Port to start up conductor on
export FACTER_CONDUCTOR_PORT=${FACTER_CONDUCTOR_PORT:=3000}
# Used specifically for the conductor/tim's callback_url that gets
# provided to imagefactory (in
# /conductor/src/config/initializers/tim.rb)
export FACTER_CONDUCTOR_HOSTNAME=${FACTER_CONDUCTOR_HOSTNAME:=`hostname`}
# RDBMS to use for the install (postgresql|sqlite)
export FACTER_RDBMS=${FACTER_RDBMS:=postgresql}
# If using postgresql, set some sane defaults if not present in environment
if [ "$FACTER_RDBMS" = "postgresql" ]; then
# assign a random database name if no database name provided by user
export FACTER_RDBMS_DBNAME=${FACTER_RDBMS_DBNAME:=conductor_$(
< /dev/urandom tr -dc a-z0-9 | head -c 4 )}
export FACTER_RDBMS_USERNAME=${FACTER_RDBMS_USERNAME:=$USER}
export FACTER_RDBMS_PASSWORD=${FACTER_RDBMS_PASSWORD:=v23zj59an}
fi
# If you want to use system ruby for the aeolus projects, do not
# define this env var. Otherwise, use (and install if necessary)
# specified ruby version locally in ~/.rbenv for $DEV_USERNAME
# export RBENV_VERSION=1.9.3-p362
# Set default Deltacloud, ImageFactory values
# (for RH network) if they're not already in the environment
export FACTER_DELTACLOUD_URL=${FACTER_DELTACLOUD_URL:=http://localhost:3002/api}
export FACTER_IMAGEFACTORY_URL=${FACTER_IMAGEFACTORY_URL:=http://localhost:8075/imagefactory}
# Create some default OAuth values
if [ "x$FACTER_OAUTH_JSON_FILE" = "x" ]; then
export FACTER_OAUTH_JSON_FILE=/tmp/oauth.json
if [ ! -f $FACTER_OAUTH_JSON_FILE ]; then
# The next command is more here for illustrative purposes and to
# allow bootstrap.sh to succeed. The values in oauth.json should
# correspond to existing credentials in an image factory and image
# warehouse install.
#
# Note that after bootstrap.sh runs (and your development is set
# up), you can always edit conductor/src/config/settings.yml and
# conductor/src/config/oauth.json to reflect updated image factory
# and image warehouse credentials.
echo -n '{"factory":{"consumer_secret":"ieqL8ojxPQBvKwCh3m36Fc6on4B+SHB/","consumer_key":"LfiaAIMFP0ASr3VGrbCDjQn1bQL81+SK"}}' > $FACTER_OAUTH_JSON_FILE
fi
fi
# Optional environment variables (sample values are given below)
#
# If the following env var is defined, checkout and start up
# deltacloud locally rather than use an existing installation.
# export SETUP_LOCAL_DELTACLOUD_RELEASE=release-1.0.5
# export SETUP_LOCAL_DELTACLOUD_PORT=3002
#
# _BRANCH variables below may be either branches, tags or commit hashes.
# Note that master is the default branch cloned from each of the three
# projects if a _BRANCH is not specified.
#
# export FACTER_AEOLUS_CLI_BRANCH=0.5.x
# export FACTER_CONDUCTOR_BRANCH=0.10.x
# export FACTER_TIM_PULL_REQUEST=v0.2.0
#
# Pull requests must be integers
#
# export FACTER_AEOLUS_CLI_PULL_REQUEST=6
# export FACTER_CONDUCTOR_PULL_REQUEST=47
# export FACTER_TIM_PULL_REQUEST=2
#
create_pg_user() {
# create the database role
sudo su - postgres -c "psql -c \"CREATE ROLE $FACTER_RDBMS_USERNAME WITH LOGIN CREATEDB SUPERUSER PASSWORD '$FACTER_RDBMS_PASSWORD';\""
# note that the SUPERUSER option can be removed once we user Rails that merge this fix: https://github.com/rails/rails/pull/8548
if [ $? -ne 0 ]; then
echo "INFO: postgresql create role $FACTER_RDBMS_USERNAME failed"
fi
}
if `netstat -tln | grep -q -P "\:$FACTER_CONDUCTOR_PORT\\s"`; then
echo "A process is already listening on port $FACTER_CONDUCTOR_PORT. Aborting"
exit 1
fi
if [ -e $FACTER_AEOLUS_WORKDIR/conductor -o -e $FACTER_AEOLUS_WORKDIR/tim -o \
-e $FACTER_AEOLUS_WORKDIR/aeolus-cli ]; then
echo -n "Already existing directories, one of $FACTER_AEOLUS_WORKDIR/conductor, "
echo "$FACTER_AEOLUS_WORKDIR/tim or $FACTER_AEOLUS_WORKDIR/aeolus-cli. Aborting"
exit 1
fi
os=unsupported
grep -Eqs 'Red Hat Enterprise Linux Server release 6|CentOS release 6' /etc/redhat-release && os=el6
grep -qs -P 'Fedora release 16' /etc/fedora-release && os=f16
grep -qs -P 'Fedora release 17' /etc/fedora-release && os=f17
grep -qs -P 'Fedora release 18' /etc/fedora-release && os=f18
test -f /etc/debian_version && os=debian
if [ "$os" = "unsupported" ]; then
echo "This script has not been tested outside of EL6, Fedora 16/17/18"
echo "or Debian. You will need to install development libraries manually."
echo
echo "Press Control-C to quit, or ENTER to continue"
read
fi
# install dependencies for fedora/rhel/centos
if [ "$os" = "f16" -o "$os" = "f17" -o "$os" = "f18" -o "$os" = "el6" ]; then
depends="git patch"
depends="$depends net-tools" # for netstat
# general ruby deps needed to roll your own ruby or build extensions
depends="$depends gcc make zlib-devel"
# Conductor-specific deps
depends="$depends libffi-devel" #ffi
depends="$depends libxml2-devel" #nokogiri
depends="$depends libxslt-devel" #nokogiri
depends="$depends gcc-c++" #eventmachine
# Puppet and puppet modules deps
depends="$depends openssl-devel lsof"
if [ "$FACTER_RDBMS" = "sqlite" ]; then
depends="$depends sqlite-devel" #sqlite3
elif [ "$FACTER_RDBMS" = "postgresql" ]; then
depends="$depends postgresql-devel postgresql postgresql-server"
fi
if [ "x$RBENV_VERSION" = "x" ]; then
# additional dependencies if using system ruby and not rbenv
depends="$depends rubygems ruby-devel"
if [ $os != "el6" ]; then
depends="$depends rubygem-bundler"
fi
else
# the ruby-build plugin to rbenv requires tar
depends="$depends tar"
fi
# Add Deltacloud build dependencies if needed
if [ "x$SETUP_LOCAL_DELTACLOUD_RELEASE" != "x" ]; then
depends="$depends bison flex libxslt openssl-devel"
depends="$depends readline-devel"
# Deltacloud added new deps after 1.0.5:
# https://mail-archives.apache.org/mod_mbox/deltacloud-dev/201212.mbox/browser
depends="$depends sqlite-devel"
fi
# If we have sudo, we are able to install missing dependencies
if [ "$HAVESUDO" = "1" ]; then
# Check which dependencies need installing
install_list=""
for dep in $depends; do
if ! `rpm -q --quiet --nodigest $dep`; then
install_list="$install_list $dep"
fi
done
# Install the needed packages
if [ "x$install_list" != "x" ]; then
sudo yum install -y $install_list
fi
# Verify the dependencies did install
fail_list=""
for dep in $depends; do
if ! `rpm -q --quiet --nodigest $dep`; then
fail_list="$fail_list $dep"
fi
done
# If anything failed verification, we tell the user and exit
if [ "x$fail_list" != "x" ]; then
echo "ABORTING: FAILED TO INSTALL $fail_list"
exit 1
fi
if [ "$FACTER_RDBMS" = "postgresql" ]; then
# If postgresql is already initialized, we do not want to overwrite settings in
# an existing /var/lib/pgsql/data/pg_hba.conf
PG_HBA_CONF_EXISTS=0
if sudo test -e /var/lib/pgsql/data/pg_hba.conf &> /dev/null; then
echo "INFO: postgresql database previously initialized"
PG_HBA_CONF_EXISTS=1
fi
# initialize the postgresql database
# if postgresql has already been initialized with the default data dir (/var/lib/pgsql/data)
# then this is a no-op
if [ "$os" == "el6" ]; then
sudo service postgresql initdb
else
sudo postgresql-setup initdb
fi
# if there was no pre-existing pg_hba.conf, set all authentication methods to 'trust' and 'md5'
if [ $PG_HBA_CONF_EXISTS -eq 0 ]; then
sudo sh -c "cat >/var/lib/pgsql/data/pg_hba.conf <<-EOD
local all all trust
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
EOD"
fi
# start the postgresql service
if [ "$os" == "el6" ]; then
sudo service postgresql start
else
sudo systemctl start postgresql.service
fi
# create the database role with CREATEDB permission
create_pg_user
fi
else
for dep in $depends; do
# sanity check that it just installed
if ! `rpm -q --quiet --nodigest $dep`; then
echo "ABORTING: $dep is not installed"
exit 1
fi
done
fi
fi
if [ "$os" = "debian" ]; then
if [ "$HAVESUDO" = "1" ]; then
if [ "$FACTER_RDBMS" = "postgresql" ]; then
sudo apt-get install -y postgresql postgresql-client libpq-dev
fi
if [ "$FACTER_RDBMS" = "sqlite" ]; then
sudo apt-get install -y sqlite3 libsqlite3-dev
fi
sudo apt-get install -y build-essential git curl libxslt1-dev libxml2-dev zlib1g zlib1g-dev libffi-dev libssl-dev libreadline-dev lsof
# adding the ruby stuff as a distinct step so we can conditionalize this a bit better later
# --just throw in a if [ "x$RBENV_VERSION" != "x" ]; then ?
sudo apt-get install -y ruby1.9.1 ruby1.9.1-dev libruby1.9.1
if [ "$FACTER_RDBMS" = "postgresql" ]; then
# set up postgres
# apt-get install postgresql starts the postgresql service, but
# attempt to start it in case it was previously installed
sudo service postgresql start
# create the database role with CREATEDB permission
create_pg_user
fi
fi
fi
mkdir -p $FACTER_AEOLUS_WORKDIR
if [ ! -d $FACTER_AEOLUS_WORKDIR ]; then
echo "ABORTING. Could not create directory $FACTER_AEOLUS_WORKDIR"
fi
if [ "x$RBENV_VERSION" != "x" ]; then
# only used for "rbenv install" in the Fedora-(16|17) / ruby 1.8.7 case.
# not tested with Fedora 18 since ruby18 is no longer supported in
# conductor/tim
if [ "x$RBENV_INSTALL_CONFIGURE_OPTS" = "x" ]; then
if [ "$os" = "f16" -o "$os" = "f17" ]; then
if echo $RBENV_VERSION | grep -qs '^1.8.7-' ; then
RBENV_INSTALL_CONFIGURE_OPTS=--without-dl
fi
fi
fi
# install rbenv plus plugins rbenv-var, ruby-build, rbenv-installer
# this is a harmless op if already installed (TODO: don't bother downloading and running if already installed)
curl -L https://raw.github.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | /bin/bash
export PATH=~/.rbenv/bin:~/.rbenv/shims:$PATH
# if this ruby version is not already installed in this user's rbenv, install it
rbenv versions | grep -q $RBENV_VERSION
if [ $? -ne 0 ]; then
CONFIGURE_OPTS=$RBENV_INSTALL_CONFIGURE_OPTS rbenv install $RBENV_VERSION
fi
# bail if the ruby version doesn't seem to be installed
rbenv versions | grep -q $RBENV_VERSION
if [ $? -ne 0 ]; then
echo was not able to "rbenv install $RBENV_VERSION". Check ~/.rbenv
exit 1
fi
# install bundler if not already installed
cd $FACTER_AEOLUS_WORKDIR
rbenv local $RBENV_VERSION
rbenv rehash
rbenv which bundle | grep -q "/$RBENV_VERSION/bin/bundle"
if [ $? -ne 0 ]; then
gem install bundler
rbenv rehash
# sanity check install of bundler
rbenv which bundle | grep -q "/$RBENV_VERSION/bin/bundle"
if [ $? -ne 0 ]; then
echo "gem install bundler in rbenv for version $RBENV_VERSION did not appear to succeed"
exit 1
fi
fi
export FACTER_RBENV_VERSION=$RBENV_VERSION
# looking up a home dir in puppet is not terribly easy, hence the next two lines
export FACTER_RBENV_HOME=~/.rbenv
fi
gem_installs="json facter puppet foreman"
if [ "$os" = "el6" -o "$os" = "debian" ]; then
gem_installs="$gem_installs bundler"
fi
# use a slightly older version of facter because latest stable of
# 1.6.14 causes an error like:
# Error: Could not run: Could not retrieve facts for <hostame>: undefined method `enum_lsdev' for Facter::Util::Processor:Module
declare -A gem_versions
gem_versions["facter"]=1.6.13
for the_gem in $gem_installs; do
if ! gem list -i $the_gem >/dev/null; then
cmd="gem install $the_gem"
if [ "x$RBENV_VERSION" = "x" ]; then
# TODO perhaps determine a way to install gem local to current
# user instead. For now, do a system install of the gem when
# using system ruby
# http://docs.rubygems.org/read/chapter/3
if [ "$HAVESUDO" = "1" ]; then
cmd="sudo $cmd"
else
echo 'Installing local gems without using rbenv is not currently supported.'
echo 'If you want to install gems system-wide using system ruby instead of rbenv, you must be a sudo-enabled user'
exit 1
fi
fi
if [ ! "x${gem_versions[$the_gem]}" = "x" ]; then
cmd="$cmd -v ${gem_versions[$the_gem]}"
fi
$cmd
if ! gem list -i $the_gem >/dev/null; then
echo "ABORTING. FAILED TO INSTALL $the_gem"
exit 1
fi
fi
done
# newly installed rbenv/gem binaries require an rbenv rehash to work
# properly in our $PATH
if [ "x$RBENV_VERSION" != "x" ]; then
cd $FACTER_AEOLUS_WORKDIR && rbenv rehash
fi
# Setup the local deltacloud instance, if the user wants one
if [ "x$SETUP_LOCAL_DELTACLOUD_RELEASE" != "x" ]; then
cd $FACTER_AEOLUS_WORKDIR
if [ -d deltacloud ]; then
echo 'INFO deltacloud dir already exists'
else
git clone https://git-wip-us.apache.org/repos/asf/deltacloud.git
fi
cd deltacloud
git checkout $SETUP_LOCAL_DELTACLOUD_RELEASE
cd server
bundle install --path ../bundle
SETUP_LOCAL_DELTACLOUD_PORT=${SETUP_LOCAL_DELTACLOUD_PORT:=3002}
if `netstat -tlpn | grep -q -P "\:$SETUP_LOCAL_DELTACLOUD_PORT\\s"`; then
echo "WARNING A process is already listening on port $SETUP_LOCAL_DELTACLOUD_PORT"
echo " Not starting up deltacloud"
else
echo "* Starting up deltacloudd on port $SETUP_LOCAL_DELTACLOUD_PORT"
# TODO: setup logging by using a custom config file
bundle exec "bin/deltacloudd -i \"mock\" -p $SETUP_LOCAL_DELTACLOUD_PORT" >log/deltacloud.log 2>&1 &
fi
fi
# These next few lines are usuall a no-op since WORKDIR
# and FACTER_AEOLUS_WORKDIR are usually the same
mkdir -p $WORKDIR
if [ ! -d $WORKDIR ]; then
echo "ABORTING. Could not create directory $WORKDIR"
exit 1
fi
cd $WORKDIR
if [ ! -d dev-tools ]; then
git clone https://github.com/aeolus-incubator/dev-tools.git
if [ "x$DEV_TOOLS_BRANCH" != "x" ]; then
cd dev-tools
git checkout $DEV_TOOLS_BRANCH
cd ..
fi
else
echo 'dev-tools DIRECTORY ALREADY EXISTS, LEAVING INTACT.'
fi
echo "Given this puppet bug http://projects.puppetlabs.com/issues/9862"
echo "you may need to add manually the 'puppet' group to the system in case of errors."
# install repos, configure and start up conductor
cd $WORKDIR/dev-tools
puppet apply -d --modulepath=. test.pp --no-report
if [ $? -eq 0 ]; then
echo "Success! Dev-tools has set up your Conductor environment along with"
echo "the related Aeolus projects, Tim and Aeolus-cli."
echo ""
echo "An instance of rails for conductor has been started on"
echo "port $FACTER_CONDUCTOR_PORT. Note that two other conductor processes have"
echo "also been started (dbomatic and delayed jobs). To stop all three"
echo "conductor processes, use \"$FACTER_AEOLUS_WORKDIR/bin/conductor stop\"."
echo ""
echo "The conductor start/stop/restart script,"
echo "$FACTER_AEOLUS_WORKDIR/bin/conductor,"
echo "which starts or stops the three processes has been created for your"
echo "convenience. Use of this script is optional; you may instead wish to"
echo "cd into $FACTER_AEOLUS_WORKDIR/conductor/src and use \"foreman start\""
echo "to start the processes in a shell or just plain old \"bundle exec\" the"
echo "commands in $FACTER_AEOLUS_WORKDIR/conductor/src/Procfile (assuming"
echo "of course you have already stopped the conductor processes that this"
echo "dev-tools script started up)."
if [ "x$RBENV_VERSION" != "x" ]; then
echo ""
echo "NOTE: RBENV_VERSION was specified when running dev-tools. If you"
echo "plan on hacking away, make sure that you add rbenv to your path, e.g.:"
echo ""
echo " export RBENV_ROOT=\"\${HOME}/.rbenv\""
echo " if [ -d \"\${RBENV_ROOT}\" ]; then"
echo " export PATH=\"\${RBENV_ROOT}/bin:\${PATH}\""
echo " eval \"\$(rbenv init -)\""
echo " fi"
fi
fi
# Arbitrary post-script command to execute
# (useful for say, seeding provider accounts)
if [ ! "x$POST_SCRIPTLET" = "x" ]; then
# When $POST_SCRIPTLET is eval'ed, it should just write the script
# to execute to stdout. It is "eval'ed" so your outside script can
# safely use bootstrap.sh environment variables.
eval $POST_SCRIPTLET | /bin/sh -x
fi