blob: 96dad7e852dc32cbb33b00bd99a491bcff98ce29 [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"
Ian Wienandada886d2015-10-07 14:06:26 +110038 local type
39 type=$(xe_min vbd-list uuid=$vbd_uuid params=type)
40 local dev
41 dev=$(xe_min vbd-list uuid=$vbd_uuid params=userdevice)
42 local vdi_uuid
43 vdi_uuid=$(xe_min vbd-list uuid=$vbd_uuid params=vdi-uuid)
Anthony Youngb62b4ca2011-10-26 22:29:08 -070044
Sean Dague0b865a52013-10-22 11:37:35 -040045 if [ "$type" == 'Disk' ] && [ "$dev" != 'xvda' ] && [ "$dev" != '0' ]; then
46 xe vdi-destroy uuid=$vdi_uuid
47 fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -070048}
49
50uninstall()
51{
Sean Dague0b865a52013-10-22 11:37:35 -040052 local vm_uuid="$1"
Ian Wienandada886d2015-10-07 14:06:26 +110053 local power_state
54 power_state=$(xe_min vm-list uuid=$vm_uuid params=power-state)
Anthony Youngb62b4ca2011-10-26 22:29:08 -070055
Sean Dague0b865a52013-10-22 11:37:35 -040056 if [ "$power_state" != "halted" ]; then
57 xe vm-shutdown vm=$vm_uuid force=true
58 fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -070059
Sean Dague0b865a52013-10-22 11:37:35 -040060 for v in $(xe_min vbd-list vm-uuid=$vm_uuid | sed -e 's/,/ /g'); do
61 destroy_vdi "$v"
62 done
Anthony Youngb62b4ca2011-10-26 22:29:08 -070063
Sean Dague0b865a52013-10-22 11:37:35 -040064 xe vm-uninstall vm=$vm_uuid force=true >/dev/null
Anthony Youngb62b4ca2011-10-26 22:29:08 -070065}
66
67uninstall_template()
68{
Sean Dague0b865a52013-10-22 11:37:35 -040069 local vm_uuid="$1"
Anthony Youngb62b4ca2011-10-26 22:29:08 -070070
Sean Dague0b865a52013-10-22 11:37:35 -040071 for v in $(xe_min vbd-list vm-uuid=$vm_uuid | sed -e 's/,/ /g'); do
72 destroy_vdi "$v"
73 done
Anthony Youngb62b4ca2011-10-26 22:29:08 -070074
Sean Dague0b865a52013-10-22 11:37:35 -040075 xe template-uninstall template-uuid=$vm_uuid force=true >/dev/null
Anthony Youngb62b4ca2011-10-26 22:29:08 -070076}
77
John Garbuttdaadf742012-04-27 18:28:28 +010078# remove the VMs and their disks
79for u in $(xe_min vm-list other-config:os-vpx=true | sed -e 's/,/ /g'); do
Sean Dague0b865a52013-10-22 11:37:35 -040080 uninstall "$u"
Anthony Youngb62b4ca2011-10-26 22:29:08 -070081done
82
John Garbuttdaadf742012-04-27 18:28:28 +010083# remove the templates
84if [ "$REMOVE_TEMPLATES" == "true" ]; then
Sean Dague0b865a52013-10-22 11:37:35 -040085 for u in $(xe_min template-list other-config:os-vpx=true | sed -e 's/,/ /g'); do
86 uninstall_template "$u"
87 done
John Garbuttdaadf742012-04-27 18:28:28 +010088fi