blob: d7ce71b60670bb7de74c712fe1462086d165227e [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 Michalecbebd7d52018-03-16 22:14:05 +0100252
253 # DEPRECATED, should be removed or moved to salt master specific function
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200254 which reclass-salt || {
255 test -e /usr/share/reclass/reclass-salt && {
256 ln -fs /usr/share/reclass/reclass-salt /usr/bin
257 }
258 }
259}
260
261configure_salt_master()
262{
263
264 echo "Configuring salt-master ..."
265
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200266 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec29d3ea32017-10-31 17:22:36 +0100267 read -d '' IGNORE_CLASS_NOTFOUND <<-EOF
268 ignore_class_notfound: True
269 ignore_class_regexp:
270 - 'service.*'
271EOF
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200272 fi
273
Petr Michalecdd1a0472017-09-06 19:27:24 +0200274 # to force alternative reclass module path
Petr Michalecd9d0a322017-09-06 19:38:32 +0200275 if [ -n "$RECLASS_SOURCE_PATH" ]; then
276 RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}"
277 else
278 RECLASS_SOURCE_PATH=""
279 fi
Petr Michalecdd1a0472017-09-06 19:27:24 +0200280
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200281 [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d
282 cat <<-EOF > /etc/salt/master.d/master.conf
283 file_roots:
284 base:
285 - /usr/share/salt-formulas/env
286 prd:
287 - /srv/salt/env/prd
288 dev:
289 - /srv/salt/env/dev
290 pillar_opts: False
291 open_mode: True
292 reclass: &reclass
293 storage_type: yaml_fs
294 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec4be5e5b2017-08-17 11:44:49 +0200295 ${IGNORE_CLASS_NOTFOUND}
Petr Michalecdd1a0472017-09-06 19:27:24 +0200296 ${RECLASS_SOURCE_PATH}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200297 ext_pillar:
298 - reclass: *reclass
299 master_tops:
300 reclass: *reclass
301EOF
302
303 echo "Configuring reclass ..."
304
305 [ ! -d /etc/reclass ] && mkdir /etc/reclass
306 cat <<-EOF > /etc/reclass/reclass-config.yml
307 storage_type: yaml_fs
308 pretty_print: True
309 output: yaml
310 inventory_base_uri: ${RECLASS_ROOT}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200311EOF
312
313 clone_reclass
314 # override some envs from cluster level *.env, use with care
315 source_local_envs
316
317 cd ${RECLASS_ROOT}
318 if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then
319 # Possibly subrepo checkout needed
320 git submodule update --init --recursive
321 fi
322
Petr Michalec212cc6a2017-08-17 21:39:40 +0200323 mkdir -vp ${RECLASS_ROOT}/nodes/_generated
Petr Michalec51ece842017-09-06 20:44:15 +0200324 rm -rvf ${RECLASS_ROOT}/nodes/_generated/*
325
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200326 CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
Petr Michalec212cc6a2017-08-17 21:39:40 +0200327 CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200328 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
Petr Michalec24b30e82017-08-21 10:59:18 +0200329 log_warn "Salt Master node specification has not been found in model."
330 log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200331 cat <<-EOF > ${CONFIG}
332 classes:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200333 - cluster.${CLUSTER_NAME}.infra.config
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200334 parameters:
335 _param:
Petr Michaleca7de6df2017-08-31 12:16:57 +0200336 salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200337 salt_master_base_environment: $SALT_ENV
Petr Michalecd81d1f52017-08-17 20:18:06 +0200338 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
ibumarskovf72e05e2017-11-29 14:37:48 +0300339 reclass_data_revision: ${RECLASS_REVISION:-$RECLASS_BRANCH}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200340 reclass_data_repository: "$RECLASS_ADDRESS"
Petr Michaleca7de6df2017-08-31 12:16:57 +0200341 reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalecd81d1f52017-08-17 20:18:06 +0200342 linux_system_codename: ${DISTRIB_CODENAME}
343 cluster_name: ${CLUSTER_NAME}
344 cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200345 linux:
346 system:
Petr Michalecd81d1f52017-08-17 20:18:06 +0200347 name: ${HOSTNAME:-cfg01}
348 domain: ${DOMAIN:-$CLUSTER_NAME.local}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200349 # ########
350EOF
351
352 if [ "$SALT_VERSION" == "latest" ]; then
353 VERSION=""
354 else
355 VERSION="version: $SALT_VERSION"
356 fi
357
358 cat <<-EOF >> ${CONFIG}
359 salt:
360 master:
361 accept_policy: open_mode
362 source:
363 engine: $SALT_SOURCE
364 $VERSION
365 minion:
366 source:
367 engine: $SALT_SOURCE
368 $VERSION
369 # ########
370 # vim: ft=yaml sw=2 ts=2 sts=2
371EOF
372 fi
Petr Michalec24b30e82017-08-21 10:59:18 +0200373
374 log_debug "Salt Master node config yaml:"
375 log_debug "$(cat ${CONFIG})"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200376}
377
378configure_salt_minion()
379{
380 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
381 cat <<-EOF > /etc/salt/minion.d/minion.conf
Petr Michaleca7de6df2017-08-31 12:16:57 +0200382 master: ${MASTER_IP:-$MASTER_HOSTNAME}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200383 id: $MINION_ID
384 EOF
385}
386
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200387install_reclass()
388{
389 VERSION=${1:-$RECLASS_VERSION}
Petr Michalec372fa032017-11-15 17:36:19 +0100390 VERSION=${VERSION:-pkg}
Petr Michalec800dd072017-11-15 17:19:26 +0100391 case ${VERSION} in
392 pkg|package)
393 which reclass || $SUDO $PKGTOOL install -y reclass
394 ;;
395 *)
Petr Michalec981f3cc2017-11-15 17:40:31 +0100396 log_warn "Install development version of reclass"
Petr Michalec1b37d3d2017-11-15 18:20:46 +0100397 # Note: dev/git/pip version...
398 # It replaces all local reclass versions on system
399 # Required for reclass/git features:
400 # $SUDO $PKGTOOL install -y libffi-dev libgit2-dev || true
Petr Michalec800dd072017-11-15 17:19:26 +0100401 for s in $(python -c "import site; print(' '.join(site.getsitepackages()))"); do
Petr Michalec23020832017-11-15 17:34:49 +0100402 sudo -H pip install --install-option="--prefix=" --upgrade --force-reinstall -I \
Petr Michalec800dd072017-11-15 17:19:26 +0100403 -t "$s" git+https://github.com/salt-formulas/reclass.git@${VERSION};
404 done
405 ;;
406 esac
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200407}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200408
409install_salt_master_pkg()
410{
411 echo -e "\nPreparing base OS repository ...\n"
412
413 configure_pkg_repo
414
415 echo -e "\nInstalling salt master ...\n"
416
417 case $PLATFORM_FAMILY in
418 debian)
419 $SUDO apt-get install -y git
alexz0adc2712018-02-11 17:33:52 +0100420 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200421 ;;
422 rhel)
423 yum install -y git
alexz0adc2712018-02-11 17:33:52 +0100424 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200425 ;;
426 esac
Jakub Josef0b611742018-01-09 17:02:01 +0100427
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200428 which reclass-salt || {
429 test -e /usr/share/reclass/reclass-salt && {
430 ln -fs /usr/share/reclass/reclass-salt /usr/bin
431 }
432 }
433
434 configure_salt_master
435
436 echo -e "\nRestarting services ...\n"
437 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
438 $SVCTOOL salt-master restart
439}
440
441install_salt_master_pip()
442{
443 echo -e "\nPreparing base OS repository ...\n"
444
445 case $PLATFORM_FAMILY in
446 debian)
447 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200448 ;;
449 rhel)
450 $SUDO yum install -y git
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200451 ;;
452 esac
453
454 echo -e "\nInstalling salt master ...\n"
455 # TODO: replace with saltstack bootstrap script
Jakub Josef0b611742018-01-09 17:02:01 +0100456
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200457 if [ "$SALT_VERSION" == "latest" ]; then
458 pip install salt
459 else
460 pip install salt==$SALT_VERSION
461 fi
462
463 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
464 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
465
466 which reclass-salt || {
467 test -e /usr/share/reclass/reclass-salt && {
468 ln -fs /usr/share/reclass/reclass-salt /usr/bin
469 }
470 }
471
472 configure_salt_master
473
474 echo -e "\nRestarting services ...\n"
475 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
476 $SVCTOOL salt-master restart
477}
478
479
480
481install_salt_minion_pkg()
482{
483
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200484 echo -e "\nInstalling salt minion ...\n"
485
486 case $PLATFORM_FAMILY in
487 debian)
alexz0adc2712018-02-11 17:33:52 +0100488 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200489 ;;
490 rhel)
alexz0adc2712018-02-11 17:33:52 +0100491 curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200492 ;;
493 esac
494
495
496 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200497 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200498}
499
500install_salt_minion_pip()
501{
502 echo -e "\nInstalling salt minion ...\n"
503
504 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
505 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
506
507 configure_salt_minion
Petr Michalec3b7fdd82017-09-08 15:35:56 +0200508 #$SVCTOOL salt-minion restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200509}
510
511
512install_salt_formula_pkg()
513{
514 configure_pkg_repo
515
516 case $PLATFORM_FAMILY in
517 debian)
518 echo "Configuring necessary formulas ..."
519
520 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
521 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100522 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200523 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
524 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
525 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
526 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
527 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
528 exit 1
529 fi
530 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
531 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
532 done
533 ;;
534 rhel)
535 # TODO
536 ;;
537 esac
538
539 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
540 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
541}
542
543install_salt_formula_git()
544{
545 echo "Configuring necessary formulas ..."
546
547 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
548 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100549 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200550 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
551 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
552 _BRANCH=${FORMULAS_BRANCH}
553 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
554 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
555 # Fallback to the master branch if the branch doesn't exist for this repository
556 _BRANCH=master
557 fi
558 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
559 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
560 exit 1
561 fi
562 } || {
563 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
564 git fetch origin/${_BRANCH} || git fetch --all
565 git checkout ${_BRANCH} && git pull || git pull;
566 cd -
567 }
568 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
569 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
570 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
571 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
572 done
573
574 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
575 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
576}
577
Petr Michalece9c84522017-12-13 17:14:08 +0100578saltservice_stop() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100579 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100580 $SUDO service salt-minion stop
581 $SUDO service salt-master stop
582 sleep ${SALT_STOPSTART_WAIT:-30}
Petr Michalec7822efe2017-12-15 16:19:32 +0100583 ${SUDO} pkill -9 salt-master && ${SUDO} rm -rf /var/run/salt/master/* || true
Petr Michalece9c84522017-12-13 17:14:08 +0100584 ${SUDO} pkill -9 salt-minion
585}
586saltservice_start() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100587 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100588 $SUDO service salt-master start
589 $SUDO service salt-minion start
590 sleep ${SALT_STOPSTART_WAIT:-30}
591}
592
593saltservice_restart() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100594 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100595 saltservice_stop
596 saltservice_start
597}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200598
599saltmaster_bootstrap() {
600
601 log_info "Salt master setup"
602 test -n "$MASTER_HOSTNAME" || exit 1
603
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200604 # override some envs from cluster level *.env, use with care
605 source_local_envs
606
Petr Michalece9c84522017-12-13 17:14:08 +0100607 saltservice_stop
608
609 clone_reclass
610
projoke3c30e7d2017-10-19 17:15:40 +0800611 SCRIPTS=$(dirname $0)
Petr Michalece9c84522017-12-13 17:14:08 +0100612
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200613 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
Adam Tengler035f24f2017-09-11 19:29:31 +0200614 export MASTER_IP=${MASTER_IP:-127.0.0.1}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200615 export MINION_ID=${MASTER_HOSTNAME}
616 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
617 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
618 fi
619 # call local "setup() master"
620 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
621 if ! setup master; then
622 #cat /tmp/salt-master-setup.log
623 log_err "salt master setup() failed."
624 exit 1
625 else
626 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
627 fi
628 }
629
Petr Michalec981f3cc2017-11-15 17:40:31 +0100630 log_info "Install reclass"
631 install_reclass ${RECLASS_VERSION/dev*/develop}
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200632
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200633 log_info "Re/starting salt services"
Petr Michalece9c84522017-12-13 17:14:08 +0100634 saltservice_restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200635}
636
637# Init salt master
638saltmaster_init() {
639
640 log_info "Runing saltmaster states"
641 test -n "$MASTER_HOSTNAME" || exit 1
642
643 set -e
644 $SUDO salt-call saltutil.sync_all >/dev/null
645
Petr Michalecda014202017-08-18 11:26:07 +0200646 # workarond isolated and not fully bootstraped environments
Petr Michalec2801acb2017-10-31 15:00:15 +0100647 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec54ee4082017-11-30 17:02:17 +0100648 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 +0100649 fi
Petr Michalec8ce8a652017-11-30 15:39:20 +0100650 PILLAR='{'${SALT_MASTER_PILLAR}' "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalecda014202017-08-18 11:26:07 +0200651
Petr Michalec8b92d552017-11-29 12:28:50 +0100652 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100653 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100654 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200655 fi
656
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200657 # Revert temporary SaltMaster minimal configuration, if any
658 pushd $RECLASS_ROOT
659 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
Petr Michalec87778fc2017-11-02 10:13:25 +0100660 PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200661 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200662 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200663 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
664 git checkout -- $RECLASS_ROOT/nodes || true
665 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
Petr Michalec7edf8322017-11-30 12:01:09 +0100666 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100667 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100668 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200669 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200670 fi
671 popd
672
Petr Michalecda014202017-08-18 11:26:07 +0200673 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
674 log_info "State: salt.master.service"
Jakub Josef32563412018-01-17 17:55:05 +0100675 # ensure salt master is started
676 saltservice_start
Petr Michalec9fec8b32017-10-05 14:46:42 +0200677 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalec4f043772017-12-19 15:40:54 +0100678 saltservice_start
Petr Michalecda014202017-08-18 11:26:07 +0200679
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200680 log_info "State: salt.master.storage.node"
681 set +e
Petr Michalec7edf8322017-11-30 12:01:09 +0100682 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
683 # FIXME: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec9fec8b32017-10-05 14:46:42 +0200684 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200685 ret=$?
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200686
Petr Michalecdd646db2017-12-18 15:56:18 +0100687 set -e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200688 if [[ $ret -eq 2 ]]; then
689 log_err "State reclass.storage.node failed with exit code 2 but continuing."
690 elif [[ $ret -ne 0 ]]; then
691 log_err "State reclass.storage.node failed with exit code $ret"
692 exit 1
693 fi
694
Petr Michalecdd646db2017-12-18 15:56:18 +0100695 set +e
696 log_info "Updating minion.conf -> master: localhost"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200697 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
Petr Michalecdd646db2017-12-18 15:56:18 +0100698
699 log_info "Re/starting salt services" # in order to load new deployed configuration from model
Petr Michalece9c84522017-12-13 17:14:08 +0100700 saltservice_restart
Petr Michalecdd646db2017-12-18 15:56:18 +0100701
702 log_info "Salt Util sync all"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200703 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
704
705 verify_salt_master
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200706
707}
708
709
710function verify_salt_master() {
711 set -e
712
713 log_info "Verify Salt master"
714 test -n "$MASTER_HOSTNAME" || exit 1
715
716 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200717 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
718 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200719 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
720 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
721 $SUDO salt-call --no-color grains.items
722 $SUDO salt-call --no-color pillar.data
723 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200724 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200725 if ! $SUDO reclass --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
726 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
727 exit 1
728 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100729 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200730}
731
732function verify_salt_minion() {
733 set -e
734 node=$1
735 log_info "Verifying ${node}"
736 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
737 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
738 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
739 fi
740 if ! $SUDO reclass --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
741 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
742 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
743 exit 1
744 fi
745 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100746 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200747}
748
749function verify_salt_minions() {
750 #set -e
Petr Michalec679f15b2017-09-18 16:16:29 +0200751 NODES=$(find $RECLASS_ROOT/nodes/_generated/ -name "*.yml" | grep -v "cfg")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200752 log_info "Verifying minions: $(echo ${NODES}|xargs)"
753
754 # Parallel
755 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
756 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
757
758 function filterFails() {
759 grep -v '/grains' | tee -a $1 | tail -n20
760 }
761
762 log_info "Verify nodes"
763 passed=0
764 for node in ${NODES}; do
765 node=$(basename $node .yml)
766
767 # filter first in cluster.. ctl-01, mon-01, etc..
768 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
769 verify_salt_minion ${node} || continue
770 else
771 echo Skipped $node.
772 fi
773 passed=$(($passed+1))
774 done
775 # fail on failures
776 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
777 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
778 test ! $passed -lt $total || {
779 tail -n50 /tmp/*.pillar_verify
780 return 1
781 }
Petr Michalec7edf8322017-11-30 12:01:09 +0100782 #set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200783}
784
785
786
787##########################################
788# To install salt master/minon
789
790function install() {
791 setup $@
792}
793
794function setup() {
795 # CLI
796 while [ x"$1" != x"" ]; do
797 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
798
799 case $1 in
800 master )
801 install_salt_master_$SALT_SOURCE
802 install_salt_minion_$SALT_SOURCE
803 install_salt_formula_$FORMULAS_SOURCE
804 ;;
805 minion )
806 install_salt_minion_$SALT_SOURCE
807 ;;
808 esac
809 shift
810 done
811 echo DONE
812}
813
814function bootstrap() {
815 log_info "Bootstrap & verification of SaltMaster and configured minions."
816 trap _atexit INT TERM EXIT
817
818 system_config_master
819 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200820 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200821 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200822}
823
824function default() {
825 bootstrap $@
826}
827
828
829##########################################
830[[ "$0" != "$BASH_SOURCE" ]] || {
831# unless file is being sourced
832 default $@
833}