Skip to content

Instantly share code, notes, and snippets.

@mwartell
Created March 2, 2022 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwartell/54999c5a3edb4a43269482ef71e3d6ba to your computer and use it in GitHub Desktop.
Save mwartell/54999c5a3edb4a43269482ef71e3d6ba to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
usage()
{
cat <<EOF
Armory Docker Build Script "
This build script helps to build the docker images necessary for armory exectuion
The primary purpose of this script is to build the images from scratch and to be
used by the armory CI toolchain.
Armory now uses scm versioning from git, therefore if you want to re-build containers
from a stable armory release, simply checkout that tag and then run this script
usage: build.sh [-f|nc|dr|v|h]
REQUIRED
-f | --framework which framework to build:
[all|base|tf2|pytorch|pytorch-deepspeech]
"
OPTIONAL
-t | --tag additional tag to apply to each built image
--no-cache build clean images (i.e. using --no-cache)
--dry-run only show the build calls that would be run
-v | --verbose show logs in plain text (uses --progress=plain)
-h | --help print this help
EOF
}
# Setting Defaults
no_cache=false
armory_version="$(python setup.py --version)"
dryrun=false
verbose="--progress=auto"
repo="twosixarmory"
framework=""
tag=${armory_version%.*} # removing last part that tracks every edit
tag=${tag/+/-} # Cannot have + sign in tags
additional_tag=false
# Making sure execution path is correct
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
if [ "$PWD" != "$(dirname $script_dir)" ]; then
echo "build script must be run from within the armory root not ${PWD}"
exit 1
fi
## Parsing CLI Arguments
if [ $# == 0 ] ; then usage ; exit 1; fi
# cannonicalize arguments: turn --opt=val to --opt val etc.
rewrite=$(getopt -o f:t:vnh --long framework:,tag:,verbose,dry-run,help,no-cache \
-n "$0" -- "$@")
[ $? -ne 0 ] && exit 1
eval set -- "$rewrite"
while true ; do
case "$1" in
-f | --framework) framework="$2"; shift 2 ;;
-t | --tag) tag="$2"; shift 2;;
-v | --verbose) verbose="--progress-plain" ; shift;;
-n | --dry-run) dryrun=true ; shift ;;
--no-cache) no_cache=true; shift ;;
-h | --help) usage; shift ;;
--) shift; break ;;
*) break;;
esac
done
## Conducting Checks of CLI args
if [ "$framework" == "" ] ; then
echo "must specify --framework"
exit 1
fi
case $framework in
pytorch|pytorch-deepspeech|tf2|base) frameworks=$framework ;;
all) frameworks="base pytorch tf2 pytorch-deepspeech" ;;
*) echo "ERROR: framework argument must be tf2, pytorch, pytorch-deepspeech, base, or all, not $framework" ; exit 1 ;;
esac
# echo processed args -f $frameworks -t $tag -v $verbose -n $dryrun --no-cache $no_cache
for framework in $frameworks ; do
echo ""
echo "------------------------------------------------"
echo "building docker image for framework: $framework"
cmd="docker build"
if $no_cache; then
cmd+=" --no-cache"
else
cmd+=" --cache-from ${repo}/${framework}:${tag}"
fi
cmd+=" --force-rm"
cmd+=" --file $SCRIPT_DIR/Dockerfile-${framework}"
if [ $framework != "base" ]; then
cmd+=" --build-arg base_image_tag=${tag}"
cmd+=" --build-arg armory_version=${armory_version}"
fi
cmd+=" -t ${repo}/${framework}:${tag}"
cmd+=" $verbose"
cmd+=" ."
if $dryrun; then
echo "dryrun. would have executed: "
echo " -> $cmd"
else
echo "executing: "
echo " -> $cmd"
$cmd
fi
if [ "$additional_tag" != "" ]; then
if $dryrun; then
echo "dryrun. would have executed: "
echo docker tag "${repo}/${framework}:${TAG}" "${repo}/${framework}:${additional_tag}"
else
echo "Executing: "
echo docker tag "${repo}/${framework}:${TAG}" "${repo}/${framework}:${additional_tag}"
docker tag "${repo}/${framework}:${TAG}" "${repo}/${framework}:${additional_tag}"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment