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