Skip to content

Commit d4c80c0

Browse files
committed
feat: Adding new Send CMD notification option that can use exec to execute a command on a container
1 parent f43ba86 commit d4c80c0

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,18 @@ dest = "/etc/nginx/conf.d/default.conf"
223223
watch = true
224224
wait = "500ms:2s"
225225

226+
[[config]]
227+
template = "/etc/docker-gen/templates/cnames.tmpl"
228+
dest = "etc/dnsmasq.d/05-pihole-custom-cname.conf"
229+
watch = true
230+
wait = "500ms:2s"
231+
226232
[config.NotifyContainers]
227233
nginx = 1 # 1 is a signal number to be sent; here SIGHUP
228234
e75a60548dc9 = 1 # a key can be either container name (nginx) or ID
235+
236+
[config.NotifyContainersCmd]
237+
pihole = ["pihole", "reloaddns"]
229238
```
230239

231240
---

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Config struct {
1616
NotifyContainers map[string]int
1717
NotifyContainersFilter map[string][]string
1818
NotifyContainersSignal int
19+
NotifyContainersCmd map[string][]string
1920
ContainerFilter map[string][]string
2021
Interval int
2122
KeepBlankLines bool

internal/generator/generator.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package generator
22

33
import (
4+
"bytes"
45
"fmt"
56
"log"
67
"os"
@@ -217,6 +218,7 @@ func (g *generator) generateFromEvents() {
217218
g.runNotifyCmd(cfg)
218219
g.sendSignalToContainers(cfg)
219220
g.sendSignalToFilteredContainers(cfg)
221+
g.sendCmdToContainers(cfg)
220222
}
221223
}(cfg)
222224
}
@@ -388,6 +390,54 @@ func (g *generator) sendSignalToFilteredContainers(config config.Config) {
388390
}
389391
}
390392

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+
391441
func (g *generator) getContainers(config config.Config) ([]*context.RuntimeContainer, error) {
392442
apiInfo, err := g.Client.Info()
393443
if err != nil {

0 commit comments

Comments
 (0)