Skip to content

Commit a13b532

Browse files
jyf111caoxianfei1
authored andcommitted
[fix] older versions of the ss command do not have the --no-header option
solution: execute the ss command in a temporary container to precheck for ports in use instead Signed-off-by: jyf111 <[email protected]>
1 parent 28dd6b0 commit a13b532

File tree

1 file changed

+91
-15
lines changed

1 file changed

+91
-15
lines changed

internal/task/task/checker/network.go

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const (
4343
FORMAT_FILTER_SPORT = "( sport = :%d )"
4444

4545
HTTP_SERVER_CONTAINER_NAME = "curveadm-precheck-nginx"
46+
CHECK_PORT_CONTAINER_NAME = "curveadm-precheck-port"
4647
)
4748

4849
// TASK: check port in use
@@ -71,6 +72,53 @@ func joinPorts(dc *topology.DeployConfig, addresses []Address) string {
7172
return strings.Join(ports, ",")
7273
}
7374

75+
func getCheckPortContainerName(curveadm *cli.CurveAdm, dc *topology.DeployConfig) string {
76+
return fmt.Sprintf("%s-%s-%s",
77+
CHECK_PORT_CONTAINER_NAME,
78+
dc.GetRole(),
79+
curveadm.GetServiceId(dc.GetId()))
80+
}
81+
82+
type step2CheckPortStatus struct {
83+
containerId *string
84+
success *bool
85+
dc *topology.DeployConfig
86+
curveadm *cli.CurveAdm
87+
port int
88+
}
89+
90+
// execute the "ss" command within a temporary container
91+
func (s *step2CheckPortStatus) Execute(ctx *context.Context) error {
92+
filter := fmt.Sprintf(FORMAT_FILTER_SPORT, s.port)
93+
cli := ctx.Module().Shell().SocketStatistics(filter)
94+
cli.AddOption("--no-header")
95+
cli.AddOption("--listening")
96+
command, err := cli.String()
97+
if err != nil {
98+
return err
99+
}
100+
101+
var out string
102+
steps := []task.Step{}
103+
steps = append(steps, &step.ContainerExec{
104+
ContainerId: s.containerId,
105+
Command: command,
106+
Out: &out,
107+
ExecOptions: s.curveadm.ExecOptions(),
108+
})
109+
steps = append(steps, &step.Lambda{
110+
Lambda: checkPortInUse(s.success, &out, s.dc.GetHost(), s.port),
111+
})
112+
113+
for _, step := range steps {
114+
err := step.Execute(ctx)
115+
if err != nil {
116+
return err
117+
}
118+
}
119+
return nil
120+
}
121+
74122
func NewCheckPortInUseTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*task.Task, error) {
75123
hc, err := curveadm.GetHost(dc.GetHost())
76124
if err != nil {
@@ -82,19 +130,35 @@ func NewCheckPortInUseTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (*
82130
dc.GetHost(), dc.GetRole(), joinPorts(dc, addresses))
83131
t := task.NewTask("Check Port In Use <network>", subname, hc.GetSSHConfig())
84132

85-
var out string
133+
var containerId, out string
86134
var success bool
135+
t.AddStep(&step.PullImage{
136+
Image: dc.GetContainerImage(),
137+
ExecOptions: curveadm.ExecOptions(),
138+
})
139+
t.AddStep(&step.CreateContainer{
140+
Image: dc.GetContainerImage(),
141+
Command: "-c 'sleep infinity'", // keep the container running
142+
Entrypoint: "/bin/bash",
143+
Name: getCheckPortContainerName(curveadm, dc),
144+
Remove: true,
145+
Out: &containerId,
146+
ExecOptions: curveadm.ExecOptions(),
147+
})
148+
t.AddStep(&step.StartContainer{
149+
ContainerId: &containerId,
150+
Success: &success,
151+
Out: &out,
152+
ExecOptions: curveadm.ExecOptions(),
153+
})
154+
87155
for _, address := range addresses {
88-
t.AddStep(&step.SocketStatistics{
89-
Filter: fmt.Sprintf(FORMAT_FILTER_SPORT, address.Port),
90-
Listening: true,
91-
NoHeader: true,
92-
Success: &success,
93-
Out: &out,
94-
ExecOptions: curveadm.ExecOptions(),
95-
})
96-
t.AddStep(&step.Lambda{
97-
Lambda: checkPortInUse(&success, &out, dc.GetHost(), address.Port),
156+
t.AddStep(&step2CheckPortStatus{
157+
containerId: &containerId,
158+
success: &success,
159+
dc: dc,
160+
curveadm: curveadm,
161+
port: address.Port,
98162
})
99163
}
100164

@@ -164,7 +228,7 @@ func getNginxListens(dc *topology.DeployConfig) string {
164228
return strings.Join(listens, " ")
165229
}
166230

167-
func getContainerName(curveadm *cli.CurveAdm, dc *topology.DeployConfig) string {
231+
func getHTTPServerContainerName(curveadm *cli.CurveAdm, dc *topology.DeployConfig) string {
168232
return fmt.Sprintf("%s-%s-%s",
169233
HTTP_SERVER_CONTAINER_NAME,
170234
dc.GetRole(),
@@ -204,7 +268,7 @@ func NewStartHTTPServerTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) (
204268
Image: dc.GetContainerImage(),
205269
Command: command,
206270
Entrypoint: "/bin/bash",
207-
Name: getContainerName(curveadm, dc),
271+
Name: getHTTPServerContainerName(curveadm, dc),
208272
Remove: true,
209273
Out: &containerId,
210274
ExecOptions: curveadm.ExecOptions(),
@@ -283,7 +347,7 @@ func NewCheckNetworkFirewallTask(curveadm *cli.CurveAdm, dc *topology.DeployConf
283347
return t, nil
284348
}
285349

286-
// TASK: stop http server
350+
// TASK: stop container
287351
type step2StopContainer struct {
288352
containerId *string
289353
dc *topology.DeployConfig
@@ -332,7 +396,19 @@ func NewCleanEnvironmentTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig)
332396
t.AddStep(&step.ListContainers{
333397
ShowAll: true,
334398
Format: `"{{.ID}}"`,
335-
Filter: fmt.Sprintf("name=%s", getContainerName(curveadm, dc)),
399+
Filter: fmt.Sprintf("name=%s", getCheckPortContainerName(curveadm, dc)),
400+
Out: &out,
401+
ExecOptions: curveadm.ExecOptions(),
402+
})
403+
t.AddStep(&step2StopContainer{
404+
containerId: &out,
405+
dc: dc,
406+
curveadm: curveadm,
407+
})
408+
t.AddStep(&step.ListContainers{
409+
ShowAll: true,
410+
Format: `"{{.ID}}"`,
411+
Filter: fmt.Sprintf("name=%s", getHTTPServerContainerName(curveadm, dc)),
336412
Out: &out,
337413
ExecOptions: curveadm.ExecOptions(),
338414
})

0 commit comments

Comments
 (0)