blob: ac260949c43d79f8e4935aecfcdd1c6f0f6c44b0 [file] [log] [blame]
Anthony Youngb62b4ca2011-10-26 22:29:08 -07001#!/bin/bash
2#
3# Copyright (c) 2011 Citrix Systems, Inc.
ZhiQiang Fan7d562152013-09-20 02:20:35 +08004# Copyright 2011 OpenStack Foundation
Anthony Youngb62b4ca2011-10-26 22:29:08 -07005# All Rights Reserved.
6#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License. You may obtain
9# a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16# License for the specific language governing permissions and limitations
17# under the License.
18#
19
John Garbuttdaadf742012-04-27 18:28:28 +010020set -ex
Anthony Youngb62b4ca2011-10-26 22:29:08 -070021
John Garbuttdaadf742012-04-27 18:28:28 +010022# By default, don't remove the templates
23REMOVE_TEMPLATES=${REMOVE_TEMPLATES:-"false"}
24if [ "$1" = "--remove-templates" ]; then
25 REMOVE_TEMPLATES=true
26fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -070027
28xe_min()
29{
30 local cmd="$1"
31 shift
John Garbuttdaadf742012-04-27 18:28:28 +010032 xe "$cmd" --minimal "$@"
Anthony Youngb62b4ca2011-10-26 22:29:08 -070033}
34
35destroy_vdi()
36{
37 local vbd_uuid="$1"
38 local type=$(xe_min vbd-list uuid=$vbd_uuid params=type)
39 local dev=$(xe_min vbd-list uuid=$vbd_uuid params=userdevice)
40 local vdi_uuid=$(xe_min vbd-list uuid=$vbd_uuid params=vdi-uuid)
41
John Garbuttdaadf742012-04-27 18:28:28 +010042 if [ "$type" == 'Disk' ] && [ "$dev" != 'xvda' ] && [ "$dev" != '0' ]; then
Anthony Youngb62b4ca2011-10-26 22:29:08 -070043 xe vdi-destroy uuid=$vdi_uuid
Anthony Youngb62b4ca2011-10-26 22:29:08 -070044 fi
45}
46
47uninstall()
48{
49 local vm_uuid="$1"
50 local power_state=$(xe_min vm-list uuid=$vm_uuid params=power-state)
51
John Garbuttdaadf742012-04-27 18:28:28 +010052 if [ "$power_state" != "halted" ]; then
Anthony Youngb62b4ca2011-10-26 22:29:08 -070053 xe vm-shutdown vm=$vm_uuid force=true
Anthony Youngb62b4ca2011-10-26 22:29:08 -070054 fi
55
John Garbuttdaadf742012-04-27 18:28:28 +010056 for v in $(xe_min vbd-list vm-uuid=$vm_uuid | sed -e 's/,/ /g'); do
57 destroy_vdi "$v"
58 done
Anthony Youngb62b4ca2011-10-26 22:29:08 -070059
Anthony Youngb62b4ca2011-10-26 22:29:08 -070060 xe vm-uninstall vm=$vm_uuid force=true >/dev/null
Anthony Youngb62b4ca2011-10-26 22:29:08 -070061}
62
63uninstall_template()
64{
65 local vm_uuid="$1"
66
John Garbuttdaadf742012-04-27 18:28:28 +010067 for v in $(xe_min vbd-list vm-uuid=$vm_uuid | sed -e 's/,/ /g'); do
68 destroy_vdi "$v"
69 done
Anthony Youngb62b4ca2011-10-26 22:29:08 -070070
Anthony Youngb62b4ca2011-10-26 22:29:08 -070071 xe template-uninstall template-uuid=$vm_uuid force=true >/dev/null
Anthony Youngb62b4ca2011-10-26 22:29:08 -070072}
73
John Garbuttdaadf742012-04-27 18:28:28 +010074# remove the VMs and their disks
75for u in $(xe_min vm-list other-config:os-vpx=true | sed -e 's/,/ /g'); do
Anthony Youngb62b4ca2011-10-26 22:29:08 -070076 uninstall "$u"
77done
78
John Garbuttdaadf742012-04-27 18:28:28 +010079# remove the templates
80if [ "$REMOVE_TEMPLATES" == "true" ]; then
81 for u in $(xe_min template-list other-config:os-vpx=true | sed -e 's/,/ /g'); do
82 uninstall_template "$u"
83 done
84fi