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 | |
| 9 | # TODO: |
| 10 | # - use PPA repository as formula source |
| 11 | # - support for spm/yum |
| 12 | |
| 13 | |
| 14 | # Source specific env vars. |
| 15 | # shopt -u dotglob |
| 16 | export RECLASS_ROOT=${RECLASS_ROOT:-/srv/salt/reclass} |
| 17 | function source_local_envs() { |
Petr Michalec | b444ef9 | 2017-08-17 13:53:14 +0200 | [diff] [blame] | 18 | 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] | 19 | for f in $(find $path -maxdepth 1 -name '*.env' 2> /dev/null); do |
| 20 | echo "Sourcing env variables from $f" |
| 21 | source $f |
| 22 | done |
| 23 | done |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 24 | } |
| 25 | source_local_envs |
| 26 | |
| 27 | ########################################## |
| 28 | # Set defaults env variables |
| 29 | |
| 30 | if [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then |
| 31 | set -x |
| 32 | SALT_LOG_LEVEL="--state-verbose=true -ldebug" |
| 33 | fi |
| 34 | |
| 35 | export MAGENTA='\033[0;95m' |
| 36 | export YELLOW='\033[1;33m' |
| 37 | export BLUE='\033[0;35m' |
| 38 | export CYAN='\033[0;96m' |
| 39 | export RED='\033[0;31m' |
| 40 | export NC='\033[0m' # No Color' |
| 41 | |
| 42 | export LC_ALL=C |
| 43 | export SALT_LOG_LEVEL="--state-verbose=false -lerror" |
| 44 | export SALT_OPTS="${SALT_OPTS:- --timeout=120 --state-output=changes --retcode-passthrough --force-color $SALT_LOG_LEVEL }" |
| 45 | export SALT_STATE_RETRY=${SALT_STATE_RETRY:-3} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 46 | |
| 47 | |
| 48 | # salt apt repository |
| 49 | test -e /etc/lsb-release && eval $(cat /etc/lsb-release) |
| 50 | which lsb_release && DISTRIB_CODENAME=${DISTRIB_CODENAME:-$(lsb_release -cs)} |
| 51 | # |
| 52 | export APT_REPOSITORY="deb [arch=amd64] http://apt-mk.mirantis.com/${DISTRIB_CODENAME} ${DISTRIB_REVISION:-stable} salt" |
| 53 | export APT_REPOSITORY_GPG=${APT_REPOSITORY_GPG:-http://apt-mk.mirantis.com/public.gpg} |
| 54 | |
| 55 | # reclass |
| 56 | export RECLASS_ADDRESS=${RECLASS_ADDRESS:-https://github.com/salt-formulas/openstack-salt.git} # https/git |
| 57 | |
| 58 | # formula |
| 59 | export FORMULAS_BASE=${FORMULAS_BASE:-https://github.com/salt-formulas} |
| 60 | export FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas} |
| 61 | export FORMULAS_BRANCH=${FORMULAS_BRANCH:-master} |
| 62 | export FORMULAS_SOURCE=${FORMULAS_SOURCE:-pkg} # pkg/git |
| 63 | # essential set of formulas (known to by used on cfg01 node for most setups) |
chnyda | 86d63b4 | 2017-08-31 10:05:00 +0200 | [diff] [blame] | 64 | FORMULAS_SALT_MASTER=${FORMULAS_SALT_MASTER:- $EXTRA_FORMULAS memcached openssh 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} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 65 | # minimal set of formulas for salt-master bootstrap |
| 66 | declare -a FORMULAS_SALT_MASTER=(linux reclass salt git $(echo $FORMULAS_SALT_MASTER)) |
| 67 | export FORMULAS_SALT_MASTER |
| 68 | |
| 69 | # system / host |
Petr Michalec | c749e86 | 2017-09-06 21:10:10 +0200 | [diff] [blame] | 70 | export HOSTNAME=${HOSTNAME:-`hostname -s`} |
| 71 | export HOSTNAME=${HOSTNAME//.*/} |
| 72 | export DOMAIN=${DOMAIN:-`hostname -d`} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 73 | export DOMAIN=${DOMAIN:-bootstrap.local} |
| 74 | |
| 75 | # salt |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 76 | export MINION_ID=${MINION_ID:-${HOSTNAME}.${DOMAIN}} |
Petr Michalec | 2e3a499 | 2017-08-17 14:20:43 +0200 | [diff] [blame] | 77 | export MASTER_HOSTNAME=${MASTER_HOSTNAME:-${HOSTNAME}.${DOMAIN}} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 78 | |
| 79 | # saltstack |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 80 | SALT_SOURCE=${SALT_SOURCE:-pkg} |
Petr Michalec | 33c0489 | 2017-09-08 15:10:30 +0200 | [diff] [blame^] | 81 | SALT_VERSION=${SALT_VERSION:-2016.3} |
| 82 | BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True} |
| 83 | BOOTSTRAP_SALTSTACK_VERSION=${BOOTSTRAP_SALTSTACK_VERSION:- stable $SALT_VERSION } |
| 84 | BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX $BOOTSTRAP_SALTSTACK_VERSION } |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 85 | |
| 86 | # environment |
| 87 | if [ "$FORMULAS_SOURCE" == "git" ]; then |
| 88 | SALT_ENV=${SALT_ENV:-dev} |
| 89 | elif [ "$FORMULAS_SOURCE" == "pkg" ]; then |
| 90 | SALT_ENV=${SALT_ENV:-prd} |
| 91 | fi |
Petr Michalec | 05b5245 | 2017-09-08 14:26:59 +0200 | [diff] [blame] | 92 | eval "$(grep -h '=' /etc/*release 2> /dev/null)" |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 93 | PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z) |
| 94 | case $PLATFORM_FAMILY in |
| 95 | debian ) |
| 96 | PKGTOOL="$SUDO apt-get" |
| 97 | test ${VERSION_ID//\.*/} -ge 16 && { |
| 98 | SVCTOOL=service |
| 99 | } || { SVCTOOL=service |
| 100 | } |
| 101 | ;; |
| 102 | rhel ) |
| 103 | PKGTOOL="$SUDO yum" |
| 104 | test ${VERSION_ID//\.*/} -ge 7 && { |
| 105 | SVCTOOL=systemctl |
| 106 | } || { SVCTOOL=service |
| 107 | } |
| 108 | ;; |
| 109 | esac |
| 110 | |
| 111 | export PLATFORM_FAMILY |
| 112 | export PKGTOOL |
| 113 | export SVCTOOL |
| 114 | |
| 115 | ########################################## |
| 116 | # FUNCTIONS |
| 117 | |
| 118 | log_info() { |
| 119 | echo -e "${YELLOW}[INFO] $* ${NC}" |
| 120 | } |
| 121 | |
| 122 | log_warn() { |
| 123 | echo -e "${MAGENTA}[WARN] $* ${NC}" |
| 124 | } |
| 125 | |
Petr Michalec | 24b30e8 | 2017-08-21 10:59:18 +0200 | [diff] [blame] | 126 | log_debug() { |
| 127 | echo -e "${CYAN}[WARN] $* ${NC}" |
| 128 | } |
| 129 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 130 | log_err() { |
| 131 | echo -e "${RED}[ERROR] $* ${NC}" >&2 |
| 132 | } |
| 133 | |
| 134 | configure_pkg_repo() |
| 135 | { |
| 136 | |
| 137 | case $PLATFORM_FAMILY in |
| 138 | debian) |
| 139 | if [ -n "$APT_REPOSITORY_PPA" ]; then |
| 140 | which add-apt-repository || $SUDO apt-get install -y software-properties-common |
| 141 | $SUDO add-apt-repository -y ppa:${APT_REPOSITORY_PPA} |
| 142 | else |
| 143 | echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/bootstrap.list >/dev/null |
| 144 | curl -sL $APT_REPOSITORY_GPG | $SUDO apt-key add - |
| 145 | fi |
| 146 | $SUDO apt-get clean |
| 147 | $SUDO apt-get update |
| 148 | ;; |
| 149 | rhel) |
| 150 | $SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm |
| 151 | $SUDO yum clean all |
| 152 | ;; |
| 153 | esac |
| 154 | |
| 155 | } |
| 156 | |
| 157 | _atexit() { |
| 158 | RETVAL=$? |
| 159 | trap true INT TERM EXIT |
| 160 | |
| 161 | if [ $RETVAL -ne 0 ]; then |
| 162 | log_err "Execution failed" |
| 163 | else |
| 164 | log_info "Execution successful" |
| 165 | fi |
| 166 | return $RETVAL |
| 167 | } |
| 168 | |
| 169 | retry() { |
| 170 | local tries |
| 171 | if [[ $1 =~ ^[0-9]+$ ]]; then |
| 172 | tries=$1; shift |
| 173 | else |
| 174 | tries=3 |
| 175 | fi |
| 176 | for i in $(seq 1 $tries); do |
| 177 | "$@" && return 0 || sleep $i |
| 178 | done |
| 179 | return 1 |
| 180 | } |
| 181 | |
| 182 | function clone_reclass() { |
Petr Michalec | a361d4e | 2017-09-08 11:38:16 +0200 | [diff] [blame] | 183 | if [ ! -d ${RECLASS_ROOT}/classes ]; then |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 184 | # No reclass at all, clone from given address |
| 185 | ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true |
| 186 | if echo ${RECLASS_BRANCH:-master} | egrep -q "^refs"; then |
| 187 | git clone ${RECLASS_ADDRESS} ${RECLASS_ROOT} |
| 188 | cd ${RECLASS_ROOT} |
| 189 | git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH:-master} && git checkout FETCH_HEAD |
| 190 | cd - |
| 191 | else |
| 192 | git clone -b ${RECLASS_BRANCH:-master} ${RECLASS_ADDRESS} ${RECLASS_ROOT}; |
| 193 | fi; |
| 194 | fi; |
Petr Michalec | a361d4e | 2017-09-08 11:38:16 +0200 | [diff] [blame] | 195 | if [ ! -d ${RECLASS_ROOT}/classes -o ! -d ${RECLASS_ROOT}/nodes ]; then |
| 196 | log_err "Reclass ${RECLASS_ROOT} is not fetched locally;" |
| 197 | ls -Rla ${RECLASS_ROOT} |
| 198 | exit 1 |
| 199 | fi; |
| 200 | $SUDO mkdir -p $RECLASS_ROOT/classes/service |
| 201 | $SUDO mkdir -p $RECLASS_ROOT/nodes/_generated |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
| 205 | ########################################## |
| 206 | # Main calls |
| 207 | |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 208 | system_config_ssh_conf() { |
| 209 | for conf in ~/.ssh/config /root/.ssh/config; do |
| 210 | $SUDO mkdir -p $(dirname $conf) |
| 211 | if ! grep StrictHostKeyChecking $conf; then |
| 212 | # this should be used only in CI environment |
| 213 | echo -e "Host *\n\tStrictHostKeyChecking no\n" | $SUDO tee $conf >/dev/null |
| 214 | fi |
| 215 | done |
| 216 | if ! grep github.com ~/.ssh/known_hosts; then |
| 217 | ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true |
| 218 | fi |
| 219 | } |
| 220 | |
| 221 | system_config_salt_modules_prereq() { |
| 222 | # salt-formulas custom modules dependencies, etc: |
| 223 | $SUDO $PKGTOOL install -y iproute2 curl sudo apt-transport-https python-psutil python-apt python-m2crypto python-oauth python-pip &>/dev/null |
| 224 | } |
| 225 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 226 | system_config_minion() { |
| 227 | log_info "System configuration salt minion" |
| 228 | } |
| 229 | |
| 230 | system_config_master() { |
| 231 | log_info "System configuration salt master" |
| 232 | |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 233 | system_config_salt_modules_prereq |
| 234 | system_config_ssh_conf |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 235 | |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 236 | if ! grep '127.0.1.2.*salt' /etc/hosts; then |
| 237 | echo "127.0.1.2 salt" | $SUDO tee -a /etc/hosts >/dev/null |
| 238 | fi |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 239 | |
| 240 | which reclass || $SUDO $PKGTOOL install -y reclass |
| 241 | |
| 242 | which reclass-salt || { |
| 243 | test -e /usr/share/reclass/reclass-salt && { |
| 244 | ln -fs /usr/share/reclass/reclass-salt /usr/bin |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | configure_salt_master() |
| 250 | { |
| 251 | |
| 252 | echo "Configuring salt-master ..." |
| 253 | |
Petr Michalec | 4be5e5b | 2017-08-17 11:44:49 +0200 | [diff] [blame] | 254 | if [[ $RECLASS_IGNORE_CLASS_NOTFOUND =~ ^(True|true|1|yes)$ ]]; then |
| 255 | IGNORE_CLASS_NOTFOUND="ignore_class_notfound: True" |
| 256 | fi |
| 257 | |
Petr Michalec | dd1a047 | 2017-09-06 19:27:24 +0200 | [diff] [blame] | 258 | # to force alternative reclass module path |
Petr Michalec | d9d0a32 | 2017-09-06 19:38:32 +0200 | [diff] [blame] | 259 | if [ -n "$RECLASS_SOURCE_PATH" ]; then |
| 260 | RECLASS_SOURCE_PATH="reclass_source_path: ${RECLASS_SOURCE_PATH}" |
| 261 | else |
| 262 | RECLASS_SOURCE_PATH="" |
| 263 | fi |
Petr Michalec | dd1a047 | 2017-09-06 19:27:24 +0200 | [diff] [blame] | 264 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 265 | [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d |
| 266 | cat <<-EOF > /etc/salt/master.d/master.conf |
| 267 | file_roots: |
| 268 | base: |
| 269 | - /usr/share/salt-formulas/env |
| 270 | prd: |
| 271 | - /srv/salt/env/prd |
| 272 | dev: |
| 273 | - /srv/salt/env/dev |
| 274 | pillar_opts: False |
| 275 | open_mode: True |
| 276 | reclass: &reclass |
| 277 | storage_type: yaml_fs |
| 278 | inventory_base_uri: ${RECLASS_ROOT} |
Petr Michalec | 4be5e5b | 2017-08-17 11:44:49 +0200 | [diff] [blame] | 279 | ${IGNORE_CLASS_NOTFOUND} |
Petr Michalec | dd1a047 | 2017-09-06 19:27:24 +0200 | [diff] [blame] | 280 | ${RECLASS_SOURCE_PATH} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 281 | ext_pillar: |
| 282 | - reclass: *reclass |
| 283 | master_tops: |
| 284 | reclass: *reclass |
| 285 | EOF |
| 286 | |
| 287 | echo "Configuring reclass ..." |
| 288 | |
| 289 | [ ! -d /etc/reclass ] && mkdir /etc/reclass |
| 290 | cat <<-EOF > /etc/reclass/reclass-config.yml |
| 291 | storage_type: yaml_fs |
| 292 | pretty_print: True |
| 293 | output: yaml |
| 294 | inventory_base_uri: ${RECLASS_ROOT} |
Petr Michalec | 4be5e5b | 2017-08-17 11:44:49 +0200 | [diff] [blame] | 295 | ${IGNORE_CLASS_NOTFOUND} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 296 | EOF |
| 297 | |
| 298 | clone_reclass |
| 299 | # override some envs from cluster level *.env, use with care |
| 300 | source_local_envs |
| 301 | |
| 302 | cd ${RECLASS_ROOT} |
| 303 | if [ ! -d ${RECLASS_ROOT}/classes/system/linux ]; then |
| 304 | # Possibly subrepo checkout needed |
| 305 | git submodule update --init --recursive |
| 306 | fi |
| 307 | |
Petr Michalec | 212cc6a | 2017-08-17 21:39:40 +0200 | [diff] [blame] | 308 | mkdir -vp ${RECLASS_ROOT}/nodes/_generated |
Petr Michalec | 51ece84 | 2017-09-06 20:44:15 +0200 | [diff] [blame] | 309 | rm -rvf ${RECLASS_ROOT}/nodes/_generated/* |
| 310 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 311 | 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] | 312 | CONFIG=${CONFIG:-${RECLASS_ROOT}/nodes/_generated/${MINION_ID}.yml} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 313 | if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then |
Petr Michalec | 24b30e8 | 2017-08-21 10:59:18 +0200 | [diff] [blame] | 314 | log_warn "Salt Master node specification has not been found in model." |
| 315 | log_warn "Creating temporary cfg01 configuration for bootstrap: ${CONFIG}" |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 316 | cat <<-EOF > ${CONFIG} |
| 317 | classes: |
Petr Michalec | d81d1f5 | 2017-08-17 20:18:06 +0200 | [diff] [blame] | 318 | - cluster.${CLUSTER_NAME}.infra.config |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 319 | parameters: |
| 320 | _param: |
Petr Michalec | a7de6df | 2017-08-31 12:16:57 +0200 | [diff] [blame] | 321 | single_address: ${MASTER_IP:-$MASTER_HOSTNAME} |
| 322 | salt_master_host: ${MASTER_IP:-$MASTER_HOSTNAME} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 323 | salt_master_base_environment: $SALT_ENV |
Petr Michalec | d81d1f5 | 2017-08-17 20:18:06 +0200 | [diff] [blame] | 324 | salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master} |
| 325 | reclass_data_revision: ${RECLASS_BRANCH:-master} |
| 326 | reclass_data_repository: "$RECLASS_ADDRESS" |
Petr Michalec | a7de6df | 2017-08-31 12:16:57 +0200 | [diff] [blame] | 327 | reclass_config_master: ${MASTER_IP:-$MASTER_HOSTNAME} |
Petr Michalec | d81d1f5 | 2017-08-17 20:18:06 +0200 | [diff] [blame] | 328 | linux_system_codename: ${DISTRIB_CODENAME} |
| 329 | cluster_name: ${CLUSTER_NAME} |
| 330 | cluster_domain: ${DOMAIN:-$CLUSTER_NAME.local} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 331 | linux: |
| 332 | system: |
Petr Michalec | d81d1f5 | 2017-08-17 20:18:06 +0200 | [diff] [blame] | 333 | name: ${HOSTNAME:-cfg01} |
| 334 | domain: ${DOMAIN:-$CLUSTER_NAME.local} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 335 | # ######## |
| 336 | EOF |
| 337 | |
| 338 | if [ "$SALT_VERSION" == "latest" ]; then |
| 339 | VERSION="" |
| 340 | else |
| 341 | VERSION="version: $SALT_VERSION" |
| 342 | fi |
| 343 | |
| 344 | cat <<-EOF >> ${CONFIG} |
| 345 | salt: |
| 346 | master: |
| 347 | accept_policy: open_mode |
| 348 | source: |
| 349 | engine: $SALT_SOURCE |
| 350 | $VERSION |
| 351 | minion: |
| 352 | source: |
| 353 | engine: $SALT_SOURCE |
| 354 | $VERSION |
| 355 | # ######## |
| 356 | # vim: ft=yaml sw=2 ts=2 sts=2 |
| 357 | EOF |
| 358 | fi |
Petr Michalec | 24b30e8 | 2017-08-21 10:59:18 +0200 | [diff] [blame] | 359 | |
| 360 | log_debug "Salt Master node config yaml:" |
| 361 | log_debug "$(cat ${CONFIG})" |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | configure_salt_minion() |
| 365 | { |
| 366 | [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d |
| 367 | cat <<-EOF > /etc/salt/minion.d/minion.conf |
Petr Michalec | a7de6df | 2017-08-31 12:16:57 +0200 | [diff] [blame] | 368 | master: ${MASTER_IP:-$MASTER_HOSTNAME} |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 369 | id: $MINION_ID |
| 370 | EOF |
| 371 | } |
| 372 | |
Petr Michalec | a6cd2bd | 2017-09-07 16:59:19 +0200 | [diff] [blame] | 373 | install_reclass() |
| 374 | { |
| 375 | VERSION=${1:-$RECLASS_VERSION} |
| 376 | VERSION=${VERSION:-master} |
| 377 | # tries to replace all local version system version |
| 378 | for s in $(python -c "import site; print(' '.join(site.getsitepackages()))"); do |
Petr Michalec | a2f351c | 2017-09-07 17:37:44 +0200 | [diff] [blame] | 379 | sudo -H pip install --upgrade --force-reinstall -I \ |
Petr Michalec | a6cd2bd | 2017-09-07 16:59:19 +0200 | [diff] [blame] | 380 | -t "$s" git+https://github.com/salt-formulas/reclass.git@${VERSION}; |
| 381 | done |
| 382 | } |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 383 | |
| 384 | install_salt_master_pkg() |
| 385 | { |
| 386 | echo -e "\nPreparing base OS repository ...\n" |
| 387 | |
| 388 | configure_pkg_repo |
| 389 | |
| 390 | echo -e "\nInstalling salt master ...\n" |
| 391 | |
| 392 | case $PLATFORM_FAMILY in |
| 393 | debian) |
| 394 | $SUDO apt-get install -y git |
| 395 | which reclass || $SUDO apt install -qqq -y reclass |
| 396 | curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true |
| 397 | ;; |
| 398 | rhel) |
| 399 | yum install -y git |
| 400 | which reclass || $SUDO yum install -y reclass |
| 401 | curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true |
| 402 | ;; |
| 403 | esac |
| 404 | |
| 405 | which reclass-salt || { |
| 406 | test -e /usr/share/reclass/reclass-salt && { |
| 407 | ln -fs /usr/share/reclass/reclass-salt /usr/bin |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | configure_salt_master |
| 412 | |
| 413 | echo -e "\nRestarting services ...\n" |
| 414 | [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub |
| 415 | $SVCTOOL salt-master restart |
| 416 | } |
| 417 | |
| 418 | install_salt_master_pip() |
| 419 | { |
| 420 | echo -e "\nPreparing base OS repository ...\n" |
| 421 | |
| 422 | case $PLATFORM_FAMILY in |
| 423 | debian) |
| 424 | $SUDO apt-get install -y python-pip python-dev zlib1g-dev git |
| 425 | which reclass || $SUDO apt-get install -y reclass |
| 426 | ;; |
| 427 | rhel) |
| 428 | $SUDO yum install -y git |
| 429 | which reclass || $SUDO yum install -y reclass |
| 430 | ;; |
| 431 | esac |
| 432 | |
| 433 | echo -e "\nInstalling salt master ...\n" |
| 434 | # TODO: replace with saltstack bootstrap script |
| 435 | |
| 436 | if [ "$SALT_VERSION" == "latest" ]; then |
| 437 | pip install salt |
| 438 | else |
| 439 | pip install salt==$SALT_VERSION |
| 440 | fi |
| 441 | |
| 442 | 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 |
| 443 | ln -s /usr/local/bin/salt-master /usr/bin/salt-master |
| 444 | |
| 445 | which reclass-salt || { |
| 446 | test -e /usr/share/reclass/reclass-salt && { |
| 447 | ln -fs /usr/share/reclass/reclass-salt /usr/bin |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | configure_salt_master |
| 452 | |
| 453 | echo -e "\nRestarting services ...\n" |
| 454 | [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub |
| 455 | $SVCTOOL salt-master restart |
| 456 | } |
| 457 | |
| 458 | |
| 459 | |
| 460 | install_salt_minion_pkg() |
| 461 | { |
| 462 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 463 | echo -e "\nInstalling salt minion ...\n" |
| 464 | |
| 465 | case $PLATFORM_FAMILY in |
| 466 | debian) |
| 467 | curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true |
| 468 | ;; |
| 469 | rhel) |
| 470 | curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true |
| 471 | ;; |
| 472 | esac |
| 473 | |
| 474 | |
| 475 | configure_salt_minion |
| 476 | |
| 477 | $SVCTOOL salt-minion restart |
| 478 | } |
| 479 | |
| 480 | install_salt_minion_pip() |
| 481 | { |
| 482 | echo -e "\nInstalling salt minion ...\n" |
| 483 | |
| 484 | 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 |
| 485 | ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion |
| 486 | |
| 487 | configure_salt_minion |
| 488 | $SVCTOOL salt-minion restart |
| 489 | } |
| 490 | |
| 491 | |
| 492 | install_salt_formula_pkg() |
| 493 | { |
| 494 | configure_pkg_repo |
| 495 | |
| 496 | case $PLATFORM_FAMILY in |
| 497 | debian) |
| 498 | echo "Configuring necessary formulas ..." |
| 499 | |
| 500 | [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service |
| 501 | # Set essentials if FORMULAS_SALT_MASTER is not defined at all |
| 502 | [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached") |
| 503 | for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do |
| 504 | echo -e "\nConfiguring salt formula ${formula_service} ...\n" |
| 505 | [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \ |
| 506 | if ! $SUDO apt-get install -y salt-formula-${formula_service}; then |
| 507 | echo -e "\nInstall salt-formula-${formula_service} failed.\n" |
| 508 | exit 1 |
| 509 | fi |
| 510 | [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \ |
| 511 | ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} ${RECLASS_ROOT}/classes/service/${formula_service} |
| 512 | done |
| 513 | ;; |
| 514 | rhel) |
| 515 | # TODO |
| 516 | ;; |
| 517 | esac |
| 518 | |
| 519 | [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo "" |
| 520 | [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo "" |
| 521 | } |
| 522 | |
| 523 | install_salt_formula_git() |
| 524 | { |
| 525 | echo "Configuring necessary formulas ..." |
| 526 | |
| 527 | [ ! -d ${RECLASS_ROOT}/classes/service ] && mkdir -p ${RECLASS_ROOT}/classes/service |
| 528 | # Set essentials if FORMULAS_SALT_MASTER is not defined at all |
| 529 | [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached") |
| 530 | for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do |
| 531 | echo -e "\nConfiguring salt formula ${formula_service} ...\n" |
| 532 | _BRANCH=${FORMULAS_BRANCH} |
| 533 | [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && { |
| 534 | if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then |
| 535 | # Fallback to the master branch if the branch doesn't exist for this repository |
| 536 | _BRANCH=master |
| 537 | fi |
| 538 | if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then |
| 539 | echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n" |
| 540 | exit 1 |
| 541 | fi |
| 542 | } || { |
| 543 | cd ${FORMULAS_PATH}/env/_formulas/${formula_service}; |
| 544 | git fetch origin/${_BRANCH} || git fetch --all |
| 545 | git checkout ${_BRANCH} && git pull || git pull; |
| 546 | cd - |
| 547 | } |
| 548 | [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \ |
| 549 | ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service} |
| 550 | [ ! -L "${RECLASS_ROOT}/classes/service/${formula_service}" ] && \ |
| 551 | ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service ${RECLASS_ROOT}/classes/service/${formula_service} |
| 552 | done |
| 553 | |
| 554 | [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo "" |
| 555 | [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo "" |
| 556 | } |
| 557 | |
| 558 | |
| 559 | saltmaster_bootstrap() { |
| 560 | |
| 561 | log_info "Salt master setup" |
| 562 | test -n "$MASTER_HOSTNAME" || exit 1 |
| 563 | |
| 564 | clone_reclass |
| 565 | # override some envs from cluster level *.env, use with care |
| 566 | source_local_envs |
| 567 | |
| 568 | pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {} |
| 569 | pkill -9 salt-minion |
| 570 | test -e ${SCRIPTS}/.salt-master-setup.sh.passed || { |
Petr Michalec | a7de6df | 2017-08-31 12:16:57 +0200 | [diff] [blame] | 571 | export MASTER_IP=localhost |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 572 | export MINION_ID=${MASTER_HOSTNAME} |
| 573 | if ! [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then |
| 574 | SALT_MASTER_SETUP_OUTPUT='/dev/stdout' |
| 575 | fi |
| 576 | # call local "setup() master" |
| 577 | #if ! $SUDO ${SCRIPTS}/salt-master-setup.sh master &> ${SALT_MASTER_SETUP_OUTPUT:-/tmp/salt-master-setup.log}; then |
| 578 | if ! setup master; then |
| 579 | #cat /tmp/salt-master-setup.log |
| 580 | log_err "salt master setup() failed." |
| 581 | exit 1 |
| 582 | else |
| 583 | $SUDO touch ${SCRIPTS}/.salt-master-setup.sh.passed |
| 584 | fi |
| 585 | } |
| 586 | |
Petr Michalec | a6cd2bd | 2017-09-07 16:59:19 +0200 | [diff] [blame] | 587 | if [[ $RECLASS_VERSION =~ ^(dev|devel|master)$ ]]; then |
Petr Michalec | 2f9a35b | 2017-09-07 17:08:06 +0200 | [diff] [blame] | 588 | log_warn "Install development version of reclass" |
Petr Michalec | a6cd2bd | 2017-09-07 16:59:19 +0200 | [diff] [blame] | 589 | install_reclass ${RECLASS_VERSION/dev*/master} |
| 590 | fi |
| 591 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 592 | log_info "Re/starting salt services" |
| 593 | pgrep salt-master | sed /$$/d | xargs --no-run-if-empty -i{} $SUDO kill -9 {} |
| 594 | pkill -9 salt-minion |
| 595 | sleep 1 |
| 596 | $SUDO service salt-master restart |
| 597 | $SUDO service salt-minion restart |
| 598 | sleep 10 |
| 599 | } |
| 600 | |
| 601 | # Init salt master |
| 602 | saltmaster_init() { |
| 603 | |
| 604 | log_info "Runing saltmaster states" |
| 605 | test -n "$MASTER_HOSTNAME" || exit 1 |
| 606 | |
| 607 | set -e |
| 608 | $SUDO salt-call saltutil.sync_all >/dev/null |
| 609 | |
| 610 | # TODO: Placeholder update saltmaster spec (nodes/FQDN.yml) to be able to bootstrap with minimal configuration |
| 611 | # (ie: with linux, git, salt formulas) |
| 612 | |
| 613 | #log_info "Verify SaltMaster, before salt-master is fully initialized" |
| 614 | #if ! $SUDO reclass-salt -p ${MASTER_HOSTNAME} &> /tmp/${MASTER_HOSTNAME}.pillar;then |
| 615 | # log_warn "Node verification before initialization failed."; cat /tmp/${MASTER_HOSTNAME}.pillar; |
| 616 | #fi |
| 617 | |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 618 | |
| 619 | # workarond isolated and not fully bootstraped environments |
Petr Michalec | 571d6b8 | 2017-08-18 13:25:24 +0200 | [diff] [blame] | 620 | PILLAR='{"salt":{"master":{"pillar":{"reclass":{"ignore_class_notfound": "'${RECLASS_IGNORE_CLASS_NOTFOUND:-False}'"}}}}, "reclass":{"storage":{"data_source":{"engine":"local"}}} }' |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 621 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 622 | log_info "State: salt.master.env" |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 623 | if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env pillar="$PILLAR"; then |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 624 | log_err "State salt.master.env failed, keep your eyes wide open." |
| 625 | fi |
| 626 | |
| 627 | log_info "State: salt.master.pillar" |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 628 | retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar="$PILLAR" |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 629 | # Note: sikp reclass data dir states |
| 630 | # in order to avoid pull from configured repo/branch |
| 631 | |
| 632 | # Revert temporary SaltMaster minimal configuration, if any |
| 633 | pushd $RECLASS_ROOT |
| 634 | if [ $(git diff --name-only nodes | sort | uniq | wc -l) -ge 1 ]; then |
| 635 | git status || true |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 636 | 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] | 637 | log_info "Checkout HEAD state of $RECLASS_ROOT/nodes/*." |
| 638 | git checkout -- $RECLASS_ROOT/nodes || true |
| 639 | log_info "Re-Run states: salt.master.env and salt.master.pillar according the HEAD state." |
| 640 | log_info "State: salt.master.env" |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 641 | if ! $SUDO salt-call ${SALT_OPTS} -linfo state.apply salt.master.env pillar="$PILLAR"; then |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 642 | log_err "State salt.master.env failed, keep your eyes wide open." |
| 643 | fi |
| 644 | log_info "State: salt.master.pillar" |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 645 | retry ${SALT_STATE_RETRY} $SUDO salt-call ${SALT_OPTS} state.apply salt.master.pillar pillar="$PILLAR" |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 646 | fi |
| 647 | popd |
| 648 | |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 649 | # finally re-configure salt master conf, ie: may remove ignore_class_notfound option |
| 650 | log_info "State: salt.master.service" |
Petr Michalec | 7f3e7e7 | 2017-08-18 13:49:24 +0200 | [diff] [blame] | 651 | $SUDO salt-call ${SALT_OPTS} state.apply salt.master.service || true |
Petr Michalec | da01420 | 2017-08-18 11:26:07 +0200 | [diff] [blame] | 652 | |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 653 | log_info "State: salt.master.storage.node" |
| 654 | set +e |
Petr Michalec | a514134 | 2017-08-16 12:55:20 +0200 | [diff] [blame] | 655 | # TODO: PLACEHOLDER TO TRIGGER NODE GENERATION THROUG SALT REACT. |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 656 | $SUDO salt-call ${SALT_OPTS} state.apply reclass.storage.node |
| 657 | ret=$? |
| 658 | set -e |
| 659 | |
| 660 | if [[ $ret -eq 2 ]]; then |
| 661 | log_err "State reclass.storage.node failed with exit code 2 but continuing." |
| 662 | elif [[ $ret -ne 0 ]]; then |
| 663 | log_err "State reclass.storage.node failed with exit code $ret" |
| 664 | exit 1 |
| 665 | fi |
| 666 | |
| 667 | log_info "Re/starting salt services" |
| 668 | $SUDO sed -i 's/^master:.*/master: localhost/' /etc/salt/minion.d/minion.conf |
Petr Michalec | 5951ff4 | 2017-09-07 18:29:53 +0200 | [diff] [blame] | 669 | $SUDO service salt-master restart >/dev/null |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 670 | $SUDO service salt-minion restart >/dev/null |
| 671 | $SUDO salt-call ${SALT_OPTS} saltutil.sync_all >/dev/null |
| 672 | |
| 673 | verify_salt_master |
| 674 | set +e |
| 675 | |
| 676 | } |
| 677 | |
| 678 | |
| 679 | function verify_salt_master() { |
| 680 | set -e |
| 681 | |
| 682 | log_info "Verify Salt master" |
| 683 | test -n "$MASTER_HOSTNAME" || exit 1 |
| 684 | |
| 685 | if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then |
Petr Michalec | a514134 | 2017-08-16 12:55:20 +0200 | [diff] [blame] | 686 | $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} reclass.validate_yaml > /tmp/${MASTER_HOSTNAME}.reclass.validate_yaml |
| 687 | $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] | 688 | $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} grains.item roles > /tmp/${MASTER_HOSTNAME}.grains.item.roles |
| 689 | $SUDO salt-call ${SALT_OPTS} --id=${MASTER_HOSTNAME} state.show_lowstate > /tmp/${MASTER_HOSTNAME}.state.show_state |
| 690 | $SUDO salt-call --no-color grains.items |
| 691 | $SUDO salt-call --no-color pillar.data |
| 692 | fi |
Petr Michalec | a514134 | 2017-08-16 12:55:20 +0200 | [diff] [blame] | 693 | # TODO: REMOVE reclass --nodeinfo section / run only on debug - as the only required is reclass.validate_* |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 694 | if ! $SUDO reclass --nodeinfo ${MASTER_HOSTNAME} > /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo; then |
| 695 | log_err "For more details see full log /tmp/${MASTER_HOSTNAME}.reclass.nodeinfo" |
| 696 | exit 1 |
| 697 | fi |
| 698 | } |
| 699 | |
| 700 | function verify_salt_minion() { |
| 701 | set -e |
| 702 | node=$1 |
| 703 | log_info "Verifying ${node}" |
| 704 | if [[ $VERIFY_SALT_CALL =~ ^(True|true|1|yes)$ ]]; then |
| 705 | $SUDO salt-call ${SALT_OPTS} --id=${node} grains.item roles > /tmp/${node}.grains.item.roles |
| 706 | $SUDO salt-call ${SALT_OPTS} --id=${node} state.show_lowstate > /tmp/${node}.state.show_lowstate |
| 707 | fi |
| 708 | if ! $SUDO reclass --nodeinfo ${node} > /tmp/${node}.reclass.nodeinfo; then |
| 709 | log_err "For more details see full log /tmp/${node}.reclass.nodeinfo" |
| 710 | if [[ ${BREAK_ON_VERIFICATION_ERROR:-yes} =~ ^(True|true|1|yes)$ ]]; then |
| 711 | exit 1 |
| 712 | fi |
| 713 | fi |
| 714 | } |
| 715 | |
| 716 | function verify_salt_minions() { |
| 717 | #set -e |
| 718 | NODES=$(find $RECLASS_ROOT/nodes/ -name "*.yml" | grep -v "cfg") |
| 719 | log_info "Verifying minions: $(echo ${NODES}|xargs)" |
| 720 | |
| 721 | # Parallel |
| 722 | #echo $NODES | parallel --no-notice -j 2 --halt 2 "verify_salt_minion \$(basename {} .yml) > {}.pillar_verify" |
| 723 | #ls -lrta *.pillar_verify | tail -n 1 | xargs -n1 tail -n30 |
| 724 | |
| 725 | function filterFails() { |
| 726 | grep -v '/grains' | tee -a $1 | tail -n20 |
| 727 | } |
| 728 | |
| 729 | log_info "Verify nodes" |
| 730 | passed=0 |
| 731 | for node in ${NODES}; do |
| 732 | node=$(basename $node .yml) |
| 733 | |
| 734 | # filter first in cluster.. ctl-01, mon-01, etc.. |
| 735 | if [[ "${node//.*}" =~ 01 || "${node//.*}" =~ 02 ]] ;then |
| 736 | verify_salt_minion ${node} || continue |
| 737 | else |
| 738 | echo Skipped $node. |
| 739 | fi |
| 740 | passed=$(($passed+1)) |
| 741 | done |
| 742 | # fail on failures |
| 743 | total=$(echo $NODES | xargs --no-run-if-empty -n1 echo |wc -l) |
| 744 | test ! $passed -lt $total || log_err "Results: $passed of $total passed." |
| 745 | test ! $passed -lt $total || { |
| 746 | tail -n50 /tmp/*.pillar_verify |
| 747 | return 1 |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | |
| 752 | |
| 753 | ########################################## |
| 754 | # To install salt master/minon |
| 755 | |
| 756 | function install() { |
| 757 | setup $@ |
| 758 | } |
| 759 | |
| 760 | function setup() { |
| 761 | # CLI |
| 762 | while [ x"$1" != x"" ]; do |
| 763 | which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null |
| 764 | |
| 765 | case $1 in |
| 766 | master ) |
| 767 | install_salt_master_$SALT_SOURCE |
| 768 | install_salt_minion_$SALT_SOURCE |
| 769 | install_salt_formula_$FORMULAS_SOURCE |
| 770 | ;; |
| 771 | minion ) |
| 772 | install_salt_minion_$SALT_SOURCE |
| 773 | ;; |
| 774 | esac |
| 775 | shift |
| 776 | done |
| 777 | echo DONE |
| 778 | } |
| 779 | |
| 780 | function bootstrap() { |
| 781 | log_info "Bootstrap & verification of SaltMaster and configured minions." |
| 782 | trap _atexit INT TERM EXIT |
| 783 | |
| 784 | system_config_master |
| 785 | saltmaster_bootstrap &&\ |
Petr Michalec | 58575b9 | 2017-08-20 10:42:25 +0200 | [diff] [blame] | 786 | saltmaster_init #&&\ |
Petr Michalec | a514134 | 2017-08-16 12:55:20 +0200 | [diff] [blame] | 787 | #verify_salt_minions |
Petr Michalec | 1ed1b3c | 2017-08-08 19:19:01 +0200 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | function default() { |
| 791 | bootstrap $@ |
| 792 | } |
| 793 | |
| 794 | |
| 795 | ########################################## |
| 796 | [[ "$0" != "$BASH_SOURCE" ]] || { |
| 797 | # unless file is being sourced |
| 798 | default $@ |
| 799 | } |