blob: 91ab96a0d27f839e08cc296158235e0f176415f6 [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
azvyagintseve93f8372018-09-12 16:50:58 +030013__ScriptVersion="2018.09.12"
14__ScriptName="bootstrap.sh"
15__ScriptFullName="$0"
16__ScriptArgs="$*"
17
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020018# Source specific env vars.
19# shopt -u dotglob
20export RECLASS_ROOT=${RECLASS_ROOT:-/srv/salt/reclass}
21function source_local_envs() {
Petr Michalecb444ef92017-08-17 13:53:14 +020022 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 +030023 for f in $(find $path -maxdepth 1 -name '*.env' 2> /dev/null); do
24 echo "Sourcing env variables from $f"
25 source $f
26 done
27 done
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020028}
29source_local_envs
30
31##########################################
32# Set defaults env variables
33
34if [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
35 set -x
36 SALT_LOG_LEVEL="--state-verbose=true -ldebug"
37fi
38
39export MAGENTA='\033[0;95m'
40export YELLOW='\033[1;33m'
41export BLUE='\033[0;35m'
42export CYAN='\033[0;96m'
43export RED='\033[0;31m'
44export NC='\033[0m' # No Color'
45
46export LC_ALL=C
47export SALT_LOG_LEVEL="--state-verbose=false -lerror"
48export SALT_OPTS="${SALT_OPTS:- --timeout=120 --state-output=changes --retcode-passthrough --force-color $SALT_LOG_LEVEL }"
49export SALT_STATE_RETRY=${SALT_STATE_RETRY:-3}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020050
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020051# salt apt repository
52test -e /etc/lsb-release && eval $(cat /etc/lsb-release)
53which lsb_release && DISTRIB_CODENAME=${DISTRIB_CODENAME:-$(lsb_release -cs)}
54#
Dmitry Ukov95492a02017-10-23 11:34:45 +040055export APT_REPOSITORY=${APT_REPOSITORY:- deb [arch=amd64] http://apt.mirantis.com/${DISTRIB_CODENAME} ${DISTRIB_REVISION:-stable} salt}
chnyda30c07a62017-09-20 14:28:02 +020056export APT_REPOSITORY_GPG=${APT_REPOSITORY_GPG:-http://apt.mirantis.com/public.gpg}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020057# reclass
58export RECLASS_ADDRESS=${RECLASS_ADDRESS:-https://github.com/salt-formulas/openstack-salt.git} # https/git
ibumarskovf72e05e2017-11-29 14:37:48 +030059export RECLASS_BRANCH=${RECLASS_BRANCH:-master}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020060
61# formula
62export FORMULAS_BASE=${FORMULAS_BASE:-https://github.com/salt-formulas}
63export FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas}
64export FORMULAS_BRANCH=${FORMULAS_BRANCH:-master}
65export FORMULAS_SOURCE=${FORMULAS_SOURCE:-pkg} # pkg/git
Petr Michalec515356f2017-11-16 11:20:20 +010066# Essential set of formulas (known to by used on cfg01 node for most setups during bootsrap)
67# 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}
azvyagintseve93f8372018-09-12 16:50:58 +030068FORMULAS_SALT_MASTER=${FORMULAS_SALT_MASTER:- "$EXTRA_FORMULAS"}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020069# minimal set of formulas for salt-master bootstrap
azvyagintseve93f8372018-09-12 16:50:58 +030070declare -a FORMULAS_SALT_MASTER=(reclass salt git openssh linux $(echo "${FORMULAS_SALT_MASTER}"))
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020071export FORMULAS_SALT_MASTER
72
73# system / host
Petr Michalecc749e862017-09-06 21:10:10 +020074export HOSTNAME=${HOSTNAME:-`hostname -s`}
75export HOSTNAME=${HOSTNAME//.*/}
76export DOMAIN=${DOMAIN:-`hostname -d`}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020077export DOMAIN=${DOMAIN:-bootstrap.local}
78
79# salt
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020080export MINION_ID=${MINION_ID:-${HOSTNAME}.${DOMAIN}}
Petr Michalec2e3a4992017-08-17 14:20:43 +020081export MASTER_HOSTNAME=${MASTER_HOSTNAME:-${HOSTNAME}.${DOMAIN}}
azvyagintseve3f6f402018-08-23 17:21:49 +030082# Ignore state.apply salt.master.env error
83export SA_IGNORE_ERROR_SALT_MASTER_ENV=${SA_IGNORE_ERROR_SALT_MASTER_ENV:-False}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020084
85# saltstack
Petr Michalec33c04892017-09-08 15:10:30 +020086BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True}
alexz0adc2712018-02-11 17:33:52 +010087BOOTSTRAP_SALTSTACK_COM=${BOOTSTRAP_SALTSTACK_COM:-"https://bootstrap.saltstack.com"}
Martin Polreich16673dd2018-04-20 12:08:56 +020088BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION:- stable 2017.7 }
Jakub Josef0b611742018-01-09 17:02:01 +010089BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION//latest*/stable}
alexz0adc2712018-02-11 17:33:52 +010090# TODO add 'r' option by default
91# Currently, without 'r' option, upstream saltstack repos will be used. Otherwise
92# local one should be used, from http://apt.mirantis.com/ or whatever.
Petr Michalec33c04892017-09-08 15:10:30 +020093BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX $BOOTSTRAP_SALTSTACK_VERSION }
Petr Michalece11cf612017-09-08 18:21:48 +020094SALT_SOURCE=${SALT_SOURCE:-pkg}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020095
Petr Michalecb3120042018-03-17 11:17:32 +010096# SECURITY
Petr Michalecca2471f2018-03-17 11:17:32 +010097SSH_STRICTHOSTKEYCHECKING=no
Petr Michalecb3120042018-03-17 11:17:32 +010098
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020099# environment
100if [ "$FORMULAS_SOURCE" == "git" ]; then
101 SALT_ENV=${SALT_ENV:-dev}
102elif [ "$FORMULAS_SOURCE" == "pkg" ]; then
103 SALT_ENV=${SALT_ENV:-prd}
104fi
Petr Michalec05b52452017-09-08 14:26:59 +0200105eval "$(grep -h '=' /etc/*release 2> /dev/null)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200106PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z)
107case $PLATFORM_FAMILY in
108 debian )
109 PKGTOOL="$SUDO apt-get"
110 test ${VERSION_ID//\.*/} -ge 16 && {
111 SVCTOOL=service
112 } || { SVCTOOL=service
113 }
114 ;;
115 rhel )
116 PKGTOOL="$SUDO yum"
117 test ${VERSION_ID//\.*/} -ge 7 && {
118 SVCTOOL=systemctl
119 } || { SVCTOOL=service
120 }
121 ;;
122esac
123
124export PLATFORM_FAMILY
125export PKGTOOL
126export SVCTOOL
127
128##########################################
129# FUNCTIONS
130
131log_info() {
132 echo -e "${YELLOW}[INFO] $* ${NC}"
133}
134
135log_warn() {
136 echo -e "${MAGENTA}[WARN] $* ${NC}"
137}
138
Petr Michalec24b30e82017-08-21 10:59:18 +0200139log_debug() {
140 echo -e "${CYAN}[WARN] $* ${NC}"
141}
142
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200143log_err() {
144 echo -e "${RED}[ERROR] $* ${NC}" >&2
145}
146
147configure_pkg_repo()
148{
149
150 case $PLATFORM_FAMILY in
151 debian)
152 if [ -n "$APT_REPOSITORY_PPA" ]; then
153 which add-apt-repository || $SUDO apt-get install -y software-properties-common
154 $SUDO add-apt-repository -y ppa:${APT_REPOSITORY_PPA}
155 else
Mykyta Karpinfbcb5482017-10-04 14:15:25 +0300156 echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/mcp_salt.list >/dev/null
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200157 curl -sL $APT_REPOSITORY_GPG | $SUDO apt-key add -
158 fi
159 $SUDO apt-get clean
160 $SUDO apt-get update
161 ;;
162 rhel)
163 $SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm
164 $SUDO yum clean all
165 ;;
166 esac
167
168}
169
170_atexit() {
171 RETVAL=$?
172 trap true INT TERM EXIT
173
174 if [ $RETVAL -ne 0 ]; then
175 log_err "Execution failed"
176 else
177 log_info "Execution successful"
178 fi
179 return $RETVAL
180}
181
182retry() {
183 local tries
184 if [[ $1 =~ ^[0-9]+$ ]]; then
185 tries=$1; shift
186 else
187 tries=3
188 fi
Petr Michalec262e3ad2017-10-06 13:41:58 +0200189 ret=1
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200190 for i in $(seq 1 $tries); do
Petr Michalec262e3ad2017-10-06 13:41:58 +0200191 "$@" && return $? || ret=$?
192 sleep $i
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200193 done
Petr Michalec9fec8b32017-10-05 14:46:42 +0200194 return $ret
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200195}
196
197function clone_reclass() {
Petr Michaleca361d4e2017-09-08 11:38:16 +0200198 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200199 # No reclass at all, clone from given address
alexz0adc2712018-02-11 17:33:52 +0100200 # In case, non-github repo
201 _tmp_host=$(echo ${RECLASS_ADDRESS} | awk -F/ '{print $3}')
202 ssh-keyscan -T 1 -H ${_tmp_host} >> ~/.ssh/known_hosts || true
203 # But, awk is not perfect solution, so lets make double-check for github
204 ssh-keyscan -T 1 -H github.com >> ~/.ssh/known_hosts || true
ibumarskovf72e05e2017-11-29 14:37:48 +0300205 if echo ${RECLASS_BRANCH} | egrep -q "^refs"; then
206 git clone ${RECLASS_ADDRESS} ${RECLASS_ROOT}
207 cd ${RECLASS_ROOT}
208 git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH} && git checkout FETCH_HEAD
209 export RECLASS_REVISION=$(git rev-parse HEAD)
210 cd -
211 else
212 git clone -b ${RECLASS_BRANCH} ${RECLASS_ADDRESS} ${RECLASS_ROOT};
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200213 fi;
214 fi;
Petr Michalec679f15b2017-09-18 16:16:29 +0200215 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michaleca361d4e2017-09-08 11:38:16 +0200216 log_err "Reclass ${RECLASS_ROOT} is not fetched locally;"
217 ls -Rla ${RECLASS_ROOT}
218 exit 1
219 fi;
220 $SUDO mkdir -p $RECLASS_ROOT/classes/service
221 $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200222}
223
224
225##########################################
226# Main calls
227
Petr Michalecca2471f2018-03-17 11:17:32 +0100228ci_config_ssh() {
229
230 # for CI current user
Petr Michalecb3120042018-03-17 11:17:32 +0100231 conf=~/.ssh/config
232 mkdir -p $(dirname $conf)
233 touch $conf
Petr Michalecca2471f2018-03-17 11:17:32 +0100234 echo -e "Host *\n\tStrictHostKeyChecking $SSH_STRICTHOSTKEYCHECKING\n" | tee -a $conf >/dev/null
Petr Michalecb3120042018-03-17 11:17:32 +0100235 touch ~/.ssh/known_hosts
Petr Michalecca2471f2018-03-17 11:17:32 +0100236
Petr Michalecda014202017-08-18 11:26:07 +0200237 if ! grep github.com ~/.ssh/known_hosts; then
238 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
Petr Michalecb3120042018-03-17 11:17:32 +0100239 ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts || true
Petr Michalecda014202017-08-18 11:26:07 +0200240 fi
Petr Michalecb3120042018-03-17 11:17:32 +0100241
242 # for root
243 #conf=/root/.ssh/config
244 #$SUDO mkdir -p $(dirname $conf)
245 #$SUDO touch $conf
246 #if ! $SSH_STRICTHOSTKEYCHECKING; then
247 # echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee -a $conf >/dev/null
248 #fi
Petr Michalecda014202017-08-18 11:26:07 +0200249}
250
Petr Michalecca2471f2018-03-17 11:17:32 +0100251# DEPRECATED
252system_config_ssh_conf() {
253 # for backward compatibility
254 ci_config_ssh
Petr Michalecda014202017-08-18 11:26:07 +0200255}
256
257system_config_salt_modules_prereq() {
258 # salt-formulas custom modules dependencies, etc:
Petr Ruzickade2cd3b2017-12-07 14:41:20 +0100259 $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 +0200260}
261
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200262system_config_minion() {
263 log_info "System configuration salt minion"
264}
265
266system_config_master() {
267 log_info "System configuration salt master"
268
Petr Michalecda014202017-08-18 11:26:07 +0200269 system_config_salt_modules_prereq
Petr Michalecca2471f2018-03-17 11:17:32 +0100270 ci_config_ssh
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200271
Petr Michalecda014202017-08-18 11:26:07 +0200272 if ! grep '127.0.1.2.*salt' /etc/hosts; then
273 echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null
274 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200275}
276
277configure_salt_master()
278{
279
280 echo "Configuring salt-master ..."
281
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200282 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec29d3ea32017-10-31 17:22:36 +0100283 read -d '' IGNORE_CLASS_NOTFOUND <<-EOF
284 ignore_class_notfound: True
285 ignore_class_regexp:
286 - 'service.*'
287EOF
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200288 fi
289
Petr Michalecdd1a0472017-09-06 19:27:24 +0200290 # to force alternative reclass module path
Petr Michalecd9d0a322017-09-06 19:38:32 +0200291 if [ -n "$RECLASS_SOURCE_PATH" ]; then
292 RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}"
293 else
294 RECLASS_SOURCE_PATH=""
295 fi
Petr Michalecdd1a0472017-09-06 19:27:24 +0200296
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200297 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
298 cat <<-EOF > /etc/salt/master.d/master.conf
299 file_roots:
300 base:
301 - /usr/share/salt-formulas/env
302 prd:
303 - /srv/salt/env/prd
304 dev:
305 - /srv/salt/env/dev
306 pillar_opts: False
307 open_mode: True
308 reclass: &reclass
309 storage_type: yaml_fs
310 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200311 ${IGNORE_CLASS_NOTFOUND}
Petr Michalecdd1a0472017-09-06 19:27:24 +0200312 ${RECLASS_SOURCE_PATH}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200313 ext_pillar:
314 - reclass: *reclass
315 master_tops:
316 reclass: *reclass
317EOF
318
319 echo "Configuring reclass ..."
320
321 [ ! -d /etc/reclass ] && mkdir /etc/reclass
322 cat <<-EOF > /etc/reclass/reclass-config.yml
323 storage_type: yaml_fs
324 pretty_print: True
325 output: yaml
326 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200327EOF
328
329 clone_reclass
330 # override some envs from cluster level *.env, use with care
331 source_local_envs
332
333 cd ${RECLASS_ROOT}
334 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
335 # Possibly subrepo checkout needed
336 git submodule update --init --recursive
337 fi
338
Petr Michalec212cc6a2017-08-17 21:39:40 +0200339 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec51ece842017-09-06 20:44:15 +0200340 rm -rvf ${RECLASS_ROOT}/nodes/_generated/*
341
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200342 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200343 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200344 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
Petr Michalec24b30e82017-08-21 10:59:18 +0200345 log_warn "Salt Master node specification has not been found in model."
346 log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200347 cat <<-EOF > ${CONFIG}
348 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200349 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200350 parameters:
351 _param:
Petr Michaleca7de6df2017-08-31 12:16:57 +0200352 salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200353 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200354 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
ibumarskovf72e05e2017-11-29 14:37:48 +0300355 reclass_data_revision: ${RECLASS_REVISION:-$RECLASS_BRANCH}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200356 reclass_data_repository: "$RECLASS_ADDRESS"
Petr Michaleca7de6df2017-08-31 12:16:57 +0200357 reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200358 linux_system_codename: ${DISTRIB_CODENAME}
359 cluster_name: ${CLUSTER_NAME}
360 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200361 linux:
362 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200363 name: ${HOSTNAME:-cfg01}
364 domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200365 # ########
366EOF
367
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200368 cat <<-EOF >> ${CONFIG}
369 salt:
370 master:
371 accept_policy: open_mode
372 source:
373 engine: $SALT_SOURCE
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200374 minion:
375 source:
376 engine: $SALT_SOURCE
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200377 # ########
378 # vim: ft=yaml sw=2 ts=2 sts=2
379EOF
380 fi
Petr Michalec24b30e82017-08-21 10:59:18 +0200381
382 log_debug "Salt Master node config yaml:"
383 log_debug "$(cat ${CONFIG})"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200384}
385
386configure_salt_minion()
387{
388 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
389 cat <<-EOF > /etc/salt/minion.d/minion.conf
Petr Michaleca7de6df2017-08-31 12:16:57 +0200390 master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200391 id: $MINION_ID
392 EOF
393}
394
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200395install_reclass()
396{
397 VERSION=${1:-$RECLASS_VERSION}
Petr Michalec372fa032017-11-15 17:36:19 +0100398 VERSION=${VERSION:-pkg}
Petr Michalec800dd072017-11-15 17:19:26 +0100399 case ${VERSION} in
400 pkg|package)
401 which reclass || $SUDO $PKGTOOL install -y reclass
rootb9e5d7c2018-03-17 12:12:44 +0000402 which reclass-salt || {
403 if [ -e /usr/share/reclass/reclass-salt ]; then
Petr Michalec6ba1c362018-03-17 15:01:51 +0100404 ln -fs /usr/share/reclass/reclass-salt /usr/bin
rootb9e5d7c2018-03-17 12:12:44 +0000405 fi
406 }
Petr Michalec800dd072017-11-15 17:19:26 +0100407 ;;
408 *)
Petr Michalec981f3cc2017-11-15 17:40:31 +0100409 log_warn "Install development version of reclass"
Petr Michalec1b37d3d2017-11-15 18:20:46 +0100410 # Note: dev/git/pip version...
411 # It replaces all local reclass versions on system
412 # Required for reclass/git features:
413 # $SUDO $PKGTOOL install -y libffi-dev libgit2-dev || true
Petr Michalec800dd072017-11-15 17:19:26 +0100414 for s in $(python -c "import site; print(' '.join(site.getsitepackages()))"); do
Petr Michalec23020832017-11-15 17:34:49 +0100415 sudo -H pip install --install-option="--prefix=" --upgrade --force-reinstall -I \
Petr Michalec800dd072017-11-15 17:19:26 +0100416 -t "$s" git+https://github.com/salt-formulas/reclass.git@${VERSION};
417 done
418 ;;
419 esac
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200420}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200421
422install_salt_master_pkg()
423{
424 echo -e "\nPreparing base OS repository ...\n"
425
426 configure_pkg_repo
427
428 echo -e "\nInstalling salt master ...\n"
429
430 case $PLATFORM_FAMILY in
431 debian)
432 $SUDO apt-get install -y git
alexz0adc2712018-02-11 17:33:52 +0100433 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200434 ;;
435 rhel)
436 yum install -y git
alexz0adc2712018-02-11 17:33:52 +0100437 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200438 ;;
439 esac
Jakub Josef0b611742018-01-09 17:02:01 +0100440
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200441 configure_salt_master
442
443 echo -e "\nRestarting services ...\n"
444 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
445 $SVCTOOL salt-master restart
446}
447
448install_salt_master_pip()
449{
450 echo -e "\nPreparing base OS repository ...\n"
451
452 case $PLATFORM_FAMILY in
453 debian)
454 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200455 ;;
456 rhel)
457 $SUDO yum install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200458 ;;
459 esac
460
461 echo -e "\nInstalling salt master ...\n"
462 # TODO: replace with saltstack bootstrap script
Jakub Josef0b611742018-01-09 17:02:01 +0100463
Petr Michalec88a95a22018-01-09 17:21:08 +0100464 if [ -z ${PIP_SALT_VERSION} ] || [ "$PIP_SALT_VERSION" == "latest" ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200465 pip install salt
466 else
Petr Michalec88a95a22018-01-09 17:21:08 +0100467 pip install salt==${PIP_SALT_VERSION}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200468 fi
469
470 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
471 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
472
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200473 configure_salt_master
474
475 echo -e "\nRestarting services ...\n"
476 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
477 $SVCTOOL salt-master restart
478}
479
480
481
482install_salt_minion_pkg()
483{
484
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200485 echo -e "\nInstalling salt minion ...\n"
486
487 case $PLATFORM_FAMILY in
488 debian)
alexz0adc2712018-02-11 17:33:52 +0100489 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200490 ;;
491 rhel)
alexz0adc2712018-02-11 17:33:52 +0100492 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200493 ;;
494 esac
495
496
497 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200498 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200499}
500
501install_salt_minion_pip()
502{
503 echo -e "\nInstalling salt minion ...\n"
504
505 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
506 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
507
508 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200509 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200510}
511
azvyagintseve93f8372018-09-12 16:50:58 +0300512function install_salt_formula_pkg_all(){
513 log_warn "Installing and configuring ALL formulas ..."
514 if ! $SUDO apt-get install -y 'salt-formula-*'; then
515 echo -e "\nInstall ALL formulas by mask 'salt-formula-*' failed.\n"
516 exit 1
517 fi
518 for formula_service in $(ls ${FORMULAS_PATH}/reclass/service/); do
519 #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
520 formula_service=${formula_service//-/$'_'}
521 if [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ]; then
522 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
523 fi
524 done
525}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200526
527install_salt_formula_pkg()
528{
529 configure_pkg_repo
530
531 case $PLATFORM_FAMILY in
532 debian)
533 echo "Configuring necessary formulas ..."
534
535 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
azvyagintseve93f8372018-09-12 16:50:58 +0300536 if [[ "${FORMULAS_SALT_MASTER}" =~ '*' ]]; then
537 install_salt_formula_pkg_all
538 return
539 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200540 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100541 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200542 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
543 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
544 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
545 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
546 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
547 exit 1
548 fi
Oleksii Grudev691d4712018-04-02 10:23:46 +0300549 #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
550 formula_service=${formula_service//-/$'_'}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200551 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
552 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
553 done
554 ;;
555 rhel)
556 # TODO
557 ;;
558 esac
559
560 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
561 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
562}
563
564install_salt_formula_git()
565{
566 echo "Configuring necessary formulas ..."
567
568 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
569 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100570 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200571 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
572 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
573 _BRANCH=${FORMULAS_BRANCH}
574 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
575 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
576 # Fallback to the master branch if the branch doesn't exist for this repository
577 _BRANCH=master
578 fi
579 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
580 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
581 exit 1
582 fi
583 } || {
584 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
585 git fetch origin/${_BRANCH} || git fetch --all
586 git checkout ${_BRANCH} && git pull || git pull;
587 cd -
588 }
589 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
590 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
591 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
592 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
593 done
594
595 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
596 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
597}
598
Petr Michalece9c84522017-12-13 17:14:08 +0100599saltservice_stop() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100600 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100601 $SUDO service salt-minion stop
602 $SUDO service salt-master stop
603 sleep ${SALT_STOPSTART_WAIT:-30}
Petr Michalec7822efe2017-12-15 16:19:32 +0100604 ${SUDO} pkill -9 salt-master && ${SUDO} rm -rf /var/run/salt/master/* || true
Petr Michalece9c84522017-12-13 17:14:08 +0100605 ${SUDO} pkill -9 salt-minion
606}
607saltservice_start() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100608 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100609 $SUDO service salt-master start
610 $SUDO service salt-minion start
611 sleep ${SALT_STOPSTART_WAIT:-30}
612}
613
614saltservice_restart() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100615 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100616 saltservice_stop
617 saltservice_start
618}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200619
620saltmaster_bootstrap() {
621
622 log_info "Salt master setup"
623 test -n "$MASTER_HOSTNAME" || exit 1
624
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200625 # override some envs from cluster level *.env, use with care
626 source_local_envs
627
Petr Michalece9c84522017-12-13 17:14:08 +0100628 saltservice_stop
629
630 clone_reclass
631
projoke3c30e7d2017-10-19 17:15:40 +0800632 SCRIPTS=$(dirname $0)
Petr Michalece9c84522017-12-13 17:14:08 +0100633
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200634 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
Adam Tengler035f24f2017-09-11 19:29:31 +0200635 export MASTER_IP=${MASTER_IP:-127.0.0.1}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200636 export MINION_ID=${MASTER_HOSTNAME}
637 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
638 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
639 fi
640 # call local "setup() master"
641 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
642 if ! setup master; then
643 #cat /tmp/salt-master-setup.log
644 log_err "salt master setup() failed."
645 exit 1
646 else
647 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
648 fi
649 }
650
Petr Michalec981f3cc2017-11-15 17:40:31 +0100651 log_info "Install reclass"
652 install_reclass ${RECLASS_VERSION/dev*/develop}
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200653
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200654 log_info "Re/starting salt services"
Petr Michalece9c84522017-12-13 17:14:08 +0100655 saltservice_restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200656}
657
658# Init salt master
659saltmaster_init() {
660
661 log_info "Runing saltmaster states"
662 test -n "$MASTER_HOSTNAME" || exit 1
663
664 set -e
665 $SUDO salt-call saltutil.sync_all >/dev/null
666
Petr Michalecda014202017-08-18 11:26:07 +0200667 # workarond isolated and not fully bootstraped environments
Petr Michalec2801acb2017-10-31 15:00:15 +0100668 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec54ee4082017-11-30 17:02:17 +0100669 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 +0100670 fi
Petr Michalec8ce8a652017-11-30 15:39:20 +0100671 PILLAR='{'${SALT_MASTER_PILLAR}' "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalecda014202017-08-18 11:26:07 +0200672
Petr Michalec8b92d552017-11-29 12:28:50 +0100673 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100674 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100675 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
azvyagintseve3f6f402018-08-23 17:21:49 +0300676 if [[ $SA_IGNORE_ERROR_SALT_MASTER_ENV =~ ^(False|false|0|no)$ ]]; then
677 exit 1
678 fi
679
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200680 fi
681
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200682 # Revert temporary SaltMaster minimal configuration, if any
683 pushd $RECLASS_ROOT
684 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
Petr Michalec87778fc2017-11-02 10:13:25 +0100685 PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200686 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200687 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200688 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
689 git checkout -- $RECLASS_ROOT/nodes || true
690 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
Petr Michalec7edf8322017-11-30 12:01:09 +0100691 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100692 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100693 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
azvyagintseve3f6f402018-08-23 17:21:49 +0300694 if [[ $SA_IGNORE_ERROR_SALT_MASTER_ENV =~ ^(False|false|0|no)$ ]]; then
695 exit 1
696 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200697 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200698 fi
699 popd
700
Petr Michalecda014202017-08-18 11:26:07 +0200701 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
702 log_info "State: salt.master.service"
Jakub Josef32563412018-01-17 17:55:05 +0100703 # ensure salt master is started
704 saltservice_start
Petr Michalec9fec8b32017-10-05 14:46:42 +0200705 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalec4f043772017-12-19 15:40:54 +0100706 saltservice_start
Petr Michalecda014202017-08-18 11:26:07 +0200707
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200708 log_info "State: salt.master.storage.node"
709 set +e
Petr Michalec7edf8322017-11-30 12:01:09 +0100710 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
711 # FIXME: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec9fec8b32017-10-05 14:46:42 +0200712 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200713 ret=$?
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200714
Petr Michalecdd646db2017-12-18 15:56:18 +0100715 set -e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200716 if [[ $ret -eq 2 ]]; then
717 log_err "State reclass.storage.node failed with exit code 2 but continuing."
718 elif [[ $ret -ne 0 ]]; then
719 log_err "State reclass.storage.node failed with exit code $ret"
720 exit 1
721 fi
722
Petr Michalecdd646db2017-12-18 15:56:18 +0100723 set +e
724 log_info "Updating minion.conf -> master: localhost"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200725 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
Petr Michalecdd646db2017-12-18 15:56:18 +0100726
727 log_info "Re/starting salt services" # in order to load new deployed configuration from model
Petr Michalece9c84522017-12-13 17:14:08 +0100728 saltservice_restart
Petr Michalecdd646db2017-12-18 15:56:18 +0100729
730 log_info "Salt Util sync all"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200731 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
732
733 verify_salt_master
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200734
735}
736
Petr Michalec71e32682018-03-22 08:34:34 +0100737## CI Workarounds
738
739function mockup_node_registration() {
740
741 # for dynamic nodes registrations (require github.com/salt-formulas/salt-formula-reclass)
742
743 source /etc/os-release
744 os_codename=$VERSION_CODENAME
745 domain=$(hostname -d)
746 master_ip=$(hostname -I | awk '{print $1}')
747 fake_ip_preffix=$(echo $master_ip | awk -F. '{print $1"."$2}')
748 fake_ip_base=10
749
750 # SHOULD BE ALREADY RUN AS A PART OF BOOTSTRAP
751 # sync, in order to load custom modules
752 #salt-call saltutil.sync_all
753
754 # SHOULD BE ALREADY RUN AS A PART OF BOOTSTRAP
755 #PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
756 #salt-call state.apply reclass.storage.node pillar="$PILLAR" > /dev/null 2>/dev/null || true
757
758 # rotate over dynamic hosts
759 for host_idx in {01..02};do
760 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
761 hostname=${host}${host_idx}
762 fake_ip_base=$(($fake_ip_base + 1))
763
764 node_network01_ip="$fake_ip_preffix.11.$fake_ip_base"
765 node_network02_ip="$fake_ip_preffix.12.$fake_ip_base"
766 node_network03_ip="$fake_ip_preffix.13.$fake_ip_base"
767 node_network04_ip="$fake_ip_preffix.14.$fake_ip_base"
768 node_network05_ip="$fake_ip_preffix.15.$fake_ip_base"
769 node_network01_iface=$(ls /sys/class/net/ | grep -v lo | sort | head -n1)
770 node_network02_iface="eth1"
771 node_network03_iface="eth2"
772 node_network04_iface="eth3"
773 node_network05_iface="eth4"
774
775 declare -A vars
776 vars=(
777 ["node_master_ip"]=
778 ["node_os"]=${os_codename}
779 ["node_deploy_ip"]=${node_network01_ip}
780 ["node_deploy_iface"]=${node_network01_iface}
781 ["node_control_ip"]=${node_network02_ip}
782 ["node_control_iface"]=${node_network02_iface}
783 ["node_tenant_ip"]=${node_network03_ip}
784 ["node_tenant_iface"]=${node_network03_iface}
785 ["node_external_ip"]=${node_network04_ip}
786 ["node_external_iface"]=${node_network04_iface}
787 ["node_baremetal_ip"]=${node_network05_ip}
788 ["node_baremetal_iface"]=${node_network05_iface}
789 ["node_domain"]=$domain
790 ["node_cluster"]=$(hostname -d |awk -F. '{print $1}')
791 ["node_hostname"]=$hostname
792 )
793
794 data=""; i=0
795 for key in "${!vars[@]}"; do
796 data+="\"${key}\": \"${vars[${key}]}\""
797 i=$(($i+1))
798 if [ $i -lt ${#vars[@]} ]; then
799 data+=", "
800 fi
801 done
802 echo "Classifying node $hostname"
803 NODECR='"node_name": "'${hostname}.${domain}'", "node_data": {'$data'}'
804 PILLAR='{'${NODECR}', "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
805 salt-call state.apply reclass.reactor_sls.node_register pillar="$PILLAR" #-l info
806 #salt-call event.send "reclass/minion/classify" "{$data}"
807 done
808 done
809
810}
811
812## VERIFY
813
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200814
815function verify_salt_master() {
816 set -e
817
818 log_info "Verify Salt master"
819 test -n "$MASTER_HOSTNAME" || exit 1
820
821 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200822 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
823 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200824 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
825 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
826 $SUDO salt-call --no-color grains.items
827 $SUDO salt-call --no-color pillar.data
828 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200829 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec6e40efc2018-03-22 09:47:47 +0100830 if ! $SUDO python -m reclass.cli --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200831 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
832 exit 1
833 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100834 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200835}
836
837function verify_salt_minion() {
838 set -e
839 node=$1
840 log_info "Verifying ${node}"
841 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
842 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
843 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
844 fi
Petr Michalec6e40efc2018-03-22 09:47:47 +0100845 if ! $SUDO python -m reclass.cli --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200846 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
847 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
848 exit 1
849 fi
850 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100851 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200852}
853
854function verify_salt_minions() {
855 #set -e
Petr Michalec679f15b2017-09-18 16:16:29 +0200856 NODES=$(find $RECLASS_ROOT/nodes/_generated/ -name "*.yml" | grep -v "cfg")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200857 log_info "Verifying minions: $(echo ${NODES}|xargs)"
858
859 # Parallel
860 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
861 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
862
863 function filterFails() {
864 grep -v '/grains' | tee -a $1 | tail -n20
865 }
866
867 log_info "Verify nodes"
868 passed=0
869 for node in ${NODES}; do
870 node=$(basename $node .yml)
871
872 # filter first in cluster.. ctl-01, mon-01, etc..
873 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
874 verify_salt_minion ${node} || continue
875 else
876 echo Skipped $node.
877 fi
878 passed=$(($passed+1))
879 done
880 # fail on failures
881 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
882 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
883 test ! $passed -lt $total || {
884 tail -n50 /tmp/*.pillar_verify
885 return 1
886 }
Petr Michalec7edf8322017-11-30 12:01:09 +0100887 #set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200888}
889
890
891
892##########################################
893# To install salt master/minon
894
895function install() {
896 setup $@
897}
898
899function setup() {
900 # CLI
901 while [ x"$1" != x"" ]; do
902 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
903
904 case $1 in
905 master )
906 install_salt_master_$SALT_SOURCE
907 install_salt_minion_$SALT_SOURCE
908 install_salt_formula_$FORMULAS_SOURCE
909 ;;
910 minion )
911 install_salt_minion_$SALT_SOURCE
912 ;;
913 esac
914 shift
915 done
916 echo DONE
917}
918
919function bootstrap() {
920 log_info "Bootstrap & verification of SaltMaster and configured minions."
921 trap _atexit INT TERM EXIT
922
923 system_config_master
924 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200925 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200926 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200927}
928
929function default() {
930 bootstrap $@
931}
932
933
934##########################################
azvyagintseve93f8372018-09-12 16:50:58 +0300935log_info "Running version: ${__ScriptVersion}"
936log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'"
937
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200938[[ "$0" != "$BASH_SOURCE" ]] || {
939# unless file is being sourced
940 default $@
941}