Skip to content

only trigger by container events #2

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
NAME=dockerhook
HARDWARE=$(shell uname -m)
VERSION=0.1.0
VERSION=0.1.1

build:
go get || true && go build

release:
rm -rf release
mkdir release
GOOS=linux go build -o release/$(NAME)
GOOS=linux go build -ldflags "-X main.Version=$(VERSION)" -o release/$(NAME)
cd release && tar -zcf $(NAME)_$(VERSION)_linux_$(HARDWARE).tgz $(NAME)
GOOS=darwin go build -o release/$(NAME)
GOOS=darwin go build -ldflags "-X main.Version=$(VERSION)" -o release/$(NAME)
cd release && tar -zcf $(NAME)_$(VERSION)_darwin_$(HARDWARE).tgz $(NAME)
rm release/$(NAME)
echo "$(VERSION)" > release/version
Expand Down
40 changes: 27 additions & 13 deletions dockerhook.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package main

import (
"log"
"os"
"bytes"
"encoding/json"
"flag"
"strings"
"fmt"
"path/filepath"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"bytes"
"encoding/json"

"github.com/flynn/go-shlex"
dockerapi "github.com/fsouza/go-dockerclient"
)

var debug = flag.Bool("d", false, "debug mode displays handler output")
var shell = flag.Bool("s", false, "run handler via SHELL")
var printVerrion = flag.Bool("v", false, "print version and exit")

var skipInspect = map[string]bool {
"destroy": true,
"untag": true,
"delete": true,
var Version string

var skipInspect = map[string]bool{
"destroy": true,
"untag": true,
"delete": true,
}

func init() {
Expand Down Expand Up @@ -81,7 +84,7 @@ func trigger(hook []string, event, id string, docker *dockerapi.Client) {
var cmd *exec.Cmd
if *shell && os.Getenv("SHELL") != "" {
cmd = exec.Command(os.Getenv("SHELL"), "-c", strings.Join(hook, " "))
} else {
} else {
cmd = exec.Command(hook[0], hook[1:]...)
}
if !skipInspect[event] {
Expand All @@ -99,6 +102,11 @@ func trigger(hook []string, event, id string, docker *dockerapi.Client) {

func main() {
flag.Parse()
if *printVerrion {
fmt.Println("version:", Version)
os.Exit(0)
}

if flag.NArg() < 1 {
flag.Usage()
os.Exit(64)
Expand Down Expand Up @@ -128,9 +136,15 @@ func main() {
events := make(chan *dockerapi.APIEvents)
assert(docker.AddEventListener(events))
log.Println("info: listening for Docker events...")
filter := "container"
if os.Getenv("DOCKER_EVENT_FILTER") != "" {
filter = os.Getenv("DOCKER_EVENT_FILTER")
}
for msg := range events {
go trigger(hook, msg.Status, msg.ID, docker)
if msg.Type == filter || filter == "all" {
go trigger(hook, msg.Status, msg.ID, docker)
}
}

log.Fatal("fatal: docker event loop closed") // todo: reconnect?
}
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/progrium/dockerhook

go 1.15

require (
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/fsouza/go-dockerclient v1.7.4 // indirect
)
Loading