Repository navigation
Expand file tree
/
Copy pathentrypoint.sh
More file actions
101 lines (88 loc) · 3.31 KB
/
Copy pathentrypoint.sh
File metadata and controls
101 lines (88 loc) · 3.31 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
#!/usr/bin/env sh
set -eu
# Accept the existing Docker environment variables as well as the parameter
# names used in app.yml. Pennsieve runtime versions have used both uppercase
# environment variables and PARAM_<NAME> for declared parameters.
ACTION="${DANDI_ACTION:-${ACTION:-${PARAM_ACTION:-${action:-}}}}"
OUTPUT_DIR="${OUTPUT_DIR:-${PARAM_OUTPUT_DIR:-${output_dir:-/output}}}"
INPUT_DIR="${INPUT_DIR:-${PARAM_INPUT_DIR:-${input_dir:-/input}}}"
UPLOAD_WORK_DIR="${UPLOAD_WORK_DIR:-${PARAM_UPLOAD_WORK_DIR:-${upload_work_dir:-/work}}}"
DATASET_ID="${DATASET_ID:-${DANDISET_ID:-${PARAM_DATASET_ID:-${dataset_id:-}}}}"
DANDI_INSTANCE="${DANDI_INSTANCE:-${PARAM_DANDI_INSTANCE:-${dandi_instance:-ember-dandi}}}"
DANDI_URL="${DANDI_URL:-${PARAM_DANDI_URL:-${dandi_url:-}}}"
DANDI_API_KEY="${DANDI_API_KEY:-${PARAM_DANDI_API_KEY:-${dandi_api_key:-}}}"
DANDISET_URL="${DANDISET_URL:-${PARAM_DANDISET_URL:-${dandiset_url:-}}}"
usage() {
cat >&2 <<'EOF'
Set one action:
DANDI_ACTION=download
DANDI_ACTION=upload
Download:
DANDI_URL=<DANDI URL or identifier>
OUTPUT_DIR=<download destination directory, default: /output>
Upload:
DATASET_ID=<DANDI/EMBER Dandiset identifier>
INPUT_DIR=<source folder to organize, default: /input>
DANDI_API_KEY=<token>
Optional:
DANDISET_URL=https://dandi.emberarchive.org/dandiset/<DATASET_ID>/draft
UPLOAD_WORK_DIR=/work
DANDI_INSTANCE=ember-dandi
EOF
}
die() {
echo "ERROR: $*" >&2
usage
exit 64
}
api_key_env_name() {
printf '%s_API_KEY\n' "$1" | tr '[:lower:]' '[:upper:]' | tr '-' '_'
}
# DANDI CLI reads credentials from an instance-specific environment variable
# (for example, EMBER_DANDI_API_KEY). Make an optional token available for
# authenticated downloads as well as uploads; only uploads require one.
if [ -n "$DANDI_API_KEY" ]; then
instance_api_key_env="$(api_key_env_name "$DANDI_INSTANCE")"
export "$instance_api_key_env=$DANDI_API_KEY"
fi
case "$ACTION" in
download)
if [ -z "${DANDI_URL:-}" ]; then
die "DANDI_URL is required for downloads"
fi
mkdir -p "$OUTPUT_DIR"
cd "$OUTPUT_DIR"
exec dandi download "$DANDI_URL"
;;
upload)
if [ -z "$DATASET_ID" ]; then
die "DATASET_ID or DANDISET_ID is required for uploads"
fi
if [ -z "${DANDI_API_KEY:-}" ]; then
die "DANDI_API_KEY is required for uploads"
fi
if [ ! -d "$INPUT_DIR" ]; then
die "INPUT_DIR does not exist or is not a directory: $INPUT_DIR"
fi
dandiset_url="${DANDISET_URL:-https://dandi.emberarchive.org/dandiset/$DATASET_ID/draft}"
mkdir -p "$UPLOAD_WORK_DIR"
cd "$UPLOAD_WORK_DIR"
dandi download "$dandiset_url"
if [ -d "$DATASET_ID" ]; then
cd "$DATASET_ID"
else
dandiset_yaml="$(find . -maxdepth 3 -name dandiset.yaml -type f | head -n 1 || true)"
if [ -z "$dandiset_yaml" ]; then
die "could not find downloaded dandiset.yaml for DATASET_ID=$DATASET_ID"
fi
cd "$(dirname "$dandiset_yaml")"
fi
dandi organize "$INPUT_DIR" -f dry
dandi organize "$INPUT_DIR"
dandi validate .
exec dandi upload -i "$DANDI_INSTANCE"
;;
*)
die "DANDI_ACTION must be download or upload"
;;
esac