blob: c37254768c5c727f6bf4695251cbf31362d5bb8d [file] [log] [blame]
Ales Komarek1b373112017-08-08 08:48:56 +02001#!/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 Michalec554c4a32017-08-08 10:00:03 +020011# Source specific env vars.
12for 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
14done;
15
Ales Komarek1b373112017-08-08 08:48:56 +020016# DEAULTS
17# salt apt repository
18test -e /etc/lsb-release && eval $(cat /etc/lsb-release)
19which lsb_release && DISTRIB_CODENAME=${DISTRIB_CODENAME:-$(lsb_release -cs)}
20#
21export APT_REPOSITORY="deb [arch=amd64] http://apt-mk.mirantis.com/${DISTRIB_CODENAME} ${DISTRIB_REVISION:-stable} salt"
22export APT_REPOSITORY_GPG=${APT_REPOSITORY_GPG:-http://apt-mk.mirantis.com/public.gpg}
23
24# reclass
25export RECLASS_ADDRESS=${RECLASS_ADDRESS:-https://github.com/salt-formulas/openstack-salt.git} # https/git
26
27# formula
28export FORMULAS_BASE=${FORMULAS_BASE:-https://github.com/salt-formulas}
29export FORMULAS_PATH=${FORMULAS_PATH:-/usr/share/salt-formulas}
30export FORMULAS_BRANCH=${FORMULAS_BRANCH:-master}
31export FORMULAS_SOURCE=${FORMULAS_SOURCE:-pkg} # pkg/git
32# essential set of formulas (known to by used on cfg01 node for most setups)
33FORMULAS_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}
34# minimal set of formulas for salt-master bootstrap
35declare -a FORMULAS_SALT_MASTER=(linux reclass salt git $(echo $FORMULAS_SALT_MASTER))
36export FORMULAS_SALT_MASTER
37
38# system / host
39export HOSTNAME=${HOSTNAME:-cfg01}
40export DOMAIN=${DOMAIN:-bootstrap.local}
41
42# salt
43export SALT_MASTER=${SALT_MASTER:-127.0.0.1} # ip or fqdn
44export MINION_ID=${MINION_ID:-${HOSTNAME}.${DOMAIN}}
45
46# saltstack
47BOOTSTRAP_SALTSTACK=${BOOTSTRAP_SALTSTACK:-True}
48BOOTSTRAP_SALTSTACK_OPTS=${BOOTSTRAP_SALTSTACK_OPTS:- -dX stable 2016.3 }
49
50# ENVIRONMENT
51#############
52SALT_SOURCE=${SALT_SOURCE:-pkg}
53SALT_VERSION=${SALT_VERSION:-latest}
54
55
56if [ "$FORMULAS_SOURCE" == "git" ]; then
57 SALT_ENV=${SALT_ENV:-dev}
58elif [ "$FORMULAS_SOURCE" == "pkg" ]; then
59 SALT_ENV=${SALT_ENV:-prd}
60fi
61
62eval $(cat /etc/*release 2> /dev/null)
63PLATFORM_FAMILY=$(echo ${ID_LIKE// */} | tr A-Z a-z)
64
65case $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 ;;
80esac
81
82export PLATFORM_FAMILY
83export PKGTOOL
84export SVCTOOL
85
86# FUNCTIONS
87###########
88configure_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
111configure_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
134EOF
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
144EOF
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 Michaleced921b12017-08-08 14:09:00 +0200149 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 Komarek1b373112017-08-08 08:48:56 +0200157 fi;
158
Ales Komarek1b373112017-08-08 08:48:56 +0200159 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
Petr Michaleccec538e2017-08-14 13:21:30 +0200164
165 # Source specific env vars. - temp. change to workaround bootstrap.
166 for path in /srv/salt/reclass/classes/cluster /srv/salt/reclass/classes/cluster/${CLUSTER_NAME}; do
167 export $(find $path -maxdepth 1 -name '*.env' 2> /dev/null | xargs --no-run-if-empty cat ) > /dev/null
168 done;
Ales Komarek1b373112017-08-08 08:48:56 +0200169
170 #sed -ie "s#\(reclass_data_revision.\).*#\1 $RECLASS_BRANCH#" $(find nodes -name ${MASTER_HOSTNAME}.yml|tail -n1)
171
172 mkdir -vp /srv/salt/reclass/nodes
173 CONFIG=$(find /srv/salt/reclass/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
174 CONFIG=${CONFIG:-/srv/salt/reclass/nodes/${MINION_ID}.yml}
175 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
176 cat <<-EOF > ${CONFIG}
177 classes:
178 - service.git.client
179 - system.linux.system.single
180 - system.salt.master.single
181 - system.salt.master.$FORMULAS_SOURCE
182 - system.reclass.storage.salt
183 parameters:
184 _param:
185 reclass_data_repository: "$RECLASS_ADDRESS"
186 reclass_data_revision: ${RECLASS_BRANCH:-master}
187 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
188 reclass_config_master: $SALT_MASTER
189 single_address: $SALT_MASTER
190 salt_master_host: $SALT_MASTER
191 salt_master_base_environment: $SALT_ENV
Adam Tengler1a9f1a72017-08-08 12:20:01 +0000192 linux_system_codename: $DISTRIB_CODENAME
Ales Komarek1b373112017-08-08 08:48:56 +0200193 linux:
194 system:
195 name: $MINION_ID
196 domain: $DOMAIN
197 # ########
198EOF
199
200 if [ "$SALT_VERSION" == "latest" ]; then
201 VERSION=""
202 else
203 VERSION="version: $SALT_VERSION"
204 fi
205
206 cat <<-EOF >> ${CONFIG}
207 salt:
208 master:
209 accept_policy: open_mode
210 source:
211 engine: $SALT_SOURCE
212 $VERSION
213 minion:
214 source:
215 engine: $SALT_SOURCE
216 $VERSION
217 # ########
218 # vim: ft=yaml sw=2 ts=2 sts=2
219EOF
220 fi
221}
222
223configure_salt_minion()
224{
225 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
226 cat <<-EOF > /etc/salt/minion.d/minion.conf
227 master: $SALT_MASTER
228 id: $MINION_ID
229 EOF
230}
231
232
233install_salt_master_pkg()
234{
235 echo -e "\nPreparing base OS repository ...\n"
236
237 configure_pkg_repo
238
239 echo -e "\nInstalling salt master ...\n"
240
241 case $PLATFORM_FAMILY in
242 debian)
243 $SUDO apt-get install -y git
244 which reclass || $SUDO apt install -qqq -y reclass
245 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
246 ;;
247 rhel)
248 yum install -y git
249 which reclass || $SUDO yum install -y reclass
250 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
251 ;;
252 esac
253
254 which reclass-salt || {
255 test -e /usr/share/reclass/reclass-salt && {
256 ln -fs /usr/share/reclass/reclass-salt /usr/bin
257 }
258 }
259
260 configure_salt_master
261
262 echo -e "\nRestarting services ...\n"
263 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
264 $SVCTOOL salt-master restart
265}
266
267install_salt_master_pip()
268{
269 echo -e "\nPreparing base OS repository ...\n"
270
271 case $PLATFORM_FAMILY in
272 debian)
273 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
274 which reclass || $SUDO apt-get install -y reclass
275 ;;
276 rhel)
277 $SUDO yum install -y git
278 which reclass || $SUDO yum install -y reclass
279 ;;
280 esac
281
282 echo -e "\nInstalling salt master ...\n"
283 # TODO: replace with saltstack bootstrap script
284
285 if [ "$SALT_VERSION" == "latest" ]; then
286 pip install salt
287 else
288 pip install salt==$SALT_VERSION
289 fi
290
291 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
292 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
293
294 which reclass-salt || {
295 test -e /usr/share/reclass/reclass-salt && {
296 ln -fs /usr/share/reclass/reclass-salt /usr/bin
297 }
298 }
299
300 configure_salt_master
301
302 echo -e "\nRestarting services ...\n"
303 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
304 $SVCTOOL salt-master restart
305}
306
307
308
309install_salt_minion_pkg()
310{
311
312 configure_pkg_repo
313
314 echo -e "\nInstalling salt minion ...\n"
315
316 case $PLATFORM_FAMILY in
317 debian)
318 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
319 ;;
320 rhel)
321 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
322 ;;
323 esac
324
325
326 configure_salt_minion
327
328 $SVCTOOL salt-minion restart
329}
330
331install_salt_minion_pip()
332{
333 echo -e "\nInstalling salt minion ...\n"
334
335 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
336 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
337
338 configure_salt_minion
339 $SVCTOOL salt-minion restart
340}
341
342
343install_salt_formula_pkg()
344{
345 configure_pkg_repo
346
347 case $PLATFORM_FAMILY in
348 debian)
349 echo "Configuring necessary formulas ..."
350
351 [ ! -d /srv/salt/reclass/classes/service ] && mkdir -p /srv/salt/reclass/classes/service
352 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
353 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
354 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
355 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
356 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
357 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
358 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
359 exit 1
360 fi
361 [ ! -L "/srv/salt/reclass/classes/service/${formula_service}" ] && \
362 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} /srv/salt/reclass/classes/service/${formula_service}
363 done
364 ;;
365 rhel)
366 # TODO
367 ;;
368 esac
369
370 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
371 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
372}
373
374install_salt_formula_git()
375{
376 echo "Configuring necessary formulas ..."
377
378 [ ! -d /srv/salt/reclass/classes/service ] && mkdir -p /srv/salt/reclass/classes/service
379 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
380 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
381 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
382 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
383 _BRANCH=${FORMULAS_BRANCH}
384 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
385 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
386 # Fallback to the master branch if the branch doesn't exist for this repository
387 _BRANCH=master
388 fi
389 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
390 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
391 exit 1
392 fi
393 } || {
394 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
395 git fetch origin/${_BRANCH} || git fetch --all
396 git checkout ${_BRANCH} && git pull || git pull;
397 cd -
398 }
399 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
400 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
401 [ ! -L "/srv/salt/reclass/classes/service/${formula_service}" ] && \
402 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service /srv/salt/reclass/classes/service/${formula_service}
403 done
404
405 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
406 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
407}
408
409# MAIN
410####
411[[ "$0" != "$BASH_SOURCE" ]] || {
412# unless file is being sourced
413
414 # DEBUGING
415 #set -x
416 #test -e $(dirname $0)/env/salt.env && source $(dirname $0)/env/salt.env
417 #set
418
419 # CLI
420 while [ x"$1" != x"" ]; do
421 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
422
423 case $1 in
424 master )
425 install_salt_master_$SALT_SOURCE
426 install_salt_minion_$SALT_SOURCE
427 install_salt_formula_$FORMULAS_SOURCE
428 ;;
429 minion )
430 install_salt_minion_$SALT_SOURCE
431 ;;
432 esac
433 shift
434 done
435 echo DONE
436}