blob: 44c4bce2dc5bab983662e49ead8dc70369854c3f [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 Michalecdd1a0472017-09-06 19:27:24 +0200253 # to force alternative reclass module path
Petr Michalecd9d0a322017-09-06 19:38:32 +0200254 if [ -n "$RECLASS_SOURCE_PATH" ]; then
255 RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}"
256 else
257 RECLASS_SOURCE_PATH=""
258 fi
Petr Michalecdd1a0472017-09-06 19:27:24 +0200259
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200260 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
261 cat <<-EOF > /etc/salt/master.d/master.conf
262 file_roots:
263 base:
264 - /usr/share/salt-formulas/env
265 prd:
266 - /srv/salt/env/prd
267 dev:
268 - /srv/salt/env/dev
269 pillar_opts: False
270 open_mode: True
271 reclass: &reclass
272 storage_type: yaml_fs
273 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200274 ${IGNORE_CLASS_NOTFOUND}
Petr Michalecdd1a0472017-09-06 19:27:24 +0200275 ${RECLASS_SOURCE_PATH}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200276 ext_pillar:
277 - reclass: *reclass
278 master_tops:
279 reclass: *reclass
280EOF
281
282 echo "Configuring reclass ..."
283
284 [ ! -d /etc/reclass ] && mkdir /etc/reclass
285 cat <<-EOF > /etc/reclass/reclass-config.yml
286 storage_type: yaml_fs
287 pretty_print: True
288 output: yaml
289 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200290 ${IGNORE_CLASS_NOTFOUND}
Petr Michalecdd1a0472017-09-06 19:27:24 +0200291 ${RECLASS_SOURCE_PATH}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200292EOF
293
294 clone_reclass
295 # override some envs from cluster level *.env, use with care
296 source_local_envs
297
298 cd ${RECLASS_ROOT}
299 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
300 # Possibly subrepo checkout needed
301 git submodule update --init --recursive
302 fi
303
Petr Michalec212cc6a2017-08-17 21:39:40 +0200304 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec51ece842017-09-06 20:44:15 +0200305 rm -rvf ${RECLASS_ROOT}/nodes/_generated/*
306
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200307 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200308 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200309 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
Petr Michalec24b30e82017-08-21 10:59:18 +0200310 log_warn "Salt Master node specification has not been found in model."
311 log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200312 cat <<-EOF > ${CONFIG}
313 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200314 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200315 parameters:
316 _param:
Petr Michaleca7de6df2017-08-31 12:16:57 +0200317 single_address: ${MASTER_IP:-$MASTER_HOSTNAME}
318 salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200319 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200320 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
321 reclass_data_revision: ${RECLASS_BRANCH:-master}
322 reclass_data_repository: "$RECLASS_ADDRESS"
Petr Michaleca7de6df2017-08-31 12:16:57 +0200323 reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200324 linux_system_codename: ${DISTRIB_CODENAME}
325 cluster_name: ${CLUSTER_NAME}
326 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200327 linux:
328 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200329 name: ${HOSTNAME:-cfg01}
330 domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200331 # ########
332EOF
333
334 if [ "$SALT_VERSION" == "latest" ]; then
335 VERSION=""
336 else
337 VERSION="version: $SALT_VERSION"
338 fi
339
340 cat <<-EOF >> ${CONFIG}
341 salt:
342 master:
343 accept_policy: open_mode
344 source:
345 engine: $SALT_SOURCE
346 $VERSION
347 minion:
348 source:
349 engine: $SALT_SOURCE
350 $VERSION
351 # ########
352 # vim: ft=yaml sw=2 ts=2 sts=2
353EOF
354 fi
Petr Michalec24b30e82017-08-21 10:59:18 +0200355
356 log_debug "Salt Master node config yaml:"
357 log_debug "$(cat ${CONFIG})"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200358}
359
360configure_salt_minion()
361{
362 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
363 cat <<-EOF > /etc/salt/minion.d/minion.conf
Petr Michaleca7de6df2017-08-31 12:16:57 +0200364 master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200365 id: $MINION_ID
366 EOF
367}
368
369
370install_salt_master_pkg()
371{
372 echo -e "\nPreparing base OS repository ...\n"
373
374 configure_pkg_repo
375
376 echo -e "\nInstalling salt master ...\n"
377
378 case $PLATFORM_FAMILY in
379 debian)
380 $SUDO apt-get install -y git
381 which reclass || $SUDO apt install -qqq -y reclass
382 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
383 ;;
384 rhel)
385 yum install -y git
386 which reclass || $SUDO yum install -y reclass
387 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
388 ;;
389 esac
390
391 which reclass-salt || {
392 test -e /usr/share/reclass/reclass-salt && {
393 ln -fs /usr/share/reclass/reclass-salt /usr/bin
394 }
395 }
396
397 configure_salt_master
398
399 echo -e "\nRestarting services ...\n"
400 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
401 $SVCTOOL salt-master restart
402}
403
404install_salt_master_pip()
405{
406 echo -e "\nPreparing base OS repository ...\n"
407
408 case $PLATFORM_FAMILY in
409 debian)
410 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
411 which reclass || $SUDO apt-get install -y reclass
412 ;;
413 rhel)
414 $SUDO yum install -y git
415 which reclass || $SUDO yum install -y reclass
416 ;;
417 esac
418
419 echo -e "\nInstalling salt master ...\n"
420 # TODO: replace with saltstack bootstrap script
421
422 if [ "$SALT_VERSION" == "latest" ]; then
423 pip install salt
424 else
425 pip install salt==$SALT_VERSION
426 fi
427
428 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
429 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
430
431 which reclass-salt || {
432 test -e /usr/share/reclass/reclass-salt && {
433 ln -fs /usr/share/reclass/reclass-salt /usr/bin
434 }
435 }
436
437 configure_salt_master
438
439 echo -e "\nRestarting services ...\n"
440 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
441 $SVCTOOL salt-master restart
442}
443
444
445
446install_salt_minion_pkg()
447{
448
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200449 echo -e "\nInstalling salt minion ...\n"
450
451 case $PLATFORM_FAMILY in
452 debian)
453 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
454 ;;
455 rhel)
456 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
457 ;;
458 esac
459
460
461 configure_salt_minion
462
463 $SVCTOOL salt-minion restart
464}
465
466install_salt_minion_pip()
467{
468 echo -e "\nInstalling salt minion ...\n"
469
470 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
471 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
472
473 configure_salt_minion
474 $SVCTOOL salt-minion restart
475}
476
477
478install_salt_formula_pkg()
479{
480 configure_pkg_repo
481
482 case $PLATFORM_FAMILY in
483 debian)
484 echo "Configuring necessary formulas ..."
485
486 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
487 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
488 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
489 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
490 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
491 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
492 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
493 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
494 exit 1
495 fi
496 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
497 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
498 done
499 ;;
500 rhel)
501 # TODO
502 ;;
503 esac
504
505 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
506 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
507}
508
509install_salt_formula_git()
510{
511 echo "Configuring necessary formulas ..."
512
513 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
514 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
515 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
516 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
517 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
518 _BRANCH=${FORMULAS_BRANCH}
519 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
520 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
521 # Fallback to the master branch if the branch doesn't exist for this repository
522 _BRANCH=master
523 fi
524 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
525 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
526 exit 1
527 fi
528 } || {
529 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
530 git fetch origin/${_BRANCH} || git fetch --all
531 git checkout ${_BRANCH} && git pull || git pull;
532 cd -
533 }
534 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
535 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
536 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
537 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
538 done
539
540 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
541 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
542}
543
544
545saltmaster_bootstrap() {
546
547 log_info "Salt master setup"
548 test -n "$MASTER_HOSTNAME" || exit 1
549
550 clone_reclass
551 # override some envs from cluster level *.env, use with care
552 source_local_envs
553
554 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
555 pkill -9 salt-minion
556 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
Petr Michaleca7de6df2017-08-31 12:16:57 +0200557 export MASTER_IP=localhost
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200558 export MINION_ID=${MASTER_HOSTNAME}
559 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
560 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
561 fi
562 # call local "setup() master"
563 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
564 if ! setup master; then
565 #cat /tmp/salt-master-setup.log
566 log_err "salt master setup() failed."
567 exit 1
568 else
569 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
570 fi
571 }
572
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200573 log_info "Re/starting salt services"
574 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
575 pkill -9 salt-minion
576 sleep 1
577 $SUDO service salt-master restart
578 $SUDO service salt-minion restart
579 sleep 10
580}
581
582# Init salt master
583saltmaster_init() {
584
585 log_info "Runing saltmaster states"
586 test -n "$MASTER_HOSTNAME" || exit 1
587
588 set -e
589 $SUDO salt-call saltutil.sync_all >/dev/null
590
591 # TODO: Placeholder update saltmaster spec (nodes/FQDN.yml) to be able to bootstrap with minimal configuration
592 # (ie: with linux, git, salt formulas)
593
594 #log_info "Verify SaltMaster, before salt-master is fully initialized"
595 #if ! $SUDO reclass-salt -p ${MASTER_HOSTNAME} &> /tmp/${MASTER_HOSTNAME}.pillar;then
596 # log_warn "Node verification before initialization failed."; cat /tmp/${MASTER_HOSTNAME}.pillar;
597 #fi
598
Petr Michalecda014202017-08-18 11:26:07 +0200599
600 # workarond isolated and not fully bootstraped environments
Petr Michalec571d6b82017-08-18 13:25:24 +0200601 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 +0200602
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200603 log_info "State: salt.master.env"
Petr Michalecda014202017-08-18 11:26:07 +0200604 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env pillar="$PILLAR"; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200605 log_err "State salt.master.env failed, keep your eyes wide open."
606 fi
607
608 log_info "State: salt.master.pillar"
Petr Michalecda014202017-08-18 11:26:07 +0200609 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar="$PILLAR"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200610 # Note: sikp reclass data dir states
611 # in order to avoid pull from configured repo/branch
612
613 # Revert temporary SaltMaster minimal configuration, if any
614 pushd $RECLASS_ROOT
615 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
616 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200617 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200618 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
619 git checkout -- $RECLASS_ROOT/nodes || true
620 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
621 log_info "State: salt.master.env"
Petr Michalecda014202017-08-18 11:26:07 +0200622 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env pillar="$PILLAR"; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200623 log_err "State salt.master.env failed, keep your eyes wide open."
624 fi
625 log_info "State: salt.master.pillar"
Petr Michalecda014202017-08-18 11:26:07 +0200626 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar="$PILLAR"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200627 fi
628 popd
629
Petr Michalecda014202017-08-18 11:26:07 +0200630 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
631 log_info "State: salt.master.service"
Petr Michalec7f3e7e72017-08-18 13:49:24 +0200632 $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalecda014202017-08-18 11:26:07 +0200633
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200634 log_info "State: salt.master.storage.node"
635 set +e
Petr Michaleca5141342017-08-16 12:55:20 +0200636 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200637 $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
638 ret=$?
639 set -e
640
641 if [[ $ret -eq 2 ]]; then
642 log_err "State reclass.storage.node failed with exit code 2 but continuing."
643 elif [[ $ret -ne 0 ]]; then
644 log_err "State reclass.storage.node failed with exit code $ret"
645 exit 1
646 fi
647
648 log_info "Re/starting salt services"
649 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
650 $SUDO service salt-minion restart >/dev/null
651 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
652
653 verify_salt_master
654 set +e
655
656}
657
658
659function verify_salt_master() {
660 set -e
661
662 log_info "Verify Salt master"
663 test -n "$MASTER_HOSTNAME" || exit 1
664
665 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200666 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
667 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200668 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
669 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
670 $SUDO salt-call --no-color grains.items
671 $SUDO salt-call --no-color pillar.data
672 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200673 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200674 if ! $SUDO reclass --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
675 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
676 exit 1
677 fi
678}
679
680function verify_salt_minion() {
681 set -e
682 node=$1
683 log_info "Verifying ${node}"
684 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
685 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
686 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
687 fi
688 if ! $SUDO reclass --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
689 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
690 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
691 exit 1
692 fi
693 fi
694}
695
696function verify_salt_minions() {
697 #set -e
698 NODES=$(find $RECLASS_ROOT/nodes/ -name "*.yml" | grep -v "cfg")
699 log_info "Verifying minions: $(echo ${NODES}|xargs)"
700
701 # Parallel
702 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
703 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
704
705 function filterFails() {
706 grep -v '/grains' | tee -a $1 | tail -n20
707 }
708
709 log_info "Verify nodes"
710 passed=0
711 for node in ${NODES}; do
712 node=$(basename $node .yml)
713
714 # filter first in cluster.. ctl-01, mon-01, etc..
715 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
716 verify_salt_minion ${node} || continue
717 else
718 echo Skipped $node.
719 fi
720 passed=$(($passed+1))
721 done
722 # fail on failures
723 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
724 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
725 test ! $passed -lt $total || {
726 tail -n50 /tmp/*.pillar_verify
727 return 1
728 }
729}
730
731
732
733##########################################
734# To install salt master/minon
735
736function install() {
737 setup $@
738}
739
740function setup() {
741 # CLI
742 while [ x"$1" != x"" ]; do
743 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
744
745 case $1 in
746 master )
747 install_salt_master_$SALT_SOURCE
748 install_salt_minion_$SALT_SOURCE
749 install_salt_formula_$FORMULAS_SOURCE
750 ;;
751 minion )
752 install_salt_minion_$SALT_SOURCE
753 ;;
754 esac
755 shift
756 done
757 echo DONE
758}
759
760function bootstrap() {
761 log_info "Bootstrap & verification of SaltMaster and configured minions."
762 trap _atexit INT TERM EXIT
763
764 system_config_master
765 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200766 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200767 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200768}
769
770function default() {
771 bootstrap $@
772}
773
774
775##########################################
776[[ "$0" != "$BASH_SOURCE" ]] || {
777# unless file is being sourced
778 default $@
779}