-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathuse-oci.sh
executable file
·80 lines (65 loc) · 2.08 KB
/
use-oci.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
#!/bin/bash
# Restores a trusted artifact, content of the destination will be removed.
#
#
# Positional parametes are artifact pairs. These are strings. Each contains two parts separated by
# an equal sign (=). The left portion refers to the uri of where the artifact can be fetch from.
# This must be prefixed with "oci:" to indicate that it is indeed meant to be processed by this
# script. The right side specifies where the artifact will be extracted to. For example,
# oci:registry/org/repo:latest@sha256:123=/home/user/Downloads/artifact means the artifact will be
# fetched from registry/org/repo and extract to the /home/user/Downloads/artifact directory.
#
set -o errexit
set -o nounset
set -o pipefail
tar_opts=-zxpf
if [[ ! -z "${DEBUG:-}" ]]; then
tar_opts=-zxvpf
set -o xtrace
fi
# contains name=path artifact pairs
artifact_pairs=()
while [[ $# -gt 0 ]]; do
case $1 in
-*)
echo "Unknown option $1"
exit 1
;;
*)
artifact_pairs+=("$1")
shift
;;
esac
done
# read in any oras options
source oras_opts.sh
for artifact_pair in "${artifact_pairs[@]}"; do
uri="${artifact_pair/=*}"
destination="$(realpath "${artifact_pair/*=}")"
if [ -z "${uri}" ]; then
echo WARN: artifact URI not provided, "(given: ${artifact_pair})"
continue
fi
if [ -z "${destination}" ]; then
echo WARN: destination not provided, "(given: ${artifact_pair})"
continue
fi
if [ "${destination}" == "/" ]; then
echo Not a valid destination: "${destination}", resolves to /
exit 1
fi
if [ -f "${destination}/.skip-trusted-artifacts" ]; then
echo WARN: found skip file in "${destination}"
continue
fi
mkdir -p "${destination}"
type="${uri/:*}"
if [ "${type}" != "oci" ]; then
echo Unsupported archive type: "${type}"
exit 1
fi
name="${uri#*:}"
oras blob fetch "${oras_opts[@]}" --registry-config <(select-oci-auth.sh ${name}) \
"${name}" --output - | tar -C "${destination}" "${tar_opts}" -
echo "Restored artifact ${name} to ${destination}"
done