| 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" | 
 | 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 Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 41 |  | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 42 |     if [ "$type" == 'Disk' ] && [ "$dev" != 'xvda' ] && [ "$dev" != '0' ]; then | 
 | 43 |         xe vdi-destroy uuid=$vdi_uuid | 
 | 44 |     fi | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 45 | } | 
 | 46 |  | 
 | 47 | uninstall() | 
 | 48 | { | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 49 |     local vm_uuid="$1" | 
 | 50 |     local power_state=$(xe_min vm-list uuid=$vm_uuid params=power-state) | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 51 |  | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 52 |     if [ "$power_state" != "halted" ]; then | 
 | 53 |         xe vm-shutdown vm=$vm_uuid force=true | 
 | 54 |     fi | 
| 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 |     for v in $(xe_min vbd-list vm-uuid=$vm_uuid | sed -e 's/,/ /g'); do | 
 | 57 |         destroy_vdi "$v" | 
 | 58 |     done | 
| 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 |     xe vm-uninstall vm=$vm_uuid force=true >/dev/null | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 61 | } | 
 | 62 |  | 
 | 63 | uninstall_template() | 
 | 64 | { | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 65 |     local vm_uuid="$1" | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 66 |  | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 67 |     for v in $(xe_min vbd-list vm-uuid=$vm_uuid | sed -e 's/,/ /g'); do | 
 | 68 |         destroy_vdi "$v" | 
 | 69 |     done | 
| 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 |     xe template-uninstall template-uuid=$vm_uuid force=true >/dev/null | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 72 | } | 
 | 73 |  | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 74 | # remove the VMs and their disks | 
 | 75 | 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] | 76 |     uninstall "$u" | 
| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 77 | done | 
 | 78 |  | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 79 | # remove the templates | 
 | 80 | if [ "$REMOVE_TEMPLATES" == "true" ]; then | 
| Sean Dague | 0b865a5 | 2013-10-22 11:37:35 -0400 | [diff] [blame] | 81 |     for u in $(xe_min template-list other-config:os-vpx=true | sed -e 's/,/ /g'); do | 
 | 82 |         uninstall_template "$u" | 
 | 83 |     done | 
| John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 84 | fi |