-
Notifications
You must be signed in to change notification settings - Fork 34
/
sm_provision.sh
executable file
·318 lines (284 loc) · 10.2 KB
/
sm_provision.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/bin/bash
# Server Manager Provisioner
set -e
# Setup Logging
datetime_string=$(date +%Y_%m_%d__%H_%M_%S)
PROVISION_DIR=/var/log/contrail/sm_provisioning/$datetime_string
mkdir -p $PROVISION_DIR
log_file=$PROVISION_DIR/provision_$datetime_string.log
exec &> >(tee -a "$log_file")
# Prep
eval SCRIPT_PATH=$(dirname $0)
SCRIPT_PATH=$(cd $SCRIPT_PATH; pwd)
start_time=$(date +"%s")
space=" "
arrow="---->"
# Defaults
SOURCES_LIST="sources_list"
TESTBED="testbed.py"
DEFAULT_DOMAIN=""
CONTRAIL_PKG=""
CONTRAIL_STORAGE_PKG=""
STORAGE_KEYS_INI=""
HOSTIP=""
INSTALL_SM_LITE="install_sm_lite"
CLEANUP_PUPPET_AGENT=""
NO_LOCAL_REPO=1
LOCAL_REPO_DIR=/opt/contrail/contrail_local_repo
CLUSTER_ID="cluster_auto_$RANDOM"
NO_SM_MON=""
NO_SM_WEBUI=""
SM_WEBUI_PORT=""
function usage()
{
echo "Usage"
echo ""
echo "$0"
echo -e "\t-h --help"
echo -e "\t-c|--contrail-package <pkg>"
echo -e "\t-cs|--contrail-storage-package <pkg>"
echo -e "\t-sk|--storage-keys-ini-file <file>"
echo -e "\t-t|--testbed <testbed.py>"
echo -e "\t-d|--default-domain <domain name>"
echo -e "\t-ni|--no-install-sm-lite"
echo -e "\t-cp|--cleanup-puppet-agent"
echo -e "\t-nr|--no-local-repo"
echo -e "\t-nm|--no-sm-mon"
echo -e "\t-nw|--no-sm-webui"
echo -e "\t-swp|--sm-webui-port"
echo -e "\t-ip|--hostip"
echo -e "\t-cid|--cluster-id <cluster-id>"
echo ""
}
if [ "$#" -eq 0 ]; then
usage
exit
fi
while [[ $# > 0 ]]
do
key="$1"
case $key in
-c|--contrail-package)
CONTRAIL_PKG="$2"
shift # past argument
;;
-cs|--contrail-storage-package)
CONTRAIL_STORAGE_PKG="$2"
shift # past argument
;;
-sk|--storage-keys-ini-file)
STORAGE_KEYS_INI="$2"
shift # past argument
;;
-t|--testbed)
TESTBED="$2"
shift # past argument
;;
-d|--default-domain)
DEFAULT_DOMAIN="$2"
shift # past argument
;;
-nr|--no-local-repo)
NO_LOCAL_REPO=0
;;
-ni|--no-install-sm-lite)
INSTALL_SM_LITE=""
;;
-nm|--no-sm-mon)
NO_SM_MON="--nosm-mon"
;;
-nw|--no-sm-webui)
NO_SM_WEBUI="--nowebui"
;;
-swp|--sm-webui-port)
SM_WEBUI_PORT="$2"
;;
-cp|--no-cleanup-puppet-agent)
CLEANUP_PUPPET_AGENT="cleanup_puppet_agent"
;;
-cid|--cluster-id)
CLUSTER_ID="$2"
shift # past argument
;;
-ip|--hostip)
HOSTIP="$2"
shift # past argument
;;
-h|--help)
usage
exit
;;
*)
# unknown option
echo "ERROR: unknown parameter $key"
usage
exit 1
;;
esac
shift # past argument or value
done
# Verify Mandatory Arguments exists
if [ "$TESTBED" == "" ] || [ "$CONTRAIL_PKG" == "" ]; then
exit
fi
function get_real_path ()
{
eval contrail_package=$1
if [[ "$contrail_package" = /* ]]; then
echo $contrail_package
else
echo $PWD/$contrail_package
fi
}
# Update with real path
CONTRAIL_PKG=$(get_real_path $CONTRAIL_PKG)
TESTBED=$(get_real_path $TESTBED)
if [ -f "$CONTRAIL_STORAGE_PKG" ]; then
CONTRAIL_STORAGE_PKG=$(get_real_path $CONTRAIL_STORAGE_PKG)
fi
function unmount_contrail_local_repo()
{
echo "$arrow Removing contrail local repo - $LOCAL_REPO_DIR"
# Remove local repo dir
if [ -d $LOCAL_REPO_DIR ]; then
rm -rf $LOCAL_REPO_DIR
fi
# Remove preference file
if [ -f /etc/apt/preferences.d/contrail_local_repo ]; then
rm -f /etc/apt/preferences.d/contrail_local_repo
fi
set +e
grep "^deb file:$LOCAL_REPO_DIR ./" /etc/apt/sources.list
exit_status=$?
set -e
if [ $exit_status == 0 ]; then
sed -i "s#deb file:$LOCAL_REPO_DIR ./##g" /etc/apt/sources.list
apt-get update >> $log_file 2>&1
fi
}
function mount_contrail_local_repo()
{
set -e
# check if package is available
if [ ! -f "$CONTRAIL_PKG" ]; then
echo "ERROR: $CONTRAIL_PKG : No Such file..."
exit 2
fi
# mount package and create local repo
echo "$space$arrow Creating local lepo -- $LOCAL_REPO_DIR"
set +e
grep "^deb file:$LOCAL_REPO_DIR ./" /etc/apt/sources.list
exit_status=$?
set -e
if [ $exit_status != 0 ]; then
mkdir -p $LOCAL_REPO_DIR
file_type=$(file $CONTRAIL_PKG | cut -f2 -d' ')
if [ "$file_type" == "Debian" ]; then
dpkg -x $CONTRAIL_PKG $LOCAL_REPO_DIR >> $log_file 2>&1
(cd $LOCAL_REPO_DIR && tar xfz opt/contrail/contrail_packages/*.tgz >> $log_file 2>&1)
elif [ "$file_type" == "gzip" ]; then
tar xzf $CONTRAIL_PKG -C $LOCAL_REPO_DIR >> $log_file 2>&1
else
echo "ERROR: $CONTRAIL_PKG: Invalid package"
exit 2
fi
(cd $LOCAL_REPO_DIR && DEBIAN_FRONTEND=noninteractive dpkg -i binutils_*.deb dpkg-dev_*.deb libdpkg-perl_*.deb make_*.deb patch_*.deb >> $log_file 2>&1)
(cd $LOCAL_REPO_DIR && dpkg-scanpackages . | gzip -9c > Packages.gz | >> $log_file 2>&1)
datetime_string=$(date +%Y_%m_%d__%H_%M_%S)
cp /etc/apt/sources.list /etc/apt/sources.list.contrail.$datetime_string
echo >> /etc/apt/sources.list
sed -i "1 i\deb file:$LOCAL_REPO_DIR ./" /etc/apt/sources.list
cp -v /opt/contrail/contrail_server_manager/contrail_local_preferences /etc/apt/preferences.d/contrail_local_repo >> $log_file 2>&1
apt-get update >> $log_file 2>&1
fi
}
function cleanup_puppet_agent()
{
set +e
apt-get -y --purge autoremove puppet puppet-common hiera >> $log_file 2>&1
set -e
}
if [ "$CLEANUP_PUPPET_AGENT" != "" ]; then
echo "$arrow Remove puppet agent, if it is present"
cleanup_puppet_agent
fi
# Install sever manager
if [ "$INSTALL_SM_LITE" != "" ]; then
# Create a local repo from contrail-install packages
# so packages from this repo gets preferred
if [ $NO_LOCAL_REPO != 0 ]; then
echo "$arrow Provision contrail local repo"
mount_contrail_local_repo
LOCAL_REPO_MOUNTED=1
fi
echo "$arrow Install server manager without cobbler option"
pushd /opt/contrail/contrail_server_manager >> $log_file 2>&1
optional_args=""
if [ ! -z "$HOSTIP" ]; then
optional_args="--hostip=$HOSTIP"
fi
./setup.sh --all --smlite ${NO_SM_MON} ${NO_SM_WEBUI} $optional_args
popd >> $log_file 2>&1
fi
if [ -f /etc/contrail/config.global.sm.js ] && [ "$SM_WEBUI_PORT" != "" ]
then
echo "$space$arrow Changing SM Webui Port to $SM_WEBUI_PORT"
sed -i "s|config.https_port =.*|config.https_port = '${SM_WEBUI_PORT}';|g" /etc/contrail/config.global.sm.js
service supervisor-webui-sm restart >> $log_file 2>&1
fi
echo "$space$arrow Convert testbed.py to server manager entities"
# Convert testbed.py to server manager object json files
optional_args=""
if [ ! -z "$CONTRAIL_STORAGE_PKG" ]; then
optional_args="--contrail-storage-packages ${CONTRAIL_STORAGE_PKG}"
fi
if [ ! -z "$STORAGE_KEYS_INI" ]; then
STORAGE_KEYS_INI=$(python -c "import os; import sys; print(os.path.abspath(sys.argv[1]))" $STORAGE_KEYS_INI)
optional_args="$optional_args --storage-keys-ini-file $STORAGE_KEYS_INI"
fi
if [ ! -z "$CLUSTER_ID" ]; then
optional_args="$optional_args --cluster-id $CLUSTER_ID"
fi
cd $PROVISION_DIR && /opt/contrail/server_manager/client/testbed_parser.py --testbed ${TESTBED} --contrail-packages ${CONTRAIL_PKG} $optional_args
echo "$arrow Pre provision checks to make sure setup is ready for contrail provisioning"
# Precheck the targets to make sure that, ready for contrail provisioning
SERVER_MGR_IP=$(grep listen_ip_addr /opt/contrail/server_manager/sm-config.ini | grep -Po "listen_ip_addr = \K.*")
cd $PROVISION_DIR && /opt/contrail/server_manager/client/preconfig.py --server-json server.json \
--server-manager-ip ${SERVER_MGR_IP} \
--server-manager-repo-port 80
# Remove contrail local repo if any
if [[ $LOCAL_REPO_MOUNTED -eq 1 ]]; then
unmount_contrail_local_repo
fi
echo "$arrow Adding server manager objects to server manager database"
if grep -q domain /opt/contrail/server_manager/client/sm-client-config.ini; then
sed -i "s|domain.*=*|domain = ${DEFAULT_DOMAIN}|g" /opt/contrail/server_manager/client/sm-client-config.ini
else
sed -i "/^\[CLUSTER\]/a domain = ${DEFAULT_DOMAIN}" /opt/contrail/server_manager/client/sm-client-config.ini
fi
# Retrieve info from json files
cd $PROVISION_DIR && read IMAGE_ID IMAGE_VERSION IMAGE_TYPE <<< $(python -c "import json;\
fid = open('image.json', 'r');\
contents = fid.read();\
cjson = json.loads(contents);\
fid.close();\
print cjson['image'][0]['id'],\
cjson['image'][0]['version'],\
cjson['image'][0]['type']")
cd $PROVISION_DIR && CLUSTER_ID=$(python -c "import json;\
fid = open('cluster.json', 'r');\
data = json.load(fid);\
fid.close();\
print data['cluster'][0]['id']")
# Create package, cluster, server objects
cd $PROVISION_DIR && server-manager add image -f image.json
cd $PROVISION_DIR && server-manager add cluster -f cluster.json
cd $PROVISION_DIR && server-manager add server -f server.json
echo "$arrow Provisioning the cluster"
# Provision the cluster
cd $PROVISION_DIR && server-manager provision -F --cluster_id $CLUSTER_ID ${IMAGE_ID}
end_time=$(date +"%s")
diff=$(($end_time-$start_time))
echo "$arrow Provisioning is issued, and took $(($diff / 60)) minutes and $(($diff % 60)) seconds."
echo "$arrow Populated JSON files and logs are saved at $PROVISION_DIR"
echo "$arrow Check provisioning status using /opt/contrail/contrail_server_manager/provision_status.sh"