blob: cd76cd74eb6935abd9c3d1a164d3de6991b4ca47 [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}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020046
47
48# salt apt repository
49test -e /etc/lsb-release && eval $(cat /etc/lsb-release)
50which lsb_release && DISTRIB_CODENAME=${DISTRIB_CODENAME:-$(lsb_release -cs)}
51#
52export APT_REPOSITORY="deb [arch=amd64] http://apt-mk.mirantis.com/${DISTRIB_CODENAME} ${DISTRIB_REVISION:-stable} salt"
53export APT_REPOSITORY_GPG=${APT_REPOSITORY_GPG:-http://apt-mk.mirantis.com/public.gpg}
54
55# reclass
56export RECLASS_ADDRESS=${RECLASS_ADDRESS:-https://github.com/salt-formulas/openstack-salt.git} # https/git
57
58# formula
59export FORMULAS_BASE=${FORMULAS_BASE:-https://github.com/salt-formulas}
60export FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas}
61export FORMULAS_BRANCH=${FORMULAS_BRANCH:-master}
62export FORMULAS_SOURCE=${FORMULAS_SOURCE:-pkg} # pkg/git
63# essential set of formulas (known to by used on cfg01 node for most setups)
chnyda86d63b42017-08-31 10:05:00 +020064FORMULAS_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 +020065# minimal set of formulas for salt-master bootstrap
66declare -a FORMULAS_SALT_MASTER=(linux reclass salt git $(echo $FORMULAS_SALT_MASTER))
67export FORMULAS_SALT_MASTER
68
69# system / host
Petr Michalecc749e862017-09-06 21:10:10 +020070export HOSTNAME=${HOSTNAME:-`hostname -s`}
71export HOSTNAME=${HOSTNAME//.*/}
72export DOMAIN=${DOMAIN:-`hostname -d`}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020073export 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
Petr Michalec33c04892017-09-08 15:10:30 +020080BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True}
Petr Michalece11cf612017-09-08 18:21:48 +020081BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION:- stable 2016.3 }
Petr Michalec33c04892017-09-08 15:10:30 +020082BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX $BOOTSTRAP_SALTSTACK_VERSION }
Petr Michalece11cf612017-09-08 18:21:48 +020083SALT_SOURCE=${SALT_SOURCE:-pkg}
84# the version below is used salt pillar data
85SALT_VERSION=${SALT_VERSION:-latest}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020086
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
Petr Michalec05b52452017-09-08 14:26:59 +020093eval "$(grep -h '=' /etc/*release 2> /dev/null)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020094PLATFORM_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() {
Petr Michaleca361d4e2017-09-08 11:38:16 +0200184 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200185 # 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;
Petr Michaleca361d4e2017-09-08 11:38:16 +0200196 if [ ! -d ${RECLASS_ROOT}/classes -o ! -d ${RECLASS_ROOT}/nodes ]; then
197 log_err "Reclass ${RECLASS_ROOT} is not fetched locally;"
198 ls -Rla ${RECLASS_ROOT}
199 exit 1
200 fi;
201 $SUDO mkdir -p $RECLASS_ROOT/classes/service
202 $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200203}
204
205
206##########################################
207# Main calls
208
Petr Michalecda014202017-08-18 11:26:07 +0200209system_config_ssh_conf() {
210 for conf in ~/.ssh/config /root/.ssh/config; do
211 $SUDO mkdir -p $(dirname $conf)
212 if ! grep StrictHostKeyChecking $conf; then
213 # this should be used only in CI environment
214 echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee $conf >/dev/null
215 fi
216 done
217 if ! grep github.com ~/.ssh/known_hosts; then
218 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
219 fi
220}
221
222system_config_salt_modules_prereq() {
223 # salt-formulas custom modules dependencies, etc:
224 $SUDO $PKGTOOL install -y iproute2 curl sudo apt-transport-https python-psutil python-apt python-m2crypto python-oauth python-pip &>/dev/null
225}
226
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200227system_config_minion() {
228 log_info "System configuration salt minion"
229}
230
231system_config_master() {
232 log_info "System configuration salt master"
233
Petr Michalecda014202017-08-18 11:26:07 +0200234 system_config_salt_modules_prereq
235 system_config_ssh_conf
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200236
Petr Michalecda014202017-08-18 11:26:07 +0200237 if ! grep '127.0.1.2.*salt' /etc/hosts; then
238 echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null
239 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200240
241 which reclass || $SUDO $PKGTOOL install -y reclass
242
243 which reclass-salt || {
244 test -e /usr/share/reclass/reclass-salt && {
245 ln -fs /usr/share/reclass/reclass-salt /usr/bin
246 }
247 }
248}
249
250configure_salt_master()
251{
252
253 echo "Configuring salt-master ..."
254
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200255 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
256 IGNORE_CLASS_NOTFOUND="ignore_class_notfound: True"
257 fi
258
Petr Michalecdd1a0472017-09-06 19:27:24 +0200259 # to force alternative reclass module path
Petr Michalecd9d0a322017-09-06 19:38:32 +0200260 if [ -n "$RECLASS_SOURCE_PATH" ]; then
261 RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}"
262 else
263 RECLASS_SOURCE_PATH=""
264 fi
Petr Michalecdd1a0472017-09-06 19:27:24 +0200265
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200266 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
267 cat <<-EOF > /etc/salt/master.d/master.conf
268 file_roots:
269 base:
270 - /usr/share/salt-formulas/env
271 prd:
272 - /srv/salt/env/prd
273 dev:
274 - /srv/salt/env/dev
275 pillar_opts: False
276 open_mode: True
277 reclass: &reclass
278 storage_type: yaml_fs
279 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200280 ${IGNORE_CLASS_NOTFOUND}
Petr Michalecdd1a0472017-09-06 19:27:24 +0200281 ${RECLASS_SOURCE_PATH}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200282 ext_pillar:
283 - reclass: *reclass
284 master_tops:
285 reclass: *reclass
286EOF
287
288 echo "Configuring reclass ..."
289
290 [ ! -d /etc/reclass ] && mkdir /etc/reclass
291 cat <<-EOF > /etc/reclass/reclass-config.yml
292 storage_type: yaml_fs
293 pretty_print: True
294 output: yaml
295 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200296 ${IGNORE_CLASS_NOTFOUND}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200297EOF
298
299 clone_reclass
300 # override some envs from cluster level *.env, use with care
301 source_local_envs
302
303 cd ${RECLASS_ROOT}
304 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
305 # Possibly subrepo checkout needed
306 git submodule update --init --recursive
307 fi
308
Petr Michalec212cc6a2017-08-17 21:39:40 +0200309 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec51ece842017-09-06 20:44:15 +0200310 rm -rvf ${RECLASS_ROOT}/nodes/_generated/*
311
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200312 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200313 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200314 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
Petr Michalec24b30e82017-08-21 10:59:18 +0200315 log_warn "Salt Master node specification has not been found in model."
316 log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200317 cat <<-EOF > ${CONFIG}
318 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200319 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200320 parameters:
321 _param:
Petr Michaleca7de6df2017-08-31 12:16:57 +0200322 single_address: ${MASTER_IP:-$MASTER_HOSTNAME}
323 salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200324 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200325 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
326 reclass_data_revision: ${RECLASS_BRANCH:-master}
327 reclass_data_repository: "$RECLASS_ADDRESS"
Petr Michaleca7de6df2017-08-31 12:16:57 +0200328 reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200329 linux_system_codename: ${DISTRIB_CODENAME}
330 cluster_name: ${CLUSTER_NAME}
331 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200332 linux:
333 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200334 name: ${HOSTNAME:-cfg01}
335 domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200336 # ########
337EOF
338
339 if [ "$SALT_VERSION" == "latest" ]; then
340 VERSION=""
341 else
342 VERSION="version: $SALT_VERSION"
343 fi
344
345 cat <<-EOF >> ${CONFIG}
346 salt:
347 master:
348 accept_policy: open_mode
349 source:
350 engine: $SALT_SOURCE
351 $VERSION
352 minion:
353 source:
354 engine: $SALT_SOURCE
355 $VERSION
356 # ########
357 # vim: ft=yaml sw=2 ts=2 sts=2
358EOF
359 fi
Petr Michalec24b30e82017-08-21 10:59:18 +0200360
361 log_debug "Salt Master node config yaml:"
362 log_debug "$(cat ${CONFIG})"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200363}
364
365configure_salt_minion()
366{
367 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
368 cat <<-EOF > /etc/salt/minion.d/minion.conf
Petr Michaleca7de6df2017-08-31 12:16:57 +0200369 master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200370 id: $MINION_ID
371 EOF
372}
373
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200374install_reclass()
375{
376 VERSION=${1:-$RECLASS_VERSION}
377 VERSION=${VERSION:-master}
378 # tries to replace all local version system version
379 for s in $(python -c "import site; print(' '.join(site.getsitepackages()))"); do
Petr Michaleca2f351c2017-09-07 17:37:44 +0200380 sudo -H pip install --upgrade --force-reinstall -I \
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200381 -t "$s" git+https://github.com/salt-formulas/reclass.git@${VERSION};
382 done
383}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200384
385install_salt_master_pkg()
386{
387 echo -e "\nPreparing base OS repository ...\n"
388
389 configure_pkg_repo
390
391 echo -e "\nInstalling salt master ...\n"
392
393 case $PLATFORM_FAMILY in
394 debian)
395 $SUDO apt-get install -y git
396 which reclass || $SUDO apt install -qqq -y reclass
397 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
398 ;;
399 rhel)
400 yum install -y git
401 which reclass || $SUDO yum install -y reclass
402 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
403 ;;
404 esac
405
406 which reclass-salt || {
407 test -e /usr/share/reclass/reclass-salt && {
408 ln -fs /usr/share/reclass/reclass-salt /usr/bin
409 }
410 }
411
412 configure_salt_master
413
414 echo -e "\nRestarting services ...\n"
415 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
416 $SVCTOOL salt-master restart
417}
418
419install_salt_master_pip()
420{
421 echo -e "\nPreparing base OS repository ...\n"
422
423 case $PLATFORM_FAMILY in
424 debian)
425 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
426 which reclass || $SUDO apt-get install -y reclass
427 ;;
428 rhel)
429 $SUDO yum install -y git
430 which reclass || $SUDO yum install -y reclass
431 ;;
432 esac
433
434 echo -e "\nInstalling salt master ...\n"
435 # TODO: replace with saltstack bootstrap script
436
437 if [ "$SALT_VERSION" == "latest" ]; then
438 pip install salt
439 else
440 pip install salt==$SALT_VERSION
441 fi
442
443 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
444 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
445
446 which reclass-salt || {
447 test -e /usr/share/reclass/reclass-salt && {
448 ln -fs /usr/share/reclass/reclass-salt /usr/bin
449 }
450 }
451
452 configure_salt_master
453
454 echo -e "\nRestarting services ...\n"
455 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
456 $SVCTOOL salt-master restart
457}
458
459
460
461install_salt_minion_pkg()
462{
463
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200464 echo -e "\nInstalling salt minion ...\n"
465
466 case $PLATFORM_FAMILY in
467 debian)
468 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
469 ;;
470 rhel)
471 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
472 ;;
473 esac
474
475
476 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200477 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200478}
479
480install_salt_minion_pip()
481{
482 echo -e "\nInstalling salt minion ...\n"
483
484 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
485 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
486
487 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200488 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200489}
490
491
492install_salt_formula_pkg()
493{
494 configure_pkg_repo
495
496 case $PLATFORM_FAMILY in
497 debian)
498 echo "Configuring necessary formulas ..."
499
500 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
501 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
502 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
503 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
504 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
505 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
506 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
507 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
508 exit 1
509 fi
510 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
511 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
512 done
513 ;;
514 rhel)
515 # TODO
516 ;;
517 esac
518
519 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
520 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
521}
522
523install_salt_formula_git()
524{
525 echo "Configuring necessary formulas ..."
526
527 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
528 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
529 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
530 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
531 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
532 _BRANCH=${FORMULAS_BRANCH}
533 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
534 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
535 # Fallback to the master branch if the branch doesn't exist for this repository
536 _BRANCH=master
537 fi
538 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
539 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
540 exit 1
541 fi
542 } || {
543 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
544 git fetch origin/${_BRANCH} || git fetch --all
545 git checkout ${_BRANCH} && git pull || git pull;
546 cd -
547 }
548 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
549 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
550 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
551 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
552 done
553
554 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
555 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
556}
557
558
559saltmaster_bootstrap() {
560
561 log_info "Salt master setup"
562 test -n "$MASTER_HOSTNAME" || exit 1
563
564 clone_reclass
565 # override some envs from cluster level *.env, use with care
566 source_local_envs
567
568 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
569 pkill -9 salt-minion
570 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
Petr Michaleca7de6df2017-08-31 12:16:57 +0200571 export MASTER_IP=localhost
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200572 export MINION_ID=${MASTER_HOSTNAME}
573 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
574 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
575 fi
576 # call local "setup() master"
577 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
578 if ! setup master; then
579 #cat /tmp/salt-master-setup.log
580 log_err "salt master setup() failed."
581 exit 1
582 else
583 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
584 fi
585 }
586
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200587 if [[ $RECLASS_VERSION =~ ^(dev|devel|master)$ ]]; then
Petr Michalec2f9a35b2017-09-07 17:08:06 +0200588 log_warn "Install development version of reclass"
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200589 install_reclass ${RECLASS_VERSION/dev*/master}
590 fi
591
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200592 log_info "Re/starting salt services"
593 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
594 pkill -9 salt-minion
595 sleep 1
596 $SUDO service salt-master restart
597 $SUDO service salt-minion restart
598 sleep 10
599}
600
601# Init salt master
602saltmaster_init() {
603
604 log_info "Runing saltmaster states"
605 test -n "$MASTER_HOSTNAME" || exit 1
606
607 set -e
608 $SUDO salt-call saltutil.sync_all >/dev/null
609
610 # TODO: Placeholder update saltmaster spec (nodes/FQDN.yml) to be able to bootstrap with minimal configuration
611 # (ie: with linux, git, salt formulas)
612
613 #log_info "Verify SaltMaster, before salt-master is fully initialized"
614 #if ! $SUDO reclass-salt -p ${MASTER_HOSTNAME} &> /tmp/${MASTER_HOSTNAME}.pillar;then
615 # log_warn "Node verification before initialization failed."; cat /tmp/${MASTER_HOSTNAME}.pillar;
616 #fi
617
Petr Michalecda014202017-08-18 11:26:07 +0200618
619 # workarond isolated and not fully bootstraped environments
Petr Michalec571d6b82017-08-18 13:25:24 +0200620 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 +0200621
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200622 log_info "State: salt.master.env"
Petr Michalecda014202017-08-18 11:26:07 +0200623 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env pillar="$PILLAR"; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200624 log_err "State salt.master.env failed, keep your eyes wide open."
625 fi
626
627 log_info "State: salt.master.pillar"
Petr Michalecda014202017-08-18 11:26:07 +0200628 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar="$PILLAR"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200629 # Note: sikp reclass data dir states
630 # in order to avoid pull from configured repo/branch
631
632 # Revert temporary SaltMaster minimal configuration, if any
633 pushd $RECLASS_ROOT
634 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
635 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200636 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200637 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
638 git checkout -- $RECLASS_ROOT/nodes || true
639 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
640 log_info "State: salt.master.env"
Petr Michalecda014202017-08-18 11:26:07 +0200641 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env pillar="$PILLAR"; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200642 log_err "State salt.master.env failed, keep your eyes wide open."
643 fi
644 log_info "State: salt.master.pillar"
Petr Michalecda014202017-08-18 11:26:07 +0200645 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar="$PILLAR"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200646 fi
647 popd
648
Petr Michalecda014202017-08-18 11:26:07 +0200649 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
650 log_info "State: salt.master.service"
Petr Michalec7f3e7e72017-08-18 13:49:24 +0200651 $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalecda014202017-08-18 11:26:07 +0200652
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200653 log_info "State: salt.master.storage.node"
654 set +e
Petr Michaleca5141342017-08-16 12:55:20 +0200655 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200656 $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
657 ret=$?
658 set -e
659
660 if [[ $ret -eq 2 ]]; then
661 log_err "State reclass.storage.node failed with exit code 2 but continuing."
662 elif [[ $ret -ne 0 ]]; then
663 log_err "State reclass.storage.node failed with exit code $ret"
664 exit 1
665 fi
666
667 log_info "Re/starting salt services"
668 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
Petr Michalecc0bfa972017-09-08 15:21:01 +0200669 $SUDO service salt-minion stop
670 $SUDO service salt-master stop
671 sleep 3
672 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {}
673 $SUDO service salt-master start
674 $SUDO service salt-minion start
675 sleep 10
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200676 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
677
678 verify_salt_master
679 set +e
680
681}
682
683
684function verify_salt_master() {
685 set -e
686
687 log_info "Verify Salt master"
688 test -n "$MASTER_HOSTNAME" || exit 1
689
690 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200691 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
692 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200693 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
694 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
695 $SUDO salt-call --no-color grains.items
696 $SUDO salt-call --no-color pillar.data
697 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200698 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200699 if ! $SUDO reclass --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
700 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
701 exit 1
702 fi
703}
704
705function verify_salt_minion() {
706 set -e
707 node=$1
708 log_info "Verifying ${node}"
709 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
710 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
711 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
712 fi
713 if ! $SUDO reclass --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
714 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
715 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
716 exit 1
717 fi
718 fi
719}
720
721function verify_salt_minions() {
722 #set -e
723 NODES=$(find $RECLASS_ROOT/nodes/ -name "*.yml" | grep -v "cfg")
724 log_info "Verifying minions: $(echo ${NODES}|xargs)"
725
726 # Parallel
727 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
728 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
729
730 function filterFails() {
731 grep -v '/grains' | tee -a $1 | tail -n20
732 }
733
734 log_info "Verify nodes"
735 passed=0
736 for node in ${NODES}; do
737 node=$(basename $node .yml)
738
739 # filter first in cluster.. ctl-01, mon-01, etc..
740 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
741 verify_salt_minion ${node} || continue
742 else
743 echo Skipped $node.
744 fi
745 passed=$(($passed+1))
746 done
747 # fail on failures
748 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
749 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
750 test ! $passed -lt $total || {
751 tail -n50 /tmp/*.pillar_verify
752 return 1
753 }
754}
755
756
757
758##########################################
759# To install salt master/minon
760
761function install() {
762 setup $@
763}
764
765function setup() {
766 # CLI
767 while [ x"$1" != x"" ]; do
768 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
769
770 case $1 in
771 master )
772 install_salt_master_$SALT_SOURCE
773 install_salt_minion_$SALT_SOURCE
774 install_salt_formula_$FORMULAS_SOURCE
775 ;;
776 minion )
777 install_salt_minion_$SALT_SOURCE
778 ;;
779 esac
780 shift
781 done
782 echo DONE
783}
784
785function bootstrap() {
786 log_info "Bootstrap & verification of SaltMaster and configured minions."
787 trap _atexit INT TERM EXIT
788
789 system_config_master
790 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200791 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200792 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200793}
794
795function default() {
796 bootstrap $@
797}
798
799
800##########################################
801[[ "$0" != "$BASH_SOURCE" ]] || {
802# unless file is being sourced
803 default $@
804}