-
Notifications
You must be signed in to change notification settings - Fork 0
/
crowbar-finish.sh
executable file
·292 lines (243 loc) · 9.55 KB
/
crowbar-finish.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/bin/bash
# Copyright 2015, Greg Althaus
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
date
. /etc/profile
hostname_re='([[:alnum:]]+\.){2,}[[:alnum:]]+'
check_hostname() {
[[ $(hostname) =~ $hostname_re ]] && return
echo "The hostname for the system must already be set to its proper name!"
exit 1
}
check_hostname
[[ $FQDN ]] || export FQDN="$(hostname)"
DOMAINNAME=${FQDN#*.}
HOSTNAME=${FQDN%%.*}
set -x
# Get config file from consul
CONSUL_MACL=$(jq .acl_master_token </etc/consul.d/default.json | awk -F\" '{ print $2 }')
curl "http://127.0.0.1:8500/v1/kv/opencrowbar/private/bootstrap?token=$CONSUL_MACL&raw" > config/processed.json
# Process networks
admin_nets=()
network_count=`jq ".networks | length" config/processed.json`
for ((i=0; i < network_count; i++)) ; do
network=`jq ".networks[$i]" config/processed.json`
name=`jq -r ".networks[$i].name" config/processed.json`
category=`jq -r ".networks[$i].category" config/processed.json`
if crowbar networks show $name >/dev/null 2>&1 ; then
crowbar networks update $name "$network"
else
crowbar networks create "$network"
fi
if [ "$category" == "admin" ] ; then
admin_nets=(${admin_nets[@]} $name)
fi
done
function contains () {
local arr=(${@:2})
local el=$1
local marr=(${arr[@]/#$el/})
[[ "${#arr[@]}" != "${#marr[@]}" ]]
}
# join networks
network_count=`jq ".networks_to_join | length" config/processed.json`
for ((i=0; i < network_count; i++)) ; do
network=`jq -r ".networks_to_join[$i]" config/processed.json`
crowbar networks add $network to "$FQDN"
if contains $network $admin_nets ; then
admin_net_name=$network
fi
done
# Process services/servers
service_count=`jq ".services | length" config/processed.json`
for ((i=0; i < service_count; i++)) ; do
service_type=`jq -r ".services[$i].type" config/processed.json`
service_name=`jq -r ".services[$i].name" config/processed.json`
has_service=`jq -r ".services[$i].has_service" config/processed.json`
if [ "$has_service" == "false" ] ; then
continue
fi
service_role=`jq -r ".services[$i].service_role" config/processed.json`
if [ "$service_role" == "null" ] ; then
if [[ $service_name == *"-"* ]] ; then
service_role="${service_name}_service"
else
service_role="${service_name}-service"
fi
fi
# Add the internal services
crowbar nodes bind "system-phantom.internal.local" to "$service_role"
done
for ((i=0; i < service_count; i++)) ; do
# Add external servers.
service_type=`jq -r ".services[$i].type" config/processed.json`
service_name=`jq -r ".services[$i].name" config/processed.json`
if [ "$service_type" == "external" ] ; then
continue
fi
server_role=`jq -r ".services[$i].server_role" config/processed.json`
if [ "$server_role" == "null" ] ; then
if [[ $service_name == *"-"* ]] ; then
server_role="${service_name}_server"
else
server_role="${service_name}-server"
fi
fi
IFS=',' read -ra ADDR <<< "$server_role"
for k in "${ADDR[@]}"; do
crowbar nodes bind "$FQDN" to $k
done
attrs=`jq ".services[$i].attributes" config/processed.json`
if [ "$attrs" != "null" ] ; then
count=`jq ".services[$i].attributes|keys|length" config/processed.json`
for ((k=0; k < count; k++)) ; do
kname=`jq -r ".services[$i].attributes|keys|.[$k]" config/processed.json`
kvalue=`jq ".services[$i].attributes[\"$kname\"]" config/processed.json`
crowbar nodes set "$FQDN" attrib $kname to "{ \"value\": $kvalue }"
done
fi
done
for ((i=0; i < service_count; i++)) ; do
service_type=`jq -r ".services[$i].type" config/processed.json`
service_name=`jq -r ".services[$i].name" config/processed.json`
if [ "$service_type" == "internal" ] ; then
continue
fi
if [[ $service_name == *"-"* ]] ; then
service_name="${service_name}_service"
else
service_name="${service_name}-service"
fi
datacenter=`jq -r ".services[$i].datacenter" config/processed.json`
if [ "$datacenter" == "null" ] ; then
datacenter="opencrowbar"
fi
node_name=`jq -r ".services[$i].node_name" config/processed.json`
if [ "$node_name" == "null" ] ; then
node_name="External"
fi
service_ip=`jq -r ".services[$i].service_ip" config/processed.json`
service_port=`jq -r ".services[$i].service_port" config/processed.json`
service_tag=`jq -r ".services[$i].service_tag" config/processed.json`
if [ "$service_tag" == "null" ] ; then
service_tag="system"
fi
curl -X PUT -d "{\"Datacenter\": \"$datacenter\", \"Node\": \"$node_name\", \"Address\": \"$service_ip\", \"Service\": {\"Service\": \"$service_name\", \"Port\": $service_port, \"Address\": \"$service_ip\", \"Tags\": [ \"$service_tag\" ]} }" http://127.0.0.1:8500/v1/catalog/register
attrs=`jq ".services[$i].keys" config/processed.json`
if [ "$attrs" != "null" ] ; then
CONSUL_MACL=$(jq .acl_master_token </etc/consul.d/default.json | awk -F\" '{ print $2 }')
count=`jq ".services[$i].keys|keys|length" config/processed.json`
for ((k=0; k < count; k++)) ; do
kname=`jq -r ".services[$i].keys|keys|.[$k]" config/processed.json`
kvalue=`jq -r ".services[$i].keys[\"$kname\"]" config/processed.json`
curl -X PUT -d "$kvalue" http://127.0.0.1:8500/v1/kv/$kname?token=$CONSUL_MACL
done
fi
done
# Deployments
deployment_count=`jq ".deployments | length" config/processed.json`
for ((i=0; i < deployment_count; i++)) ; do
name=`jq -r ".deployments[$i].deployment.name" config/processed.json`
deployment=`jq ".deployments[$i].deployment" config/processed.json`
# Create or update the deployment
if crowbar deployments show $name >/dev/null 2>&1 ; then
crowbar deployments update $name "$deployment"
else
crowbar deployments create "$deployment"
fi
# Add roles
dr_count=`jq ".deployments[$i].roles | length" config/processed.json`
for ((dri=0; dri < dr_count; dri++)) ; do
dr_role=`jq -r ".deployments[$i].roles[$dri]" config/processed.json`
crowbar deployments bind $name to $dr_role 2>/dev/null || true
done
# Update attributes
count=`jq ".deployments[$i].attributes|keys|length" config/processed.json`
for ((k=0; k < count; k++)) ; do
kname=`jq -r ".deployments[$i].attributes|keys|.[$k]" config/processed.json`
kvalue=`jq ".deployments[$i].attributes[\"$kname\"]" config/processed.json`
crowbar deployments set $name attrib $kname to "{ \"value\": $kvalue }"
done
crowbar deployments commit $name
done
# Commit the phantom
crowbar nodes propose "system-phantom.internal.local"
crowbar nodes set "system-phantom.internal.local" attrib dns-domain to "{ \"value\": \"$DOMAINNAME\" }"
crowbar nodes commit "system-phantom.internal.local"
# Add the now mostly empty admin-node
crowbar nodes bind "$FQDN" to crowbar-build-root-key
crowbar nodes bind "$FQDN" to crowbar-admin-node
# Add keys into the system
keys=`jq -r .ssh_keys config/processed.json`
crowbar deployments set system attrib crowbar-access_keys to "{ \"value\": $keys }"
crowbar deployments commit system
# Add/Update DNS Filters into the system
filter_count=`jq ".filters | length" config/processed.json`
for ((i=0; i < filter_count; i++)) ; do
dnf=`jq ".filters[$i]" config/processed.json`
name=`jq -r ".filters[$i].name" config/processed.json`
if crowbar dnsnamefilters show $name >/dev/null 2>&1 ; then
crowbar dnsnamefilters update $name "$dnf"
else
crowbar dnsnamefilters create "$dnf"
fi
done
# Add/Update users into the system
user_count=`jq ".users | length" config/processed.json`
for ((i=0; i < user_count; i++)) ; do
user=`jq ".users[$i]" config/processed.json`
name=`jq -r ".users[$i].username" config/processed.json`
if crowbar users show $name >/dev/null 2>&1 ; then
crowbar users update $name "$user"
else
crowbar users create "$user"
fi
done
# Figure out what IP addresses we should have, and add them.
# If the above adds an address, we need to make sure it starts on the node.
ip_re='([0-9a-f.:]+/[0-9]+)'
netline=$(crowbar nodes addresses "$FQDN" on $admin_net_name)
nets=(${netline//,/ })
for net in "${nets[@]}"; do
[[ $net =~ $ip_re ]] || continue
net=${BASH_REMATCH[1]}
# Make this more complicated and exact later.
ip addr add "$net" dev eth0 || :
echo "${net%/*} $FQDN" >> /etc/hosts || :
done
# If we have an http_proxy set, then we should make sure
# we have no-proxy setup correctly and use it.
if [[ $http_proxy ]] ; then
# Now that we have shiny new IP addresses, make sure that Squid has the right
# addresses in place for always_direct exceptions, and pick up the new proxy
# environment variables.
(
. bootstrap.sh
chef-solo -c /opt/opencrowbar/core/bootstrap/chef-solo.rb -o "${proxy_recipes}"
)
. /etc/profile
# Make sure that Crowbar is running with the proper environment variables
service crowbar stop
service crowbar start
while ! crowbar users list; do
sleep 1
done
fi
crowbar nodes commit "$FQDN"
# Mark the node as alive.
crowbar nodes update "$FQDN" '{"alive": true}'
echo "Configuration Complete, you can watch annealing from the UI. \`su - crowbar\` to begin managing the system."
# Converge the admin node.
crowbar converge && date