blob: 45de520fcbab358d6bfd5d345821ceb956557a0f [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
ibumarskovf72e05e2017-11-29 14:37:48 +030057export RECLASS_BRANCH=${RECLASS_BRANCH:-master}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020058
59# formula
60export FORMULAS_BASE=${FORMULAS_BASE:-https://github.com/salt-formulas}
61export FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas}
62export FORMULAS_BRANCH=${FORMULAS_BRANCH:-master}
63export FORMULAS_SOURCE=${FORMULAS_SOURCE:-pkg} # pkg/git
Petr Michalec515356f2017-11-16 11:20:20 +010064# Essential set of formulas (known to by used on cfg01 node for most setups during bootsrap)
65# DEPRECATED: FORMULAS_SALT_MASTER=${FORMULAS_SALT_MASTER:- $EXTRA_FORMULAS maas memcached 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 horizon}
Petr Michalece07cd0b2017-11-15 11:41:57 +010066FORMULAS_SALT_MASTER=${FORMULAS_SALT_MASTER:- $EXTRA_FORMULAS}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020067# minimal set of formulas for salt-master bootstrap
Petr Michalec515356f2017-11-16 11:20:20 +010068declare -a FORMULAS_SALT_MASTER=(reclass salt git openssh linux $(echo $FORMULAS_SALT_MASTER))
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020069export 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
Petr Michalec33c04892017-09-08 15:10:30 +020082BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True}
Petr Michalece11cf612017-09-08 18:21:48 +020083BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION:- stable 2016.3 }
Petr Michalec33c04892017-09-08 15:10:30 +020084BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX $BOOTSTRAP_SALTSTACK_VERSION }
Petr Michalece11cf612017-09-08 18:21:48 +020085SALT_SOURCE=${SALT_SOURCE:-pkg}
86# the version below is used salt pillar data
87SALT_VERSION=${SALT_VERSION:-latest}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020088
89# environment
90if [ "$FORMULAS_SOURCE" == "git" ]; then
91 SALT_ENV=${SALT_ENV:-dev}
92elif [ "$FORMULAS_SOURCE" == "pkg" ]; then
93 SALT_ENV=${SALT_ENV:-prd}
94fi
Petr Michalec05b52452017-09-08 14:26:59 +020095eval "$(grep -h '=' /etc/*release 2> /dev/null)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020096PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z)
97case $PLATFORM_FAMILY in
98 debian )
99 PKGTOOL="$SUDO apt-get"
100 test ${VERSION_ID//\.*/} -ge 16 && {
101 SVCTOOL=service
102 } || { SVCTOOL=service
103 }
104 ;;
105 rhel )
106 PKGTOOL="$SUDO yum"
107 test ${VERSION_ID//\.*/} -ge 7 && {
108 SVCTOOL=systemctl
109 } || { SVCTOOL=service
110 }
111 ;;
112esac
113
114export PLATFORM_FAMILY
115export PKGTOOL
116export SVCTOOL
117
118##########################################
119# FUNCTIONS
120
121log_info() {
122 echo -e "${YELLOW}[INFO] $* ${NC}"
123}
124
125log_warn() {
126 echo -e "${MAGENTA}[WARN] $* ${NC}"
127}
128
Petr Michalec24b30e82017-08-21 10:59:18 +0200129log_debug() {
130 echo -e "${CYAN}[WARN] $* ${NC}"
131}
132
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200133log_err() {
134 echo -e "${RED}[ERROR] $* ${NC}" >&2
135}
136
137configure_pkg_repo()
138{
139
140 case $PLATFORM_FAMILY in
141 debian)
142 if [ -n "$APT_REPOSITORY_PPA" ]; then
143 which add-apt-repository || $SUDO apt-get install -y software-properties-common
144 $SUDO add-apt-repository -y ppa:${APT_REPOSITORY_PPA}
145 else
Mykyta Karpinfbcb5482017-10-04 14:15:25 +0300146 echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/mcp_salt.list >/dev/null
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200147 curl -sL $APT_REPOSITORY_GPG | $SUDO apt-key add -
148 fi
149 $SUDO apt-get clean
150 $SUDO apt-get update
151 ;;
152 rhel)
153 $SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm
154 $SUDO yum clean all
155 ;;
156 esac
157
158}
159
160_atexit() {
161 RETVAL=$?
162 trap true INT TERM EXIT
163
164 if [ $RETVAL -ne 0 ]; then
165 log_err "Execution failed"
166 else
167 log_info "Execution successful"
168 fi
169 return $RETVAL
170}
171
172retry() {
173 local tries
174 if [[ $1 =~ ^[0-9]+$ ]]; then
175 tries=$1; shift
176 else
177 tries=3
178 fi
Petr Michalec262e3ad2017-10-06 13:41:58 +0200179 ret=1
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200180 for i in $(seq 1 $tries); do
Petr Michalec262e3ad2017-10-06 13:41:58 +0200181 "$@" && return $? || ret=$?
182 sleep $i
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200183 done
Petr Michalec9fec8b32017-10-05 14:46:42 +0200184 return $ret
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200185}
186
187function clone_reclass() {
Petr Michaleca361d4e2017-09-08 11:38:16 +0200188 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200189 # No reclass at all, clone from given address
190 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
ibumarskovf72e05e2017-11-29 14:37:48 +0300191 if echo ${RECLASS_BRANCH} | egrep -q "^refs"; then
192 git clone ${RECLASS_ADDRESS} ${RECLASS_ROOT}
193 cd ${RECLASS_ROOT}
194 git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH} && git checkout FETCH_HEAD
195 export RECLASS_REVISION=$(git rev-parse HEAD)
196 cd -
197 else
198 git clone -b ${RECLASS_BRANCH} ${RECLASS_ADDRESS} ${RECLASS_ROOT};
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200199 fi;
200 fi;
Petr Michalec679f15b2017-09-18 16:16:29 +0200201 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michaleca361d4e2017-09-08 11:38:16 +0200202 log_err "Reclass ${RECLASS_ROOT} is not fetched locally;"
203 ls -Rla ${RECLASS_ROOT}
204 exit 1
205 fi;
206 $SUDO mkdir -p $RECLASS_ROOT/classes/service
207 $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200208}
209
210
211##########################################
212# Main calls
213
Petr Michalecda014202017-08-18 11:26:07 +0200214system_config_ssh_conf() {
215 for conf in ~/.ssh/config /root/.ssh/config; do
216 $SUDO mkdir -p $(dirname $conf)
217 if ! grep StrictHostKeyChecking $conf; then
218 # this should be used only in CI environment
219 echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee $conf >/dev/null
220 fi
221 done
222 if ! grep github.com ~/.ssh/known_hosts; then
223 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
224 fi
225}
226
227system_config_salt_modules_prereq() {
228 # salt-formulas custom modules dependencies, etc:
229 $SUDO $PKGTOOL install -y iproute2 curl sudo apt-transport-https python-psutil python-apt python-m2crypto python-oauth python-pip &>/dev/null
230}
231
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200232system_config_minion() {
233 log_info "System configuration salt minion"
234}
235
236system_config_master() {
237 log_info "System configuration salt master"
238
Petr Michalecda014202017-08-18 11:26:07 +0200239 system_config_salt_modules_prereq
240 system_config_ssh_conf
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200241
Petr Michalecda014202017-08-18 11:26:07 +0200242 if ! grep '127.0.1.2.*salt' /etc/hosts; then
243 echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null
244 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200245
246 which reclass || $SUDO $PKGTOOL install -y reclass
247
248 which reclass-salt || {
249 test -e /usr/share/reclass/reclass-salt && {
250 ln -fs /usr/share/reclass/reclass-salt /usr/bin
251 }
252 }
253}
254
255configure_salt_master()
256{
257
258 echo "Configuring salt-master ..."
259
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200260 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec29d3ea32017-10-31 17:22:36 +0100261 read -d '' IGNORE_CLASS_NOTFOUND <<-EOF
262 ignore_class_notfound: True
263 ignore_class_regexp:
264 - 'service.*'
265EOF
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200266 fi
267
Petr Michalecdd1a0472017-09-06 19:27:24 +0200268 # to force alternative reclass module path
Petr Michalecd9d0a322017-09-06 19:38:32 +0200269 if [ -n "$RECLASS_SOURCE_PATH" ]; then
270 RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}"
271 else
272 RECLASS_SOURCE_PATH=""
273 fi
Petr Michalecdd1a0472017-09-06 19:27:24 +0200274
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200275 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
276 cat <<-EOF > /etc/salt/master.d/master.conf
277 file_roots:
278 base:
279 - /usr/share/salt-formulas/env
280 prd:
281 - /srv/salt/env/prd
282 dev:
283 - /srv/salt/env/dev
284 pillar_opts: False
285 open_mode: True
286 reclass: &reclass
287 storage_type: yaml_fs
288 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200289 ${IGNORE_CLASS_NOTFOUND}
Petr Michalecdd1a0472017-09-06 19:27:24 +0200290 ${RECLASS_SOURCE_PATH}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200291 ext_pillar:
292 - reclass: *reclass
293 master_tops:
294 reclass: *reclass
295EOF
296
297 echo "Configuring reclass ..."
298
299 [ ! -d /etc/reclass ] && mkdir /etc/reclass
300 cat <<-EOF > /etc/reclass/reclass-config.yml
301 storage_type: yaml_fs
302 pretty_print: True
303 output: yaml
304 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200305EOF
306
307 clone_reclass
308 # override some envs from cluster level *.env, use with care
309 source_local_envs
310
311 cd ${RECLASS_ROOT}
312 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
313 # Possibly subrepo checkout needed
314 git submodule update --init --recursive
315 fi
316
Petr Michalec212cc6a2017-08-17 21:39:40 +0200317 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec51ece842017-09-06 20:44:15 +0200318 rm -rvf ${RECLASS_ROOT}/nodes/_generated/*
319
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200320 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200321 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200322 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
Petr Michalec24b30e82017-08-21 10:59:18 +0200323 log_warn "Salt Master node specification has not been found in model."
324 log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200325 cat <<-EOF > ${CONFIG}
326 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200327 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200328 parameters:
329 _param:
Petr Michaleca7de6df2017-08-31 12:16:57 +0200330 salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200331 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200332 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
ibumarskovf72e05e2017-11-29 14:37:48 +0300333 reclass_data_revision: ${RECLASS_REVISION:-$RECLASS_BRANCH}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200334 reclass_data_repository: "$RECLASS_ADDRESS"
Petr Michaleca7de6df2017-08-31 12:16:57 +0200335 reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200336 linux_system_codename: ${DISTRIB_CODENAME}
337 cluster_name: ${CLUSTER_NAME}
338 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200339 linux:
340 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200341 name: ${HOSTNAME:-cfg01}
342 domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200343 # ########
344EOF
345
346 if [ "$SALT_VERSION" == "latest" ]; then
347 VERSION=""
348 else
349 VERSION="version: $SALT_VERSION"
350 fi
351
352 cat <<-EOF >> ${CONFIG}
353 salt:
354 master:
355 accept_policy: open_mode
356 source:
357 engine: $SALT_SOURCE
358 $VERSION
359 minion:
360 source:
361 engine: $SALT_SOURCE
362 $VERSION
363 # ########
364 # vim: ft=yaml sw=2 ts=2 sts=2
365EOF
366 fi
Petr Michalec24b30e82017-08-21 10:59:18 +0200367
368 log_debug "Salt Master node config yaml:"
369 log_debug "$(cat ${CONFIG})"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200370}
371
372configure_salt_minion()
373{
374 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
375 cat <<-EOF > /etc/salt/minion.d/minion.conf
Petr Michaleca7de6df2017-08-31 12:16:57 +0200376 master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200377 id: $MINION_ID
378 EOF
379}
380
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200381install_reclass()
382{
383 VERSION=${1:-$RECLASS_VERSION}
Petr Michalec372fa032017-11-15 17:36:19 +0100384 VERSION=${VERSION:-pkg}
Petr Michalec800dd072017-11-15 17:19:26 +0100385 case ${VERSION} in
386 pkg|package)
387 which reclass || $SUDO $PKGTOOL install -y reclass
388 ;;
389 *)
Petr Michalec981f3cc2017-11-15 17:40:31 +0100390 log_warn "Install development version of reclass"
Petr Michalec1b37d3d2017-11-15 18:20:46 +0100391 # Note: dev/git/pip version...
392 # It replaces all local reclass versions on system
393 # Required for reclass/git features:
394 # $SUDO $PKGTOOL install -y libffi-dev libgit2-dev || true
Petr Michalec800dd072017-11-15 17:19:26 +0100395 for s in $(python -c "import site; print(' '.join(site.getsitepackages()))"); do
Petr Michalec23020832017-11-15 17:34:49 +0100396 sudo -H pip install --install-option="--prefix=" --upgrade --force-reinstall -I \
Petr Michalec800dd072017-11-15 17:19:26 +0100397 -t "$s" git+https://github.com/salt-formulas/reclass.git@${VERSION};
398 done
399 ;;
400 esac
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200401}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200402
403install_salt_master_pkg()
404{
405 echo -e "\nPreparing base OS repository ...\n"
406
407 configure_pkg_repo
408
409 echo -e "\nInstalling salt master ...\n"
410
411 case $PLATFORM_FAMILY in
412 debian)
413 $SUDO apt-get install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200414 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
415 ;;
416 rhel)
417 yum install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200418 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
419 ;;
420 esac
421
422 which reclass-salt || {
423 test -e /usr/share/reclass/reclass-salt && {
424 ln -fs /usr/share/reclass/reclass-salt /usr/bin
425 }
426 }
427
428 configure_salt_master
429
430 echo -e "\nRestarting services ...\n"
431 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
432 $SVCTOOL salt-master restart
433}
434
435install_salt_master_pip()
436{
437 echo -e "\nPreparing base OS repository ...\n"
438
439 case $PLATFORM_FAMILY in
440 debian)
441 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200442 ;;
443 rhel)
444 $SUDO yum install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200445 ;;
446 esac
447
448 echo -e "\nInstalling salt master ...\n"
449 # TODO: replace with saltstack bootstrap script
450
451 if [ "$SALT_VERSION" == "latest" ]; then
452 pip install salt
453 else
454 pip install salt==$SALT_VERSION
455 fi
456
457 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
458 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
459
460 which reclass-salt || {
461 test -e /usr/share/reclass/reclass-salt && {
462 ln -fs /usr/share/reclass/reclass-salt /usr/bin
463 }
464 }
465
466 configure_salt_master
467
468 echo -e "\nRestarting services ...\n"
469 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
470 $SVCTOOL salt-master restart
471}
472
473
474
475install_salt_minion_pkg()
476{
477
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200478 echo -e "\nInstalling salt minion ...\n"
479
480 case $PLATFORM_FAMILY in
481 debian)
482 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
483 ;;
484 rhel)
485 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
486 ;;
487 esac
488
489
490 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200491 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200492}
493
494install_salt_minion_pip()
495{
496 echo -e "\nInstalling salt minion ...\n"
497
498 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
499 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
500
501 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200502 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200503}
504
505
506install_salt_formula_pkg()
507{
508 configure_pkg_repo
509
510 case $PLATFORM_FAMILY in
511 debian)
512 echo "Configuring necessary formulas ..."
513
514 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
515 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100516 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200517 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
518 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
519 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
520 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
521 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
522 exit 1
523 fi
524 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
525 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
526 done
527 ;;
528 rhel)
529 # TODO
530 ;;
531 esac
532
533 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
534 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
535}
536
537install_salt_formula_git()
538{
539 echo "Configuring necessary formulas ..."
540
541 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
542 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100543 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200544 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
545 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
546 _BRANCH=${FORMULAS_BRANCH}
547 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
548 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
549 # Fallback to the master branch if the branch doesn't exist for this repository
550 _BRANCH=master
551 fi
552 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
553 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
554 exit 1
555 fi
556 } || {
557 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
558 git fetch origin/${_BRANCH} || git fetch --all
559 git checkout ${_BRANCH} && git pull || git pull;
560 cd -
561 }
562 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
563 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
564 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
565 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
566 done
567
568 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
569 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
570}
571
Petr Michalece9c84522017-12-13 17:14:08 +0100572saltservice_stop() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100573 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100574 $SUDO service salt-minion stop
575 $SUDO service salt-master stop
576 sleep ${SALT_STOPSTART_WAIT:-30}
Petr Michalec7822efe2017-12-15 16:19:32 +0100577 ${SUDO} pkill -9 salt-master && ${SUDO} rm -rf /var/run/salt/master/* || true
Petr Michalece9c84522017-12-13 17:14:08 +0100578 ${SUDO} pkill -9 salt-minion
579}
580saltservice_start() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100581 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100582 $SUDO service salt-master start
583 $SUDO service salt-minion start
584 sleep ${SALT_STOPSTART_WAIT:-30}
585}
586
587saltservice_restart() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100588 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100589 saltservice_stop
590 saltservice_start
591}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200592
593saltmaster_bootstrap() {
594
595 log_info "Salt master setup"
596 test -n "$MASTER_HOSTNAME" || exit 1
597
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200598 # override some envs from cluster level *.env, use with care
599 source_local_envs
600
Petr Michalece9c84522017-12-13 17:14:08 +0100601 saltservice_stop
602
603 clone_reclass
604
projoke3c30e7d2017-10-19 17:15:40 +0800605 SCRIPTS=$(dirname $0)
Petr Michalece9c84522017-12-13 17:14:08 +0100606
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200607 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
Adam Tengler035f24f2017-09-11 19:29:31 +0200608 export MASTER_IP=${MASTER_IP:-127.0.0.1}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200609 export MINION_ID=${MASTER_HOSTNAME}
610 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
611 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
612 fi
613 # call local "setup() master"
614 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
615 if ! setup master; then
616 #cat /tmp/salt-master-setup.log
617 log_err "salt master setup() failed."
618 exit 1
619 else
620 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
621 fi
622 }
623
Petr Michalec981f3cc2017-11-15 17:40:31 +0100624 log_info "Install reclass"
625 install_reclass ${RECLASS_VERSION/dev*/develop}
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200626
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200627 log_info "Re/starting salt services"
Petr Michalece9c84522017-12-13 17:14:08 +0100628 saltservice_restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200629}
630
631# Init salt master
632saltmaster_init() {
633
634 log_info "Runing saltmaster states"
635 test -n "$MASTER_HOSTNAME" || exit 1
636
637 set -e
638 $SUDO salt-call saltutil.sync_all >/dev/null
639
Petr Michalecda014202017-08-18 11:26:07 +0200640 # workarond isolated and not fully bootstraped environments
Petr Michalec2801acb2017-10-31 15:00:15 +0100641 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec54ee4082017-11-30 17:02:17 +0100642 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 +0100643 fi
Petr Michalec8ce8a652017-11-30 15:39:20 +0100644 PILLAR='{'${SALT_MASTER_PILLAR}' "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalecda014202017-08-18 11:26:07 +0200645
Petr Michalec8b92d552017-11-29 12:28:50 +0100646 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100647 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100648 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200649 fi
650
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200651 # Revert temporary SaltMaster minimal configuration, if any
652 pushd $RECLASS_ROOT
653 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
Petr Michalec87778fc2017-11-02 10:13:25 +0100654 PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200655 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200656 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200657 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
658 git checkout -- $RECLASS_ROOT/nodes || true
659 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
Petr Michalec7edf8322017-11-30 12:01:09 +0100660 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100661 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100662 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200663 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200664 fi
665 popd
666
Petr Michalecda014202017-08-18 11:26:07 +0200667 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
668 log_info "State: salt.master.service"
Petr Michalec9fec8b32017-10-05 14:46:42 +0200669 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalec4f043772017-12-19 15:40:54 +0100670 saltservice_start
Petr Michalecda014202017-08-18 11:26:07 +0200671
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200672 log_info "State: salt.master.storage.node"
673 set +e
Petr Michalec7edf8322017-11-30 12:01:09 +0100674 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
675 # FIXME: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec9fec8b32017-10-05 14:46:42 +0200676 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200677 ret=$?
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200678
Petr Michalecdd646db2017-12-18 15:56:18 +0100679 set -e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200680 if [[ $ret -eq 2 ]]; then
681 log_err "State reclass.storage.node failed with exit code 2 but continuing."
682 elif [[ $ret -ne 0 ]]; then
683 log_err "State reclass.storage.node failed with exit code $ret"
684 exit 1
685 fi
686
Petr Michalecdd646db2017-12-18 15:56:18 +0100687 set +e
688 log_info "Updating minion.conf -> master: localhost"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200689 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
Petr Michalecdd646db2017-12-18 15:56:18 +0100690
691 log_info "Re/starting salt services" # in order to load new deployed configuration from model
Petr Michalece9c84522017-12-13 17:14:08 +0100692 saltservice_restart
Petr Michalecdd646db2017-12-18 15:56:18 +0100693
694 log_info "Salt Util sync all"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200695 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
696
697 verify_salt_master
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200698
699}
700
701
702function verify_salt_master() {
703 set -e
704
705 log_info "Verify Salt master"
706 test -n "$MASTER_HOSTNAME" || exit 1
707
708 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200709 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
710 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200711 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
712 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
713 $SUDO salt-call --no-color grains.items
714 $SUDO salt-call --no-color pillar.data
715 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200716 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200717 if ! $SUDO reclass --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
718 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
719 exit 1
720 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100721 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200722}
723
724function verify_salt_minion() {
725 set -e
726 node=$1
727 log_info "Verifying ${node}"
728 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
729 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
730 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
731 fi
732 if ! $SUDO reclass --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
733 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
734 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
735 exit 1
736 fi
737 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100738 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200739}
740
741function verify_salt_minions() {
742 #set -e
Petr Michalec679f15b2017-09-18 16:16:29 +0200743 NODES=$(find $RECLASS_ROOT/nodes/_generated/ -name "*.yml" | grep -v "cfg")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200744 log_info "Verifying minions: $(echo ${NODES}|xargs)"
745
746 # Parallel
747 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
748 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
749
750 function filterFails() {
751 grep -v '/grains' | tee -a $1 | tail -n20
752 }
753
754 log_info "Verify nodes"
755 passed=0
756 for node in ${NODES}; do
757 node=$(basename $node .yml)
758
759 # filter first in cluster.. ctl-01, mon-01, etc..
760 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
761 verify_salt_minion ${node} || continue
762 else
763 echo Skipped $node.
764 fi
765 passed=$(($passed+1))
766 done
767 # fail on failures
768 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
769 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
770 test ! $passed -lt $total || {
771 tail -n50 /tmp/*.pillar_verify
772 return 1
773 }
Petr Michalec7edf8322017-11-30 12:01:09 +0100774 #set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200775}
776
777
778
779##########################################
780# To install salt master/minon
781
782function install() {
783 setup $@
784}
785
786function setup() {
787 # CLI
788 while [ x"$1" != x"" ]; do
789 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
790
791 case $1 in
792 master )
793 install_salt_master_$SALT_SOURCE
794 install_salt_minion_$SALT_SOURCE
795 install_salt_formula_$FORMULAS_SOURCE
796 ;;
797 minion )
798 install_salt_minion_$SALT_SOURCE
799 ;;
800 esac
801 shift
802 done
803 echo DONE
804}
805
806function bootstrap() {
807 log_info "Bootstrap & verification of SaltMaster and configured minions."
808 trap _atexit INT TERM EXIT
809
810 system_config_master
811 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200812 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200813 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200814}
815
816function default() {
817 bootstrap $@
818}
819
820
821##########################################
822[[ "$0" != "$BASH_SOURCE" ]] || {
823# unless file is being sourced
824 default $@
825}