Statistics
| Branch: | Tag: | Revision:

one / src / tm_mad / ceph / delete @ 3cfbb666

History | View | Annotate | Download (5.8 KB)

1
#!/bin/bash
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2017, 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
# DELETE <host:remote_system_ds/disk.i|host:remote_system_ds/>
20
#   - host is the target host to deploy the VM
21
#   - remote_system_ds is the path for the system datastore in the host
22

    
23
DST=$1
24
VM_ID=$2
25
DS_ID=$3
26

    
27
#--------------------------------------------------------------------------------
28

    
29
if [ -z "${ONE_LOCATION}" ]; then
30
    TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
31
    LIB_LOCATION=/usr/lib/one
32
else
33
    TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
34
    LIB_LOCATION=$ONE_LOCATION/lib
35
fi
36

    
37
DRIVER_PATH=$(dirname $0)
38

    
39
source $TMCOMMON
40
source ${DRIVER_PATH}/../../datastore/ceph/ceph.conf
41
source ${DRIVER_PATH}/../../datastore/libfs.sh
42

    
43
CEPH_UTILS=${DRIVER_PATH}/../../datastore/ceph/ceph_utils.sh
44

    
45
#-------------------------------------------------------------------------------
46
# Process destination
47
#-------------------------------------------------------------------------------
48

    
49
DST_PATH=`arg_path $DST`
50
DST_HOST=`arg_host $DST`
51

    
52
XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"
53

    
54
#-------------------------------------------------------------------------------
55
# Delete and exit if directory
56
#-------------------------------------------------------------------------------
57

    
58
if [ `is_disk $DST_PATH` -eq 0 ]; then
59
    # Directory: delete checkpoint and directory
60

    
61
    unset i j XPATH_ELEMENTS
62

    
63
    while IFS= read -r -d '' element; do
64
        XPATH_ELEMENTS[i++]="$element"
65
    done < <(onedatastore show -x $DS_ID | $XPATH \
66
                        /DATASTORE/TEMPLATE/SOURCE \
67
                        /DATASTORE/TEMPLATE/CLONE \
68
                        /DATASTORE/TEMPLATE/CEPH_USER \
69
                        /DATASTORE/TEMPLATE/CEPH_KEY \
70
                        /DATASTORE/TEMPLATE/CEPH_CONF \
71
                        /DATASTORE/TEMPLATE/POOL_NAME)
72

    
73
    SRC="${XPATH_ELEMENTS[j++]}"
74
    CLONE="${XPATH_ELEMENTS[j++]}"
75
    CEPH_USER="${XPATH_ELEMENTS[j++]}"
76
    CEPH_KEY="${XPATH_ELEMENTS[j++]}"
77
    CEPH_CONF="${XPATH_ELEMENTS[j++]}"
78
    POOL_NAME="${XPATH_ELEMENTS[j++]:-$POOL_NAME}"
79

    
80
    if [ -n "$CEPH_USER" ]; then
81
        RBD="$RBD --id ${CEPH_USER}"
82
    fi
83

    
84
    if [ -n "$CEPH_KEY" ]; then
85
        RBD="$RBD --keyfile ${CEPH_KEY}"
86
    fi
87

    
88
    if [ -n "$CEPH_CONF" ]; then
89
        RBD="$RBD --conf ${CEPH_CONF}"
90
    fi
91

    
92
    RBD_SRC_CHECKPOINT="${POOL_NAME}/one-sys-${VM_ID}-checkpoint"
93

    
94
    ssh_exec_and_log "$DST_HOST" "$RBD rm $RBD_SRC_CHECKPOINT 2>/dev/null || exit 0" \
95
                 "Error deleting $RBD_SRC_CHECKPOINT in $DST_HOST"
96

    
97
    log "Deleting $DST_PATH"
98
    ssh_exec_and_log "$DST_HOST" "rm -rf $DST_PATH" "Error deleting $DST_PATH"
99
    exit 0
100
fi
101

    
102
#-------------------------------------------------------------------------------
103
# Get Image information
104
#-------------------------------------------------------------------------------
105

    
106
DISK_ID=$(echo "$DST_PATH" | $AWK -F. '{print $NF}')
107

    
108
unset i j XPATH_ELEMENTS
109

    
110
while IFS= read -r -d '' element; do
111
    XPATH_ELEMENTS[i++]="$element"
112
done < <(onevm show -x $VM_ID | $XPATH \
113
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/SOURCE \
114
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CLONE \
115
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CEPH_USER \
116
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CEPH_KEY \
117
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CEPH_CONF \
118
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/POOL_NAME)
119

    
120
SRC="${XPATH_ELEMENTS[j++]}"
121
CLONE="${XPATH_ELEMENTS[j++]}"
122
CEPH_USER="${XPATH_ELEMENTS[j++]}"
123
CEPH_KEY="${XPATH_ELEMENTS[j++]}"
124
CEPH_CONF="${XPATH_ELEMENTS[j++]}"
125
POOL_NAME="${XPATH_ELEMENTS[j++]:-$POOL_NAME}"
126

    
127
# No need to delete no cloned images
128
if [ "$CLONE" = "NO" ]; then
129
    exit 0
130
fi
131

    
132
if [ -n "$CEPH_USER" ]; then
133
    RBD="$RBD --id ${CEPH_USER}"
134
fi
135

    
136
if [ -n "$CEPH_KEY" ]; then
137
    RBD="$RBD --keyfile ${CEPH_KEY}"
138
fi
139

    
140
if [ -n "$CEPH_CONF" ]; then
141
    RBD="$RBD --conf ${CEPH_CONF}"
142
fi
143

    
144
if [ -n "$SRC" ]; then
145
    # cloned, so the name will be "<pool>/one-<imageid>-<vmid>-<diskid>"
146
    RBD_SRC="${SRC}-${VM_ID}-${DISK_ID}"
147
    RBD_SNAP="${VM_ID}-${DISK_ID}"
148
else
149
    # volatile
150
    RBD_SRC="${POOL_NAME}/one-sys-${VM_ID}-${DISK_ID}"
151
    RBD_SNAP=""
152
fi
153

    
154
#-------------------------------------------------------------------------------
155
# Delete the device
156
#-------------------------------------------------------------------------------
157

    
158
log "Deleting $DST_PATH"
159

    
160
DELETE_CMD=$(cat <<EOF
161
    RBD="${RBD}"
162

    
163
    if [ "\$(rbd_format $RBD_SRC)" = "2" ]; then
164
        rbd_rm_r \$(rbd_top_parent $RBD_SRC)
165

    
166
        if [ -n "$RBD_SNAP" ]; then
167
            rbd_rm_snap $SRC $RBD_SNAP
168
        fi
169
    else
170
        $RBD rm $RBD_SRC
171
    fi
172
EOF
173
)
174

    
175
ssh_exec_and_log_stdin "$DST_HOST" "$DELETE_CMD" "$CEPH_UTILS" \
176
                 "Error deleting $RBD_SRC in $DST_HOST"