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