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