|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "sync" |
| 10 | + |
| 11 | + "github.com/BurntSushi/toml" |
| 12 | + docker "github.com/fsouza/go-dockerclient" |
| 13 | + "github.com/jwilder/docker-gen" |
| 14 | +) |
| 15 | + |
| 16 | +type stringslice []string |
| 17 | + |
| 18 | +var ( |
| 19 | + buildVersion string |
| 20 | + version bool |
| 21 | + watch bool |
| 22 | + notifyCmd string |
| 23 | + notifyOutput bool |
| 24 | + notifySigHUPContainerID string |
| 25 | + onlyExposed bool |
| 26 | + onlyPublished bool |
| 27 | + configFiles stringslice |
| 28 | + configs dockergen.ConfigFile |
| 29 | + interval int |
| 30 | + keepBlankLines bool |
| 31 | + endpoint string |
| 32 | + tlsCert string |
| 33 | + tlsKey string |
| 34 | + tlsCaCert string |
| 35 | + tlsVerify bool |
| 36 | + tlsCertPath string |
| 37 | + wg sync.WaitGroup |
| 38 | +) |
| 39 | + |
| 40 | +func (strings *stringslice) String() string { |
| 41 | + return "[]" |
| 42 | +} |
| 43 | + |
| 44 | +func (strings *stringslice) Set(value string) error { |
| 45 | + // TODO: Throw an error for duplicate `dest` |
| 46 | + *strings = append(*strings, value) |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func usage() { |
| 51 | + println(`Usage: docker-gen [options] template [dest] |
| 52 | +
|
| 53 | +Generate files from docker container meta-data |
| 54 | +
|
| 55 | +Options:`) |
| 56 | + flag.PrintDefaults() |
| 57 | + |
| 58 | + println(` |
| 59 | +Arguments: |
| 60 | + template - path to a template to generate |
| 61 | + dest - path to a write the template. If not specfied, STDOUT is used`) |
| 62 | + |
| 63 | + println(` |
| 64 | +Environment Variables: |
| 65 | + DOCKER_HOST - default value for -endpoint |
| 66 | + DOCKER_CERT_PATH - directory path containing key.pem, cert.pem and ca.pem |
| 67 | + DOCKER_TLS_VERIFY - enable client TLS verification |
| 68 | +`) |
| 69 | + println(`For more information, see https://github.com/jwilder/docker-gen`) |
| 70 | +} |
| 71 | + |
| 72 | +func loadConfig(file string) error { |
| 73 | + _, err := toml.DecodeFile(file, &configs) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func initFlags() { |
| 81 | + |
| 82 | + certPath := filepath.Join(os.Getenv("DOCKER_CERT_PATH")) |
| 83 | + if certPath == "" { |
| 84 | + certPath = filepath.Join(os.Getenv("HOME"), ".docker") |
| 85 | + } |
| 86 | + flag.BoolVar(&version, "version", false, "show version") |
| 87 | + flag.BoolVar(&watch, "watch", false, "watch for container changes") |
| 88 | + flag.BoolVar(&onlyExposed, "only-exposed", false, "only include containers with exposed ports") |
| 89 | + |
| 90 | + flag.BoolVar(&onlyPublished, "only-published", false, |
| 91 | + "only include containers with published ports (implies -only-exposed)") |
| 92 | + flag.BoolVar(¬ifyOutput, "notify-output", false, "log the output(stdout/stderr) of notify command") |
| 93 | + flag.StringVar(¬ifyCmd, "notify", "", "run command after template is regenerated (e.g `restart xyz`)") |
| 94 | + flag.StringVar(¬ifySigHUPContainerID, "notify-sighup", "", |
| 95 | + "send HUP signal to container. Equivalent to `docker kill -s HUP container-ID`") |
| 96 | + flag.Var(&configFiles, "config", "config files with template directives. Config files will be merged if this option is specified multiple times.") |
| 97 | + flag.IntVar(&interval, "interval", 0, "notify command interval (secs)") |
| 98 | + flag.BoolVar(&keepBlankLines, "keep-blank-lines", false, "keep blank lines in the output file") |
| 99 | + flag.StringVar(&endpoint, "endpoint", "", "docker api endpoint (tcp|unix://..). Default unix:///var/run/docker.sock") |
| 100 | + flag.StringVar(&tlsCert, "tlscert", filepath.Join(certPath, "cert.pem"), "path to TLS client certificate file") |
| 101 | + flag.StringVar(&tlsKey, "tlskey", filepath.Join(certPath, "key.pem"), "path to TLS client key file") |
| 102 | + flag.StringVar(&tlsCaCert, "tlscacert", filepath.Join(certPath, "ca.pem"), "path to TLS CA certificate file") |
| 103 | + flag.BoolVar(&tlsVerify, "tlsverify", os.Getenv("DOCKER_TLS_VERIFY") != "", "verify docker daemon's TLS certicate") |
| 104 | + |
| 105 | + flag.Usage = usage |
| 106 | + flag.Parse() |
| 107 | +} |
| 108 | + |
| 109 | +func main() { |
| 110 | + initFlags() |
| 111 | + |
| 112 | + if version { |
| 113 | + fmt.Println(buildVersion) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + if flag.NArg() < 1 && len(configFiles) == 0 { |
| 118 | + usage() |
| 119 | + os.Exit(1) |
| 120 | + } |
| 121 | + |
| 122 | + if len(configFiles) > 0 { |
| 123 | + for _, configFile := range configFiles { |
| 124 | + err := loadConfig(configFile) |
| 125 | + if err != nil { |
| 126 | + log.Fatalf("error loading config %s: %s\n", configFile, err) |
| 127 | + } |
| 128 | + } |
| 129 | + } else { |
| 130 | + config := dockergen.Config{ |
| 131 | + Template: flag.Arg(0), |
| 132 | + Dest: flag.Arg(1), |
| 133 | + Watch: watch, |
| 134 | + NotifyCmd: notifyCmd, |
| 135 | + NotifyOutput: notifyOutput, |
| 136 | + NotifyContainers: make(map[string]docker.Signal), |
| 137 | + OnlyExposed: onlyExposed, |
| 138 | + OnlyPublished: onlyPublished, |
| 139 | + Interval: interval, |
| 140 | + KeepBlankLines: keepBlankLines, |
| 141 | + } |
| 142 | + if notifySigHUPContainerID != "" { |
| 143 | + config.NotifyContainers[notifySigHUPContainerID] = docker.SIGHUP |
| 144 | + } |
| 145 | + configs = dockergen.ConfigFile{ |
| 146 | + Config: []dockergen.Config{config}} |
| 147 | + } |
| 148 | + |
| 149 | + generator, err := dockergen.NewGenerator(dockergen.GeneratorConfig{ |
| 150 | + Endpoint: endpoint, |
| 151 | + TLSKey: tlsKey, |
| 152 | + TLSCert: tlsCert, |
| 153 | + TLSCACert: tlsCaCert, |
| 154 | + TLSVerify: tlsVerify, |
| 155 | + ConfigFile: configs, |
| 156 | + }) |
| 157 | + |
| 158 | + if err != nil { |
| 159 | + log.Fatalf("error creating generator: %v", err) |
| 160 | + } |
| 161 | + |
| 162 | + if err := generator.Generate(); err != nil { |
| 163 | + log.Fatalf("error running generate: %v", err) |
| 164 | + } |
| 165 | +} |
0 commit comments