blob: 53a620b0435d7ebbf0099ddb53ab832b0e043f12 (
plain)
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
|
#!/bin/bash
# Test if the named environment variable is set and not zero length
# is_set env-var
function is_set {
local var=\$"$1"
eval "[[ -n \"$var\" ]]"
}
# Prints backtrace info
# filename:lineno:function
# backtrace level
function backtrace {
local level=$1
local deep
deep=$((${#BASH_SOURCE[@]} - 1))
echo "[Call Trace]"
while [ $level -le $deep ]; do
echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
deep=$((deep - 1))
done
}
# Prints line number and "message" in error format
# err $LINENO "message"
function err {
local exitcode=$?
local xtrace
xtrace=$(set +o | grep xtrace)
set +o xtrace
local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
echo $msg 1>&2;
if [[ -n ${LOGDIR} ]]; then
echo $msg >> "${LOGDIR}/error.log"
fi
$xtrace
return $exitcode
}
# Prints line number and "message" then exits
# die $LINENO "message"
function die {
local exitcode=$?
local xtrace
xtrace=$(set +o | grep xtrace)
set +o xtrace
local line=$1; shift
if [ $exitcode == 0 ]; then
exitcode=1
fi
backtrace 2
err $line "$*"
# Give buffers a second to flush
sleep 1
$xtrace
exit $exitcode
}
# Checks an environment variable is not set or has length 0 OR if the
# exit code is non-zero and prints "message" and exits
# NOTE: env-var is the variable name without a '$'
# die_if_not_set $LINENO env-var "message"
function die_if_not_set {
local exitcode=$?
local xtrace
xtrace=$(set +o | grep xtrace)
set +o xtrace
local line=$1; shift
local evar=$1; shift
if ! is_set $evar || [ $exitcode != 0 ]; then
die $line "$*"
fi
$xtrace
}
# Check the function is defined
# die_if_not_defined $LINENO function-name "message"
function die_if_not_defined {
local xtrace
xtrace=$(set +o | grep xtrace)
set +o xtrace
local line=$1; shift
local func_name=$1; shift
if ! declare -f "$func_name" > /dev/null; then
die $line "$*"
fi
$xtrace
}
# Wait until the condition is met.
# wait_until condition timeout interval
function wait_until {
local condition="$1"
local timeout=$2
local interval=$3
while eval ${condition}
do
sleep ${interval}
timeout=$((${timeout} - ${interval}))
if [[ ${timeout} < 0 ]]; then
err $LINENO "timed out ($condition)..."
return 1
fi
done
}
# Print IP address of the first vNIC owned by specified VM via virsh
# get_first_vnic_ip vm_name
function get_first_vnic_ip {
local vm_name=$1
_vnic_mac=$(sudo virsh domiflist $vm_name | \
sed -n -e 's/^.*\([0-9a-f]\{2\}\(:[0-9a-f]\{2\}\)\{5\}\).*$/\1/p' | \
head -1)
die_if_not_set $LINENO _vnic_mac
_vnic_ip=$(arp -e | grep $_vnic_mac | awk '{print $1}')
die_if_not_set $LINENO _vnic_ip
echo $_vnic_ip
}
|