|
1 | 1 | package generator |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "fmt" |
5 | 6 | "log" |
6 | 7 | "os" |
@@ -217,6 +218,7 @@ func (g *generator) generateFromEvents() { |
217 | 218 | g.runNotifyCmd(cfg) |
218 | 219 | g.sendSignalToContainers(cfg) |
219 | 220 | g.sendSignalToFilteredContainers(cfg) |
| 221 | + g.sendCmdToContainers(cfg) |
220 | 222 | } |
221 | 223 | }(cfg) |
222 | 224 | } |
@@ -388,6 +390,54 @@ func (g *generator) sendSignalToFilteredContainers(config config.Config) { |
388 | 390 | } |
389 | 391 | } |
390 | 392 |
|
| 393 | +func (g *generator) sendCmdToContainers(config config.Config) { |
| 394 | + if len(config.NotifyContainersCmd) < 1 { |
| 395 | + return |
| 396 | + } |
| 397 | + for container, cmd := range config.NotifyContainersCmd { |
| 398 | + log.Printf("Sending container '%s' cmd '%s'", container, cmd) |
| 399 | + |
| 400 | + createOpts := docker.CreateExecOptions{ |
| 401 | + Container: container, |
| 402 | + AttachStdout: true, |
| 403 | + AttachStderr: true, |
| 404 | + Tty: true, |
| 405 | + Cmd: cmd, |
| 406 | + } |
| 407 | + |
| 408 | + execObj, err := g.Client.CreateExec(createOpts) |
| 409 | + if err != nil { |
| 410 | + log.Printf("Error creating cmd execution: %s", err) |
| 411 | + continue |
| 412 | + } |
| 413 | + |
| 414 | + var stdout bytes.Buffer |
| 415 | + |
| 416 | + startOpts := docker.StartExecOptions{ |
| 417 | + OutputStream: &stdout, |
| 418 | + Tty: true, |
| 419 | + RawTerminal: true, |
| 420 | + } |
| 421 | + |
| 422 | + err = g.Client.StartExec(execObj.ID, startOpts) |
| 423 | + if err != nil { |
| 424 | + log.Printf("Error executing command for container %s: %v", container, err) |
| 425 | + continue |
| 426 | + } |
| 427 | + |
| 428 | + inspect, err := g.Client.InspectExec(execObj.ID) |
| 429 | + if err != nil { |
| 430 | + log.Printf("Error inspecting exec for container %s: %v", container, err) |
| 431 | + continue |
| 432 | + } |
| 433 | + if inspect.ExitCode != 0 { |
| 434 | + log.Printf("Command failed on container %s. Exit Code: %d. Output: %s", container, inspect.ExitCode, stdout.String()) |
| 435 | + } else { |
| 436 | + log.Printf("Command executed successfully on container %s. Output: %s", container, stdout.String()) |
| 437 | + } |
| 438 | + } |
| 439 | +} |
| 440 | + |
391 | 441 | func (g *generator) getContainers(config config.Config) ([]*context.RuntimeContainer, error) { |
392 | 442 | apiInfo, err := g.Client.Info() |
393 | 443 | if err != nil { |
|
0 commit comments