Skip to content

Commit 6db27f7

Browse files
committed
+ cp-into-docker-run: add -p option
1 parent aed64c6 commit 6db27f7

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ source <(curl -fsSL https://raw.githubusercontent.com/oldratlee/useful-scripts/r
5656
不重排序输入完成整个输入行的去重。相比系统的`uniq`命令加强的是可以跨行去重,不需要排序输入。
5757
1. [ap and rp](docs/shell.md#-ap-and-rp)
5858
批量转换文件路径为绝对路径/相对路径,会自动跟踪链接并规范化路径。
59+
1. [cp-into-docker-run](docs/shell.md#-cp-into-docker-run)
60+
一个`Docker`使用的便利脚本。拷贝本机的执行文件到指定的`docker container`中并在`docker container`中执行。
5961
1. [tcp-connection-state-counter](docs/shell.md#-tcp-connection-state-counter)
6062
统计各个`TCP`连接状态的个数。用于方便排查系统连接负荷问题。
6163
1. [xpl and xpf](docs/shell.md#-xpl-and-xpf)

bin/cp-into-docker-run

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
5760
run options:
5861
-v, --verbose show operation step infos
@@ -78,6 +81,7 @@ container_name=
7881
docker_user=
7982
docker_workdir=
8083
docker_tmpdir=/tmp
84+
docker_command_cp_path=
8185
verbose=false
8286
declare -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
126134
done
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")"
149162
fi
150163
run_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

159178
cleanupWhenExit() {
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
}
163184
trap "cleanupWhenExit" EXIT
164185

docs/shell.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,13 @@ docker options:
391391
-c, --container destination docker container
392392
-u, --docker-user docker username or UID to run command
393393
optional, docker default is (maybe) root user
394-
-w, --workdir working directory inside the container
394+
-w, --workdir absolute working directory inside the container
395395
optional, docker default is (maybe) root dir
396396
-t, --tmpdir tmp dir in docker to copy command
397397
optional, default is /tmp
398+
-p, --cp-path destination path in docker of the command(including file name)
399+
if specified, command will be kept when run finished
400+
optional, default is under tmp dir and deleted when run finished
398401

399402
run options:
400403
-v, --verbose show operation step infos
@@ -432,7 +435,7 @@ SYN_SENT 7
432435
433436
### 贡献者
434437
435-
[sunuslee](https://github.com/sunuslee)改进此脚本,增加对`MacOS`的支持。 [#56](https://github.com/oldratlee/useful-scripts/pull/56)
438+
[sunuslee](https://github.com/sunuslee) 改进此脚本,增加对`MacOS`的支持。 [#56](https://github.com/oldratlee/useful-scripts/pull/56)
436439
437440
🍺 [xpl](../bin/xpl) and [xpf](../bin/xpf)
438441
----------------------
@@ -536,7 +539,7 @@ colorEchoWithoutNewLine "4;33;40" "Hello world!" "Hello Hell!"
536539
537540
### 贡献者
538541
539-
[姜太公](https://github.com/jzwlqx)提供循环输出彩色组合的脚本。
542+
[姜太公](https://github.com/jzwlqx) 提供循环输出彩色组合的脚本。
540543
541544
### 参考资料
542545

0 commit comments

Comments
 (0)