blob: af925c599f0732242f055377acdcb385c4d6e77f [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#
Dmitry Ukov95492a02017-10-23 11:34:45 +040052export APT_REPOSITORY=${APT_REPOSITORY:- deb [arch=amd64] http://apt.mirantis.com/${DISTRIB_CODENAME} ${DISTRIB_REVISION:-stable} salt}
chnyda30c07a62017-09-20 14:28:02 +020053export APT_REPOSITORY_GPG=${APT_REPOSITORY_GPG:-http://apt.mirantis.com/public.gpg}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020054
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)
Petr Michalece07cd0b2017-11-15 11:41:57 +010064FORMULAS_SALT_MASTER=${FORMULAS_SALT_MASTER:- $EXTRA_FORMULAS}
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
Mykyta Karpinfbcb5482017-10-04 14:15:25 +0300144 echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/mcp_salt.list >/dev/null
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200145 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
Petr Michalec262e3ad2017-10-06 13:41:58 +0200177 ret=1
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200178 for i in $(seq 1 $tries); do
Petr Michalec262e3ad2017-10-06 13:41:58 +0200179 "$@" && return $? || ret=$?
180 sleep $i
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200181 done
Petr Michalec9fec8b32017-10-05 14:46:42 +0200182 return $ret
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200183}
184
185function clone_reclass() {
Petr Michaleca361d4e2017-09-08 11:38:16 +0200186 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200187 # No reclass at all, clone from given address
188 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
189 if echo ${RECLASS_BRANCH:-master} | egrep -q "^refs"; then
190 git clone ${RECLASS_ADDRESS} ${RECLASS_ROOT}
191 cd ${RECLASS_ROOT}
192 git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH:-master} && git checkout FETCH_HEAD
193 cd -
194 else
195 git clone -b ${RECLASS_BRANCH:-master} ${RECLASS_ADDRESS} ${RECLASS_ROOT};
196 fi;
197 fi;
Petr Michalec679f15b2017-09-18 16:16:29 +0200198 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michaleca361d4e2017-09-08 11:38:16 +0200199 log_err "Reclass ${RECLASS_ROOT} is not fetched locally;"
200 ls -Rla ${RECLASS_ROOT}
201 exit 1
202 fi;
203 $SUDO mkdir -p $RECLASS_ROOT/classes/service
204 $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200205}
206
207
208##########################################
209# Main calls
210
Petr Michalecda014202017-08-18 11:26:07 +0200211system_config_ssh_conf() {
212 for conf in ~/.ssh/config /root/.ssh/config; do
213 $SUDO mkdir -p $(dirname $conf)
214 if ! grep StrictHostKeyChecking $conf; then
215 # this should be used only in CI environment
216 echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee $conf >/dev/null
217 fi
218 done
219 if ! grep github.com ~/.ssh/known_hosts; then
220 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
221 fi
222}
223
224system_config_salt_modules_prereq() {
225 # salt-formulas custom modules dependencies, etc:
226 $SUDO $PKGTOOL install -y iproute2 curl sudo apt-transport-https python-psutil python-apt python-m2crypto python-oauth python-pip &>/dev/null
227}
228
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200229system_config_minion() {
230 log_info "System configuration salt minion"
231}
232
233system_config_master() {
234 log_info "System configuration salt master"
235
Petr Michalecda014202017-08-18 11:26:07 +0200236 system_config_salt_modules_prereq
237 system_config_ssh_conf
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200238
Petr Michalecda014202017-08-18 11:26:07 +0200239 if ! grep '127.0.1.2.*salt' /etc/hosts; then
240 echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null
241 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200242
243 which reclass || $SUDO $PKGTOOL install -y reclass
244
245 which reclass-salt || {
246 test -e /usr/share/reclass/reclass-salt && {
247 ln -fs /usr/share/reclass/reclass-salt /usr/bin
248 }
249 }
250}
251
252configure_salt_master()
253{
254
255 echo "Configuring salt-master ..."
256
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200257 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec29d3ea32017-10-31 17:22:36 +0100258 read -d '' IGNORE_CLASS_NOTFOUND <<-EOF
259 ignore_class_notfound: True
260 ignore_class_regexp:
261 - 'service.*'
262EOF
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200263 fi
264
Petr Michalecdd1a0472017-09-06 19:27:24 +0200265 # to force alternative reclass module path
Petr Michalecd9d0a322017-09-06 19:38:32 +0200266 if [ -n "$RECLASS_SOURCE_PATH" ]; then
267 RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}"
268 else
269 RECLASS_SOURCE_PATH=""
270 fi
Petr Michalecdd1a0472017-09-06 19:27:24 +0200271
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200272 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
273 cat <<-EOF > /etc/salt/master.d/master.conf
274 file_roots:
275 base:
276 - /usr/share/salt-formulas/env
277 prd:
278 - /srv/salt/env/prd
279 dev:
280 - /srv/salt/env/dev
281 pillar_opts: False
282 open_mode: True
283 reclass: &reclass
284 storage_type: yaml_fs
285 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200286 ${IGNORE_CLASS_NOTFOUND}
Petr Michalecdd1a0472017-09-06 19:27:24 +0200287 ${RECLASS_SOURCE_PATH}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200288 ext_pillar:
289 - reclass: *reclass
290 master_tops:
291 reclass: *reclass
292EOF
293
294 echo "Configuring reclass ..."
295
296 [ ! -d /etc/reclass ] && mkdir /etc/reclass
297 cat <<-EOF > /etc/reclass/reclass-config.yml
298 storage_type: yaml_fs
299 pretty_print: True
300 output: yaml
301 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200302EOF
303
304 clone_reclass
305 # override some envs from cluster level *.env, use with care
306 source_local_envs
307
308 cd ${RECLASS_ROOT}
309 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
310 # Possibly subrepo checkout needed
311 git submodule update --init --recursive
312 fi
313
Petr Michalec212cc6a2017-08-17 21:39:40 +0200314 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec51ece842017-09-06 20:44:15 +0200315 rm -rvf ${RECLASS_ROOT}/nodes/_generated/*
316
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200317 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200318 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200319 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
Petr Michalec24b30e82017-08-21 10:59:18 +0200320 log_warn "Salt Master node specification has not been found in model."
321 log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200322 cat <<-EOF > ${CONFIG}
323 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200324 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200325 parameters:
326 _param:
Petr Michaleca7de6df2017-08-31 12:16:57 +0200327 salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200328 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200329 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
330 reclass_data_revision: ${RECLASS_BRANCH:-master}
331 reclass_data_repository: "$RECLASS_ADDRESS"
Petr Michaleca7de6df2017-08-31 12:16:57 +0200332 reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200333 linux_system_codename: ${DISTRIB_CODENAME}
334 cluster_name: ${CLUSTER_NAME}
335 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200336 linux:
337 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200338 name: ${HOSTNAME:-cfg01}
339 domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200340 # ########
341EOF
342
343 if [ "$SALT_VERSION" == "latest" ]; then
344 VERSION=""
345 else
346 VERSION="version: $SALT_VERSION"
347 fi
348
349 cat <<-EOF >> ${CONFIG}
350 salt:
351 master:
352 accept_policy: open_mode
353 source:
354 engine: $SALT_SOURCE
355 $VERSION
356 minion:
357 source:
358 engine: $SALT_SOURCE
359 $VERSION
360 # ########
361 # vim: ft=yaml sw=2 ts=2 sts=2
362EOF
363 fi
Petr Michalec24b30e82017-08-21 10:59:18 +0200364
365 log_debug "Salt Master node config yaml:"
366 log_debug "$(cat ${CONFIG})"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200367}
368
369configure_salt_minion()
370{
371 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
372 cat <<-EOF > /etc/salt/minion.d/minion.conf
Petr Michaleca7de6df2017-08-31 12:16:57 +0200373 master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200374 id: $MINION_ID
375 EOF
376}
377
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200378install_reclass()
379{
380 VERSION=${1:-$RECLASS_VERSION}
Petr Michalec372fa032017-11-15 17:36:19 +0100381 VERSION=${VERSION:-pkg}
Petr Michalec800dd072017-11-15 17:19:26 +0100382 case ${VERSION} in
383 pkg|package)
384 which reclass || $SUDO $PKGTOOL install -y reclass
385 ;;
386 *)
Petr Michalec981f3cc2017-11-15 17:40:31 +0100387 log_warn "Install development version of reclass"
Petr Michalec1b37d3d2017-11-15 18:20:46 +0100388 # Note: dev/git/pip version...
389 # It replaces all local reclass versions on system
390 # Required for reclass/git features:
391 # $SUDO $PKGTOOL install -y libffi-dev libgit2-dev || true
Petr Michalec800dd072017-11-15 17:19:26 +0100392 for s in $(python -c "import site; print(' '.join(site.getsitepackages()))"); do
Petr Michalec23020832017-11-15 17:34:49 +0100393 sudo -H pip install --install-option="--prefix=" --upgrade --force-reinstall -I \
Petr Michalec800dd072017-11-15 17:19:26 +0100394 -t "$s" git+https://github.com/salt-formulas/reclass.git@${VERSION};
395 done
396 ;;
397 esac
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200398}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200399
400install_salt_master_pkg()
401{
402 echo -e "\nPreparing base OS repository ...\n"
403
404 configure_pkg_repo
405
406 echo -e "\nInstalling salt master ...\n"
407
408 case $PLATFORM_FAMILY in
409 debian)
410 $SUDO apt-get install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200411 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
412 ;;
413 rhel)
414 yum install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200415 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
416 ;;
417 esac
418
419 which reclass-salt || {
420 test -e /usr/share/reclass/reclass-salt && {
421 ln -fs /usr/share/reclass/reclass-salt /usr/bin
422 }
423 }
424
425 configure_salt_master
426
427 echo -e "\nRestarting services ...\n"
428 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
429 $SVCTOOL salt-master restart
430}
431
432install_salt_master_pip()
433{
434 echo -e "\nPreparing base OS repository ...\n"
435
436 case $PLATFORM_FAMILY in
437 debian)
438 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200439 ;;
440 rhel)
441 $SUDO yum install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200442 ;;
443 esac
444
445 echo -e "\nInstalling salt master ...\n"
446 # TODO: replace with saltstack bootstrap script
447
448 if [ "$SALT_VERSION" == "latest" ]; then
449 pip install salt
450 else
451 pip install salt==$SALT_VERSION
452 fi
453
454 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
455 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
456
457 which reclass-salt || {
458 test -e /usr/share/reclass/reclass-salt && {
459 ln -fs /usr/share/reclass/reclass-salt /usr/bin
460 }
461 }
462
463 configure_salt_master
464
465 echo -e "\nRestarting services ...\n"
466 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
467 $SVCTOOL salt-master restart
468}
469
470
471
472install_salt_minion_pkg()
473{
474
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200475 echo -e "\nInstalling salt minion ...\n"
476
477 case $PLATFORM_FAMILY in
478 debian)
479 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
480 ;;
481 rhel)
482 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
483 ;;
484 esac
485
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
491install_salt_minion_pip()
492{
493 echo -e "\nInstalling salt minion ...\n"
494
495 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
496 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
497
498 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200499 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200500}
501
502
503install_salt_formula_pkg()
504{
505 configure_pkg_repo
506
507 case $PLATFORM_FAMILY in
508 debian)
509 echo "Configuring necessary formulas ..."
510
511 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
512 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
513 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
514 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
515 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
516 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
517 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
518 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
519 exit 1
520 fi
521 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
522 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
523 done
524 ;;
525 rhel)
526 # TODO
527 ;;
528 esac
529
530 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
531 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
532}
533
534install_salt_formula_git()
535{
536 echo "Configuring necessary formulas ..."
537
538 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
539 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
540 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
541 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
542 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
543 _BRANCH=${FORMULAS_BRANCH}
544 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
545 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
546 # Fallback to the master branch if the branch doesn't exist for this repository
547 _BRANCH=master
548 fi
549 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
550 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
551 exit 1
552 fi
553 } || {
554 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
555 git fetch origin/${_BRANCH} || git fetch --all
556 git checkout ${_BRANCH} && git pull || git pull;
557 cd -
558 }
559 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
560 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
561 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
562 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
563 done
564
565 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
566 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
567}
568
569
570saltmaster_bootstrap() {
571
572 log_info "Salt master setup"
573 test -n "$MASTER_HOSTNAME" || exit 1
574
575 clone_reclass
576 # override some envs from cluster level *.env, use with care
577 source_local_envs
578
Petr Michalec7ed7d8f2017-10-10 15:38:29 +0200579 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {} || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200580 pkill -9 salt-minion
projoke3c30e7d2017-10-19 17:15:40 +0800581 SCRIPTS=$(dirname $0)
582
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200583 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
Adam Tengler035f24f2017-09-11 19:29:31 +0200584 export MASTER_IP=${MASTER_IP:-127.0.0.1}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200585 export MINION_ID=${MASTER_HOSTNAME}
586 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
587 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
588 fi
589 # call local "setup() master"
590 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
591 if ! setup master; then
592 #cat /tmp/salt-master-setup.log
593 log_err "salt master setup() failed."
594 exit 1
595 else
596 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
597 fi
598 }
599
Petr Michalec981f3cc2017-11-15 17:40:31 +0100600 log_info "Install reclass"
601 install_reclass ${RECLASS_VERSION/dev*/develop}
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200602
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200603 log_info "Re/starting salt services"
Petr Michalec7ed7d8f2017-10-10 15:38:29 +0200604 $SUDO service salt-minion stop
605 $SUDO service salt-master stop
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200606 sleep 10
Petr Michalec7ed7d8f2017-10-10 15:38:29 +0200607 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {} || true
608 pkill -9 salt-minion
609 $SUDO service salt-master start
610 $SUDO service salt-minion start
611 sleep 15
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200612}
613
614# Init salt master
615saltmaster_init() {
616
617 log_info "Runing saltmaster states"
618 test -n "$MASTER_HOSTNAME" || exit 1
619
620 set -e
621 $SUDO salt-call saltutil.sync_all >/dev/null
622
623 # TODO: Placeholder update saltmaster spec (nodes/FQDN.yml) to be able to bootstrap with minimal configuration
624 # (ie: with linux, git, salt formulas)
625
626 #log_info "Verify SaltMaster, before salt-master is fully initialized"
627 #if ! $SUDO reclass-salt -p ${MASTER_HOSTNAME} &> /tmp/${MASTER_HOSTNAME}.pillar;then
628 # log_warn "Node verification before initialization failed."; cat /tmp/${MASTER_HOSTNAME}.pillar;
629 #fi
630
Petr Michalecda014202017-08-18 11:26:07 +0200631 # workarond isolated and not fully bootstraped environments
Petr Michalec2801acb2017-10-31 15:00:15 +0100632 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec64adbd72017-10-31 15:26:41 +0100633 SALT_MASTER_PILLAR='"salt":{"master":{"pillar":{"reclass":{"ignore_class_notfound": "'${RECLASS_IGNORE_CLASS_NOTFOUND:-False}'", "ignore_class_regexp": ["service.*"]}}}},'
Petr Michalec2801acb2017-10-31 15:00:15 +0100634 fi
Petr Michalec64adbd72017-10-31 15:26:41 +0100635 PILLAR='{'${SALT_MASTER_PILLAR}' "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalecda014202017-08-18 11:26:07 +0200636
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200637 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
642 log_info "State: salt.master.pillar"
Petr Michalecda014202017-08-18 11:26:07 +0200643 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar="$PILLAR"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200644 # Note: sikp reclass data dir states
645 # in order to avoid pull from configured repo/branch
646
647 # Revert temporary SaltMaster minimal configuration, if any
648 pushd $RECLASS_ROOT
649 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
Petr Michalec87778fc2017-11-02 10:13:25 +0100650 PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200651 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200652 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200653 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
654 git checkout -- $RECLASS_ROOT/nodes || true
655 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
656 log_info "State: salt.master.env"
Petr Michalecda014202017-08-18 11:26:07 +0200657 if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env pillar="$PILLAR"; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200658 log_err "State salt.master.env failed, keep your eyes wide open."
659 fi
660 log_info "State: salt.master.pillar"
Petr Michalecda014202017-08-18 11:26:07 +0200661 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar="$PILLAR"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200662 fi
663 popd
664
Petr Michalecda014202017-08-18 11:26:07 +0200665 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
666 log_info "State: salt.master.service"
Petr Michalec9fec8b32017-10-05 14:46:42 +0200667 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalecda014202017-08-18 11:26:07 +0200668
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200669 log_info "State: salt.master.storage.node"
670 set +e
Petr Michaleca5141342017-08-16 12:55:20 +0200671 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec9fec8b32017-10-05 14:46:42 +0200672 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200673 ret=$?
674 set -e
675
676 if [[ $ret -eq 2 ]]; then
677 log_err "State reclass.storage.node failed with exit code 2 but continuing."
678 elif [[ $ret -ne 0 ]]; then
679 log_err "State reclass.storage.node failed with exit code $ret"
680 exit 1
681 fi
682
683 log_info "Re/starting salt services"
684 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
Petr Michalecc0bfa972017-09-08 15:21:01 +0200685 $SUDO service salt-minion stop
686 $SUDO service salt-master stop
Petr Michalec7ed7d8f2017-10-10 15:38:29 +0200687 sleep 10
688 pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {} || true
Petr Michalecc0bfa972017-09-08 15:21:01 +0200689 $SUDO service salt-master start
690 $SUDO service salt-minion start
Petr Michalec7ed7d8f2017-10-10 15:38:29 +0200691 sleep 15
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200692 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
693
694 verify_salt_master
695 set +e
696
697}
698
699
700function verify_salt_master() {
701 set -e
702
703 log_info "Verify Salt master"
704 test -n "$MASTER_HOSTNAME" || exit 1
705
706 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200707 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
708 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200709 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
710 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
711 $SUDO salt-call --no-color grains.items
712 $SUDO salt-call --no-color pillar.data
713 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200714 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200715 if ! $SUDO reclass --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
716 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
717 exit 1
718 fi
719}
720
721function verify_salt_minion() {
722 set -e
723 node=$1
724 log_info "Verifying ${node}"
725 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
726 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
727 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
728 fi
729 if ! $SUDO reclass --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
730 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
731 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
732 exit 1
733 fi
734 fi
735}
736
737function verify_salt_minions() {
738 #set -e
Petr Michalec679f15b2017-09-18 16:16:29 +0200739 NODES=$(find $RECLASS_ROOT/nodes/_generated/ -name "*.yml" | grep -v "cfg")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200740 log_info "Verifying minions: $(echo ${NODES}|xargs)"
741
742 # Parallel
743 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
744 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
745
746 function filterFails() {
747 grep -v '/grains' | tee -a $1 | tail -n20
748 }
749
750 log_info "Verify nodes"
751 passed=0
752 for node in ${NODES}; do
753 node=$(basename $node .yml)
754
755 # filter first in cluster.. ctl-01, mon-01, etc..
756 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
757 verify_salt_minion ${node} || continue
758 else
759 echo Skipped $node.
760 fi
761 passed=$(($passed+1))
762 done
763 # fail on failures
764 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
765 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
766 test ! $passed -lt $total || {
767 tail -n50 /tmp/*.pillar_verify
768 return 1
769 }
770}
771
772
773
774##########################################
775# To install salt master/minon
776
777function install() {
778 setup $@
779}
780
781function setup() {
782 # CLI
783 while [ x"$1" != x"" ]; do
784 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
785
786 case $1 in
787 master )
788 install_salt_master_$SALT_SOURCE
789 install_salt_minion_$SALT_SOURCE
790 install_salt_formula_$FORMULAS_SOURCE
791 ;;
792 minion )
793 install_salt_minion_$SALT_SOURCE
794 ;;
795 esac
796 shift
797 done
798 echo DONE
799}
800
801function bootstrap() {
802 log_info "Bootstrap & verification of SaltMaster and configured minions."
803 trap _atexit INT TERM EXIT
804
805 system_config_master
806 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200807 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200808 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200809}
810
811function default() {
812 bootstrap $@
813}
814
815
816##########################################
817[[ "$0" != "$BASH_SOURCE" ]] || {
818# unless file is being sourced
819 default $@
820}