blob: a6e18cd79c6c2499eb47fe69531f8a7b06d4a3ce [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
9# TODO:
10# - use PPA repository as formula source
11# - support for spm/yum
12
13
14# Source specific env vars.
15# shopt -u dotglob
16export RECLASS_ROOT=${RECLASS_ROOT:-/srv/salt/reclass}
17function source_local_envs() {
Petr Michalecb444ef92017-08-17 13:53:14 +020018 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 +030019 for f in $(find $path -maxdepth 1 -name '*.env' 2> /dev/null); do
20 echo "Sourcing env variables from $f"
21 source $f
22 done
23 done
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020024}
25source_local_envs
26
27##########################################
28# Set defaults env variables
29
30if [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
31 set -x
32 SALT_LOG_LEVEL="--state-verbose=true -ldebug"
33fi
34
35export MAGENTA='\033[0;95m'
36export YELLOW='\033[1;33m'
37export BLUE='\033[0;35m'
38export CYAN='\033[0;96m'
39export RED='\033[0;31m'
40export NC='\033[0m' # No Color'
41
42export LC_ALL=C
43export SALT_LOG_LEVEL="--state-verbose=false -lerror"
44export SALT_OPTS="${SALT_OPTS:- --timeout=120 --state-output=changes --retcode-passthrough --force-color $SALT_LOG_LEVEL }"
45export SALT_STATE_RETRY=${SALT_STATE_RETRY:-3}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020046
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020047# salt apt repository
48test -e /etc/lsb-release && eval $(cat /etc/lsb-release)
49which lsb_release && DISTRIB_CODENAME=${DISTRIB_CODENAME:-$(lsb_release -cs)}
50#
Dmitry Ukov95492a02017-10-23 11:34:45 +040051export APT_REPOSITORY=${APT_REPOSITORY:- deb [arch=amd64] http://apt.mirantis.com/${DISTRIB_CODENAME} ${DISTRIB_REVISION:-stable} salt}
chnyda30c07a62017-09-20 14:28:02 +020052export APT_REPOSITORY_GPG=${APT_REPOSITORY_GPG:-http://apt.mirantis.com/public.gpg}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020053# reclass
54export RECLASS_ADDRESS=${RECLASS_ADDRESS:-https://github.com/salt-formulas/openstack-salt.git} # https/git
ibumarskovf72e05e2017-11-29 14:37:48 +030055export RECLASS_BRANCH=${RECLASS_BRANCH:-master}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020056
57# formula
58export FORMULAS_BASE=${FORMULAS_BASE:-https://github.com/salt-formulas}
59export FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas}
60export FORMULAS_BRANCH=${FORMULAS_BRANCH:-master}
61export FORMULAS_SOURCE=${FORMULAS_SOURCE:-pkg} # pkg/git
Petr Michalec515356f2017-11-16 11:20:20 +010062# Essential set of formulas (known to by used on cfg01 node for most setups during bootsrap)
63# DEPRECATED: FORMULAS_SALT_MASTER=${FORMULAS_SALT_MASTER:- $EXTRA_FORMULAS maas memcached ntp nginx collectd sensu heka sphinx mysql grafana libvirt rsyslog glusterfs postfix xtrabackup freeipa prometheus telegraf elasticsearch kibana rundeck devops-portal rsync docker keepalived aptly jenkins gerrit artifactory influxdb horizon}
Petr Michalece07cd0b2017-11-15 11:41:57 +010064FORMULAS_SALT_MASTER=${FORMULAS_SALT_MASTER:- $EXTRA_FORMULAS}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020065# minimal set of formulas for salt-master bootstrap
Petr Michalec515356f2017-11-16 11:20:20 +010066declare -a FORMULAS_SALT_MASTER=(reclass salt git openssh linux $(echo $FORMULAS_SALT_MASTER))
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020067export FORMULAS_SALT_MASTER
68
69# system / host
Petr Michalecc749e862017-09-06 21:10:10 +020070export HOSTNAME=${HOSTNAME:-`hostname -s`}
71export HOSTNAME=${HOSTNAME//.*/}
72export DOMAIN=${DOMAIN:-`hostname -d`}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020073export DOMAIN=${DOMAIN:-bootstrap.local}
74
75# salt
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020076export MINION_ID=${MINION_ID:-${HOSTNAME}.${DOMAIN}}
Petr Michalec2e3a4992017-08-17 14:20:43 +020077export MASTER_HOSTNAME=${MASTER_HOSTNAME:-${HOSTNAME}.${DOMAIN}}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020078
79# saltstack
Petr Michalec33c04892017-09-08 15:10:30 +020080BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True}
alexz0adc2712018-02-11 17:33:52 +010081BOOTSTRAP_SALTSTACK_COM=${BOOTSTRAP_SALTSTACK_COM:-"https://bootstrap.saltstack.com"}
Petr Michalece11cf612017-09-08 18:21:48 +020082BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION:- stable 2016.3 }
Jakub Josef0b611742018-01-09 17:02:01 +010083BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION//latest*/stable}
alexz0adc2712018-02-11 17:33:52 +010084# TODO add 'r' option by default
85# Currently, without 'r' option, upstream saltstack repos will be used. Otherwise
86# local one should be used, from http://apt.mirantis.com/ or whatever.
Petr Michalec33c04892017-09-08 15:10:30 +020087BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX $BOOTSTRAP_SALTSTACK_VERSION }
Petr Michalece11cf612017-09-08 18:21:48 +020088SALT_SOURCE=${SALT_SOURCE:-pkg}
89# the version below is used salt pillar data
90SALT_VERSION=${SALT_VERSION:-latest}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020091
92# environment
93if [ "$FORMULAS_SOURCE" == "git" ]; then
94 SALT_ENV=${SALT_ENV:-dev}
95elif [ "$FORMULAS_SOURCE" == "pkg" ]; then
96 SALT_ENV=${SALT_ENV:-prd}
97fi
Petr Michalec05b52452017-09-08 14:26:59 +020098eval "$(grep -h '=' /etc/*release 2> /dev/null)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +020099PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z)
100case $PLATFORM_FAMILY in
101 debian )
102 PKGTOOL="$SUDO apt-get"
103 test ${VERSION_ID//\.*/} -ge 16 && {
104 SVCTOOL=service
105 } || { SVCTOOL=service
106 }
107 ;;
108 rhel )
109 PKGTOOL="$SUDO yum"
110 test ${VERSION_ID//\.*/} -ge 7 && {
111 SVCTOOL=systemctl
112 } || { SVCTOOL=service
113 }
114 ;;
115esac
116
117export PLATFORM_FAMILY
118export PKGTOOL
119export SVCTOOL
120
121##########################################
122# FUNCTIONS
123
124log_info() {
125 echo -e "${YELLOW}[INFO] $* ${NC}"
126}
127
128log_warn() {
129 echo -e "${MAGENTA}[WARN] $* ${NC}"
130}
131
Petr Michalec24b30e82017-08-21 10:59:18 +0200132log_debug() {
133 echo -e "${CYAN}[WARN] $* ${NC}"
134}
135
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200136log_err() {
137 echo -e "${RED}[ERROR] $* ${NC}" >&2
138}
139
140configure_pkg_repo()
141{
142
143 case $PLATFORM_FAMILY in
144 debian)
145 if [ -n "$APT_REPOSITORY_PPA" ]; then
146 which add-apt-repository || $SUDO apt-get install -y software-properties-common
147 $SUDO add-apt-repository -y ppa:${APT_REPOSITORY_PPA}
148 else
Mykyta Karpinfbcb5482017-10-04 14:15:25 +0300149 echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/mcp_salt.list >/dev/null
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200150 curl -sL $APT_REPOSITORY_GPG | $SUDO apt-key add -
151 fi
152 $SUDO apt-get clean
153 $SUDO apt-get update
154 ;;
155 rhel)
156 $SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm
157 $SUDO yum clean all
158 ;;
159 esac
160
161}
162
163_atexit() {
164 RETVAL=$?
165 trap true INT TERM EXIT
166
167 if [ $RETVAL -ne 0 ]; then
168 log_err "Execution failed"
169 else
170 log_info "Execution successful"
171 fi
172 return $RETVAL
173}
174
175retry() {
176 local tries
177 if [[ $1 =~ ^[0-9]+$ ]]; then
178 tries=$1; shift
179 else
180 tries=3
181 fi
Petr Michalec262e3ad2017-10-06 13:41:58 +0200182 ret=1
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200183 for i in $(seq 1 $tries); do
Petr Michalec262e3ad2017-10-06 13:41:58 +0200184 "$@" && return $? || ret=$?
185 sleep $i
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200186 done
Petr Michalec9fec8b32017-10-05 14:46:42 +0200187 return $ret
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200188}
189
190function clone_reclass() {
Petr Michaleca361d4e2017-09-08 11:38:16 +0200191 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200192 # No reclass at all, clone from given address
alexz0adc2712018-02-11 17:33:52 +0100193 # In case, non-github repo
194 _tmp_host=$(echo ${RECLASS_ADDRESS} | awk -F/ '{print $3}')
195 ssh-keyscan -T 1 -H ${_tmp_host} >> ~/.ssh/known_hosts || true
196 # But, awk is not perfect solution, so lets make double-check for github
197 ssh-keyscan -T 1 -H github.com >> ~/.ssh/known_hosts || true
ibumarskovf72e05e2017-11-29 14:37:48 +0300198 if echo ${RECLASS_BRANCH} | egrep -q "^refs"; then
199 git clone ${RECLASS_ADDRESS} ${RECLASS_ROOT}
200 cd ${RECLASS_ROOT}
201 git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH} && git checkout FETCH_HEAD
202 export RECLASS_REVISION=$(git rev-parse HEAD)
203 cd -
204 else
205 git clone -b ${RECLASS_BRANCH} ${RECLASS_ADDRESS} ${RECLASS_ROOT};
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200206 fi;
207 fi;
Petr Michalec679f15b2017-09-18 16:16:29 +0200208 if [ ! -d ${RECLASS_ROOT}/classes ]; then
Petr Michaleca361d4e2017-09-08 11:38:16 +0200209 log_err "Reclass ${RECLASS_ROOT} is not fetched locally;"
210 ls -Rla ${RECLASS_ROOT}
211 exit 1
212 fi;
213 $SUDO mkdir -p $RECLASS_ROOT/classes/service
214 $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200215}
216
217
218##########################################
219# Main calls
220
Petr Michalecda014202017-08-18 11:26:07 +0200221system_config_ssh_conf() {
222 for conf in ~/.ssh/config /root/.ssh/config; do
223 $SUDO mkdir -p $(dirname $conf)
224 if ! grep StrictHostKeyChecking $conf; then
225 # this should be used only in CI environment
226 echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee $conf >/dev/null
227 fi
228 done
229 if ! grep github.com ~/.ssh/known_hosts; then
230 ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true
231 fi
232}
233
234system_config_salt_modules_prereq() {
235 # salt-formulas custom modules dependencies, etc:
236 $SUDO $PKGTOOL install -y iproute2 curl sudo apt-transport-https python-psutil python-apt python-m2crypto python-oauth python-pip &>/dev/null
237}
238
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200239system_config_minion() {
240 log_info "System configuration salt minion"
241}
242
243system_config_master() {
244 log_info "System configuration salt master"
245
Petr Michalecda014202017-08-18 11:26:07 +0200246 system_config_salt_modules_prereq
247 system_config_ssh_conf
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200248
Petr Michalecda014202017-08-18 11:26:07 +0200249 if ! grep '127.0.1.2.*salt' /etc/hosts; then
250 echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null
251 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200252
253 which reclass || $SUDO $PKGTOOL install -y reclass
254
255 which reclass-salt || {
256 test -e /usr/share/reclass/reclass-salt && {
257 ln -fs /usr/share/reclass/reclass-salt /usr/bin
258 }
259 }
260}
261
262configure_salt_master()
263{
264
265 echo "Configuring salt-master ..."
266
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200267 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec29d3ea32017-10-31 17:22:36 +0100268 read -d '' IGNORE_CLASS_NOTFOUND <<-EOF
269 ignore_class_notfound: True
270 ignore_class_regexp:
271 - 'service.*'
272EOF
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200273 fi
274
Petr Michalecdd1a0472017-09-06 19:27:24 +0200275 # to force alternative reclass module path
Petr Michalecd9d0a322017-09-06 19:38:32 +0200276 if [ -n "$RECLASS_SOURCE_PATH" ]; then
277 RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}"
278 else
279 RECLASS_SOURCE_PATH=""
280 fi
Petr Michalecdd1a0472017-09-06 19:27:24 +0200281
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200282 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
283 cat <<-EOF > /etc/salt/master.d/master.conf
284 file_roots:
285 base:
286 - /usr/share/salt-formulas/env
287 prd:
288 - /srv/salt/env/prd
289 dev:
290 - /srv/salt/env/dev
291 pillar_opts: False
292 open_mode: True
293 reclass: &reclass
294 storage_type: yaml_fs
295 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200296 ${IGNORE_CLASS_NOTFOUND}
Petr Michalecdd1a0472017-09-06 19:27:24 +0200297 ${RECLASS_SOURCE_PATH}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200298 ext_pillar:
299 - reclass: *reclass
300 master_tops:
301 reclass: *reclass
302EOF
303
304 echo "Configuring reclass ..."
305
306 [ ! -d /etc/reclass ] && mkdir /etc/reclass
307 cat <<-EOF > /etc/reclass/reclass-config.yml
308 storage_type: yaml_fs
309 pretty_print: True
310 output: yaml
311 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200312EOF
313
314 clone_reclass
315 # override some envs from cluster level *.env, use with care
316 source_local_envs
317
318 cd ${RECLASS_ROOT}
319 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
320 # Possibly subrepo checkout needed
321 git submodule update --init --recursive
322 fi
323
Petr Michalec212cc6a2017-08-17 21:39:40 +0200324 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec51ece842017-09-06 20:44:15 +0200325 rm -rvf ${RECLASS_ROOT}/nodes/_generated/*
326
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200327 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200328 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200329 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
Petr Michalec24b30e82017-08-21 10:59:18 +0200330 log_warn "Salt Master node specification has not been found in model."
331 log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200332 cat <<-EOF > ${CONFIG}
333 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200334 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200335 parameters:
336 _param:
Petr Michaleca7de6df2017-08-31 12:16:57 +0200337 salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200338 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200339 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
ibumarskovf72e05e2017-11-29 14:37:48 +0300340 reclass_data_revision: ${RECLASS_REVISION:-$RECLASS_BRANCH}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200341 reclass_data_repository: "$RECLASS_ADDRESS"
Petr Michaleca7de6df2017-08-31 12:16:57 +0200342 reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200343 linux_system_codename: ${DISTRIB_CODENAME}
344 cluster_name: ${CLUSTER_NAME}
345 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200346 linux:
347 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200348 name: ${HOSTNAME:-cfg01}
349 domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200350 # ########
351EOF
352
353 if [ "$SALT_VERSION" == "latest" ]; then
354 VERSION=""
355 else
356 VERSION="version: $SALT_VERSION"
357 fi
358
359 cat <<-EOF >> ${CONFIG}
360 salt:
361 master:
362 accept_policy: open_mode
363 source:
364 engine: $SALT_SOURCE
365 $VERSION
366 minion:
367 source:
368 engine: $SALT_SOURCE
369 $VERSION
370 # ########
371 # vim: ft=yaml sw=2 ts=2 sts=2
372EOF
373 fi
Petr Michalec24b30e82017-08-21 10:59:18 +0200374
375 log_debug "Salt Master node config yaml:"
376 log_debug "$(cat ${CONFIG})"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200377}
378
379configure_salt_minion()
380{
381 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
382 cat <<-EOF > /etc/salt/minion.d/minion.conf
Petr Michaleca7de6df2017-08-31 12:16:57 +0200383 master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200384 id: $MINION_ID
385 EOF
386}
387
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200388install_reclass()
389{
390 VERSION=${1:-$RECLASS_VERSION}
Petr Michalec372fa032017-11-15 17:36:19 +0100391 VERSION=${VERSION:-pkg}
Petr Michalec800dd072017-11-15 17:19:26 +0100392 case ${VERSION} in
393 pkg|package)
394 which reclass || $SUDO $PKGTOOL install -y reclass
395 ;;
396 *)
Petr Michalec981f3cc2017-11-15 17:40:31 +0100397 log_warn "Install development version of reclass"
Petr Michalec1b37d3d2017-11-15 18:20:46 +0100398 # Note: dev/git/pip version...
399 # It replaces all local reclass versions on system
400 # Required for reclass/git features:
401 # $SUDO $PKGTOOL install -y libffi-dev libgit2-dev || true
Petr Michalec800dd072017-11-15 17:19:26 +0100402 for s in $(python -c "import site; print(' '.join(site.getsitepackages()))"); do
Petr Michalec23020832017-11-15 17:34:49 +0100403 sudo -H pip install --install-option="--prefix=" --upgrade --force-reinstall -I \
Petr Michalec800dd072017-11-15 17:19:26 +0100404 -t "$s" git+https://github.com/salt-formulas/reclass.git@${VERSION};
405 done
406 ;;
407 esac
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200408}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200409
410install_salt_master_pkg()
411{
412 echo -e "\nPreparing base OS repository ...\n"
413
414 configure_pkg_repo
415
416 echo -e "\nInstalling salt master ...\n"
417
418 case $PLATFORM_FAMILY in
419 debian)
420 $SUDO apt-get install -y git
alexz0adc2712018-02-11 17:33:52 +0100421 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200422 ;;
423 rhel)
424 yum install -y git
alexz0adc2712018-02-11 17:33:52 +0100425 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200426 ;;
427 esac
Jakub Josef0b611742018-01-09 17:02:01 +0100428
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200429 which reclass-salt || {
430 test -e /usr/share/reclass/reclass-salt && {
431 ln -fs /usr/share/reclass/reclass-salt /usr/bin
432 }
433 }
434
435 configure_salt_master
436
437 echo -e "\nRestarting services ...\n"
438 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
439 $SVCTOOL salt-master restart
440}
441
442install_salt_master_pip()
443{
444 echo -e "\nPreparing base OS repository ...\n"
445
446 case $PLATFORM_FAMILY in
447 debian)
448 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200449 ;;
450 rhel)
451 $SUDO yum install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200452 ;;
453 esac
454
455 echo -e "\nInstalling salt master ...\n"
456 # TODO: replace with saltstack bootstrap script
Jakub Josef0b611742018-01-09 17:02:01 +0100457
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200458 if [ "$SALT_VERSION" == "latest" ]; then
459 pip install salt
460 else
461 pip install salt==$SALT_VERSION
462 fi
463
464 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
465 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
466
467 which reclass-salt || {
468 test -e /usr/share/reclass/reclass-salt && {
469 ln -fs /usr/share/reclass/reclass-salt /usr/bin
470 }
471 }
472
473 configure_salt_master
474
475 echo -e "\nRestarting services ...\n"
476 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
477 $SVCTOOL salt-master restart
478}
479
480
481
482install_salt_minion_pkg()
483{
484
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200485 echo -e "\nInstalling salt minion ...\n"
486
487 case $PLATFORM_FAMILY in
488 debian)
alexz0adc2712018-02-11 17:33:52 +0100489 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200490 ;;
491 rhel)
alexz0adc2712018-02-11 17:33:52 +0100492 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200493 ;;
494 esac
495
496
497 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200498 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200499}
500
501install_salt_minion_pip()
502{
503 echo -e "\nInstalling salt minion ...\n"
504
505 curl -Lo /etc/init.d/salt-minion https://anonscm.debian.org/cgit/pkg-salt/salt.git/plain/debian/salt-minion.init && chmod 755 /etc/init.d/salt-minion
506 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
507
508 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200509 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200510}
511
512
513install_salt_formula_pkg()
514{
515 configure_pkg_repo
516
517 case $PLATFORM_FAMILY in
518 debian)
519 echo "Configuring necessary formulas ..."
520
521 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
522 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100523 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200524 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
525 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
526 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
527 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
528 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
529 exit 1
530 fi
531 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
532 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
533 done
534 ;;
535 rhel)
536 # TODO
537 ;;
538 esac
539
540 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
541 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
542}
543
544install_salt_formula_git()
545{
546 echo "Configuring necessary formulas ..."
547
548 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
549 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100550 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200551 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
552 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
553 _BRANCH=${FORMULAS_BRANCH}
554 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
555 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
556 # Fallback to the master branch if the branch doesn't exist for this repository
557 _BRANCH=master
558 fi
559 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
560 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
561 exit 1
562 fi
563 } || {
564 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
565 git fetch origin/${_BRANCH} || git fetch --all
566 git checkout ${_BRANCH} && git pull || git pull;
567 cd -
568 }
569 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
570 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
571 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
572 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
573 done
574
575 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
576 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
577}
578
Petr Michalece9c84522017-12-13 17:14:08 +0100579saltservice_stop() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100580 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100581 $SUDO service salt-minion stop
582 $SUDO service salt-master stop
583 sleep ${SALT_STOPSTART_WAIT:-30}
Petr Michalec7822efe2017-12-15 16:19:32 +0100584 ${SUDO} pkill -9 salt-master && ${SUDO} rm -rf /var/run/salt/master/* || true
Petr Michalece9c84522017-12-13 17:14:08 +0100585 ${SUDO} pkill -9 salt-minion
586}
587saltservice_start() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100588 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100589 $SUDO service salt-master start
590 $SUDO service salt-minion start
591 sleep ${SALT_STOPSTART_WAIT:-30}
592}
593
594saltservice_restart() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100595 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100596 saltservice_stop
597 saltservice_start
598}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200599
600saltmaster_bootstrap() {
601
602 log_info "Salt master setup"
603 test -n "$MASTER_HOSTNAME" || exit 1
604
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200605 # override some envs from cluster level *.env, use with care
606 source_local_envs
607
Petr Michalece9c84522017-12-13 17:14:08 +0100608 saltservice_stop
609
610 clone_reclass
611
projoke3c30e7d2017-10-19 17:15:40 +0800612 SCRIPTS=$(dirname $0)
Petr Michalece9c84522017-12-13 17:14:08 +0100613
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200614 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
Adam Tengler035f24f2017-09-11 19:29:31 +0200615 export MASTER_IP=${MASTER_IP:-127.0.0.1}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200616 export MINION_ID=${MASTER_HOSTNAME}
617 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
618 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
619 fi
620 # call local "setup() master"
621 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
622 if ! setup master; then
623 #cat /tmp/salt-master-setup.log
624 log_err "salt master setup() failed."
625 exit 1
626 else
627 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
628 fi
629 }
630
Petr Michalec981f3cc2017-11-15 17:40:31 +0100631 log_info "Install reclass"
632 install_reclass ${RECLASS_VERSION/dev*/develop}
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200633
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200634 log_info "Re/starting salt services"
Petr Michalece9c84522017-12-13 17:14:08 +0100635 saltservice_restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200636}
637
638# Init salt master
639saltmaster_init() {
640
641 log_info "Runing saltmaster states"
642 test -n "$MASTER_HOSTNAME" || exit 1
643
644 set -e
645 $SUDO salt-call saltutil.sync_all >/dev/null
646
Petr Michalecda014202017-08-18 11:26:07 +0200647 # workarond isolated and not fully bootstraped environments
Petr Michalec2801acb2017-10-31 15:00:15 +0100648 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec54ee4082017-11-30 17:02:17 +0100649 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 +0100650 fi
Petr Michalec8ce8a652017-11-30 15:39:20 +0100651 PILLAR='{'${SALT_MASTER_PILLAR}' "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalecda014202017-08-18 11:26:07 +0200652
Petr Michalec8b92d552017-11-29 12:28:50 +0100653 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100654 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100655 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200656 fi
657
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200658 # Revert temporary SaltMaster minimal configuration, if any
659 pushd $RECLASS_ROOT
660 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
Petr Michalec87778fc2017-11-02 10:13:25 +0100661 PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200662 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200663 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200664 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
665 git checkout -- $RECLASS_ROOT/nodes || true
666 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
Petr Michalec7edf8322017-11-30 12:01:09 +0100667 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100668 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100669 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200670 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200671 fi
672 popd
673
Petr Michalecda014202017-08-18 11:26:07 +0200674 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
675 log_info "State: salt.master.service"
Jakub Josef32563412018-01-17 17:55:05 +0100676 # ensure salt master is started
677 saltservice_start
Petr Michalec9fec8b32017-10-05 14:46:42 +0200678 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalec4f043772017-12-19 15:40:54 +0100679 saltservice_start
Petr Michalecda014202017-08-18 11:26:07 +0200680
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200681 log_info "State: salt.master.storage.node"
682 set +e
Petr Michalec7edf8322017-11-30 12:01:09 +0100683 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
684 # FIXME: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec9fec8b32017-10-05 14:46:42 +0200685 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200686 ret=$?
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200687
Petr Michalecdd646db2017-12-18 15:56:18 +0100688 set -e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200689 if [[ $ret -eq 2 ]]; then
690 log_err "State reclass.storage.node failed with exit code 2 but continuing."
691 elif [[ $ret -ne 0 ]]; then
692 log_err "State reclass.storage.node failed with exit code $ret"
693 exit 1
694 fi
695
Petr Michalecdd646db2017-12-18 15:56:18 +0100696 set +e
697 log_info "Updating minion.conf -> master: localhost"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200698 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
Petr Michalecdd646db2017-12-18 15:56:18 +0100699
700 log_info "Re/starting salt services" # in order to load new deployed configuration from model
Petr Michalece9c84522017-12-13 17:14:08 +0100701 saltservice_restart
Petr Michalecdd646db2017-12-18 15:56:18 +0100702
703 log_info "Salt Util sync all"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200704 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
705
706 verify_salt_master
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200707
708}
709
710
711function verify_salt_master() {
712 set -e
713
714 log_info "Verify Salt master"
715 test -n "$MASTER_HOSTNAME" || exit 1
716
717 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200718 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
719 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200720 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
721 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
722 $SUDO salt-call --no-color grains.items
723 $SUDO salt-call --no-color pillar.data
724 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200725 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200726 if ! $SUDO reclass --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
727 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
728 exit 1
729 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100730 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200731}
732
733function verify_salt_minion() {
734 set -e
735 node=$1
736 log_info "Verifying ${node}"
737 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
738 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
739 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
740 fi
741 if ! $SUDO reclass --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
742 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
743 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
744 exit 1
745 fi
746 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100747 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200748}
749
750function verify_salt_minions() {
751 #set -e
Petr Michalec679f15b2017-09-18 16:16:29 +0200752 NODES=$(find $RECLASS_ROOT/nodes/_generated/ -name "*.yml" | grep -v "cfg")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200753 log_info "Verifying minions: $(echo ${NODES}|xargs)"
754
755 # Parallel
756 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
757 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
758
759 function filterFails() {
760 grep -v '/grains' | tee -a $1 | tail -n20
761 }
762
763 log_info "Verify nodes"
764 passed=0
765 for node in ${NODES}; do
766 node=$(basename $node .yml)
767
768 # filter first in cluster.. ctl-01, mon-01, etc..
769 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
770 verify_salt_minion ${node} || continue
771 else
772 echo Skipped $node.
773 fi
774 passed=$(($passed+1))
775 done
776 # fail on failures
777 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
778 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
779 test ! $passed -lt $total || {
780 tail -n50 /tmp/*.pillar_verify
781 return 1
782 }
Petr Michalec7edf8322017-11-30 12:01:09 +0100783 #set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200784}
785
786
787
788##########################################
789# To install salt master/minon
790
791function install() {
792 setup $@
793}
794
795function setup() {
796 # CLI
797 while [ x"$1" != x"" ]; do
798 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
799
800 case $1 in
801 master )
802 install_salt_master_$SALT_SOURCE
803 install_salt_minion_$SALT_SOURCE
804 install_salt_formula_$FORMULAS_SOURCE
805 ;;
806 minion )
807 install_salt_minion_$SALT_SOURCE
808 ;;
809 esac
810 shift
811 done
812 echo DONE
813}
814
815function bootstrap() {
816 log_info "Bootstrap & verification of SaltMaster and configured minions."
817 trap _atexit INT TERM EXIT
818
819 system_config_master
820 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200821 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200822 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200823}
824
825function default() {
826 bootstrap $@
827}
828
829
830##########################################
831[[ "$0" != "$BASH_SOURCE" ]] || {
832# unless file is being sourced
833 default $@
834}