blob: f12fc1cfc2da192751b67d6eeac0f581b9d8ef44 [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 Michalecd2ba9372018-03-17 15:08:49 +0100252
253 which reclass || $SUDO $PKGTOOL install -y reclass
254
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200255 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
Oleksii Grudev691d4712018-04-02 10:23:46 +0300531 #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement
532 formula_service=${formula_service//-/$'_'}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200533 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
534 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service}
535 done
536 ;;
537 rhel)
538 # TODO
539 ;;
540 esac
541
542 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
543 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
544}
545
546install_salt_formula_git()
547{
548 echo "Configuring necessary formulas ..."
549
550 [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service
551 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
Petr Michalec2d753972017-11-30 16:24:37 +0100552 [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200553 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
554 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
555 _BRANCH=${FORMULAS_BRANCH}
556 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
557 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
558 # Fallback to the master branch if the branch doesn't exist for this repository
559 _BRANCH=master
560 fi
561 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
562 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
563 exit 1
564 fi
565 } || {
566 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
567 git fetch origin/${_BRANCH} || git fetch --all
568 git checkout ${_BRANCH} && git pull || git pull;
569 cd -
570 }
571 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
572 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
573 [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \
574 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service}
575 done
576
577 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
578 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
579}
580
Petr Michalece9c84522017-12-13 17:14:08 +0100581saltservice_stop() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100582 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100583 $SUDO service salt-minion stop
584 $SUDO service salt-master stop
585 sleep ${SALT_STOPSTART_WAIT:-30}
Petr Michalec7822efe2017-12-15 16:19:32 +0100586 ${SUDO} pkill -9 salt-master && ${SUDO} rm -rf /var/run/salt/master/* || true
Petr Michalece9c84522017-12-13 17:14:08 +0100587 ${SUDO} pkill -9 salt-minion
588}
589saltservice_start() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100590 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100591 $SUDO service salt-master start
592 $SUDO service salt-minion start
593 sleep ${SALT_STOPSTART_WAIT:-30}
594}
595
596saltservice_restart() {
Petr Michalecdd646db2017-12-18 15:56:18 +0100597 set +e
Petr Michalece9c84522017-12-13 17:14:08 +0100598 saltservice_stop
599 saltservice_start
600}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200601
602saltmaster_bootstrap() {
603
604 log_info "Salt master setup"
605 test -n "$MASTER_HOSTNAME" || exit 1
606
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200607 # override some envs from cluster level *.env, use with care
608 source_local_envs
609
Petr Michalece9c84522017-12-13 17:14:08 +0100610 saltservice_stop
611
612 clone_reclass
613
projoke3c30e7d2017-10-19 17:15:40 +0800614 SCRIPTS=$(dirname $0)
Petr Michalece9c84522017-12-13 17:14:08 +0100615
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200616 test -e ${SCRIPTS}/.salt-master-setup.sh.passed || {
Adam Tengler035f24f2017-09-11 19:29:31 +0200617 export MASTER_IP=${MASTER_IP:-127.0.0.1}
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200618 export MINION_ID=${MASTER_HOSTNAME}
619 if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
620 SALT_MASTER_SETUP_OUTPUT='/dev/stdout'
621 fi
622 # call local "setup() master"
623 #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then
624 if ! setup master; then
625 #cat /tmp/salt-master-setup.log
626 log_err "salt master setup() failed."
627 exit 1
628 else
629 $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed
630 fi
631 }
632
Petr Michalec981f3cc2017-11-15 17:40:31 +0100633 log_info "Install reclass"
634 install_reclass ${RECLASS_VERSION/dev*/develop}
Petr Michaleca6cd2bd2017-09-07 16:59:19 +0200635
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200636 log_info "Re/starting salt services"
Petr Michalece9c84522017-12-13 17:14:08 +0100637 saltservice_restart
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200638}
639
640# Init salt master
641saltmaster_init() {
642
643 log_info "Runing saltmaster states"
644 test -n "$MASTER_HOSTNAME" || exit 1
645
646 set -e
647 $SUDO salt-call saltutil.sync_all >/dev/null
648
Petr Michalecda014202017-08-18 11:26:07 +0200649 # workarond isolated and not fully bootstraped environments
Petr Michalec2801acb2017-10-31 15:00:15 +0100650 if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then
Petr Michalec54ee4082017-11-30 17:02:17 +0100651 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 +0100652 fi
Petr Michalec8ce8a652017-11-30 15:39:20 +0100653 PILLAR='{'${SALT_MASTER_PILLAR}' "reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalecda014202017-08-18 11:26:07 +0200654
Petr Michalec8b92d552017-11-29 12:28:50 +0100655 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100656 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100657 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200658 fi
659
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200660 # Revert temporary SaltMaster minimal configuration, if any
661 pushd $RECLASS_ROOT
662 if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then
Petr Michalec87778fc2017-11-02 10:13:25 +0100663 PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }'
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200664 git status || true
Petr Michalecda014202017-08-18 11:26:07 +0200665 log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200666 log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*."
667 git checkout -- $RECLASS_ROOT/nodes || true
668 log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state."
Petr Michalec7edf8322017-11-30 12:01:09 +0100669 log_info "State: salt.master.env,salt.master.pillar"
Petr Michalec54ee4082017-11-30 17:02:17 +0100670 if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then
Petr Michalec85ef7ad2017-11-30 15:21:19 +0100671 log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200672 fi
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200673 fi
674 popd
675
Petr Michalecda014202017-08-18 11:26:07 +0200676 # finally re-configure salt master conf, ie: may remove ignore_class_notfound option
677 log_info "State: salt.master.service"
Jakub Josef32563412018-01-17 17:55:05 +0100678 # ensure salt master is started
679 saltservice_start
Petr Michalec9fec8b32017-10-05 14:46:42 +0200680 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true
Petr Michalec4f043772017-12-19 15:40:54 +0100681 saltservice_start
Petr Michalecda014202017-08-18 11:26:07 +0200682
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200683 log_info "State: salt.master.storage.node"
684 set +e
Petr Michalec7edf8322017-11-30 12:01:09 +0100685 # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
686 # FIXME: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT.
Petr Michalec9fec8b32017-10-05 14:46:42 +0200687 retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200688 ret=$?
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200689
Petr Michalecdd646db2017-12-18 15:56:18 +0100690 set -e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200691 if [[ $ret -eq 2 ]]; then
692 log_err "State reclass.storage.node failed with exit code 2 but continuing."
693 elif [[ $ret -ne 0 ]]; then
694 log_err "State reclass.storage.node failed with exit code $ret"
695 exit 1
696 fi
697
Petr Michalecdd646db2017-12-18 15:56:18 +0100698 set +e
699 log_info "Updating minion.conf -> master: localhost"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200700 $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf
Petr Michalecdd646db2017-12-18 15:56:18 +0100701
702 log_info "Re/starting salt services" # in order to load new deployed configuration from model
Petr Michalece9c84522017-12-13 17:14:08 +0100703 saltservice_restart
Petr Michalecdd646db2017-12-18 15:56:18 +0100704
705 log_info "Salt Util sync all"
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200706 $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null
707
708 verify_salt_master
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200709
710}
711
712
713function verify_salt_master() {
714 set -e
715
716 log_info "Verify Salt master"
717 test -n "$MASTER_HOSTNAME" || exit 1
718
719 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
Petr Michaleca5141342017-08-16 12:55:20 +0200720 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml
721 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200722 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles
723 $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state
724 $SUDO salt-call --no-color grains.items
725 $SUDO salt-call --no-color pillar.data
726 fi
Petr Michaleca5141342017-08-16 12:55:20 +0200727 # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_*
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200728 if ! $SUDO reclass --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then
729 log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo"
730 exit 1
731 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100732 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200733}
734
735function verify_salt_minion() {
736 set -e
737 node=$1
738 log_info "Verifying ${node}"
739 if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then
740 $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles
741 $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate
742 fi
743 if ! $SUDO reclass --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then
744 log_err "For more details see full log /tmp/${node}.reclass.nodeinfo"
745 if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then
746 exit 1
747 fi
748 fi
Petr Michalec7edf8322017-11-30 12:01:09 +0100749 set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200750}
751
752function verify_salt_minions() {
753 #set -e
Petr Michalec679f15b2017-09-18 16:16:29 +0200754 NODES=$(find $RECLASS_ROOT/nodes/_generated/ -name "*.yml" | grep -v "cfg")
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200755 log_info "Verifying minions: $(echo ${NODES}|xargs)"
756
757 # Parallel
758 #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify"
759 #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30
760
761 function filterFails() {
762 grep -v '/grains' | tee -a $1 | tail -n20
763 }
764
765 log_info "Verify nodes"
766 passed=0
767 for node in ${NODES}; do
768 node=$(basename $node .yml)
769
770 # filter first in cluster.. ctl-01, mon-01, etc..
771 if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then
772 verify_salt_minion ${node} || continue
773 else
774 echo Skipped $node.
775 fi
776 passed=$(($passed+1))
777 done
778 # fail on failures
779 total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l)
780 test ! $passed -lt $total || log_err "Results: $passed of $total passed."
781 test ! $passed -lt $total || {
782 tail -n50 /tmp/*.pillar_verify
783 return 1
784 }
Petr Michalec7edf8322017-11-30 12:01:09 +0100785 #set +e
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200786}
787
788
789
790##########################################
791# To install salt master/minon
792
793function install() {
794 setup $@
795}
796
797function setup() {
798 # CLI
799 while [ x"$1" != x"" ]; do
800 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
801
802 case $1 in
803 master )
804 install_salt_master_$SALT_SOURCE
805 install_salt_minion_$SALT_SOURCE
806 install_salt_formula_$FORMULAS_SOURCE
807 ;;
808 minion )
809 install_salt_minion_$SALT_SOURCE
810 ;;
811 esac
812 shift
813 done
814 echo DONE
815}
816
817function bootstrap() {
818 log_info "Bootstrap & verification of SaltMaster and configured minions."
819 trap _atexit INT TERM EXIT
820
821 system_config_master
822 saltmaster_bootstrap &&\
Petr Michalec58575b92017-08-20 10:42:25 +0200823 saltmaster_init #&&\
Petr Michaleca5141342017-08-16 12:55:20 +0200824 #verify_salt_minions
Petr Michalec1ed1b3c2017-08-08 19:19:01 +0200825}
826
827function default() {
828 bootstrap $@
829}
830
831
832##########################################
833[[ "$0" != "$BASH_SOURCE" ]] || {
834# unless file is being sourced
835 default $@
836}