-
Notifications
You must be signed in to change notification settings - Fork 3
/
get-replicas
executable file
·154 lines (119 loc) · 2.78 KB
/
get-replicas
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
#!/usr/bin/env bash
show_help() {
cat << EOF
$EXEC_NAME version $VERSION
Usage:
$EXEC_NAME [options] <data object>
retrieves information about the replicas of a data object
Parameters:
<data object> the path to the data object
Options:
-R, --resc RESC only retrieve replicas from the root resource RESC
-h, --help show help and exit
-v, --version show version and exit
Summary:
For each replica of the data object, size, resource hierarchy, the host name of
the resource server serving the storage resource, and the path to the file. It
writes this information to standard out with the following format with one line
per replica.
<size> <resource hierarchy> <resource server> <file>
Prerequisites:
1) The data object may not have a carriage return in its path.
2) The user must be initialized with iRODS as an admin user.
EOF
}
set -o errexit
EXEC_NAME=$(basename "$0")
readonly EXEC_NAME
readonly VERSION=1
show_version() {
printf '%s\n' "$VERSION"
}
show_help_and_error_out() {
show_help >&2
exit 1
}
if ! opts=$(getopt --name "$EXEC_NAME" --options hR:v --longoptions help,resc:,version -- "$@")
then
printf '\n' >&2
show_help_and_error_out
fi
eval set -- "$opts"
declare resc
while true
do
case "$1" in
-h|--help)
show_help
exit 0
;;
-R|--resc)
resc="$2"
shift 2
;;
-v|--version)
show_version
exit 0
;;
--)
shift
break
;;
*)
show_help_and_error_out
;;
esac
done
if [ "$#" -lt 1 ]
then
show_help_and_error_out
fi
readonly OBJ_PATH="$*"
ask_irods() {
local resc="$1"
local collPath
collPath=$(dirname "$OBJ_PATH")
local objName
objName=$(basename "$OBJ_PATH")
local resp
resp="$(\
iquest --no-page \
'%s %s %s %s' \
"select DATA_ID, DATA_SIZE, DATA_RESC_HIER, DATA_PATH
where COLL_NAME = '$collPath' and DATA_NAME = '$objName'" )"
local ec="$?"
if [ "$ec" -ne 0 ] || [ "$resp" = 'CAT_NO_ROWS_FOUND: Nothing was found matching your query' ]
then
printf 'invalid data object: %s\n' "$OBJ_PATH" >&2
else
cut --delimiter ' ' --fields 2- <<< "$resp" | filter_resc "$resc"
fi
}
filter_resc() {
local resc="$1"
if [[ -z "$resc" ]]
then
cat
else
local entry
while IFS= read -r entry
do
local size rescHier
read -r size rescHier _ <<< "$entry"
if [[ "$rescHier" =~ ^$resc ]]
then
echo "$entry"
fi
done
fi
}
while read -r size rescHier filePath
do
storeResc="${rescHier##*;}"
if ! storeHost=$(iquest '%s' "SELECT RESC_LOC WHERE RESC_NAME = '$storeResc'")
then
printf 'Failed to retrieve location for resource %s\n' "$storeResc" >&2
else
printf '%s %s %s %s\n' "$size" "$rescHier" "$storeHost" "$filePath"
fi
done < <(ask_irods "${resc-}")