Skip to content

Commit 82aac76

Browse files
committed
First version of containerdiag.bat
Signed-off-by: Kevin Grigorenko <kevin.grigorenko@us.ibm.com>
1 parent 88651ef commit 82aac76

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

containerdiag.bat

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
:: containerdiag.bat
2+
@echo off
3+
4+
SETLOCAL ENABLEDELAYEDEXPANSION
5+
6+
set "SCRIPTNAME=%~nx0"
7+
8+
goto :start
9+
10+
:usage
11+
echo usage: !SCRIPTNAME! [options] [/d DEPLOYMENT] [/p POD] COMMANDS...
12+
echo(
13+
echo( /d DEPLOYMENT: Run COMMANDS on all pods in the specified DEPLOYMENT
14+
echo( /i IMAGE: The image used for the debug pod (default quay.io/ibm/containerdiag)
15+
echo( /k: By default, this script uses oc if available. This options forces the use of kubectl
16+
echo( /n NAMESPACE: Namespace (optional; defaults to current namespace)
17+
echo( /p POD: Run COMMANDS on the specified POD
18+
echo( /q: Do not append the pod name to COMMANDS
19+
echo( /v: verbose output to stderr
20+
echo(
21+
echo( COMMANDS will be passed to oc debug node along with the pod name at the end
22+
echo( (unless -q is specified in which case the pod name is not appended)
23+
exit /B 1
24+
25+
:printInfo
26+
echo [!date! !time!] !SCRIPTNAME!: %*
27+
goto :eof
28+
29+
:printVerbose
30+
echo [!date! !time!] !SCRIPTNAME!: %*
31+
goto :eof
32+
33+
:processPod
34+
if "!APPEND!" == "1" (
35+
call :printInfo Processing pod !TARGETPOD! on worker node !WORKER! with %* !TARGETPOD!
36+
!CTL! debug node/!WORKER! !CTL_DEBUG_FLAGS! --image=!IMAGE! -- %* !TARGETPOD!
37+
) else (
38+
call :printInfo Processing pod !TARGETPOD! on worker node !WORKER! with %*
39+
)
40+
goto :eof
41+
42+
:start
43+
set "NAMESPACE="
44+
set "VERBOSE=0"
45+
set "APPEND=1"
46+
set "CTL=oc"
47+
set "CTL_DEBUG_FLAGS=-t"
48+
set "IMAGE=quay.io/ibm/containerdiag:latest"
49+
set "TARGETDEPLOYMENT="
50+
set "TARGETPOD="
51+
52+
set "I=0"
53+
for %%x in (%*) do (
54+
set /A "I+=1"
55+
set "ARGS[!I!]=%%~x"
56+
)
57+
58+
set /a "REMAININGINDEX=1"
59+
60+
for /L %%J in (1,1,!I!) do (
61+
if /i "!ARGS[%%J]!" == "/?" goto :usage
62+
if /i "!ARGS[%%J]!" == "-?" goto :usage
63+
if /i "!ARGS[%%J]!" == "-h" goto :usage
64+
if /i "!ARGS[%%J]!" == "--help" goto :usage
65+
66+
set /a "K=%%J+1"
67+
if /i "!ARGS[%%J]!" == "/d" call set "TARGETDEPLOYMENT=%%ARGS[!K!]%%" & set /a "REMAININGINDEX+=2"
68+
if /i "!ARGS[%%J]!" == "-d" call set "TARGETDEPLOYMENT=%%ARGS[!K!]%%" & set /a "REMAININGINDEX+=2"
69+
70+
if /i "!ARGS[%%J]!" == "/p" call set "TARGETPOD=%%ARGS[!K!]%%" & set /a "REMAININGINDEX+=2"
71+
if /i "!ARGS[%%J]!" == "-p" call set "TARGETPOD=%%ARGS[!K!]%%" & set /a "REMAININGINDEX+=2"
72+
73+
if /i "!ARGS[%%J]!" == "/i" call set "IMAGE=%%ARGS[!K!]%%" & set /a "REMAININGINDEX+=2"
74+
if /i "!ARGS[%%J]!" == "-i" call set "IMAGE=%%ARGS[!K!]%%" & set /a "REMAININGINDEX+=2"
75+
76+
if /i "!ARGS[%%J]!" == "/n" call set "NAMESPACE=%%ARGS[!K!]%%" & set /a "REMAININGINDEX+=2"
77+
if /i "!ARGS[%%J]!" == "-n" call set "NAMESPACE=%%ARGS[!K!]%%" & set /a "REMAININGINDEX+=2"
78+
79+
if /i "!ARGS[%%J]!" == "/v" call set "VERBOSE=1" & set /a "REMAININGINDEX+=1"
80+
if /i "!ARGS[%%J]!" == "-v" call set "VERBOSE=1" & set /a "REMAININGINDEX+=1"
81+
82+
if /i "!ARGS[%%J]!" == "/q" call set "APPEND=0" & set /a "REMAININGINDEX+=1"
83+
if /i "!ARGS[%%J]!" == "-q" call set "APPEND=0" & set /a "REMAININGINDEX+=1"
84+
85+
if /i "!ARGS[%%J]!" == "/k" call set "CTL=kubectl" & set "CTL_DEBUG_FLAGS=-it" & set /a "REMAININGINDEX+=1"
86+
if /i "!ARGS[%%J]!" == "-k" call set "CTL=kubectl" & set "CTL_DEBUG_FLAGS=-it" & set /a "REMAININGINDEX+=1"
87+
)
88+
89+
where /q oc
90+
if ERRORLEVEL 1 (
91+
where /q kubectl
92+
if ERRORLEVEL 1 (
93+
echo Could not find the command oc or kubectl on PATH
94+
goto :usage
95+
) else (
96+
set "CTL=kubectl"
97+
set "CTL_DEBUG_FLAGS=-it"
98+
)
99+
)
100+
101+
set "REMAININGARGS="
102+
for /L %%J in (!REMAININGINDEX!,1,!I!) do (
103+
set "REMAININGARGS=!REMAININGARGS! !ARGS[%%J]!"
104+
)
105+
106+
if "!TARGETDEPLOYMENT!" == "" (
107+
if "!TARGETPOD!" == "" (
108+
echo ERROR: Either /d DEPLOYMENT or /p POD must be specified
109+
goto :usage
110+
)
111+
)
112+
113+
if "!REMAININGARGS!" == "" echo ERROR: Missing COMMANDS & goto :usage
114+
115+
call :printInfo Script started with !CTL! and !IMAGE!
116+
117+
if "!VERBOSE!" == "1" call :printVerbose Commands: !REMAININGARGS!
118+
119+
if "!NAMESPACE!" == "" (
120+
for /F "tokens=* USEBACKQ" %%g in (`!CTL! config view --minify --output "jsonpath={..namespace}"`) do set "NAMESPACE=%%g"
121+
if "!NAMESPACE!" == "" set "NAMESPACE=default"
122+
)
123+
124+
if not "!TARGETDEPLOYMENT!" == "" (
125+
call :printInfo Querying available replicas for deployment !TARGETDEPLOYMENT! in namespace !NAMESPACE!
126+
127+
echo ERROR: Only -p POD is implemented
128+
goto :usage
129+
) else (
130+
if not "!TARGETPOD!" == "" (
131+
call :printInfo Querying worker node for pod !TARGETPOD! in namespace !NAMESPACE!
132+
133+
for /F "tokens=* USEBACKQ" %%g in (`!CTL! get pod !TARGETPOD! --namespace !NAMESPACE! --output "jsonpath={.spec.nodeName}"`) do set "WORKER=%%g"
134+
135+
if "!WORKER!" == "" (
136+
call :printInfo Error getting pod information. See previous output. Ensure you specify the right namespace with /n NAMESPACE
137+
exit /B 1
138+
)
139+
140+
call :processPod !REMAININGARGS!
141+
)
142+
)
143+
144+
call :printInfo Finished

0 commit comments

Comments
 (0)