blob: 9d0dcd12d34171fbd7e77d29792bc2b259a158d9 [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
227 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
228 cat <<-EOF > /etc/salt/master.d/master.conf
229 file_roots:
230 base:
231 - /usr/share/salt-formulas/env
232 prd:
233 - /srv/salt/env/prd
234 dev:
235 - /srv/salt/env/dev
236 pillar_opts: False
237 open_mode: True
238 reclass: &reclass
239 storage_type: yaml_fs
240 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalecbd5586d2017-08-16 17:08:51 +0200241 ignore_class_notfound: ${RECLASS_IGNORE_CLASS_NOTFOUND:-False}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200242 ext_pillar:
243 - reclass: *reclass
244 master_tops:
245 reclass: *reclass
246EOF
247
248 echo "Configuring reclass ..."
249
250 [ ! -d /etc/reclass ] && mkdir /etc/reclass
251 cat <<-EOF > /etc/reclass/reclass-config.yml
252 storage_type: yaml_fs
253 pretty_print: True
254 output: yaml
255 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalecbd5586d2017-08-16 17:08:51 +0200256 ignore_class_notfound: ${RECLASS_IGNORE_CLASS_NOTFOUND:-False}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200257EOF
258
259 clone_reclass
260 # override some envs from cluster level *.env, use with care
261 source_local_envs
262
263 cd ${RECLASS_ROOT}
264 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
265 # Possibly subrepo checkout needed
266 git submodule update --init --recursive
267 fi
268
269 #sed -ie "s#\(reclass_data_revision.\).*#\1 $RECLASS_BRANCH#" $(find nodes -name ${MASTER_HOSTNAME}.yml|tail -n1)
270
271 mkdir -vp ${RECLASS_ROOT}/nodes
272 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
273 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/${MINION_ID}.yml}
274 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
275 cat <<-EOF > ${CONFIG}
276 classes:
277 - service.git.client
278 - system.linux.system.single
279 - system.salt.master.single
280 - system.salt.master.$FORMULAS_SOURCE
281 - system.reclass.storage.salt
282 parameters:
283 _param:
284 reclass_data_repository: "$RECLASS_ADDRESS"
285 reclass_data_revision: ${RECLASS_BRANCH:-master}
286 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
287 reclass_config_master: $SALT_MASTER
288 single_address: $SALT_MASTER
289 salt_master_host: $SALT_MASTER
290 salt_master_base_environment: $SALT_ENV
291 linux_system_codename: $DISTRIB_CODENAME
292 linux:
293 system:
294 name: $MINION_ID
295 domain: $DOMAIN
296 # ########
297EOF
298
299 if [ "$SALT_VERSION" == "latest" ]; then
300 VERSION=""
301 else
302 VERSION="version: $SALT_VERSION"
303 fi
304
305 cat <<-EOF >> ${CONFIG}
306 salt:
307 master:
308 accept_policy: open_mode
309 source:
310 engine: $SALT_SOURCE
311 $VERSION
312 minion:
313 source:
314 engine: $SALT_SOURCE
315 $VERSION
316 # ########
317 # vim: ft=yaml sw=2 ts=2 sts=2
318EOF
319 fi
320}
321
322configure_salt_minion()
323{
324 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
325 cat <<-EOF > /etc/salt/minion.d/minion.conf
326 master: $SALT_MASTER
327 id: $MINION_ID
328 EOF
329}
330
331
332install_salt_master_pkg()
333{
334 echo -e "\nPreparing base OS repository ...\n"
335
336 configure_pkg_repo
337
338 echo -e "\nInstalling salt master ...\n"
339
340 case $PLATFORM_FAMILY in
341 debian)
342 $SUDO apt-get install -y git
343 which reclass || $SUDO apt install -qqq -y reclass
344 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
345 ;;
346 rhel)
347 yum install -y git
348 which reclass || $SUDO yum install -y reclass
349 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
350 ;;
351 esac
352
353 which reclass-salt || {
354 test -e /usr/share/reclass/reclass-salt && {
355 ln -fs /usr/share/reclass/reclass-salt /usr/bin
356 }
357 }
358
359 configure_salt_master
360
361 echo -e "\nRestarting services ...\n"
362 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
363 $SVCTOOL salt-master restart
364}
365
366install_salt_master_pip()
367{
368 echo -e "\nPreparing base OS repository ...\n"
369
370 case $PLATFORM_FAMILY in
371 debian)
372 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
373 which reclass || $SUDO apt-get install -y reclass
374 ;;
375 rhel)
376 $SUDO yum install -y git
377 which reclass || $SUDO yum install -y reclass
378 ;;
379 esac
380
381 echo -e "\nInstalling salt master ...\n"
382 # TODO: replace with saltstack bootstrap script
383
384 if [ "$SALT_VERSION" == "latest" ]; then
385 pip install salt
386 else
387 pip install salt==$SALT_VERSION
388 fi
389
390 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
391 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
392
393 which reclass-salt || {
394 test -e /usr/share/reclass/reclass-salt && {
395 ln -fs /usr/share/reclass/reclass-salt /usr/bin
396 }
397 }
398
399 configure_salt_master
400
401 echo -e "\nRestarting services ...\n"
402 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
403 $SVCTOOL salt-master restart
404}
405
406
407
408install_salt_minion_pkg()
409{
410
411 configure_pkg_repo
412
413 echo -e "\nInstalling salt minion ...\n"
414
415 case $PLATFORM_FAMILY in
416 debian)
417 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
418 ;;
419 rhel)
420 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
421 ;;
422 esac
423
424
425 configure_salt_minion
426
427 $SVCTOOL salt-minion restart
428}
429
430install_salt_minion_pip()
431{
432 echo -e "\nInstalling salt minion ...\n"
433
434 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
435 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
436
437 configure_salt_minion
438 $SVCTOOL salt-minion restart
439}
440
441
442install_salt_formula_pkg()
443{
444 configure_pkg_repo
445
446 case $PLATFORM_FAMILY in
447 debian)
448 echo "Configuring necessary formulas ..."
449
450 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
451 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
452 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
453 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
454 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
455 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
456 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
457 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
458 exit 1
459 fi
460 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
461 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
462 done
463 ;;
464 rhel)
465 # TODO
466 ;;
467 esac
468
469 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
470 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
471}
472
473install_salt_formula_git()
474{
475 echo "Configuring necessary formulas ..."
476
477 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
478 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
479 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
480 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
481 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
482 _BRANCH=${FORMULAS_BRANCH}
483 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
484 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
485 # Fallback to the master branch if the branch doesn't exist for this repository
486 _BRANCH=master
487 fi
488 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
489 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
490 exit 1
491 fi
492 } || {
493 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
494 git fetch origin/${_BRANCH} || git fetch --all
495 git checkout ${_BRANCH} && git pull || git pull;
496 cd -
497 }
498 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
499 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
500 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
501 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
502 done
503
504 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
505 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
506}
507
508
509saltmaster_bootstrap() {
510
511 log_info "Salt master setup"
512 test -n "$MASTER_HOSTNAME" || exit 1
513
514 clone_reclass
515 # override some envs from cluster level *.env, use with care
516 source_local_envs
517
518 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
519 pkill -9 salt-minion
520 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
521 export SALT_MASTER=localhost
522 export MINION_ID=${MASTER_HOSTNAME}
523 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
524 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
525 fi
526 # call local "setup() master"
527 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
528 if ! setup master; then
529 #cat /tmp/salt-master-setup.log
530 log_err "salt master setup() failed."
531 exit 1
532 else
533 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
534 fi
535 }
536
537 log_info "Clean up generated"
538 cd $RECLASS_ROOT
539 $SUDO rm -rf $RECLASS_ROOT/nodes/_generated/*
540
541 log_info "Re/starting salt services"
542 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
543 pkill -9 salt-minion
544 sleep 1
545 $SUDO service salt-master restart
546 $SUDO service salt-minion restart
547 sleep 10
548}
549
550# Init salt master
551saltmaster_init() {
552
553 log_info "Runing saltmaster states"
554 test -n "$MASTER_HOSTNAME" || exit 1
555
556 set -e
557 $SUDO salt-call saltutil.sync_all >/dev/null
558
559 # TODO: Placeholder update saltmaster spec (nodes/FQDN.yml) to be able to bootstrap with minimal configuration
560 # (ie: with linux, git, salt formulas)
561
562 #log_info "Verify SaltMaster, before salt-master is fully initialized"
563 #if ! $SUDO reclass-salt -p ${MASTER_HOSTNAME} &> /tmp/${MASTER_HOSTNAME}.pillar;then
564 # log_warn "Node verification before initialization failed."; cat /tmp/${MASTER_HOSTNAME}.pillar;
565 #fi
566
567 log_info "State: salt.master.env"
568 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env; then
569 log_err "State salt.master.env failed, keep your eyes wide open."
570 fi
571
572 log_info "State: salt.master.pillar"
573 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar='{"reclass":{"storage":{"data_source":{"engine":"local"}}}}'
574 # Note: sikp reclass data dir states
575 # in order to avoid pull from configured repo/branch
576
577 # Revert temporary SaltMaster minimal configuration, if any
578 pushd $RECLASS_ROOT
579 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
580 git status || true
581 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from salt-master-setup.sh call)"
582 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
583 git checkout -- $RECLASS_ROOT/nodes || true
584 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
585 log_info "State: salt.master.env"
586 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env; then
587 log_err "State salt.master.env failed, keep your eyes wide open."
588 fi
589 log_info "State: salt.master.pillar"
590 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar='{"reclass":{"storage":{"data_source":{"engine":"local"}}}}'
591 fi
592 popd
593
594 log_info "State: salt.master.storage.node"
595 set +e
596 $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
597 ret=$?
598 set -e
599
600 if [[ $ret -eq 2 ]]; then
601 log_err "State reclass.storage.node failed with exit code 2 but continuing."
602 elif [[ $ret -ne 0 ]]; then
603 log_err "State reclass.storage.node failed with exit code $ret"
604 exit 1
605 fi
606
607 log_info "Re/starting salt services"
608 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
609 $SUDO service salt-minion restart >/dev/null
610 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
611
612 verify_salt_master
613 set +e
614
615}
616
617
618function verify_salt_master() {
619 set -e
620
621 log_info "Verify Salt master"
622 test -n "$MASTER_HOSTNAME" || exit 1
623
624 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
625 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
626 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
627 $SUDO salt-call --no-color grains.items
628 $SUDO salt-call --no-color pillar.data
629 fi
630 if ! $SUDO reclass --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
631 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
632 exit 1
633 fi
634}
635
636function verify_salt_minion() {
637 set -e
638 node=$1
639 log_info "Verifying ${node}"
640 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
641 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
642 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
643 fi
644 if ! $SUDO reclass --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
645 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
646 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
647 exit 1
648 fi
649 fi
650}
651
652function verify_salt_minions() {
653 #set -e
654 NODES=$(find $RECLASS_ROOT/nodes/ -name "*.yml" | grep -v "cfg")
655 log_info "Verifying minions: $(echo ${NODES}|xargs)"
656
657 # Parallel
658 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
659 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
660
661 function filterFails() {
662 grep -v '/grains' | tee -a $1 | tail -n20
663 }
664
665 log_info "Verify nodes"
666 passed=0
667 for node in ${NODES}; do
668 node=$(basename $node .yml)
669
670 # filter first in cluster.. ctl-01, mon-01, etc..
671 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
672 verify_salt_minion ${node} || continue
673 else
674 echo Skipped $node.
675 fi
676 passed=$(($passed+1))
677 done
678 # fail on failures
679 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
680 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
681 test ! $passed -lt $total || {
682 tail -n50 /tmp/*.pillar_verify
683 return 1
684 }
685}
686
687
688
689##########################################
690# To install salt master/minon
691
692function install() {
693 setup $@
694}
695
696function setup() {
697 # CLI
698 while [ x"$1" != x"" ]; do
699 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
700
701 case $1 in
702 master )
703 install_salt_master_$SALT_SOURCE
704 install_salt_minion_$SALT_SOURCE
705 install_salt_formula_$FORMULAS_SOURCE
706 ;;
707 minion )
708 install_salt_minion_$SALT_SOURCE
709 ;;
710 esac
711 shift
712 done
713 echo DONE
714}
715
716function bootstrap() {
717 log_info "Bootstrap & verification of SaltMaster and configured minions."
718 trap _atexit INT TERM EXIT
719
720 system_config_master
721 saltmaster_bootstrap &&\
722 saltmaster_init &&\
723 verify_salt_minions
724}
725
726function default() {
727 bootstrap $@
728}
729
730
731##########################################
732[[ "$0" != "$BASH_SOURCE" ]] || {
733# unless file is being sourced
734 default $@
735}