1
|
#!/bin/bash
|
2
|
|
3
|
# -------------------------------------------------------------------------- #
|
4
|
# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems #
|
5
|
# #
|
6
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
|
7
|
# not use this file except in compliance with the License. You may obtain #
|
8
|
# a copy of the License at #
|
9
|
# #
|
10
|
# http://www.apache.org/licenses/LICENSE-2.0 #
|
11
|
# #
|
12
|
# Unless required by applicable law or agreed to in writing, software #
|
13
|
# distributed under the License is distributed on an "AS IS" BASIS, #
|
14
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
|
15
|
# See the License for the specific language governing permissions and #
|
16
|
# limitations under the License. #
|
17
|
#--------------------------------------------------------------------------- #
|
18
|
|
19
|
###############################################################################
|
20
|
# This script is used to remove a VM image (SRC) from the image repository
|
21
|
###############################################################################
|
22
|
|
23
|
# ------------ Set up the environment to source common tools ------------
|
24
|
|
25
|
# Log parameters to avoid duplicate message merging by syslog
|
26
|
logger -t one-ds-delete "Start datastore RM script $1 $2"
|
27
|
|
28
|
if [ -z "${ONE_LOCATION}" ]; then
|
29
|
LIB_LOCATION=/usr/lib/one
|
30
|
else
|
31
|
LIB_LOCATION=$ONE_LOCATION/lib
|
32
|
fi
|
33
|
|
34
|
. $LIB_LOCATION/sh/scripts_common.sh
|
35
|
|
36
|
DRIVER_PATH=$(dirname $0)
|
37
|
source ${DRIVER_PATH}/../libfs.sh
|
38
|
|
39
|
# -------- Get rm and datastore arguments from OpenNebula core ------------
|
40
|
|
41
|
DRV_ACTION=$1
|
42
|
ID=$2
|
43
|
|
44
|
XPATH="${DRIVER_PATH}/../xpath.rb -b $DRV_ACTION"
|
45
|
|
46
|
unset i XPATH_ELEMENTS
|
47
|
|
48
|
while IFS= read -r -d '' element; do
|
49
|
XPATH_ELEMENTS[i++]="$element"
|
50
|
done < <($XPATH /DS_DRIVER_ACTION_DATA/IMAGE/SOURCE \
|
51
|
/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/BRIDGE_LIST \
|
52
|
/DS_DRIVER_ACTION_DATA/DATASTORE/BASE_PATH)
|
53
|
|
54
|
unset i
|
55
|
|
56
|
SRC="${XPATH_ELEMENTS[i++]}"
|
57
|
BRIDGE_LIST="${XPATH_ELEMENTS[i++]}"
|
58
|
BASE_PATH="${XPATH_ELEMENTS[i++]}"
|
59
|
|
60
|
# ------------ Remove the image from the repository ------------
|
61
|
logger -t one-ds-delete "ID='${ID}', SRC='${SRC}', BASE_PATH='${BASE_PATH}', BRIDGE_LIST='${BRIDGE_LIST}', REMOTE_RM_CMD='${REMOTE_RM_CMD}'"
|
62
|
|
63
|
if [ -n "$BRIDGE_LIST" ]; then
|
64
|
DST_HOST=`get_destination_host $ID`
|
65
|
logger -t one-ds-delete "SSH EXEC: $DST_HOST [ -f $SRC ] && rm -rf $SRC $SRC.snap"
|
66
|
|
67
|
ssh_exec_and_log "$DST_HOST" "[ -f $SRC ] && rm -rf $SRC $SRC.snap" \
|
68
|
"Error deleting $SRC in $DST_HOST"
|
69
|
else
|
70
|
BASENAME_SRC=`basename "${SRC##$REMOTE_RM_CMD}"`
|
71
|
logger -t one-ds-delete "BASENAME_SRC='${BASENAME_SRC}'"
|
72
|
|
73
|
if [ -f "$SRC" -a `dirname "$SRC"` = "$BASE_PATH" -a -n "$BASENAME_SRC" ]
|
74
|
then
|
75
|
log "Removing $SRC from the image repository"
|
76
|
logger -t one-ds-delete "rm -rf $SRC $SRC.snap"
|
77
|
exec_and_log "rm -rf $SRC $SRC.snap" \
|
78
|
"Error deleting $SRC"
|
79
|
else
|
80
|
logger -t one-ds-delete "Bad formed or unavailable Image source: ${SRC}"
|
81
|
log_error "Bad formed or unavailable Image source: ${SRC}"
|
82
|
exit 1
|
83
|
fi
|
84
|
fi
|