Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (c) 2011 Citrix Systems, Inc. |
ZhiQiang Fan | 7d56215 | 2013-09-20 02:20:35 +0800 | [diff] [blame] | 4 | # Copyright 2011 OpenStack Foundation |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 5 | # 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 Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 20 | set -ex |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 21 | |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 22 | # By default, don't remove the templates |
| 23 | REMOVE_TEMPLATES=${REMOVE_TEMPLATES:-"false"} |
| 24 | if [ "$1" = "--remove-templates" ]; then |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 25 | REMOVE_TEMPLATES=true |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 26 | fi |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 27 | |
| 28 | xe_min() |
| 29 | { |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 30 | local cmd="$1" |
| 31 | shift |
| 32 | xe "$cmd" --minimal "$@" |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | destroy_vdi() |
| 36 | { |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 37 | local vbd_uuid="$1" |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 38 | 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 Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 44 | |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 45 | if [ "$type" == 'Disk' ] && [ "$dev" != 'xvda' ] && [ "$dev" != '0' ]; then |
| 46 | xe vdi-destroy uuid=$vdi_uuid |
| 47 | fi |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | uninstall() |
| 51 | { |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 52 | local vm_uuid="$1" |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 53 | local power_state |
| 54 | power_state=$(xe_min vm-list uuid=$vm_uuid params=power-state) |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 55 | |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 56 | if [ "$power_state" != "halted" ]; then |
| 57 | xe vm-shutdown vm=$vm_uuid force=true |
| 58 | fi |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 59 | |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 60 | for v in $(xe_min vbd-list vm-uuid=$vm_uuid | sed -e 's/,/ /g'); do |
| 61 | destroy_vdi "$v" |
| 62 | done |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 63 | |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 64 | xe vm-uninstall vm=$vm_uuid force=true >/dev/null |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | uninstall_template() |
| 68 | { |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 69 | local vm_uuid="$1" |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 70 | |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 71 | for v in $(xe_min vbd-list vm-uuid=$vm_uuid | sed -e 's/,/ /g'); do |
| 72 | destroy_vdi "$v" |
| 73 | done |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 74 | |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 75 | xe template-uninstall template-uuid=$vm_uuid force=true >/dev/null |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 76 | } |
| 77 | |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 78 | # remove the VMs and their disks |
| 79 | for u in $(xe_min vm-list other-config:os-vpx=true | sed -e 's/,/ /g'); do |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 80 | uninstall "$u" |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 81 | done |
| 82 | |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 83 | # remove the templates |
| 84 | if [ "$REMOVE_TEMPLATES" == "true" ]; then |
Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 85 | for u in $(xe_min template-list other-config:os-vpx=true | sed -e 's/,/ /g'); do |
| 86 | uninstall_template "$u" |
| 87 | done |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 88 | fi |