Skip to content

Commit 2ee3f3d

Browse files
authored
[scripts]: add prompt for updating container with unsigned package (apple#1468)
Closes apple#1467 and some minor QOL updates
1 parent 9f4e779 commit 2ee3f3d

1 file changed

Lines changed: 43 additions & 19 deletions

File tree

scripts/update-container.sh

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ INSTALL_DIR="/usr/local"
1919
OPTS=0
2020
LATEST=false
2121
VERSION=
22+
FORCE=false
2223
TMP_DIR=
2324

2425
# Release Info
@@ -29,8 +30,10 @@ RELEASE_VERSION=
2930
# Package Info
3031
PKG_URL=
3132
PKG_FILE=
32-
PRIMARY_PKG=
33-
FALLBACK_PKG=
33+
SIGNED_PRIMARY_PKG=
34+
SIGNED_FALLBACK_PKG=
35+
UNSIGNED_PRIMARY_PKG=
36+
UNSIGNED_FALLBACK_PKG=
3437

3538
check_installed_version() {
3639
local target_version="$1"
@@ -46,30 +49,34 @@ check_installed_version() {
4649
}
4750

4851
usage() {
49-
echo "Usage: $0 {-v <version>}"
52+
echo "Usage: $0 {-v <version> | -f}"
5053
echo "Update container"
5154
echo
5255
echo "Options:"
53-
echo "v <version> Install a specific release version"
54-
echo "No argument Defaults to latest release version"
56+
echo "v <version> Update to a specific release version"
57+
echo "f Force update"
58+
echo "No argument Defaults to the latest release version"
5559
exit 1
5660
}
5761

58-
while getopts ":v:" arg; do
62+
while getopts ":v:f" arg; do
5963
case "$arg" in
6064
v)
6165
VERSION="$OPTARG"
6266
((OPTS+=1))
6367
;;
68+
f)
69+
FORCE=true
70+
;;
6471
*)
6572
echo "Invalid option: -${OPTARG}"
6673
usage
6774
;;
6875
esac
6976
done
7077

71-
# Default to install the latest release version
72-
if [ "$OPTS" -eq 0 ]; then
78+
# Default to upgrade to the latest release version
79+
if [[ -z "$VERSION" ]]; then
7380
LATEST=true
7481
fi
7582

@@ -93,17 +100,17 @@ error() { echo "Error: $*" >&2; exit 1; }
93100
if [[ "$LATEST" == true ]]; then
94101
RELEASE_URL="https://api.github.com/repos/apple/container/releases/latest"
95102
RELEASE_VERSION=$(curl -fsSL "$RELEASE_URL" | jq -r '.tag_name')
96-
if check_installed_version "$RELEASE_VERSION"; then
97-
echo "Container is already on latest version $RELEASE_VERSION"
103+
if check_installed_version "$RELEASE_VERSION" && [[ "$FORCE" != true ]]; then
104+
echo "Container is already on latest version $RELEASE_VERSION (use -f to force update)"
98105
exit 0
99106
else
100107
echo "Updating to latest version $RELEASE_VERSION"
101108
fi
102109
elif [[ -n "$VERSION" ]]; then
103110
RELEASE_URL="https://api.github.com/repos/apple/container/releases/tags/$VERSION"
104111
RELEASE_VERSION="$VERSION"
105-
if check_installed_version "$RELEASE_VERSION"; then
106-
echo "Container is already on version $RELEASE_VERSION"
112+
if check_installed_version "$RELEASE_VERSION" && [[ "$FORCE" != true ]]; then
113+
echo "Container is already on version $RELEASE_VERSION (use -f to force update)"
107114
exit 0
108115
else
109116
echo "Updating to release version $RELEASE_VERSION"
@@ -116,15 +123,32 @@ RELEASE_JSON=$(curl -fsSL "$RELEASE_URL") || {
116123
}
117124

118125
# Possible package names
119-
PRIMARY_PKG="container-installer-signed.pkg"
120-
FALLBACK_PKG="container-$RELEASE_VERSION-installer-signed.pkg"
126+
SIGNED_PRIMARY_PKG="container-installer-signed.pkg"
127+
SIGNED_FALLBACK_PKG="container-$RELEASE_VERSION-installer-signed.pkg"
128+
UNSIGNED_PRIMARY_PKG="container-installer-unsigned.pkg"
129+
UNSIGNED_FALLBACK_PKG="container-$RELEASE_VERSION-installer-unsigned.pkg"
121130

122-
# Find the package URL
131+
# Find the signed package
123132
PKG_URL=$(echo "$RELEASE_JSON" | jq -r \
124-
--arg primary "$PRIMARY_PKG" \
125-
--arg fallback "$FALLBACK_PKG" \
133+
--arg primary "$SIGNED_PRIMARY_PKG" \
134+
--arg fallback "$SIGNED_FALLBACK_PKG" \
126135
'.assets[] | select(.name == $primary or .name == $fallback) | .browser_download_url' | head -n1)
127-
[[ -n "$PKG_URL" ]] || error "Neither $PRIMARY_PKG nor $FALLBACK_PKG found"
136+
137+
# If no signed package found, prompt and try unsigned
138+
if [[ -z "$PKG_URL" ]]; then
139+
read -r -p "No signed package found. Upgrade using the unsigned package instead? (Y/n): " confirm
140+
if [[ "$confirm" =~ ^[yY]([eE][sS])?$ ]]; then
141+
echo "NOTE: re-run this script to upgrade to the signed package, when it becomes available"
142+
PKG_URL=$(echo "$RELEASE_JSON" | jq -r \
143+
--arg u1 "$UNSIGNED_PRIMARY_PKG" \
144+
--arg u2 "$UNSIGNED_FALLBACK_PKG" \
145+
'.assets[] | select(.name == $u1 or .name == $u2) | .browser_download_url' | head -n1)
146+
else
147+
echo "Exiting without updating"
148+
exit 0
149+
fi
150+
fi
151+
[[ -n "$PKG_URL" ]] || error "No suitable package found"
128152

129153
PKG_FILE="$TMP_DIR/$(basename "$PKG_URL")"
130154

@@ -135,5 +159,5 @@ curl -fSL "$PKG_URL" -o "$PKG_FILE"
135159
echo "Installing package to $INSTALL_DIR..."
136160
sudo installer -pkg "$PKG_FILE" -target / >/dev/null 2>&1 || error "Installer failed"
137161

138-
echo "Installed successfully"
162+
echo "Updated successfully"
139163
container --version || error "'container' command not found"

0 commit comments

Comments
 (0)