-
Notifications
You must be signed in to change notification settings - Fork 4
/
allas-mount
executable file
·290 lines (237 loc) · 7.59 KB
/
allas-mount
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
#!/bin/bash
#default user
user=($USER)
#read static variables
inst_root="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
#inst_root=$(dirname $(readlink -f $0))
source $inst_root/a_env_conf
# local variables
bucket_name=("") #object to retrieve
mount_point=("") #where to store the results
read_or_write="--read-only"
print_help=(0)
task="mount"
os_project_name=("none")
mode=("swift")
query=("")
object_with_bucket=(0)
target_directory=(".")
#Process command line
while [[ $# -ge 1 ]]
do
case "$1" in
'--bucket' | '-b')
# query file
bucket_name=($2)
shift
shift
;;
'--project' | '-p' )
os_project_name=($2)
shift
shift
;;
'--readonly' | '-r' )
os_project_name=($2)
shift
shift
;;
'--mountpoint' | '-m' )
mount_point=$(basename $2)
if [[ $(dirname $2) != "." ]]; then
target_dir=$(dirname $2)
mount_point=(1)
fi
new_target_file=(1)
shift
shift
;;
'--umount' | '-u' )
task="unmount"
if [[ -e "$2" ]]; then
mount_point=$(basename $2)
else
echo "Mount point: $2 not found"
exit 1
fi
shift
shift
;;
'--list' | '-l' )
echo "Active rclone mount commands for ${storage_server}:"
ps -edalf | grep "rclone mount ${storage_server}:" | grep -v -w grep
exit 0
;;
'--write' | '-w' )
read_or_write=""
shift
;;
'-h' | '--help' )
print_help=(1)
shift
;;
*)
if [[ $bucket_name != "" ]]; then
echo "unknown option: $1"
echo "Object name is: $object"
exit 1
fi
bucket_name=("$1")
shift # No more switches
;;
esac
done
if [ $print_help -eq 1 ]; then
cat <<EOF
This tool mounts a bucket in Allas to be used as a read-only disk areas.
Syntax of the command is:
allas-mount <bucket_name>
By default the bucket is mounted to a directory named after the bucket.
You can use optional mount points with option -m (--mountpoint)
allas-mount <bucket_name> -m <dir_name>
To unmount a bucket use option -u or --umount:
allas-mount -u <dir_name>
Option -l (--list) lists the currently active rclone mount commands, launched by allas-mount.
allas-mount -l
By default buckets are mounted as read-only directories. With option -w (--write) write permission is
added to the mounted bucket. Writing data to allas this way can cause problems in the cases where the
same object is modified by several processes at the same time.
allas-mount <bucket_name> -w
EOF
exit 0
fi
#Check if connection works
if [[ $mode == "swift" ]]
then
#here we check that rclone is available
if [[ $(which rclone 2> /dev/null | wc -l ) -ne 1 ]];then
echo ""
echo "rclone is not available!"
echo "Please install rclone."
exit 1
fi
#connection test
test=$(rclone about ${storage_server}: 2> /dev/null | wc -l)
#test=$(swift stat 2> /dev/null | grep -c "Account:")
if [[ $test -lt 1 ]]
then
source $inst_root/allas_conf -f
test=$(rclone about ${storage_server}: 2> /dev/null | wc -l)
if [[ $test -lt 1 ]]
then
echo "No connection to Allas!"
echo "Please try setting up the connection again."
echo "by running command:"
echo ""
echo " source $allas_conf_path"
exit 1
fi
fi
fi
echo "debug 1"
#Unmounting
if [[ $task == "unmount" ]]; then
if [[ $(echo $OSTYPE | grep -i -c linux) -eq 1 ]]; then
echo "Un-mounting $mount_point"
rclone_pid=$(ps -edalf | grep "rclone mount ${storage_server}:" | grep -v -w grep | grep -w "$mount_point" | awk '{print $4}')
else
rclone_pid=$(ps -eda | grep "rclone mount ${storage_server}:" | grep -v -w grep | grep -w "$mount_point" | awk '{print $1}')
fi
kill $rclone_pid
exit 0
fi
#Configure rclone
if [ ! -e $HOME/.config/rclone ]
then
mkdir -p $HOME/.config/rclone
fi
if [ -e $HOME/.config/rclone/rclone.conf ]
then
rc_check=$(grep -c $storage_server $HOME/.config/rclone/rclone.conf)
if [ $rc_check -lt 1 ]
then
echo '['"$storage_service"']' >> $HOME/.config/rclone/rclone.conf
echo 'type = swift' >> $HOME/.config/rclone/rclone.conf
echo 'env_auth = true' >> $HOME/.config/rclone/rclone.conf
fi
else
echo '['"$storage_service"']' >> $HOME/.config/rclone/rclone.conf
echo 'type = swift' >> $HOME/.config/rclone/rclone.conf
echo 'env_auth = true' >> $HOME/.config/rclone/rclone.conf
fi
#Assign project to be used if not defined
if [[ $os_project_name == "none" ]]
then
os_project_name=$OS_PROJECT_NAME
fi
if [[ $os_project_name != $OS_PROJECT_NAME ]]
then
echo "Switching allas configuration to use project $os_project_name"
source $allas_conf_path -user $user $os_project_name -keeppasswd
export OS_PROJECT_NAME=$os_project_name
fi
#source /appl/opt/allas_conf
#input=("$1")
if [[ $bucket_name == "" ]]
then
#echo "rclone lsf ${storage_server}: | grep -v segments | tr -d '/' "
#rclone lsf ${storage_server}: | grep -v segments | tr -d "/" | tr "\n" " "
all_buckets=($(rclone lsf ${storage_server}: | grep -v segments | tr -d "/" | tr "\n" " "))
echo "Please choose a bucket by giving an item number form the list above: "
case ${#all_buckets[@]} in
0)
echo "Error: No buckets found!" >&2
exit 1
;;
*)
select bucket_name in "${all_buckets[@]}" "abort allas-mount";
do
[[ -n "$bucket_name" ]] && break
done
if [[ "$bucket_name" == "abort allas-mount" ]]; then
exit 0
fi
;;
esac
fi
if [[ $mount_point == "" ]]; then
mount_point=$bucket_name
fi
#check that bucket exists
check_bucket=$(rclone size ${storage_server}:$bucket_name 2>&1 | wc -l)
if [[ $check_bucket -eq 0 ]]; then
echo "Bucket name: $bucket_name not found in $storage_server."
echo ""
exit 1
fi
#Test if the target file already exists
#echo "Target: ${target_dir}/${target_name}"
if [[ -e ${mount_point} ]]; then
if [[ $( find $mount_point | wc -l ) -gt 1 ]];then
echo "Mount point: $mount_point is not an empty directory"
exit 1
fi
else
mkdir $mount_point
fi
if [[ $read_or_write == "" ]]; then
echo ""
echo "You are mounting bucket $bucket_name with write access included."
echo "The default read-only mounting is recommended."
echo "Note that if objects in this bucket are modified by other processes"
echo "while the bucket is mounted with write access, you may end up to problems."
echo ""
echo "Confirm that you understand this:[yes/no]"
read ans
if [[ $ans == "yes" || $ans == "y" ]]; then
echo "Mounting."
else
echo "Bucket was not mounted"
exit 0
fi
fi
#mount
#rclone mount ${storage_server}:$bucket_name $mount_point --read-only --daemon --daemon-timeout 21600s
run-rclone-mount.sh $storage_server $bucket_name $mount_point $read_or_write &
echo "Allas bucket: $bucket_name is mounted to $mount_point"
exit