blob: 8f33a72923548f6efa2fa0aae3851f957506cab5 [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
Petr Michalec554c4a32017-08-08 10:00:03 +0200148 git clone ${RECLASS_ADDRESS} /srv/salt/reclass -b ${RECLASS_BRANCH:-master}
Ales Komarek1b373112017-08-08 08:48:56 +0200149 fi;
150
Ales Komarek1b373112017-08-08 08:48:56 +0200151 cd /srv/salt/reclass
152 if [ ! -d /srv/salt/reclass/classes/system/linux ]; then
153 # Possibly subrepo checkout needed
154 git submodule update --init --recursive
155 fi
156
157 #sed -ie "s#\(reclass_data_revision.\).*#\1 $RECLASS_BRANCH#" $(find nodes -name ${MASTER_HOSTNAME}.yml|tail -n1)
158
159 mkdir -vp /srv/salt/reclass/nodes
160 CONFIG=$(find /srv/salt/reclass/nodes -name ${MINION_ID}.yml| grep yml | tail -n1)
161 CONFIG=${CONFIG:-/srv/salt/reclass/nodes/${MINION_ID}.yml}
162 if [[ $SALT_MASTER_BOOTSTRAP_MINIMIZED =~ ^(True|true|1|yes)$ || ! -f "${CONFIG}" ]]; then
163 cat <<-EOF > ${CONFIG}
164 classes:
165 - service.git.client
166 - system.linux.system.single
167 - system.salt.master.single
168 - system.salt.master.$FORMULAS_SOURCE
169 - system.reclass.storage.salt
170 parameters:
171 _param:
172 reclass_data_repository: "$RECLASS_ADDRESS"
173 reclass_data_revision: ${RECLASS_BRANCH:-master}
174 salt_formula_branch: ${SALT_FORMULAS_BRANCH:-master}
175 reclass_config_master: $SALT_MASTER
176 single_address: $SALT_MASTER
177 salt_master_host: $SALT_MASTER
178 salt_master_base_environment: $SALT_ENV
179 linux_system_codename: $DISTRIB_CODENAME
180 linux:
181 system:
182 name: $MINION_ID
183 domain: $DOMAIN
184 # ########
185EOF
186
187 if [ "$SALT_VERSION" == "latest" ]; then
188 VERSION=""
189 else
190 VERSION="version: $SALT_VERSION"
191 fi
192
193 cat <<-EOF >> ${CONFIG}
194 salt:
195 master:
196 accept_policy: open_mode
197 source:
198 engine: $SALT_SOURCE
199 $VERSION
200 minion:
201 source:
202 engine: $SALT_SOURCE
203 $VERSION
204 # ########
205 # vim: ft=yaml sw=2 ts=2 sts=2
206EOF
207 fi
208}
209
210configure_salt_minion()
211{
212 [ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d
213 cat <<-EOF > /etc/salt/minion.d/minion.conf
214 master: $SALT_MASTER
215 id: $MINION_ID
216 EOF
217}
218
219
220install_salt_master_pkg()
221{
222 echo -e "\nPreparing base OS repository ...\n"
223
224 configure_pkg_repo
225
226 echo -e "\nInstalling salt master ...\n"
227
228 case $PLATFORM_FAMILY in
229 debian)
230 $SUDO apt-get install -y git
231 which reclass || $SUDO apt install -qqq -y reclass
232 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
233 ;;
234 rhel)
235 yum install -y git
236 which reclass || $SUDO yum install -y reclass
237 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- -M ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
238 ;;
239 esac
240
241 which reclass-salt || {
242 test -e /usr/share/reclass/reclass-salt && {
243 ln -fs /usr/share/reclass/reclass-salt /usr/bin
244 }
245 }
246
247 configure_salt_master
248
249 echo -e "\nRestarting services ...\n"
250 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
251 $SVCTOOL salt-master restart
252}
253
254install_salt_master_pip()
255{
256 echo -e "\nPreparing base OS repository ...\n"
257
258 case $PLATFORM_FAMILY in
259 debian)
260 $SUDO apt-get install -y python-pip python-dev zlib1g-dev git
261 which reclass || $SUDO apt-get install -y reclass
262 ;;
263 rhel)
264 $SUDO yum install -y git
265 which reclass || $SUDO yum install -y reclass
266 ;;
267 esac
268
269 echo -e "\nInstalling salt master ...\n"
270 # TODO: replace with saltstack bootstrap script
271
272 if [ "$SALT_VERSION" == "latest" ]; then
273 pip install salt
274 else
275 pip install salt==$SALT_VERSION
276 fi
277
278 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
279 ln -s /usr/local/bin/salt-master /usr/bin/salt-master
280
281 which reclass-salt || {
282 test -e /usr/share/reclass/reclass-salt && {
283 ln -fs /usr/share/reclass/reclass-salt /usr/bin
284 }
285 }
286
287 configure_salt_master
288
289 echo -e "\nRestarting services ...\n"
290 [ -f /etc/salt/pki/minion/minion_master.pub ] && rm -f /etc/salt/pki/minion/minion_master.pub
291 $SVCTOOL salt-master restart
292}
293
294
295
296install_salt_minion_pkg()
297{
298
299 configure_pkg_repo
300
301 echo -e "\nInstalling salt minion ...\n"
302
303 case $PLATFORM_FAMILY in
304 debian)
305 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
306 ;;
307 rhel)
308 curl -L https://bootstrap.saltstack.com | $SUDO sh -s -- ${BOOTSTRAP_SALTSTACK_OPTS} &>/dev/null || true
309 ;;
310 esac
311
312
313 configure_salt_minion
314
315 $SVCTOOL salt-minion restart
316}
317
318install_salt_minion_pip()
319{
320 echo -e "\nInstalling salt minion ...\n"
321
322 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
323 ln -s /usr/local/bin/salt-minion /usr/bin/salt-minion
324
325 configure_salt_minion
326 $SVCTOOL salt-minion restart
327}
328
329
330install_salt_formula_pkg()
331{
332 configure_pkg_repo
333
334 case $PLATFORM_FAMILY in
335 debian)
336 echo "Configuring necessary formulas ..."
337
338 [ ! -d /srv/salt/reclass/classes/service ] && mkdir -p /srv/salt/reclass/classes/service
339 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
340 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
341 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
342 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
343 [ ! -d "${FORMULAS_PATH}/env/${formula_service}" ] && \
344 if ! $SUDO apt-get install -y salt-formula-${formula_service}; then
345 echo -e "\nInstall salt-formula-${formula_service} failed.\n"
346 exit 1
347 fi
348 [ ! -L "/srv/salt/reclass/classes/service/${formula_service}" ] && \
349 ln -sf ${FORMULAS_PATH}/reclass/service/${formula_service} /srv/salt/reclass/classes/service/${formula_service}
350 done
351 ;;
352 rhel)
353 # TODO
354 ;;
355 esac
356
357 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
358 [ ! -L /srv/salt/env/prd ] && ln -s ${FORMULAS_PATH}/env /srv/salt/env/prd || echo ""
359}
360
361install_salt_formula_git()
362{
363 echo "Configuring necessary formulas ..."
364
365 [ ! -d /srv/salt/reclass/classes/service ] && mkdir -p /srv/salt/reclass/classes/service
366 # Set essentials if FORMULAS_SALT_MASTER is not defined at all
367 [ -z ${FORMULAS_SALT_MASTER+x} ] && declare -a FORMULAS_SALT_MASTER=("linux" "reclass" "salt" "memcached")
368 for formula_service in "${FORMULAS_SALT_MASTER[@]}"; do
369 echo -e "\nConfiguring salt formula ${formula_service} ...\n"
370 _BRANCH=${FORMULAS_BRANCH}
371 [ ! -d "${FORMULAS_PATH}/env/_formulas/${formula_service}" ] && {
372 if ! git ls-remote --exit-code --heads ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${_BRANCH}; then
373 # Fallback to the master branch if the branch doesn't exist for this repository
374 _BRANCH=master
375 fi
376 if ! git clone ${FORMULAS_BASE}/salt-formula-${formula_service}.git ${FORMULAS_PATH}/env/_formulas/${formula_service} -b ${_BRANCH}; then
377 echo -e "\nCloning of ${FORMULAS_BASE}/salt-formula-${formula_service}.git failed.\n"
378 exit 1
379 fi
380 } || {
381 cd ${FORMULAS_PATH}/env/_formulas/${formula_service};
382 git fetch origin/${_BRANCH} || git fetch --all
383 git checkout ${_BRANCH} && git pull || git pull;
384 cd -
385 }
386 [ ! -L "/usr/share/salt-formulas/env/${formula_service}" ] && \
387 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/${formula_service} /usr/share/salt-formulas/env/${formula_service}
388 [ ! -L "/srv/salt/reclass/classes/service/${formula_service}" ] && \
389 ln -sf ${FORMULAS_PATH}/env/_formulas/${formula_service}/metadata/service /srv/salt/reclass/classes/service/${formula_service}
390 done
391
392 [ ! -d /srv/salt/env ] && mkdir -p /srv/salt/env || echo ""
393 [ ! -L /srv/salt/env/dev ] && ln -s /usr/share/salt-formulas/env /srv/salt/env/dev || echo ""
394}
395
396# MAIN
397####
398[[ "$0" != "$BASH_SOURCE" ]] || {
399# unless file is being sourced
400
401 # DEBUGING
402 #set -x
403 #test -e $(dirname $0)/env/salt.env && source $(dirname $0)/env/salt.env
404 #set
405
406 # CLI
407 while [ x"$1" != x"" ]; do
408 which curl &>/dev/null || $PKGTOOL -y install curl &>/dev/null
409
410 case $1 in
411 master )
412 install_salt_master_$SALT_SOURCE
413 install_salt_minion_$SALT_SOURCE
414 install_salt_formula_$FORMULAS_SOURCE
415 ;;
416 minion )
417 install_salt_minion_$SALT_SOURCE
418 ;;
419 esac
420 shift
421 done
422 echo DONE
423}