1
0
Fork 0
env/.local/bin/runenv

134 lines
2.3 KiB
Plaintext
Raw Normal View History

2018-04-01 21:36:34 +08:00
#!/bin/sh
2018-10-19 23:34:25 +08:00
CONTAINER_IMAGE="registry.makerforce.io/ambrose/env"
2018-12-09 03:48:55 +08:00
CONTAINER_IMAGE_PWN="registry.makerforce.io/ambrose/pwn"
2018-04-02 11:39:51 +08:00
CONTAINER_HOME="/home/ambrose"
2018-04-01 22:44:55 +08:00
OPTIND=1
2018-04-02 12:25:14 +08:00
workdir=""
2018-04-01 22:44:55 +08:00
delete=""
name=""
2018-04-02 11:39:51 +08:00
ports="\n\t"
2018-04-01 22:44:55 +08:00
volumes=""
2018-12-09 03:48:55 +08:00
image="$CONTAINER_IMAGE"
2018-04-01 22:44:55 +08:00
2018-04-01 22:55:11 +08:00
show_help() {
cat << EOF
2019-01-09 11:36:46 +08:00
Usage: ${0##*/} [-xwt] [-n NAME] [-p PORT]... [-v LIST]... [CMD]...
${0##*/} -l
${0##*/} -a NAME
${0##*/} -e NAME [CMD]...
${0##*/} -h
2018-04-01 22:55:11 +08:00
Starts an env Docker container. If CMD is specified, starts CMD
instead of a shell.
-x delete Docker container after exit. will loose data
2018-04-02 12:25:14 +08:00
-w forward \$PWD into $CONTAINER_HOME/src, and start there
2018-04-02 11:39:51 +08:00
-n NAME give container a NAME
2018-04-01 22:55:11 +08:00
-p PORT forward host PORT to container PORT
-v LIST mount volume LIST. specify as LOCAL:MOUNT
2018-12-09 03:48:55 +08:00
-t run pwntools instead
2019-01-09 11:36:46 +08:00
-l list running containers and exit
-a NAME attach to a running container
-e NAME execute zsh or CMD on a running container
-h display this help and exit
2018-04-01 22:55:11 +08:00
EOF
}
2019-01-09 11:36:46 +08:00
list_running() {
2019-01-09 11:16:20 +08:00
docker ps \
--filter \
label=org.label-schema.vcs-url=https://git.makerforce.io/ambrose/env.git \
--format \
"table {{.Names}}\t{{.RunningFor}}\t{{.Ports}}\t{{.Command}}"
}
2019-01-09 11:36:46 +08:00
while getopts "xwn:p:v:tla:e:h" opt; do
2018-04-01 22:44:55 +08:00
case "$opt" in
x)
delete="--rm "
;;
2018-04-02 12:19:49 +08:00
w)
2018-04-02 12:25:14 +08:00
workdir="\n\t--workdir $CONTAINER_HOME/src "
2018-04-08 22:36:35 +08:00
volumes="$volumes\n\t-v '$PWD':$CONTAINER_HOME/src "
2018-04-02 11:39:51 +08:00
;;
2018-04-01 22:44:55 +08:00
n)
name="--name $OPTARG "
;;
p)
2018-04-02 11:39:51 +08:00
ports="$ports-p $OPTARG:$OPTARG "
2018-04-01 22:44:55 +08:00
;;
v)
2018-04-08 22:36:35 +08:00
volumes="$volumes\n\t-v '$OPTARG' "
2018-04-01 22:44:55 +08:00
;;
2019-01-09 11:36:46 +08:00
t)
image="$CONTAINER_IMAGE_PWN"
2018-04-01 22:55:11 +08:00
;;
2019-01-09 11:16:20 +08:00
l)
2019-01-09 11:36:46 +08:00
list_running
2019-01-09 11:16:20 +08:00
exit 0
;;
2019-01-09 11:36:46 +08:00
a)
mode_attach="true"
name="$OPTARG"
;;
e)
mode_execute="true"
name="$OPTARG"
;;
h)
show_help
exit 0
2018-12-09 03:48:55 +08:00
;;
2018-04-01 22:44:55 +08:00
:)
echo "Option -$OPTARG requires an argument." >&2
2018-04-01 22:55:11 +08:00
show_help
2018-04-01 22:44:55 +08:00
exit 1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
2018-04-01 22:55:11 +08:00
show_help
2018-04-01 22:44:55 +08:00
exit 1
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
2018-04-02 11:39:51 +08:00
if [ "$ports" = "\n\t" ]; then
ports=""
fi
2019-01-09 11:36:46 +08:00
if [ "$mode_attach" = "true" ]; then
docker attach \
$name
exit 0
fi
if [ "$mode_execute" = "true" ]; then
cmd="$@"
if [ -z "$@" ]; then
cmd="/bin/zsh"
fi
docker exec -it \
$name \
$cmd
exit 0
fi
2018-12-09 03:48:55 +08:00
docker pull $image
2018-04-01 22:44:55 +08:00
cmd="\n\
2018-04-02 12:25:14 +08:00
docker run -it $delete$name$ports$workdir$volumes\n\
2018-12-09 03:48:55 +08:00
$image \n\
2018-04-01 22:44:55 +08:00
$@\
"
echo
echo "Running: $cmd"
2018-04-08 22:36:35 +08:00
bash -c "`echo $cmd | tr -d '\n'`" # Ugh...