blob: 6a867ec476f335809045872e3352b27bcf383d1f [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
Petr Michalecb3120042018-03-17 11:17:32 +01009# NOTE: This script is collection of shared functions to automate bootstrap of "salted" CI environments.
10# Its not intended to be used in PRODUCTION unless you know what you are doing.
11# You have been warned!
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020012
13# Source specific env vars.
14# shopt -u dotglob
15export RECLASS_ROOT=${RECLASS_ROOT:-/srv/salt/reclass}
16function source_local_envs() {
Petr Michalecb444ef92017-08-17 13:53:14 +020017 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 +030018 for f in $(find $path -maxdepth 1 -name '*.env' 2> /dev/null); do
19 echo "Sourcing env variables from $f"
20 source $f
21 done
22 done
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020023}
24source_local_envs
25
26##########################################
27# Set defaults env variables
28
29if [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
30 set -x
31 SALT_LOG_LEVEL="--state-verbose=true -ldebug"
32fi
33
34export MAGENTA='\033[0;95m'
35export YELLOW='\033[1;33m'
36export BLUE='\033[0;35m'
37export CYAN='\033[0;96m'
38export RED='\033[0;31m'
39export NC='\033[0m' # No Color'
40
41export LC_ALL=C
42export SALT_LOG_LEVEL="--state-verbose=false -lerror"
43export SALT_OPTS="${SALT_OPTS:- --timeout=120 --state-output=changes --retcode-passthrough --force-color $SALT_LOG_LEVEL }"
44export SALT_STATE_RETRY=${SALT_STATE_RETRY:-3}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020045
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020046# salt apt repository
47test -e /etc/lsb-release && eval $(cat /etc/lsb-release)
48which lsb_release && DISTRIB_CODENAME=${DISTRIB_CODENAME:-$(lsb_release -cs)}
49#
Dmitry Ukov95492a02017-10-23 11:34:45 +040050export APT_REPOSITORY=${APT_REPOSITORY:- deb [arch=amd64] http://apt.mirantis.com/${DISTRIB_CODENAME} ${DISTRIB_REVISION:-stable} salt}
chnyda30c07a62017-09-20 14:28:02 +020051export APT_REPOSITORY_GPG=${APT_REPOSITORY_GPG:-http://apt.mirantis.com/public.gpg}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020052# reclass
53export RECLASS_ADDRESS=${RECLASS_ADDRESS:-https://github.com/salt-formulas/openstack-salt.git} # https/git
ibumarskovf72e05e2017-11-29 14:37:48 +030054export RECLASS_BRANCH=${RECLASS_BRANCH:-master}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020055
56# formula
57export FORMULAS_BASE=${FORMULAS_BASE:-https://github.com/salt-formulas}
58export FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas}
59export FORMULAS_BRANCH=${FORMULAS_BRANCH:-master}
60export FORMULAS_SOURCE=${FORMULAS_SOURCE:-pkg} # pkg/git
Petr Michalec515356f2017-11-16 11:20:20 +010061# Essential set of formulas (known to by used on cfg01 node for most setups during bootsrap)
62# 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 +010063FORMULAS_SALT_MASTER=${FORMULAS_SALT_MASTER:- $EXTRA_FORMULAS}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020064# minimal set of formulas for salt-master bootstrap
Petr Michalec515356f2017-11-16 11:20:20 +010065declare -a FORMULAS_SALT_MASTER=(reclass salt git openssh linux $(echo $FORMULAS_SALT_MASTER))
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020066export FORMULAS_SALT_MASTER
67
68# system / host
Petr Michalecc749e862017-09-06 21:10:10 +020069export HOSTNAME=${HOSTNAME:-`hostname -s`}
70export HOSTNAME=${HOSTNAME//.*/}
71export DOMAIN=${DOMAIN:-`hostname -d`}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020072export DOMAIN=${DOMAIN:-bootstrap.local}
73
74# salt
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020075export MINION_ID=${MINION_ID:-${HOSTNAME}.${DOMAIN}}
Petr Michalec2e3a4992017-08-17 14:20:43 +020076export MASTER_HOSTNAME=${MASTER_HOSTNAME:-${HOSTNAME}.${DOMAIN}}
azvyagintseve3f6f402018-08-23 17:21:49 +030077# Ignore state.apply salt.master.env error
78export SA_IGNORE_ERROR_SALT_MASTER_ENV=${SA_IGNORE_ERROR_SALT_MASTER_ENV:-False}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020079
80# saltstack
Petr Michalec33c04892017-09-08 15:10:30 +020081BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True}
alexz0adc2712018-02-11 17:33:52 +010082BOOTSTRAP_SALTSTACK_COM=${BOOTSTRAP_SALTSTACK_COM:-"https://bootstrap.saltstack.com"}
Martin Polreich16673dd2018-04-20 12:08:56 +020083BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION:- stable 2017.7 }
Jakub Josef0b611742018-01-09 17:02:01 +010084BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION//latest*/stable}
alexz0adc2712018-02-11 17:33:52 +010085# TODO add 'r' option by default
86# Currently, without 'r' option, upstream saltstack repos will be used. Otherwise
87# local one should be used, from http://apt.mirantis.com/ or whatever.
Petr Michalec33c04892017-09-08 15:10:30 +020088BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX $BOOTSTRAP_SALTSTACK_VERSION }
Petr Michalece11cf612017-09-08 18:21:48 +020089SALT_SOURCE=${SALT_SOURCE:-pkg}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020090
Petr Michalecb3120042018-03-17 11:17:32 +010091# SECURITY
Petr Michalecca2471f2018-03-17 11:17:32 +010092SSH_STRICTHOSTKEYCHECKING=no
Petr Michalecb3120042018-03-17 11:17:32 +010093
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020094# environment
95if [ "$FORMULAS_SOURCE" == "git" ]; then
96 SALT_ENV=${SALT_ENV:-dev}
97elif [ "$FORMULAS_SOURCE" == "pkg" ]; then
98 SALT_ENV=${SALT_ENV:-prd}
99fi
Petr Michalec05b52452017-09-08 14:26:59 +0200100eval "$(grep -h '=' /etc/*release 2> /dev/null)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200101PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z)
102case $PLATFORM_FAMILY in
103 debian )
104 PKGTOOL="$SUDO apt-get"
105 test ${VERSION_ID//\.*/} -ge 16 && {
106 SVCTOOL=service
107 } || { SVCTOOL=service
108 }
109 ;;
110 rhel )
111 PKGTOOL="$SUDO yum"
112 test ${VERSION_ID//\.*/} -ge 7 && {
113 SVCTOOL=systemctl
114 } || { SVCTOOL=service
115 }
116 ;;
117esac
118
119export PLATFORM_FAMILY
120export PKGTOOL
121export SVCTOOL
122
123##########################################
124# FUNCTIONS
125
126log_info() {
127 echo -e "${YELLOW}[INFO] $* ${NC}"
128}
129
130log_warn() {
131 echo -e "${MAGENTA}[WARN] $* ${NC}"
132}
133
Petr Michalec24b30e82017-08-21 10:59:18 +0200134log_debug() {
135 echo -e "${CYAN}[WARN] $* ${NC}"
136}
137
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200138log_err() {
139 echo -e "${RED}[ERROR] $* ${NC}" >&2
140}
141
142configure_pkg_repo()
143{
144
145 case $PLATFORM_FAMILY in
146 debian)
147 if [ -n "$APT_REPOSITORY_PPA" ]; then
148 which add-apt-repository || $SUDO apt-get install -y software-properties-common
149 $SUDO add-apt-repository -y ppa:${APT_REPOSITORY_PPA}
150 else
Mykyta Karpinfbcb5482017-10-04 14:15:25 +0300151 echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/mcp_salt.list >/dev/null
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200152 curl -sL $APT_REPOSITORY_GPG | $SUDO apt-key add -
153 fi
154 $SUDO apt-get clean
155 $SUDO apt-get update
156 ;;
157 rhel)
158 $SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm
159 $SUDO yum clean all
160 ;;
161 esac
162
163}
164
165_atexit() {
166 RETVAL=$?
167 trap true INT TERM EXIT
168
169 if [ $RETVAL -ne 0 ]; then
170 log_err "Execution failed"
171 else
172 log_info "Execution successful"
173 fi
174 return $RETVAL
175}
176
177retry() {
178 local tries
179 if [[ $1 =~ ^[0-9]+$ ]]; then
180 tries=$1; shift
181 else
182 tries=3
183 fi
Petr Michalec262e3ad2017-10-06 13:41:58 +0200184 ret=1
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200185 for i in $(seq 1 $tries); do
Petr Michalec262e3ad2017-10-06 13:41:58 +0200186 "$@" && return $? || ret=$?
187 sleep $i
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200188 done
Petr Michalec9fec8b32017-10-05 14:46:42 +0200189 return $ret
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200190}
191
192function clone_reclass() {
Petr Michaleca361d4e2017-09-08 11:38:16 +0200193 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200194 # No reclass at all, clone from given address
alexz0adc2712018-02-11 17:33:52 +0100195 # In case, non-github repo
196 _tmp_host=$(echo ${RECLASS_ADDRESS} | awk -F/ '{print $3}')
197 ssh-keyscan -T 1 -H ${_tmp_host} >> ~/.ssh/known_hosts || true
198 # But, awk is not perfect solution, so lets make double-check for github
199 ssh-keyscan -T 1 -H github.com >> ~/.ssh/known_hosts || true
ibumarskovf72e05e2017-11-29 14:37:48 +0300200 if echo ${RECLASS_BRANCH} | egrep -q "^refs"; then
201 git clone ${RECLASS_ADDRESS} ${RECLASS_ROOT}
202 cd ${RECLASS_ROOT}
203 git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH} && git checkout FETCH_HEAD
204 export RECLASS_REVISION=$(git rev-parse HEAD)
205 cd -
206 else
207 git clone -b ${RECLASS_BRANCH} ${RECLASS_ADDRESS} ${RECLASS_ROOT};
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200208 fi;
209 fi;
Petr Michalec679f15b2017-09-18 16:16:29 +0200210 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michaleca361d4e2017-09-08 11:38:16 +0200211 log_err "Reclass ${RECLASS_ROOT} is not fetched locally;"
212 ls -Rla ${RECLASS_ROOT}
213 exit 1
214 fi;
215 $SUDO mkdir -p $RECLASS_ROOT/classes/service
216 $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200217}
218
219
220##########################################
221# Main calls
222
Petr Michalecca2471f2018-03-17 11:17:32 +0100223ci_config_ssh() {
224
225 # for CI current user
Petr Michalecb3120042018-03-17 11:17:32 +0100226 conf=~/.ssh/config
227 mkdir -p $(dirname $conf)
228 touch $conf
Petr Michalecca2471f2018-03-17 11:17:32 +0100229 echo -e "Host *\n\tStrictHostKeyChecking $SSH_STRICTHOSTKEYCHECKING\n" | tee -a $conf >/dev/null
Petr Michalecb3120042018-03-17 11:17:32 +0100230 touch ~/.ssh/known_hosts
Petr Michalecca2471f2018-03-17 11:17:32 +0100231
Petr Michalecda014202017-08-18 11:26:07 +0200232 if ! grep github.com ~/.ssh/known_hosts; then
233 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
Petr Michalecb3120042018-03-17 11:17:32 +0100234 ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts || true
Petr Michalecda014202017-08-18 11:26:07 +0200235 fi
Petr Michalecb3120042018-03-17 11:17:32 +0100236
237 # for root
238 #conf=/root/.ssh/config
239 #$SUDO mkdir -p $(dirname $conf)
240 #$SUDO touch $conf
241 #if ! $SSH_STRICTHOSTKEYCHECKING; then
242 # echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee -a $conf >/dev/null
243 #fi
Petr Michalecda014202017-08-18 11:26:07 +0200244}
245
Petr Michalecca2471f2018-03-17 11:17:32 +0100246# DEPRECATED
247system_config_ssh_conf() {
248 # for backward compatibility
249 ci_config_ssh
Petr Michalecda014202017-08-18 11:26:07 +0200250}
251
252system_config_salt_modules_prereq() {
253 # salt-formulas custom modules dependencies, etc:
Petr Ruzickade2cd3b2017-12-07 14:41:20 +0100254 $SUDO $PKGTOOL install -y apt-transport-https curl git iproute2 openssh-client python-psutil python-apt python-m2crypto python-oauth python-pip sudo &>/dev/null
Petr Michalecda014202017-08-18 11:26:07 +0200255}
256
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200257system_config_minion() {
258 log_info "System configuration salt minion"
259}
260
261system_config_master() {
262 log_info "System configuration salt master"
263
Petr Michalecda014202017-08-18 11:26:07 +0200264 system_config_salt_modules_prereq
Petr Michalecca2471f2018-03-17 11:17:32 +0100265 ci_config_ssh
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200266
Petr Michalecda014202017-08-18 11:26:07 +0200267 if ! grep '127.0.1.2.*salt' /etc/hosts; then
268 echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null
269 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200270}
271
272configure_salt_master()
273{
274
275 echo "Configuring salt-master ..."
276
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200277 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec29d3ea32017-10-31 17:22:36 +0100278 read -d '' IGNORE_CLASS_NOTFOUND <<-EOF
279 ignore_class_notfound: True
280 ignore_class_regexp:
281 - 'service.*'
282EOF
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200283 fi
284
Petr Michalecdd1a0472017-09-06 19:27:24 +0200285 # to force alternative reclass module path
Petr Michalecd9d0a322017-09-06 19:38:32 +0200286 if [ -n "$RECLASS_SOURCE_PATH" ]; then
287 RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}"
288 else
289 RECLASS_SOURCE_PATH=""
290 fi
Petr Michalecdd1a0472017-09-06 19:27:24 +0200291
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200292 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
293 cat <<-EOF > /etc/salt/master.d/master.conf
294 file_roots:
295 base:
296 - /usr/share/salt-formulas/env
297 prd:
298 - /srv/salt/env/prd
299 dev:
300 - /srv/salt/env/dev
301 pillar_opts: False
302 open_mode: True
303 reclass: &reclass
304 storage_type: yaml_fs
305 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200306 ${IGNORE_CLASS_NOTFOUND}
Petr Michalecdd1a0472017-09-06 19:27:24 +0200307 ${RECLASS_SOURCE_PATH}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200308 ext_pillar:
309 - reclass: *reclass
310 master_tops:
311 reclass: *reclass
312EOF
313
314 echo "Configuring reclass ..."
315
316 [ ! -d /etc/reclass ] && mkdir /etc/reclass
317 cat <<-EOF > /etc/reclass/reclass-config.yml
318 storage_type: yaml_fs
319 pretty_print: True
320 output: yaml
321 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200322EOF
323
324 clone_reclass
325 # override some envs from cluster level *.env, use with care
326 source_local_envs
327
328 cd ${RECLASS_ROOT}
329 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
330 # Possibly subrepo checkout needed
331 git submodule update --init --recursive
332 fi
333
Petr Michalec212cc6a2017-08-17 21:39:40 +0200334 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec51ece842017-09-06 20:44:15 +0200335 rm -rvf ${RECLASS_ROOT}/nodes/_generated/*
336
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200337 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200338 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200339 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
Petr Michalec24b30e82017-08-21 10:59:18 +0200340 log_warn "Salt Master node specification has not been found in model."
341 log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200342 cat <<-EOF > ${CONFIG}
343 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200344 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200345 parameters:
346 _param:
Petr Michaleca7de6df2017-08-31 12:16:57 +0200347 salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200348 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200349 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
ibumarskovf72e05e2017-11-29 14:37:48 +0300350 reclass_data_revision: ${RECLASS_REVISION:-$RECLASS_BRANCH}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200351 reclass_data_repository: "$RECLASS_ADDRESS"
Petr Michaleca7de6df2017-08-31 12:16:57 +0200352 reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200353 linux_system_codename: ${DISTRIB_CODENAME}
354 cluster_name: ${CLUSTER_NAME}
355 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200356 linux:
357 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200358 name: ${HOSTNAME:-cfg01}
359 domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200360 # ########
361EOF
362
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200363 cat <<-EOF >> ${CONFIG}
364 salt:
365 master:
366 accept_policy: open_mode
367 source:
368 engine: $SALT_SOURCE
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200369 minion:
370 source:
371 engine: $SALT_SOURCE
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200372 # ########
373 # vim: ft=yaml sw=2 ts=2 sts=2
374EOF
375 fi
Petr Michalec24b30e82017-08-21 10:59:18 +0200376
377 log_debug "Salt Master node config yaml:"
378 log_debug "$(cat ${CONFIG})"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200379}
380
381configure_salt_minion()
382{
383 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
384 cat <<-EOF > /etc/salt/minion.d/minion.conf
Petr Michaleca7de6df2017-08-31 12:16:57 +0200385 master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200386 id: $MINION_ID
387 EOF
388}
389
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200390install_reclass()
391{
392 VERSION=${1:-$RECLASS_VERSION}
Petr Michalec372fa032017-11-15 17:36:19 +0100393 VERSION=${VERSION:-pkg}
Petr Michalec800dd072017-11-15 17:19:26 +0100394 case ${VERSION} in
395 pkg|package)
396 which reclass || $SUDO $PKGTOOL install -y reclass
rootb9e5d7c2018-03-17 12:12:44 +0000397 which reclass-salt || {
398 if [ -e /usr/share/reclass/reclass-salt ]; then
Petr Michalec6ba1c362018-03-17 15:01:51 +0100399 ln -fs /usr/share/reclass/reclass-salt /usr/bin
rootb9e5d7c2018-03-17 12:12:44 +0000400 fi
401 }
Petr Michalec800dd072017-11-15 17:19:26 +0100402 ;;
403 *)
Petr Michalec981f3cc2017-11-15 17:40:31 +0100404 log_warn "Install development version of reclass"
Petr Michalec1b37d3d2017-11-15 18:20:46 +0100405 # Note: dev/git/pip version...
406 # It replaces all local reclass versions on system
407 # Required for reclass/git features:
408 # $SUDO $PKGTOOL install -y libffi-dev libgit2-dev || true
Petr Michalec800dd072017-11-15 17:19:26 +0100409 for s in $(python -c "import site; print(' '.join(site.getsitepackages()))"); do
Petr Michalec23020832017-11-15 17:34:49 +0100410 sudo -H pip install --install-option="--prefix=" --upgrade --force-reinstall -I \
Petr Michalec800dd072017-11-15 17:19:26 +0100411 -t "$s" git+https://github.com/salt-formulas/reclass.git@${VERSION};
412 done
413 ;;
414 esac
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200415}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200416
417install_salt_master_pkg()
418{
419 echo -e "\nPreparing base OS repository ...\n"
420
421 configure_pkg_repo
422
423 echo -e "\nInstalling salt master ...\n"
424
425 case $PLATFORM_FAMILY in
426 debian)
427 $SUDO apt-get install -y git
alexz0adc2712018-02-11 17:33:52 +0100428 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200429 ;;
430 rhel)
431 yum install -y git
alexz0adc2712018-02-11 17:33:52 +0100432 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200433 ;;
434 esac
Jakub Josef0b611742018-01-09 17:02:01 +0100435
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200436 configure_salt_master
437
438 echo -e "\nRestarting services ...\n"
439 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
440 $SVCTOOL salt-master restart
441}
442
443install_salt_master_pip()
444{
445 echo -e "\nPreparing base OS repository ...\n"
446
447 case $PLATFORM_FAMILY in
448 debian)
449 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200450 ;;
451 rhel)
452 $SUDO yum install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200453 ;;
454 esac
455
456 echo -e "\nInstalling salt master ...\n"
457 # TODO: replace with saltstack bootstrap script
Jakub Josef0b611742018-01-09 17:02:01 +0100458
Petr Michalec88a95a22018-01-09 17:21:08 +0100459 if [ -z ${PIP_SALT_VERSION} ] || [ "$PIP_SALT_VERSION" == "latest" ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200460 pip install salt
461 else
Petr Michalec88a95a22018-01-09 17:21:08 +0100462 pip install salt==${PIP_SALT_VERSION}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200463 fi
464
465 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
466 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
467
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200468 configure_salt_master
469
470 echo -e "\nRestarting services ...\n"
471 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
472 $SVCTOOL salt-master restart
473}
474
475
476
477install_salt_minion_pkg()
478{
479
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200480 echo -e "\nInstalling salt minion ...\n"
481
482 case $PLATFORM_FAMILY in
483 debian)
alexz0adc2712018-02-11 17:33:52 +0100484 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200485 ;;
486 rhel)
alexz0adc2712018-02-11 17:33:52 +0100487 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200488 ;;
489 esac
490
491
492 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200493 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200494}
495
496install_salt_minion_pip()
497{
498 echo -e "\nInstalling salt minion ...\n"
499
500 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
501 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
502
503 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200504 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200505}
506
507
508install_salt_formula_pkg()
509{
510 configure_pkg_repo
511
512 case $PLATFORM_FAMILY in
513 debian)
514 echo "Configuring necessary formulas ..."
515
516 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
517 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100518 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200519 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
520 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
521 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
522 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
523 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
524 exit 1
525 fi
Oleksii Grudev691d4712018-04-02 10:23:46 +0300526 #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
527 formula_service=${formula_service//-/$'_'}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200528 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
529 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
530 done
531 ;;
532 rhel)
533 # TODO
534 ;;
535 esac
536
537 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
538 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
539}
540
541install_salt_formula_git()
542{
543 echo "Configuring necessary formulas ..."
544
545 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
546 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100547 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200548 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
549 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
550 _BRANCH=${FORMULAS_BRANCH}
551 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
552 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
553 # Fallback to the master branch if the branch doesn't exist for this repository
554 _BRANCH=master
555 fi
556 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
557 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
558 exit 1
559 fi
560 } || {
561 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
562 git fetch origin/${_BRANCH} || git fetch --all
563 git checkout ${_BRANCH} && git pull || git pull;
564 cd -
565 }
566 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
567 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
568 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
569 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
570 done
571
572 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
573 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
574}
575
Petr Michalece9c84522017-12-13 17:14:08 +0100576saltservice_stop() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100577 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100578 $SUDO service salt-minion stop
579 $SUDO service salt-master stop
580 sleep ${SALT_STOPSTART_WAIT:-30}
Petr Michalec7822efe2017-12-15 16:19:32 +0100581 ${SUDO} pkill -9 salt-master && ${SUDO} rm -rf /var/run/salt/master/* || true
Petr Michalece9c84522017-12-13 17:14:08 +0100582 ${SUDO} pkill -9 salt-minion
583}
584saltservice_start() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100585 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100586 $SUDO service salt-master start
587 $SUDO service salt-minion start
588 sleep ${SALT_STOPSTART_WAIT:-30}
589}
590
591saltservice_restart() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100592 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100593 saltservice_stop
594 saltservice_start
595}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200596
597saltmaster_bootstrap() {
598
599 log_info "Salt master setup"
600 test -n "$MASTER_HOSTNAME" || exit 1
601
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200602 # override some envs from cluster level *.env, use with care
603 source_local_envs
604
Petr Michalece9c84522017-12-13 17:14:08 +0100605 saltservice_stop
606
607 clone_reclass
608
projoke3c30e7d2017-10-19 17:15:40 +0800609 SCRIPTS=$(dirname $0)
Petr Michalece9c84522017-12-13 17:14:08 +0100610
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200611 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
Adam Tengler035f24f2017-09-11 19:29:31 +0200612 export MASTER_IP=${MASTER_IP:-127.0.0.1}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200613 export MINION_ID=${MASTER_HOSTNAME}
614 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
615 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
616 fi
617 # call local "setup() master"
618 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
619 if ! setup master; then
620 #cat /tmp/salt-master-setup.log
621 log_err "salt master setup() failed."
622 exit 1
623 else
624 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
625 fi
626 }
627
Petr Michalec981f3cc2017-11-15 17:40:31 +0100628 log_info "Install reclass"
629 install_reclass ${RECLASS_VERSION/dev*/develop}
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200630
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200631 log_info "Re/starting salt services"
Petr Michalece9c84522017-12-13 17:14:08 +0100632 saltservice_restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200633}
634
635# Init salt master
636saltmaster_init() {
637
638 log_info "Runing saltmaster states"
639 test -n "$MASTER_HOSTNAME" || exit 1
640
641 set -e
642 $SUDO salt-call saltutil.sync_all >/dev/null
643
Petr Michalecda014202017-08-18 11:26:07 +0200644 # workarond isolated and not fully bootstraped environments
Petr Michalec2801acb2017-10-31 15:00:15 +0100645 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec54ee4082017-11-30 17:02:17 +0100646 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 +0100647 fi
Petr Michalec8ce8a652017-11-30 15:39:20 +0100648 PILLAR='{'${SALT_MASTER_PILLAR}' "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalecda014202017-08-18 11:26:07 +0200649
Petr Michalec8b92d552017-11-29 12:28:50 +0100650 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100651 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100652 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
azvyagintseve3f6f402018-08-23 17:21:49 +0300653 if [[ $SA_IGNORE_ERROR_SALT_MASTER_ENV =~ ^(False|false|0|no)$ ]]; then
654 exit 1
655 fi
656
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200657 fi
658
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200659 # Revert temporary SaltMaster minimal configuration, if any
660 pushd $RECLASS_ROOT
661 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
Petr Michalec87778fc2017-11-02 10:13:25 +0100662 PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200663 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200664 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200665 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
666 git checkout -- $RECLASS_ROOT/nodes || true
667 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
Petr Michalec7edf8322017-11-30 12:01:09 +0100668 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100669 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100670 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
azvyagintseve3f6f402018-08-23 17:21:49 +0300671 if [[ $SA_IGNORE_ERROR_SALT_MASTER_ENV =~ ^(False|false|0|no)$ ]]; then
672 exit 1
673 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200674 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200675 fi
676 popd
677
Petr Michalecda014202017-08-18 11:26:07 +0200678 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
679 log_info "State: salt.master.service"
Jakub Josef32563412018-01-17 17:55:05 +0100680 # ensure salt master is started
681 saltservice_start
Petr Michalec9fec8b32017-10-05 14:46:42 +0200682 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalec4f043772017-12-19 15:40:54 +0100683 saltservice_start
Petr Michalecda014202017-08-18 11:26:07 +0200684
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200685 log_info "State: salt.master.storage.node"
686 set +e
Petr Michalec7edf8322017-11-30 12:01:09 +0100687 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
688 # FIXME: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec9fec8b32017-10-05 14:46:42 +0200689 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200690 ret=$?
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200691
Petr Michalecdd646db2017-12-18 15:56:18 +0100692 set -e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200693 if [[ $ret -eq 2 ]]; then
694 log_err "State reclass.storage.node failed with exit code 2 but continuing."
695 elif [[ $ret -ne 0 ]]; then
696 log_err "State reclass.storage.node failed with exit code $ret"
697 exit 1
698 fi
699
Petr Michalecdd646db2017-12-18 15:56:18 +0100700 set +e
701 log_info "Updating minion.conf -> master: localhost"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200702 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
Petr Michalecdd646db2017-12-18 15:56:18 +0100703
704 log_info "Re/starting salt services" # in order to load new deployed configuration from model
Petr Michalece9c84522017-12-13 17:14:08 +0100705 saltservice_restart
Petr Michalecdd646db2017-12-18 15:56:18 +0100706
707 log_info "Salt Util sync all"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200708 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
709
710 verify_salt_master
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200711
712}
713
Petr Michalec71e32682018-03-22 08:34:34 +0100714## CI Workarounds
715
716function mockup_node_registration() {
717
718 # for dynamic nodes registrations (require github.com/salt-formulas/salt-formula-reclass)
719
720 source /etc/os-release
721 os_codename=$VERSION_CODENAME
722 domain=$(hostname -d)
723 master_ip=$(hostname -I | awk '{print $1}')
724 fake_ip_preffix=$(echo $master_ip | awk -F. '{print $1"."$2}')
725 fake_ip_base=10
726
727 # SHOULD BE ALREADY RUN AS A PART OF BOOTSTRAP
728 # sync, in order to load custom modules
729 #salt-call saltutil.sync_all
730
731 # SHOULD BE ALREADY RUN AS A PART OF BOOTSTRAP
732 #PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
733 #salt-call state.apply reclass.storage.node pillar="$PILLAR" > /dev/null 2>/dev/null || true
734
735 # rotate over dynamic hosts
736 for host_idx in {01..02};do
737 for host in `salt-call pillar.items reclass:storage |grep '<<node_hostname>>' | awk -F_ '{print $NF}' | sed 's/[0-9]*//g' | egrep -v cfg | sort -u`; do
738 hostname=${host}${host_idx}
739 fake_ip_base=$(($fake_ip_base + 1))
740
741 node_network01_ip="$fake_ip_preffix.11.$fake_ip_base"
742 node_network02_ip="$fake_ip_preffix.12.$fake_ip_base"
743 node_network03_ip="$fake_ip_preffix.13.$fake_ip_base"
744 node_network04_ip="$fake_ip_preffix.14.$fake_ip_base"
745 node_network05_ip="$fake_ip_preffix.15.$fake_ip_base"
746 node_network01_iface=$(ls /sys/class/net/ | grep -v lo | sort | head -n1)
747 node_network02_iface="eth1"
748 node_network03_iface="eth2"
749 node_network04_iface="eth3"
750 node_network05_iface="eth4"
751
752 declare -A vars
753 vars=(
754 ["node_master_ip"]=
755 ["node_os"]=${os_codename}
756 ["node_deploy_ip"]=${node_network01_ip}
757 ["node_deploy_iface"]=${node_network01_iface}
758 ["node_control_ip"]=${node_network02_ip}
759 ["node_control_iface"]=${node_network02_iface}
760 ["node_tenant_ip"]=${node_network03_ip}
761 ["node_tenant_iface"]=${node_network03_iface}
762 ["node_external_ip"]=${node_network04_ip}
763 ["node_external_iface"]=${node_network04_iface}
764 ["node_baremetal_ip"]=${node_network05_ip}
765 ["node_baremetal_iface"]=${node_network05_iface}
766 ["node_domain"]=$domain
767 ["node_cluster"]=$(hostname -d |awk -F. '{print $1}')
768 ["node_hostname"]=$hostname
769 )
770
771 data=""; i=0
772 for key in "${!vars[@]}"; do
773 data+="\"${key}\": \"${vars[${key}]}\""
774 i=$(($i+1))
775 if [ $i -lt ${#vars[@]} ]; then
776 data+=", "
777 fi
778 done
779 echo "Classifying node $hostname"
780 NODECR='"node_name": "'${hostname}.${domain}'", "node_data": {'$data'}'
781 PILLAR='{'${NODECR}', "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
782 salt-call state.apply reclass.reactor_sls.node_register pillar="$PILLAR" #-l info
783 #salt-call event.send "reclass/minion/classify" "{$data}"
784 done
785 done
786
787}
788
789## VERIFY
790
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200791
792function verify_salt_master() {
793 set -e
794
795 log_info "Verify Salt master"
796 test -n "$MASTER_HOSTNAME" || exit 1
797
798 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200799 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
800 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200801 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
802 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
803 $SUDO salt-call --no-color grains.items
804 $SUDO salt-call --no-color pillar.data
805 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200806 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec6e40efc2018-03-22 09:47:47 +0100807 if ! $SUDO python -m reclass.cli --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200808 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
809 exit 1
810 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100811 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200812}
813
814function verify_salt_minion() {
815 set -e
816 node=$1
817 log_info "Verifying ${node}"
818 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
819 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
820 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
821 fi
Petr Michalec6e40efc2018-03-22 09:47:47 +0100822 if ! $SUDO python -m reclass.cli --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200823 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
824 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
825 exit 1
826 fi
827 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100828 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200829}
830
831function verify_salt_minions() {
832 #set -e
Petr Michalec679f15b2017-09-18 16:16:29 +0200833 NODES=$(find $RECLASS_ROOT/nodes/_generated/ -name "*.yml" | grep -v "cfg")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200834 log_info "Verifying minions: $(echo ${NODES}|xargs)"
835
836 # Parallel
837 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
838 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
839
840 function filterFails() {
841 grep -v '/grains' | tee -a $1 | tail -n20
842 }
843
844 log_info "Verify nodes"
845 passed=0
846 for node in ${NODES}; do
847 node=$(basename $node .yml)
848
849 # filter first in cluster.. ctl-01, mon-01, etc..
850 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
851 verify_salt_minion ${node} || continue
852 else
853 echo Skipped $node.
854 fi
855 passed=$(($passed+1))
856 done
857 # fail on failures
858 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
859 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
860 test ! $passed -lt $total || {
861 tail -n50 /tmp/*.pillar_verify
862 return 1
863 }
Petr Michalec7edf8322017-11-30 12:01:09 +0100864 #set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200865}
866
867
868
869##########################################
870# To install salt master/minon
871
872function install() {
873 setup $@
874}
875
876function setup() {
877 # CLI
878 while [ x"$1" != x"" ]; do
879 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
880
881 case $1 in
882 master )
883 install_salt_master_$SALT_SOURCE
884 install_salt_minion_$SALT_SOURCE
885 install_salt_formula_$FORMULAS_SOURCE
886 ;;
887 minion )
888 install_salt_minion_$SALT_SOURCE
889 ;;
890 esac
891 shift
892 done
893 echo DONE
894}
895
896function bootstrap() {
897 log_info "Bootstrap & verification of SaltMaster and configured minions."
898 trap _atexit INT TERM EXIT
899
900 system_config_master
901 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200902 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200903 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200904}
905
906function default() {
907 bootstrap $@
908}
909
910
911##########################################
912[[ "$0" != "$BASH_SOURCE" ]] || {
913# unless file is being sourced
914 default $@
915}