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