Skip to content

Instantly share code, notes, and snippets.

@Johannestegner
Last active June 9, 2022 11:01
Show Gist options
  • Select an option

  • Save Johannestegner/093e8053eabd795ed84b83e9610aed6b to your computer and use it in GitHub Desktop.

Select an option

Save Johannestegner/093e8053eabd795ed84b83e9610aed6b to your computer and use it in GitHub Desktop.
Helper file for conversion of architectures and tags for docker buildx.
#!/bin/ash
# This file contains a few different helper methods
# for building multi-arch images with docker buildx.
# Most of the methods is to convert tags and architectures, but it
# might be updated to contain more later on.
#
# Convert a "standard" architecture name into the format that buildx
# is expecting.
#
# arguments:
# $1 - String - Architecture name in "standard" format (e.g., x86_64, aarch64 etc).
#
function convert_arch () {
case "$1" in
# AMD64: x86_64, amd64, x64,
x86_64 | amd64 | x64)
echo "linux/amd64"
exit;;
aarch64 | arm64 | arm64v8)
echo "linux/arm64"
exit;;
armv6)
echo "linux/arm/v6"
exit;;
armv7 | arm32v7 | arm32 | armhf)
echo "linux/arm/v7"
exit;;
ppc64le | ppc64el)
echo "linux/ppc64le"
exit;;
s390x)
echo "linux/s390x"
exit;;
x86 | i386 | 386)
echo "linux/386"
exit;;
esac
}
#
# Takes multiple architecture in "standard" format and echos them
# in the docker buildx expected format as a , seperated string.
#
# arguments:
# $@: - String[] - Architecture names as strings.
#
function arch_list () {
for arc in "$@"; do
echo -n "$(convert_arch "${arc}"),"
done
}
#
# Takes a set of image names and tag names as comma seperated strings
# and converts it into a list of `-t <img_name>:<tag_name>` for each entry.
#
# arguments:
# $1 - String - Image names as a comma seperated string (e.g., "img1,img2").
# $2 - String - Tag names as a comma seperated string (e.g., "tag1,tag2").
#
function make_tags () {
IMGS=$(echo "$1" | awk 'BEGIN{RS=","} {print}')
TAGS=$(echo "$2" | awk 'BEGIN{RS=","} {print}')
for tag in ${TAGS}; do
for img in ${IMGS}; do
echo -n "-t ${img}:${tag} "
done
done
}
#
# Takes a set of image names and tag names as comma seperated strings
# and converts it into a list of `<img_name>:<tag_name>` for each entry.
#
# arguments:
# $1 - String - Image names as a comma seperated string (e.g., "img1,img2").
# $2 - String - Tag names as a comma seperated string (e.g., "tag1,tag2").
#
function image_list () {
IMGS=$(echo "$1" | awk 'BEGIN{RS=","} {print}')
TAGS=$(echo "$2" | awk 'BEGIN{RS=","} {print}')
for tag in ${TAGS}; do
for img in ${IMGS}; do
echo -n "${img}:${tag} "
done
done
}
#
# Simple function to allow adding multiple tags to a specific image.
#
# arguemtns:
# $1 - String - Origin image.
# $@ - String[] - New image:tag to create.
#
function multi_tag () {
ORIGIN=$1
shift
for tag in "$@"; do
docker tag "${ORIGIN}" "${tag}"
echo "${ORIGIN} ==> ${tag}"
done
}
#
# Simple function to push multiple tags in one command.
#
# arguments:
# $@ - String[] - image:tag list to push.
#
function multi_push () {
echo "Pushing the following tags: ${*}."
for target in "$@"; do
docker push "${target}"
done
}
case "$1" in
imagelist)
shift
image_list "$1" "$2"
exit;;
arch)
shift
convert_arch "$1"
exit;;
archlist)
shift
arch_list "${@}" | head -c -1
exit;;
taglist)
shift
make_tags "$1" "$2"
exit;;
multipush)
shift
multi_push "$@"
exit;;
multitag)
shift
multi_tag "$@"
exit;;
*)
make_tags "$1" "$2"
exit;;
esac
@Johannestegner
Copy link
Copy Markdown
Author

To add:

# Get dependencies required for a lib or executable.
scanelf --needed --nobanner --format '%n#p' $1 | tr ',' '\n' | sort -u | awk 'system("[ -e /usr/lib/" $1 " ]") == 0 { next } { print "so:" $1 }'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment