@@ -49,10 +49,13 @@ docker options:
4949 -c, --container destination docker container
5050 -u, --docker-user docker username or UID to run command
5151 optional, docker default is (maybe) root user
52- -w, --workdir working directory inside the container
52+ -w, --workdir absolute working directory inside the container
5353 optional, docker default is (maybe) root dir
5454 -t, --tmpdir tmp dir in docker to copy command
5555 optional, default is /tmp
56+ -p, --cp-path destination path in docker of the command(including file name)
57+ if specified, command will be kept when run finished
58+ optional, default is under tmp dir and deleted when run finished
5659
5760run options:
5861 -v, --verbose show operation step infos
@@ -78,6 +81,7 @@ container_name=
7881docker_user=
7982docker_workdir=
8083docker_tmpdir=/tmp
84+ docker_command_cp_path=
8185verbose=false
8286declare -a args=()
8387
@@ -99,6 +103,10 @@ while (($# > 0)); do
99103 docker_tmpdir=" $2 "
100104 shift 2
101105 ;;
106+ -p | --cp-path)
107+ docker_command_cp_path=" $2 "
108+ shift 2
109+ ;;
102110 -v | --verbose)
103111 verbose=true
104112 shift
@@ -125,6 +133,11 @@ while (($# > 0)); do
125133 esac
126134done
127135
136+ if [ -n " ${docker_command_cp_path:- } " ]; then
137+ [[ " $docker_command_cp_path " =~ ^/ ]] ||
138+ die " the command path in docker to copy must be absolute path: $docker_command_cp_path "
139+ fi
140+
128141# ###############################################################################
129142# biz logic
130143# ###############################################################################
@@ -148,17 +161,25 @@ if [ ! -f "$specified_run_command" ]; then
148161 run_command=" $( which " $specified_run_command " ) "
149162fi
150163run_command=" $( $READLINK_CMD -f " $run_command " ) "
164+ run_command_base_name=" $( basename " $run_command " ) "
151165
152- run_timestamp=" $( date " +%Y%m%d_%H%M%S" ) "
153- readonly uuid=" ${PROG} _${run_timestamp} _${$} _${RANDOM} "
166+ if [ -n " ${docker_command_cp_path:- } " ]; then
167+ run_command_dir_in_docker=" $( dirname " $docker_command_cp_path " ) "
168+ readonly run_command_in_docker=" $docker_command_cp_path "
169+ else
170+ run_timestamp=" $( date " +%Y%m%d_%H%M%S" ) "
171+ readonly uuid=" ${PROG} _${run_timestamp} _${$} _${RANDOM} "
172+ readonly work_tmp_dir_in_docker=" $docker_tmpdir /$uuid "
154173
155- run_command_base_name =" $( basename " $run_command " ) "
156- readonly run_command_dir_in_docker =" $docker_tmpdir / $uuid "
157- readonly run_command_in_docker= " $run_command_dir_in_docker / $run_command_base_name "
174+ readonly run_command_dir_in_docker =" $work_tmp_dir_in_docker "
175+ readonly run_command_in_docker =" $run_command_dir_in_docker / $run_command_base_name "
176+ fi
158177
159178cleanupWhenExit () {
179+ [ -n " ${work_tmp_dir_in_docker:- } " ] || return 0
180+
160181 # remove tmp dir in docker by root user
161- docker exec " ${container_name} " rm -rf -- " $run_command_dir_in_docker " & > /dev/null
182+ docker exec " ${container_name} " rm -rf -- " $work_tmp_dir_in_docker " & > /dev/null
162183}
163184trap " cleanupWhenExit" EXIT
164185
0 commit comments