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