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