blob: 627d7219ef09171630152c8357a95399fe28b14d [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)
chnyda86d63b42017-08-31 10:05:00 +020066FORMULAS_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 rsync docker keepalived aptly jenkins gerrit artifactory influxdb}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020067# 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
Petr Michalec24b30e82017-08-21 10:59:18 +0200126log_debug() {
127 echo -e "${CYAN}[WARN] $* ${NC}"
128}
129
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200130log_err() {
131 echo -e "${RED}[ERROR] $* ${NC}" >&2
132}
133
134configure_pkg_repo()
135{
136
137 case $PLATFORM_FAMILY in
138 debian)
139 if [ -n "$APT_REPOSITORY_PPA" ]; then
140 which add-apt-repository || $SUDO apt-get install -y software-properties-common
141 $SUDO add-apt-repository -y ppa:${APT_REPOSITORY_PPA}
142 else
143 echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/bootstrap.list >/dev/null
144 curl -sL $APT_REPOSITORY_GPG | $SUDO apt-key add -
145 fi
146 $SUDO apt-get clean
147 $SUDO apt-get update
148 ;;
149 rhel)
150 $SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm
151 $SUDO yum clean all
152 ;;
153 esac
154
155}
156
157_atexit() {
158 RETVAL=$?
159 trap true INT TERM EXIT
160
161 if [ $RETVAL -ne 0 ]; then
162 log_err "Execution failed"
163 else
164 log_info "Execution successful"
165 fi
166 return $RETVAL
167}
168
169retry() {
170 local tries
171 if [[ $1 =~ ^[0-9]+$ ]]; then
172 tries=$1; shift
173 else
174 tries=3
175 fi
176 for i in $(seq 1 $tries); do
177 "$@" && return 0 || sleep $i
178 done
179 return 1
180}
181
182function clone_reclass() {
183 if [ ! -d ${RECLASS_ROOT} ]; then
184 # No reclass at all, clone from given address
185 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
186 if echo ${RECLASS_BRANCH:-master} | egrep -q "^refs"; then
187 git clone ${RECLASS_ADDRESS} ${RECLASS_ROOT}
188 cd ${RECLASS_ROOT}
189 git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH:-master} && git checkout FETCH_HEAD
190 cd -
191 else
192 git clone -b ${RECLASS_BRANCH:-master} ${RECLASS_ADDRESS} ${RECLASS_ROOT};
193 fi;
194 fi;
195}
196
197
198##########################################
199# Main calls
200
Petr Michalecda014202017-08-18 11:26:07 +0200201system_config_ssh_conf() {
202 for conf in ~/.ssh/config /root/.ssh/config; do
203 $SUDO mkdir -p $(dirname $conf)
204 if ! grep StrictHostKeyChecking $conf; then
205 # this should be used only in CI environment
206 echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee $conf >/dev/null
207 fi
208 done
209 if ! grep github.com ~/.ssh/known_hosts; then
210 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
211 fi
212}
213
214system_config_salt_modules_prereq() {
215 # salt-formulas custom modules dependencies, etc:
216 $SUDO $PKGTOOL install -y iproute2 curl sudo apt-transport-https python-psutil python-apt python-m2crypto python-oauth python-pip &>/dev/null
217}
218
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200219system_config_minion() {
220 log_info "System configuration salt minion"
221}
222
223system_config_master() {
224 log_info "System configuration salt master"
225
Petr Michalecda014202017-08-18 11:26:07 +0200226 system_config_salt_modules_prereq
227 system_config_ssh_conf
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200228
229 $SUDO mkdir -p $RECLASS_ROOT/classes/service
Petr Michalecda014202017-08-18 11:26:07 +0200230 $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated
231
232 if ! grep '127.0.1.2.*salt' /etc/hosts; then
233 echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null
234 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200235
236 which reclass || $SUDO $PKGTOOL install -y reclass
237
238 which reclass-salt || {
239 test -e /usr/share/reclass/reclass-salt && {
240 ln -fs /usr/share/reclass/reclass-salt /usr/bin
241 }
242 }
243}
244
245configure_salt_master()
246{
247
248 echo "Configuring salt-master ..."
249
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200250 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
251 IGNORE_CLASS_NOTFOUND="ignore_class_notfound: True"
252 fi
253
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200254 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
255 cat <<-EOF > /etc/salt/master.d/master.conf
256 file_roots:
257 base:
258 - /usr/share/salt-formulas/env
259 prd:
260 - /srv/salt/env/prd
261 dev:
262 - /srv/salt/env/dev
263 pillar_opts: False
264 open_mode: True
265 reclass: &reclass
266 storage_type: yaml_fs
267 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200268 ${IGNORE_CLASS_NOTFOUND}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200269 ext_pillar:
270 - reclass: *reclass
271 master_tops:
272 reclass: *reclass
273EOF
274
275 echo "Configuring reclass ..."
276
277 [ ! -d /etc/reclass ] && mkdir /etc/reclass
278 cat <<-EOF > /etc/reclass/reclass-config.yml
279 storage_type: yaml_fs
280 pretty_print: True
281 output: yaml
282 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200283 ${IGNORE_CLASS_NOTFOUND}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200284EOF
285
286 clone_reclass
287 # override some envs from cluster level *.env, use with care
288 source_local_envs
289
290 cd ${RECLASS_ROOT}
291 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
292 # Possibly subrepo checkout needed
293 git submodule update --init --recursive
294 fi
295
296 #sed -ie "s#\(reclass_data_revision.\).*#\1 $RECLASS_BRANCH#" $(find nodes -name ${MASTER_HOSTNAME}.yml|tail -n1)
297
Petr Michalec212cc6a2017-08-17 21:39:40 +0200298 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200299 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200300 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200301 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
Petr Michalec24b30e82017-08-21 10:59:18 +0200302 log_warn "Salt Master node specification has not been found in model."
303 log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200304 cat <<-EOF > ${CONFIG}
305 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200306 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200307 parameters:
308 _param:
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200309 single_address: $SALT_MASTER
310 salt_master_host: $SALT_MASTER
311 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200312 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
313 reclass_data_revision: ${RECLASS_BRANCH:-master}
314 reclass_data_repository: "$RECLASS_ADDRESS"
315 reclass_config_master: $SALT_MASTER
316 linux_system_codename: ${DISTRIB_CODENAME}
317 cluster_name: ${CLUSTER_NAME}
318 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200319 linux:
320 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200321 name: ${HOSTNAME:-cfg01}
322 domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200323 # ########
324EOF
325
326 if [ "$SALT_VERSION" == "latest" ]; then
327 VERSION=""
328 else
329 VERSION="version: $SALT_VERSION"
330 fi
331
332 cat <<-EOF >> ${CONFIG}
333 salt:
334 master:
335 accept_policy: open_mode
336 source:
337 engine: $SALT_SOURCE
338 $VERSION
339 minion:
340 source:
341 engine: $SALT_SOURCE
342 $VERSION
343 # ########
344 # vim: ft=yaml sw=2 ts=2 sts=2
345EOF
346 fi
Petr Michalec24b30e82017-08-21 10:59:18 +0200347
348 log_debug "Salt Master node config yaml:"
349 log_debug "$(cat ${CONFIG})"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200350}
351
352configure_salt_minion()
353{
354 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
355 cat <<-EOF > /etc/salt/minion.d/minion.conf
356 master: $SALT_MASTER
357 id: $MINION_ID
358 EOF
359}
360
361
362install_salt_master_pkg()
363{
364 echo -e "\nPreparing base OS repository ...\n"
365
366 configure_pkg_repo
367
368 echo -e "\nInstalling salt master ...\n"
369
370 case $PLATFORM_FAMILY in
371 debian)
372 $SUDO apt-get install -y git
373 which reclass || $SUDO apt install -qqq -y reclass
374 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
375 ;;
376 rhel)
377 yum install -y git
378 which reclass || $SUDO yum install -y reclass
379 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
380 ;;
381 esac
382
383 which reclass-salt || {
384 test -e /usr/share/reclass/reclass-salt && {
385 ln -fs /usr/share/reclass/reclass-salt /usr/bin
386 }
387 }
388
389 configure_salt_master
390
391 echo -e "\nRestarting services ...\n"
392 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
393 $SVCTOOL salt-master restart
394}
395
396install_salt_master_pip()
397{
398 echo -e "\nPreparing base OS repository ...\n"
399
400 case $PLATFORM_FAMILY in
401 debian)
402 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
403 which reclass || $SUDO apt-get install -y reclass
404 ;;
405 rhel)
406 $SUDO yum install -y git
407 which reclass || $SUDO yum install -y reclass
408 ;;
409 esac
410
411 echo -e "\nInstalling salt master ...\n"
412 # TODO: replace with saltstack bootstrap script
413
414 if [ "$SALT_VERSION" == "latest" ]; then
415 pip install salt
416 else
417 pip install salt==$SALT_VERSION
418 fi
419
420 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
421 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
422
423 which reclass-salt || {
424 test -e /usr/share/reclass/reclass-salt && {
425 ln -fs /usr/share/reclass/reclass-salt /usr/bin
426 }
427 }
428
429 configure_salt_master
430
431 echo -e "\nRestarting services ...\n"
432 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
433 $SVCTOOL salt-master restart
434}
435
436
437
438install_salt_minion_pkg()
439{
440
441 configure_pkg_repo
442
443 echo -e "\nInstalling salt minion ...\n"
444
445 case $PLATFORM_FAMILY in
446 debian)
447 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
448 ;;
449 rhel)
450 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
451 ;;
452 esac
453
454
455 configure_salt_minion
456
457 $SVCTOOL salt-minion restart
458}
459
460install_salt_minion_pip()
461{
462 echo -e "\nInstalling salt minion ...\n"
463
464 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
465 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
466
467 configure_salt_minion
468 $SVCTOOL salt-minion restart
469}
470
471
472install_salt_formula_pkg()
473{
474 configure_pkg_repo
475
476 case $PLATFORM_FAMILY in
477 debian)
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 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
486 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
487 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
488 exit 1
489 fi
490 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
491 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
492 done
493 ;;
494 rhel)
495 # TODO
496 ;;
497 esac
498
499 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
500 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
501}
502
503install_salt_formula_git()
504{
505 echo "Configuring necessary formulas ..."
506
507 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
508 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
509 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
510 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
511 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
512 _BRANCH=${FORMULAS_BRANCH}
513 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
514 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
515 # Fallback to the master branch if the branch doesn't exist for this repository
516 _BRANCH=master
517 fi
518 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
519 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
520 exit 1
521 fi
522 } || {
523 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
524 git fetch origin/${_BRANCH} || git fetch --all
525 git checkout ${_BRANCH} && git pull || git pull;
526 cd -
527 }
528 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
529 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
530 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
531 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
532 done
533
534 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
535 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
536}
537
538
539saltmaster_bootstrap() {
540
541 log_info "Salt master setup"
542 test -n "$MASTER_HOSTNAME" || exit 1
543
544 clone_reclass
545 # override some envs from cluster level *.env, use with care
546 source_local_envs
547
548 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
549 pkill -9 salt-minion
550 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
551 export SALT_MASTER=localhost
552 export MINION_ID=${MASTER_HOSTNAME}
553 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
554 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
555 fi
556 # call local "setup() master"
557 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
558 if ! setup master; then
559 #cat /tmp/salt-master-setup.log
560 log_err "salt master setup() failed."
561 exit 1
562 else
563 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
564 fi
565 }
566
567 log_info "Clean up generated"
568 cd $RECLASS_ROOT
569 $SUDO rm -rf $RECLASS_ROOT/nodes/_generated/*
570
571 log_info "Re/starting salt services"
572 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
573 pkill -9 salt-minion
574 sleep 1
575 $SUDO service salt-master restart
576 $SUDO service salt-minion restart
577 sleep 10
578}
579
580# Init salt master
581saltmaster_init() {
582
583 log_info "Runing saltmaster states"
584 test -n "$MASTER_HOSTNAME" || exit 1
585
586 set -e
587 $SUDO salt-call saltutil.sync_all >/dev/null
588
589 # TODO: Placeholder update saltmaster spec (nodes/FQDN.yml) to be able to bootstrap with minimal configuration
590 # (ie: with linux, git, salt formulas)
591
592 #log_info "Verify SaltMaster, before salt-master is fully initialized"
593 #if ! $SUDO reclass-salt -p ${MASTER_HOSTNAME} &> /tmp/${MASTER_HOSTNAME}.pillar;then
594 # log_warn "Node verification before initialization failed."; cat /tmp/${MASTER_HOSTNAME}.pillar;
595 #fi
596
Petr Michalecda014202017-08-18 11:26:07 +0200597
598 # workarond isolated and not fully bootstraped environments
Petr Michalec571d6b82017-08-18 13:25:24 +0200599 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 +0200600
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200601 log_info "State: salt.master.env"
Petr Michalecda014202017-08-18 11:26:07 +0200602 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env pillar="$PILLAR"; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200603 log_err "State salt.master.env failed, keep your eyes wide open."
604 fi
605
606 log_info "State: salt.master.pillar"
Petr Michalecda014202017-08-18 11:26:07 +0200607 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar="$PILLAR"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200608 # Note: sikp reclass data dir states
609 # in order to avoid pull from configured repo/branch
610
611 # Revert temporary SaltMaster minimal configuration, if any
612 pushd $RECLASS_ROOT
613 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
614 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200615 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200616 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
617 git checkout -- $RECLASS_ROOT/nodes || true
618 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
619 log_info "State: salt.master.env"
Petr Michalecda014202017-08-18 11:26:07 +0200620 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env pillar="$PILLAR"; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200621 log_err "State salt.master.env failed, keep your eyes wide open."
622 fi
623 log_info "State: salt.master.pillar"
Petr Michalecda014202017-08-18 11:26:07 +0200624 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar="$PILLAR"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200625 fi
626 popd
627
Petr Michalecda014202017-08-18 11:26:07 +0200628 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
629 log_info "State: salt.master.service"
Petr Michalec7f3e7e72017-08-18 13:49:24 +0200630 $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalecda014202017-08-18 11:26:07 +0200631
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200632 log_info "State: salt.master.storage.node"
633 set +e
Petr Michaleca5141342017-08-16 12:55:20 +0200634 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200635 $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
636 ret=$?
637 set -e
638
639 if [[ $ret -eq 2 ]]; then
640 log_err "State reclass.storage.node failed with exit code 2 but continuing."
641 elif [[ $ret -ne 0 ]]; then
642 log_err "State reclass.storage.node failed with exit code $ret"
643 exit 1
644 fi
645
646 log_info "Re/starting salt services"
647 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
648 $SUDO service salt-minion restart >/dev/null
649 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
650
651 verify_salt_master
652 set +e
653
654}
655
656
657function verify_salt_master() {
658 set -e
659
660 log_info "Verify Salt master"
661 test -n "$MASTER_HOSTNAME" || exit 1
662
663 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200664 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
665 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200666 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
667 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
668 $SUDO salt-call --no-color grains.items
669 $SUDO salt-call --no-color pillar.data
670 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200671 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200672 if ! $SUDO reclass --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
673 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
674 exit 1
675 fi
676}
677
678function verify_salt_minion() {
679 set -e
680 node=$1
681 log_info "Verifying ${node}"
682 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
683 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
684 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
685 fi
686 if ! $SUDO reclass --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
687 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
688 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
689 exit 1
690 fi
691 fi
692}
693
694function verify_salt_minions() {
695 #set -e
696 NODES=$(find $RECLASS_ROOT/nodes/ -name "*.yml" | grep -v "cfg")
697 log_info "Verifying minions: $(echo ${NODES}|xargs)"
698
699 # Parallel
700 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
701 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
702
703 function filterFails() {
704 grep -v '/grains' | tee -a $1 | tail -n20
705 }
706
707 log_info "Verify nodes"
708 passed=0
709 for node in ${NODES}; do
710 node=$(basename $node .yml)
711
712 # filter first in cluster.. ctl-01, mon-01, etc..
713 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
714 verify_salt_minion ${node} || continue
715 else
716 echo Skipped $node.
717 fi
718 passed=$(($passed+1))
719 done
720 # fail on failures
721 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
722 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
723 test ! $passed -lt $total || {
724 tail -n50 /tmp/*.pillar_verify
725 return 1
726 }
727}
728
729
730
731##########################################
732# To install salt master/minon
733
734function install() {
735 setup $@
736}
737
738function setup() {
739 # CLI
740 while [ x"$1" != x"" ]; do
741 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
742
743 case $1 in
744 master )
745 install_salt_master_$SALT_SOURCE
746 install_salt_minion_$SALT_SOURCE
747 install_salt_formula_$FORMULAS_SOURCE
748 ;;
749 minion )
750 install_salt_minion_$SALT_SOURCE
751 ;;
752 esac
753 shift
754 done
755 echo DONE
756}
757
758function bootstrap() {
759 log_info "Bootstrap & verification of SaltMaster and configured minions."
760 trap _atexit INT TERM EXIT
761
762 system_config_master
763 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200764 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200765 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200766}
767
768function default() {
769 bootstrap $@
770}
771
772
773##########################################
774[[ "$0" != "$BASH_SOURCE" ]] || {
775# unless file is being sourced
776 default $@
777}