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