blob: d705eed23f0fad341d5e7b6e1e20c6ba9e635b5a [file] [log] [blame]
Petr Michalec1ed1b3c2017-08-08 19:19:01 +02001#!/bin/bash -e
2
3# bootstrap.sh
4
5# Installs Salt and configure minimal SaltMaster or Minion to be used with:
6# - http://github.com/salt-formulas-scripts
7# - http://github.com/salt-formulas/salt-formula-salt (salt.master sls)
8
9# TODO:
10# - use PPA repository as formula source
11# - support for spm/yum
12
13
14# Source specific env vars.
15# shopt -u dotglob
16export RECLASS_ROOT=${RECLASS_ROOT:-/srv/salt/reclass}
17function source_local_envs() {
18 for path in /tmp/kitchen /srv/salt . ${RECLASS_ROOT}/classes/cluster ${RECLASS_ROOT}/classes/cluster/${CLUSTER_NAME}; do
Vasyl Saienko7a4161c2017-08-14 17:43:15 +030019 for f in $(find $path -maxdepth 1 -name '*.env' 2> /dev/null); do
20 echo "Sourcing env variables from $f"
21 source $f
22 done
23 done
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020024}
25source_local_envs
26
27##########################################
28# Set defaults env variables
29
30if [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
31 set -x
32 SALT_LOG_LEVEL="--state-verbose=true -ldebug"
33fi
34
35export MAGENTA='\033[0;95m'
36export YELLOW='\033[1;33m'
37export BLUE='\033[0;35m'
38export CYAN='\033[0;96m'
39export RED='\033[0;31m'
40export NC='\033[0m' # No Color'
41
42export LC_ALL=C
43export SALT_LOG_LEVEL="--state-verbose=false -lerror"
44export SALT_OPTS="${SALT_OPTS:- --timeout=120 --state-output=changes --retcode-passthrough --force-color $SALT_LOG_LEVEL }"
45export SALT_STATE_RETRY=${SALT_STATE_RETRY:-3}
46export BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True}
47export BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX stable 2016.3 }
48
49
50# salt apt repository
51test -e /etc/lsb-release && eval $(cat /etc/lsb-release)
52which lsb_release && DISTRIB_CODENAME=${DISTRIB_CODENAME:-$(lsb_release -cs)}
53#
54export APT_REPOSITORY="deb [arch=amd64] http://apt-mk.mirantis.com/${DISTRIB_CODENAME} ${DISTRIB_REVISION:-stable} salt"
55export APT_REPOSITORY_GPG=${APT_REPOSITORY_GPG:-http://apt-mk.mirantis.com/public.gpg}
56
57# reclass
58export RECLASS_ADDRESS=${RECLASS_ADDRESS:-https://github.com/salt-formulas/openstack-salt.git} # https/git
59
60# formula
61export FORMULAS_BASE=${FORMULAS_BASE:-https://github.com/salt-formulas}
62export FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas}
63export FORMULAS_BRANCH=${FORMULAS_BRANCH:-master}
64export FORMULAS_SOURCE=${FORMULAS_SOURCE:-pkg} # pkg/git
65# essential set of formulas (known to by used on cfg01 node for most setups)
66FORMULAS_SALT_MASTER=${FORMULAS_SALT_MASTER:- $EXTRA_FORMULAS memcached openssh ntp nginx collectd sensu heka sphinx mysql grafana libvirt rsyslog glusterfs postfix xtrabackup freeipa prometheus telegraf elasticsearch kibana rundeck devops-portal}
67# minimal set of formulas for salt-master bootstrap
68declare -a FORMULAS_SALT_MASTER=(linux reclass salt git $(echo $FORMULAS_SALT_MASTER))
69export FORMULAS_SALT_MASTER
70
71# system / host
72export HOSTNAME=${HOSTNAME:-cfg01}
73export DOMAIN=${DOMAIN:-bootstrap.local}
74
75# salt
76export SALT_MASTER=${SALT_MASTER:-127.0.0.1} # ip or fqdn
77export MINION_ID=${MINION_ID:-${HOSTNAME}.${DOMAIN}}
78
79# saltstack
80BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True}
81BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX stable 2016.3 }
82SALT_SOURCE=${SALT_SOURCE:-pkg}
83SALT_VERSION=${SALT_VERSION:-latest}
84
85# environment
86if [ "$FORMULAS_SOURCE" == "git" ]; then
87 SALT_ENV=${SALT_ENV:-dev}
88elif [ "$FORMULAS_SOURCE" == "pkg" ]; then
89 SALT_ENV=${SALT_ENV:-prd}
90fi
91eval $(cat /etc/*release 2> /dev/null)
92PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z)
93case $PLATFORM_FAMILY in
94 debian )
95 PKGTOOL="$SUDO apt-get"
96 test ${VERSION_ID//\.*/} -ge 16 && {
97 SVCTOOL=service
98 } || { SVCTOOL=service
99 }
100 ;;
101 rhel )
102 PKGTOOL="$SUDO yum"
103 test ${VERSION_ID//\.*/} -ge 7 && {
104 SVCTOOL=systemctl
105 } || { SVCTOOL=service
106 }
107 ;;
108esac
109
110export PLATFORM_FAMILY
111export PKGTOOL
112export SVCTOOL
113
114##########################################
115# FUNCTIONS
116
117log_info() {
118 echo -e "${YELLOW}[INFO] $* ${NC}"
119}
120
121log_warn() {
122 echo -e "${MAGENTA}[WARN] $* ${NC}"
123}
124
125log_err() {
126 echo -e "${RED}[ERROR] $* ${NC}" >&2
127}
128
129configure_pkg_repo()
130{
131
132 case $PLATFORM_FAMILY in
133 debian)
134 if [ -n "$APT_REPOSITORY_PPA" ]; then
135 which add-apt-repository || $SUDO apt-get install -y software-properties-common
136 $SUDO add-apt-repository -y ppa:${APT_REPOSITORY_PPA}
137 else
138 echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/bootstrap.list >/dev/null
139 curl -sL $APT_REPOSITORY_GPG | $SUDO apt-key add -
140 fi
141 $SUDO apt-get clean
142 $SUDO apt-get update
143 ;;
144 rhel)
145 $SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm
146 $SUDO yum clean all
147 ;;
148 esac
149
150}
151
152_atexit() {
153 RETVAL=$?
154 trap true INT TERM EXIT
155
156 if [ $RETVAL -ne 0 ]; then
157 log_err "Execution failed"
158 else
159 log_info "Execution successful"
160 fi
161 return $RETVAL
162}
163
164retry() {
165 local tries
166 if [[ $1 =~ ^[0-9]+$ ]]; then
167 tries=$1; shift
168 else
169 tries=3
170 fi
171 for i in $(seq 1 $tries); do
172 "$@" && return 0 || sleep $i
173 done
174 return 1
175}
176
177function clone_reclass() {
178 if [ ! -d ${RECLASS_ROOT} ]; then
179 # No reclass at all, clone from given address
180 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
181 if echo ${RECLASS_BRANCH:-master} | egrep -q "^refs"; then
182 git clone ${RECLASS_ADDRESS} ${RECLASS_ROOT}
183 cd ${RECLASS_ROOT}
184 git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH:-master} && git checkout FETCH_HEAD
185 cd -
186 else
187 git clone -b ${RECLASS_BRANCH:-master} ${RECLASS_ADDRESS} ${RECLASS_ROOT};
188 fi;
189 fi;
190}
191
192
193##########################################
194# Main calls
195
196system_config_minion() {
197 log_info "System configuration salt minion"
198}
199
200system_config_master() {
201 log_info "System configuration salt master"
202
203 # salt-formulas custom modules dependencies, etc:
204 $SUDO $PKGTOOL install -y iproute2 curl sudo apt-transport-https python-psutil python-apt python-m2crypto python-oauth python-pip &>/dev/null
205
206 $SUDO mkdir -p $RECLASS_ROOT/classes/service
207 $SUDO mkdir -p /root/.ssh
208 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
209 echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee ~/.ssh/config >/dev/null
210 echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee /root/.ssh/config >/dev/null
211 echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null
212
213 which reclass || $SUDO $PKGTOOL install -y reclass
214
215 which reclass-salt || {
216 test -e /usr/share/reclass/reclass-salt && {
217 ln -fs /usr/share/reclass/reclass-salt /usr/bin
218 }
219 }
220}
221
222configure_salt_master()
223{
224
225 echo "Configuring salt-master ..."
226
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200227 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
228 IGNORE_CLASS_NOTFOUND="ignore_class_notfound: True"
229 fi
230
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200231 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
232 cat <<-EOF > /etc/salt/master.d/master.conf
233 file_roots:
234 base:
235 - /usr/share/salt-formulas/env
236 prd:
237 - /srv/salt/env/prd
238 dev:
239 - /srv/salt/env/dev
240 pillar_opts: False
241 open_mode: True
242 reclass: &reclass
243 storage_type: yaml_fs
244 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200245 ${IGNORE_CLASS_NOTFOUND}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200246 ext_pillar:
247 - reclass: *reclass
248 master_tops:
249 reclass: *reclass
250EOF
251
252 echo "Configuring reclass ..."
253
254 [ ! -d /etc/reclass ] && mkdir /etc/reclass
255 cat <<-EOF > /etc/reclass/reclass-config.yml
256 storage_type: yaml_fs
257 pretty_print: True
258 output: yaml
259 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200260 ${IGNORE_CLASS_NOTFOUND}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200261EOF
262
263 clone_reclass
264 # override some envs from cluster level *.env, use with care
265 source_local_envs
266
267 cd ${RECLASS_ROOT}
268 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
269 # Possibly subrepo checkout needed
270 git submodule update --init --recursive
271 fi
272
273 #sed -ie "s#\(reclass_data_revision.\).*#\1 $RECLASS_BRANCH#" $(find nodes -name ${MASTER_HOSTNAME}.yml|tail -n1)
274
275 mkdir -vp ${RECLASS_ROOT}/nodes
276 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
277 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/${MINION_ID}.yml}
278 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
279 cat <<-EOF > ${CONFIG}
280 classes:
281 - service.git.client
282 - system.linux.system.single
283 - system.salt.master.single
284 - system.salt.master.$FORMULAS_SOURCE
285 - system.reclass.storage.salt
286 parameters:
287 _param:
288 reclass_data_repository: "$RECLASS_ADDRESS"
289 reclass_data_revision: ${RECLASS_BRANCH:-master}
290 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
291 reclass_config_master: $SALT_MASTER
292 single_address: $SALT_MASTER
293 salt_master_host: $SALT_MASTER
294 salt_master_base_environment: $SALT_ENV
295 linux_system_codename: $DISTRIB_CODENAME
296 linux:
297 system:
298 name: $MINION_ID
299 domain: $DOMAIN
300 # ########
301EOF
302
303 if [ "$SALT_VERSION" == "latest" ]; then
304 VERSION=""
305 else
306 VERSION="version: $SALT_VERSION"
307 fi
308
309 cat <<-EOF >> ${CONFIG}
310 salt:
311 master:
312 accept_policy: open_mode
313 source:
314 engine: $SALT_SOURCE
315 $VERSION
316 minion:
317 source:
318 engine: $SALT_SOURCE
319 $VERSION
320 # ########
321 # vim: ft=yaml sw=2 ts=2 sts=2
322EOF
323 fi
324}
325
326configure_salt_minion()
327{
328 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
329 cat <<-EOF > /etc/salt/minion.d/minion.conf
330 master: $SALT_MASTER
331 id: $MINION_ID
332 EOF
333}
334
335
336install_salt_master_pkg()
337{
338 echo -e "\nPreparing base OS repository ...\n"
339
340 configure_pkg_repo
341
342 echo -e "\nInstalling salt master ...\n"
343
344 case $PLATFORM_FAMILY in
345 debian)
346 $SUDO apt-get install -y git
347 which reclass || $SUDO apt install -qqq -y reclass
348 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
349 ;;
350 rhel)
351 yum install -y git
352 which reclass || $SUDO yum install -y reclass
353 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
354 ;;
355 esac
356
357 which reclass-salt || {
358 test -e /usr/share/reclass/reclass-salt && {
359 ln -fs /usr/share/reclass/reclass-salt /usr/bin
360 }
361 }
362
363 configure_salt_master
364
365 echo -e "\nRestarting services ...\n"
366 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
367 $SVCTOOL salt-master restart
368}
369
370install_salt_master_pip()
371{
372 echo -e "\nPreparing base OS repository ...\n"
373
374 case $PLATFORM_FAMILY in
375 debian)
376 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
377 which reclass || $SUDO apt-get install -y reclass
378 ;;
379 rhel)
380 $SUDO yum install -y git
381 which reclass || $SUDO yum install -y reclass
382 ;;
383 esac
384
385 echo -e "\nInstalling salt master ...\n"
386 # TODO: replace with saltstack bootstrap script
387
388 if [ "$SALT_VERSION" == "latest" ]; then
389 pip install salt
390 else
391 pip install salt==$SALT_VERSION
392 fi
393
394 curl -Lo /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
395 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
396
397 which reclass-salt || {
398 test -e /usr/share/reclass/reclass-salt && {
399 ln -fs /usr/share/reclass/reclass-salt /usr/bin
400 }
401 }
402
403 configure_salt_master
404
405 echo -e "\nRestarting services ...\n"
406 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
407 $SVCTOOL salt-master restart
408}
409
410
411
412install_salt_minion_pkg()
413{
414
415 configure_pkg_repo
416
417 echo -e "\nInstalling salt minion ...\n"
418
419 case $PLATFORM_FAMILY in
420 debian)
421 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
422 ;;
423 rhel)
424 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
425 ;;
426 esac
427
428
429 configure_salt_minion
430
431 $SVCTOOL salt-minion restart
432}
433
434install_salt_minion_pip()
435{
436 echo -e "\nInstalling salt minion ...\n"
437
438 curl -Lo /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
439 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
440
441 configure_salt_minion
442 $SVCTOOL salt-minion restart
443}
444
445
446install_salt_formula_pkg()
447{
448 configure_pkg_repo
449
450 case $PLATFORM_FAMILY in
451 debian)
452 echo "Configuring necessary formulas ..."
453
454 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
455 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
456 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
457 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
458 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
459 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
460 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
461 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
462 exit 1
463 fi
464 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
465 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
466 done
467 ;;
468 rhel)
469 # TODO
470 ;;
471 esac
472
473 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
474 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
475}
476
477install_salt_formula_git()
478{
479 echo "Configuring necessary formulas ..."
480
481 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
482 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
483 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
484 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
485 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
486 _BRANCH=${FORMULAS_BRANCH}
487 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
488 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
489 # Fallback to the master branch if the branch doesn't exist for this repository
490 _BRANCH=master
491 fi
492 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
493 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
494 exit 1
495 fi
496 } || {
497 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
498 git fetch origin/${_BRANCH} || git fetch --all
499 git checkout ${_BRANCH} && git pull || git pull;
500 cd -
501 }
502 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
503 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
504 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
505 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
506 done
507
508 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
509 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
510}
511
512
513saltmaster_bootstrap() {
514
515 log_info "Salt master setup"
516 test -n "$MASTER_HOSTNAME" || exit 1
517
518 clone_reclass
519 # override some envs from cluster level *.env, use with care
520 source_local_envs
521
522 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
523 pkill -9 salt-minion
524 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
525 export SALT_MASTER=localhost
526 export MINION_ID=${MASTER_HOSTNAME}
527 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
528 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
529 fi
530 # call local "setup() master"
531 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
532 if ! setup master; then
533 #cat /tmp/salt-master-setup.log
534 log_err "salt master setup() failed."
535 exit 1
536 else
537 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
538 fi
539 }
540
541 log_info "Clean up generated"
542 cd $RECLASS_ROOT
543 $SUDO rm -rf $RECLASS_ROOT/nodes/_generated/*
544
545 log_info "Re/starting salt services"
546 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
547 pkill -9 salt-minion
548 sleep 1
549 $SUDO service salt-master restart
550 $SUDO service salt-minion restart
551 sleep 10
552}
553
554# Init salt master
555saltmaster_init() {
556
557 log_info "Runing saltmaster states"
558 test -n "$MASTER_HOSTNAME" || exit 1
559
560 set -e
561 $SUDO salt-call saltutil.sync_all >/dev/null
562
563 # TODO: Placeholder update saltmaster spec (nodes/FQDN.yml) to be able to bootstrap with minimal configuration
564 # (ie: with linux, git, salt formulas)
565
566 #log_info "Verify SaltMaster, before salt-master is fully initialized"
567 #if ! $SUDO reclass-salt -p ${MASTER_HOSTNAME} &> /tmp/${MASTER_HOSTNAME}.pillar;then
568 # log_warn "Node verification before initialization failed."; cat /tmp/${MASTER_HOSTNAME}.pillar;
569 #fi
570
571 log_info "State: salt.master.env"
572 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env; then
573 log_err "State salt.master.env failed, keep your eyes wide open."
574 fi
575
576 log_info "State: salt.master.pillar"
577 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar='{"reclass":{"storage":{"data_source":{"engine":"local"}}}}'
578 # Note: sikp reclass data dir states
579 # in order to avoid pull from configured repo/branch
580
581 # Revert temporary SaltMaster minimal configuration, if any
582 pushd $RECLASS_ROOT
583 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
584 git status || true
585 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from salt-master-setup.sh call)"
586 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
587 git checkout -- $RECLASS_ROOT/nodes || true
588 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
589 log_info "State: salt.master.env"
590 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env; then
591 log_err "State salt.master.env failed, keep your eyes wide open."
592 fi
593 log_info "State: salt.master.pillar"
594 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar='{"reclass":{"storage":{"data_source":{"engine":"local"}}}}'
595 fi
596 popd
597
598 log_info "State: salt.master.storage.node"
599 set +e
600 $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
601 ret=$?
602 set -e
603
604 if [[ $ret -eq 2 ]]; then
605 log_err "State reclass.storage.node failed with exit code 2 but continuing."
606 elif [[ $ret -ne 0 ]]; then
607 log_err "State reclass.storage.node failed with exit code $ret"
608 exit 1
609 fi
610
611 log_info "Re/starting salt services"
612 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
613 $SUDO service salt-minion restart >/dev/null
614 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
615
616 verify_salt_master
617 set +e
618
619}
620
621
622function verify_salt_master() {
623 set -e
624
625 log_info "Verify Salt master"
626 test -n "$MASTER_HOSTNAME" || exit 1
627
628 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
629 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
630 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
631 $SUDO salt-call --no-color grains.items
632 $SUDO salt-call --no-color pillar.data
633 fi
634 if ! $SUDO reclass --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
635 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
636 exit 1
637 fi
638}
639
640function verify_salt_minion() {
641 set -e
642 node=$1
643 log_info "Verifying ${node}"
644 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
645 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
646 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
647 fi
648 if ! $SUDO reclass --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
649 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
650 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
651 exit 1
652 fi
653 fi
654}
655
656function verify_salt_minions() {
657 #set -e
658 NODES=$(find $RECLASS_ROOT/nodes/ -name "*.yml" | grep -v "cfg")
659 log_info "Verifying minions: $(echo ${NODES}|xargs)"
660
661 # Parallel
662 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
663 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
664
665 function filterFails() {
666 grep -v '/grains' | tee -a $1 | tail -n20
667 }
668
669 log_info "Verify nodes"
670 passed=0
671 for node in ${NODES}; do
672 node=$(basename $node .yml)
673
674 # filter first in cluster.. ctl-01, mon-01, etc..
675 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
676 verify_salt_minion ${node} || continue
677 else
678 echo Skipped $node.
679 fi
680 passed=$(($passed+1))
681 done
682 # fail on failures
683 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
684 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
685 test ! $passed -lt $total || {
686 tail -n50 /tmp/*.pillar_verify
687 return 1
688 }
689}
690
691
692
693##########################################
694# To install salt master/minon
695
696function install() {
697 setup $@
698}
699
700function setup() {
701 # CLI
702 while [ x"$1" != x"" ]; do
703 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
704
705 case $1 in
706 master )
707 install_salt_master_$SALT_SOURCE
708 install_salt_minion_$SALT_SOURCE
709 install_salt_formula_$FORMULAS_SOURCE
710 ;;
711 minion )
712 install_salt_minion_$SALT_SOURCE
713 ;;
714 esac
715 shift
716 done
717 echo DONE
718}
719
720function bootstrap() {
721 log_info "Bootstrap & verification of SaltMaster and configured minions."
722 trap _atexit INT TERM EXIT
723
724 system_config_master
725 saltmaster_bootstrap &&\
726 saltmaster_init &&\
727 verify_salt_minions
728}
729
730function default() {
731 bootstrap $@
732}
733
734
735##########################################
736[[ "$0" != "$BASH_SOURCE" ]] || {
737# unless file is being sourced
738 default $@
739}