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