-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·225 lines (201 loc) · 6.17 KB
/
setup.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
#!/bin/bash
tmp_file=$(mktemp)
function finish
{
rm -f $tmp_file
}
trap finish EXIT
function init_ssh_host
{
if [ $# -ne 1 ]; then
echo "No arg passed to init_ssh_host"
exit 1
fi
local host=$1
ssh-keygen -R $host &> $tmp_file
if [ $? -ne 0 ]; then
echo "Failed removing key $host"; cat $tmp_file; return 1;
fi
ssh -o StrictHostKeyChecking=no root@$host 'uname -a' &> $tmp_file
if [ $? -ne 0 ]; then
echo "Failed initial login to $host"; cat $tmp_file; return 1;
fi
return $?
}
cd "$(dirname $0)"
#Has DO_PAT and SSH_FINGERPRINT defined
source ./private.sh
if [ $# -eq 0 ]; then
echo "no hosts specified nothing to do"
exit 1
elif [ "$1" == "-d" -o "$1" == "--destroy" ]; then
terraform plan -destroy -out=destroy_plan.tfplan \
-var "do_token=${DO_PAT}" \
-var "pub_key=$HOME/.ssh/id_rsa.pub" \
-var "pvt_key=$HOME/.ssh/id_rsa" \
-var "ssh_fingerprint=$SSH_FINGERPRINT" \
./infrastructure/ &> $tmp_file
if [ $? -ne 0 ]; then
echo "Failed planning terraform destroy"
cat $tmp_file
exit 1
fi
terraform apply destroy_plan.tfplan &> $tmp_file
if [ $? -eq 0 ]; then
rm -f destroy_plan.tfplan
else
echo "Failed implementing terraform destroy"
cat $tmp_file
exit 1
fi
exit 0
fi
#Prompt for sudo password so it doesn't have to happen later
sudo --validate
master=$1
shift
minions=""
while [ $# -ne 0 ]; do
minions="$minions$1 "
shift
done
echo "Master: $master"
echo "Minions: $minions"
#Cleanup then create the minion terraform files
rm -f infrastructure/minion_*.tf
for minion in $minions; do
sed -e "s/minion_name/$minion/" infrastructure/minion.template \
> infrastructure/minion_$minion.tf
done
echo "Implimenting terraform"
terraform apply \
-var "do_token=${DO_PAT}" \
-var "pub_key=$HOME/.ssh/id_rsa.pub" \
-var "pvt_key=$HOME/.ssh/id_rsa" \
-var "ssh_fingerprint=$SSH_FINGERPRINT" \
./infrastructure/ &> $tmp_file
if [ $? -ne 0 ]; then echo "Failed terraform step"; cat $tmp_file; fi
ipv4_address=""
name=""
while read -r line; do
if [[ "$line" =~ digitalocean ]]; then
sudo sed -i -e "/.*$name.*/d" /etc/hosts
echo "$ipv4_address $name" | sudo tee -a /etc/hosts
elif [[ "$line" =~ name\ = ]]; then
name=$(echo "$line" | sed -e 's/^.*= *//')
elif [[ "$line" =~ ipv4_address ]]; then
ipv4_address=$(echo "$line" | sed -e 's/^.*= *//')
else
/bin/true
fi
done < <(terraform show | tac; echo '')
echo "== Initializing salt master $master =="
init_ssh_host $master &> $tmp_file
if [ $? -ne 0 ]; then
echo "Failed initializing ssh to master"
cat $tmp_file
exit
fi
rm -f init.sh
cat > init.sh <<- EOT
#!/bin/bash
tmp_file=\$(mktemp)
function finish
{
rm -f \$tmp_file
}
trap finish EXIT
echo 'America/Chicago' > /etc/timezone
apt-get install --yes screen vim &> \$tmp_file
if [ \$? -ne 0 ]; then
echo "Failed installing screen and vim:"
cat \$tmp_file
exit 1
fi
echo " Adding salt repo"
add-apt-repository --yes ppa:saltstack/salt &> \$tmp_file
if [ \$? -ne 0 ]; then echo "Failed ppa add:"; cat \$tmp_file; exit 1; fi
echo " Updating apt \$(hostname)"
apt-get update &> \$tmp_file
if [ \$? -ne 0 ]; then echo "Failed apt update:"; cat \$tmp_file; exit 1; fi
echo " Installing salt-master"
apt-get install --yes salt-master &> \$tmp_file
if [ \$? -ne 0 ]; then echo "Failed install master:"; cat \$tmp_file; exit 1; fi
EOT
scp init.sh root@$master:/root/ >/dev/null
ssh root@$master 'bash /root/init.sh'
if [ $? -ne 0 ]; then echo "Failed running init on master"; exit; fi
scp pre_salt/salt_master.conf root@$master:/etc/salt/master >/dev/null
if [ $? -ne 0 ]; then echo "Failed coping over salt master"; exit; fi
scp -r srv/* root@$master:/srv/ >/dev/null
if [ $? -ne 0 ]; then echo "Failed copying salt info to master"; exit; fi
ssh root@$master 'systemctl start salt-master'
if [ $? -ne 0 ]; then echo "Failed starting salt-master"; exit; fi
rm -f init.sh
cat > init.sh <<- EOT
#!/bin/bash
tmp_file=\$(mktemp)
function finish
{
rm -f \$tmp_file
}
trap finish EXIT
echo 'America/Chicago' > /etc/timezone
echo " Adding salt repo"
add-apt-repository --yes ppa:saltstack/salt &> \$tmp_file
if [ \$? -ne 0 ]; then echo "Failed ppa add:"; cat \$tmp_file; exit 1; fi
echo " Updating apt \$(hostname -s)"
apt-get update &> \$tmp_file
if [ \$? -ne 0 ]; then echo "Failed apt update:"; cat \$tmp_file; exit 1; fi
echo " Installing salt-minion \$(hostname)"
apt-get install --yes salt-minion &> \$tmp_file
if [ \$? -ne 0 ]; then
echo "Failed install of salt-minion trying again";
apt-get install --yes salt-minion &> \$tmp_file
if [ \$? -ne 0 ]; then
echo "Failed retry of install of salt-minion";
cat \$tmp_file;
exit 1;
fi
fi
echo '$(getent hosts saltmaster | awk '{ print $1 }') salt' >> /etc/hosts
echo 'mine_interval: 5' > /etc/salt/minion.d/minion.conf
systemctl start salt-minion
EOT
for minion in $minions; do
echo "== Initializing minion $minion =="
init_ssh_host $minion
if [ $? -ne 0 ]; then echo "Failed init of minion $minion"; continue; fi
scp init.sh root@$minion:/root/ >/dev/null
ssh root@$minion 'bash /root/init.sh'
if [ $? -ne 0 ]; then echo "Failed running init on $minion"; continue; fi
done
echo "sleep for 10 to let all minions connect"
sleep 10
all_accepted=1
for minion in $minions; do
ssh root@$master "salt-key -a $minion -y" &> $tmp_file
ssh root@$master "salt-key --list=accepted" | grep --quiet $minion
if [ $? -ne 0 ]; then
echo "Failed accepting $minion, retrying"
sleep 5
ssh root@$master "salt-key -a $minion -y" &> $tmp_file
fi
ssh root@$master "salt-key --list=accepted" | grep --quiet $minion
if [ $? -ne 0 ]; then
echo "Failed accepting $minion"
all_accepted=0
continue
fi
echo "Accepted minion $minion"
done
if [ $all_accepted -ne 1 ]; then
echo "Not all minions were accepted"
ssh root@$master "salt-key --list=un"
exit 1
fi
ssh root@$master "salt '*' state.highstate"
if [ $? -ne 0 ]; then
echo "##==> Failed bringing up highstate <==##"
fi
echo "done"