blob: 1ed249433aedb82281a74ccfe87e875e4f1db3b5 [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
Sean Dague0b865a52013-10-22 11:37:35 -040025 REMOVE_TEMPLATES=true
John Garbuttdaadf742012-04-27 18:28:28 +010026fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -070027
28xe_min()
29{
Sean Dague0b865a52013-10-22 11:37:35 -040030 local cmd="$1"
31 shift
32 xe "$cmd" --minimal "$@"
Anthony Youngb62b4ca2011-10-26 22:29:08 -070033}
34
35destroy_vdi()
36{
Sean Dague0b865a52013-10-22 11:37:35 -040037 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)
Anthony Youngb62b4ca2011-10-26 22:29:08 -070041
Sean Dague0b865a52013-10-22 11:37:35 -040042 if [ "$type" == 'Disk' ] && [ "$dev" != 'xvda' ] && [ "$dev" != '0' ]; then
43 xe vdi-destroy uuid=$vdi_uuid
44 fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -070045}
46
47uninstall()
48{
Sean Dague0b865a52013-10-22 11:37:35 -040049 local vm_uuid="$1"
50 local power_state=$(xe_min vm-list uuid=$vm_uuid params=power-state)
Anthony Youngb62b4ca2011-10-26 22:29:08 -070051
Sean Dague0b865a52013-10-22 11:37:35 -040052 if [ "$power_state" != "halted" ]; then
53 xe vm-shutdown vm=$vm_uuid force=true
54 fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -070055
Sean Dague0b865a52013-10-22 11:37:35 -040056 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
Sean Dague0b865a52013-10-22 11:37:35 -040060 xe vm-uninstall vm=$vm_uuid force=true >/dev/null
Anthony Youngb62b4ca2011-10-26 22:29:08 -070061}
62
63uninstall_template()
64{
Sean Dague0b865a52013-10-22 11:37:35 -040065 local vm_uuid="$1"
Anthony Youngb62b4ca2011-10-26 22:29:08 -070066
Sean Dague0b865a52013-10-22 11:37:35 -040067 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
Sean Dague0b865a52013-10-22 11:37:35 -040071 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
Sean Dague0b865a52013-10-22 11:37:35 -040076 uninstall "$u"
Anthony Youngb62b4ca2011-10-26 22:29:08 -070077done
78
John Garbuttdaadf742012-04-27 18:28:28 +010079# remove the templates
80if [ "$REMOVE_TEMPLATES" == "true" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -040081 for u in $(xe_min template-list other-config:os-vpx=true | sed -e 's/,/ /g'); do
82 uninstall_template "$u"
83 done
John Garbuttdaadf742012-04-27 18:28:28 +010084fi