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