forked from mvallim/kubernetes-under-the-hood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-config-interfaces.sh
executable file
·108 lines (95 loc) · 2.5 KB
/
post-config-interfaces.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
#!/bin/bash
PROG="$(basename "${0}")"
SCRIPT_DIR="$(cd "$(dirname "${0}")" && pwd)"
VMNAME=""
POST_CONFIG_INTERFACES_FILE=""
VBOXMANAGE=`which vboxmanage`
usage() {
echo -e "USAGE: ${PROG} [--vm-name <VMNAME>]\n"
}
help_exit() {
usage
echo "This is a utility script for post configure vm.
Options:
-v, --vm-name VMNAME
Name of VistualBox vm.
-i, --post-config-interfaces POST_CONFIG_INTERFACES_FILE
Path to an post config interface data file.
-h, --help Output this help message.
"
exit 0
}
assign() {
key="${1}"
value="${key#*=}"
if [[ "${value}" != "${key}" ]]; then
# key was of the form 'key=value'
echo "${value}"
return 0
elif [[ "x${2}" != "x" ]]; then
echo "${2}"
return 2
else
output "Required parameter for '-${key}' not specified.\n"
usage
exit 1
fi
keypos=$keylen
}
while [[ $# -ge 1 ]]; do
key="${1}"
case $key in
-*)
keylen=${#key}
keypos=1
while [[ $keypos -lt $keylen ]]; do
case ${key:${keypos}} in
v|-vm-name)
VMNAME=$(assign "${key:${keypos}}" "${2}")
if [[ $? -eq 2 ]]; then shift; fi
keypos=$keylen
;;
i|-post-config-interfaces)
POST_CONFIG_INTERFACES_FILE=$(assign "${key:${keypos}}" "${2}")
if [[ $? -eq 2 ]]; then shift; fi
keypos=$keylen
;;
h*|-help)
help_exit
;;
*)
output "Unknown option '${key:${keypos}}'.\n"
usage
exit 1
;;
esac
((keypos++))
done
;;
esac
shift
done
if [[ -z ${VMNAME} ]]; then
echo "VM name not found"
exit 1
fi
if [[ -z ${POST_CONFIG_INTERFACES_FILE} || ! -f ${POST_CONFIG_INTERFACES_FILE} ]]; then
echo "Post config interface data file, not found"
exit 1
fi
NICS=`cat ${POST_CONFIG_INTERFACES_FILE} | shyaml keys`
for nic in ${NICS}; do
ID=`cat ${POST_CONFIG_INTERFACES_FILE} | shyaml get-value $nic.id`
TYPE=`cat ${POST_CONFIG_INTERFACES_FILE} | shyaml get-value $nic.type`
if [[ "${TYPE}" = "nat" ]]; then
${VBOXMANAGE} modifyvm ${VMNAME} --nic${ID} ${TYPE}
fi
if [[ "${TYPE}" = "intnet" ]]; then
NAME=`cat ${POST_CONFIG_INTERFACES_FILE} | shyaml get-value $nic.name`
${VBOXMANAGE} modifyvm ${VMNAME} --nic${ID} ${TYPE} --intnet${ID} ${NAME}
fi
if [[ "${TYPE}" = "hostonly" ]]; then
ADAPTER=`cat ${POST_CONFIG_INTERFACES_FILE} | shyaml get-value $nic.adapter`
${VBOXMANAGE} modifyvm ${VMNAME} --nic${ID} ${TYPE} --hostonlyadapter${ID} ${ADAPTER}
fi
done