-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrclone.sh
executable file
·318 lines (282 loc) · 9 KB
/
rclone.sh
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
#!/usr/bin/env bash
[ -z "$DEBUG" ] || set -x
set -euo pipefail
# See bin/finalize to check predefined vars
ROOT="/home/vcap"
export APP_ROOT="${ROOT}/app"
export AUTH_ROOT="${ROOT}/auth"
export APP_CONFIG="${ROOT}/.config/rclone/rclone.conf"
export APP_VCAP_SERVICES="${APP_ROOT}/VCAP_SERVICES"
### Configuration env vars
# https://rclone.org/docs/#environment-variables
export RCLONE_RC_ADDR=":${PORT}"
export AUTO_START_ACTIONS="${AUTO_START_ACTIONS:-$APP_ROOT/post-start.sh}"
export BINDING_NAME="${BINDING_NAME:-}"
export GCS_LOCATION="${GCS_LOCATION:-europe-west4}"
export GCS_PROJECT_NUMBER="${GCS_PROJECT_NUMBER:-217463809547}"
export CLONE_SOURCE_SERVICE="${CLONE_SOURCE_SERVICE:-}"
export CLONE_SOURCE_BUCKET="${CLONE_SOURCE_BUCKET:-}"
export CLONE_DESTINATION_SERVICE="${CLONE_DESTINATION_SERVICE:-}"
export CLONE_DESTINATION_BUCKET="${CLONE_DESTINATION_BUCKET:-}"
export CLONE_TIMER="${CLONE_TIMER:-0}"
export CLONE_MODE="${CLONE_MODE:-copy}"
export AUTH_USER="${AUTH_USER:-admin}"
export AUTH_PASSWORD="${AUTH_PASSWORD:-}"
export RCLONE_RC_SERVE="${RCLONE_RC_SERVE:-true}"
export RCLONE_CONFIG="${RCLONE_CONFIG:-$APP_ROOT/rclone.conf}"
###
get_binding_service() {
local binding_name="${1}"
jq --arg b "${binding_name}" '[.[][] | select(.binding_name == $b)]' <<<"${VCAP_SERVICES}"
}
services_has_tag() {
local services="${1}"
local tag="${2}"
jq --arg t "${tag}" -e '.[].tags | contains([$t])' <<<"${services}" >/dev/null
}
get_s3_service() {
local name=${1:-"aws-s3"}
jq --arg n "${name}" '.[$n]' <<<"${VCAP_SERVICES}"
}
get_gcs_service() {
local name=${1:-"google-storage"}
jq --arg n "${name}" '.[$n]' <<<"${VCAP_SERVICES}"
}
set_s3_rclone_config() {
local config="${1}"
local services="${2}"
jq -r '.[] |
"["+ .name +"]
type = s3
provider = AWS
access_key_id = "+ .credentials.ACCESS_KEY_ID +"
secret_access_key = "+ .credentials.SECRET_ACCESS_KEY +"
region = "+ (.credentials.S3_API_URL | split(".")[0] | split("s3-")[1]) +"
location_constraint = "+ (.credentials.S3_API_URL | split(".")[0] | split("s3-")[1]) +"
acl = private
env_auth = false
"' <<<"${services}" >> ${config}
}
set_gcs_rclone_config() {
local config="${1}"
local services="${2}"
for s in $(jq -r '.[] | .name' <<<"${services}")
do
jq -r --arg n "${s}" '.[] | select(.name == $n) | .credentials.PrivateKeyData' <<<"${services}" | base64 -d > "${AUTH_ROOT}/${s}-auth.json"
done
jq --arg pa "${AUTH_ROOT}" --arg l "${GCS_LOCATION}" --arg pn "${GCS_PROJECT_NUMBER}" -r '.[] |
"["+ .name +"]
type = google cloud storage
client_id =
client_secret =
project_number = "+ $pn +"
service_account_file = "+ $pa +"/"+ .name +"-auth.json
storage_class = REGIONAL
location = "+ $l +"
"' <<<"${services}" >> ${config}
}
generate_rclone_config_from_vcap_services() {
local rconfig="${1}"
local binding_name="${2}"
local service=""
if [ -n "${binding_name}" ] && [ "${binding_name}" != "null" ]
then
service=$(get_binding_service "${binding_name}")
if [ -n "${service}" ] && [ "${service}" != "null" ]
then
if services_has_tag "${service}" "gcp"
then
set_gcs_rclone_config "${rconfig}" "${service}"
else
set_s3_rclone_config "${rconfig}" "${service}"
fi
else
return 1
fi
else
service=$(get_s3_service)
if [ -n "${service}" ] && [ "${service}" != "null" ]
then
set_s3_rclone_config "${rconfig}" "${service}"
fi
service=$(get_gcs_service)
if [ -n "${service}" ] && [ "${service}" != "null" ]
then
set_gcs_rclone_config "${rconfig}" "${service}"
fi
fi
return 0
}
get_bucket_from_service() {
local s="${1}"
local services="${2}"
local bucket=""
local rvalue=0
# first, try GCS style
bucket=$(jq -r -e --arg s "${s}" '.[][] | select(.name == $s) | .credentials.bucket_name' <<<"${services}")
rvalue=$?
# if empty, try S3
if [ -z "${bucket}" ] || [ ${rvalue} -ne 0 ]
then
bucket=$(jq -r -e --arg s "${s}" '.[][] | select(.name == $s) | .credentials.BUCKET_NAME' <<<"${services}")
rvalue=$?
fi
echo $bucket
return $rvalue
}
random_string() {
(
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-32} | head -n 1 || true
)
}
merge_vcap_services_from_file() {
local f="${1}"
local tempf
if [ -r "${f}" ]
then
if jq type <<<$(<"${f}") > /dev/null 2>&1
then
tempf=$(mktemp)
echo "${VCAP_SERVICES}" > ${tempf}
jq -s '{"google-storage": ((.[0]."google-storage" + .[1]."google-storage") // []), "aws-s3": ((.[0]."aws-s3" + .[1]."aws-s3") // []) }' "${f}" "${tempf}"
rm -f ${tempf}
else
return 1
fi
else
echo "${VCAP_SERVICES}"
fi
return 0
}
# exec process rclone
launch() {
local cmd="${1}"
shift
local pid
local rvalue
if [ -n "${CLONE_SOURCE_SERVICE}" ] && ! CLONE_SOURCE_BUCKET=${CLONE_SOURCE_BUCKET:-$(get_bucket_from_service "${CLONE_SOURCE_SERVICE}" "${VCAP_SERVICES}")}
then
echo ">> Error, cannot find bucket on service: ${CLONE_SOURCE_SERVICE}" >&2
return 1
fi
if [ -n "${CLONE_DESTINATION_SERVICE}" ] && ! CLONE_DESTINATION_BUCKET=${CLONE_DESTINATION_BUCKET:-$(get_bucket_from_service "${CLONE_DESTINATION_SERVICE}" "${VCAP_SERVICES}")}
then
echo ">> Error, cannot find bucket on service: ${CLONE_DESTINATION_SERVICE}" >&2
return 1
fi
if ! [[ "${CLONE_MODE}" =~ ^(copy|sync|move)$ ]]
then
echo ">> Error, service '${CLONE_MODE}' not valid!, only copy|sync|move is allowed!" >&2
return 1
fi
# run rclone server
(
echo ">> Launching pid=$$: $cmd $@"
{
exec $cmd $@
} 2>&1
) &
pid=$!
sleep 20
if ! ps -p ${pid} >/dev/null 2>&1
then
echo ">> Error launching: '$cmd $@'" >&2
return 1
fi
if [ -r "${AUTO_START_ACTIONS}" ]
then
[ -x "${AUTO_START_ACTIONS}" ] || chmod a+x "${AUTO_START_ACTIONS}"
(
{
echo ">> Launching post-start pid=$$: $@"
export CLONE_SOURCE_BUCKET="${CLONE_SOURCE_BUCKET}"
export CLONE_DESTINATION_BUCKET="${CLONE_DESTINATION_BUCKET}"
export RCLONE="$cmd"
sleep 1
${AUTO_START_ACTIONS}
}
) &
elif [ -n "${CLONE_SOURCE_BUCKET}" ] && [ -n "${CLONE_DESTINATION_BUCKET}" ]
then
(
{
while true
do
echo ">> Launching ${CLONE_MODE} job '${CLONE_SOURCE_SERVICE}:${CLONE_SOURCE_BUCKET}' -> '${CLONE_DESTINATION_SERVICE}:${CLONE_DESTINATION_BUCKET}', pid=$$"
sleep 1
$cmd -vv rc sync/${CLONE_MODE} srcFs="${CLONE_SOURCE_SERVICE}:${CLONE_SOURCE_BUCKET}" dstFs="${CLONE_DESTINATION_SERVICE}:${CLONE_DESTINATION_BUCKET}"
[ "${CLONE_TIMER}" == "0" ] && break || sleep ${CLONE_TIMER}
done
}
) &
fi
wait ${pid} 2>/dev/null
rvalue=$?
echo ">> Finish pid=${pid}: ${rvalue}"
return ${rvalue}
}
configure_rclone() {
if [ -r "${RCLONE_CONFIG}" ]
then
echo >> "${RCLONE_CONFIG}"
else
touch "${RCLONE_CONFIG}"
fi
mkdir -p $(dirname "${APP_CONFIG}")
ln -sf "${RCLONE_CONFIG}" "${APP_CONFIG}"
if ! VCAP_SERVICES=$(merge_vcap_services_from_file "${APP_VCAP_SERVICES}")
then
echo ">> Error, ${APP_VCAP_SERVICES} is not a valid json file!" >&2
return 1
else
echo "$VCAP_SERVICES" > "${APP_ROOT}/VCAP_SERVICES.final"
fi
if ! generate_rclone_config_from_vcap_services "${RCLONE_CONFIG}" "${BINDING_NAME}"
then
echo ">> Error, service '${BINDING_NAME}' not found!" >&2
return 1
fi
return 0
}
run_rclone() {
local cmd="rclone -v --config "${RCLONE_CONFIG}" --rc-addr ${RCLONE_RC_ADDR}"
mkdir -p "${AUTH_ROOT}"
if [ -z "${AUTH_PASSWORD}" ]
then
AUTH_PASSWORD=$(random_string 16)
echo "* Generated random password for user ${AUTH_USER} in ${AUTH_ROOT}/${AUTH_USER}.password"
echo "${AUTH_PASSWORD}" > "${AUTH_ROOT}/${AUTH_USER}.password"
fi
cmd="${cmd} --rc-user ${AUTH_USER} --rc-pass ${AUTH_PASSWORD}"
if configure_rclone
then
if [ "x${RCLONE_RC_SERVE}" == "xtrue" ]
then
launch "${cmd}" rcd --rc-web-gui --rc-serve $@
else
launch "${cmd}" rcd --rc-web-gui $@
fi
fi
}
# Program
if [ "$0" == "${BASH_SOURCE[0]}" ]
then
# run
if [ -n "${CF_INSTANCE_INDEX}" ]
then
if [ ${CF_INSTANCE_INDEX} -eq 0 ]
then
run_rclone $@
exit $?
else
echo "ERROR, no more than 1 instance allowed with this buildpack!" >&2
exit 1
fi
else
run_rclone $@
exit $?
fi
else
# Create a valid conf but not run
configure_rclone
fi