2023-01-17 15:20:30 -08:00
|
|
|
#!/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")
|
2023-04-03 14:14:17 -07:00
|
|
|
local mp;local cuser; local hmp; local vname; local prod; local priv
|
|
|
|
local hostmp; local efile; local entrypoint; local evar
|
|
|
|
local envf; local image; local append_distro; local options
|
2023-01-17 15:20:30 -08:00
|
|
|
|
|
|
|
declare OPTION; declare OPTARG; declare OPTIND
|
|
|
|
OPTIND=0
|
2023-04-03 14:14:17 -07:00
|
|
|
while getopts 'lf:o:dpr:t:u:m:h:kv:e:s:' OPTION; do
|
2023-01-17 15:20:30 -08:00
|
|
|
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
|
|
|
|
case "$OPTION" in
|
2023-03-25 11:27:22 -07:00
|
|
|
s)
|
|
|
|
entrypoint="--entrypoint $OPTARG"
|
|
|
|
;;
|
2023-04-03 14:14:17 -07:00
|
|
|
e)
|
|
|
|
if ! source_env_file $OPTARG; then return 2; fi
|
|
|
|
efile=true
|
|
|
|
;;
|
2023-03-25 11:27:22 -07:00
|
|
|
f)
|
2023-04-03 14:14:17 -07:00
|
|
|
envf="--env-file $OPTARG"
|
2023-03-25 11:27:22 -07:00
|
|
|
;;
|
2023-03-22 09:10:54 -07:00
|
|
|
d)
|
|
|
|
dryrun="echo "
|
|
|
|
;;
|
2023-01-17 15:20:30 -08:00
|
|
|
u)
|
2023-01-21 22:47:00 -08:00
|
|
|
cuser=$OPTARG
|
|
|
|
;;
|
2023-04-03 14:14:17 -07:00
|
|
|
l)
|
|
|
|
# append distro name to image name
|
|
|
|
append_distro=true
|
|
|
|
;;
|
2023-03-22 09:10:54 -07:00
|
|
|
k)
|
|
|
|
keep=true
|
2023-01-21 22:47:00 -08:00
|
|
|
;;
|
2023-03-22 09:10:54 -07:00
|
|
|
# s)
|
|
|
|
# save=true
|
|
|
|
# ;;
|
2023-01-21 22:47:00 -08:00
|
|
|
m)
|
|
|
|
mp=$OPTARG
|
|
|
|
;;
|
2023-03-22 09:10:54 -07:00
|
|
|
o)
|
2023-03-25 11:27:22 -07:00
|
|
|
options=$OPTARG
|
2023-03-22 09:10:54 -07:00
|
|
|
;;
|
2023-01-21 22:47:00 -08:00
|
|
|
h)
|
|
|
|
hmp=$OPTARG
|
|
|
|
;;
|
2023-04-03 14:14:17 -07:00
|
|
|
v)
|
2023-01-21 22:47:00 -08:00
|
|
|
evar="-e $OPTARG"
|
2023-01-17 15:20:30 -08:00
|
|
|
;;
|
|
|
|
t)
|
2023-01-21 22:47:00 -08:00
|
|
|
TAG=$OPTARG
|
|
|
|
;;
|
2023-01-17 15:20:30 -08:00
|
|
|
p)
|
2023-01-21 22:47:00 -08:00
|
|
|
priv=--privileged
|
|
|
|
;;
|
|
|
|
r)
|
|
|
|
prod=$OPTARG
|
2023-01-17 15:20:30 -08:00
|
|
|
;;
|
|
|
|
*) echo unknown run option -$OPTARG
|
|
|
|
echo "USAGE: try <options>"
|
|
|
|
echo "available options: -t <latest> custom tag "
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $((OPTIND - 1))
|
|
|
|
|
2023-04-03 14:14:17 -07:00
|
|
|
[[ ! $efile ]] && source_env_file
|
2023-01-17 15:20:30 -08:00
|
|
|
|
2023-04-03 14:14:17 -07:00
|
|
|
image=$(make_image_name $@)
|
|
|
|
|
|
|
|
echo image name to try: $image
|
|
|
|
|
|
|
|
[[ ! $image ]] && echo must supply an image to try && return 1
|
2023-01-17 15:20:30 -08:00
|
|
|
|
2023-01-21 22:47:00 -08:00
|
|
|
if [[ $prod ]]; then
|
2023-01-17 15:20:30 -08:00
|
|
|
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}
|
|
|
|
|
2023-01-21 22:47:00 -08:00
|
|
|
docker rm try-$name > /dev/null 2>&1
|
|
|
|
if [[ $mp ]]; then
|
2023-03-22 09:10:54 -07:00
|
|
|
hostmp="${hmp:-${PWD}/mnt/$mp}"
|
2023-03-25 11:27:22 -07:00
|
|
|
[[ ! $(isAbsPath $hostmp) ]] && hostmp=$PWD/$hostmp
|
2023-03-22 09:10:54 -07:00
|
|
|
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
|
2023-01-17 15:20:30 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo starting container with image: $image, and name $name
|
|
|
|
echo at container prompt type \'exit\' to exit from shell and remove trial container
|
2023-03-22 09:10:54 -07:00
|
|
|
# --entrypoint /opt/scripts/entrypoint.sh \
|
2023-04-03 14:14:17 -07:00
|
|
|
$dryrun docker run -i -t --rm $priv $evar $options \
|
|
|
|
${entrypoint} ${evnf} \
|
2023-01-21 22:47:00 -08:00
|
|
|
$([[ $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"
|
2023-03-22 09:10:54 -07:00
|
|
|
if [[ $mp ]] && [[ ! $dryrun ]]; then
|
|
|
|
# if [[ ! $save ]]; then
|
2023-01-21 22:47:00 -08:00
|
|
|
echo removing volume $vname used for mapping
|
|
|
|
docker volume rm $vname > /dev/null
|
2023-03-22 09:10:54 -07:00
|
|
|
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
|
2023-01-21 22:47:00 -08:00
|
|
|
fi
|
|
|
|
|
2023-01-17 15:20:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
# if script was executed then call the function
|
2023-03-25 11:27:22 -07:00
|
|
|
(return 0 2>/dev/null) || try_container "$@"
|