uci-docker-build/lib/src/try

96 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# starts a trail container with passed image with a bash prompt
# $1 image name, $2 user
# user can be also prepended by using u option
# added tag is "latest" by default, use t option for alternate tag
# if p flag is used script will scrub any local image and attempt to download a deployed to docker image
try_container () {
declare -A arch=( ["x86_64"]="" ["aarch64"]="-arm64")
[[ $# -lt 1 ]] && echo "image name required to try" && return 1
declare OPTION; declare OPTARG; declare OPTIND
OPTIND=0
while getopts 'pt:u:' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in
u)
ruser=$OPTARG
;;
t)
TAG=$OPTARG
;;
p)
PROD=$OPTARG
;;
*) echo unknown run option -$OPTARG
echo "USAGE: try <options>"
echo "available options: -t <latest> custom tag "
;;
esac
done
shift $((OPTIND - 1))
user=${2:-$ruser}
image=$([[ $user ]] && echo ${user}/)$1
# :${TAG:-latest}
if [[ $PROD ]]; then
echo removing any local copy of image $image
docker image rm $image
host=prod
else
host=local
# TODO change this
image=${image/:/${arch[$(uname -p)]}:}
fi
name=${image//\//-}
image=$image:${TAG:-latest}
bmount () {
[[ $1 == "-u" ]] && umount=true && shift
declare mp=${1:-opt}
declare dir=${2:-${PWD}/_$mp}
vname=try-$name-$mp
if [[ $umount ]]; then
echo removing volume ....
docker volume rm $vname
echo "volume $vname was removed"
echo "Do you want to also delete the bound volume directory $dir?"
read -n 1 -p "Warning: ALL changes in $mp directory in container will be lost! [y]=>" REPLY
[[ $REPLY != "y" ]] && echo -e "\n" && exit 0
sudo rm -rf $dir
else
mkdir -p $dir
docker volume create --driver local \
--opt type=none \
--opt device=$dir \
--opt o=bind \
$vname
# echo "type=volume,dst=/$mp,volume-driver=local,volume-opt=type=none,volume-opt=o=bind,volume-opt=device=$dir"
fi
}
echo starting container with image: $image, and name $name
echo at container prompt type \'exit\' to exit from shell and remove trial container
docker rm try-$name > /dev/null 2>&1
docker run -i -t --rm --entrypoint /usr/bin/env --name try-$name --hostname try-$host-$name \
-v "$(bmount)":/opt \
$image \
/bin/bash -l
echo "done with session, removing containter try-$name"
bmount -u
}
# if script was executed then call the function
(return 0 2>/dev/null) || try_container $@