blob: f35689aa80a1e0634b895dbf8baab24a744b0007 [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 }
Jakub Josef0b611742018-01-09 17:02:01 +010084BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION//latest*/stable}
Petr Michalec33c04892017-09-08 15:10:30 +020085BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX $BOOTSTRAP_SALTSTACK_VERSION }
Petr Michalece11cf612017-09-08 18:21:48 +020086SALT_SOURCE=${SALT_SOURCE:-pkg}
87# the version below is used salt pillar data
88SALT_VERSION=${SALT_VERSION:-latest}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020089
90# environment
91if [ "$FORMULAS_SOURCE" == "git" ]; then
92 SALT_ENV=${SALT_ENV:-dev}
93elif [ "$FORMULAS_SOURCE" == "pkg" ]; then
94 SALT_ENV=${SALT_ENV:-prd}
95fi
Petr Michalec05b52452017-09-08 14:26:59 +020096eval "$(grep -h '=' /etc/*release 2> /dev/null)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020097PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z)
98case $PLATFORM_FAMILY in
99 debian )
100 PKGTOOL="$SUDO apt-get"
101 test ${VERSION_ID//\.*/} -ge 16 && {
102 SVCTOOL=service
103 } || { SVCTOOL=service
104 }
105 ;;
106 rhel )
107 PKGTOOL="$SUDO yum"
108 test ${VERSION_ID//\.*/} -ge 7 && {
109 SVCTOOL=systemctl
110 } || { SVCTOOL=service
111 }
112 ;;
113esac
114
115export PLATFORM_FAMILY
116export PKGTOOL
117export SVCTOOL
118
119##########################################
120# FUNCTIONS
121
122log_info() {
123 echo -e "${YELLOW}[INFO] $* ${NC}"
124}
125
126log_warn() {
127 echo -e "${MAGENTA}[WARN] $* ${NC}"
128}
129
Petr Michalec24b30e82017-08-21 10:59:18 +0200130log_debug() {
131 echo -e "${CYAN}[WARN] $* ${NC}"
132}
133
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200134log_err() {
135 echo -e "${RED}[ERROR] $* ${NC}" >&2
136}
137
138configure_pkg_repo()
139{
140
141 case $PLATFORM_FAMILY in
142 debian)
143 if [ -n "$APT_REPOSITORY_PPA" ]; then
144 which add-apt-repository || $SUDO apt-get install -y software-properties-common
145 $SUDO add-apt-repository -y ppa:${APT_REPOSITORY_PPA}
146 else
Mykyta Karpinfbcb5482017-10-04 14:15:25 +0300147 echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/mcp_salt.list >/dev/null
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200148 curl -sL $APT_REPOSITORY_GPG | $SUDO apt-key add -
149 fi
150 $SUDO apt-get clean
151 $SUDO apt-get update
152 ;;
153 rhel)
154 $SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm
155 $SUDO yum clean all
156 ;;
157 esac
158
159}
160
161_atexit() {
162 RETVAL=$?
163 trap true INT TERM EXIT
164
165 if [ $RETVAL -ne 0 ]; then
166 log_err "Execution failed"
167 else
168 log_info "Execution successful"
169 fi
170 return $RETVAL
171}
172
173retry() {
174 local tries
175 if [[ $1 =~ ^[0-9]+$ ]]; then
176 tries=$1; shift
177 else
178 tries=3
179 fi
Petr Michalec262e3ad2017-10-06 13:41:58 +0200180 ret=1
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200181 for i in $(seq 1 $tries); do
Petr Michalec262e3ad2017-10-06 13:41:58 +0200182 "$@" && return $? || ret=$?
183 sleep $i
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200184 done
Petr Michalec9fec8b32017-10-05 14:46:42 +0200185 return $ret
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200186}
187
188function clone_reclass() {
Petr Michaleca361d4e2017-09-08 11:38:16 +0200189 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200190 # No reclass at all, clone from given address
191 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
ibumarskovf72e05e2017-11-29 14:37:48 +0300192 if echo ${RECLASS_BRANCH} | egrep -q "^refs"; then
193 git clone ${RECLASS_ADDRESS} ${RECLASS_ROOT}
194 cd ${RECLASS_ROOT}
195 git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH} && git checkout FETCH_HEAD
196 export RECLASS_REVISION=$(git rev-parse HEAD)
197 cd -
198 else
199 git clone -b ${RECLASS_BRANCH} ${RECLASS_ADDRESS} ${RECLASS_ROOT};
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200200 fi;
201 fi;
Petr Michalec679f15b2017-09-18 16:16:29 +0200202 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michaleca361d4e2017-09-08 11:38:16 +0200203 log_err "Reclass ${RECLASS_ROOT} is not fetched locally;"
204 ls -Rla ${RECLASS_ROOT}
205 exit 1
206 fi;
207 $SUDO mkdir -p $RECLASS_ROOT/classes/service
208 $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200209}
210
211
212##########################################
213# Main calls
214
Petr Michalecda014202017-08-18 11:26:07 +0200215system_config_ssh_conf() {
216 for conf in ~/.ssh/config /root/.ssh/config; do
217 $SUDO mkdir -p $(dirname $conf)
218 if ! grep StrictHostKeyChecking $conf; then
219 # this should be used only in CI environment
220 echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee $conf >/dev/null
221 fi
222 done
223 if ! grep github.com ~/.ssh/known_hosts; then
224 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
225 fi
226}
227
228system_config_salt_modules_prereq() {
229 # salt-formulas custom modules dependencies, etc:
230 $SUDO $PKGTOOL install -y iproute2 curl sudo apt-transport-https python-psutil python-apt python-m2crypto python-oauth python-pip &>/dev/null
231}
232
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200233system_config_minion() {
234 log_info "System configuration salt minion"
235}
236
237system_config_master() {
238 log_info "System configuration salt master"
239
Petr Michalecda014202017-08-18 11:26:07 +0200240 system_config_salt_modules_prereq
241 system_config_ssh_conf
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200242
Petr Michalecda014202017-08-18 11:26:07 +0200243 if ! grep '127.0.1.2.*salt' /etc/hosts; then
244 echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null
245 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200246
247 which reclass || $SUDO $PKGTOOL install -y reclass
248
249 which reclass-salt || {
250 test -e /usr/share/reclass/reclass-salt && {
251 ln -fs /usr/share/reclass/reclass-salt /usr/bin
252 }
253 }
254}
255
256configure_salt_master()
257{
258
259 echo "Configuring salt-master ..."
260
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200261 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec29d3ea32017-10-31 17:22:36 +0100262 read -d '' IGNORE_CLASS_NOTFOUND <<-EOF
263 ignore_class_notfound: True
264 ignore_class_regexp:
265 - 'service.*'
266EOF
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200267 fi
268
Petr Michalecdd1a0472017-09-06 19:27:24 +0200269 # to force alternative reclass module path
Petr Michalecd9d0a322017-09-06 19:38:32 +0200270 if [ -n "$RECLASS_SOURCE_PATH" ]; then
271 RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}"
272 else
273 RECLASS_SOURCE_PATH=""
274 fi
Petr Michalecdd1a0472017-09-06 19:27:24 +0200275
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200276 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
277 cat <<-EOF > /etc/salt/master.d/master.conf
278 file_roots:
279 base:
280 - /usr/share/salt-formulas/env
281 prd:
282 - /srv/salt/env/prd
283 dev:
284 - /srv/salt/env/dev
285 pillar_opts: False
286 open_mode: True
287 reclass: &reclass
288 storage_type: yaml_fs
289 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200290 ${IGNORE_CLASS_NOTFOUND}
Petr Michalecdd1a0472017-09-06 19:27:24 +0200291 ${RECLASS_SOURCE_PATH}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200292 ext_pillar:
293 - reclass: *reclass
294 master_tops:
295 reclass: *reclass
296EOF
297
298 echo "Configuring reclass ..."
299
300 [ ! -d /etc/reclass ] && mkdir /etc/reclass
301 cat <<-EOF > /etc/reclass/reclass-config.yml
302 storage_type: yaml_fs
303 pretty_print: True
304 output: yaml
305 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200306EOF
307
308 clone_reclass
309 # override some envs from cluster level *.env, use with care
310 source_local_envs
311
312 cd ${RECLASS_ROOT}
313 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
314 # Possibly subrepo checkout needed
315 git submodule update --init --recursive
316 fi
317
Petr Michalec212cc6a2017-08-17 21:39:40 +0200318 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec51ece842017-09-06 20:44:15 +0200319 rm -rvf ${RECLASS_ROOT}/nodes/_generated/*
320
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200321 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200322 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200323 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
Petr Michalec24b30e82017-08-21 10:59:18 +0200324 log_warn "Salt Master node specification has not been found in model."
325 log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200326 cat <<-EOF > ${CONFIG}
327 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200328 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200329 parameters:
330 _param:
Petr Michaleca7de6df2017-08-31 12:16:57 +0200331 salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200332 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200333 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
ibumarskovf72e05e2017-11-29 14:37:48 +0300334 reclass_data_revision: ${RECLASS_REVISION:-$RECLASS_BRANCH}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200335 reclass_data_repository: "$RECLASS_ADDRESS"
Petr Michaleca7de6df2017-08-31 12:16:57 +0200336 reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200337 linux_system_codename: ${DISTRIB_CODENAME}
338 cluster_name: ${CLUSTER_NAME}
339 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200340 linux:
341 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200342 name: ${HOSTNAME:-cfg01}
343 domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200344 # ########
345EOF
346
347 if [ "$SALT_VERSION" == "latest" ]; then
348 VERSION=""
349 else
350 VERSION="version: $SALT_VERSION"
351 fi
352
353 cat <<-EOF >> ${CONFIG}
354 salt:
355 master:
356 accept_policy: open_mode
357 source:
358 engine: $SALT_SOURCE
359 $VERSION
360 minion:
361 source:
362 engine: $SALT_SOURCE
363 $VERSION
364 # ########
365 # vim: ft=yaml sw=2 ts=2 sts=2
366EOF
367 fi
Petr Michalec24b30e82017-08-21 10:59:18 +0200368
369 log_debug "Salt Master node config yaml:"
370 log_debug "$(cat ${CONFIG})"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200371}
372
373configure_salt_minion()
374{
375 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
376 cat <<-EOF > /etc/salt/minion.d/minion.conf
Petr Michaleca7de6df2017-08-31 12:16:57 +0200377 master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200378 id: $MINION_ID
379 EOF
380}
381
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200382install_reclass()
383{
384 VERSION=${1:-$RECLASS_VERSION}
Petr Michalec372fa032017-11-15 17:36:19 +0100385 VERSION=${VERSION:-pkg}
Petr Michalec800dd072017-11-15 17:19:26 +0100386 case ${VERSION} in
387 pkg|package)
388 which reclass || $SUDO $PKGTOOL install -y reclass
389 ;;
390 *)
Petr Michalec981f3cc2017-11-15 17:40:31 +0100391 log_warn "Install development version of reclass"
Petr Michalec1b37d3d2017-11-15 18:20:46 +0100392 # Note: dev/git/pip version...
393 # It replaces all local reclass versions on system
394 # Required for reclass/git features:
395 # $SUDO $PKGTOOL install -y libffi-dev libgit2-dev || true
Petr Michalec800dd072017-11-15 17:19:26 +0100396 for s in $(python -c "import site; print(' '.join(site.getsitepackages()))"); do
Petr Michalec23020832017-11-15 17:34:49 +0100397 sudo -H pip install --install-option="--prefix=" --upgrade --force-reinstall -I \
Petr Michalec800dd072017-11-15 17:19:26 +0100398 -t "$s" git+https://github.com/salt-formulas/reclass.git@${VERSION};
399 done
400 ;;
401 esac
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200402}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200403
404install_salt_master_pkg()
405{
406 echo -e "\nPreparing base OS repository ...\n"
407
408 configure_pkg_repo
409
410 echo -e "\nInstalling salt master ...\n"
411
412 case $PLATFORM_FAMILY in
413 debian)
414 $SUDO apt-get 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 rhel)
418 yum install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200419 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
420 ;;
421 esac
Jakub Josef0b611742018-01-09 17:02:01 +0100422
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200423 which reclass-salt || {
424 test -e /usr/share/reclass/reclass-salt && {
425 ln -fs /usr/share/reclass/reclass-salt /usr/bin
426 }
427 }
428
429 configure_salt_master
430
431 echo -e "\nRestarting services ...\n"
432 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
433 $SVCTOOL salt-master restart
434}
435
436install_salt_master_pip()
437{
438 echo -e "\nPreparing base OS repository ...\n"
439
440 case $PLATFORM_FAMILY in
441 debian)
442 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200443 ;;
444 rhel)
445 $SUDO yum install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200446 ;;
447 esac
448
449 echo -e "\nInstalling salt master ...\n"
450 # TODO: replace with saltstack bootstrap script
Jakub Josef0b611742018-01-09 17:02:01 +0100451
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200452 if [ "$SALT_VERSION" == "latest" ]; then
453 pip install salt
454 else
455 pip install salt==$SALT_VERSION
456 fi
457
458 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
459 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
460
461 which reclass-salt || {
462 test -e /usr/share/reclass/reclass-salt && {
463 ln -fs /usr/share/reclass/reclass-salt /usr/bin
464 }
465 }
466
467 configure_salt_master
468
469 echo -e "\nRestarting services ...\n"
470 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
471 $SVCTOOL salt-master restart
472}
473
474
475
476install_salt_minion_pkg()
477{
478
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200479 echo -e "\nInstalling salt minion ...\n"
480
481 case $PLATFORM_FAMILY in
482 debian)
483 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
484 ;;
485 rhel)
486 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
487 ;;
488 esac
489
490
491 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200492 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200493}
494
495install_salt_minion_pip()
496{
497 echo -e "\nInstalling salt minion ...\n"
498
499 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
500 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
501
502 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200503 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200504}
505
506
507install_salt_formula_pkg()
508{
509 configure_pkg_repo
510
511 case $PLATFORM_FAMILY in
512 debian)
513 echo "Configuring necessary formulas ..."
514
515 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
516 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100517 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200518 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
519 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
520 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
521 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
522 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
523 exit 1
524 fi
525 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
526 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
527 done
528 ;;
529 rhel)
530 # TODO
531 ;;
532 esac
533
534 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
535 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
536}
537
538install_salt_formula_git()
539{
540 echo "Configuring necessary formulas ..."
541
542 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
543 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100544 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200545 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
546 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
547 _BRANCH=${FORMULAS_BRANCH}
548 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
549 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
550 # Fallback to the master branch if the branch doesn't exist for this repository
551 _BRANCH=master
552 fi
553 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
554 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
555 exit 1
556 fi
557 } || {
558 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
559 git fetch origin/${_BRANCH} || git fetch --all
560 git checkout ${_BRANCH} && git pull || git pull;
561 cd -
562 }
563 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
564 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
565 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
566 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
567 done
568
569 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
570 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
571}
572
Petr Michalece9c84522017-12-13 17:14:08 +0100573saltservice_stop() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100574 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100575 $SUDO service salt-minion stop
576 $SUDO service salt-master stop
577 sleep ${SALT_STOPSTART_WAIT:-30}
Petr Michalec7822efe2017-12-15 16:19:32 +0100578 ${SUDO} pkill -9 salt-master && ${SUDO} rm -rf /var/run/salt/master/* || true
Petr Michalece9c84522017-12-13 17:14:08 +0100579 ${SUDO} pkill -9 salt-minion
580}
581saltservice_start() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100582 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100583 $SUDO service salt-master start
584 $SUDO service salt-minion start
585 sleep ${SALT_STOPSTART_WAIT:-30}
586}
587
588saltservice_restart() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100589 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100590 saltservice_stop
591 saltservice_start
592}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200593
594saltmaster_bootstrap() {
595
596 log_info "Salt master setup"
597 test -n "$MASTER_HOSTNAME" || exit 1
598
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200599 # override some envs from cluster level *.env, use with care
600 source_local_envs
601
Petr Michalece9c84522017-12-13 17:14:08 +0100602 saltservice_stop
603
604 clone_reclass
605
projoke3c30e7d2017-10-19 17:15:40 +0800606 SCRIPTS=$(dirname $0)
Petr Michalece9c84522017-12-13 17:14:08 +0100607
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200608 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
Adam Tengler035f24f2017-09-11 19:29:31 +0200609 export MASTER_IP=${MASTER_IP:-127.0.0.1}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200610 export MINION_ID=${MASTER_HOSTNAME}
611 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
612 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
613 fi
614 # call local "setup() master"
615 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
616 if ! setup master; then
617 #cat /tmp/salt-master-setup.log
618 log_err "salt master setup() failed."
619 exit 1
620 else
621 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
622 fi
623 }
624
Petr Michalec981f3cc2017-11-15 17:40:31 +0100625 log_info "Install reclass"
626 install_reclass ${RECLASS_VERSION/dev*/develop}
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200627
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200628 log_info "Re/starting salt services"
Petr Michalece9c84522017-12-13 17:14:08 +0100629 saltservice_restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200630}
631
632# Init salt master
633saltmaster_init() {
634
635 log_info "Runing saltmaster states"
636 test -n "$MASTER_HOSTNAME" || exit 1
637
638 set -e
639 $SUDO salt-call saltutil.sync_all >/dev/null
640
Petr Michalecda014202017-08-18 11:26:07 +0200641 # workarond isolated and not fully bootstraped environments
Petr Michalec2801acb2017-10-31 15:00:15 +0100642 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec54ee4082017-11-30 17:02:17 +0100643 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 +0100644 fi
Petr Michalec8ce8a652017-11-30 15:39:20 +0100645 PILLAR='{'${SALT_MASTER_PILLAR}' "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalecda014202017-08-18 11:26:07 +0200646
Petr Michalec8b92d552017-11-29 12:28:50 +0100647 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100648 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100649 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200650 fi
651
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200652 # Revert temporary SaltMaster minimal configuration, if any
653 pushd $RECLASS_ROOT
654 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
Petr Michalec87778fc2017-11-02 10:13:25 +0100655 PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200656 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200657 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200658 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
659 git checkout -- $RECLASS_ROOT/nodes || true
660 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
Petr Michalec7edf8322017-11-30 12:01:09 +0100661 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100662 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100663 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200664 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200665 fi
666 popd
667
Petr Michalecda014202017-08-18 11:26:07 +0200668 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
669 log_info "State: salt.master.service"
Jakub Josef32563412018-01-17 17:55:05 +0100670 # ensure salt master is started
671 saltservice_start
Petr Michalec9fec8b32017-10-05 14:46:42 +0200672 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalec4f043772017-12-19 15:40:54 +0100673 saltservice_start
Petr Michalecda014202017-08-18 11:26:07 +0200674
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200675 log_info "State: salt.master.storage.node"
676 set +e
Petr Michalec7edf8322017-11-30 12:01:09 +0100677 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
678 # FIXME: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec9fec8b32017-10-05 14:46:42 +0200679 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200680 ret=$?
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200681
Petr Michalecdd646db2017-12-18 15:56:18 +0100682 set -e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200683 if [[ $ret -eq 2 ]]; then
684 log_err "State reclass.storage.node failed with exit code 2 but continuing."
685 elif [[ $ret -ne 0 ]]; then
686 log_err "State reclass.storage.node failed with exit code $ret"
687 exit 1
688 fi
689
Petr Michalecdd646db2017-12-18 15:56:18 +0100690 set +e
691 log_info "Updating minion.conf -> master: localhost"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200692 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
Petr Michalecdd646db2017-12-18 15:56:18 +0100693
694 log_info "Re/starting salt services" # in order to load new deployed configuration from model
Petr Michalece9c84522017-12-13 17:14:08 +0100695 saltservice_restart
Petr Michalecdd646db2017-12-18 15:56:18 +0100696
697 log_info "Salt Util sync all"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200698 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
699
700 verify_salt_master
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200701
702}
703
704
705function verify_salt_master() {
706 set -e
707
708 log_info "Verify Salt master"
709 test -n "$MASTER_HOSTNAME" || exit 1
710
711 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200712 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
713 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200714 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
715 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
716 $SUDO salt-call --no-color grains.items
717 $SUDO salt-call --no-color pillar.data
718 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200719 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200720 if ! $SUDO reclass --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
721 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
722 exit 1
723 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100724 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200725}
726
727function verify_salt_minion() {
728 set -e
729 node=$1
730 log_info "Verifying ${node}"
731 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
732 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
733 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
734 fi
735 if ! $SUDO reclass --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
736 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
737 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
738 exit 1
739 fi
740 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100741 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200742}
743
744function verify_salt_minions() {
745 #set -e
Petr Michalec679f15b2017-09-18 16:16:29 +0200746 NODES=$(find $RECLASS_ROOT/nodes/_generated/ -name "*.yml" | grep -v "cfg")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200747 log_info "Verifying minions: $(echo ${NODES}|xargs)"
748
749 # Parallel
750 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
751 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
752
753 function filterFails() {
754 grep -v '/grains' | tee -a $1 | tail -n20
755 }
756
757 log_info "Verify nodes"
758 passed=0
759 for node in ${NODES}; do
760 node=$(basename $node .yml)
761
762 # filter first in cluster.. ctl-01, mon-01, etc..
763 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
764 verify_salt_minion ${node} || continue
765 else
766 echo Skipped $node.
767 fi
768 passed=$(($passed+1))
769 done
770 # fail on failures
771 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
772 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
773 test ! $passed -lt $total || {
774 tail -n50 /tmp/*.pillar_verify
775 return 1
776 }
Petr Michalec7edf8322017-11-30 12:01:09 +0100777 #set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200778}
779
780
781
782##########################################
783# To install salt master/minon
784
785function install() {
786 setup $@
787}
788
789function setup() {
790 # CLI
791 while [ x"$1" != x"" ]; do
792 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
793
794 case $1 in
795 master )
796 install_salt_master_$SALT_SOURCE
797 install_salt_minion_$SALT_SOURCE
798 install_salt_formula_$FORMULAS_SOURCE
799 ;;
800 minion )
801 install_salt_minion_$SALT_SOURCE
802 ;;
803 esac
804 shift
805 done
806 echo DONE
807}
808
809function bootstrap() {
810 log_info "Bootstrap & verification of SaltMaster and configured minions."
811 trap _atexit INT TERM EXIT
812
813 system_config_master
814 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200815 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200816 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200817}
818
819function default() {
820 bootstrap $@
821}
822
823
824##########################################
825[[ "$0" != "$BASH_SOURCE" ]] || {
826# unless file is being sourced
827 default $@
828}