blob: f6428cea35a27cbd46e7f98683c181446348886e [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
Petr Michalecda014202017-08-18 11:26:07 +0200197system_config_ssh_conf() {
198 for conf in ~/.ssh/config /root/.ssh/config; do
199 $SUDO mkdir -p $(dirname $conf)
200 if ! grep StrictHostKeyChecking $conf; then
201 # this should be used only in CI environment
202 echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee $conf >/dev/null
203 fi
204 done
205 if ! grep github.com ~/.ssh/known_hosts; then
206 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
207 fi
208}
209
210system_config_salt_modules_prereq() {
211 # salt-formulas custom modules dependencies, etc:
212 $SUDO $PKGTOOL install -y iproute2 curl sudo apt-transport-https python-psutil python-apt python-m2crypto python-oauth python-pip &>/dev/null
213}
214
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200215system_config_minion() {
216 log_info "System configuration salt minion"
217}
218
219system_config_master() {
220 log_info "System configuration salt master"
221
Petr Michalecda014202017-08-18 11:26:07 +0200222 system_config_salt_modules_prereq
223 system_config_ssh_conf
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200224
225 $SUDO mkdir -p $RECLASS_ROOT/classes/service
Petr Michalecda014202017-08-18 11:26:07 +0200226 $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated
227
228 if ! grep '127.0.1.2.*salt' /etc/hosts; then
229 echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null
230 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200231
232 which reclass || $SUDO $PKGTOOL install -y reclass
233
234 which reclass-salt || {
235 test -e /usr/share/reclass/reclass-salt && {
236 ln -fs /usr/share/reclass/reclass-salt /usr/bin
237 }
238 }
239}
240
241configure_salt_master()
242{
243
244 echo "Configuring salt-master ..."
245
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200246 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
247 IGNORE_CLASS_NOTFOUND="ignore_class_notfound: True"
248 fi
249
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200250 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
251 cat <<-EOF > /etc/salt/master.d/master.conf
252 file_roots:
253 base:
254 - /usr/share/salt-formulas/env
255 prd:
256 - /srv/salt/env/prd
257 dev:
258 - /srv/salt/env/dev
259 pillar_opts: False
260 open_mode: True
261 reclass: &reclass
262 storage_type: yaml_fs
263 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200264 ${IGNORE_CLASS_NOTFOUND}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200265 ext_pillar:
266 - reclass: *reclass
267 master_tops:
268 reclass: *reclass
269EOF
270
271 echo "Configuring reclass ..."
272
273 [ ! -d /etc/reclass ] && mkdir /etc/reclass
274 cat <<-EOF > /etc/reclass/reclass-config.yml
275 storage_type: yaml_fs
276 pretty_print: True
277 output: yaml
278 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200279 ${IGNORE_CLASS_NOTFOUND}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200280EOF
281
282 clone_reclass
283 # override some envs from cluster level *.env, use with care
284 source_local_envs
285
286 cd ${RECLASS_ROOT}
287 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
288 # Possibly subrepo checkout needed
289 git submodule update --init --recursive
290 fi
291
292 #sed -ie "s#\(reclass_data_revision.\).*#\1 $RECLASS_BRANCH#" $(find nodes -name ${MASTER_HOSTNAME}.yml|tail -n1)
293
Petr Michalec212cc6a2017-08-17 21:39:40 +0200294 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200295 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200296 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200297 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
298 cat <<-EOF > ${CONFIG}
299 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200300 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200301 parameters:
302 _param:
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200303 single_address: $SALT_MASTER
304 salt_master_host: $SALT_MASTER
305 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200306 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
307 reclass_data_revision: ${RECLASS_BRANCH:-master}
308 reclass_data_repository: "$RECLASS_ADDRESS"
309 reclass_config_master: $SALT_MASTER
310 linux_system_codename: ${DISTRIB_CODENAME}
311 cluster_name: ${CLUSTER_NAME}
312 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200313 linux:
314 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200315 name: ${HOSTNAME:-cfg01}
316 domain: ${DOMAIN:-$CLUSTER_NAME.local}
317 #reclass:
318 # storage:
319 # data_source:
320 # engine: local
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200321 # ########
322EOF
323
324 if [ "$SALT_VERSION" == "latest" ]; then
325 VERSION=""
326 else
327 VERSION="version: $SALT_VERSION"
328 fi
329
330 cat <<-EOF >> ${CONFIG}
331 salt:
332 master:
333 accept_policy: open_mode
334 source:
335 engine: $SALT_SOURCE
336 $VERSION
337 minion:
338 source:
339 engine: $SALT_SOURCE
340 $VERSION
341 # ########
342 # vim: ft=yaml sw=2 ts=2 sts=2
343EOF
344 fi
345}
346
347configure_salt_minion()
348{
349 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
350 cat <<-EOF > /etc/salt/minion.d/minion.conf
351 master: $SALT_MASTER
352 id: $MINION_ID
353 EOF
354}
355
356
357install_salt_master_pkg()
358{
359 echo -e "\nPreparing base OS repository ...\n"
360
361 configure_pkg_repo
362
363 echo -e "\nInstalling salt master ...\n"
364
365 case $PLATFORM_FAMILY in
366 debian)
367 $SUDO apt-get install -y git
368 which reclass || $SUDO apt install -qqq -y reclass
369 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
370 ;;
371 rhel)
372 yum install -y git
373 which reclass || $SUDO yum install -y reclass
374 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
375 ;;
376 esac
377
378 which reclass-salt || {
379 test -e /usr/share/reclass/reclass-salt && {
380 ln -fs /usr/share/reclass/reclass-salt /usr/bin
381 }
382 }
383
384 configure_salt_master
385
386 echo -e "\nRestarting services ...\n"
387 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
388 $SVCTOOL salt-master restart
389}
390
391install_salt_master_pip()
392{
393 echo -e "\nPreparing base OS repository ...\n"
394
395 case $PLATFORM_FAMILY in
396 debian)
397 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
398 which reclass || $SUDO apt-get install -y reclass
399 ;;
400 rhel)
401 $SUDO yum install -y git
402 which reclass || $SUDO yum install -y reclass
403 ;;
404 esac
405
406 echo -e "\nInstalling salt master ...\n"
407 # TODO: replace with saltstack bootstrap script
408
409 if [ "$SALT_VERSION" == "latest" ]; then
410 pip install salt
411 else
412 pip install salt==$SALT_VERSION
413 fi
414
415 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
416 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
417
418 which reclass-salt || {
419 test -e /usr/share/reclass/reclass-salt && {
420 ln -fs /usr/share/reclass/reclass-salt /usr/bin
421 }
422 }
423
424 configure_salt_master
425
426 echo -e "\nRestarting services ...\n"
427 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
428 $SVCTOOL salt-master restart
429}
430
431
432
433install_salt_minion_pkg()
434{
435
436 configure_pkg_repo
437
438 echo -e "\nInstalling salt minion ...\n"
439
440 case $PLATFORM_FAMILY in
441 debian)
442 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
443 ;;
444 rhel)
445 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
446 ;;
447 esac
448
449
450 configure_salt_minion
451
452 $SVCTOOL salt-minion restart
453}
454
455install_salt_minion_pip()
456{
457 echo -e "\nInstalling salt minion ...\n"
458
459 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
460 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
461
462 configure_salt_minion
463 $SVCTOOL salt-minion restart
464}
465
466
467install_salt_formula_pkg()
468{
469 configure_pkg_repo
470
471 case $PLATFORM_FAMILY in
472 debian)
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 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
481 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
482 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
483 exit 1
484 fi
485 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
486 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
487 done
488 ;;
489 rhel)
490 # TODO
491 ;;
492 esac
493
494 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
495 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
496}
497
498install_salt_formula_git()
499{
500 echo "Configuring necessary formulas ..."
501
502 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
503 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
504 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
505 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
506 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
507 _BRANCH=${FORMULAS_BRANCH}
508 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
509 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
510 # Fallback to the master branch if the branch doesn't exist for this repository
511 _BRANCH=master
512 fi
513 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
514 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
515 exit 1
516 fi
517 } || {
518 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
519 git fetch origin/${_BRANCH} || git fetch --all
520 git checkout ${_BRANCH} && git pull || git pull;
521 cd -
522 }
523 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
524 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
525 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
526 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
527 done
528
529 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
530 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
531}
532
533
534saltmaster_bootstrap() {
535
536 log_info "Salt master setup"
537 test -n "$MASTER_HOSTNAME" || exit 1
538
539 clone_reclass
540 # override some envs from cluster level *.env, use with care
541 source_local_envs
542
543 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
544 pkill -9 salt-minion
545 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
546 export SALT_MASTER=localhost
547 export MINION_ID=${MASTER_HOSTNAME}
548 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
549 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
550 fi
551 # call local "setup() master"
552 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
553 if ! setup master; then
554 #cat /tmp/salt-master-setup.log
555 log_err "salt master setup() failed."
556 exit 1
557 else
558 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
559 fi
560 }
561
562 log_info "Clean up generated"
563 cd $RECLASS_ROOT
564 $SUDO rm -rf $RECLASS_ROOT/nodes/_generated/*
565
566 log_info "Re/starting salt services"
567 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
568 pkill -9 salt-minion
569 sleep 1
570 $SUDO service salt-master restart
571 $SUDO service salt-minion restart
572 sleep 10
573}
574
575# Init salt master
576saltmaster_init() {
577
578 log_info "Runing saltmaster states"
579 test -n "$MASTER_HOSTNAME" || exit 1
580
581 set -e
582 $SUDO salt-call saltutil.sync_all >/dev/null
583
584 # TODO: Placeholder update saltmaster spec (nodes/FQDN.yml) to be able to bootstrap with minimal configuration
585 # (ie: with linux, git, salt formulas)
586
587 #log_info "Verify SaltMaster, before salt-master is fully initialized"
588 #if ! $SUDO reclass-salt -p ${MASTER_HOSTNAME} &> /tmp/${MASTER_HOSTNAME}.pillar;then
589 # log_warn "Node verification before initialization failed."; cat /tmp/${MASTER_HOSTNAME}.pillar;
590 #fi
591
Petr Michalecda014202017-08-18 11:26:07 +0200592
593 # workarond isolated and not fully bootstraped environments
Petr Michalec571d6b82017-08-18 13:25:24 +0200594 PILLAR='{"salt":{"master":{"pillar":{"reclass":{"ignore_class_notfound": "'${RECLASS_IGNORE_CLASS_NOTFOUND:-False}'"}}}}, "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalecda014202017-08-18 11:26:07 +0200595
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200596 log_info "State: salt.master.env"
Petr Michalecda014202017-08-18 11:26:07 +0200597 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env pillar="$PILLAR"; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200598 log_err "State salt.master.env failed, keep your eyes wide open."
599 fi
600
601 log_info "State: salt.master.pillar"
Petr Michalecda014202017-08-18 11:26:07 +0200602 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar="$PILLAR"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200603 # Note: sikp reclass data dir states
604 # in order to avoid pull from configured repo/branch
605
606 # Revert temporary SaltMaster minimal configuration, if any
607 pushd $RECLASS_ROOT
608 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
609 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200610 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200611 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
612 git checkout -- $RECLASS_ROOT/nodes || true
613 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
614 log_info "State: salt.master.env"
Petr Michalecda014202017-08-18 11:26:07 +0200615 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env pillar="$PILLAR"; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200616 log_err "State salt.master.env failed, keep your eyes wide open."
617 fi
618 log_info "State: salt.master.pillar"
Petr Michalecda014202017-08-18 11:26:07 +0200619 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar="$PILLAR"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200620 fi
621 popd
622
Petr Michalecda014202017-08-18 11:26:07 +0200623 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
624 log_info "State: salt.master.service"
Petr Michalec7f3e7e72017-08-18 13:49:24 +0200625 $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalecda014202017-08-18 11:26:07 +0200626
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200627 log_info "State: salt.master.storage.node"
628 set +e
Petr Michaleca5141342017-08-16 12:55:20 +0200629 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200630 $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
631 ret=$?
632 set -e
633
634 if [[ $ret -eq 2 ]]; then
635 log_err "State reclass.storage.node failed with exit code 2 but continuing."
636 elif [[ $ret -ne 0 ]]; then
637 log_err "State reclass.storage.node failed with exit code $ret"
638 exit 1
639 fi
640
641 log_info "Re/starting salt services"
642 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
643 $SUDO service salt-minion restart >/dev/null
644 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
645
646 verify_salt_master
647 set +e
648
649}
650
651
652function verify_salt_master() {
653 set -e
654
655 log_info "Verify Salt master"
656 test -n "$MASTER_HOSTNAME" || exit 1
657
658 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200659 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
660 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200661 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
662 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
663 $SUDO salt-call --no-color grains.items
664 $SUDO salt-call --no-color pillar.data
665 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200666 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200667 if ! $SUDO reclass --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
668 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
669 exit 1
670 fi
671}
672
673function verify_salt_minion() {
674 set -e
675 node=$1
676 log_info "Verifying ${node}"
677 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
678 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
679 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
680 fi
681 if ! $SUDO reclass --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
682 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
683 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
684 exit 1
685 fi
686 fi
687}
688
689function verify_salt_minions() {
690 #set -e
691 NODES=$(find $RECLASS_ROOT/nodes/ -name "*.yml" | grep -v "cfg")
692 log_info "Verifying minions: $(echo ${NODES}|xargs)"
693
694 # Parallel
695 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
696 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
697
698 function filterFails() {
699 grep -v '/grains' | tee -a $1 | tail -n20
700 }
701
702 log_info "Verify nodes"
703 passed=0
704 for node in ${NODES}; do
705 node=$(basename $node .yml)
706
707 # filter first in cluster.. ctl-01, mon-01, etc..
708 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
709 verify_salt_minion ${node} || continue
710 else
711 echo Skipped $node.
712 fi
713 passed=$(($passed+1))
714 done
715 # fail on failures
716 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
717 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
718 test ! $passed -lt $total || {
719 tail -n50 /tmp/*.pillar_verify
720 return 1
721 }
722}
723
724
725
726##########################################
727# To install salt master/minon
728
729function install() {
730 setup $@
731}
732
733function setup() {
734 # CLI
735 while [ x"$1" != x"" ]; do
736 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
737
738 case $1 in
739 master )
740 install_salt_master_$SALT_SOURCE
741 install_salt_minion_$SALT_SOURCE
742 install_salt_formula_$FORMULAS_SOURCE
743 ;;
744 minion )
745 install_salt_minion_$SALT_SOURCE
746 ;;
747 esac
748 shift
749 done
750 echo DONE
751}
752
753function bootstrap() {
754 log_info "Bootstrap & verification of SaltMaster and configured minions."
755 trap _atexit INT TERM EXIT
756
757 system_config_master
758 saltmaster_bootstrap &&\
759 saltmaster_init &&\
Petr Michaleca5141342017-08-16 12:55:20 +0200760 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200761}
762
763function default() {
764 bootstrap $@
765}
766
767
768##########################################
769[[ "$0" != "$BASH_SOURCE" ]] || {
770# unless file is being sourced
771 default $@
772}