blob: de9ddafcb8f93c564b681f5c5e81d6a05067c0a2 [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
sgarbuz43c50ea2019-03-29 14:49:24 +020013__ScriptVersion="2019.02.27"
azvyagintseve93f8372018-09-12 16:50:58 +030014__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#
Vasyl Saienko70069c82018-12-15 14:11:53 +020055export APT_REPOSITORY=${APT_REPOSITORY:- deb [arch=amd64] http://mirror.mirantis.com/${DISTRIB_REVISION:-testing}/salt-formulas/${DISTRIB_CODENAME} ${DISTRIB_CODENAME} main}
56export APT_REPOSITORY_GPG=${APT_REPOSITORY_GPG:-http://mirror.mirantis.com/${DISTRIB_REVISION:-testing}/salt-formulas/${DISTRIB_CODENAME}/archive-salt-formulas.key}
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}
sgarbuz43c50ea2019-03-29 14:49:24 +020060export RECLASS_SYSTEM_ADDRESS=${RECLASS_SYSTEM_ADDRESS:-http://gerrit.mcp.mirantis.com/salt-models/reclass-system}
61export RECLASS_SYSTEM_BRANCH=${RECLASS_SYSTEM_BRANCH:-}
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
Petr Michalec33c04892017-09-08 15:10:30 +020092BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True}
alexz0adc2712018-02-11 17:33:52 +010093BOOTSTRAP_SALTSTACK_COM=${BOOTSTRAP_SALTSTACK_COM:-"https://bootstrap.saltstack.com"}
Martin Polreich16673dd2018-04-20 12:08:56 +020094BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION:- stable 2017.7 }
Jakub Josef0b611742018-01-09 17:02:01 +010095BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION//latest*/stable}
alexz0adc2712018-02-11 17:33:52 +010096# TODO add 'r' option by default
97# Currently, without 'r' option, upstream saltstack repos will be used. Otherwise
Vasyl Saienko28e3d462018-12-14 17:27:15 +020098# local one should be used, from http://mirror.mirantis.com/ or whatever.
Vasyl Saienko48a10bb2018-11-29 10:17:09 +000099BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX $BOOTSTRAP_SALTSTACK_VERSION }
Petr Michalece11cf612017-09-08 18:21:48 +0200100SALT_SOURCE=${SALT_SOURCE:-pkg}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200101
Petr Michalecb3120042018-03-17 11:17:32 +0100102# SECURITY
Petr Michalecca2471f2018-03-17 11:17:32 +0100103SSH_STRICTHOSTKEYCHECKING=no
Petr Michalecb3120042018-03-17 11:17:32 +0100104
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200105# environment
106if [ "$FORMULAS_SOURCE" == "git" ]; then
107 SALT_ENV=${SALT_ENV:-dev}
108elif [ "$FORMULAS_SOURCE" == "pkg" ]; then
109 SALT_ENV=${SALT_ENV:-prd}
110fi
Petr Michalec05b52452017-09-08 14:26:59 +0200111eval "$(grep -h '=' /etc/*release 2> /dev/null)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200112PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z)
113case $PLATFORM_FAMILY in
114 debian )
115 PKGTOOL="$SUDO apt-get"
116 test ${VERSION_ID//\.*/} -ge 16 && {
117 SVCTOOL=service
118 } || { SVCTOOL=service
119 }
120 ;;
121 rhel )
122 PKGTOOL="$SUDO yum"
123 test ${VERSION_ID//\.*/} -ge 7 && {
124 SVCTOOL=systemctl
125 } || { SVCTOOL=service
126 }
127 ;;
128esac
129
130export PLATFORM_FAMILY
131export PKGTOOL
132export SVCTOOL
133
134##########################################
135# FUNCTIONS
136
137log_info() {
138 echo -e "${YELLOW}[INFO] $* ${NC}"
139}
140
141log_warn() {
142 echo -e "${MAGENTA}[WARN] $* ${NC}"
143}
144
Petr Michalec24b30e82017-08-21 10:59:18 +0200145log_debug() {
146 echo -e "${CYAN}[WARN] $* ${NC}"
147}
148
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200149log_err() {
150 echo -e "${RED}[ERROR] $* ${NC}" >&2
151}
152
153configure_pkg_repo()
154{
Vasyl Saienko48a10bb2018-11-29 10:17:09 +0000155
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}
Vasyl Saienko48a10bb2018-11-29 10:17:09 +0000161 else
162 echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/mcp_salt.list >/dev/null
163 curl -sL $APT_REPOSITORY_GPG | $SUDO apt-key add -
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200164 fi
165 $SUDO apt-get clean
166 $SUDO apt-get update
167 ;;
168 rhel)
169 $SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm
170 $SUDO yum clean all
171 ;;
172 esac
173
174}
175
176_atexit() {
177 RETVAL=$?
178 trap true INT TERM EXIT
179
180 if [ $RETVAL -ne 0 ]; then
181 log_err "Execution failed"
182 else
183 log_info "Execution successful"
184 fi
185 return $RETVAL
186}
187
188retry() {
189 local tries
190 if [[ $1 =~ ^[0-9]+$ ]]; then
191 tries=$1; shift
192 else
193 tries=3
194 fi
Petr Michalec262e3ad2017-10-06 13:41:58 +0200195 ret=1
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200196 for i in $(seq 1 $tries); do
Petr Michalec262e3ad2017-10-06 13:41:58 +0200197 "$@" && return $? || ret=$?
198 sleep $i
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200199 done
Petr Michalec9fec8b32017-10-05 14:46:42 +0200200 return $ret
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200201}
202
203function clone_reclass() {
Petr Michaleca361d4e2017-09-08 11:38:16 +0200204 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200205 # No reclass at all, clone from given address
alexz0adc2712018-02-11 17:33:52 +0100206 # In case, non-github repo
207 _tmp_host=$(echo ${RECLASS_ADDRESS} | awk -F/ '{print $3}')
208 ssh-keyscan -T 1 -H ${_tmp_host} >> ~/.ssh/known_hosts || true
209 # But, awk is not perfect solution, so lets make double-check for github
210 ssh-keyscan -T 1 -H github.com >> ~/.ssh/known_hosts || true
ibumarskovf72e05e2017-11-29 14:37:48 +0300211 if echo ${RECLASS_BRANCH} | egrep -q "^refs"; then
212 git clone ${RECLASS_ADDRESS} ${RECLASS_ROOT}
213 cd ${RECLASS_ROOT}
214 git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH} && git checkout FETCH_HEAD
215 export RECLASS_REVISION=$(git rev-parse HEAD)
216 cd -
217 else
218 git clone -b ${RECLASS_BRANCH} ${RECLASS_ADDRESS} ${RECLASS_ROOT};
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200219 fi;
220 fi;
Petr Michalec679f15b2017-09-18 16:16:29 +0200221 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michaleca361d4e2017-09-08 11:38:16 +0200222 log_err "Reclass ${RECLASS_ROOT} is not fetched locally;"
223 ls -Rla ${RECLASS_ROOT}
224 exit 1
225 fi;
226 $SUDO mkdir -p $RECLASS_ROOT/classes/service
227 $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200228}
229
230
231##########################################
232# Main calls
233
Petr Michalecca2471f2018-03-17 11:17:32 +0100234ci_config_ssh() {
235
236 # for CI current user
Petr Michalecb3120042018-03-17 11:17:32 +0100237 conf=~/.ssh/config
238 mkdir -p $(dirname $conf)
239 touch $conf
Petr Michalecca2471f2018-03-17 11:17:32 +0100240 echo -e "Host *\n\tStrictHostKeyChecking $SSH_STRICTHOSTKEYCHECKING\n" | tee -a $conf >/dev/null
Petr Michalecb3120042018-03-17 11:17:32 +0100241 touch ~/.ssh/known_hosts
Petr Michalecca2471f2018-03-17 11:17:32 +0100242
Petr Michalecda014202017-08-18 11:26:07 +0200243 if ! grep github.com ~/.ssh/known_hosts; then
244 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
Petr Michalecb3120042018-03-17 11:17:32 +0100245 ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts || true
Petr Michalecda014202017-08-18 11:26:07 +0200246 fi
Petr Michalecb3120042018-03-17 11:17:32 +0100247
248 # for root
249 #conf=/root/.ssh/config
250 #$SUDO mkdir -p $(dirname $conf)
251 #$SUDO touch $conf
252 #if ! $SSH_STRICTHOSTKEYCHECKING; then
253 # echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee -a $conf >/dev/null
254 #fi
Petr Michalecda014202017-08-18 11:26:07 +0200255}
256
Petr Michalecca2471f2018-03-17 11:17:32 +0100257# DEPRECATED
258system_config_ssh_conf() {
259 # for backward compatibility
260 ci_config_ssh
Petr Michalecda014202017-08-18 11:26:07 +0200261}
262
263system_config_salt_modules_prereq() {
264 # salt-formulas custom modules dependencies, etc:
Petr Ruzickade2cd3b2017-12-07 14:41:20 +0100265 $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 +0200266}
267
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200268system_config_minion() {
269 log_info "System configuration salt minion"
270}
271
272system_config_master() {
273 log_info "System configuration salt master"
274
Petr Michalecda014202017-08-18 11:26:07 +0200275 system_config_salt_modules_prereq
Petr Michalecca2471f2018-03-17 11:17:32 +0100276 ci_config_ssh
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200277
Petr Michalecda014202017-08-18 11:26:07 +0200278 if ! grep '127.0.1.2.*salt' /etc/hosts; then
279 echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null
280 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200281}
282
283configure_salt_master()
284{
285
286 echo "Configuring salt-master ..."
287
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200288 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec29d3ea32017-10-31 17:22:36 +0100289 read -d '' IGNORE_CLASS_NOTFOUND <<-EOF
290 ignore_class_notfound: True
291 ignore_class_regexp:
292 - 'service.*'
293EOF
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200294 fi
295
Petr Michalecdd1a0472017-09-06 19:27:24 +0200296 # to force alternative reclass module path
Petr Michalecd9d0a322017-09-06 19:38:32 +0200297 if [ -n "$RECLASS_SOURCE_PATH" ]; then
298 RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}"
299 else
300 RECLASS_SOURCE_PATH=""
301 fi
Petr Michalecdd1a0472017-09-06 19:27:24 +0200302
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200303 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
304 cat <<-EOF > /etc/salt/master.d/master.conf
305 file_roots:
306 base:
307 - /usr/share/salt-formulas/env
308 prd:
309 - /srv/salt/env/prd
310 dev:
311 - /srv/salt/env/dev
312 pillar_opts: False
313 open_mode: True
314 reclass: &reclass
315 storage_type: yaml_fs
316 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200317 ${IGNORE_CLASS_NOTFOUND}
Petr Michalecdd1a0472017-09-06 19:27:24 +0200318 ${RECLASS_SOURCE_PATH}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200319 ext_pillar:
320 - reclass: *reclass
azvyagintsev11927a62019-02-11 12:28:52 +0200321 - gpg: {}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200322 master_tops:
323 reclass: *reclass
324EOF
325
326 echo "Configuring reclass ..."
327
328 [ ! -d /etc/reclass ] && mkdir /etc/reclass
329 cat <<-EOF > /etc/reclass/reclass-config.yml
330 storage_type: yaml_fs
331 pretty_print: True
332 output: yaml
333 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200334EOF
335
336 clone_reclass
337 # override some envs from cluster level *.env, use with care
338 source_local_envs
339
340 cd ${RECLASS_ROOT}
341 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200342 git submodule update --init --recursive
sgarbuz43c50ea2019-03-29 14:49:24 +0200343 if [ -n "${RECLASS_SYSTEM_BRANCH}" ]; then
344 pushd classes/system
345 git checkout master && git clean -fd
346 if echo ${RECLASS_SYSTEM_BRANCH} | egrep -q "^refs"; then
347 git fetch ${RECLASS_SYSTEM_ADDRESS} ${RECLASS_SYSTEM_BRANCH} && git checkout FETCH_HEAD
348 else
349 git checkout ${RECLASS_SYSTEM_BRANCH}
350 fi
351 popd
352 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200353 fi
354
Petr Michalec212cc6a2017-08-17 21:39:40 +0200355 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec51ece842017-09-06 20:44:15 +0200356
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200357 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200358 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200359 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
Petr Michalec24b30e82017-08-21 10:59:18 +0200360 log_warn "Salt Master node specification has not been found in model."
361 log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200362 cat <<-EOF > ${CONFIG}
363 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200364 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200365 parameters:
366 _param:
Petr Michaleca7de6df2017-08-31 12:16:57 +0200367 salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200368 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200369 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
ibumarskovf72e05e2017-11-29 14:37:48 +0300370 reclass_data_revision: ${RECLASS_REVISION:-$RECLASS_BRANCH}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200371 reclass_data_repository: "$RECLASS_ADDRESS"
Petr Michaleca7de6df2017-08-31 12:16:57 +0200372 reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200373 linux_system_codename: ${DISTRIB_CODENAME}
374 cluster_name: ${CLUSTER_NAME}
375 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200376 linux:
377 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200378 name: ${HOSTNAME:-cfg01}
379 domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200380 # ########
381EOF
382
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200383 cat <<-EOF >> ${CONFIG}
384 salt:
385 master:
386 accept_policy: open_mode
387 source:
388 engine: $SALT_SOURCE
azvyagintsev11927a62019-02-11 12:28:52 +0200389 ext_pillars:
390 100_gpg_pillar_renderer:
391 module: gpg
392 params: {}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200393 minion:
394 source:
395 engine: $SALT_SOURCE
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200396 # ########
397 # vim: ft=yaml sw=2 ts=2 sts=2
398EOF
399 fi
Petr Michalec24b30e82017-08-21 10:59:18 +0200400
401 log_debug "Salt Master node config yaml:"
402 log_debug "$(cat ${CONFIG})"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200403}
404
405configure_salt_minion()
406{
407 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
408 cat <<-EOF > /etc/salt/minion.d/minion.conf
Petr Michaleca7de6df2017-08-31 12:16:57 +0200409 master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200410 id: $MINION_ID
411 EOF
412}
413
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200414install_reclass()
415{
416 VERSION=${1:-$RECLASS_VERSION}
Petr Michalec372fa032017-11-15 17:36:19 +0100417 VERSION=${VERSION:-pkg}
Petr Michalec800dd072017-11-15 17:19:26 +0100418 case ${VERSION} in
419 pkg|package)
420 which reclass || $SUDO $PKGTOOL install -y reclass
rootb9e5d7c2018-03-17 12:12:44 +0000421 which reclass-salt || {
422 if [ -e /usr/share/reclass/reclass-salt ]; then
Petr Michalec6ba1c362018-03-17 15:01:51 +0100423 ln -fs /usr/share/reclass/reclass-salt /usr/bin
rootb9e5d7c2018-03-17 12:12:44 +0000424 fi
425 }
Petr Michalec800dd072017-11-15 17:19:26 +0100426 ;;
427 *)
Petr Michalec981f3cc2017-11-15 17:40:31 +0100428 log_warn "Install development version of reclass"
Petr Michalec1b37d3d2017-11-15 18:20:46 +0100429 # Note: dev/git/pip version...
430 # It replaces all local reclass versions on system
431 # Required for reclass/git features:
432 # $SUDO $PKGTOOL install -y libffi-dev libgit2-dev || true
Petr Michalec800dd072017-11-15 17:19:26 +0100433 for s in $(python -c "import site; print(' '.join(site.getsitepackages()))"); do
Petr Michalec23020832017-11-15 17:34:49 +0100434 sudo -H pip install --install-option="--prefix=" --upgrade --force-reinstall -I \
Petr Michalec800dd072017-11-15 17:19:26 +0100435 -t "$s" git+https://github.com/salt-formulas/reclass.git@${VERSION};
436 done
437 ;;
438 esac
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200439}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200440
441install_salt_master_pkg()
442{
443 echo -e "\nPreparing base OS repository ...\n"
444
445 configure_pkg_repo
446
447 echo -e "\nInstalling salt master ...\n"
448
449 case $PLATFORM_FAMILY in
450 debian)
451 $SUDO apt-get install -y git
alexz0adc2712018-02-11 17:33:52 +0100452 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200453 ;;
454 rhel)
455 yum install -y git
alexz0adc2712018-02-11 17:33:52 +0100456 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200457 ;;
458 esac
Jakub Josef0b611742018-01-09 17:02:01 +0100459
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200460 configure_salt_master
461
462 echo -e "\nRestarting services ...\n"
463 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
464 $SVCTOOL salt-master restart
465}
466
467install_salt_master_pip()
468{
469 echo -e "\nPreparing base OS repository ...\n"
470
471 case $PLATFORM_FAMILY in
472 debian)
473 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200474 ;;
475 rhel)
476 $SUDO yum install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200477 ;;
478 esac
479
480 echo -e "\nInstalling salt master ...\n"
481 # TODO: replace with saltstack bootstrap script
Jakub Josef0b611742018-01-09 17:02:01 +0100482
Petr Michalec88a95a22018-01-09 17:21:08 +0100483 if [ -z ${PIP_SALT_VERSION} ] || [ "$PIP_SALT_VERSION" == "latest" ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200484 pip install salt
485 else
Petr Michalec88a95a22018-01-09 17:21:08 +0100486 pip install salt==${PIP_SALT_VERSION}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200487 fi
488
489 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
490 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
491
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200492 configure_salt_master
493
494 echo -e "\nRestarting services ...\n"
495 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
496 $SVCTOOL salt-master restart
497}
498
499
500
501install_salt_minion_pkg()
502{
503
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200504 echo -e "\nInstalling salt minion ...\n"
505
506 case $PLATFORM_FAMILY in
507 debian)
alexz0adc2712018-02-11 17:33:52 +0100508 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200509 ;;
510 rhel)
alexz0adc2712018-02-11 17:33:52 +0100511 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200512 ;;
513 esac
514
515
516 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200517 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200518}
519
520install_salt_minion_pip()
521{
522 echo -e "\nInstalling salt minion ...\n"
523
524 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
525 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
526
527 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200528 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200529}
530
azvyagintseve93f8372018-09-12 16:50:58 +0300531function install_salt_formula_pkg_all(){
532 log_warn "Installing and configuring ALL formulas ..."
533 if ! $SUDO apt-get install -y 'salt-formula-*'; then
534 echo -e "\nInstall ALL formulas by mask 'salt-formula-*' failed.\n"
535 exit 1
536 fi
537 for formula_service in $(ls ${FORMULAS_PATH}/reclass/service/); do
538 #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
539 formula_service=${formula_service//-/$'_'}
540 if [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ]; then
541 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
542 fi
543 done
544}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200545
546install_salt_formula_pkg()
547{
548 configure_pkg_repo
549
550 case $PLATFORM_FAMILY in
551 debian)
552 echo "Configuring necessary formulas ..."
553
554 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
azvyagintseva7a91d52018-09-13 12:58:39 +0300555 if [[ ${EXTRA_FORMULAS_PKG_ALL} =~ ^(True|true|1|yes)$ ]]; then
azvyagintseve93f8372018-09-12 16:50:58 +0300556 install_salt_formula_pkg_all
557 return
558 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200559 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100560 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200561 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
562 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
563 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
564 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
565 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
566 exit 1
567 fi
Oleksii Grudev691d4712018-04-02 10:23:46 +0300568 #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
569 formula_service=${formula_service//-/$'_'}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200570 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
571 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
572 done
573 ;;
574 rhel)
575 # TODO
576 ;;
577 esac
578
579 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
580 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
581}
582
583install_salt_formula_git()
584{
585 echo "Configuring necessary formulas ..."
586
587 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
588 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100589 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200590 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
591 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
592 _BRANCH=${FORMULAS_BRANCH}
593 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
594 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
595 # Fallback to the master branch if the branch doesn't exist for this repository
596 _BRANCH=master
597 fi
598 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
599 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
600 exit 1
601 fi
602 } || {
603 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
604 git fetch origin/${_BRANCH} || git fetch --all
605 git checkout ${_BRANCH} && git pull || git pull;
606 cd -
607 }
608 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
609 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
610 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
611 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
612 done
613
614 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
615 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
616}
617
Petr Michalece9c84522017-12-13 17:14:08 +0100618saltservice_stop() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100619 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100620 $SUDO service salt-minion stop
621 $SUDO service salt-master stop
622 sleep ${SALT_STOPSTART_WAIT:-30}
Petr Michalec7822efe2017-12-15 16:19:32 +0100623 ${SUDO} pkill -9 salt-master && ${SUDO} rm -rf /var/run/salt/master/* || true
Petr Michalece9c84522017-12-13 17:14:08 +0100624 ${SUDO} pkill -9 salt-minion
625}
626saltservice_start() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100627 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100628 $SUDO service salt-master start
629 $SUDO service salt-minion start
630 sleep ${SALT_STOPSTART_WAIT:-30}
631}
632
633saltservice_restart() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100634 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100635 saltservice_stop
636 saltservice_start
637}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200638
639saltmaster_bootstrap() {
640
641 log_info "Salt master setup"
642 test -n "$MASTER_HOSTNAME" || exit 1
643
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200644 # override some envs from cluster level *.env, use with care
645 source_local_envs
646
Petr Michalece9c84522017-12-13 17:14:08 +0100647 saltservice_stop
648
649 clone_reclass
650
projoke3c30e7d2017-10-19 17:15:40 +0800651 SCRIPTS=$(dirname $0)
Petr Michalece9c84522017-12-13 17:14:08 +0100652
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200653 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
Adam Tengler035f24f2017-09-11 19:29:31 +0200654 export MASTER_IP=${MASTER_IP:-127.0.0.1}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200655 export MINION_ID=${MASTER_HOSTNAME}
656 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
657 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
658 fi
659 # call local "setup() master"
660 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
661 if ! setup master; then
662 #cat /tmp/salt-master-setup.log
663 log_err "salt master setup() failed."
664 exit 1
665 else
666 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
667 fi
668 }
669
Petr Michalec981f3cc2017-11-15 17:40:31 +0100670 log_info "Install reclass"
671 install_reclass ${RECLASS_VERSION/dev*/develop}
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200672
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200673 log_info "Re/starting salt services"
Petr Michalece9c84522017-12-13 17:14:08 +0100674 saltservice_restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200675}
676
677# Init salt master
678saltmaster_init() {
679
680 log_info "Runing saltmaster states"
681 test -n "$MASTER_HOSTNAME" || exit 1
682
683 set -e
684 $SUDO salt-call saltutil.sync_all >/dev/null
685
Petr Michalecda014202017-08-18 11:26:07 +0200686 # workarond isolated and not fully bootstraped environments
Petr Michalec2801acb2017-10-31 15:00:15 +0100687 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec54ee4082017-11-30 17:02:17 +0100688 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 +0100689 fi
Petr Michalec8ce8a652017-11-30 15:39:20 +0100690 PILLAR='{'${SALT_MASTER_PILLAR}' "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalecda014202017-08-18 11:26:07 +0200691
Petr Michalec8b92d552017-11-29 12:28:50 +0100692 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100693 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100694 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
azvyagintseve3f6f402018-08-23 17:21:49 +0300695 if [[ $SA_IGNORE_ERROR_SALT_MASTER_ENV =~ ^(False|false|0|no)$ ]]; then
696 exit 1
697 fi
698
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200699 fi
700
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200701 # Revert temporary SaltMaster minimal configuration, if any
702 pushd $RECLASS_ROOT
703 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
Petr Michalec87778fc2017-11-02 10:13:25 +0100704 PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200705 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200706 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200707 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
708 git checkout -- $RECLASS_ROOT/nodes || true
709 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
Petr Michalec7edf8322017-11-30 12:01:09 +0100710 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100711 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100712 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
azvyagintseve3f6f402018-08-23 17:21:49 +0300713 if [[ $SA_IGNORE_ERROR_SALT_MASTER_ENV =~ ^(False|false|0|no)$ ]]; then
714 exit 1
715 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200716 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200717 fi
718 popd
719
Petr Michalecda014202017-08-18 11:26:07 +0200720 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
721 log_info "State: salt.master.service"
Jakub Josef32563412018-01-17 17:55:05 +0100722 # ensure salt master is started
723 saltservice_start
Petr Michalec9fec8b32017-10-05 14:46:42 +0200724 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalec4f043772017-12-19 15:40:54 +0100725 saltservice_start
Petr Michalecda014202017-08-18 11:26:07 +0200726
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200727 log_info "State: salt.master.storage.node"
728 set +e
Petr Michalec7edf8322017-11-30 12:01:09 +0100729 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
730 # FIXME: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec9fec8b32017-10-05 14:46:42 +0200731 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200732 ret=$?
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200733
Petr Michalecdd646db2017-12-18 15:56:18 +0100734 set -e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200735 if [[ $ret -eq 2 ]]; then
736 log_err "State reclass.storage.node failed with exit code 2 but continuing."
737 elif [[ $ret -ne 0 ]]; then
738 log_err "State reclass.storage.node failed with exit code $ret"
739 exit 1
740 fi
741
Petr Michalecdd646db2017-12-18 15:56:18 +0100742 set +e
743 log_info "Updating minion.conf -> master: localhost"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200744 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
Petr Michalecdd646db2017-12-18 15:56:18 +0100745
746 log_info "Re/starting salt services" # in order to load new deployed configuration from model
Petr Michalece9c84522017-12-13 17:14:08 +0100747 saltservice_restart
Petr Michalecdd646db2017-12-18 15:56:18 +0100748
749 log_info "Salt Util sync all"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200750 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
751
752 verify_salt_master
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200753
754}
755
Petr Michalec71e32682018-03-22 08:34:34 +0100756## CI Workarounds
757
758function mockup_node_registration() {
759
760 # for dynamic nodes registrations (require github.com/salt-formulas/salt-formula-reclass)
761
762 source /etc/os-release
763 os_codename=$VERSION_CODENAME
764 domain=$(hostname -d)
765 master_ip=$(hostname -I | awk '{print $1}')
766 fake_ip_preffix=$(echo $master_ip | awk -F. '{print $1"."$2}')
767 fake_ip_base=10
768
769 # SHOULD BE ALREADY RUN AS A PART OF BOOTSTRAP
770 # sync, in order to load custom modules
771 #salt-call saltutil.sync_all
772
773 # SHOULD BE ALREADY RUN AS A PART OF BOOTSTRAP
774 #PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
775 #salt-call state.apply reclass.storage.node pillar="$PILLAR" > /dev/null 2>/dev/null || true
776
777 # rotate over dynamic hosts
778 for host_idx in {01..02};do
779 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
780 hostname=${host}${host_idx}
781 fake_ip_base=$(($fake_ip_base + 1))
782
783 node_network01_ip="$fake_ip_preffix.11.$fake_ip_base"
784 node_network02_ip="$fake_ip_preffix.12.$fake_ip_base"
785 node_network03_ip="$fake_ip_preffix.13.$fake_ip_base"
786 node_network04_ip="$fake_ip_preffix.14.$fake_ip_base"
787 node_network05_ip="$fake_ip_preffix.15.$fake_ip_base"
788 node_network01_iface=$(ls /sys/class/net/ | grep -v lo | sort | head -n1)
789 node_network02_iface="eth1"
790 node_network03_iface="eth2"
791 node_network04_iface="eth3"
792 node_network05_iface="eth4"
793
794 declare -A vars
795 vars=(
796 ["node_master_ip"]=
797 ["node_os"]=${os_codename}
798 ["node_deploy_ip"]=${node_network01_ip}
799 ["node_deploy_iface"]=${node_network01_iface}
800 ["node_control_ip"]=${node_network02_ip}
801 ["node_control_iface"]=${node_network02_iface}
802 ["node_tenant_ip"]=${node_network03_ip}
803 ["node_tenant_iface"]=${node_network03_iface}
804 ["node_external_ip"]=${node_network04_ip}
805 ["node_external_iface"]=${node_network04_iface}
806 ["node_baremetal_ip"]=${node_network05_ip}
807 ["node_baremetal_iface"]=${node_network05_iface}
808 ["node_domain"]=$domain
809 ["node_cluster"]=$(hostname -d |awk -F. '{print $1}')
810 ["node_hostname"]=$hostname
811 )
812
813 data=""; i=0
814 for key in "${!vars[@]}"; do
815 data+="\"${key}\": \"${vars[${key}]}\""
816 i=$(($i+1))
817 if [ $i -lt ${#vars[@]} ]; then
818 data+=", "
819 fi
820 done
821 echo "Classifying node $hostname"
822 NODECR='"node_name": "'${hostname}.${domain}'", "node_data": {'$data'}'
823 PILLAR='{'${NODECR}', "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
824 salt-call state.apply reclass.reactor_sls.node_register pillar="$PILLAR" #-l info
825 #salt-call event.send "reclass/minion/classify" "{$data}"
826 done
827 done
828
829}
830
831## VERIFY
832
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200833
834function verify_salt_master() {
835 set -e
836
837 log_info "Verify Salt master"
838 test -n "$MASTER_HOSTNAME" || exit 1
839
840 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200841 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
842 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200843 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
844 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
845 $SUDO salt-call --no-color grains.items
846 $SUDO salt-call --no-color pillar.data
847 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200848 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec6e40efc2018-03-22 09:47:47 +0100849 if ! $SUDO python -m reclass.cli --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200850 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
851 exit 1
852 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100853 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200854}
855
856function verify_salt_minion() {
857 set -e
858 node=$1
859 log_info "Verifying ${node}"
860 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
861 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
862 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
863 fi
Petr Michalec6e40efc2018-03-22 09:47:47 +0100864 if ! $SUDO python -m reclass.cli --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200865 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
866 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
867 exit 1
868 fi
869 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100870 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200871}
872
873function verify_salt_minions() {
874 #set -e
Petr Michalec679f15b2017-09-18 16:16:29 +0200875 NODES=$(find $RECLASS_ROOT/nodes/_generated/ -name "*.yml" | grep -v "cfg")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200876 log_info "Verifying minions: $(echo ${NODES}|xargs)"
877
878 # Parallel
879 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
880 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
881
882 function filterFails() {
883 grep -v '/grains' | tee -a $1 | tail -n20
884 }
885
886 log_info "Verify nodes"
887 passed=0
888 for node in ${NODES}; do
889 node=$(basename $node .yml)
890
891 # filter first in cluster.. ctl-01, mon-01, etc..
892 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
893 verify_salt_minion ${node} || continue
894 else
895 echo Skipped $node.
896 fi
897 passed=$(($passed+1))
898 done
899 # fail on failures
900 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
901 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
902 test ! $passed -lt $total || {
903 tail -n50 /tmp/*.pillar_verify
904 return 1
905 }
Petr Michalec7edf8322017-11-30 12:01:09 +0100906 #set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200907}
908
909
910
911##########################################
912# To install salt master/minon
913
914function install() {
915 setup $@
916}
917
918function setup() {
919 # CLI
920 while [ x"$1" != x"" ]; do
921 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
922
923 case $1 in
924 master )
925 install_salt_master_$SALT_SOURCE
926 install_salt_minion_$SALT_SOURCE
927 install_salt_formula_$FORMULAS_SOURCE
928 ;;
929 minion )
930 install_salt_minion_$SALT_SOURCE
931 ;;
932 esac
933 shift
934 done
935 echo DONE
936}
937
938function bootstrap() {
939 log_info "Bootstrap & verification of SaltMaster and configured minions."
940 trap _atexit INT TERM EXIT
941
942 system_config_master
943 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200944 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200945 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200946}
947
948function default() {
949 bootstrap $@
950}
951
952
953##########################################
azvyagintseve93f8372018-09-12 16:50:58 +0300954log_info "Running version: ${__ScriptVersion}"
955log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'"
956
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200957[[ "$0" != "$BASH_SOURCE" ]] || {
958# unless file is being sourced
959 default $@
960}