-
Notifications
You must be signed in to change notification settings - Fork 4
/
a-delete
executable file
·322 lines (277 loc) · 8.16 KB
/
a-delete
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/bin/bash
#default user
user=($USER)
#read static variables
#inst_root=$(dirname $(readlink -f $0))
inst_root="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source $inst_root/a_env_conf
source $inst_root/allas-lib
#local variables
object_name=""
print_help=0
os_project_name="$OS_PROJECT_NAME"
input_def=""
mode="swift"
show_filelist=0
query=""
object_with_bucket=0
remove_bucket=0
force=0
defined_user="x"
# read customer defaults
if [[ -e $HOME/.a_tools_conf ]]; then
echo "Reading customer settings"
source $HOME/.a_tools_conf
fi
#Process command line
while [[ $# -ge 1 ]]
do
case "$1" in
'--object' | '-o' )
# query file
object_name="$2"
shift
shift
;;
'--project' | '-p' )
os_project_name="$2"
shift
shift
;;
'--bucket' | '-b' )
object_with_bucket=(1)
shift
;;
'--force' | '-f')
force=1
shift
;;
'--FORCE' | '-F')
force=2
shift
;;
'--user' | '-u')
user="$2"
defined_user="$2"
shift
shift
;;
'--rmb')
remove_bucket=1
shift
;;
'--s3cmd' )
mode="s3cmd"
shift
;;
'--lumi' | '-L' )
mode="lumi"
shift
;;
'--allas' | '-A' )
mode="allas"
shift
;;
'-h' | '--help' )
print_help=1
shift
;;
*)
if [[ $object_name != "" ]]; then
echo "unknown option: $1"
echo "Object name is: $object"
exit 1
fi
object_name="$1"
shift # No more switches
;;
esac
done
if [[ $object_name == "" ]]
then
print_help=1
fi
if [ $print_help -eq 1 ]; then
cat <<EOF
This tool is used to remove data that has been uploaded to Allas service using the a-put command.
The basic syntax of the command is:
a-delete object_name
Options:
-p, --project <project_ID> Delete objects form the buckets of the defined project instead of the currently configured project.
-b --bucket Object name includes bucket name and the command does not try to use the default bucket names.
-u, --user <username> Option allows you to assign a user account that is used to confirm the object ownership.
-f, --force Don't ask confirmation when deleting a file
--rmb Remove empty bucket.
-F, --FORCE In conjunction with --rmb, this option removes a non-empty bucket.
Related commands: a-put, a-get, a-find, a-info
EOF
exit
fi
#
if [[ $defined_user != "x" ]]; then
user=$defined_user
fi
#source $HOME/.allas_default
project_label=$(echo ${os_project_name} | sed -e s/"project_"/""/g)
#Check if connection works
if [[ $mode == "swift" ]]; then
storage_server="allas"
storage_name="Allas"
check_swift_connection
fi
if [[ $mode == "s3cmd" ]]; then
storage_server="s3allas"
storage_name="Allas"
fi
if [[ $mode == "lumi" ]]; then
storage_server="lumi-o"
storage_name="Lumi-o"
fi
#The method to remove a bucket
if [[ $remove_bucket -eq 1 ]]; then
if [[ $force -eq 2 ]]; then
if [[ $object_name != "" ]]; then
rclone delete ${storage_server}:"$object_name"
rclone rmdir ${storage_server}:"$object_name"
echo "$object_name deleted"
fi
fi
if [[ $force -eq 1 ]]; then
if [[ $object_name != "" ]]; then
rclone rmdir ${storage_server}:"$object_name"
#swift delete "$object_name"
fi
fi
if [[ $force -eq 0 ]]; then
echo ""
echo "Are you sure want to remove bucket:"
echo $object_name
echo "[y/n]"
read answer
if [[ $answer == "y" ]] || [[ $answer == "yes" ]]; then
rclone rmdir ${storage_server}:"$object_name"
#swift delete ${object_name}
echo "${object_name} was deleted."
else
echo "${object_name} was not deleted."
fi
fi
exit 1
fi
#the method to remove object
#check that object exists
check_os=$(rclone ls ${storage_server}:"$object_name" 2> /dev/null | wc -l)
if [ $check_os -ne 1 ]; then
echo "Object name: $object_name not found in $storage_server."
echo ""
#objects=($(a-find -a | grep "$object_name"))
#if [ ${#objects[@]} -eq 1 ]; then
# echo "Did you mean this object:"
# echo " $objects"
#fi
#if [ ${#objects[@]} -gt 1 ]; then
# echo "Did you mean some of these objects:"
# for on in ${objects[@]}
# do
# echo " $on"
# done
#fi
if [ ${#objects[@]} -eq 0 ]; then
echo "Try running command:"
echo " a-find $object_name"
fi
exit 1
else
object_with_bucket=(1)
fi
# check if object name contains bucket (if not defined with bucket)
if [[ $object_with_bucket == 0 ]]; then
if [ $(echo $object_name | grep -c "${user}-${project_label}-MISC" ) -eq 1 ]
then
object_with_bucket=(1)
bucket=("${user}-${project_label}-MISC")
fi
if [ $(echo $object_name | grep -c "${user}-${project_label}-HOME" ) -eq 1 ]
then
object_with_bucket=(1)
bucket=("${user}-${project_label}-HOME")
fi
if [ $(echo $object_name | grep -c "${user}-${project_label}-SCRATCH" ) -eq 1 ]
then
object_with_bucket=(1)
bucket=("${user}-${project_label}-HOME")
fi
fi
# check all buckets if the bucket is not defined
if [ $object_with_bucket == 0 ]
then
buckets=("${user}-${project_label}-MISC ${user}-${project_label}-HOME ${user}-${project_label}-SCRATCH")
num_buckets=(0)
# go through the buckets
for bn in $buckets
do
bucket_found=$(rclone ls ${storage_server}:$bn/$object_name 2> /dev/null | wc -l)
if [ $bucket_found -eq 1 ]; then
echo "Bucket: $bn contains object:$object_name"
(( num_buckets = num_buckets + 1 ))
bucket=("$bn")
fi
done
if [[ $num_buckets -eq 0 ]]; then
echo ""
echo "Could not find object: $object_name "
exit 1
fi
if [[ $num_buckets -gt 1 ]]; then
echo ""
echo "Object $object_name was found in several buckets!"
echo "Please include bucket name in the object name"
exit 1
fi
#add bucket to the object name
object_name=("$bucket/$object_name")
else
#check if object exists
bucket_found=$(rclone ls ${storage_server}:/"$object_name" 2> /dev/null | wc -l)
if [ $bucket_found -ne 1 ]; then
echo ""
echo "Object $object_name was not found"
echo ""
echo "You can list all the available objects with command:"
echo " a-find"
fi
fi
ameta_found=$(rclone ls ${storage_server}:/"${object_name}_ameta" 2> /dev/null | wc -l)
if [ $ameta_found -eq 1 ]; then
object_owner=$(rclone cat ${storage_server}:/"${object_name}_ameta" |grep "^user:" | awk '{print $2}')
if [[ $user != $object_owner ]]; then
echo ""
echo "Your username ($user) is not matching to the username assigned to the object ($object_owner)"
echo "You can use option --user to enforce the deletion tool to use a specific user name"
echo "${object_name} was not deleted"
exit 1
fi
fi
#remove object
if [[ $force -eq 1 ]]; then
rclone deletefile ${storage_server}:/"${object_name}"
if [ $ameta_found -eq 1 ]; then
rclone deletefile ${storage_server}:/"${object_name}_ameta"
fi
else
echo ""
echo "Are you sure want to remove object:"
echo $object_name
echo "[y/n]"
read answer
if [[ $answer == "y" ]] || [[ $answer == "yes" ]]; then
rclone deletefile ${storage_server}:/"${object_name}"
if [ $ameta_found -eq 1 ]; then
rclone deletefile ${storage_server}:/"${object_name}_ameta"
fi
echo "${object_name} was deleted."
else
echo "${object_name} was not deleted."
fi
fi
exit