Repository navigation
Expand file tree
/
Copy pathnc-sync
More file actions
117 lines (92 loc) · 2.89 KB
/
Copy pathnc-sync
File metadata and controls
117 lines (92 loc) · 2.89 KB
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
#!/usr/bin/env bash
set -euo pipefail
workdir="/home/jovyan/drive"
workspace="/workspace"
NC_SYNC_INTERVAL="${NC_SYNC_INTERVAL:-60s}"
NC_SYNC_BACKOFF="${NC_SYNC_BACKOFF:-300s}"
NC_SYNC_TIMEOUT="${NC_SYNC_TIMEOUT:-6h}"
rmdir --ignore-fail-on-non-empty /home/jovyan/work || true
mkdir -p "${workdir}"
if [[ ! -d "${workspace}" ]]; then
echo "[nc-sync] ERROR: ${workspace} does not exist"
exit 1
fi
cd "${workspace}"
if [[ -e drive && ! -L drive ]]; then
echo "[nc-sync] ERROR: ${workspace}/drive exists but is not a symlink"
exit 1
fi
if [[ ! -L drive ]]; then
ln -s "${workdir}" drive
fi
server="http://${NEXTCLOUD_HOST:-localhost:8081}"
json_file="${workdir}/.access_token.json"
# Exclude eodata from Nextcloud sync
exclude_file="${workdir}/.sync-exclude.lst"
cat > "${exclude_file}" <<'EOF'
eodata
eodata/*
EOF
refresh_token() {
local json token expires_at
json=""
if [[ -n "${JUPYTERHUB_API_TOKEN:-}" && -n "${JUPYTER_HOST:-}" ]]; then
json="$(curl -fsS \
--header "Authorization: token ${JUPYTERHUB_API_TOKEN}" \
"http://${JUPYTER_HOST}/services/refresh-token/tokens" || true)"
fi
if [[ -z "${json}" || "$(echo "${json}" | jq -r '.access_token // empty' 2>/dev/null)" == "" ]]; then
if [[ -z "${NEXTCLOUD_ACCESS_TOKEN:-}" ]]; then
echo "[nc-sync] ERROR: unable to refresh token and NEXTCLOUD_ACCESS_TOKEN is not set" >&2
return 1
fi
token="${NEXTCLOUD_ACCESS_TOKEN}"
expires_at="$(date -d "10 min" +%s)"
json="$(jq -n \
--arg token "${token}" \
--argjson expires "${expires_at}" \
'{access_token: $token, token_expires: $expires}')"
fi
echo "${json}" > "${json_file}"
jq -r '.access_token // empty' "${json_file}"
}
get_token() {
local now token expires_at
if [[ -f "${json_file}" ]] && jq empty "${json_file}" >/dev/null 2>&1; then
now="$(date +%s)"
token="$(jq -r '.access_token // empty' "${json_file}")"
expires_at="$(jq -r '.token_expires // 0' "${json_file}" | sed 's/\..*//')"
if [[ -z "${token}" || ! "${expires_at}" =~ ^[0-9]+$ || "${expires_at}" -lt "${now}" ]]; then
refresh_token
else
echo "${token}"
fi
else
refresh_token
fi
}
ncsync() {
while true; do
token="$(get_token || true)"
if [[ -n "${token}" && "${token}" != "null" && -n "${JUPYTERHUB_USER:-}" ]]; then
echo "[nc-sync] starting sync"
if timeout "${NC_SYNC_TIMEOUT}" nextcloudcmd -n -s \
--user "${JUPYTERHUB_USER}" \
--password "${token}" \
--exclude "${exclude_file}" \
--path / \
"${workdir}" \
"${server}"; then
echo "[nc-sync] sync completed"
sleep "${NC_SYNC_INTERVAL}"
else
echo "[nc-sync] WARNING: sync failed or timed out, backing off"
sleep "${NC_SYNC_BACKOFF}"
fi
else
echo "[nc-sync] missing token or user, skipping sync"
sleep "${NC_SYNC_BACKOFF}"
fi
done
}
ncsync &