-
Notifications
You must be signed in to change notification settings - Fork 0
/
salt_testrun.sh
233 lines (167 loc) · 6.33 KB
/
salt_testrun.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/bash
# Salt Master/Minion preconfiguration script
# 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
###########################################################
# Check for command dependencies
function checkCommands() {
if [[ $(which --help 2>/dev/null) ]] && [[ $(echo --help 2>/dev/null) ]]; then
COMMANDS=(grep mkdir systemctl id wget cat tee awk sed chmod)
a=0
for command in ${COMMANDS[@]}; do
if [[ ! $(which $command 2>/dev/null) ]]; then
COMMANDS_NOTFOUND[$a]=$command
let a++
fi
done
if [[ -v COMMANDS_NOTFOUND ]]; then
echo -e "\n${bash_red}Error:${bash_color_default} The following commands could not be found: ${COMMANDS_NOTFOUND[*]}\nAborting\n"
exit 1
fi
else
exit 1
fi
}
checkCommands
###########################################################
# Check root
function checkRoot() {
if [ $(id -u) -ne 0 ]; then
echo "Run the script with root permissions (sudo or root)."
exit 1
fi
}
checkRoot
###########################################################
# Check network connection
function checkInternet() {
if [[ $(echo $(wget --delete-after -q -T 5 github.com -o -)$?) -ne 0 ]]; then
echo -e "\nInternet connection failed (GitHub). Please check your connection and try again.\n"
exit 1
fi
}
checkInternet
###########################################################
# Welcome message
function welcomeMessage() {
read -r -p "This script will install Salt Master/Minion test environment. Continue? [y/N] " answer
if [[ ! $(echo $answer | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo -e "Aborting.\n"
exit 1
fi
unset answer
}
welcomeMessage
###########################################################
# Run package database updates?
function packageUpdateQ() {
read -r -p "Refresh package databases before Salt installation? [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
#Arch*)
# pkgmgr_cmd() {
# if [[ -z UPDATES ]]; then
# pacman -Suy && pacman -Fy
# fi
# pacman -S $1
# }
# PKGS=(salt openssh vagrant virtualbox)
# ;;
Ubuntu*)
pkgmgr_cmd() {
if [[ -v UPDATES ]]; then
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() {
if [[ ! -d /srv/salt/apache/{data,scripts} ]]; then
mkdir -p /srv/salt/apache/{data,scripts}
fi
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
# Adding this sleep timer may reduce chance to failed connection tests in some cases
sleep 5
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
###########################################################
function getFiles() {
echo -e "Downloading additional files\n"
wget -q https://raw.githubusercontent.com/Fincer/salt_testrun/master/scripts/salt_pillar_apache_sample.sh \
-O /srv/salt/apache/scripts/salt_pillar_apache_sample.sh
wget -q https://raw.githubusercontent.com/Fincer/salt_testrun/master/data/sampleindex.html \
-O /srv/salt/apache/data/sampleindex.html
wget -q https://raw.githubusercontent.com/Fincer/salt_testrun/master/data/sampleindex_functions.js \
-O /srv/salt/apache/data/sampleindex_functions.js
chmod -R u+rw,go+r /srv/salt/apache
}
getFiles
cd /srv/salt/apache/scripts
bash ./salt_pillar_apache_sample.sh
sudo -u \#1000 xdg-open $(echo "http://${DISTRO}.${MINION_NAME}.com" | awk '{print tolower($0)}') &