Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 1 | #!/bin/bash -e |
| 2 | |
| 3 | # bootstrap.sh |
| 4 | |
| 5 | # Installs Salt and configure minimal SaltMaster or Minion to be used with: |
| 6 | # - http://github.com/salt-formulas-scripts |
| 7 | # - http://github.com/salt-formulas/salt-formula-salt (salt.master sls) |
| 8 | |
Petr Michalec | b312004 | 2018-03-17 11:17:32 +0100 | [diff] [blame] | 9 | # NOTE: This script is collection of shared functions to automate bootstrap of "salted" CI environments. |
| 10 | # Its not intended to be used in PRODUCTION unless you know what you are doing. |
| 11 | # You have been warned! |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 12 | |
sgarbuz | 43c50ea | 2019-03-29 14:49:24 +0200 | [diff] [blame] | 13 | __ScriptVersion="2019.02.27" |
azvyagintsev | e93f837 | 2018-09-12 16:50:58 +0300 | [diff] [blame] | 14 | __ScriptName="bootstrap.sh" |
| 15 | __ScriptFullName="$0" |
| 16 | __ScriptArgs="$*" |
| 17 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 18 | # Source specific env vars. |
| 19 | # shopt -u dotglob |
| 20 | export RECLASS_ROOT=${RECLASS_ROOT:-/srv/salt/reclass} |
| 21 | function source_local_envs() { |
Petr Michalec | b444ef9 | 2017-08-17 13:53:14 +0200 | [diff] [blame] | 22 | for path in / /tmp/kitchen /srv/salt . ${RECLASS_ROOT}/classes/cluster ${RECLASS_ROOT}/classes/cluster/${CLUSTER_NAME}; do |
Vasyl Saienko | 7a4161c | 2017-08-14 17:43:15 +0300 | [diff] [blame] | 23 | for f in $(find $path -maxdepth 1 -name '*.env' 2> /dev/null); do |
| 24 | echo "Sourcing env variables from $f" |
| 25 | source $f |
| 26 | done |
| 27 | done |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 28 | } |
| 29 | source_local_envs |
| 30 | |
| 31 | ########################################## |
| 32 | # Set defaults env variables |
| 33 | |
| 34 | if [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then |
| 35 | set -x |
| 36 | SALT_LOG_LEVEL="--state-verbose=true -ldebug" |
| 37 | fi |
| 38 | |
| 39 | export MAGENTA='\033[0;95m' |
| 40 | export YELLOW='\033[1;33m' |
| 41 | export BLUE='\033[0;35m' |
| 42 | export CYAN='\033[0;96m' |
| 43 | export RED='\033[0;31m' |
| 44 | export NC='\033[0m' # No Color' |
| 45 | |
| 46 | export LC_ALL=C |
| 47 | export SALT_LOG_LEVEL="--state-verbose=false -lerror" |
| 48 | export SALT_OPTS="${SALT_OPTS:- --timeout=120 --state-output=changes --retcode-passthrough --force-color $SALT_LOG_LEVEL }" |
| 49 | export SALT_STATE_RETRY=${SALT_STATE_RETRY:-3} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 50 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 51 | # salt apt repository |
| 52 | test -e /etc/lsb-release && eval $(cat /etc/lsb-release) |
| 53 | which lsb_release && DISTRIB_CODENAME=${DISTRIB_CODENAME:-$(lsb_release -cs)} |
| 54 | # |
Vasyl Saienko | 70069c8 | 2018-12-15 14:11:53 +0200 | [diff] [blame] | 55 | export APT_REPOSITORY=${APT_REPOSITORY:- deb [arch=amd64] http://mirror.mirantis.com/${DISTRIB_REVISION:-testing}/salt-formulas/${DISTRIB_CODENAME} ${DISTRIB_CODENAME} main} |
| 56 | export APT_REPOSITORY_GPG=${APT_REPOSITORY_GPG:-http://mirror.mirantis.com/${DISTRIB_REVISION:-testing}/salt-formulas/${DISTRIB_CODENAME}/archive-salt-formulas.key} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 57 | # reclass |
| 58 | export RECLASS_ADDRESS=${RECLASS_ADDRESS:-https://github.com/salt-formulas/openstack-salt.git} # https/git |
ibumarskov | f72e05e | 2017-11-29 14:37:48 +0300 | [diff] [blame] | 59 | export RECLASS_BRANCH=${RECLASS_BRANCH:-master} |
sgarbuz | 43c50ea | 2019-03-29 14:49:24 +0200 | [diff] [blame] | 60 | export RECLASS_SYSTEM_ADDRESS=${RECLASS_SYSTEM_ADDRESS:-http://gerrit.mcp.mirantis.com/salt-models/reclass-system} |
| 61 | export RECLASS_SYSTEM_BRANCH=${RECLASS_SYSTEM_BRANCH:-} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 62 | |
| 63 | # formula |
| 64 | export FORMULAS_BASE=${FORMULAS_BASE:-https://github.com/salt-formulas} |
| 65 | export FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas} |
| 66 | export FORMULAS_BRANCH=${FORMULAS_BRANCH:-master} |
| 67 | export FORMULAS_SOURCE=${FORMULAS_SOURCE:-pkg} # pkg/git |
Petr Michalec | 515356f | 2017-11-16 11:20:20 +0100 | [diff] [blame] | 68 | # Essential set of formulas (known to by used on cfg01 node for most setups during bootsrap) |
| 69 | # DEPRECATED: FORMULAS_SALT_MASTER=${FORMULAS_SALT_MASTER:- $EXTRA_FORMULAS maas memcached ntp nginx collectd sensu heka sphinx mysql grafana libvirt rsyslog glusterfs postfix xtrabackup freeipa prometheus telegraf elasticsearch kibana rundeck devops-portal rsync docker keepalived aptly jenkins gerrit artifactory influxdb horizon} |
azvyagintsev | e93f837 | 2018-09-12 16:50:58 +0300 | [diff] [blame] | 70 | FORMULAS_SALT_MASTER=${FORMULAS_SALT_MASTER:- "$EXTRA_FORMULAS"} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 71 | # minimal set of formulas for salt-master bootstrap |
azvyagintsev | e93f837 | 2018-09-12 16:50:58 +0300 | [diff] [blame] | 72 | declare -a FORMULAS_SALT_MASTER=(reclass salt git openssh linux $(echo "${FORMULAS_SALT_MASTER}")) |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 73 | export FORMULAS_SALT_MASTER |
| 74 | |
azvyagintsev | a7a91d5 | 2018-09-13 12:58:39 +0300 | [diff] [blame] | 75 | |
| 76 | # Install 'salt-formula-*'. Works only with FORMULAS_SOURCE==pkg |
| 77 | export EXTRA_FORMULAS_PKG_ALL=${EXTRA_FORMULAS_PKG_ALL:-False} |
| 78 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 79 | # system / host |
Petr Michalec | c749e86 | 2017-09-06 21:10:10 +0200 | [diff] [blame] | 80 | export HOSTNAME=${HOSTNAME:-`hostname -s`} |
| 81 | export HOSTNAME=${HOSTNAME//.*/} |
| 82 | export DOMAIN=${DOMAIN:-`hostname -d`} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 83 | export DOMAIN=${DOMAIN:-bootstrap.local} |
| 84 | |
| 85 | # salt |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 86 | export MINION_ID=${MINION_ID:-${HOSTNAME}.${DOMAIN}} |
Petr Michalec | 2e3a499 | 2017-08-17 14:20:43 +0200 | [diff] [blame] | 87 | export MASTER_HOSTNAME=${MASTER_HOSTNAME:-${HOSTNAME}.${DOMAIN}} |
azvyagintsev | e3f6f40 | 2018-08-23 17:21:49 +0300 | [diff] [blame] | 88 | # Ignore state.apply salt.master.env error |
| 89 | export SA_IGNORE_ERROR_SALT_MASTER_ENV=${SA_IGNORE_ERROR_SALT_MASTER_ENV:-False} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 90 | |
| 91 | # saltstack |
Petr Michalec | 33c0489 | 2017-09-08 15:10:30 +0200 | [diff] [blame] | 92 | BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True} |
Pavlo Shchelokovskyy | d676952 | 2021-07-05 15:02:48 +0300 | [diff] [blame] | 93 | BOOTSTRAP_SALTSTACK_COM=${BOOTSTRAP_SALTSTACK_COM:-"https://gerrit.mcp.mirantis.com/projects/salt-formulas%2Fsalt-formulas-scripts/branches/master/files/bootstrap-salt.sh/content"} |
Martin Polreich | 16673dd | 2018-04-20 12:08:56 +0200 | [diff] [blame] | 94 | BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION:- stable 2017.7 } |
Jakub Josef | 0b61174 | 2018-01-09 17:02:01 +0100 | [diff] [blame] | 95 | BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION//latest*/stable} |
alexz | 0adc271 | 2018-02-11 17:33:52 +0100 | [diff] [blame] | 96 | # TODO add 'r' option by default |
| 97 | # Currently, without 'r' option, upstream saltstack repos will be used. Otherwise |
Vasyl Saienko | 28e3d46 | 2018-12-14 17:27:15 +0200 | [diff] [blame] | 98 | # local one should be used, from http://mirror.mirantis.com/ or whatever. |
Vasyl Saienko | 48a10bb | 2018-11-29 10:17:09 +0000 | [diff] [blame] | 99 | BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX $BOOTSTRAP_SALTSTACK_VERSION } |
Petr Michalec | e11cf61 | 2017-09-08 18:21:48 +0200 | [diff] [blame] | 100 | SALT_SOURCE=${SALT_SOURCE:-pkg} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 101 | |
Petr Michalec | b312004 | 2018-03-17 11:17:32 +0100 | [diff] [blame] | 102 | # SECURITY |
Petr Michalec | ca2471f | 2018-03-17 11:17:32 +0100 | [diff] [blame] | 103 | SSH_STRICTHOSTKEYCHECKING=no |
Petr Michalec | b312004 | 2018-03-17 11:17:32 +0100 | [diff] [blame] | 104 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 105 | # environment |
| 106 | if [ "$FORMULAS_SOURCE" == "git" ]; then |
| 107 | SALT_ENV=${SALT_ENV:-dev} |
| 108 | elif [ "$FORMULAS_SOURCE" == "pkg" ]; then |
| 109 | SALT_ENV=${SALT_ENV:-prd} |
| 110 | fi |
Petr Michalec | 05b5245 | 2017-09-08 14:26:59 +0200 | [diff] [blame] | 111 | eval "$(grep -h '=' /etc/*release 2> /dev/null)" |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 112 | PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z) |
| 113 | case $PLATFORM_FAMILY in |
| 114 | debian ) |
| 115 | PKGTOOL="$SUDO apt-get" |
| 116 | test ${VERSION_ID//\.*/} -ge 16 && { |
| 117 | SVCTOOL=service |
| 118 | } || { SVCTOOL=service |
| 119 | } |
| 120 | ;; |
| 121 | rhel ) |
| 122 | PKGTOOL="$SUDO yum" |
| 123 | test ${VERSION_ID//\.*/} -ge 7 && { |
| 124 | SVCTOOL=systemctl |
| 125 | } || { SVCTOOL=service |
| 126 | } |
| 127 | ;; |
| 128 | esac |
| 129 | |
| 130 | export PLATFORM_FAMILY |
| 131 | export PKGTOOL |
| 132 | export SVCTOOL |
| 133 | |
| 134 | ########################################## |
| 135 | # FUNCTIONS |
| 136 | |
| 137 | log_info() { |
| 138 | echo -e "${YELLOW}[INFO] $* ${NC}" |
| 139 | } |
| 140 | |
| 141 | log_warn() { |
| 142 | echo -e "${MAGENTA}[WARN] $* ${NC}" |
| 143 | } |
| 144 | |
Petr Michalec | 24b30e8 | 2017-08-21 10:59:18 +0200 | [diff] [blame] | 145 | log_debug() { |
| 146 | echo -e "${CYAN}[WARN] $* ${NC}" |
| 147 | } |
| 148 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 149 | log_err() { |
| 150 | echo -e "${RED}[ERROR] $* ${NC}" >&2 |
| 151 | } |
| 152 | |
| 153 | configure_pkg_repo() |
| 154 | { |
Vasyl Saienko | 48a10bb | 2018-11-29 10:17:09 +0000 | [diff] [blame] | 155 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 156 | case $PLATFORM_FAMILY in |
| 157 | debian) |
| 158 | if [ -n "$APT_REPOSITORY_PPA" ]; then |
| 159 | which add-apt-repository || $SUDO apt-get install -y software-properties-common |
| 160 | $SUDO add-apt-repository -y ppa:${APT_REPOSITORY_PPA} |
Vasyl Saienko | 48a10bb | 2018-11-29 10:17:09 +0000 | [diff] [blame] | 161 | else |
| 162 | echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/mcp_salt.list >/dev/null |
| 163 | curl -sL $APT_REPOSITORY_GPG | $SUDO apt-key add - |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 164 | fi |
| 165 | $SUDO apt-get clean |
| 166 | $SUDO apt-get update |
| 167 | ;; |
| 168 | rhel) |
| 169 | $SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm |
| 170 | $SUDO yum clean all |
| 171 | ;; |
| 172 | esac |
| 173 | |
| 174 | } |
| 175 | |
| 176 | _atexit() { |
| 177 | RETVAL=$? |
| 178 | trap true INT TERM EXIT |
| 179 | |
| 180 | if [ $RETVAL -ne 0 ]; then |
| 181 | log_err "Execution failed" |
| 182 | else |
| 183 | log_info "Execution successful" |
| 184 | fi |
| 185 | return $RETVAL |
| 186 | } |
| 187 | |
| 188 | retry() { |
| 189 | local tries |
| 190 | if [[ $1 =~ ^[0-9]+$ ]]; then |
| 191 | tries=$1; shift |
| 192 | else |
| 193 | tries=3 |
| 194 | fi |
Petr Michalec | 262e3ad | 2017-10-06 13:41:58 +0200 | [diff] [blame] | 195 | ret=1 |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 196 | for i in $(seq 1 $tries); do |
Petr Michalec | 262e3ad | 2017-10-06 13:41:58 +0200 | [diff] [blame] | 197 | "$@" && return $? || ret=$? |
| 198 | sleep $i |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 199 | done |
Petr Michalec | 9fec8b3 | 2017-10-05 14:46:42 +0200 | [diff] [blame] | 200 | return $ret |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | function clone_reclass() { |
Petr Michalec | a361d4e | 2017-09-08 11:38:16 +0200 | [diff] [blame] | 204 | if [ ! -d ${RECLASS_ROOT}/classes ]; then |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 205 | # No reclass at all, clone from given address |
alexz | 0adc271 | 2018-02-11 17:33:52 +0100 | [diff] [blame] | 206 | # In case, non-github repo |
| 207 | _tmp_host=$(echo ${RECLASS_ADDRESS} | awk -F/ '{print $3}') |
| 208 | ssh-keyscan -T 1 -H ${_tmp_host} >> ~/.ssh/known_hosts || true |
| 209 | # But, awk is not perfect solution, so lets make double-check for github |
| 210 | ssh-keyscan -T 1 -H github.com >> ~/.ssh/known_hosts || true |
ibumarskov | f72e05e | 2017-11-29 14:37:48 +0300 | [diff] [blame] | 211 | if echo ${RECLASS_BRANCH} | egrep -q "^refs"; then |
| 212 | git clone ${RECLASS_ADDRESS} ${RECLASS_ROOT} |
| 213 | cd ${RECLASS_ROOT} |
| 214 | git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH} && git checkout FETCH_HEAD |
| 215 | export RECLASS_REVISION=$(git rev-parse HEAD) |
| 216 | cd - |
| 217 | else |
| 218 | git clone -b ${RECLASS_BRANCH} ${RECLASS_ADDRESS} ${RECLASS_ROOT}; |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 219 | fi; |
| 220 | fi; |
Petr Michalec | 679f15b | 2017-09-18 16:16:29 +0200 | [diff] [blame] | 221 | if [ ! -d ${RECLASS_ROOT}/classes ]; then |
Petr Michalec | a361d4e | 2017-09-08 11:38:16 +0200 | [diff] [blame] | 222 | log_err "Reclass ${RECLASS_ROOT} is not fetched locally;" |
| 223 | ls -Rla ${RECLASS_ROOT} |
| 224 | exit 1 |
| 225 | fi; |
| 226 | $SUDO mkdir -p $RECLASS_ROOT/classes/service |
| 227 | $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
| 231 | ########################################## |
| 232 | # Main calls |
| 233 | |
Petr Michalec | ca2471f | 2018-03-17 11:17:32 +0100 | [diff] [blame] | 234 | ci_config_ssh() { |
| 235 | |
| 236 | # for CI current user |
Petr Michalec | b312004 | 2018-03-17 11:17:32 +0100 | [diff] [blame] | 237 | conf=~/.ssh/config |
| 238 | mkdir -p $(dirname $conf) |
| 239 | touch $conf |
Petr Michalec | ca2471f | 2018-03-17 11:17:32 +0100 | [diff] [blame] | 240 | echo -e "Host *\n\tStrictHostKeyChecking $SSH_STRICTHOSTKEYCHECKING\n" | tee -a $conf >/dev/null |
Petr Michalec | b312004 | 2018-03-17 11:17:32 +0100 | [diff] [blame] | 241 | touch ~/.ssh/known_hosts |
Petr Michalec | ca2471f | 2018-03-17 11:17:32 +0100 | [diff] [blame] | 242 | |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 243 | if ! grep github.com ~/.ssh/known_hosts; then |
| 244 | ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true |
Petr Michalec | b312004 | 2018-03-17 11:17:32 +0100 | [diff] [blame] | 245 | ssh-keyscan -H gitlab.com >> ~/.ssh/known_hosts || true |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 246 | fi |
Petr Michalec | b312004 | 2018-03-17 11:17:32 +0100 | [diff] [blame] | 247 | |
| 248 | # for root |
| 249 | #conf=/root/.ssh/config |
| 250 | #$SUDO mkdir -p $(dirname $conf) |
| 251 | #$SUDO touch $conf |
| 252 | #if ! $SSH_STRICTHOSTKEYCHECKING; then |
| 253 | # echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee -a $conf >/dev/null |
| 254 | #fi |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Petr Michalec | ca2471f | 2018-03-17 11:17:32 +0100 | [diff] [blame] | 257 | # DEPRECATED |
| 258 | system_config_ssh_conf() { |
| 259 | # for backward compatibility |
| 260 | ci_config_ssh |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | system_config_salt_modules_prereq() { |
| 264 | # salt-formulas custom modules dependencies, etc: |
Petr Ruzicka | de2cd3b | 2017-12-07 14:41:20 +0100 | [diff] [blame] | 265 | $SUDO $PKGTOOL install -y apt-transport-https curl git iproute2 openssh-client python-psutil python-apt python-m2crypto python-oauth python-pip sudo &>/dev/null |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 268 | system_config_minion() { |
| 269 | log_info "System configuration salt minion" |
| 270 | } |
| 271 | |
| 272 | system_config_master() { |
| 273 | log_info "System configuration salt master" |
| 274 | |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 275 | system_config_salt_modules_prereq |
Petr Michalec | ca2471f | 2018-03-17 11:17:32 +0100 | [diff] [blame] | 276 | ci_config_ssh |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 277 | |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 278 | if ! grep '127.0.1.2.*salt' /etc/hosts; then |
| 279 | echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null |
| 280 | fi |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | configure_salt_master() |
| 284 | { |
| 285 | |
| 286 | echo "Configuring salt-master ..." |
| 287 | |
Petr Michalec | 4be5e5b | 2017-08-17 11:44:49 +0200 | [diff] [blame] | 288 | if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then |
Petr Michalec | 29d3ea3 | 2017-10-31 17:22:36 +0100 | [diff] [blame] | 289 | read -d '' IGNORE_CLASS_NOTFOUND <<-EOF |
| 290 | ignore_class_notfound: True |
| 291 | ignore_class_regexp: |
| 292 | - 'service.*' |
| 293 | EOF |
Petr Michalec | 4be5e5b | 2017-08-17 11:44:49 +0200 | [diff] [blame] | 294 | fi |
| 295 | |
Petr Michalec | dd1a047 | 2017-09-06 19:27:24 +0200 | [diff] [blame] | 296 | # to force alternative reclass module path |
Petr Michalec | d9d0a32 | 2017-09-06 19:38:32 +0200 | [diff] [blame] | 297 | if [ -n "$RECLASS_SOURCE_PATH" ]; then |
| 298 | RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}" |
| 299 | else |
| 300 | RECLASS_SOURCE_PATH="" |
| 301 | fi |
Petr Michalec | dd1a047 | 2017-09-06 19:27:24 +0200 | [diff] [blame] | 302 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 303 | [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d |
| 304 | cat <<-EOF > /etc/salt/master.d/master.conf |
| 305 | file_roots: |
| 306 | base: |
| 307 | - /usr/share/salt-formulas/env |
| 308 | prd: |
| 309 | - /srv/salt/env/prd |
| 310 | dev: |
| 311 | - /srv/salt/env/dev |
| 312 | pillar_opts: False |
| 313 | open_mode: True |
| 314 | reclass: &reclass |
| 315 | storage_type: yaml_fs |
| 316 | inventory_base_uri: ${RECLASS_ROOT} |
Petr Michalec | 4be5e5b | 2017-08-17 11:44:49 +0200 | [diff] [blame] | 317 | ${IGNORE_CLASS_NOTFOUND} |
Petr Michalec | dd1a047 | 2017-09-06 19:27:24 +0200 | [diff] [blame] | 318 | ${RECLASS_SOURCE_PATH} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 319 | ext_pillar: |
| 320 | - reclass: *reclass |
azvyagintsev | 11927a6 | 2019-02-11 12:28:52 +0200 | [diff] [blame] | 321 | - gpg: {} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 322 | master_tops: |
| 323 | reclass: *reclass |
| 324 | EOF |
| 325 | |
| 326 | echo "Configuring reclass ..." |
| 327 | |
| 328 | [ ! -d /etc/reclass ] && mkdir /etc/reclass |
| 329 | cat <<-EOF > /etc/reclass/reclass-config.yml |
| 330 | storage_type: yaml_fs |
| 331 | pretty_print: True |
| 332 | output: yaml |
| 333 | inventory_base_uri: ${RECLASS_ROOT} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 334 | EOF |
| 335 | |
| 336 | clone_reclass |
| 337 | # override some envs from cluster level *.env, use with care |
| 338 | source_local_envs |
| 339 | |
| 340 | cd ${RECLASS_ROOT} |
| 341 | if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 342 | git submodule update --init --recursive |
sgarbuz | 43c50ea | 2019-03-29 14:49:24 +0200 | [diff] [blame] | 343 | if [ -n "${RECLASS_SYSTEM_BRANCH}" ]; then |
| 344 | pushd classes/system |
| 345 | git checkout master && git clean -fd |
| 346 | if echo ${RECLASS_SYSTEM_BRANCH} | egrep -q "^refs"; then |
| 347 | git fetch ${RECLASS_SYSTEM_ADDRESS} ${RECLASS_SYSTEM_BRANCH} && git checkout FETCH_HEAD |
| 348 | else |
| 349 | git checkout ${RECLASS_SYSTEM_BRANCH} |
| 350 | fi |
| 351 | popd |
| 352 | fi |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 353 | fi |
| 354 | |
Petr Michalec | 212cc6a | 2017-08-17 21:39:40 +0200 | [diff] [blame] | 355 | mkdir -vp ${RECLASS_ROOT}/nodes/_generated |
Petr Michalec | 51ece84 | 2017-09-06 20:44:15 +0200 | [diff] [blame] | 356 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 357 | CONFIG=$(find ${RECLASS_ROOT}/nodes -name ${MINION_ID}.yml| grep yml | tail -n1) |
Petr Michalec | 212cc6a | 2017-08-17 21:39:40 +0200 | [diff] [blame] | 358 | CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 359 | if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then |
Petr Michalec | 24b30e8 | 2017-08-21 10:59:18 +0200 | [diff] [blame] | 360 | log_warn "Salt Master node specification has not been found in model." |
| 361 | log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}" |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 362 | cat <<-EOF > ${CONFIG} |
| 363 | classes: |
Petr Michalec | d81d1f5 | 2017-08-17 20:18:06 +0200 | [diff] [blame] | 364 | - cluster.${CLUSTER_NAME}.infra.config |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 365 | parameters: |
| 366 | _param: |
Petr Michalec | a7de6df | 2017-08-31 12:16:57 +0200 | [diff] [blame] | 367 | salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 368 | salt_master_base_environment: $SALT_ENV |
Petr Michalec | d81d1f5 | 2017-08-17 20:18:06 +0200 | [diff] [blame] | 369 | salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master} |
ibumarskov | f72e05e | 2017-11-29 14:37:48 +0300 | [diff] [blame] | 370 | reclass_data_revision: ${RECLASS_REVISION:-$RECLASS_BRANCH} |
Petr Michalec | d81d1f5 | 2017-08-17 20:18:06 +0200 | [diff] [blame] | 371 | reclass_data_repository: "$RECLASS_ADDRESS" |
Petr Michalec | a7de6df | 2017-08-31 12:16:57 +0200 | [diff] [blame] | 372 | reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME} |
Petr Michalec | d81d1f5 | 2017-08-17 20:18:06 +0200 | [diff] [blame] | 373 | linux_system_codename: ${DISTRIB_CODENAME} |
| 374 | cluster_name: ${CLUSTER_NAME} |
| 375 | cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 376 | linux: |
| 377 | system: |
Petr Michalec | d81d1f5 | 2017-08-17 20:18:06 +0200 | [diff] [blame] | 378 | name: ${HOSTNAME:-cfg01} |
| 379 | domain: ${DOMAIN:-$CLUSTER_NAME.local} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 380 | # ######## |
| 381 | EOF |
| 382 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 383 | cat <<-EOF >> ${CONFIG} |
| 384 | salt: |
| 385 | master: |
| 386 | accept_policy: open_mode |
| 387 | source: |
| 388 | engine: $SALT_SOURCE |
azvyagintsev | 11927a6 | 2019-02-11 12:28:52 +0200 | [diff] [blame] | 389 | ext_pillars: |
| 390 | 100_gpg_pillar_renderer: |
| 391 | module: gpg |
| 392 | params: {} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 393 | minion: |
| 394 | source: |
| 395 | engine: $SALT_SOURCE |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 396 | # ######## |
| 397 | # vim: ft=yaml sw=2 ts=2 sts=2 |
| 398 | EOF |
| 399 | fi |
Petr Michalec | 24b30e8 | 2017-08-21 10:59:18 +0200 | [diff] [blame] | 400 | |
| 401 | log_debug "Salt Master node config yaml:" |
| 402 | log_debug "$(cat ${CONFIG})" |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | configure_salt_minion() |
| 406 | { |
| 407 | [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d |
| 408 | cat <<-EOF > /etc/salt/minion.d/minion.conf |
Petr Michalec | a7de6df | 2017-08-31 12:16:57 +0200 | [diff] [blame] | 409 | master: ${MASTER_IP:-$MASTER_HOSTNAME} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 410 | id: $MINION_ID |
| 411 | EOF |
| 412 | } |
| 413 | |
Petr Michalec | a6cd2bd | 2017-09-07 16:59:19 +0200 | [diff] [blame] | 414 | install_reclass() |
| 415 | { |
| 416 | VERSION=${1:-$RECLASS_VERSION} |
Petr Michalec | 372fa03 | 2017-11-15 17:36:19 +0100 | [diff] [blame] | 417 | VERSION=${VERSION:-pkg} |
Petr Michalec | 800dd07 | 2017-11-15 17:19:26 +0100 | [diff] [blame] | 418 | case ${VERSION} in |
| 419 | pkg|package) |
| 420 | which reclass || $SUDO $PKGTOOL install -y reclass |
root | b9e5d7c | 2018-03-17 12:12:44 +0000 | [diff] [blame] | 421 | which reclass-salt || { |
| 422 | if [ -e /usr/share/reclass/reclass-salt ]; then |
Petr Michalec | 6ba1c36 | 2018-03-17 15:01:51 +0100 | [diff] [blame] | 423 | ln -fs /usr/share/reclass/reclass-salt /usr/bin |
root | b9e5d7c | 2018-03-17 12:12:44 +0000 | [diff] [blame] | 424 | fi |
| 425 | } |
Petr Michalec | 800dd07 | 2017-11-15 17:19:26 +0100 | [diff] [blame] | 426 | ;; |
| 427 | *) |
Petr Michalec | 981f3cc | 2017-11-15 17:40:31 +0100 | [diff] [blame] | 428 | log_warn "Install development version of reclass" |
Petr Michalec | 1b37d3d | 2017-11-15 18:20:46 +0100 | [diff] [blame] | 429 | # Note: dev/git/pip version... |
| 430 | # It replaces all local reclass versions on system |
| 431 | # Required for reclass/git features: |
| 432 | # $SUDO $PKGTOOL install -y libffi-dev libgit2-dev || true |
Petr Michalec | 800dd07 | 2017-11-15 17:19:26 +0100 | [diff] [blame] | 433 | for s in $(python -c "import site; print(' '.join(site.getsitepackages()))"); do |
Petr Michalec | 2302083 | 2017-11-15 17:34:49 +0100 | [diff] [blame] | 434 | sudo -H pip install --install-option="--prefix=" --upgrade --force-reinstall -I \ |
Petr Michalec | 800dd07 | 2017-11-15 17:19:26 +0100 | [diff] [blame] | 435 | -t "$s" git+https://github.com/salt-formulas/reclass.git@${VERSION}; |
| 436 | done |
| 437 | ;; |
| 438 | esac |
Petr Michalec | a6cd2bd | 2017-09-07 16:59:19 +0200 | [diff] [blame] | 439 | } |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 440 | |
| 441 | install_salt_master_pkg() |
| 442 | { |
| 443 | echo -e "\nPreparing base OS repository ...\n" |
| 444 | |
| 445 | configure_pkg_repo |
| 446 | |
| 447 | echo -e "\nInstalling salt master ...\n" |
| 448 | |
Vladimir Khlyunev | b564ae1 | 2021-07-14 16:23:45 +0400 | [diff] [blame] | 449 | # vkhlyunev: Gerrit returns file only in base64 encode, but there are other places (like offline image) |
| 450 | # that uses common plain-text files - ugly "solution" below |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 451 | case $PLATFORM_FAMILY in |
| 452 | debian) |
| 453 | $SUDO apt-get install -y git |
Vladimir Khlyunev | b564ae1 | 2021-07-14 16:23:45 +0400 | [diff] [blame] | 454 | ( curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d ) && ( |
| 455 | curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null ) || ( |
| 456 | curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null ) |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 457 | ;; |
| 458 | rhel) |
| 459 | yum install -y git |
Vladimir Khlyunev | b564ae1 | 2021-07-14 16:23:45 +0400 | [diff] [blame] | 460 | ( curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d ) && ( |
| 461 | curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null ) || ( |
| 462 | curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null ) |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 463 | ;; |
| 464 | esac |
Jakub Josef | 0b61174 | 2018-01-09 17:02:01 +0100 | [diff] [blame] | 465 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 466 | configure_salt_master |
| 467 | |
| 468 | echo -e "\nRestarting services ...\n" |
| 469 | [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub |
| 470 | $SVCTOOL salt-master restart |
| 471 | } |
| 472 | |
| 473 | install_salt_master_pip() |
| 474 | { |
| 475 | echo -e "\nPreparing base OS repository ...\n" |
| 476 | |
| 477 | case $PLATFORM_FAMILY in |
| 478 | debian) |
| 479 | $SUDO apt-get install -y python-pip python-dev zlib1g-dev git |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 480 | ;; |
| 481 | rhel) |
| 482 | $SUDO yum install -y git |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 483 | ;; |
| 484 | esac |
| 485 | |
| 486 | echo -e "\nInstalling salt master ...\n" |
| 487 | # TODO: replace with saltstack bootstrap script |
Jakub Josef | 0b61174 | 2018-01-09 17:02:01 +0100 | [diff] [blame] | 488 | |
Petr Michalec | 88a95a2 | 2018-01-09 17:21:08 +0100 | [diff] [blame] | 489 | if [ -z ${PIP_SALT_VERSION} ] || [ "$PIP_SALT_VERSION" == "latest" ]; then |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 490 | pip install salt |
| 491 | else |
Petr Michalec | 88a95a2 | 2018-01-09 17:21:08 +0100 | [diff] [blame] | 492 | pip install salt==${PIP_SALT_VERSION} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 493 | fi |
| 494 | |
| 495 | 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 |
| 496 | ln -s /usr/local/bin/salt-master /usr/bin/salt-master |
| 497 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 498 | configure_salt_master |
| 499 | |
| 500 | echo -e "\nRestarting services ...\n" |
| 501 | [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub |
| 502 | $SVCTOOL salt-master restart |
| 503 | } |
| 504 | |
| 505 | |
| 506 | |
| 507 | install_salt_minion_pkg() |
| 508 | { |
| 509 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 510 | echo -e "\nInstalling salt minion ...\n" |
| 511 | |
Vladimir Khlyunev | b564ae1 | 2021-07-14 16:23:45 +0400 | [diff] [blame] | 512 | # vkhlyunev: Gerrit returns file only in base64 encode, but there are other places (like offline image) |
| 513 | # that uses common plain-text files - ugly "solution" below |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 514 | case $PLATFORM_FAMILY in |
| 515 | debian) |
Vladimir Khlyunev | b564ae1 | 2021-07-14 16:23:45 +0400 | [diff] [blame] | 516 | ( curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d) && ( |
| 517 | curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null ) || ( |
| 518 | curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null ) |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 519 | ;; |
| 520 | rhel) |
Vladimir Khlyunev | b564ae1 | 2021-07-14 16:23:45 +0400 | [diff] [blame] | 521 | ( curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d) && ( |
| 522 | curl -L ${BOOTSTRAP_SALTSTACK_COM} | base64 -d | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null ) || ( |
| 523 | curl -L ${BOOTSTRAP_SALTSTACK_COM} | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null ) |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 524 | ;; |
| 525 | esac |
| 526 | |
| 527 | |
| 528 | configure_salt_minion |
Petr Michalec | 3b7fdd8 | 2017-09-08 15:35:56 +0200 | [diff] [blame] | 529 | #$SVCTOOL salt-minion restart |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | install_salt_minion_pip() |
| 533 | { |
| 534 | echo -e "\nInstalling salt minion ...\n" |
| 535 | |
| 536 | 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 |
| 537 | ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion |
| 538 | |
| 539 | configure_salt_minion |
Petr Michalec | 3b7fdd8 | 2017-09-08 15:35:56 +0200 | [diff] [blame] | 540 | #$SVCTOOL salt-minion restart |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 541 | } |
| 542 | |
azvyagintsev | e93f837 | 2018-09-12 16:50:58 +0300 | [diff] [blame] | 543 | function install_salt_formula_pkg_all(){ |
| 544 | log_warn "Installing and configuring ALL formulas ..." |
| 545 | if ! $SUDO apt-get install -y 'salt-formula-*'; then |
| 546 | echo -e "\nInstall ALL formulas by mask 'salt-formula-*' failed.\n" |
| 547 | exit 1 |
| 548 | fi |
| 549 | for formula_service in $(ls ${FORMULAS_PATH}/reclass/service/); do |
| 550 | #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement |
| 551 | formula_service=${formula_service//-/$'_'} |
| 552 | if [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ]; then |
| 553 | ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service} |
| 554 | fi |
| 555 | done |
| 556 | } |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 557 | |
| 558 | install_salt_formula_pkg() |
| 559 | { |
| 560 | configure_pkg_repo |
| 561 | |
| 562 | case $PLATFORM_FAMILY in |
| 563 | debian) |
| 564 | echo "Configuring necessary formulas ..." |
| 565 | |
| 566 | [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service |
azvyagintsev | a7a91d5 | 2018-09-13 12:58:39 +0300 | [diff] [blame] | 567 | if [[ ${EXTRA_FORMULAS_PKG_ALL} =~ ^(True|true|1|yes)$ ]]; then |
azvyagintsev | e93f837 | 2018-09-12 16:50:58 +0300 | [diff] [blame] | 568 | install_salt_formula_pkg_all |
| 569 | return |
| 570 | fi |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 571 | # Set essentials if FORMULAS_SALT_MASTER is not defined at all |
Petr Michalec | 2d75397 | 2017-11-30 16:24:37 +0100 | [diff] [blame] | 572 | [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached") |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 573 | for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do |
| 574 | echo -e "\nConfiguring salt formula ${formula_service} ...\n" |
| 575 | [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \ |
| 576 | if ! $SUDO apt-get install -y salt-formula-${formula_service}; then |
| 577 | echo -e "\nInstall salt-formula-${formula_service} failed.\n" |
| 578 | exit 1 |
| 579 | fi |
Oleksii Grudev | 691d471 | 2018-04-02 10:23:46 +0300 | [diff] [blame] | 580 | #Since some salt formula names contain "-" and in symlinks they should contain "_" adding replacement |
| 581 | formula_service=${formula_service//-/$'_'} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 582 | [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \ |
| 583 | ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service} |
| 584 | done |
| 585 | ;; |
| 586 | rhel) |
| 587 | # TODO |
| 588 | ;; |
| 589 | esac |
| 590 | |
| 591 | [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo "" |
| 592 | [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo "" |
| 593 | } |
| 594 | |
| 595 | install_salt_formula_git() |
| 596 | { |
| 597 | echo "Configuring necessary formulas ..." |
| 598 | |
| 599 | [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service |
| 600 | # Set essentials if FORMULAS_SALT_MASTER is not defined at all |
Petr Michalec | 2d75397 | 2017-11-30 16:24:37 +0100 | [diff] [blame] | 601 | [ -z ${FORMULAS_SALT_MASTER} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached") |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 602 | for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do |
| 603 | echo -e "\nConfiguring salt formula ${formula_service} ...\n" |
| 604 | _BRANCH=${FORMULAS_BRANCH} |
| 605 | [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && { |
| 606 | if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then |
| 607 | # Fallback to the master branch if the branch doesn't exist for this repository |
| 608 | _BRANCH=master |
| 609 | fi |
| 610 | if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then |
| 611 | echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n" |
| 612 | exit 1 |
| 613 | fi |
| 614 | } || { |
| 615 | cd ${FORMULAS_PATH}/env/_formulas/${formula_service}; |
| 616 | git fetch origin/${_BRANCH} || git fetch --all |
| 617 | git checkout ${_BRANCH} && git pull || git pull; |
| 618 | cd - |
| 619 | } |
| 620 | [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \ |
| 621 | ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service} |
| 622 | [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \ |
| 623 | ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service} |
| 624 | done |
| 625 | |
| 626 | [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo "" |
| 627 | [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo "" |
| 628 | } |
| 629 | |
Petr Michalec | e9c8452 | 2017-12-13 17:14:08 +0100 | [diff] [blame] | 630 | saltservice_stop() { |
Petr Michalec | dd646db | 2017-12-18 15:56:18 +0100 | [diff] [blame] | 631 | set +e |
Petr Michalec | e9c8452 | 2017-12-13 17:14:08 +0100 | [diff] [blame] | 632 | $SUDO service salt-minion stop |
| 633 | $SUDO service salt-master stop |
| 634 | sleep ${SALT_STOPSTART_WAIT:-30} |
Petr Michalec | 7822efe | 2017-12-15 16:19:32 +0100 | [diff] [blame] | 635 | ${SUDO} pkill -9 salt-master && ${SUDO} rm -rf /var/run/salt/master/* || true |
Petr Michalec | e9c8452 | 2017-12-13 17:14:08 +0100 | [diff] [blame] | 636 | ${SUDO} pkill -9 salt-minion |
| 637 | } |
| 638 | saltservice_start() { |
Petr Michalec | dd646db | 2017-12-18 15:56:18 +0100 | [diff] [blame] | 639 | set +e |
Petr Michalec | e9c8452 | 2017-12-13 17:14:08 +0100 | [diff] [blame] | 640 | $SUDO service salt-master start |
| 641 | $SUDO service salt-minion start |
| 642 | sleep ${SALT_STOPSTART_WAIT:-30} |
| 643 | } |
| 644 | |
| 645 | saltservice_restart() { |
Petr Michalec | dd646db | 2017-12-18 15:56:18 +0100 | [diff] [blame] | 646 | set +e |
Petr Michalec | e9c8452 | 2017-12-13 17:14:08 +0100 | [diff] [blame] | 647 | saltservice_stop |
| 648 | saltservice_start |
| 649 | } |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 650 | |
| 651 | saltmaster_bootstrap() { |
| 652 | |
| 653 | log_info "Salt master setup" |
| 654 | test -n "$MASTER_HOSTNAME" || exit 1 |
| 655 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 656 | # override some envs from cluster level *.env, use with care |
| 657 | source_local_envs |
| 658 | |
Petr Michalec | e9c8452 | 2017-12-13 17:14:08 +0100 | [diff] [blame] | 659 | saltservice_stop |
| 660 | |
| 661 | clone_reclass |
| 662 | |
projoke | 3c30e7d | 2017-10-19 17:15:40 +0800 | [diff] [blame] | 663 | SCRIPTS=$(dirname $0) |
Petr Michalec | e9c8452 | 2017-12-13 17:14:08 +0100 | [diff] [blame] | 664 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 665 | test -e ${SCRIPTS}/.salt-master-setup.sh.passed || { |
Adam Tengler | 035f24f | 2017-09-11 19:29:31 +0200 | [diff] [blame] | 666 | export MASTER_IP=${MASTER_IP:-127.0.0.1} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 667 | export MINION_ID=${MASTER_HOSTNAME} |
| 668 | if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then |
| 669 | SALT_MASTER_SETUP_OUTPUT='/dev/stdout' |
| 670 | fi |
| 671 | # call local "setup() master" |
| 672 | #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then |
| 673 | if ! setup master; then |
| 674 | #cat /tmp/salt-master-setup.log |
| 675 | log_err "salt master setup() failed." |
| 676 | exit 1 |
| 677 | else |
| 678 | $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed |
| 679 | fi |
| 680 | } |
| 681 | |
Petr Michalec | 981f3cc | 2017-11-15 17:40:31 +0100 | [diff] [blame] | 682 | log_info "Install reclass" |
| 683 | install_reclass ${RECLASS_VERSION/dev*/develop} |
Petr Michalec | a6cd2bd | 2017-09-07 16:59:19 +0200 | [diff] [blame] | 684 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 685 | log_info "Re/starting salt services" |
Petr Michalec | e9c8452 | 2017-12-13 17:14:08 +0100 | [diff] [blame] | 686 | saltservice_restart |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | # Init salt master |
| 690 | saltmaster_init() { |
| 691 | |
| 692 | log_info "Runing saltmaster states" |
| 693 | test -n "$MASTER_HOSTNAME" || exit 1 |
| 694 | |
| 695 | set -e |
| 696 | $SUDO salt-call saltutil.sync_all >/dev/null |
| 697 | |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 698 | # workarond isolated and not fully bootstraped environments |
Petr Michalec | 2801acb | 2017-10-31 15:00:15 +0100 | [diff] [blame] | 699 | if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then |
Petr Michalec | 54ee408 | 2017-11-30 17:02:17 +0100 | [diff] [blame] | 700 | SALT_MASTER_PILLAR='"salt":{"master":{"pillar":{"reclass":{"ignore_class_notfound": '${RECLASS_IGNORE_CLASS_NOTFOUND:-False}', "ignore_class_regexp": ["service.*"]}}}},' |
Petr Michalec | 2801acb | 2017-10-31 15:00:15 +0100 | [diff] [blame] | 701 | fi |
Petr Michalec | 8ce8a65 | 2017-11-30 15:39:20 +0100 | [diff] [blame] | 702 | PILLAR='{'${SALT_MASTER_PILLAR}' "reclass":{"storage":{"data_source":{"engine":"local"}}} }' |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 703 | |
Petr Michalec | 8b92d55 | 2017-11-29 12:28:50 +0100 | [diff] [blame] | 704 | log_info "State: salt.master.env,salt.master.pillar" |
Petr Michalec | 54ee408 | 2017-11-30 17:02:17 +0100 | [diff] [blame] | 705 | if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then |
Petr Michalec | 85ef7ad | 2017-11-30 15:21:19 +0100 | [diff] [blame] | 706 | log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!" |
azvyagintsev | e3f6f40 | 2018-08-23 17:21:49 +0300 | [diff] [blame] | 707 | if [[ $SA_IGNORE_ERROR_SALT_MASTER_ENV =~ ^(False|false|0|no)$ ]]; then |
| 708 | exit 1 |
| 709 | fi |
| 710 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 711 | fi |
| 712 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 713 | # Revert temporary SaltMaster minimal configuration, if any |
| 714 | pushd $RECLASS_ROOT |
| 715 | if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then |
Petr Michalec | 87778fc | 2017-11-02 10:13:25 +0100 | [diff] [blame] | 716 | PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }' |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 717 | git status || true |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 718 | log_warn "Locally modified $RECLASS_ROOT/nodes found. (Possibly salt-master minimized setup from bootstrap.sh call)" |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 719 | log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*." |
| 720 | git checkout -- $RECLASS_ROOT/nodes || true |
| 721 | log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state." |
Petr Michalec | 7edf832 | 2017-11-30 12:01:09 +0100 | [diff] [blame] | 722 | log_info "State: salt.master.env,salt.master.pillar" |
Petr Michalec | 54ee408 | 2017-11-30 17:02:17 +0100 | [diff] [blame] | 723 | if ! $SUDO salt-call ${SALT_OPTS} state.apply salt.master.env,salt.master.pillar pillar="$PILLAR"; then |
Petr Michalec | 85ef7ad | 2017-11-30 15:21:19 +0100 | [diff] [blame] | 724 | log_err "State \`salt.master.env,salt.master.pillar pillar=$PILLAR\` failed, keep your eyes wide open!" |
azvyagintsev | e3f6f40 | 2018-08-23 17:21:49 +0300 | [diff] [blame] | 725 | if [[ $SA_IGNORE_ERROR_SALT_MASTER_ENV =~ ^(False|false|0|no)$ ]]; then |
| 726 | exit 1 |
| 727 | fi |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 728 | fi |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 729 | fi |
| 730 | popd |
| 731 | |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 732 | # finally re-configure salt master conf, ie: may remove ignore_class_notfound option |
| 733 | log_info "State: salt.master.service" |
Jakub Josef | 3256341 | 2018-01-17 17:55:05 +0100 | [diff] [blame] | 734 | # ensure salt master is started |
| 735 | saltservice_start |
Petr Michalec | 9fec8b3 | 2017-10-05 14:46:42 +0200 | [diff] [blame] | 736 | retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true |
Petr Michalec | 4f04377 | 2017-12-19 15:40:54 +0100 | [diff] [blame] | 737 | saltservice_start |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 738 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 739 | log_info "State: salt.master.storage.node" |
| 740 | set +e |
Petr Michalec | 7edf832 | 2017-11-30 12:01:09 +0100 | [diff] [blame] | 741 | # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT. |
| 742 | # FIXME: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT. |
Petr Michalec | 9fec8b3 | 2017-10-05 14:46:42 +0200 | [diff] [blame] | 743 | retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 744 | ret=$? |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 745 | |
Petr Michalec | dd646db | 2017-12-18 15:56:18 +0100 | [diff] [blame] | 746 | set -e |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 747 | if [[ $ret -eq 2 ]]; then |
| 748 | log_err "State reclass.storage.node failed with exit code 2 but continuing." |
| 749 | elif [[ $ret -ne 0 ]]; then |
| 750 | log_err "State reclass.storage.node failed with exit code $ret" |
| 751 | exit 1 |
| 752 | fi |
| 753 | |
Petr Michalec | dd646db | 2017-12-18 15:56:18 +0100 | [diff] [blame] | 754 | set +e |
| 755 | log_info "Updating minion.conf -> master: localhost" |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 756 | $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf |
Petr Michalec | dd646db | 2017-12-18 15:56:18 +0100 | [diff] [blame] | 757 | |
| 758 | log_info "Re/starting salt services" # in order to load new deployed configuration from model |
Petr Michalec | e9c8452 | 2017-12-13 17:14:08 +0100 | [diff] [blame] | 759 | saltservice_restart |
Petr Michalec | dd646db | 2017-12-18 15:56:18 +0100 | [diff] [blame] | 760 | |
| 761 | log_info "Salt Util sync all" |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 762 | $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null |
| 763 | |
| 764 | verify_salt_master |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 765 | |
| 766 | } |
| 767 | |
Petr Michalec | 71e3268 | 2018-03-22 08:34:34 +0100 | [diff] [blame] | 768 | ## CI Workarounds |
| 769 | |
| 770 | function mockup_node_registration() { |
| 771 | |
| 772 | # for dynamic nodes registrations (require github.com/salt-formulas/salt-formula-reclass) |
| 773 | |
| 774 | source /etc/os-release |
| 775 | os_codename=$VERSION_CODENAME |
| 776 | domain=$(hostname -d) |
| 777 | master_ip=$(hostname -I | awk '{print $1}') |
| 778 | fake_ip_preffix=$(echo $master_ip | awk -F. '{print $1"."$2}') |
| 779 | fake_ip_base=10 |
| 780 | |
| 781 | # SHOULD BE ALREADY RUN AS A PART OF BOOTSTRAP |
| 782 | # sync, in order to load custom modules |
| 783 | #salt-call saltutil.sync_all |
| 784 | |
| 785 | # SHOULD BE ALREADY RUN AS A PART OF BOOTSTRAP |
| 786 | #PILLAR='{"reclass":{"storage":{"data_source":{"engine":"local"}}} }' |
| 787 | #salt-call state.apply reclass.storage.node pillar="$PILLAR" > /dev/null 2>/dev/null || true |
| 788 | |
| 789 | # rotate over dynamic hosts |
| 790 | for host_idx in {01..02};do |
| 791 | for host in `salt-call pillar.items reclass:storage |grep '<<node_hostname>>' | awk -F_ '{print $NF}' | sed 's/[0-9]*//g' | egrep -v cfg | sort -u`; do |
| 792 | hostname=${host}${host_idx} |
| 793 | fake_ip_base=$(($fake_ip_base + 1)) |
| 794 | |
| 795 | node_network01_ip="$fake_ip_preffix.11.$fake_ip_base" |
| 796 | node_network02_ip="$fake_ip_preffix.12.$fake_ip_base" |
| 797 | node_network03_ip="$fake_ip_preffix.13.$fake_ip_base" |
| 798 | node_network04_ip="$fake_ip_preffix.14.$fake_ip_base" |
| 799 | node_network05_ip="$fake_ip_preffix.15.$fake_ip_base" |
| 800 | node_network01_iface=$(ls /sys/class/net/ | grep -v lo | sort | head -n1) |
| 801 | node_network02_iface="eth1" |
| 802 | node_network03_iface="eth2" |
| 803 | node_network04_iface="eth3" |
| 804 | node_network05_iface="eth4" |
| 805 | |
| 806 | declare -A vars |
| 807 | vars=( |
| 808 | ["node_master_ip"]= |
| 809 | ["node_os"]=${os_codename} |
| 810 | ["node_deploy_ip"]=${node_network01_ip} |
| 811 | ["node_deploy_iface"]=${node_network01_iface} |
| 812 | ["node_control_ip"]=${node_network02_ip} |
| 813 | ["node_control_iface"]=${node_network02_iface} |
| 814 | ["node_tenant_ip"]=${node_network03_ip} |
| 815 | ["node_tenant_iface"]=${node_network03_iface} |
| 816 | ["node_external_ip"]=${node_network04_ip} |
| 817 | ["node_external_iface"]=${node_network04_iface} |
| 818 | ["node_baremetal_ip"]=${node_network05_ip} |
| 819 | ["node_baremetal_iface"]=${node_network05_iface} |
| 820 | ["node_domain"]=$domain |
| 821 | ["node_cluster"]=$(hostname -d |awk -F. '{print $1}') |
| 822 | ["node_hostname"]=$hostname |
| 823 | ) |
| 824 | |
| 825 | data=""; i=0 |
| 826 | for key in "${!vars[@]}"; do |
| 827 | data+="\"${key}\": \"${vars[${key}]}\"" |
| 828 | i=$(($i+1)) |
| 829 | if [ $i -lt ${#vars[@]} ]; then |
| 830 | data+=", " |
| 831 | fi |
| 832 | done |
| 833 | echo "Classifying node $hostname" |
| 834 | NODECR='"node_name": "'${hostname}.${domain}'", "node_data": {'$data'}' |
| 835 | PILLAR='{'${NODECR}', "reclass":{"storage":{"data_source":{"engine":"local"}}} }' |
| 836 | salt-call state.apply reclass.reactor_sls.node_register pillar="$PILLAR" #-l info |
| 837 | #salt-call event.send "reclass/minion/classify" "{$data}" |
| 838 | done |
| 839 | done |
| 840 | |
| 841 | } |
| 842 | |
| 843 | ## VERIFY |
| 844 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 845 | |
| 846 | function verify_salt_master() { |
| 847 | set -e |
| 848 | |
| 849 | log_info "Verify Salt master" |
| 850 | test -n "$MASTER_HOSTNAME" || exit 1 |
| 851 | |
| 852 | if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then |
Petr Michalec | a514134 | 2017-08-16 12:55:20 +0200 | [diff] [blame] | 853 | $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml |
| 854 | $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_pillar > /tmp/${MASTER_HOSTNAME}.reclass.validate_pillar |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 855 | $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles |
| 856 | $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state |
| 857 | $SUDO salt-call --no-color grains.items |
| 858 | $SUDO salt-call --no-color pillar.data |
| 859 | fi |
Petr Michalec | a514134 | 2017-08-16 12:55:20 +0200 | [diff] [blame] | 860 | # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_* |
Petr Michalec | 6e40efc | 2018-03-22 09:47:47 +0100 | [diff] [blame] | 861 | if ! $SUDO python -m reclass.cli --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 862 | log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo" |
| 863 | exit 1 |
| 864 | fi |
Petr Michalec | 7edf832 | 2017-11-30 12:01:09 +0100 | [diff] [blame] | 865 | set +e |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | function verify_salt_minion() { |
| 869 | set -e |
| 870 | node=$1 |
| 871 | log_info "Verifying ${node}" |
| 872 | if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then |
| 873 | $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles |
| 874 | $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate |
| 875 | fi |
Petr Michalec | 6e40efc | 2018-03-22 09:47:47 +0100 | [diff] [blame] | 876 | if ! $SUDO python -m reclass.cli --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 877 | log_err "For more details see full log /tmp/${node}.reclass.nodeinfo" |
| 878 | if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then |
| 879 | exit 1 |
| 880 | fi |
| 881 | fi |
Petr Michalec | 7edf832 | 2017-11-30 12:01:09 +0100 | [diff] [blame] | 882 | set +e |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | function verify_salt_minions() { |
| 886 | #set -e |
Petr Michalec | 679f15b | 2017-09-18 16:16:29 +0200 | [diff] [blame] | 887 | NODES=$(find $RECLASS_ROOT/nodes/_generated/ -name "*.yml" | grep -v "cfg") |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 888 | log_info "Verifying minions: $(echo ${NODES}|xargs)" |
| 889 | |
| 890 | # Parallel |
| 891 | #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify" |
| 892 | #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30 |
| 893 | |
| 894 | function filterFails() { |
| 895 | grep -v '/grains' | tee -a $1 | tail -n20 |
| 896 | } |
| 897 | |
| 898 | log_info "Verify nodes" |
| 899 | passed=0 |
| 900 | for node in ${NODES}; do |
| 901 | node=$(basename $node .yml) |
| 902 | |
| 903 | # filter first in cluster.. ctl-01, mon-01, etc.. |
| 904 | if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then |
| 905 | verify_salt_minion ${node} || continue |
| 906 | else |
| 907 | echo Skipped $node. |
| 908 | fi |
| 909 | passed=$(($passed+1)) |
| 910 | done |
| 911 | # fail on failures |
| 912 | total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l) |
| 913 | test ! $passed -lt $total || log_err "Results: $passed of $total passed." |
| 914 | test ! $passed -lt $total || { |
| 915 | tail -n50 /tmp/*.pillar_verify |
| 916 | return 1 |
| 917 | } |
Petr Michalec | 7edf832 | 2017-11-30 12:01:09 +0100 | [diff] [blame] | 918 | #set +e |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | |
| 922 | |
| 923 | ########################################## |
| 924 | # To install salt master/minon |
| 925 | |
| 926 | function install() { |
| 927 | setup $@ |
| 928 | } |
| 929 | |
| 930 | function setup() { |
| 931 | # CLI |
| 932 | while [ x"$1" != x"" ]; do |
| 933 | which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null |
| 934 | |
| 935 | case $1 in |
| 936 | master ) |
| 937 | install_salt_master_$SALT_SOURCE |
| 938 | install_salt_minion_$SALT_SOURCE |
| 939 | install_salt_formula_$FORMULAS_SOURCE |
| 940 | ;; |
| 941 | minion ) |
| 942 | install_salt_minion_$SALT_SOURCE |
| 943 | ;; |
| 944 | esac |
| 945 | shift |
| 946 | done |
| 947 | echo DONE |
| 948 | } |
| 949 | |
| 950 | function bootstrap() { |
| 951 | log_info "Bootstrap & verification of SaltMaster and configured minions." |
| 952 | trap _atexit INT TERM EXIT |
| 953 | |
| 954 | system_config_master |
| 955 | saltmaster_bootstrap &&\ |
Petr Michalec | 58575b9 | 2017-08-20 10:42:25 +0200 | [diff] [blame] | 956 | saltmaster_init #&&\ |
Petr Michalec | a514134 | 2017-08-16 12:55:20 +0200 | [diff] [blame] | 957 | #verify_salt_minions |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | function default() { |
| 961 | bootstrap $@ |
| 962 | } |
| 963 | |
| 964 | |
| 965 | ########################################## |
azvyagintsev | e93f837 | 2018-09-12 16:50:58 +0300 | [diff] [blame] | 966 | log_info "Running version: ${__ScriptVersion}" |
| 967 | log_info "Command line: '${__ScriptFullName} ${__ScriptArgs}'" |
| 968 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 969 | [[ "$0" != "$BASH_SOURCE" ]] || { |
| 970 | # unless file is being sourced |
| 971 | default $@ |
| 972 | } |