Ales Komarek | 1b37311 | 2017-08-08 08:48:56 +0200 | [diff] [blame] | 1 | #!/bin/bash -e |
| 2 | |
| 3 | # Installs Salt and configure minimal SaltMaster or Minion to be used with: |
| 4 | # - http://github.com/salt-formulas |
| 5 | # - http://github.com/salt-formulas/salt-formula-salt (salt.master sls) |
| 6 | |
| 7 | # TODO: |
| 8 | # - use PPA repository as formula source |
| 9 | # - support for spm/yum |
| 10 | |
| 11 | # DEAULTS |
| 12 | # salt apt repository |
| 13 | test -e /etc/lsb-release && eval $(cat /etc/lsb-release) |
| 14 | which lsb_release && DISTRIB_CODENAME=${DISTRIB_CODENAME:-$(lsb_release -cs)} |
| 15 | # |
| 16 | export APT_REPOSITORY="deb [arch=amd64] http://apt-mk.mirantis.com/${DISTRIB_CODENAME} ${DISTRIB_REVISION:-stable} salt" |
| 17 | export APT_REPOSITORY_GPG=${APT_REPOSITORY_GPG:-http://apt-mk.mirantis.com/public.gpg} |
| 18 | |
| 19 | # reclass |
| 20 | export RECLASS_ADDRESS=${RECLASS_ADDRESS:-https://github.com/salt-formulas/openstack-salt.git} # https/git |
| 21 | |
| 22 | # formula |
| 23 | export FORMULAS_BASE=${FORMULAS_BASE:-https://github.com/salt-formulas} |
| 24 | export FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas} |
| 25 | export FORMULAS_BRANCH=${FORMULAS_BRANCH:-master} |
| 26 | export FORMULAS_SOURCE=${FORMULAS_SOURCE:-pkg} # pkg/git |
| 27 | # essential set of formulas (known to by used on cfg01 node for most setups) |
| 28 | 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} |
| 29 | # minimal set of formulas for salt-master bootstrap |
| 30 | declare -a FORMULAS_SALT_MASTER=(linux reclass salt git $(echo $FORMULAS_SALT_MASTER)) |
| 31 | export FORMULAS_SALT_MASTER |
| 32 | |
| 33 | # system / host |
| 34 | export HOSTNAME=${HOSTNAME:-cfg01} |
| 35 | export DOMAIN=${DOMAIN:-bootstrap.local} |
| 36 | |
| 37 | # salt |
| 38 | export SALT_MASTER=${SALT_MASTER:-127.0.0.1} # ip or fqdn |
| 39 | export MINION_ID=${MINION_ID:-${HOSTNAME}.${DOMAIN}} |
| 40 | |
| 41 | # saltstack |
| 42 | BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True} |
| 43 | BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX stable 2016.3 } |
| 44 | |
| 45 | # ENVIRONMENT |
| 46 | ############# |
| 47 | SALT_SOURCE=${SALT_SOURCE:-pkg} |
| 48 | SALT_VERSION=${SALT_VERSION:-latest} |
| 49 | |
| 50 | |
| 51 | if [ "$FORMULAS_SOURCE" == "git" ]; then |
| 52 | SALT_ENV=${SALT_ENV:-dev} |
| 53 | elif [ "$FORMULAS_SOURCE" == "pkg" ]; then |
| 54 | SALT_ENV=${SALT_ENV:-prd} |
| 55 | fi |
| 56 | |
| 57 | eval $(cat /etc/*release 2> /dev/null) |
| 58 | PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z) |
| 59 | |
| 60 | case $PLATFORM_FAMILY in |
| 61 | debian ) |
| 62 | PKGTOOL="$SUDO apt-get" |
| 63 | test ${VERSION_ID//\.*/} -ge 16 && { |
| 64 | SVCTOOL=service |
| 65 | } || { SVCTOOL=service |
| 66 | } |
| 67 | ;; |
| 68 | rhel ) |
| 69 | PKGTOOL="$SUDO yum" |
| 70 | test ${VERSION_ID//\.*/} -ge 7 && { |
| 71 | SVCTOOL=systemctl |
| 72 | } || { SVCTOOL=service |
| 73 | } |
| 74 | ;; |
| 75 | esac |
| 76 | |
| 77 | export PLATFORM_FAMILY |
| 78 | export PKGTOOL |
| 79 | export SVCTOOL |
| 80 | |
| 81 | # FUNCTIONS |
| 82 | ########### |
| 83 | configure_pkg_repo() |
| 84 | { |
| 85 | |
| 86 | case $PLATFORM_FAMILY in |
| 87 | debian) |
| 88 | if [ -n "$APT_REPOSITORY_PPA" ]; then |
| 89 | which add-apt-repository || $SUDO apt-get install -y software-properties-common |
| 90 | $SUDO add-apt-repository -y ppa:${APT_REPOSITORY_PPA} |
| 91 | else |
| 92 | echo -e "$APT_REPOSITORY " | $SUDO tee /etc/apt/sources.list.d/bootstrap.list >/dev/null |
| 93 | curl -sL $APT_REPOSITORY_GPG | $SUDO apt-key add - |
| 94 | fi |
| 95 | $SUDO apt-get clean |
| 96 | $SUDO apt-get update |
| 97 | ;; |
| 98 | rhel) |
| 99 | $SUDO yum install -y https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el${VERSION_ID}.noarch.rpm |
| 100 | $SUDO yum clean all |
| 101 | ;; |
| 102 | esac |
| 103 | |
| 104 | } |
| 105 | |
| 106 | configure_salt_master() |
| 107 | { |
| 108 | |
| 109 | echo "Configuring salt-master ..." |
| 110 | |
| 111 | [ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d |
| 112 | cat <<-EOF > /etc/salt/master.d/master.conf |
| 113 | file_roots: |
| 114 | base: |
| 115 | - /usr/share/salt-formulas/env |
| 116 | prd: |
| 117 | - /srv/salt/env/prd |
| 118 | dev: |
| 119 | - /srv/salt/env/dev |
| 120 | pillar_opts: False |
| 121 | open_mode: True |
| 122 | reclass: &reclass |
| 123 | storage_type: yaml_fs |
| 124 | inventory_base_uri: /srv/salt/reclass |
| 125 | ext_pillar: |
| 126 | - reclass: *reclass |
| 127 | master_tops: |
| 128 | reclass: *reclass |
| 129 | EOF |
| 130 | |
| 131 | echo "Configuring reclass ..." |
| 132 | |
| 133 | [ ! -d /etc/reclass ] && mkdir /etc/reclass |
| 134 | cat <<-EOF > /etc/reclass/reclass-config.yml |
| 135 | storage_type: yaml_fs |
| 136 | pretty_print: True |
| 137 | output: yaml |
| 138 | inventory_base_uri: /srv/salt/reclass |
| 139 | EOF |
| 140 | |
| 141 | if [ ! -d /srv/salt/reclass ]; then |
| 142 | # No reclass at all, clone from given address |
| 143 | ssh-keyscan -H github.com >> ~/.ssh/known_hosts || true |
| 144 | if echo ${RECLASS_BRANCH:-master} | egrep -q "^refs"; then |
| 145 | git clone ${RECLASS_ADDRESS} /srv/salt/reclass |
| 146 | cd /srv/salt/reclass |
| 147 | git fetch ${RECLASS_ADDRESS} ${RECLASS_BRANCH:-master} && git checkout FETCH_HEAD |
| 148 | cd - |
| 149 | else |
| 150 | git clone -b ${RECLASS_BRANCH:-master} ${RECLASS_ADDRESS} /srv/salt/reclass |
| 151 | fi |
| 152 | fi; |
| 153 | |
| 154 | # Source bootstrap_vars for specific cluster if specified. |
| 155 | for cluster in /srv/salt/reclass/classes/cluster/*/; do |
| 156 | if [[ -f "$cluster/bootstrap_vars" ]]; then |
| 157 | echo "Sourcing bootstrap_vars for cluster $cluster" |
| 158 | source $cluster/bootstrap_vars |
| 159 | fi |
| 160 | done |
| 161 | |
| 162 | cd /srv/salt/reclass |
| 163 | if [ ! -d /srv/salt/reclass/classes/system/linux ]; then |
| 164 | # Possibly subrepo checkout needed |
| 165 | git submodule update --init --recursive |
| 166 | fi |
| 167 | |
| 168 | #sed -ie "s#\(reclass_data_revision.\).*#\1 $RECLASS_BRANCH#" $(find nodes -name ${MASTER_HOSTNAME}.yml|tail -n1) |
| 169 | |
| 170 | mkdir -vp /srv/salt/reclass/nodes |
| 171 | CONFIG=$(find /srv/salt/reclass/nodes -name ${MINION_ID}.yml| grep yml | tail -n1) |
| 172 | CONFIG=${CONFIG:-/srv/salt/reclass/nodes/${MINION_ID}.yml} |
| 173 | if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then |
| 174 | cat <<-EOF > ${CONFIG} |
| 175 | classes: |
| 176 | - service.git.client |
| 177 | - system.linux.system.single |
| 178 | - system.salt.master.single |
| 179 | - system.salt.master.$FORMULAS_SOURCE |
| 180 | - system.reclass.storage.salt |
| 181 | parameters: |
| 182 | _param: |
| 183 | reclass_data_repository: "$RECLASS_ADDRESS" |
| 184 | reclass_data_revision: ${RECLASS_BRANCH:-master} |
| 185 | salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master} |
| 186 | reclass_config_master: $SALT_MASTER |
| 187 | single_address: $SALT_MASTER |
| 188 | salt_master_host: $SALT_MASTER |
| 189 | salt_master_base_environment: $SALT_ENV |
Adam Tengler | 1a9f1a7 | 2017-08-08 12:20:01 +0000 | [diff] [blame^] | 190 | linux_system_codename: $DISTRIB_CODENAME |
Ales Komarek | 1b37311 | 2017-08-08 08:48:56 +0200 | [diff] [blame] | 191 | linux: |
| 192 | system: |
| 193 | name: $MINION_ID |
| 194 | domain: $DOMAIN |
| 195 | # ######## |
| 196 | EOF |
| 197 | |
| 198 | if [ "$SALT_VERSION" == "latest" ]; then |
| 199 | VERSION="" |
| 200 | else |
| 201 | VERSION="version: $SALT_VERSION" |
| 202 | fi |
| 203 | |
| 204 | cat <<-EOF >> ${CONFIG} |
| 205 | salt: |
| 206 | master: |
| 207 | accept_policy: open_mode |
| 208 | source: |
| 209 | engine: $SALT_SOURCE |
| 210 | $VERSION |
| 211 | minion: |
| 212 | source: |
| 213 | engine: $SALT_SOURCE |
| 214 | $VERSION |
| 215 | # ######## |
| 216 | # vim: ft=yaml sw=2 ts=2 sts=2 |
| 217 | EOF |
| 218 | fi |
| 219 | } |
| 220 | |
| 221 | configure_salt_minion() |
| 222 | { |
| 223 | [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d |
| 224 | cat <<-EOF > /etc/salt/minion.d/minion.conf |
| 225 | master: $SALT_MASTER |
| 226 | id: $MINION_ID |
| 227 | EOF |
| 228 | } |
| 229 | |
| 230 | |
| 231 | install_salt_master_pkg() |
| 232 | { |
| 233 | echo -e "\nPreparing base OS repository ...\n" |
| 234 | |
| 235 | configure_pkg_repo |
| 236 | |
| 237 | echo -e "\nInstalling salt master ...\n" |
| 238 | |
| 239 | case $PLATFORM_FAMILY in |
| 240 | debian) |
| 241 | $SUDO apt-get install -y git |
| 242 | which reclass || $SUDO apt install -qqq -y reclass |
| 243 | curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true |
| 244 | ;; |
| 245 | rhel) |
| 246 | yum install -y git |
| 247 | which reclass || $SUDO yum install -y reclass |
| 248 | curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true |
| 249 | ;; |
| 250 | esac |
| 251 | |
| 252 | which reclass-salt || { |
| 253 | test -e /usr/share/reclass/reclass-salt && { |
| 254 | ln -fs /usr/share/reclass/reclass-salt /usr/bin |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | configure_salt_master |
| 259 | |
| 260 | echo -e "\nRestarting services ...\n" |
| 261 | [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub |
| 262 | $SVCTOOL salt-master restart |
| 263 | } |
| 264 | |
| 265 | install_salt_master_pip() |
| 266 | { |
| 267 | echo -e "\nPreparing base OS repository ...\n" |
| 268 | |
| 269 | case $PLATFORM_FAMILY in |
| 270 | debian) |
| 271 | $SUDO apt-get install -y python-pip python-dev zlib1g-dev git |
| 272 | which reclass || $SUDO apt-get install -y reclass |
| 273 | ;; |
| 274 | rhel) |
| 275 | $SUDO yum install -y git |
| 276 | which reclass || $SUDO yum install -y reclass |
| 277 | ;; |
| 278 | esac |
| 279 | |
| 280 | echo -e "\nInstalling salt master ...\n" |
| 281 | # TODO: replace with saltstack bootstrap script |
| 282 | |
| 283 | if [ "$SALT_VERSION" == "latest" ]; then |
| 284 | pip install salt |
| 285 | else |
| 286 | pip install salt==$SALT_VERSION |
| 287 | fi |
| 288 | |
| 289 | 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 |
| 290 | ln -s /usr/local/bin/salt-master /usr/bin/salt-master |
| 291 | |
| 292 | which reclass-salt || { |
| 293 | test -e /usr/share/reclass/reclass-salt && { |
| 294 | ln -fs /usr/share/reclass/reclass-salt /usr/bin |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | configure_salt_master |
| 299 | |
| 300 | echo -e "\nRestarting services ...\n" |
| 301 | [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub |
| 302 | $SVCTOOL salt-master restart |
| 303 | } |
| 304 | |
| 305 | |
| 306 | |
| 307 | install_salt_minion_pkg() |
| 308 | { |
| 309 | |
| 310 | configure_pkg_repo |
| 311 | |
| 312 | echo -e "\nInstalling salt minion ...\n" |
| 313 | |
| 314 | case $PLATFORM_FAMILY in |
| 315 | debian) |
| 316 | curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true |
| 317 | ;; |
| 318 | rhel) |
| 319 | curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true |
| 320 | ;; |
| 321 | esac |
| 322 | |
| 323 | |
| 324 | configure_salt_minion |
| 325 | |
| 326 | $SVCTOOL salt-minion restart |
| 327 | } |
| 328 | |
| 329 | install_salt_minion_pip() |
| 330 | { |
| 331 | echo -e "\nInstalling salt minion ...\n" |
| 332 | |
| 333 | 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 |
| 334 | ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion |
| 335 | |
| 336 | configure_salt_minion |
| 337 | $SVCTOOL salt-minion restart |
| 338 | } |
| 339 | |
| 340 | |
| 341 | install_salt_formula_pkg() |
| 342 | { |
| 343 | configure_pkg_repo |
| 344 | |
| 345 | case $PLATFORM_FAMILY in |
| 346 | debian) |
| 347 | echo "Configuring necessary formulas ..." |
| 348 | |
| 349 | [ ! -d /srv/salt/reclass/classes/service ] && mkdir -p /srv/salt/reclass/classes/service |
| 350 | # Set essentials if FORMULAS_SALT_MASTER is not defined at all |
| 351 | [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached") |
| 352 | for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do |
| 353 | echo -e "\nConfiguring salt formula ${formula_service} ...\n" |
| 354 | [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \ |
| 355 | if ! $SUDO apt-get install -y salt-formula-${formula_service}; then |
| 356 | echo -e "\nInstall salt-formula-${formula_service} failed.\n" |
| 357 | exit 1 |
| 358 | fi |
| 359 | [ ! -L "/srv/salt/reclass/classes/service/${formula_service}" ] && \ |
| 360 | ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} /srv/salt/reclass/classes/service/${formula_service} |
| 361 | done |
| 362 | ;; |
| 363 | rhel) |
| 364 | # TODO |
| 365 | ;; |
| 366 | esac |
| 367 | |
| 368 | [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo "" |
| 369 | [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo "" |
| 370 | } |
| 371 | |
| 372 | install_salt_formula_git() |
| 373 | { |
| 374 | echo "Configuring necessary formulas ..." |
| 375 | |
| 376 | [ ! -d /srv/salt/reclass/classes/service ] && mkdir -p /srv/salt/reclass/classes/service |
| 377 | # Set essentials if FORMULAS_SALT_MASTER is not defined at all |
| 378 | [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached") |
| 379 | for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do |
| 380 | echo -e "\nConfiguring salt formula ${formula_service} ...\n" |
| 381 | _BRANCH=${FORMULAS_BRANCH} |
| 382 | [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && { |
| 383 | if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then |
| 384 | # Fallback to the master branch if the branch doesn't exist for this repository |
| 385 | _BRANCH=master |
| 386 | fi |
| 387 | if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then |
| 388 | echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n" |
| 389 | exit 1 |
| 390 | fi |
| 391 | } || { |
| 392 | cd ${FORMULAS_PATH}/env/_formulas/${formula_service}; |
| 393 | git fetch origin/${_BRANCH} || git fetch --all |
| 394 | git checkout ${_BRANCH} && git pull || git pull; |
| 395 | cd - |
| 396 | } |
| 397 | [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \ |
| 398 | ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service} |
| 399 | [ ! -L "/srv/salt/reclass/classes/service/${formula_service}" ] && \ |
| 400 | ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service /srv/salt/reclass/classes/service/${formula_service} |
| 401 | done |
| 402 | |
| 403 | [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo "" |
| 404 | [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo "" |
| 405 | } |
| 406 | |
| 407 | # MAIN |
| 408 | #### |
| 409 | [[ "$0" != "$BASH_SOURCE" ]] || { |
| 410 | # unless file is being sourced |
| 411 | |
| 412 | # DEBUGING |
| 413 | #set -x |
| 414 | #test -e $(dirname $0)/env/salt.env && source $(dirname $0)/env/salt.env |
| 415 | #set |
| 416 | |
| 417 | # CLI |
| 418 | while [ x"$1" != x"" ]; do |
| 419 | which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null |
| 420 | |
| 421 | case $1 in |
| 422 | master ) |
| 423 | install_salt_master_$SALT_SOURCE |
| 424 | install_salt_minion_$SALT_SOURCE |
| 425 | install_salt_formula_$FORMULAS_SOURCE |
| 426 | ;; |
| 427 | minion ) |
| 428 | install_salt_minion_$SALT_SOURCE |
| 429 | ;; |
| 430 | esac |
| 431 | shift |
| 432 | done |
| 433 | echo DONE |
| 434 | } |