Skip to content

Updates for new docker APIs #146

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

Merged
merged 4 commits into from
Jan 2, 2016
Merged
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: 2 additions & 4 deletions GLOCKFILE
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
github.com/BurntSushi/toml f87ce853111478914f0bcffa34d43a93643e6eda
github.com/Sirupsen/logrus 6ebb4e7b3c24b9fef150d7693e728cb1ebadf1f5
github.com/docker/docker 2606a2e4d3bf810ec82e373a6cd334e22e504e83
github.com/fsouza/go-dockerclient 54afc1babbc57f075f958f84904251332bd8fd73
github.com/BurntSushi/toml 056c9bc7be7190eaa7715723883caffa5f8fa3e4
github.com/fsouza/go-dockerclient e0d22d30691bcc996eca51f729a4777b8c7dc2a8
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ all: docker-gen

docker-gen:
echo "Building docker-gen"
go build -ldflags "$(LDFLAGS)"
go build -ldflags "$(LDFLAGS)" ./cmd/docker-gen

dist-clean:
rm -rf dist
rm -f docker-gen-linux-*.tar.gz
rm -f docker-gen-darwin-*.tar.gz

dist: dist-clean
mkdir -p dist/linux/amd64 && GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o dist/linux/amd64/docker-gen
mkdir -p dist/linux/i386 && GOOS=linux GOARCH=386 go build -ldflags "$(LDFLAGS)" -o dist/linux/i386/docker-gen
mkdir -p dist/linux/armel && GOOS=linux GOARCH=arm GOARM=5 go build -ldflags "$(LDFLAGS)" -o dist/linux/armel/docker-gen
mkdir -p dist/linux/armhf && GOOS=linux GOARCH=arm GOARM=6 go build -ldflags "$(LDFLAGS)" -o dist/linux/armhf/docker-gen
mkdir -p dist/darwin/amd64 && GOOS=darwin GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o dist/darwin/amd64/docker-gen
mkdir -p dist/darwin/i386 && GOOS=darwin GOARCH=386 go build -ldflags "$(LDFLAGS)" -o dist/darwin/i386/docker-gen
mkdir -p dist/linux/amd64 && GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o dist/linux/amd64/docker-gen ./cmd/docker-gen
mkdir -p dist/linux/i386 && GOOS=linux GOARCH=386 go build -ldflags "$(LDFLAGS)" -o dist/linux/i386/docker-gen ./cmd/docker-gen
mkdir -p dist/linux/armel && GOOS=linux GOARCH=arm GOARM=5 go build -ldflags "$(LDFLAGS)" -o dist/linux/armel/docker-gen ./cmd/docker-gen
mkdir -p dist/linux/armhf && GOOS=linux GOARCH=arm GOARM=6 go build -ldflags "$(LDFLAGS)" -o dist/linux/armhf/docker-gen ./cmd/docker-gen
mkdir -p dist/darwin/amd64 && GOOS=darwin GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o dist/darwin/amd64/docker-gen ./cmd/docker-gen
mkdir -p dist/darwin/i386 && GOOS=darwin GOARCH=386 go build -ldflags "$(LDFLAGS)" -o dist/darwin/i386/docker-gen ./cmd/docker-gen


release: dist
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ type RuntimeContainer struct {
IP string
IP6LinkLocal string
IP6Global string
Mounts []Mount
}

type Address struct {
Expand Down Expand Up @@ -241,6 +242,15 @@ type DockerImage struct {
Tag string
}

type Mount struct {
Name string
Source string
Destination string
Driver string
Mode string
RW bool
}

type Volume struct {
Path string
HostPath string
Expand All @@ -252,6 +262,21 @@ type SwarmNode struct {
Name string
Address Address
}

// Accessible from the root in templates as .Docker
type Docker struct {
Name string
NumContainers int
NumImages int
Version string
ApiVersion string
GoVersion string
OperatingSystem string
Architecture string
}

// Host environment variables accessible from root in templates as .Env

```

For example, this is a JSON version of an emitted RuntimeContainer struct:
Expand Down
165 changes: 165 additions & 0 deletions cmd/docker-gen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"sync"

"github.com/BurntSushi/toml"
docker "github.com/fsouza/go-dockerclient"
"github.com/jwilder/docker-gen"
)

type stringslice []string

var (
buildVersion string
version bool
watch bool
notifyCmd string
notifyOutput bool
notifySigHUPContainerID string
onlyExposed bool
onlyPublished bool
configFiles stringslice
configs dockergen.ConfigFile
interval int
keepBlankLines bool
endpoint string
tlsCert string
tlsKey string
tlsCaCert string
tlsVerify bool
tlsCertPath string
wg sync.WaitGroup
)

func (strings *stringslice) String() string {
return "[]"
}

func (strings *stringslice) Set(value string) error {
// TODO: Throw an error for duplicate `dest`
*strings = append(*strings, value)
return nil
}

func usage() {
println(`Usage: docker-gen [options] template [dest]

Generate files from docker container meta-data

Options:`)
flag.PrintDefaults()

println(`
Arguments:
template - path to a template to generate
dest - path to a write the template. If not specfied, STDOUT is used`)

println(`
Environment Variables:
DOCKER_HOST - default value for -endpoint
DOCKER_CERT_PATH - directory path containing key.pem, cert.pem and ca.pem
DOCKER_TLS_VERIFY - enable client TLS verification
`)
println(`For more information, see https://github.com/jwilder/docker-gen`)
}

func loadConfig(file string) error {
_, err := toml.DecodeFile(file, &configs)
if err != nil {
return err
}
return nil
}

func initFlags() {

certPath := filepath.Join(os.Getenv("DOCKER_CERT_PATH"))
if certPath == "" {
certPath = filepath.Join(os.Getenv("HOME"), ".docker")
}
flag.BoolVar(&version, "version", false, "show version")
flag.BoolVar(&watch, "watch", false, "watch for container changes")
flag.BoolVar(&onlyExposed, "only-exposed", false, "only include containers with exposed ports")

flag.BoolVar(&onlyPublished, "only-published", false,
"only include containers with published ports (implies -only-exposed)")
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(&notifySigHUPContainerID, "notify-sighup", "",
"send HUP signal to container. Equivalent to `docker kill -s HUP container-ID`")
flag.Var(&configFiles, "config", "config files with template directives. Config files will be merged if this option is specified multiple times.")
flag.IntVar(&interval, "interval", 0, "notify command interval (secs)")
flag.BoolVar(&keepBlankLines, "keep-blank-lines", false, "keep blank lines in the output file")
flag.StringVar(&endpoint, "endpoint", "", "docker api endpoint (tcp|unix://..). Default unix:///var/run/docker.sock")
flag.StringVar(&tlsCert, "tlscert", filepath.Join(certPath, "cert.pem"), "path to TLS client certificate file")
flag.StringVar(&tlsKey, "tlskey", filepath.Join(certPath, "key.pem"), "path to TLS client key file")
flag.StringVar(&tlsCaCert, "tlscacert", filepath.Join(certPath, "ca.pem"), "path to TLS CA certificate file")
flag.BoolVar(&tlsVerify, "tlsverify", os.Getenv("DOCKER_TLS_VERIFY") != "", "verify docker daemon's TLS certicate")

flag.Usage = usage
flag.Parse()
}

func main() {
initFlags()

if version {
fmt.Println(buildVersion)
return
}

if flag.NArg() < 1 && len(configFiles) == 0 {
usage()
os.Exit(1)
}

if len(configFiles) > 0 {
for _, configFile := range configFiles {
err := loadConfig(configFile)
if err != nil {
log.Fatalf("error loading config %s: %s\n", configFile, err)
}
}
} else {
config := dockergen.Config{
Template: flag.Arg(0),
Dest: flag.Arg(1),
Watch: watch,
NotifyCmd: notifyCmd,
NotifyOutput: notifyOutput,
NotifyContainers: make(map[string]docker.Signal),
OnlyExposed: onlyExposed,
OnlyPublished: onlyPublished,
Interval: interval,
KeepBlankLines: keepBlankLines,
}
if notifySigHUPContainerID != "" {
config.NotifyContainers[notifySigHUPContainerID] = docker.SIGHUP
}
configs = dockergen.ConfigFile{
Config: []dockergen.Config{config}}
}

generator, err := dockergen.NewGenerator(dockergen.GeneratorConfig{
Endpoint: endpoint,
TLSKey: tlsKey,
TLSCert: tlsCert,
TLSCACert: tlsCaCert,
TLSVerify: tlsVerify,
ConfigFile: configs,
})

if err != nil {
log.Fatalf("error creating generator: %v", err)
}

if err := generator.Generate(); err != nil {
log.Fatalf("error running generate: %v", err)
}
}
33 changes: 33 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dockergen

import "github.com/fsouza/go-dockerclient"

type Config struct {
Template string
Dest string
Watch bool
NotifyCmd string
NotifyOutput bool
NotifyContainers map[string]docker.Signal
OnlyExposed bool
OnlyPublished bool
Interval int
KeepBlankLines bool
}

type ConfigFile struct {
Config []Config
}

func (c *ConfigFile) FilterWatches() ConfigFile {
configWithWatches := []Config{}

for _, config := range c.Config {
if config.Watch {
configWithWatches = append(configWithWatches, config)
}
}
return ConfigFile{
Config: configWithWatches,
}
}
Loading