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