blob: 7c1cd41e1dfde0a24ed9a9c405358a9e2d1fc63f [file] [log] [blame]
Anthony Youngb62b4ca2011-10-26 22:29:08 -07001#!/bin/bash
2#
3# Copyright (c) 2011 Citrix Systems, Inc.
4# Copyright 2011 OpenStack LLC.
5# Copyright (C) 2011 Nicira, Inc
6# All Rights Reserved.
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License. You may obtain
10# a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17# License for the specific language governing permissions and limitations
18# under the License.
19#
20
21remove_data=
22if [ "$1" = "--remove-data" ]
23then
24 remove_data=1
25fi
26
27set -eu
28
29xe_min()
30{
31 local cmd="$1"
32 shift
33 /opt/xensource/bin/xe "$cmd" --minimal "$@"
34}
35
36destroy_vdi()
37{
38 local vbd_uuid="$1"
39 local type=$(xe_min vbd-list uuid=$vbd_uuid params=type)
40 local dev=$(xe_min vbd-list uuid=$vbd_uuid params=userdevice)
41 local vdi_uuid=$(xe_min vbd-list uuid=$vbd_uuid params=vdi-uuid)
42
43 if [ "$type" = 'Disk' ] && [ "$dev" != 'xvda' ] && [ "$dev" != '0' ]
44 then
45 echo -n "Destroying data disk... "
46 xe vdi-destroy uuid=$vdi_uuid
47 echo "done."
48 fi
49}
50
51uninstall()
52{
53 local vm_uuid="$1"
54 local power_state=$(xe_min vm-list uuid=$vm_uuid params=power-state)
55
56 if [ "$power_state" != "halted" ]
57 then
58 echo -n "Shutting down VM... "
59 xe vm-shutdown vm=$vm_uuid force=true
60 echo "done."
61 fi
62
63 if [ "$remove_data" = "1" ]
64 then
65 for v in $(xe_min vbd-list vm-uuid=$vm_uuid | sed -e 's/,/ /g')
66 do
67 destroy_vdi "$v"
68 done
69 fi
70
71 echo -n "Deleting VM... "
72 xe vm-uninstall vm=$vm_uuid force=true >/dev/null
73 echo "done."
74}
75
76uninstall_template()
77{
78 local vm_uuid="$1"
79
80 if [ "$remove_data" = "1" ]
81 then
82 for v in $(xe_min vbd-list vm-uuid=$vm_uuid | sed -e 's/,/ /g')
83 do
84 destroy_vdi "$v"
85 done
86 fi
87
88 echo -n "Deleting template... "
89 xe template-uninstall template-uuid=$vm_uuid force=true >/dev/null
90 echo "done."
91}
92
93
94for u in $(xe_min vm-list other-config:os-vpx=true | sed -e 's/,/ /g')
95do
96 uninstall "$u"
97done
98
99for u in $(xe_min template-list other-config:os-vpx=true | sed -e 's/,/ /g')
100do
101 uninstall_template "$u"
102done