Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support to use label for notify-sigup instead of container name #515

Closed
wants to merge 2 commits into from
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
27 changes: 27 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Docker Image CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Docker Login
env:
DOCKER_USER: ${{secrets.DOCKER_USER}}
DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}}
run: |
docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
- name: Build the Docker image
run: docker build . --file Dockerfile --tag ${{secrets.DOCKER_USER}}/docker-gen:$(date +%s)
- name: Docker Push
run: docker push ${{secrets.DOCKER_USER}}/docker-gen

2 changes: 1 addition & 1 deletion cmd/docker-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func initFlags() {
flag.BoolVar(&notifyOutput, "notify-output", false, "log the output(stdout/stderr) of notify command")
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated (e.g `restart xyz`)")
flag.StringVar(&notifyContainerID, "notify-sighup", "",
"send HUP signal to container. Equivalent to docker kill -s HUP `container-ID`")
"send HUP signal to container. You can either pass a container name/id or a filter key=value to send to multiple containers. Equivalent to docker kill -s HUP `container-ID`")
flag.StringVar(&notifyContainerID, "notify-container", "",
"container to send a signal to")
flag.IntVar(&notifyContainerSignal, "notify-signal", int(docker.SIGHUP),
Expand Down
47 changes: 30 additions & 17 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (g *generator) generateFromContainers() {
continue
}
g.runNotifyCmd(config)
g.sendSignalToContainer(config)
g.sendSignalToContainers(config)
}
}

Expand Down Expand Up @@ -162,7 +162,7 @@ func (g *generator) generateAtInterval() {
// ignore changed return value. always run notify command
template.GenerateFile(cfg, containers)
g.runNotifyCmd(cfg)
g.sendSignalToContainer(cfg)
g.sendSignalToContainers(cfg)
case sig := <-sigChan:
log.Printf("Received signal: %s\n", sig)
switch sig {
Expand Down Expand Up @@ -210,7 +210,7 @@ func (g *generator) generateFromEvents() {
continue
}
g.runNotifyCmd(cfg)
g.sendSignalToContainer(cfg)
g.sendSignalToContainers(cfg)
}
}(cfg)
}
Expand Down Expand Up @@ -332,27 +332,40 @@ func (g *generator) runNotifyCmd(config config.Config) {
}
}

func (g *generator) sendSignalToContainer(config config.Config) {
func (g *generator) sendSignalToContainer(containerID string, signal docker.Signal) {
log.Printf("Sending container '%s' signal '%v'", containerID, signal)
killOpts := docker.KillContainerOptions{
ID: containerID,
Signal: signal,
}
if err := g.Client.KillContainer(killOpts); err != nil {
log.Printf("Error sending signal to container: %s", err)
}
}

func (g *generator) sendSignalToContainers(config config.Config) {
if len(config.NotifyContainers) < 1 {
return
}

for container, signal := range config.NotifyContainers {
log.Printf("Sending container '%s' signal '%v'", container, signal)

if signal == -1 {
if err := g.Client.RestartContainer(container, 10); err != nil {
log.Printf("Error sending restarting container: %s", err)
// Check if the input has a = (filter input requires key/value input) or default back to the container name
if filterSplit := strings.SplitN(container, "=", 2); len(filterSplit) > 1 {
filters := map[string][]string{}
filters[filterSplit[0]] = []string{filterSplit[1]}
listOpts := docker.ListContainersOptions{
Filters: filters,
}
containers, err := g.Client.ListContainers(listOpts)
if err != nil {
log.Printf("Error finding containers to send signal to: %s %s", container, err)
}
for _, container := range containers {
g.sendSignalToContainer(container.ID, signal)
}
return
}

killOpts := docker.KillContainerOptions{
ID: container,
Signal: docker.Signal(signal),
}
if err := g.Client.KillContainer(killOpts); err != nil {
log.Printf("Error sending signal to container: %s", err)
} else {
g.sendSignalToContainer(container, signal)
}
}
}
Expand Down