blob: e72b030380b0d27ca4c303e45945b3f5aecf3581 [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 Michaleca5141342017-08-16 12:55:20 +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}
241 ext_pillar:
242 - reclass: *reclass
243 master_tops:
244 reclass: *reclass
245EOF
246
247 echo "Configuring reclass ..."
248
249 [ ! -d /etc/reclass ] && mkdir /etc/reclass
250 cat <<-EOF > /etc/reclass/reclass-config.yml
251 storage_type: yaml_fs
252 pretty_print: True
253 output: yaml
254 inventory_base_uri: ${RECLASS_ROOT}
255EOF
256
257 clone_reclass
258 # override some envs from cluster level *.env, use with care
259 source_local_envs
260
261 cd ${RECLASS_ROOT}
262 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
263 # Possibly subrepo checkout needed
264 git submodule update --init --recursive
265 fi
266
267 #sed -ie "s#\(reclass_data_revision.\).*#\1 $RECLASS_BRANCH#" $(find nodes -name ${MASTER_HOSTNAME}.yml|tail -n1)
268
269 mkdir -vp ${RECLASS_ROOT}/nodes
270 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
271 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/${MINION_ID}.yml}
272 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
273 cat <<-EOF > ${CONFIG}
274 classes:
275 - service.git.client
276 - system.linux.system.single
277 - system.salt.master.single
278 - system.salt.master.$FORMULAS_SOURCE
279 - system.reclass.storage.salt
280 parameters:
281 _param:
282 reclass_data_repository: "$RECLASS_ADDRESS"
283 reclass_data_revision: ${RECLASS_BRANCH:-master}
284 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
285 reclass_config_master: $SALT_MASTER
286 single_address: $SALT_MASTER
287 salt_master_host: $SALT_MASTER
288 salt_master_base_environment: $SALT_ENV
289 linux_system_codename: $DISTRIB_CODENAME
290 linux:
291 system:
292 name: $MINION_ID
293 domain: $DOMAIN
294 # ########
295EOF
296
297 if [ "$SALT_VERSION" == "latest" ]; then
298 VERSION=""
299 else
300 VERSION="version: $SALT_VERSION"
301 fi
302
303 cat <<-EOF >> ${CONFIG}
304 salt:
305 master:
306 accept_policy: open_mode
307 source:
308 engine: $SALT_SOURCE
309 $VERSION
310 minion:
311 source:
312 engine: $SALT_SOURCE
313 $VERSION
314 # ########
315 # vim: ft=yaml sw=2 ts=2 sts=2
316EOF
317 fi
318}
319
320configure_salt_minion()
321{
322 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
323 cat <<-EOF > /etc/salt/minion.d/minion.conf
324 master: $SALT_MASTER
325 id: $MINION_ID
326 EOF
327}
328
329
330install_salt_master_pkg()
331{
332 echo -e "\nPreparing base OS repository ...\n"
333
334 configure_pkg_repo
335
336 echo -e "\nInstalling salt master ...\n"
337
338 case $PLATFORM_FAMILY in
339 debian)
340 $SUDO apt-get install -y git
341 which reclass || $SUDO apt install -qqq -y reclass
342 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
343 ;;
344 rhel)
345 yum install -y git
346 which reclass || $SUDO yum install -y reclass
347 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
348 ;;
349 esac
350
351 which reclass-salt || {
352 test -e /usr/share/reclass/reclass-salt && {
353 ln -fs /usr/share/reclass/reclass-salt /usr/bin
354 }
355 }
356
357 configure_salt_master
358
359 echo -e "\nRestarting services ...\n"
360 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
361 $SVCTOOL salt-master restart
362}
363
364install_salt_master_pip()
365{
366 echo -e "\nPreparing base OS repository ...\n"
367
368 case $PLATFORM_FAMILY in
369 debian)
370 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
371 which reclass || $SUDO apt-get install -y reclass
372 ;;
373 rhel)
374 $SUDO yum install -y git
375 which reclass || $SUDO yum install -y reclass
376 ;;
377 esac
378
379 echo -e "\nInstalling salt master ...\n"
380 # TODO: replace with saltstack bootstrap script
381
382 if [ "$SALT_VERSION" == "latest" ]; then
383 pip install salt
384 else
385 pip install salt==$SALT_VERSION
386 fi
387
388 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
389 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
390
391 which reclass-salt || {
392 test -e /usr/share/reclass/reclass-salt && {
393 ln -fs /usr/share/reclass/reclass-salt /usr/bin
394 }
395 }
396
397 configure_salt_master
398
399 echo -e "\nRestarting services ...\n"
400 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
401 $SVCTOOL salt-master restart
402}
403
404
405
406install_salt_minion_pkg()
407{
408
409 configure_pkg_repo
410
411 echo -e "\nInstalling salt minion ...\n"
412
413 case $PLATFORM_FAMILY in
414 debian)
415 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
416 ;;
417 rhel)
418 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
419 ;;
420 esac
421
422
423 configure_salt_minion
424
425 $SVCTOOL salt-minion restart
426}
427
428install_salt_minion_pip()
429{
430 echo -e "\nInstalling salt minion ...\n"
431
432 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
433 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
434
435 configure_salt_minion
436 $SVCTOOL salt-minion restart
437}
438
439
440install_salt_formula_pkg()
441{
442 configure_pkg_repo
443
444 case $PLATFORM_FAMILY in
445 debian)
446 echo "Configuring necessary formulas ..."
447
448 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
449 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
450 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
451 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
452 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
453 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
454 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
455 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
456 exit 1
457 fi
458 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
459 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
460 done
461 ;;
462 rhel)
463 # TODO
464 ;;
465 esac
466
467 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
468 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
469}
470
471install_salt_formula_git()
472{
473 echo "Configuring necessary formulas ..."
474
475 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
476 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
477 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
478 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
479 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
480 _BRANCH=${FORMULAS_BRANCH}
481 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
482 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
483 # Fallback to the master branch if the branch doesn't exist for this repository
484 _BRANCH=master
485 fi
486 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
487 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
488 exit 1
489 fi
490 } || {
491 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
492 git fetch origin/${_BRANCH} || git fetch --all
493 git checkout ${_BRANCH} && git pull || git pull;
494 cd -
495 }
496 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
497 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
498 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
499 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
500 done
501
502 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
503 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
504}
505
506
507saltmaster_bootstrap() {
508
509 log_info "Salt master setup"
510 test -n "$MASTER_HOSTNAME" || exit 1
511
512 clone_reclass
513 # override some envs from cluster level *.env, use with care
514 source_local_envs
515
516 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
517 pkill -9 salt-minion
518 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
519 export SALT_MASTER=localhost
520 export MINION_ID=${MASTER_HOSTNAME}
521 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
522 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
523 fi
524 # call local "setup() master"
525 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
526 if ! setup master; then
527 #cat /tmp/salt-master-setup.log
528 log_err "salt master setup() failed."
529 exit 1
530 else
531 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
532 fi
533 }
534
535 log_info "Clean up generated"
536 cd $RECLASS_ROOT
537 $SUDO rm -rf $RECLASS_ROOT/nodes/_generated/*
538
539 log_info "Re/starting salt services"
540 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
541 pkill -9 salt-minion
542 sleep 1
543 $SUDO service salt-master restart
544 $SUDO service salt-minion restart
545 sleep 10
546}
547
548# Init salt master
549saltmaster_init() {
550
551 log_info "Runing saltmaster states"
552 test -n "$MASTER_HOSTNAME" || exit 1
553
554 set -e
555 $SUDO salt-call saltutil.sync_all >/dev/null
556
557 # TODO: Placeholder update saltmaster spec (nodes/FQDN.yml) to be able to bootstrap with minimal configuration
558 # (ie: with linux, git, salt formulas)
559
560 #log_info "Verify SaltMaster, before salt-master is fully initialized"
561 #if ! $SUDO reclass-salt -p ${MASTER_HOSTNAME} &> /tmp/${MASTER_HOSTNAME}.pillar;then
562 # log_warn "Node verification before initialization failed."; cat /tmp/${MASTER_HOSTNAME}.pillar;
563 #fi
564
565 log_info "State: salt.master.env"
566 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env; then
567 log_err "State salt.master.env failed, keep your eyes wide open."
568 fi
569
570 log_info "State: salt.master.pillar"
571 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar='{"reclass":{"storage":{"data_source":{"engine":"local"}}}}'
572 # Note: sikp reclass data dir states
573 # in order to avoid pull from configured repo/branch
574
575 # Revert temporary SaltMaster minimal configuration, if any
576 pushd $RECLASS_ROOT
577 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
578 git status || true
579 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from salt-master-setup.sh call)"
580 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
581 git checkout -- $RECLASS_ROOT/nodes || true
582 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
583 log_info "State: salt.master.env"
584 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env; then
585 log_err "State salt.master.env failed, keep your eyes wide open."
586 fi
587 log_info "State: salt.master.pillar"
588 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar='{"reclass":{"storage":{"data_source":{"engine":"local"}}}}'
589 fi
590 popd
591
592 log_info "State: salt.master.storage.node"
593 set +e
Petr Michaleca5141342017-08-16 12:55:20 +0200594 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200595 $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
596 ret=$?
597 set -e
598
599 if [[ $ret -eq 2 ]]; then
600 log_err "State reclass.storage.node failed with exit code 2 but continuing."
601 elif [[ $ret -ne 0 ]]; then
602 log_err "State reclass.storage.node failed with exit code $ret"
603 exit 1
604 fi
605
606 log_info "Re/starting salt services"
607 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
608 $SUDO service salt-minion restart >/dev/null
609 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
610
611 verify_salt_master
612 set +e
613
614}
615
616
617function verify_salt_master() {
618 set -e
619
620 log_info "Verify Salt master"
621 test -n "$MASTER_HOSTNAME" || exit 1
622
623 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
624 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
625 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
Petr Michaleca5141342017-08-16 12:55:20 +0200626 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
627 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200628 $SUDO salt-call --no-color grains.items
629 $SUDO salt-call --no-color pillar.data
630 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200631 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200632 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 &&\
Petr Michaleca5141342017-08-16 12:55:20 +0200725 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200726}
727
728function default() {
729 bootstrap $@
730}
731
732
733##########################################
734[[ "$0" != "$BASH_SOURCE" ]] || {
735# unless file is being sourced
736 default $@
737}