-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.sh
executable file
·421 lines (343 loc) · 11.6 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
#!/bin/bash -e
# DEAULTS
####
# repository
export APT_REPOSITORY_URL=${APT_REPOSITORY_URL:-http://apt.tcpcloud.eu/}
export APT_REPOSITORY_GPG=${APT_REPOSITORY_GPG:-http://apt.tcpcloud.eu/public.gpg}
export APT_REPOSITORY_TAGS=${APT_REPOSITORY_TAGS:-main tcp tcp-salt}
export APT_REPOSITORY="deb [arch=amd64] ${APT_REPOSITORY_URL}${APT_REPOSITORY_BRANCH:-nightly} ${APT_REPOSITORY_CODENM:-$(lsb_release -cs)} ${APT_REPOSITORY_TAGS:-main}"
# reclass
export RECLASS_ADDRESS=${RECLASS_ADDRESS:-https://github.com/tcpcloud/openstack-salt-model.git} # https/git
# formula
export FORMULA_SOURCE=${FORMULA_SOURCE:-pkg} # pkg/git
export FORMULA_PATH=${FORMULA_PATH:-/usr/share/salt-formulas}
export FORMULA_GIT_BRANCH=${FORMULA_GIT_BRANCH:-master}
export FORMULA_GIT_BASE_URL=${FORMULA_GIT_BASE_URL:-https://github.com/tcpcloud}
# system / host
export HOSTNAME=${HOSTNAME:-cfg01}
export DOMAIN=${DOMAIN:-bootstrap.local}
# salt
export SALT_MASTER=${SALT_MASTER:-127.0.0.1} # ip or fqdn
export MINION_ID=${MINION_ID:-${HOSTNAME}.${DOMAIN}}
# ENVIRONMENT
####
SUDO=${SUDO:-sudo}
SALT_SOURCE=${SALT_SOURCE:-pkg}
SALT_VERSION=${SALT_VERSION:-latest}
if [ "$FORMULA_SOURCE" == "git" ]; then
SALT_ENV=${SALT_ENV:-dev}
elif [ "$FORMULA_SOURCE" == "pkg" ]; then
SALT_ENV=${SALT_ENV:-prd}
fi
eval $(cat /etc/*release 2> /dev/null)
PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z)
case $PLATFORM_FAMILY in
debian )
PKGTOOL="$SUDO apt-get"
test ${VERSION_ID//\.*/} -ge 16 && {
SVCTOOL=service
} || { SVCTOOL=service
}
;;
rhel )
PKGTOOL="$SUDO yum"
test ${VERSION_ID//\.*/} -ge 7 && {
SVCTOOL=systemctl
} || { SVCTOOL=service
}
;;
esac
export PLATFORM_FAMILY
export PKGTOOL
export SVCTOOL
# FUNCTIONS
####
configure_pkg_repo()
{
case $PLATFORM_FAMILY in
debian)
if [ -n "$APT_REPOSITORY_PPA" ]; then
which add-apt-repository || $SUDO apt-get install -y software-properties-common
$SUDO add-apt-repository -y ppa:${APT_REPOSITORY_PPA}
else
echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/bootstrap.list >/dev/null
wget -O - $APT_REPOSITORY_GPG | $SUDO apt-key add -
fi
$SUDO apt-get clean
$SUDO apt-get update
;;
rhel)
$SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm
$SUDO yum clean all
;;
esac
}
# DEPRECATED
#purge_system()
#{
## debian
#if [ -x "`which invoke-rc.d 2>/dev/null`" -a -x "/etc/init.d/salt-minion" ] ; then
#$SUDO apt-get purge -y salt-minion salt-common && $SUDO apt-get autoremove -y
#fi
## rhel
#if [ -x "`which invoke-rc.d 2>/dev/null`" -a -x "/etc/init.d/salt-minion" ] ; then
#$SUDO yum remove -y salt-minion salt-common && $SUDO yum autoremove -y
#fi
#}
configure_salt_master()
{
echo "Configuring salt-master ..."
[ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
cat <<-EOF > /etc/salt/master.d/master.conf
file_roots:
base:
- /usr/share/salt-formulas/env
prd:
- /usr/salt/env/prd
dev:
- /usr/salt/env/dev
pillar_opts: False
open_mode: True
reclass: &reclass
storage_type: yaml_fs
inventory_base_uri: /srv/salt/reclass
ext_pillar:
- reclass: *reclass
master_tops:
reclass: *reclass
EOF
echo "Configuring reclass ..."
[ ! -d /etc/reclass ] && mkdir /etc/reclass
cat <<-EOF > /etc/reclass/reclass-config.yml
storage_type: yaml_fs
pretty_print: True
output: yaml
inventory_base_uri: /srv/salt/reclass
EOF
if [ ! -d /srv/salt/reclass ]; then
# No reclass at all, clone from given address
git clone ${RECLASS_ADDRESS} /srv/salt/reclass -b ${RECLASS_BRANCH:-master}
fi;
cd /srv/salt/reclass
if [ ! -d /srv/salt/reclass/classes/system/linux ]; then
# Possibly subrepo checkout needed
git submodule update --init --recursive
fi
#sed -ie "s#\(reclass_data_revision.\).*#\1 $RECLASS_BRANCH#" $(find nodes -name ${MASTER_HOSTNAME}.yml|tail -n1)
mkdir -vp /srv/salt/reclass/nodes
CONFIG=$(find /srv/salt/reclass/nodes -name ${MINION_ID}.yml|tail -n1)
CONFIG=${CONFIG:-/srv/salt/reclass/nodes/${MINION_ID}.yml}
[[ -f "${CONFIG}" ]] || {
cat <<-EOF > ${CONFIG}
classes:
- service.git.client
- system.linux.system.single
- system.openssh.client.lab
- system.salt.master.single
- system.salt.master.formula.$FORMULA_SOURCE
- system.reclass.storage.salt
parameters:
_param:
reclass_data_repository: "$RECLASS_ADDRESS"
reclass_data_revision: ${RECLASS_BRANCH:-master}
salt_formula_branch: ${FORMULA_GIT_BRANCH:-master}
reclass_config_master: $SALT_MASTER
single_address: $SALT_MASTER
salt_master_host: $SALT_MASTER
salt_master_base_environment: $SALT_ENV
linux:
system:
name: $MINION_ID
domain: $DOMAIN
EOF
if [ "$SALT_VERSION" == "latest" ]; then
VERSION=""
else
VERSION="version: $SALT_VERSION"
fi
cat <<-EOF >> ${CONFIG}
salt:
master:
accept_policy: open_mode
source:
engine: $SALT_SOURCE
$VERSION
minion:
source:
engine: $SALT_SOURCE
$VERSION
EOF
}
}
configure_salt_minion()
{
[ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
cat <<-EOF > /etc/salt/minion.d/minion.conf
master: $SALT_MASTER
id: $MINION_ID
EOF
}
install_salt_master_pkg()
{
echo -e "\nPreparing base OS repository ...\n"
configure_pkg_repo
echo -e "\nInstalling salt master ...\n"
case $PLATFORM_FAMILY in
debian)
$SUDO apt-get install -y reclass git
if [ "$SALT_VERSION" == "latest" ]; then
$SUDO apt-get install -y salt-common salt-master
else
$SUDO apt-get install -y --force-yes salt-common=$SALT_VERSION salt-master=$SALT_VERSION
fi
;;
rhel)
# TODO review rhel
#if [ "$SALT_VERSION" == "latest" ]; then
#$SUDO yum install -y salt-master
#else
#$SUDO yum install -y salt-master-SALT_VERSION
#fi
;;
esac
configure_salt_master
echo -e "\nRestarting services ...\n"
[ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
$SVCTOOL salt-master restart
}
install_salt_master_pip()
{
echo -e "\nPreparing base OS repository ...\n"
case $PLATFORM_FAMILY in
debian)
$SUDO apt-get install -y python-pip python-dev zlib1g-dev reclass git
;;
rhel)
# TODO
;;
esac
echo -e "\nInstalling salt master ...\n"
if [ "$SALT_VERSION" == "latest" ]; then
pip install salt
else
pip install salt==$SALT_VERSION
fi
wget -O /etc/init.d/salt-master https://anonscm.debian.org/cgit/pkg-salt/salt.git/plain/debian/salt-master.init && chmod 755 /etc/init.d/salt-master
ln -s /usr/local/bin/salt-master /usr/bin/salt-master
configure_salt_master
echo -e "\nRestarting services ...\n"
[ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
$SVCTOOL salt-master restart
}
install_salt_minion_pkg()
{
configure_pkg_repo
echo -e "\nInstalling salt minion ...\n"
case $PLATFORM_FAMILY in
debian)
if [ "$SALT_VERSION" == "latest" ]; then
$SUDO apt-get install -y salt-common salt-minion
else
$SUDO apt-get install -y --force-yes salt-common=$SALT_VERSION salt-minion=$SALT_VERSION
fi
;;
rhel)
# TODO, review rhel versioN
#if [ "$SALT_VERSION" == "LATEST" ]; then
#$SUDO yum install -y salt-minion
#else
#$SUDO yum install -y salt-minion-$SALT_VERSION
#fi
;;
esac
configure_salt_minion
$SVCTOOL salt-minion restart
}
install_salt_minion_pip()
{
echo -e "\nInstalling salt minion ...\n"
wget -O /etc/init.d/salt-minion https://anonscm.debian.org/cgit/pkg-salt/salt.git/plain/debian/salt-minion.init && chmod 755 /etc/init.d/salt-minion
ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
configure_salt_minion
$SVCTOOL salt-minion restart
}
install_salt_formula_pkg()
{
configure_pkg_repo
case $PLATFORM_FAMILY in
debian)
echo "Configuring necessary formulas ..."
[ ! -d /srv/salt/reclass/classes/service ] && mkdir -p /srv/salt/reclass/classes/service
declare -a formula_services=("linux" "reclass" "salt" "openssh" "ntp" "git" "nginx" "collectd" "sensu" "heka" "sphinx" "mysql" "grafana" "libvirt" "rsyslog")
for formula_service in "${formula_services[@]}"; do
echo -e "\nConfiguring salt formula ${formula_service} ...\n"
[ ! -d "${FORMULA_PATH}/env/${formula_service}" ] && \
$SUDO apt-get install -y salt-formula-${formula_service}
[ ! -L "/srv/salt/reclass/classes/service/${formula_service}" ] && \
ln -s ${FORMULA_PATH}/reclass/service/${formula_service} /srv/salt/reclass/classes/service/${formula_service}
done
;;
rhel)
# TODO
;;
esac
[ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
[ ! -L /srv/salt/env/prd ] && ln -s ${FORMULA_PATH}/env /srv/salt/env/prd || echo ""
}
install_salt_formula_git()
{
echo "Configuring necessary formulas ..."
[ ! -d /srv/salt/reclass/classes/service ] && mkdir -p /srv/salt/reclass/classes/service
declare -a formula_services=("linux" "reclass" "salt" "openssh" "ntp" "git" "nginx" "collectd" "sensu" "heka" "sphinx" "mysql" "grafana" "libvirt" "rsyslog")
for formula_service in "${formula_services[@]}"; do
echo -e "\nConfiguring salt formula ${formula_service} ...\n"
_BRANCH=${FORMULA_BRANCH}
[ ! -d "${FORMULA_PATH}/env/_formulas/${formula_service}" ] && {
if ! git ls-remote --exit-code --heads ${FORMULA_GIT_BASE_URL}/salt-formula-${formula_service}.git ${_BRANCH}; then
# Fallback to the master branch if the branch doesn't exist for this repository
_BRANCH=master
fi
git clone ${FORMULA_GIT_BASE_URL}/salt-formula-${formula_service}.git ${FORMULA_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}
} || {
cd ${FORMULA_PATH}/env/_formulas/${formula_service};
git fetch origin/${_BRANCH} || git fetch --all
git checkout ${_BRANCH} && git pull || git pull;
cd -
}
[ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
ln -s ${FORMULA_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
[ ! -L "/srv/salt/reclass/classes/service/${formula_service}" ] && \
ln -s ${FORMULA_PATH}/env/_formulas/${formula_service}/metadata/service /srv/salt/reclass/classes/service/${formula_service}
done
[ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
[ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
}
# MAIN
####
# detect if file is being sourced
# bash/korn shell compatible
#[[ "$0" != "$_" ]] && main "$@"
# bash way
[[ "$0" != "$BASH_SOURCE" ]] || {
# DEBUGING
#set -x
#test -e $(dirname $0)/env/salt.env && source $(dirname $0)/env/salt.env
#set
# CLI
while [ x"$1" != x"" ]; do
# PREREQ
which wget &>/dev/null || $PKGTOOL -y install wget &>/dev/null
case $1 in
master )
install_salt_master_$SALT_SOURCE
install_salt_minion_$SALT_SOURCE
install_salt_formula_$FORMULA_SOURCE
;;
minion )
install_salt_minion_$SALT_SOURCE
;;
esac
shift
done
echo DONE
}