-
Notifications
You must be signed in to change notification settings - Fork 43
/
get
executable file
·163 lines (143 loc) · 4.56 KB
/
get
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
#!/usr/bin/env bash
set -euo pipefail
BASE_URL=https://storage.googleapis.com/cri-o/conmon-rs
ARCH_AMD64=amd64
ARCH_ARM64=arm64
ARCH_PPC64LE=ppc64le
ARCH_S390X=s390x
ARCH=
COMMIT=
TAG=
NO_EXEC=
OUTPUT=conmonrs
usage() {
printf "Usage: %s [ -t SHA ] [-l TAG ] [-a ARCH] [ -h ]\n\n" "$(basename "$0")"
echo "Possible arguments:"
printf " -o\tOutput path for the downloaded binary (defaults to './conmonrs')\n"
printf " -t\tFull length SHA to be used (defaults to the latest available main)\n"
printf " -l\tTag to be used\n"
printf " -a\tArchitecture to retrieve (defaults to the local system)\n"
printf " -n\tDo not print the version after install, means don't execute the binary\n"
printf " -h\tShow this help message\n"
}
parse_args() {
echo "Welcome to the conmon-rs install script!"
while getopts 'a:l:no:t:h' OPTION; do
case "$OPTION" in
a)
ARCH="$OPTARG"
echo "Using architecture: $ARCH"
;;
l)
TAG="$OPTARG"
echo "Using tag: $TAG"
;;
n)
NO_EXEC=1
;;
o)
OUTPUT="$OPTARG"
echo "Using output path: $OUTPUT"
;;
t)
COMMIT="$OPTARG"
echo "Using commit: $COMMIT"
;;
h)
usage
exit 0
;;
?)
usage
exit 1
;;
esac
done
if [[ $ARCH == "" ]]; then
LOCAL_ARCH=$(uname -m)
if [[ "$LOCAL_ARCH" == x86_64 ]]; then
ARCH=$ARCH_AMD64
elif [[ "$LOCAL_ARCH" == aarch64 ]]; then
ARCH=$ARCH_ARM64
elif [[ "$LOCAL_ARCH" == "$ARCH_PPC64LE" ]]; then
ARCH=$ARCH_PPC64LE
elif [[ "$LOCAL_ARCH" == "$ARCH_S390X" ]]; then
ARCH=$ARCH_S390X
else
echo "Unsupported local architecture: $LOCAL_ARCH"
exit 1
fi
echo "No architecture provided, using: $ARCH"
fi
}
verify_requirements() {
CMDS=(curl jq)
echo "Checking if all commands are available: ${CMDS[*]}"
for CMD in "${CMDS[@]}"; do
if ! command -v "$CMD" >/dev/null; then
echo "Command $CMD not available but required"
exit 1
fi
done
}
curl_retry() {
curl -sSfL --retry 5 --retry-delay 3 "$@"
}
download_binary() {
GIT_REF=refs/heads/main
if [[ $TAG != "" ]]; then
echo "Getting commit from tag"
TAG_JSON=$(curl_retry "https://api.github.com/repos/containers/conmon-rs/git/refs/tags/$TAG")
COMMIT=$(echo "$TAG_JSON" | jq -r .object.sha)
GIT_REF=$(echo "$TAG_JSON" | jq -r .ref)
else
if [[ $COMMIT == "" ]]; then
echo "Getting latest commit on main"
COMMIT=$(curl_retry $BASE_URL/latest-main.txt)
fi
# Latest commit can be a tag
POSSIBLE_TAG_JSON=$(curl_retry "https://api.github.com/repos/containers/conmon-rs/git/refs/tags" | jq '.[] | select(.object.sha == "'"$COMMIT"'")')
if [[ $POSSIBLE_TAG_JSON != "" ]]; then
GIT_REF=$(echo "$POSSIBLE_TAG_JSON" | jq -r .ref)
echo "Commit $COMMIT is tag ref $TAG"
fi
fi
echo "Found commit: $COMMIT"
echo "Using git ref: $GIT_REF"
mkdir -p "$(dirname "$OUTPUT")"
if command -v cosign >/dev/null; then
echo "Found cosign, verifying binary signature"
TMPDIR=$(mktemp -d)
trap 'rm -rf $TMPDIR' EXIT
pushd "$TMPDIR" >/dev/null
FILES=(
"conmonrs.$ARCH"
"conmonrs.$ARCH.sig"
"conmonrs.$ARCH.cert"
)
for FILE in "${FILES[@]}"; do
curl_retry "$BASE_URL/$COMMIT/$FILE" -o "$FILE"
done
SLUG=containers/conmon-rs
cosign verify-blob "${FILES[0]}" \
--certificate-identity "https://github.com/$SLUG/.github/workflows/ci.yml@$GIT_REF" \
--certificate-oidc-issuer https://token.actions.githubusercontent.com \
--certificate-github-workflow-name ci \
--certificate-github-workflow-repository "$SLUG" \
--certificate-github-workflow-ref "$GIT_REF" \
--signature "${FILES[1]}" \
--certificate "${FILES[2]}"
popd >/dev/null
mv "$TMPDIR/${FILES[0]}" "$OUTPUT"
else
curl_retry "$BASE_URL/$COMMIT/conmonrs.$ARCH" -o "$OUTPUT"
fi
chmod +x "$OUTPUT"
printf "Installed binary into: %s\n" "$OUTPUT"
if [[ $NO_EXEC == "" ]]; then
eval "$(realpath "$OUTPUT")" -v
fi
}
parse_args "$@"
verify_requirements
download_binary