| Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 1 | #!/bin/bash | 
 | 2 | # | 
 | 3 | # Copyright (c) 2011 Citrix Systems, Inc. | 
 | 4 | # Copyright 2011 OpenStack LLC. | 
| 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 |  | 
 | 20 | remove_data= | 
 | 21 | if [ "$1" = "--remove-data" ] | 
 | 22 | then | 
 | 23 |   remove_data=1 | 
 | 24 | fi | 
 | 25 |  | 
 | 26 | set -eu | 
 | 27 |  | 
 | 28 | xe_min() | 
 | 29 | { | 
 | 30 |   local cmd="$1" | 
 | 31 |   shift | 
 | 32 |   /opt/xensource/bin/xe "$cmd" --minimal "$@" | 
 | 33 | } | 
 | 34 |  | 
 | 35 | destroy_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 |  | 
 | 42 |   if [ "$type" = 'Disk' ] && [ "$dev" != 'xvda' ] && [ "$dev" != '0' ] | 
 | 43 |   then | 
 | 44 |     echo -n "Destroying data disk... " | 
 | 45 |     xe vdi-destroy uuid=$vdi_uuid | 
 | 46 |     echo "done." | 
 | 47 |   fi | 
 | 48 | } | 
 | 49 |  | 
 | 50 | uninstall() | 
 | 51 | { | 
 | 52 |   local vm_uuid="$1" | 
 | 53 |   local power_state=$(xe_min vm-list uuid=$vm_uuid params=power-state) | 
 | 54 |  | 
 | 55 |   if [ "$power_state" != "halted" ] | 
 | 56 |   then | 
 | 57 |     echo -n "Shutting down VM... " | 
 | 58 |     xe vm-shutdown vm=$vm_uuid force=true | 
 | 59 |     echo "done." | 
 | 60 |   fi | 
 | 61 |  | 
 | 62 |   if [ "$remove_data" = "1" ] | 
 | 63 |   then | 
 | 64 |     for v in $(xe_min vbd-list vm-uuid=$vm_uuid | sed -e 's/,/ /g') | 
 | 65 |     do | 
 | 66 |       destroy_vdi "$v" | 
 | 67 |     done | 
 | 68 |   fi | 
 | 69 |  | 
 | 70 |   echo -n "Deleting VM... " | 
 | 71 |   xe vm-uninstall vm=$vm_uuid force=true >/dev/null | 
 | 72 |   echo "done." | 
 | 73 | } | 
 | 74 |  | 
 | 75 | uninstall_template() | 
 | 76 | { | 
 | 77 |   local vm_uuid="$1" | 
 | 78 |  | 
 | 79 |   if [ "$remove_data" = "1" ] | 
 | 80 |   then | 
 | 81 |     for v in $(xe_min vbd-list vm-uuid=$vm_uuid | sed -e 's/,/ /g') | 
 | 82 |     do | 
 | 83 |       destroy_vdi "$v" | 
 | 84 |     done | 
 | 85 |   fi | 
 | 86 |  | 
 | 87 |   echo -n "Deleting template... " | 
 | 88 |   xe template-uninstall template-uuid=$vm_uuid force=true >/dev/null | 
 | 89 |   echo "done." | 
 | 90 | } | 
 | 91 |  | 
 | 92 |  | 
 | 93 | for u in $(xe_min vm-list other-config:os-vpx=true | sed -e 's/,/ /g') | 
 | 94 | do | 
 | 95 |   uninstall "$u" | 
 | 96 | done | 
 | 97 |  | 
 | 98 | for u in $(xe_min template-list other-config:os-vpx=true | sed -e 's/,/ /g') | 
 | 99 | do | 
 | 100 |   uninstall_template "$u" | 
 | 101 | done |