blob: b84b27a77f600036519d7c29ed83a5796bcb57e0 [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#
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}
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
azvyagintseva7a91d52018-09-13 12:58:39 +030073
74# Install 'salt-formula-*'. Works only with FORMULAS_SOURCE==pkg
75export EXTRA_FORMULAS_PKG_ALL=${EXTRA_FORMULAS_PKG_ALL:-False}
76
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020077# system / host
Petr Michalecc749e862017-09-06 21:10:10 +020078export HOSTNAME=${HOSTNAME:-`hostname -s`}
79export HOSTNAME=${HOSTNAME//.*/}
80export DOMAIN=${DOMAIN:-`hostname -d`}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020081export DOMAIN=${DOMAIN:-bootstrap.local}
82
83# salt
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020084export MINION_ID=${MINION_ID:-${HOSTNAME}.${DOMAIN}}
Petr Michalec2e3a4992017-08-17 14:20:43 +020085export MASTER_HOSTNAME=${MASTER_HOSTNAME:-${HOSTNAME}.${DOMAIN}}
azvyagintseve3f6f402018-08-23 17:21:49 +030086# Ignore state.apply salt.master.env error
87export SA_IGNORE_ERROR_SALT_MASTER_ENV=${SA_IGNORE_ERROR_SALT_MASTER_ENV:-False}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020088
89# saltstack
Petr Michalec33c04892017-09-08 15:10:30 +020090BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True}
Pavlo Shchelokovskyy90dc0f52021-07-05 15:02:48 +030091BOOTSTRAP_SALTSTACK_COM=${BOOTSTRAP_SALTSTACK_COM:-"https://gerrit.mcp.mirantis.com/projects/salt-formulas%2Fsalt-formulas-scripts/branches/release%2F2019.2.0/files/bootstrap-salt.sh/content"}
Martin Polreich16673dd2018-04-20 12:08:56 +020092BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION:- stable 2017.7 }
Jakub Josef0b611742018-01-09 17:02:01 +010093BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION//latest*/stable}
alexz0adc2712018-02-11 17:33:52 +010094# TODO add 'r' option by default
95# Currently, without 'r' option, upstream saltstack repos will be used. Otherwise
Vasyl Saienko28e3d462018-12-14 17:27:15 +020096# local one should be used, from http://mirror.mirantis.com/ or whatever.
Vasyl Saienko48a10bb2018-11-29 10:17:09 +000097BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX $BOOTSTRAP_SALTSTACK_VERSION }
Petr Michalece11cf612017-09-08 18:21:48 +020098SALT_SOURCE=${SALT_SOURCE:-pkg}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020099
Petr Michalecb3120042018-03-17 11:17:32 +0100100# SECURITY
Petr Michalecca2471f2018-03-17 11:17:32 +0100101SSH_STRICTHOSTKEYCHECKING=no
Petr Michalecb3120042018-03-17 11:17:32 +0100102
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200103# environment
104if [ "$FORMULAS_SOURCE" == "git" ]; then
105 SALT_ENV=${SALT_ENV:-dev}
106elif [ "$FORMULAS_SOURCE" == "pkg" ]; then
107 SALT_ENV=${SALT_ENV:-prd}
108fi
Petr Michalec05b52452017-09-08 14:26:59 +0200109eval "$(grep -h '=' /etc/*release 2> /dev/null)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200110PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z)
111case $PLATFORM_FAMILY in
112 debian )
113 PKGTOOL="$SUDO apt-get"
114 test ${VERSION_ID//\.*/} -ge 16 && {
115 SVCTOOL=service
116 } || { SVCTOOL=service
117 }
118 ;;
119 rhel )
120 PKGTOOL="$SUDO yum"
121 test ${VERSION_ID//\.*/} -ge 7 && {
122 SVCTOOL=systemctl
123 } || { SVCTOOL=service
124 }
125 ;;
126esac
127
128export PLATFORM_FAMILY
129export PKGTOOL
130export SVCTOOL
131
132##########################################
133# FUNCTIONS
134
135log_info() {
136 echo -e "${YELLOW}[INFO] $* ${NC}"
137}
138
139log_warn() {
140 echo -e "${MAGENTA}[WARN] $* ${NC}"
141}
142
Petr Michalec24b30e82017-08-21 10:59:18 +0200143log_debug() {
144 echo -e "${CYAN}[WARN] $* ${NC}"
145}
146
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200147log_err() {
148 echo -e "${RED}[ERROR] $* ${NC}" >&2
149}
150
151configure_pkg_repo()
152{
Vasyl Saienko48a10bb2018-11-29 10:17:09 +0000153
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200154 case $PLATFORM_FAMILY in
155 debian)
156 if [ -n "$APT_REPOSITORY_PPA" ]; then
157 which add-apt-repository || $SUDO apt-get install -y software-properties-common
158 $SUDO add-apt-repository -y ppa:${APT_REPOSITORY_PPA}
Vasyl Saienko48a10bb2018-11-29 10:17:09 +0000159 else
160 echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/mcp_salt.list >/dev/null
161 curl -sL $APT_REPOSITORY_GPG | $SUDO apt-key add -
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200162 fi
163 $SUDO apt-get clean
164 $SUDO apt-get update
165 ;;
166 rhel)
167 $SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm
168 $SUDO yum clean all
169 ;;
170 esac
171
172}
173
174_atexit() {
175 RETVAL=$?
176 trap true INT TERM EXIT
177
178 if [ $RETVAL -ne 0 ]; then
179 log_err "Execution failed"
180 else
181 log_info "Execution successful"
182 fi
183 return $RETVAL
184}
185
186retry() {
187 local tries
188 if [[ $1 =~ ^[0-9]+$ ]]; then
189 tries=$1; shift
190 else
191 tries=3
192 fi
Petr Michalec262e3ad2017-10-06 13:41:58 +0200193 ret=1
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200194 for i in $(seq 1 $tries); do
Petr Michalec262e3ad2017-10-06 13:41:58 +0200195 "$@" && return $? || ret=$?
196 sleep $i
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200197 done
Petr Michalec9fec8b32017-10-05 14:46:42 +0200198 return $ret
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200199}
200
201function clone_reclass() {
Petr Michaleca361d4e2017-09-08 11:38:16 +0200202 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200203 # No reclass at all, clone from given address
alexz0adc2712018-02-11 17:33:52 +0100204 # In case, non-github repo
205 _tmp_host=$(echo ${RECLASS_ADDRESS} | awk -F/ '{print $3}')
206 ssh-keyscan -T 1 -H ${_tmp_host} >> ~/.ssh/known_hosts || true
207 # But, awk is not perfect solution, so lets make double-check for github
208 ssh-keyscan -T 1 -H github.com >> ~/.ssh/known_hosts || true
ibumarskovf72e05e2017-11-29 14:37:48 +0300209 if echo ${RECLASS_BRANCH} | egrep -q "^refs"; then
210 git clone ${RECLASS_ADDRESS} ${RECLASS_ROOT}
211 cd ${RECLASS_ROOT}
212 git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH} && git checkout FETCH_HEAD
213 export RECLASS_REVISION=$(git rev-parse HEAD)
214 cd -
215 else
216 git clone -b ${RECLASS_BRANCH} ${RECLASS_ADDRESS} ${RECLASS_ROOT};
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200217 fi;
218 fi;
Petr Michalec679f15b2017-09-18 16:16:29 +0200219 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michaleca361d4e2017-09-08 11:38:16 +0200220 log_err "Reclass ${RECLASS_ROOT} is not fetched locally;"
221 ls -Rla ${RECLASS_ROOT}
222 exit 1
223 fi;
224 $SUDO mkdir -p $RECLASS_ROOT/classes/service
225 $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200226}
227
228
229##########################################
230# Main calls
231
Petr Michalecca2471f2018-03-17 11:17:32 +0100232ci_config_ssh() {
233
234 # for CI current user
Petr Michalecb3120042018-03-17 11:17:32 +0100235 conf=~/.ssh/config
236 mkdir -p $(dirname $conf)
237 touch $conf
Petr Michalecca2471f2018-03-17 11:17:32 +0100238 echo -e "Host *\n\tStrictHostKeyChecking $SSH_STRICTHOSTKEYCHECKING\n" | tee -a $conf >/dev/null
Petr Michalecb3120042018-03-17 11:17:32 +0100239 touch ~/.ssh/known_hosts
Petr Michalecca2471f2018-03-17 11:17:32 +0100240
Petr Michalecda014202017-08-18 11:26:07 +0200241 if ! grep github.com ~/.ssh/known_hosts; then
242 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
Petr Michalecb3120042018-03-17 11:17:32 +0100243 ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts || true
Petr Michalecda014202017-08-18 11:26:07 +0200244 fi
Petr Michalecb3120042018-03-17 11:17:32 +0100245
246 # for root
247 #conf=/root/.ssh/config
248 #$SUDO mkdir -p $(dirname $conf)
249 #$SUDO touch $conf
250 #if ! $SSH_STRICTHOSTKEYCHECKING; then
251 # echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee -a $conf >/dev/null
252 #fi
Petr Michalecda014202017-08-18 11:26:07 +0200253}
254
Petr Michalecca2471f2018-03-17 11:17:32 +0100255# DEPRECATED
256system_config_ssh_conf() {
257 # for backward compatibility
258 ci_config_ssh
Petr Michalecda014202017-08-18 11:26:07 +0200259}
260
261system_config_salt_modules_prereq() {
262 # salt-formulas custom modules dependencies, etc:
Petr Ruzickade2cd3b2017-12-07 14:41:20 +0100263 $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 +0200264}
265
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200266system_config_minion() {
267 log_info "System configuration salt minion"
268}
269
270system_config_master() {
271 log_info "System configuration salt master"
272
Petr Michalecda014202017-08-18 11:26:07 +0200273 system_config_salt_modules_prereq
Petr Michalecca2471f2018-03-17 11:17:32 +0100274 ci_config_ssh
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200275
Petr Michalecda014202017-08-18 11:26:07 +0200276 if ! grep '127.0.1.2.*salt' /etc/hosts; then
277 echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null
278 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200279}
280
281configure_salt_master()
282{
283
284 echo "Configuring salt-master ..."
285
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200286 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec29d3ea32017-10-31 17:22:36 +0100287 read -d '' IGNORE_CLASS_NOTFOUND <<-EOF
288 ignore_class_notfound: True
289 ignore_class_regexp:
290 - 'service.*'
291EOF
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200292 fi
293
Petr Michalecdd1a0472017-09-06 19:27:24 +0200294 # to force alternative reclass module path
Petr Michalecd9d0a322017-09-06 19:38:32 +0200295 if [ -n "$RECLASS_SOURCE_PATH" ]; then
296 RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}"
297 else
298 RECLASS_SOURCE_PATH=""
299 fi
Petr Michalecdd1a0472017-09-06 19:27:24 +0200300
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200301 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
302 cat <<-EOF > /etc/salt/master.d/master.conf
303 file_roots:
304 base:
305 - /usr/share/salt-formulas/env
306 prd:
307 - /srv/salt/env/prd
308 dev:
309 - /srv/salt/env/dev
310 pillar_opts: False
311 open_mode: True
312 reclass: &reclass
313 storage_type: yaml_fs
314 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200315 ${IGNORE_CLASS_NOTFOUND}
Petr Michalecdd1a0472017-09-06 19:27:24 +0200316 ${RECLASS_SOURCE_PATH}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200317 ext_pillar:
318 - reclass: *reclass
319 master_tops:
320 reclass: *reclass
321EOF
322
323 echo "Configuring reclass ..."
324
325 [ ! -d /etc/reclass ] && mkdir /etc/reclass
326 cat <<-EOF > /etc/reclass/reclass-config.yml
327 storage_type: yaml_fs
328 pretty_print: True
329 output: yaml
330 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200331EOF
332
333 clone_reclass
334 # override some envs from cluster level *.env, use with care
335 source_local_envs
336
337 cd ${RECLASS_ROOT}
338 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
339 # Possibly subrepo checkout needed
340 git submodule update --init --recursive
341 fi
342
Petr Michalec212cc6a2017-08-17 21:39:40 +0200343 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec51ece842017-09-06 20:44:15 +0200344 rm -rvf ${RECLASS_ROOT}/nodes/_generated/*
345
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200346 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200347 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200348 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
Petr Michalec24b30e82017-08-21 10:59:18 +0200349 log_warn "Salt Master node specification has not been found in model."
350 log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200351 cat <<-EOF > ${CONFIG}
352 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200353 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200354 parameters:
355 _param:
Petr Michaleca7de6df2017-08-31 12:16:57 +0200356 salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200357 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200358 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
ibumarskovf72e05e2017-11-29 14:37:48 +0300359 reclass_data_revision: ${RECLASS_REVISION:-$RECLASS_BRANCH}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200360 reclass_data_repository: "$RECLASS_ADDRESS"
Petr Michaleca7de6df2017-08-31 12:16:57 +0200361 reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200362 linux_system_codename: ${DISTRIB_CODENAME}
363 cluster_name: ${CLUSTER_NAME}
364 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200365 linux:
366 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200367 name: ${HOSTNAME:-cfg01}
368 domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200369 # ########
370EOF
371
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200372 cat <<-EOF >> ${CONFIG}
373 salt:
374 master:
375 accept_policy: open_mode
376 source:
377 engine: $SALT_SOURCE
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200378 minion:
379 source:
380 engine: $SALT_SOURCE
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200381 # ########
382 # vim: ft=yaml sw=2 ts=2 sts=2
383EOF
384 fi
Petr Michalec24b30e82017-08-21 10:59:18 +0200385
386 log_debug "Salt Master node config yaml:"
387 log_debug "$(cat ${CONFIG})"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200388}
389
390configure_salt_minion()
391{
392 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
393 cat <<-EOF > /etc/salt/minion.d/minion.conf
Petr Michaleca7de6df2017-08-31 12:16:57 +0200394 master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200395 id: $MINION_ID
396 EOF
397}
398
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200399install_reclass()
400{
401 VERSION=${1:-$RECLASS_VERSION}
Petr Michalec372fa032017-11-15 17:36:19 +0100402 VERSION=${VERSION:-pkg}
Petr Michalec800dd072017-11-15 17:19:26 +0100403 case ${VERSION} in
404 pkg|package)
405 which reclass || $SUDO $PKGTOOL install -y reclass
rootb9e5d7c2018-03-17 12:12:44 +0000406 which reclass-salt || {
407 if [ -e /usr/share/reclass/reclass-salt ]; then
Petr Michalec6ba1c362018-03-17 15:01:51 +0100408 ln -fs /usr/share/reclass/reclass-salt /usr/bin
rootb9e5d7c2018-03-17 12:12:44 +0000409 fi
410 }
Petr Michalec800dd072017-11-15 17:19:26 +0100411 ;;
412 *)
Petr Michalec981f3cc2017-11-15 17:40:31 +0100413 log_warn "Install development version of reclass"
Petr Michalec1b37d3d2017-11-15 18:20:46 +0100414 # Note: dev/git/pip version...
415 # It replaces all local reclass versions on system
416 # Required for reclass/git features:
417 # $SUDO $PKGTOOL install -y libffi-dev libgit2-dev || true
Petr Michalec800dd072017-11-15 17:19:26 +0100418 for s in $(python -c "import site; print(' '.join(site.getsitepackages()))"); do
Petr Michalec23020832017-11-15 17:34:49 +0100419 sudo -H pip install --install-option="--prefix=" --upgrade --force-reinstall -I \
Petr Michalec800dd072017-11-15 17:19:26 +0100420 -t "$s" git+https://github.com/salt-formulas/reclass.git@${VERSION};
421 done
422 ;;
423 esac
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200424}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200425
426install_salt_master_pkg()
427{
428 echo -e "\nPreparing base OS repository ...\n"
429
430 configure_pkg_repo
431
432 echo -e "\nInstalling salt master ...\n"
433
Vladimir Khlyunev60395882021-07-14 16:23:45 +0400434 # vkhlyunev: Gerrit returns file only in base64 encode, but there are other places (like offline image)
435 # that uses common plain-text files - ugly "solution" below
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200436 case $PLATFORM_FAMILY in
437 debian)
438 $SUDO apt-get install -y git
Vladimir Khlyunev60395882021-07-14 16:23:45 +0400439 ( curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d ) && (
440 curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null ) || (
441 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null )
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200442 ;;
443 rhel)
444 yum install -y git
Vladimir Khlyunev60395882021-07-14 16:23:45 +0400445 ( curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d ) && (
446 curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null ) || (
447 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null )
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200448 ;;
449 esac
Jakub Josef0b611742018-01-09 17:02:01 +0100450
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200451 configure_salt_master
452
453 echo -e "\nRestarting services ...\n"
454 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
455 $SVCTOOL salt-master restart
456}
457
458install_salt_master_pip()
459{
460 echo -e "\nPreparing base OS repository ...\n"
461
462 case $PLATFORM_FAMILY in
463 debian)
464 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200465 ;;
466 rhel)
467 $SUDO yum install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200468 ;;
469 esac
470
471 echo -e "\nInstalling salt master ...\n"
472 # TODO: replace with saltstack bootstrap script
Jakub Josef0b611742018-01-09 17:02:01 +0100473
Petr Michalec88a95a22018-01-09 17:21:08 +0100474 if [ -z ${PIP_SALT_VERSION} ] || [ "$PIP_SALT_VERSION" == "latest" ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200475 pip install salt
476 else
Petr Michalec88a95a22018-01-09 17:21:08 +0100477 pip install salt==${PIP_SALT_VERSION}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200478 fi
479
480 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
481 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
482
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200483 configure_salt_master
484
485 echo -e "\nRestarting services ...\n"
486 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
487 $SVCTOOL salt-master restart
488}
489
490
491
492install_salt_minion_pkg()
493{
494
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200495 echo -e "\nInstalling salt minion ...\n"
496
Vladimir Khlyunev60395882021-07-14 16:23:45 +0400497 # vkhlyunev: Gerrit returns file only in base64 encode, but there are other places (like offline image)
498 # that uses common plain-text files - ugly "solution" below
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200499 case $PLATFORM_FAMILY in
500 debian)
Vladimir Khlyunev60395882021-07-14 16:23:45 +0400501 ( curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d) && (
502 curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null ) || (
503 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null )
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200504 ;;
505 rhel)
Vladimir Khlyunev60395882021-07-14 16:23:45 +0400506 ( curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d) && (
507 curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null ) || (
508 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null )
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200509 ;;
510 esac
511
512
513 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200514 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200515}
516
517install_salt_minion_pip()
518{
519 echo -e "\nInstalling salt minion ...\n"
520
521 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
522 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
523
524 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200525 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200526}
527
azvyagintseve93f8372018-09-12 16:50:58 +0300528function install_salt_formula_pkg_all(){
529 log_warn "Installing and configuring ALL formulas ..."
530 if ! $SUDO apt-get install -y 'salt-formula-*'; then
531 echo -e "\nInstall ALL formulas by mask 'salt-formula-*' failed.\n"
532 exit 1
533 fi
534 for formula_service in $(ls ${FORMULAS_PATH}/reclass/service/); do
535 #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
536 formula_service=${formula_service//-/$'_'}
537 if [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ]; then
538 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
539 fi
540 done
541}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200542
543install_salt_formula_pkg()
544{
545 configure_pkg_repo
546
547 case $PLATFORM_FAMILY in
548 debian)
549 echo "Configuring necessary formulas ..."
550
551 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
azvyagintseva7a91d52018-09-13 12:58:39 +0300552 if [[ ${EXTRA_FORMULAS_PKG_ALL} =~ ^(True|true|1|yes)$ ]]; then
azvyagintseve93f8372018-09-12 16:50:58 +0300553 install_salt_formula_pkg_all
554 return
555 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200556 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100557 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200558 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
559 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
560 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
561 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
562 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
563 exit 1
564 fi
Oleksii Grudev691d4712018-04-02 10:23:46 +0300565 #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
566 formula_service=${formula_service//-/$'_'}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200567 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
568 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
569 done
570 ;;
571 rhel)
572 # TODO
573 ;;
574 esac
575
576 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
577 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
578}
579
580install_salt_formula_git()
581{
582 echo "Configuring necessary formulas ..."
583
584 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
585 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100586 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200587 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
588 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
589 _BRANCH=${FORMULAS_BRANCH}
590 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
591 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
592 # Fallback to the master branch if the branch doesn't exist for this repository
593 _BRANCH=master
594 fi
595 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
596 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
597 exit 1
598 fi
599 } || {
600 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
601 git fetch origin/${_BRANCH} || git fetch --all
602 git checkout ${_BRANCH} && git pull || git pull;
603 cd -
604 }
605 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
606 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
607 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
608 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
609 done
610
611 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
612 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
613}
614
Petr Michalece9c84522017-12-13 17:14:08 +0100615saltservice_stop() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100616 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100617 $SUDO service salt-minion stop
618 $SUDO service salt-master stop
619 sleep ${SALT_STOPSTART_WAIT:-30}
Petr Michalec7822efe2017-12-15 16:19:32 +0100620 ${SUDO} pkill -9 salt-master && ${SUDO} rm -rf /var/run/salt/master/* || true
Petr Michalece9c84522017-12-13 17:14:08 +0100621 ${SUDO} pkill -9 salt-minion
622}
623saltservice_start() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100624 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100625 $SUDO service salt-master start
626 $SUDO service salt-minion start
627 sleep ${SALT_STOPSTART_WAIT:-30}
628}
629
630saltservice_restart() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100631 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100632 saltservice_stop
633 saltservice_start
634}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200635
636saltmaster_bootstrap() {
637
638 log_info "Salt master setup"
639 test -n "$MASTER_HOSTNAME" || exit 1
640
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200641 # override some envs from cluster level *.env, use with care
642 source_local_envs
643
Petr Michalece9c84522017-12-13 17:14:08 +0100644 saltservice_stop
645
646 clone_reclass
647
projoke3c30e7d2017-10-19 17:15:40 +0800648 SCRIPTS=$(dirname $0)
Petr Michalece9c84522017-12-13 17:14:08 +0100649
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200650 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
Adam Tengler035f24f2017-09-11 19:29:31 +0200651 export MASTER_IP=${MASTER_IP:-127.0.0.1}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200652 export MINION_ID=${MASTER_HOSTNAME}
653 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
654 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
655 fi
656 # call local "setup() master"
657 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
658 if ! setup master; then
659 #cat /tmp/salt-master-setup.log
660 log_err "salt master setup() failed."
661 exit 1
662 else
663 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
664 fi
665 }
666
Petr Michalec981f3cc2017-11-15 17:40:31 +0100667 log_info "Install reclass"
668 install_reclass ${RECLASS_VERSION/dev*/develop}
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200669
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200670 log_info "Re/starting salt services"
Petr Michalece9c84522017-12-13 17:14:08 +0100671 saltservice_restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200672}
673
674# Init salt master
675saltmaster_init() {
676
677 log_info "Runing saltmaster states"
678 test -n "$MASTER_HOSTNAME" || exit 1
679
680 set -e
681 $SUDO salt-call saltutil.sync_all >/dev/null
682
Petr Michalecda014202017-08-18 11:26:07 +0200683 # workarond isolated and not fully bootstraped environments
Petr Michalec2801acb2017-10-31 15:00:15 +0100684 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec54ee4082017-11-30 17:02:17 +0100685 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 +0100686 fi
Petr Michalec8ce8a652017-11-30 15:39:20 +0100687 PILLAR='{'${SALT_MASTER_PILLAR}' "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalecda014202017-08-18 11:26:07 +0200688
Petr Michalec8b92d552017-11-29 12:28:50 +0100689 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100690 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100691 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
azvyagintseve3f6f402018-08-23 17:21:49 +0300692 if [[ $SA_IGNORE_ERROR_SALT_MASTER_ENV =~ ^(False|false|0|no)$ ]]; then
693 exit 1
694 fi
695
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200696 fi
697
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200698 # Revert temporary SaltMaster minimal configuration, if any
699 pushd $RECLASS_ROOT
700 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
Petr Michalec87778fc2017-11-02 10:13:25 +0100701 PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200702 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200703 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200704 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
705 git checkout -- $RECLASS_ROOT/nodes || true
706 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
Petr Michalec7edf8322017-11-30 12:01:09 +0100707 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100708 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100709 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
azvyagintseve3f6f402018-08-23 17:21:49 +0300710 if [[ $SA_IGNORE_ERROR_SALT_MASTER_ENV =~ ^(False|false|0|no)$ ]]; then
711 exit 1
712 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200713 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200714 fi
715 popd
716
Petr Michalecda014202017-08-18 11:26:07 +0200717 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
718 log_info "State: salt.master.service"
Jakub Josef32563412018-01-17 17:55:05 +0100719 # ensure salt master is started
720 saltservice_start
Petr Michalec9fec8b32017-10-05 14:46:42 +0200721 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalec4f043772017-12-19 15:40:54 +0100722 saltservice_start
Petr Michalecda014202017-08-18 11:26:07 +0200723
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200724 log_info "State: salt.master.storage.node"
725 set +e
Petr Michalec7edf8322017-11-30 12:01:09 +0100726 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
727 # FIXME: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec9fec8b32017-10-05 14:46:42 +0200728 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200729 ret=$?
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200730
Petr Michalecdd646db2017-12-18 15:56:18 +0100731 set -e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200732 if [[ $ret -eq 2 ]]; then
733 log_err "State reclass.storage.node failed with exit code 2 but continuing."
734 elif [[ $ret -ne 0 ]]; then
735 log_err "State reclass.storage.node failed with exit code $ret"
736 exit 1
737 fi
738
Petr Michalecdd646db2017-12-18 15:56:18 +0100739 set +e
740 log_info "Updating minion.conf -> master: localhost"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200741 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
Petr Michalecdd646db2017-12-18 15:56:18 +0100742
743 log_info "Re/starting salt services" # in order to load new deployed configuration from model
Petr Michalece9c84522017-12-13 17:14:08 +0100744 saltservice_restart
Petr Michalecdd646db2017-12-18 15:56:18 +0100745
746 log_info "Salt Util sync all"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200747 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
748
749 verify_salt_master
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200750
751}
752
Petr Michalec71e32682018-03-22 08:34:34 +0100753## CI Workarounds
754
755function mockup_node_registration() {
756
757 # for dynamic nodes registrations (require github.com/salt-formulas/salt-formula-reclass)
758
759 source /etc/os-release
760 os_codename=$VERSION_CODENAME
761 domain=$(hostname -d)
762 master_ip=$(hostname -I | awk '{print $1}')
763 fake_ip_preffix=$(echo $master_ip | awk -F. '{print $1"."$2}')
764 fake_ip_base=10
765
766 # SHOULD BE ALREADY RUN AS A PART OF BOOTSTRAP
767 # sync, in order to load custom modules
768 #salt-call saltutil.sync_all
769
770 # SHOULD BE ALREADY RUN AS A PART OF BOOTSTRAP
771 #PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
772 #salt-call state.apply reclass.storage.node pillar="$PILLAR" > /dev/null 2>/dev/null || true
773
774 # rotate over dynamic hosts
775 for host_idx in {01..02};do
776 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
777 hostname=${host}${host_idx}
778 fake_ip_base=$(($fake_ip_base + 1))
779
780 node_network01_ip="$fake_ip_preffix.11.$fake_ip_base"
781 node_network02_ip="$fake_ip_preffix.12.$fake_ip_base"
782 node_network03_ip="$fake_ip_preffix.13.$fake_ip_base"
783 node_network04_ip="$fake_ip_preffix.14.$fake_ip_base"
784 node_network05_ip="$fake_ip_preffix.15.$fake_ip_base"
785 node_network01_iface=$(ls /sys/class/net/ | grep -v lo | sort | head -n1)
786 node_network02_iface="eth1"
787 node_network03_iface="eth2"
788 node_network04_iface="eth3"
789 node_network05_iface="eth4"
790
791 declare -A vars
792 vars=(
793 ["node_master_ip"]=
794 ["node_os"]=${os_codename}
795 ["node_deploy_ip"]=${node_network01_ip}
796 ["node_deploy_iface"]=${node_network01_iface}
797 ["node_control_ip"]=${node_network02_ip}
798 ["node_control_iface"]=${node_network02_iface}
799 ["node_tenant_ip"]=${node_network03_ip}
800 ["node_tenant_iface"]=${node_network03_iface}
801 ["node_external_ip"]=${node_network04_ip}
802 ["node_external_iface"]=${node_network04_iface}
803 ["node_baremetal_ip"]=${node_network05_ip}
804 ["node_baremetal_iface"]=${node_network05_iface}
805 ["node_domain"]=$domain
806 ["node_cluster"]=$(hostname -d |awk -F. '{print $1}')
807 ["node_hostname"]=$hostname
808 )
809
810 data=""; i=0
811 for key in "${!vars[@]}"; do
812 data+="\"${key}\": \"${vars[${key}]}\""
813 i=$(($i+1))
814 if [ $i -lt ${#vars[@]} ]; then
815 data+=", "
816 fi
817 done
818 echo "Classifying node $hostname"
819 NODECR='"node_name": "'${hostname}.${domain}'", "node_data": {'$data'}'
820 PILLAR='{'${NODECR}', "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
821 salt-call state.apply reclass.reactor_sls.node_register pillar="$PILLAR" #-l info
822 #salt-call event.send "reclass/minion/classify" "{$data}"
823 done
824 done
825
826}
827
828## VERIFY
829
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200830
831function verify_salt_master() {
832 set -e
833
834 log_info "Verify Salt master"
835 test -n "$MASTER_HOSTNAME" || exit 1
836
837 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200838 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
839 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200840 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
841 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
842 $SUDO salt-call --no-color grains.items
843 $SUDO salt-call --no-color pillar.data
844 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200845 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec6e40efc2018-03-22 09:47:47 +0100846 if ! $SUDO python -m reclass.cli --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200847 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
848 exit 1
849 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100850 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200851}
852
853function verify_salt_minion() {
854 set -e
855 node=$1
856 log_info "Verifying ${node}"
857 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
858 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
859 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
860 fi
Petr Michalec6e40efc2018-03-22 09:47:47 +0100861 if ! $SUDO python -m reclass.cli --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200862 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
863 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
864 exit 1
865 fi
866 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100867 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200868}
869
870function verify_salt_minions() {
871 #set -e
Petr Michalec679f15b2017-09-18 16:16:29 +0200872 NODES=$(find $RECLASS_ROOT/nodes/_generated/ -name "*.yml" | grep -v "cfg")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200873 log_info "Verifying minions: $(echo ${NODES}|xargs)"
874
875 # Parallel
876 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
877 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
878
879 function filterFails() {
880 grep -v '/grains' | tee -a $1 | tail -n20
881 }
882
883 log_info "Verify nodes"
884 passed=0
885 for node in ${NODES}; do
886 node=$(basename $node .yml)
887
888 # filter first in cluster.. ctl-01, mon-01, etc..
889 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
890 verify_salt_minion ${node} || continue
891 else
892 echo Skipped $node.
893 fi
894 passed=$(($passed+1))
895 done
896 # fail on failures
897 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
898 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
899 test ! $passed -lt $total || {
900 tail -n50 /tmp/*.pillar_verify
901 return 1
902 }
Petr Michalec7edf8322017-11-30 12:01:09 +0100903 #set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200904}
905
906
907
908##########################################
909# To install salt master/minon
910
911function install() {
912 setup $@
913}
914
915function setup() {
916 # CLI
917 while [ x"$1" != x"" ]; do
918 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
919
920 case $1 in
921 master )
922 install_salt_master_$SALT_SOURCE
923 install_salt_minion_$SALT_SOURCE
924 install_salt_formula_$FORMULAS_SOURCE
925 ;;
926 minion )
927 install_salt_minion_$SALT_SOURCE
928 ;;
929 esac
930 shift
931 done
932 echo DONE
933}
934
935function bootstrap() {
936 log_info "Bootstrap & verification of SaltMaster and configured minions."
937 trap _atexit INT TERM EXIT
938
939 system_config_master
940 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200941 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200942 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200943}
944
945function default() {
946 bootstrap $@
947}
948
949
950##########################################
azvyagintseve93f8372018-09-12 16:50:58 +0300951log_info "Running version: ${__ScriptVersion}"
952log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'"
953
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200954[[ "$0" != "$BASH_SOURCE" ]] || {
955# unless file is being sourced
956 default $@
957}