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