Skip to content

Commit d7fcc2f

Browse files
committed
Support installing jq
1 parent 00a8078 commit d7fcc2f

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ module "ecr_repo_image" {
4444

4545
## NOTES
4646

47-
* This module needs `curl` and `jq` on `PATH`.
47+
* This module only works under Linux.
48+
* This module needs `curl` and `jq` on `PATH`. If `jq` are missing, it will fetch
49+
and install `jq 1.7.1` locally for the appropriate architecture.
4850

4951
## EXAMPLES
5052

scripts/helpers.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#! /usr/bin/env bash
2+
set -eu
23

34
curl_with_auth_header() {
45
auth_params=()
@@ -43,3 +44,13 @@ check_vars() {
4344
fi
4445
return 0
4546
}
47+
48+
install_jq() {
49+
cd "${1:?Install directory not passed as $1 to install_jq}"
50+
arch=$(uname -m)
51+
case $arch in
52+
x86_64) arch="amd64" ;;
53+
esac
54+
curl -fsSLo jq "https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-linux-$arch"
55+
chmod 0755 jq
56+
}

scripts/pull.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ _fetch_manifest() {
3737
if [ "$image_hash" = "null" ]; then
3838
available=$(jq -r '. | map(.platform.architecture) | join(", ")' <<<"$manifests")
3939
printf "Architecture %s not found. Available options are: %s\n" "$IMAGE_ARCH" "$available" >&2
40-
exit 2
40+
exit 3
4141
else
4242
IMAGE_TAG="$image_hash" _fetch_manifest
4343
fi
4444
else
4545
echo "Unknown manifest format: " >&2
4646
echo "$curl_output" >&2
47-
exit 1
47+
exit 3
4848
fi
4949
echo '_fetch_manifest: complete' >&2
5050
}
@@ -72,7 +72,7 @@ download_image() {
7272
if [[ -z "$config_digest" || "$config_digest" == 'null' ]]; then
7373
echo 'download_image: failed to parse config_digest from manifest' >&2
7474
cat "$manifest_file_path" >&2
75-
return 1
75+
return 3
7676
fi
7777

7878
config_file_path="$dir_path/config.json"

scripts/pull_then_push.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ set -eu
1717
# PUSH_REPO_NAME='my-repo' \
1818
# PUSH_IMAGE_TAG='latest' \
1919
# ./pull_then_push.sh
20+
#
21+
# Exit codes:
22+
# * 1 - Undefined input variable
23+
# * 2 - Missing tools
24+
# * 3 - Pull failures
25+
# * 4, 5 - Push failures
26+
# * Other codes - curl failures or unexpected error
27+
#
2028
##
2129

2230
_dir_path() {
@@ -40,6 +48,25 @@ if [ -z ${PUSH_CURL_AUTH_HEADER+x} ]; then
4048
exit 1
4149
fi
4250

51+
# Check tools
52+
if ! command -v curl &>/dev/null; then
53+
echo "curl not found" >&2
54+
exit 2
55+
fi
56+
if ! command -v jq &>/dev/null; then
57+
echo "jq not found, trying to install..." >&2
58+
bin_dir="$PULL_DOWNLOAD_DIR_PATH/bin"
59+
mkdir -p "$bin_dir"
60+
bin_dir=$(readlink -f "$bin_dir")
61+
if install_jq "$bin_dir"; then
62+
export PATH="$bin_dir:$PATH"
63+
echo "Installed $(jq --version)." >&2
64+
else
65+
echo "jq not found and could not be installed" >&2
66+
exit 2
67+
fi
68+
fi
69+
4370
# Pull image
4471
export CURL_AUTH_HEADER="$PULL_CURL_AUTH_HEADER"
4572
manifest_path=$(

scripts/push.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ check_vars REPO_URL IMAGE_TAG LAYER_PATHS CONFIG_PATH MANIFEST_PATH
2424
echo "$LAYER_PATHS" | while read -r layer_path ; do
2525
if [[ ! -f "$layer_path" ]]; then
2626
echo "No file exists at layer_path of '$layer_path'" >&2
27-
exit 1
27+
exit 4
2828
fi
2929
done
3030

3131
if [[ ! -f "$CONFIG_PATH" ]]; then
3232
echo "No file exists at CONFIG_PATH='$CONFIG_PATH'" >&2
33-
exit 1
33+
exit 4
3434
fi
3535

3636

@@ -65,7 +65,7 @@ _initiate_upload() {
6565
)
6666
if [[ -z "$location" ]]; then
6767
echo '_initiate_upload: Failed to parse Location from curl_output' >&2
68-
return 1
68+
return 5
6969
fi
7070

7171
echo '_initiate_upload: complete' >&2
@@ -95,7 +95,7 @@ _patch_layer() {
9595
)
9696
if [[ -z "$location" ]]; then
9797
echo '_patch_layer: Failed to parse location from curl_output' >&2
98-
return 1
98+
return 5
9999
fi
100100

101101
echo '_patch_layer: complete' >&2

0 commit comments

Comments
 (0)