blob: a8124a0322d571b8f802cde6be27818bbef3b0f5 [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#
Oleh Hryhorovd4d06872018-11-27 14:33:45 +020055export MCP_SALT_REPO=${MCP_SALT_REPO:-deb [arch=amd64] http://apt.mirantis.com/${DISTRIB_CODENAME} ${DISTRIB_REVISION:-stable} salt}
56export APT_REPOSITORY=${APT_REPOSITORY:-${MCP_SALT_REPO}}
57export MCP_SALT_REPO_KEY=${MCP_SALT_REPO_KEY:-http://apt.mirantis.com/public.gpg}
58export APT_REPOSITORY_GPG=${APT_REPOSITORY_GPG:-${MCP_SALT_REPO_KEY}}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020059# reclass
60export RECLASS_ADDRESS=${RECLASS_ADDRESS:-https://github.com/salt-formulas/openstack-salt.git} # https/git
ibumarskovf72e05e2017-11-29 14:37:48 +030061export RECLASS_BRANCH=${RECLASS_BRANCH:-master}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020062
63# formula
64export FORMULAS_BASE=${FORMULAS_BASE:-https://github.com/salt-formulas}
65export FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas}
66export FORMULAS_BRANCH=${FORMULAS_BRANCH:-master}
67export FORMULAS_SOURCE=${FORMULAS_SOURCE:-pkg} # pkg/git
Petr Michalec515356f2017-11-16 11:20:20 +010068# Essential set of formulas (known to by used on cfg01 node for most setups during bootsrap)
69# 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 +030070FORMULAS_SALT_MASTER=${FORMULAS_SALT_MASTER:- "$EXTRA_FORMULAS"}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020071# minimal set of formulas for salt-master bootstrap
azvyagintseve93f8372018-09-12 16:50:58 +030072declare -a FORMULAS_SALT_MASTER=(reclass salt git openssh linux $(echo "${FORMULAS_SALT_MASTER}"))
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020073export FORMULAS_SALT_MASTER
74
azvyagintseva7a91d52018-09-13 12:58:39 +030075
76# Install 'salt-formula-*'. Works only with FORMULAS_SOURCE==pkg
77export EXTRA_FORMULAS_PKG_ALL=${EXTRA_FORMULAS_PKG_ALL:-False}
78
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020079# system / host
Petr Michalecc749e862017-09-06 21:10:10 +020080export HOSTNAME=${HOSTNAME:-`hostname -s`}
81export HOSTNAME=${HOSTNAME//.*/}
82export DOMAIN=${DOMAIN:-`hostname -d`}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020083export DOMAIN=${DOMAIN:-bootstrap.local}
84
85# salt
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020086export MINION_ID=${MINION_ID:-${HOSTNAME}.${DOMAIN}}
Petr Michalec2e3a4992017-08-17 14:20:43 +020087export MASTER_HOSTNAME=${MASTER_HOSTNAME:-${HOSTNAME}.${DOMAIN}}
azvyagintseve3f6f402018-08-23 17:21:49 +030088# Ignore state.apply salt.master.env error
89export SA_IGNORE_ERROR_SALT_MASTER_ENV=${SA_IGNORE_ERROR_SALT_MASTER_ENV:-False}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020090
91# saltstack
Oleh Hryhorovd4d06872018-11-27 14:33:45 +020092#
Petr Michalec33c04892017-09-08 15:10:30 +020093BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True}
alexz0adc2712018-02-11 17:33:52 +010094BOOTSTRAP_SALTSTACK_COM=${BOOTSTRAP_SALTSTACK_COM:-"https://bootstrap.saltstack.com"}
Martin Polreich16673dd2018-04-20 12:08:56 +020095BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION:- stable 2017.7 }
Jakub Josef0b611742018-01-09 17:02:01 +010096BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION//latest*/stable}
alexz0adc2712018-02-11 17:33:52 +010097# TODO add 'r' option by default
98# Currently, without 'r' option, upstream saltstack repos will be used. Otherwise
99# local one should be used, from http://apt.mirantis.com/ or whatever.
Oleh Hryhorovd4d06872018-11-27 14:33:45 +0200100BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dXr $BOOTSTRAP_SALTSTACK_VERSION }
Petr Michalece11cf612017-09-08 18:21:48 +0200101SALT_SOURCE=${SALT_SOURCE:-pkg}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200102
Petr Michalecb3120042018-03-17 11:17:32 +0100103# SECURITY
Petr Michalecca2471f2018-03-17 11:17:32 +0100104SSH_STRICTHOSTKEYCHECKING=no
Petr Michalecb3120042018-03-17 11:17:32 +0100105
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200106# environment
107if [ "$FORMULAS_SOURCE" == "git" ]; then
108 SALT_ENV=${SALT_ENV:-dev}
109elif [ "$FORMULAS_SOURCE" == "pkg" ]; then
110 SALT_ENV=${SALT_ENV:-prd}
111fi
Petr Michalec05b52452017-09-08 14:26:59 +0200112eval "$(grep -h '=' /etc/*release 2> /dev/null)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200113PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z)
114case $PLATFORM_FAMILY in
115 debian )
116 PKGTOOL="$SUDO apt-get"
117 test ${VERSION_ID//\.*/} -ge 16 && {
118 SVCTOOL=service
119 } || { SVCTOOL=service
120 }
121 ;;
122 rhel )
123 PKGTOOL="$SUDO yum"
124 test ${VERSION_ID//\.*/} -ge 7 && {
125 SVCTOOL=systemctl
126 } || { SVCTOOL=service
127 }
128 ;;
129esac
130
131export PLATFORM_FAMILY
132export PKGTOOL
133export SVCTOOL
134
135##########################################
136# FUNCTIONS
137
138log_info() {
139 echo -e "${YELLOW}[INFO] $* ${NC}"
140}
141
142log_warn() {
143 echo -e "${MAGENTA}[WARN] $* ${NC}"
144}
145
Petr Michalec24b30e82017-08-21 10:59:18 +0200146log_debug() {
147 echo -e "${CYAN}[WARN] $* ${NC}"
148}
149
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200150log_err() {
151 echo -e "${RED}[ERROR] $* ${NC}" >&2
152}
153
154configure_pkg_repo()
155{
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200156 case $PLATFORM_FAMILY in
157 debian)
158 if [ -n "$APT_REPOSITORY_PPA" ]; then
159 which add-apt-repository || $SUDO apt-get install -y software-properties-common
160 $SUDO add-apt-repository -y ppa:${APT_REPOSITORY_PPA}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200161 fi
162 $SUDO apt-get clean
163 $SUDO apt-get update
164 ;;
165 rhel)
166 $SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm
167 $SUDO yum clean all
168 ;;
169 esac
170
171}
172
173_atexit() {
174 RETVAL=$?
175 trap true INT TERM EXIT
176
177 if [ $RETVAL -ne 0 ]; then
178 log_err "Execution failed"
179 else
180 log_info "Execution successful"
181 fi
182 return $RETVAL
183}
184
185retry() {
186 local tries
187 if [[ $1 =~ ^[0-9]+$ ]]; then
188 tries=$1; shift
189 else
190 tries=3
191 fi
Petr Michalec262e3ad2017-10-06 13:41:58 +0200192 ret=1
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200193 for i in $(seq 1 $tries); do
Petr Michalec262e3ad2017-10-06 13:41:58 +0200194 "$@" && return $? || ret=$?
195 sleep $i
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200196 done
Petr Michalec9fec8b32017-10-05 14:46:42 +0200197 return $ret
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200198}
199
200function clone_reclass() {
Petr Michaleca361d4e2017-09-08 11:38:16 +0200201 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200202 # No reclass at all, clone from given address
alexz0adc2712018-02-11 17:33:52 +0100203 # In case, non-github repo
204 _tmp_host=$(echo ${RECLASS_ADDRESS} | awk -F/ '{print $3}')
205 ssh-keyscan -T 1 -H ${_tmp_host} >> ~/.ssh/known_hosts || true
206 # But, awk is not perfect solution, so lets make double-check for github
207 ssh-keyscan -T 1 -H github.com >> ~/.ssh/known_hosts || true
ibumarskovf72e05e2017-11-29 14:37:48 +0300208 if echo ${RECLASS_BRANCH} | egrep -q "^refs"; then
209 git clone ${RECLASS_ADDRESS} ${RECLASS_ROOT}
210 cd ${RECLASS_ROOT}
211 git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH} && git checkout FETCH_HEAD
212 export RECLASS_REVISION=$(git rev-parse HEAD)
213 cd -
214 else
215 git clone -b ${RECLASS_BRANCH} ${RECLASS_ADDRESS} ${RECLASS_ROOT};
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200216 fi;
217 fi;
Petr Michalec679f15b2017-09-18 16:16:29 +0200218 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michaleca361d4e2017-09-08 11:38:16 +0200219 log_err "Reclass ${RECLASS_ROOT} is not fetched locally;"
220 ls -Rla ${RECLASS_ROOT}
221 exit 1
222 fi;
223 $SUDO mkdir -p $RECLASS_ROOT/classes/service
224 $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200225}
226
227
228##########################################
229# Main calls
230
Petr Michalecca2471f2018-03-17 11:17:32 +0100231ci_config_ssh() {
232
233 # for CI current user
Petr Michalecb3120042018-03-17 11:17:32 +0100234 conf=~/.ssh/config
235 mkdir -p $(dirname $conf)
236 touch $conf
Petr Michalecca2471f2018-03-17 11:17:32 +0100237 echo -e "Host *\n\tStrictHostKeyChecking $SSH_STRICTHOSTKEYCHECKING\n" | tee -a $conf >/dev/null
Petr Michalecb3120042018-03-17 11:17:32 +0100238 touch ~/.ssh/known_hosts
Petr Michalecca2471f2018-03-17 11:17:32 +0100239
Petr Michalecda014202017-08-18 11:26:07 +0200240 if ! grep github.com ~/.ssh/known_hosts; then
241 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
Petr Michalecb3120042018-03-17 11:17:32 +0100242 ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts || true
Petr Michalecda014202017-08-18 11:26:07 +0200243 fi
Petr Michalecb3120042018-03-17 11:17:32 +0100244
245 # for root
246 #conf=/root/.ssh/config
247 #$SUDO mkdir -p $(dirname $conf)
248 #$SUDO touch $conf
249 #if ! $SSH_STRICTHOSTKEYCHECKING; then
250 # echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee -a $conf >/dev/null
251 #fi
Petr Michalecda014202017-08-18 11:26:07 +0200252}
253
Petr Michalecca2471f2018-03-17 11:17:32 +0100254# DEPRECATED
255system_config_ssh_conf() {
256 # for backward compatibility
257 ci_config_ssh
Petr Michalecda014202017-08-18 11:26:07 +0200258}
259
260system_config_salt_modules_prereq() {
261 # salt-formulas custom modules dependencies, etc:
Petr Ruzickade2cd3b2017-12-07 14:41:20 +0100262 $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 +0200263}
264
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200265system_config_minion() {
266 log_info "System configuration salt minion"
267}
268
269system_config_master() {
270 log_info "System configuration salt master"
271
Petr Michalecda014202017-08-18 11:26:07 +0200272 system_config_salt_modules_prereq
Petr Michalecca2471f2018-03-17 11:17:32 +0100273 ci_config_ssh
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200274
Petr Michalecda014202017-08-18 11:26:07 +0200275 if ! grep '127.0.1.2.*salt' /etc/hosts; then
276 echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null
277 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200278}
279
280configure_salt_master()
281{
282
283 echo "Configuring salt-master ..."
284
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200285 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec29d3ea32017-10-31 17:22:36 +0100286 read -d '' IGNORE_CLASS_NOTFOUND <<-EOF
287 ignore_class_notfound: True
288 ignore_class_regexp:
289 - 'service.*'
290EOF
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200291 fi
292
Petr Michalecdd1a0472017-09-06 19:27:24 +0200293 # to force alternative reclass module path
Petr Michalecd9d0a322017-09-06 19:38:32 +0200294 if [ -n "$RECLASS_SOURCE_PATH" ]; then
295 RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}"
296 else
297 RECLASS_SOURCE_PATH=""
298 fi
Petr Michalecdd1a0472017-09-06 19:27:24 +0200299
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200300 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
301 cat <<-EOF > /etc/salt/master.d/master.conf
302 file_roots:
303 base:
304 - /usr/share/salt-formulas/env
305 prd:
306 - /srv/salt/env/prd
307 dev:
308 - /srv/salt/env/dev
309 pillar_opts: False
310 open_mode: True
311 reclass: &reclass
312 storage_type: yaml_fs
313 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200314 ${IGNORE_CLASS_NOTFOUND}
Petr Michalecdd1a0472017-09-06 19:27:24 +0200315 ${RECLASS_SOURCE_PATH}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200316 ext_pillar:
317 - reclass: *reclass
318 master_tops:
319 reclass: *reclass
320EOF
321
322 echo "Configuring reclass ..."
323
324 [ ! -d /etc/reclass ] && mkdir /etc/reclass
325 cat <<-EOF > /etc/reclass/reclass-config.yml
326 storage_type: yaml_fs
327 pretty_print: True
328 output: yaml
329 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200330EOF
331
332 clone_reclass
333 # override some envs from cluster level *.env, use with care
334 source_local_envs
335
336 cd ${RECLASS_ROOT}
337 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
338 # Possibly subrepo checkout needed
339 git submodule update --init --recursive
340 fi
341
Petr Michalec212cc6a2017-08-17 21:39:40 +0200342 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec51ece842017-09-06 20:44:15 +0200343 rm -rvf ${RECLASS_ROOT}/nodes/_generated/*
344
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200345 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200346 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200347 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
Petr Michalec24b30e82017-08-21 10:59:18 +0200348 log_warn "Salt Master node specification has not been found in model."
349 log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200350 cat <<-EOF > ${CONFIG}
351 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200352 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200353 parameters:
354 _param:
Petr Michaleca7de6df2017-08-31 12:16:57 +0200355 salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200356 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200357 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
ibumarskovf72e05e2017-11-29 14:37:48 +0300358 reclass_data_revision: ${RECLASS_REVISION:-$RECLASS_BRANCH}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200359 reclass_data_repository: "$RECLASS_ADDRESS"
Petr Michaleca7de6df2017-08-31 12:16:57 +0200360 reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200361 linux_system_codename: ${DISTRIB_CODENAME}
362 cluster_name: ${CLUSTER_NAME}
363 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200364 linux:
365 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200366 name: ${HOSTNAME:-cfg01}
367 domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200368 # ########
369EOF
370
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200371 cat <<-EOF >> ${CONFIG}
372 salt:
373 master:
374 accept_policy: open_mode
375 source:
376 engine: $SALT_SOURCE
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200377 minion:
378 source:
379 engine: $SALT_SOURCE
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200380 # ########
381 # vim: ft=yaml sw=2 ts=2 sts=2
382EOF
383 fi
Petr Michalec24b30e82017-08-21 10:59:18 +0200384
385 log_debug "Salt Master node config yaml:"
386 log_debug "$(cat ${CONFIG})"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200387}
388
389configure_salt_minion()
390{
391 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
392 cat <<-EOF > /etc/salt/minion.d/minion.conf
Petr Michaleca7de6df2017-08-31 12:16:57 +0200393 master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200394 id: $MINION_ID
395 EOF
396}
397
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200398install_reclass()
399{
400 VERSION=${1:-$RECLASS_VERSION}
Petr Michalec372fa032017-11-15 17:36:19 +0100401 VERSION=${VERSION:-pkg}
Petr Michalec800dd072017-11-15 17:19:26 +0100402 case ${VERSION} in
403 pkg|package)
404 which reclass || $SUDO $PKGTOOL install -y reclass
rootb9e5d7c2018-03-17 12:12:44 +0000405 which reclass-salt || {
406 if [ -e /usr/share/reclass/reclass-salt ]; then
Petr Michalec6ba1c362018-03-17 15:01:51 +0100407 ln -fs /usr/share/reclass/reclass-salt /usr/bin
rootb9e5d7c2018-03-17 12:12:44 +0000408 fi
409 }
Petr Michalec800dd072017-11-15 17:19:26 +0100410 ;;
411 *)
Petr Michalec981f3cc2017-11-15 17:40:31 +0100412 log_warn "Install development version of reclass"
Petr Michalec1b37d3d2017-11-15 18:20:46 +0100413 # Note: dev/git/pip version...
414 # It replaces all local reclass versions on system
415 # Required for reclass/git features:
416 # $SUDO $PKGTOOL install -y libffi-dev libgit2-dev || true
Petr Michalec800dd072017-11-15 17:19:26 +0100417 for s in $(python -c "import site; print(' '.join(site.getsitepackages()))"); do
Petr Michalec23020832017-11-15 17:34:49 +0100418 sudo -H pip install --install-option="--prefix=" --upgrade --force-reinstall -I \
Petr Michalec800dd072017-11-15 17:19:26 +0100419 -t "$s" git+https://github.com/salt-formulas/reclass.git@${VERSION};
420 done
421 ;;
422 esac
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200423}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200424
425install_salt_master_pkg()
426{
427 echo -e "\nPreparing base OS repository ...\n"
428
429 configure_pkg_repo
430
431 echo -e "\nInstalling salt master ...\n"
432
433 case $PLATFORM_FAMILY in
434 debian)
435 $SUDO apt-get install -y git
alexz0adc2712018-02-11 17:33:52 +0100436 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200437 ;;
438 rhel)
439 yum install -y git
alexz0adc2712018-02-11 17:33:52 +0100440 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200441 ;;
442 esac
Jakub Josef0b611742018-01-09 17:02:01 +0100443
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200444 configure_salt_master
445
446 echo -e "\nRestarting services ...\n"
447 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
448 $SVCTOOL salt-master restart
449}
450
451install_salt_master_pip()
452{
453 echo -e "\nPreparing base OS repository ...\n"
454
455 case $PLATFORM_FAMILY in
456 debian)
457 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200458 ;;
459 rhel)
460 $SUDO yum install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200461 ;;
462 esac
463
464 echo -e "\nInstalling salt master ...\n"
465 # TODO: replace with saltstack bootstrap script
Jakub Josef0b611742018-01-09 17:02:01 +0100466
Petr Michalec88a95a22018-01-09 17:21:08 +0100467 if [ -z ${PIP_SALT_VERSION} ] || [ "$PIP_SALT_VERSION" == "latest" ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200468 pip install salt
469 else
Petr Michalec88a95a22018-01-09 17:21:08 +0100470 pip install salt==${PIP_SALT_VERSION}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200471 fi
472
473 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
474 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
475
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200476 configure_salt_master
477
478 echo -e "\nRestarting services ...\n"
479 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
480 $SVCTOOL salt-master restart
481}
482
483
484
485install_salt_minion_pkg()
486{
487
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200488 echo -e "\nInstalling salt minion ...\n"
489
490 case $PLATFORM_FAMILY in
491 debian)
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 rhel)
alexz0adc2712018-02-11 17:33:52 +0100495 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200496 ;;
497 esac
498
499
500 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200501 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200502}
503
504install_salt_minion_pip()
505{
506 echo -e "\nInstalling salt minion ...\n"
507
508 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
509 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
510
511 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200512 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200513}
514
azvyagintseve93f8372018-09-12 16:50:58 +0300515function install_salt_formula_pkg_all(){
516 log_warn "Installing and configuring ALL formulas ..."
517 if ! $SUDO apt-get install -y 'salt-formula-*'; then
518 echo -e "\nInstall ALL formulas by mask 'salt-formula-*' failed.\n"
519 exit 1
520 fi
521 for formula_service in $(ls ${FORMULAS_PATH}/reclass/service/); do
522 #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
523 formula_service=${formula_service//-/$'_'}
524 if [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ]; then
525 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
526 fi
527 done
528}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200529
530install_salt_formula_pkg()
531{
532 configure_pkg_repo
533
534 case $PLATFORM_FAMILY in
535 debian)
536 echo "Configuring necessary formulas ..."
537
538 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
azvyagintseva7a91d52018-09-13 12:58:39 +0300539 if [[ ${EXTRA_FORMULAS_PKG_ALL} =~ ^(True|true|1|yes)$ ]]; then
azvyagintseve93f8372018-09-12 16:50:58 +0300540 install_salt_formula_pkg_all
541 return
542 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200543 # 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 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
548 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
549 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
550 exit 1
551 fi
Oleksii Grudev691d4712018-04-02 10:23:46 +0300552 #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
553 formula_service=${formula_service//-/$'_'}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200554 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
555 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
556 done
557 ;;
558 rhel)
559 # TODO
560 ;;
561 esac
562
563 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
564 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
565}
566
567install_salt_formula_git()
568{
569 echo "Configuring necessary formulas ..."
570
571 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
572 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100573 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200574 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
575 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
576 _BRANCH=${FORMULAS_BRANCH}
577 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
578 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
579 # Fallback to the master branch if the branch doesn't exist for this repository
580 _BRANCH=master
581 fi
582 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
583 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
584 exit 1
585 fi
586 } || {
587 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
588 git fetch origin/${_BRANCH} || git fetch --all
589 git checkout ${_BRANCH} && git pull || git pull;
590 cd -
591 }
592 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
593 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
594 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
595 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
596 done
597
598 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
599 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
600}
601
Petr Michalece9c84522017-12-13 17:14:08 +0100602saltservice_stop() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100603 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100604 $SUDO service salt-minion stop
605 $SUDO service salt-master stop
606 sleep ${SALT_STOPSTART_WAIT:-30}
Petr Michalec7822efe2017-12-15 16:19:32 +0100607 ${SUDO} pkill -9 salt-master && ${SUDO} rm -rf /var/run/salt/master/* || true
Petr Michalece9c84522017-12-13 17:14:08 +0100608 ${SUDO} pkill -9 salt-minion
609}
610saltservice_start() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100611 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100612 $SUDO service salt-master start
613 $SUDO service salt-minion start
614 sleep ${SALT_STOPSTART_WAIT:-30}
615}
616
617saltservice_restart() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100618 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100619 saltservice_stop
620 saltservice_start
621}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200622
623saltmaster_bootstrap() {
624
625 log_info "Salt master setup"
626 test -n "$MASTER_HOSTNAME" || exit 1
627
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200628 # override some envs from cluster level *.env, use with care
629 source_local_envs
630
Petr Michalece9c84522017-12-13 17:14:08 +0100631 saltservice_stop
632
633 clone_reclass
634
projoke3c30e7d2017-10-19 17:15:40 +0800635 SCRIPTS=$(dirname $0)
Petr Michalece9c84522017-12-13 17:14:08 +0100636
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200637 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
Adam Tengler035f24f2017-09-11 19:29:31 +0200638 export MASTER_IP=${MASTER_IP:-127.0.0.1}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200639 export MINION_ID=${MASTER_HOSTNAME}
640 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
641 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
642 fi
643 # call local "setup() master"
644 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
645 if ! setup master; then
646 #cat /tmp/salt-master-setup.log
647 log_err "salt master setup() failed."
648 exit 1
649 else
650 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
651 fi
652 }
653
Petr Michalec981f3cc2017-11-15 17:40:31 +0100654 log_info "Install reclass"
655 install_reclass ${RECLASS_VERSION/dev*/develop}
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200656
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200657 log_info "Re/starting salt services"
Petr Michalece9c84522017-12-13 17:14:08 +0100658 saltservice_restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200659}
660
661# Init salt master
662saltmaster_init() {
663
664 log_info "Runing saltmaster states"
665 test -n "$MASTER_HOSTNAME" || exit 1
666
667 set -e
668 $SUDO salt-call saltutil.sync_all >/dev/null
669
Petr Michalecda014202017-08-18 11:26:07 +0200670 # workarond isolated and not fully bootstraped environments
Petr Michalec2801acb2017-10-31 15:00:15 +0100671 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec54ee4082017-11-30 17:02:17 +0100672 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 +0100673 fi
Petr Michalec8ce8a652017-11-30 15:39:20 +0100674 PILLAR='{'${SALT_MASTER_PILLAR}' "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalecda014202017-08-18 11:26:07 +0200675
Petr Michalec8b92d552017-11-29 12:28:50 +0100676 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100677 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100678 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
azvyagintseve3f6f402018-08-23 17:21:49 +0300679 if [[ $SA_IGNORE_ERROR_SALT_MASTER_ENV =~ ^(False|false|0|no)$ ]]; then
680 exit 1
681 fi
682
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200683 fi
684
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200685 # Revert temporary SaltMaster minimal configuration, if any
686 pushd $RECLASS_ROOT
687 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
Petr Michalec87778fc2017-11-02 10:13:25 +0100688 PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200689 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200690 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200691 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
692 git checkout -- $RECLASS_ROOT/nodes || true
693 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
Petr Michalec7edf8322017-11-30 12:01:09 +0100694 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100695 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100696 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
azvyagintseve3f6f402018-08-23 17:21:49 +0300697 if [[ $SA_IGNORE_ERROR_SALT_MASTER_ENV =~ ^(False|false|0|no)$ ]]; then
698 exit 1
699 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200700 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200701 fi
702 popd
703
Petr Michalecda014202017-08-18 11:26:07 +0200704 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
705 log_info "State: salt.master.service"
Jakub Josef32563412018-01-17 17:55:05 +0100706 # ensure salt master is started
707 saltservice_start
Petr Michalec9fec8b32017-10-05 14:46:42 +0200708 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalec4f043772017-12-19 15:40:54 +0100709 saltservice_start
Petr Michalecda014202017-08-18 11:26:07 +0200710
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200711 log_info "State: salt.master.storage.node"
712 set +e
Petr Michalec7edf8322017-11-30 12:01:09 +0100713 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
714 # FIXME: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec9fec8b32017-10-05 14:46:42 +0200715 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200716 ret=$?
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200717
Petr Michalecdd646db2017-12-18 15:56:18 +0100718 set -e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200719 if [[ $ret -eq 2 ]]; then
720 log_err "State reclass.storage.node failed with exit code 2 but continuing."
721 elif [[ $ret -ne 0 ]]; then
722 log_err "State reclass.storage.node failed with exit code $ret"
723 exit 1
724 fi
725
Petr Michalecdd646db2017-12-18 15:56:18 +0100726 set +e
727 log_info "Updating minion.conf -> master: localhost"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200728 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
Petr Michalecdd646db2017-12-18 15:56:18 +0100729
730 log_info "Re/starting salt services" # in order to load new deployed configuration from model
Petr Michalece9c84522017-12-13 17:14:08 +0100731 saltservice_restart
Petr Michalecdd646db2017-12-18 15:56:18 +0100732
733 log_info "Salt Util sync all"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200734 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
735
736 verify_salt_master
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200737
738}
739
Petr Michalec71e32682018-03-22 08:34:34 +0100740## CI Workarounds
741
742function mockup_node_registration() {
743
744 # for dynamic nodes registrations (require github.com/salt-formulas/salt-formula-reclass)
745
746 source /etc/os-release
747 os_codename=$VERSION_CODENAME
748 domain=$(hostname -d)
749 master_ip=$(hostname -I | awk '{print $1}')
750 fake_ip_preffix=$(echo $master_ip | awk -F. '{print $1"."$2}')
751 fake_ip_base=10
752
753 # SHOULD BE ALREADY RUN AS A PART OF BOOTSTRAP
754 # sync, in order to load custom modules
755 #salt-call saltutil.sync_all
756
757 # SHOULD BE ALREADY RUN AS A PART OF BOOTSTRAP
758 #PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
759 #salt-call state.apply reclass.storage.node pillar="$PILLAR" > /dev/null 2>/dev/null || true
760
761 # rotate over dynamic hosts
762 for host_idx in {01..02};do
763 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
764 hostname=${host}${host_idx}
765 fake_ip_base=$(($fake_ip_base + 1))
766
767 node_network01_ip="$fake_ip_preffix.11.$fake_ip_base"
768 node_network02_ip="$fake_ip_preffix.12.$fake_ip_base"
769 node_network03_ip="$fake_ip_preffix.13.$fake_ip_base"
770 node_network04_ip="$fake_ip_preffix.14.$fake_ip_base"
771 node_network05_ip="$fake_ip_preffix.15.$fake_ip_base"
772 node_network01_iface=$(ls /sys/class/net/ | grep -v lo | sort | head -n1)
773 node_network02_iface="eth1"
774 node_network03_iface="eth2"
775 node_network04_iface="eth3"
776 node_network05_iface="eth4"
777
778 declare -A vars
779 vars=(
780 ["node_master_ip"]=
781 ["node_os"]=${os_codename}
782 ["node_deploy_ip"]=${node_network01_ip}
783 ["node_deploy_iface"]=${node_network01_iface}
784 ["node_control_ip"]=${node_network02_ip}
785 ["node_control_iface"]=${node_network02_iface}
786 ["node_tenant_ip"]=${node_network03_ip}
787 ["node_tenant_iface"]=${node_network03_iface}
788 ["node_external_ip"]=${node_network04_ip}
789 ["node_external_iface"]=${node_network04_iface}
790 ["node_baremetal_ip"]=${node_network05_ip}
791 ["node_baremetal_iface"]=${node_network05_iface}
792 ["node_domain"]=$domain
793 ["node_cluster"]=$(hostname -d |awk -F. '{print $1}')
794 ["node_hostname"]=$hostname
795 )
796
797 data=""; i=0
798 for key in "${!vars[@]}"; do
799 data+="\"${key}\": \"${vars[${key}]}\""
800 i=$(($i+1))
801 if [ $i -lt ${#vars[@]} ]; then
802 data+=", "
803 fi
804 done
805 echo "Classifying node $hostname"
806 NODECR='"node_name": "'${hostname}.${domain}'", "node_data": {'$data'}'
807 PILLAR='{'${NODECR}', "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
808 salt-call state.apply reclass.reactor_sls.node_register pillar="$PILLAR" #-l info
809 #salt-call event.send "reclass/minion/classify" "{$data}"
810 done
811 done
812
813}
814
815## VERIFY
816
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200817
818function verify_salt_master() {
819 set -e
820
821 log_info "Verify Salt master"
822 test -n "$MASTER_HOSTNAME" || exit 1
823
824 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200825 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
826 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200827 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
828 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
829 $SUDO salt-call --no-color grains.items
830 $SUDO salt-call --no-color pillar.data
831 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200832 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec6e40efc2018-03-22 09:47:47 +0100833 if ! $SUDO python -m reclass.cli --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200834 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
835 exit 1
836 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100837 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200838}
839
840function verify_salt_minion() {
841 set -e
842 node=$1
843 log_info "Verifying ${node}"
844 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
845 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
846 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
847 fi
Petr Michalec6e40efc2018-03-22 09:47:47 +0100848 if ! $SUDO python -m reclass.cli --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200849 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
850 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
851 exit 1
852 fi
853 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100854 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200855}
856
857function verify_salt_minions() {
858 #set -e
Petr Michalec679f15b2017-09-18 16:16:29 +0200859 NODES=$(find $RECLASS_ROOT/nodes/_generated/ -name "*.yml" | grep -v "cfg")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200860 log_info "Verifying minions: $(echo ${NODES}|xargs)"
861
862 # Parallel
863 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
864 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
865
866 function filterFails() {
867 grep -v '/grains' | tee -a $1 | tail -n20
868 }
869
870 log_info "Verify nodes"
871 passed=0
872 for node in ${NODES}; do
873 node=$(basename $node .yml)
874
875 # filter first in cluster.. ctl-01, mon-01, etc..
876 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
877 verify_salt_minion ${node} || continue
878 else
879 echo Skipped $node.
880 fi
881 passed=$(($passed+1))
882 done
883 # fail on failures
884 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
885 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
886 test ! $passed -lt $total || {
887 tail -n50 /tmp/*.pillar_verify
888 return 1
889 }
Petr Michalec7edf8322017-11-30 12:01:09 +0100890 #set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200891}
892
893
894
895##########################################
896# To install salt master/minon
897
898function install() {
899 setup $@
900}
901
902function setup() {
903 # CLI
904 while [ x"$1" != x"" ]; do
905 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
906
907 case $1 in
908 master )
909 install_salt_master_$SALT_SOURCE
910 install_salt_minion_$SALT_SOURCE
911 install_salt_formula_$FORMULAS_SOURCE
912 ;;
913 minion )
914 install_salt_minion_$SALT_SOURCE
915 ;;
916 esac
917 shift
918 done
919 echo DONE
920}
921
922function bootstrap() {
923 log_info "Bootstrap & verification of SaltMaster and configured minions."
924 trap _atexit INT TERM EXIT
925
926 system_config_master
927 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200928 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200929 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200930}
931
932function default() {
933 bootstrap $@
934}
935
936
937##########################################
azvyagintseve93f8372018-09-12 16:50:58 +0300938log_info "Running version: ${__ScriptVersion}"
939log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'"
940
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200941[[ "$0" != "$BASH_SOURCE" ]] || {
942# unless file is being sourced
943 default $@
944}