~canonical-platform-qa/ubuntu-qa-tools/practisubunit

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
#!/bin/sh -e
#
# Copyright (C) 2007-2012 Canonical, Ltd.
# Author: Jamie Strandboge <jamie@canonical.com>
# License: GPLv3
#

ustconf="$HOME/.uqt-vm-tools.conf"
if [ -s "$ustconf" ]; then
    . "$ustconf"
else
    echo "Could not find '$ustconf'"
    exit 1
fi

abort_if_root() {
    if [ "$USER" = "root" ]; then
        echo "You must not be root. Aborting"
        exit 1
    fi
}

vm_exists() {
    if [ -z "$1" ]; then
        exit 1
    fi
    vm="$1"

    if virsh --connect ${vm_connect} list --all 2>/dev/null | grep -q " $vm " ; then
        return 0
    else
        return 1
    fi
}

vm_running() {
    if [ -z "$1" ]; then
        exit 1
    fi
    vm="$1"

    if virsh --connect ${vm_connect} domstate "$vm" 2>/dev/null | grep -q "running" ; then
        return 0
    else
        return 1
    fi
}

get_disks() {
    if [ -z "$1" ]; then
        exit 1
    fi
    vm="$1"

    found_disks=""
    in_disk="no"
    virsh --connect ${vm_connect} dumpxml "$vm" 2>/dev/null | while read line ; do
        if echo "$line" | egrep -q '^<disk '; then
            in_disk="yes"
            continue
        elif echo "$line" | egrep -q '^</devices>' ; then
            echo $found_disks
            break
        elif [ "$in_disk" = "yes" ]; then
            if echo "$line" | egrep -q "^<source file=.*\.iso'/>" ; then
                # found an iso so skip it, otherwise vm-remove might remove
                # your iso directory (owchy)
                in_disk="no"
                continue
            elif echo "$line" | egrep -q '^<source file=' ; then
                tmpdisk=`echo "$line" | cut -d "'" -f 2`
                found_disks="$found_disks $tmpdisk"
                in_disk="no"
            fi
        fi
    done
}

# waits ARG2 seconds for a host to come up and returns the hostname
vm_wait() {
    if [ -z "$1" ]; then
        exit 1
    fi
    local vm="$1"
    local timeout="$2"
    if [ -z "$timeout" ]; then
        timeout=100
    fi
    local quiet="$3"

    # wait until the machine is running
    local host=""
    if [ -z "$quiet" ]; then
        echo -n "Waiting for '$vm' to come up: " >&2
    fi
    for i in $(seq 1 $timeout) ; do
        host=$(vm_ping "$vm" 2>/dev/null)
        if [ -n "$host" ] ; then
            if [ -z "$quiet" ]; then
                echo " host is up" >&2
            fi
            echo "$host"
            break
        fi
        if [ -z "$quiet" ]; then
            echo -n "." >&2
        fi
        sleep 1
    done
    if [ -z "$host" ]; then
        echo " timed out waiting for '$vm'" >&2
    fi
}

# returns the hostname for a machine if it is up
vm_ping() {
    m="$1"
    if ! vm_exists $m ; then
        echo "'$m' does not exist" >&2
        return
    fi

    if ! vm_running $m ; then
        echo "'$m' is not running" >&2
        return
    fi

    avahi=".local"
    if [ "$vm_host_use_avahi" = "no" ] || [ "$vm_host_use_avahi" = "NO" ]; then
        avahi=""
    fi
    for suffix in '.' $avahi '' ; do
        h=${m}${suffix}
        if ping -c1 -w1 $h >/dev/null 2>&1 ;then
	    # Workaround recent lack of '-q' functionality in netcat-openbsd.
            # See https://launchpad.net/bugs/590925 for details.
            arg="-w1"
            if LANG=C nc -q 2>&1 | grep -q 'requires an argument' ; then
                arg="-q1"
            fi
            if nc $arg $h 22 </dev/null 2>/dev/null | grep -q ^SSH-2; then
                echo "$h"
                break
            fi
        fi
    done
}

vm_get_machines_by_prefix() {
    p="$1"
    a="$2"
    virsh --connect ${vm_connect} list --all 2>/dev/null | awk '{print $2}' | egrep "^$p.*$a$" | sort
}

vm_get_release_by_machine_name() {
    m="$1"
    for r in $vm_release_list ; do
        echo "$m" | grep -q "$r" && {
            echo "$r"
            return
        }
    done
}

vm_can_snapshot() {
    m="$1"
    if ! vm_exists $m ; then
        echo "'$m' does not exist" >&2
        return 1
    fi

    disks=`get_disks $m`
    if [ -z "$disks" ]; then
        echo "Could not find any disks" >&2
        return 1
    fi

    # iterate through the disks. If all disks have the pristine file, that
    # means we can do snapshots.
    for d in $disks ; do
        real=`readlink -f "$d"`
        if ! echo "$real" | egrep -q '.qcow2$' ; then
            echo "File extension for '$real' is not 'qcow2'" >&2
            return 1
        fi
        dir=`dirname "$real"`
        bn=`basename "$real" .qcow2`
        pristine="$dir/$bn.pristine.qcow2"
        if [ ! -e "$pristine" ]; then
            echo "'$pristine' does not exist" >&2
            return 1
        fi
    done
    return
}

vm_view() {
    vm_wait=
    if [ "$1" = "--wait" ]; then
        vm_wait="$1"
        shift
    fi

    if [ -n "$vm_viewer_args" ] && ! echo "$vm_viewer_args" | egrep -q "^[a-zA-Z0-9_=:/\.,\-]+$" ; then
        echo "Illegal character in '$vm_viewer_args'" >&2
        return 1
    fi

    m="$1"
    if ! vm_exists $m ; then
        echo "'$m' does not exist" >&2
        return 1
    fi

    if [ -n "$vm_viewer" ] && ! which "$vm_viewer" >/dev/null ; then
        echo "Could not find '$vm_viewer', using 'virt-viewer' instead" >&2
        vm_viewer="virt-viewer"
    fi

    sleep 0.5
    cd /tmp
    if [ -z "$vm_viewer" ] || [ "$vm_viewer" = "virt-viewer" ]; then
        nohup virt-viewer $vm_wait -c ${vm_connect} "$m" >/dev/null 2>&1 &
    else
        nohup "$vm_viewer" $vm_viewer_args `virsh vncdisplay "$m" | grep '^[1-9:]'` >/dev/null 2>&1 &
    fi
    cd - >/dev/null
}