-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmexec-exec
executable file
·108 lines (91 loc) · 2.55 KB
/
mexec-exec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env bash
set -euo pipefail
SCRIPTPATH=$( dirname "$(realpath "$0")" )
parallel=${PARALLEL-1}
local_file=
declare -a kgp_args keti_args kcp_args cmd
errecho() {
>&2 echo "${@//$'\n'/}"
}
source "${SCRIPTPATH}/display-help.sh"
parse_arguments() {
if [[ "$#" = "0" ]]; then
display_help
exit 1
fi
while :
do
if [[ "$#" = "0" ]]; then
break
fi
# case "${1?cmd must be provided after "--"}" in
case "$1" in
-h|--help)
display_help
exit
;;
-p|--parallel)
parallel=$2
shift 2
;;
-F|--local-file)
local_file=$2
shift 2
;;
--)
shift
cmd+=("$@")
break
;;
-c|--container)
# pass to kubectl exec
keti_args+=("$1" "$2")
kcp_args+=("$1" "$2")
shift 2
;;
-n|--namespace)
# pass to both kgp and kubectl exec
kgp_args+=("$1" "$2")
keti_args+=("$1" "$2")
kcp_args+=("$1" "$2")
shift 2
;;
-o|--output)
# ignore
shift 2
;;
*)
kgp_args+=("$1")
shift
;;
esac
done
}
cleanup() {
rm -f "${temp_file}"
}
main() {
parse_arguments "$@"
if [[ -z "${local_file}" ]]; then
kubectl get pod "${kgp_args[@]}" -o name | awk -F/ '{print $2}' | xargs -S10240 -P "${parallel}" -I{} bash -c "kubectl exec ${keti_args[*]@Q} {} -- ${cmd[*]@Q} |& sed s/^/{}\ /g"
else
if [[ ! -f "${local_file}" ]]; then
errecho "Error: local file ${local_file} not found"
exit 1
fi
remote_file=/kmexec-$(date '+%Y%m%d%H%M%S')
pods=$(kubectl get pod "${kgp_args[@]}" -o name | awk -F/ '{print $2}')
export remote_file base64_encoded
base64_encoded=$(base64 -i "${local_file}")
temp_file=$(mktemp)
trap 'cleanup' EXIT
envsubst < "${SCRIPTPATH}/scripts/remote_run_tmpl.sh" > "${temp_file}"
chmod +x "${temp_file}"
echo "Uploading file to ${remote_file}, cleanup automatically"
echo "${pods}" | xargs -P "${parallel}" -I{} bash -c "kubectl cp ${kcp_args[*]} ${temp_file} {}:${remote_file} |& sed s/^/{}\ /g"
echo
echo "Running script on every pod"
echo "${pods}" | xargs -P "${parallel}" -I{} bash -c "kubectl exec ${keti_args[*]} {} -- ${remote_file} |& sed s/^/{}\ /g"
fi
}
main "$@"