Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,18 @@ dest = "/etc/nginx/conf.d/default.conf"
watch = true
wait = "500ms:2s"

[[config]]
template = "/etc/docker-gen/templates/cnames.tmpl"
dest = "etc/dnsmasq.d/05-pihole-custom-cname.conf"
watch = true
wait = "500ms:2s"

[config.NotifyContainers]
nginx = 1 # 1 is a signal number to be sent; here SIGHUP
e75a60548dc9 = 1 # a key can be either container name (nginx) or ID

[config.NotifyContainersCmd]
pihole = ["pihole", "reloaddns"]
```

---
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Config struct {
NotifyContainers map[string]int
NotifyContainersFilter map[string][]string
NotifyContainersSignal int
NotifyContainersCmd map[string][]string
ContainerFilter map[string][]string
Interval int
KeepBlankLines bool
Expand Down
50 changes: 50 additions & 0 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generator

import (
"bytes"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -217,6 +218,7 @@
g.runNotifyCmd(cfg)
g.sendSignalToContainers(cfg)
g.sendSignalToFilteredContainers(cfg)
g.sendCmdToContainers(cfg)
}
}(cfg)
}
Expand Down Expand Up @@ -388,6 +390,54 @@
}
}

func (g *generator) sendCmdToContainers(config config.Config) {
if len(config.NotifyContainersCmd) < 1 {
return
}
for container, cmd := range config.NotifyContainersCmd {
log.Printf("Sending container '%s' cmd '%s'", container, cmd)

createOpts := docker.CreateExecOptions{
Container: container,
AttachStdout: true,
AttachStderr: true,
Tty: true,
Cmd: cmd,
}

execObj, err := g.Client.CreateExec(createOpts)
if err != nil {
log.Printf("Error creating cmd execution: %s", err)
continue
}

var stdout bytes.Buffer

startOpts := docker.StartExecOptions{
OutputStream: &stdout,
Tty: true,
RawTerminal: true,
}

err := g.Client.StartExec(execObj.ID, startOpts)

Check failure on line 422 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu/go-1.24)

no new variables on left side of :=

Check failure on line 422 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / Unit Tests (windows/go-1.24)

no new variables on left side of :=

Check failure on line 422 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / Unit Tests (macos/go-1.24)

no new variables on left side of :=

Check failure on line 422 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu/go-1.25)

no new variables on left side of :=

Check failure on line 422 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / Unit Tests (windows/go-1.25)

no new variables on left side of :=

Check failure on line 422 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / Unit Tests (macos/go-1.25)

no new variables on left side of :=
if err != nil {
log.Printf("Error executing command for container %s: %v", container, execErr)

Check failure on line 424 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu/go-1.24)

undefined: execErr

Check failure on line 424 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / Unit Tests (windows/go-1.24)

undefined: execErr

Check failure on line 424 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / Unit Tests (macos/go-1.24)

undefined: execErr

Check failure on line 424 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu/go-1.25)

undefined: execErr

Check failure on line 424 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / Unit Tests (windows/go-1.25)

undefined: execErr

Check failure on line 424 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / Unit Tests (macos/go-1.25)

undefined: execErr
continue
}

inspect, err := g.Client.InspectExec(execObj.ID)
if err != nil {
log.Printf("Error inspecting exec for container %s: %v", container, err)
continue
}
if inspect.ExitCode != 0 {
log.Printf("Command failed on container %s. Exit Code: %d. Output: %s", container, inspect.ExitCode, stdout.String())
} else {
log.Printf("Command executed successfully on container %s. Output: %s", container, stdout.String())
}
}
}

func (g *generator) getContainers(config config.Config) ([]*context.RuntimeContainer, error) {
apiInfo, err := g.Client.Info()
if err != nil {
Expand Down
Loading