uci-docker-build/lib/cmds/try

142 lines
4.1 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")
local mp;local cuser; local hmp; local bind; local vname; local prod; local priv
local dkpath; local hostmp; local evar; local hostmap; local cleanup; local efile
[[ $# -lt 1 ]] && echo "image name required to try" && return 1
declare OPTION; declare OPTARG; declare OPTIND
OPTIND=0
while getopts 'f:o:dpr:t:u:m:h:ke:s:' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in
s)
entrypoint="--entrypoint $OPTARG"
;;
f)
efile"--env-file $OPTARG"
;;
d)
dryrun="echo "
;;
u)
cuser=$OPTARG
;;
k)
keep=true
;;
# s)
# save=true
# ;;
m)
mp=$OPTARG
;;
o)
options=$OPTARG
;;
h)
hmp=$OPTARG
;;
e)
evar="-e $OPTARG"
;;
t)
TAG=$OPTARG
;;
p)
priv=--privileged
;;
r)
prod=$OPTARG
;;
*) echo unknown run option -$OPTARG
echo "USAGE: try <options>"
echo "available options: -t <latest> custom tag "
;;
esac
done
shift $((OPTIND - 1))
[[ ! $1 ]] && echo must supply an image to try && return 1
# user=${2:-$ruser}
image=$1
shift 1
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}
docker rm try-$name > /dev/null 2>&1
if [[ $mp ]]; then
hostmp="${hmp:-${PWD}/mnt/$mp}"
[[ ! $(isAbsPath $hostmp) ]] && hostmp=$PWD/$hostmp
vname="try-$name${dir//\//-}"
mkdir -p "$hostmp"
# echo bind $dir to volume $vname
$dryrun docker volume create --driver local \
--opt type=none \
--opt device=$hostmp \
--opt o=bind "$vname" > /dev/null
[[ ! $dryrun ]] && echo directory $mp in container will be mounted at $hostmp
# else
# vname="try-$name-${mp//\//-}"
# hostmap="-e HOST_MAP=$(id -u):$(id -g)"
# dkpath=$(docker info | grep -i "Docker Root Dir" | cut -d':' -f2)/volumes/$vname/_data
# echo via volume $vname
# echo linking $dkpath to $hostmp
# $dryrun ln -s $dkpath -T $hostmp
# fi
fi
echo starting container with image: $image, and name $name
echo at container prompt type \'exit\' to exit from shell and remove trial container
# --entrypoint /opt/scripts/entrypoint.sh \
$dryrun docker run -i -t --rm $priv $evar $hostmap $options \
${entrypoint} ${efile} \
$([[ $cuser ]] && echo "--user $cuser") \
--name try-$name --hostname try-$host-$name \
$([[ $mp ]] && echo "-v $vname:/$mp") \
"$image" \
"$@"
echo "done with session, removing containter try-$name"
if [[ $mp ]] && [[ ! $dryrun ]]; then
# if [[ ! $save ]]; then
echo removing volume $vname used for mapping
docker volume rm $vname > /dev/null
if [[ $keep ]]; then
echo mounted container directory $mp on host at $hostmp will not be removed
else
echo deleting directory at mountpoint $hostmp mapped to $mp in container
echo "use option -k to keep this directory after exiting container"
echo "useful for testing scripts inside the container"
rm -rf $hostmp
fi
# [[ ! $bind ]] && [[ -h $hostmp ]] && echo "removing link at $hostmp" && rm $hostmp
# fi
fi
}
# if script was executed then call the function
(return 0 2>/dev/null) || try_container "$@"