-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-setup-salt-env.sh
executable file
·141 lines (102 loc) · 4.37 KB
/
1-setup-salt-env.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
# Salt default Master/Minion preconfiguration script for a single computer
# Copyright (C) 2018 Pekka Helenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
###########################################################
# This script sets up default environment for basic Salt Master & Minion configuration
# for one computer
# Supported distributions: Ubuntu 18.04 LTS or variants
# Use alternative Saltstack official repository, not the one provided by default Ubuntu repositories?
# Usually Saltstack repository provides a newer Salt master/minion versions
#
# NOTE:Please make sure that Salt master & minion versions correspond each other!
#
USE_SALTREPO="true"
###########################################################
# Run package database updates?
function packageUpdateQ() {
read -r -p "Refresh package databases before installation (recommended)? [y/N] " answer
if [[ $(echo $answer | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
UPDATES=""
fi
unset answer
}
packageUpdateQ
###########################################################
if [[ -f /etc/os-release ]]; then
DISTRO=$(grep ^PRETTY_NAME /etc/os-release | grep -oP '(?<=")[^\s]*')
function installPackages() {
case "${DISTRO}" in
Ubuntu*)
pkgmgr_cmd() {
if [[ -v UPDATES ]]; then
# Update Salt to the latest version - 2018.3
# Yes, we use 16.04 until 18.04 will officially be available
if [[ $USE_SALTREPO == "true" ]]; then
wget -O - https://repo.saltstack.com/py3/ubuntu/16.04/amd64/latest/SALTSTACK-GPG-KEY.pub | apt-key add -
echo "deb http://repo.saltstack.com/py3/ubuntu/16.04/amd64/latest xenial main" > /etc/apt/sources.list.d/saltstack.list
elif [[ $USE_ALTREPO != "true" ]] && [[ -f /etc/apt/sources.list.d/saltstack.list ]]; then
rm /etc/apt/sources.list.d/saltstack.list
fi
apt-get update
fi
apt-get -y install $1
}
PKGS=(salt-master salt-minion)
;;
default)
echo -e "Can't recognize your Linux distribution. Aborting.\n"
exit 1
esac
pkgmgr_cmd "${PKGS[*]}"
systemctl enable salt-master.service &> /dev/null
systemctl restart salt-master.service &> /dev/null
}
installPackages
unset UPDATES
else
echo -e "Can't recognize your Linux distribution. Aborting.\n"
exit 1
fi
###########################################################
function saltEnvironment() {
function defaultMinionConf() {
MINION_NAME="defaultMinion"
if [[ -d /etc/salt ]]; then
echo -e "\nWriting default Salt minion configuration '${MINION_NAME}' to /etc/salt/minion\n"
echo -e "master: localhost\nid: ${MINION_NAME}" > /etc/salt/minion
systemctl enable salt-minion.service &> /dev/null
systemctl restart salt-minion.service &> /dev/null
salt-key -y -a ${MINION_NAME} > /dev/null
echo -e "Testing default Salt minion connection with the Salt master\n"
if [[ $(echo $(salt "${MINION_NAME}" test.ping &> /dev/null)$?) -ne 0 ]]; then
echo -e "Salt master can't connect to the default Salt minion. Aborting.\n"
exit 1
else
echo -e "Connection OK!\n"
fi
else
echo -e "Missing Salt configuration directory /etc/salt. Aborting.\n"
exit 1
fi
}
defaultMinionConf
}
if [ $? -eq 0 ]; then
saltEnvironment
else
echo -e "Unknown error. Aborting.\n"
exit 1
fi