From d36d8215601a1d389bcc874f25796442776eda61 Mon Sep 17 00:00:00 2001 From: Bilal Amarni Date: Mon, 18 Apr 2016 11:08:34 +0200 Subject: [PATCH] use libmachine directly --- README.md | 42 +++++++++++++----------------------------- dockness.go | 40 ++++++++++++++++------------------------ 2 files changed, 29 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index c51f87b..a546db4 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ DNS for Docker machines, allows to access them with the following domain format It spins up a simplistic DNS server, only listening for questions about A records. -Behind the scene it will run `docker-machine ip {machine}` in order to resolve the IP address of a given machine. +Behind the scene it will use [libmachine](https://github.com/docker/machine) in order to resolve IP addresses. ## Installation @@ -21,33 +21,31 @@ Prebuilt binaries are available in the [releases](https://github.com/bamarni/doc ### From source (requires Go) - go get github.com/miekg/dns - go get github.com/bamarni/dockness + go get github.com/miekg/dns github.com/docker/machine github.com/bamarni/dockness ## Usage dockness [options...] Options: - -tld Top-level domain to use (defaults to "docker") - -ttl Time to Live for DNS records (defaults to 0) - -port Port to listen on (defaults to "53") - -server-only Server only, doesn't try to create a resolver configuration file - -user Execute the "docker-machine ip" command as a different user (defaults to "SUDO_USER") + -tld Top-level domain to use (defaults to "docker") + -ttl Time to Live for DNS records (defaults to 0) + -port Port to listen on (defaults to "53") + -debug Enable debugging (defaults to false) ### Mac OSX To develop on Mac you probably have a local VM, using VirtualBox for example. However this machine gets assigned a dynamic IP address. -The program can be up and running in one command : +You can be up and running in a few commands, first : - > sudo dockness - 2016/02/18 10:39:52 Creating configuration file at /etc/resolver/docker... - 2016/02/18 10:39:52 Listening on :53... + > echo "nameserver 127.0.0.1\nport 10053" | sudo tee /etc/resolver/docker -*If you don't want to run the program as root, you can create the resolver file yourself and use a high port. -Cf. example in the [Configure as a service](#configure-dockness-as-a-service) section.* +It tells your Mac that the resolver for `.docker` TLD listens locally on port 10053. You can now run the resolver on this port : + + > dockness -port 10053 + 2016/02/18 10:39:52 Listening on :10053... ### Linux @@ -77,11 +75,7 @@ Doing so, it will be running in the background automatically when booting your c ### Mac OSX -Run this command : `echo "nameserver 127.0.0.1\nport 10053" | sudo tee /etc/resolver/docker` - -It tells your Mac that the resolver for `.docker` TLD listens locally on port 10053. - -You can now create the appropriate service configuration file at `~/Library/LaunchAgents/local.dockness.plist` : +You can create the appropriate service configuration file at `~/Library/LaunchAgents/local.dockness.plist` : ``` xml @@ -90,17 +84,11 @@ You can now create the appropriate service configuration file at `~/Library/Laun Disabled - EnvironmentVariables - - PATH - /usr/local/bin - Label local.dockness ProgramArguments /path/to/dockness - -server-only -port 10053 @@ -110,10 +98,6 @@ You can now create the appropriate service configuration file at `~/Library/Laun ``` -You'll have to adapt 2 parameters : -- `/path/to/dockness` -- `/usr/local/bin`, which is the directory containing your `docker-machine` executable - Finally, the service can be enabled : launchctl load ~/Library/LaunchAgents/local.dockness.plist diff --git a/dockness.go b/dockness.go index 60190f6..281aef6 100644 --- a/dockness.go +++ b/dockness.go @@ -2,17 +2,18 @@ package main import ( "flag" + "github.com/docker/machine/commands/mcndirs" + "github.com/docker/machine/libmachine" + mcnlog "github.com/docker/machine/libmachine/log" "github.com/miekg/dns" - "io/ioutil" "log" "net" "os" - "os/exec" "os/signal" - "runtime" "strings" ) +var api *libmachine.Client var ttl uint var user string @@ -33,21 +34,19 @@ func lookup(w dns.ResponseWriter, r *dns.Msg) { log.Printf("Couldn't parse the DNS question '%s'", q.Name) continue } - machine := domLevels[len(domLevels)-3] - - var output []byte - var err error - if user == "" { - output, err = exec.Command("docker-machine", "ip", machine).CombinedOutput() - } else { - output, err = exec.Command("sudo", "-u", user, "docker-machine", "ip", machine).CombinedOutput() + machineName := domLevels[len(domLevels)-3] + + machine, err := api.Load(machineName) + if err != nil { + log.Printf("Couldn't load machine '%s' : %s", machineName, err) + continue } + ip, err := machine.Driver.GetIP() if err != nil { - log.Printf("No IP found for machine '%s' (%s)", machine, output) + log.Printf("Couldn't find IP for machine '%s' : %s", machineName, err) continue } - ip := string(output[:len(output)-1]) rr = &dns.A{ Hdr: dns.RR_Header{ @@ -69,19 +68,12 @@ func main() { tld := flag.String("tld", "docker", "Top-level domain to use") flag.UintVar(&ttl, "ttl", 0, "Time to Live for DNS records") port := flag.String("port", "53", "Port to listen on") - serverOnly := flag.Bool("server-only", false, "Server only, doesn't try to create a resolver configuration") - flag.StringVar(&user, "user", os.Getenv("SUDO_USER"), "Execute the 'docker-machine ip' command as this user") + debug := flag.Bool("debug", false, "Enable debugging") flag.Parse() - if *serverOnly == false && runtime.GOOS == "darwin" { - confPath := "/etc/resolver/" + *tld - log.Printf("Creating configuration file at %s...", confPath) - conf := []byte("nameserver 127.0.0.1\nport " + *port + "\n") - if err := ioutil.WriteFile(confPath, conf, 0644); err != nil { - log.Fatalf("Could not create configuration file: %s", err) - } - defer os.Remove(confPath) - } + mcnlog.SetDebug(*debug) + api = libmachine.NewClient(mcndirs.GetBaseDir(), mcndirs.GetMachineCertDir()) + defer api.Close() addr := ":" + *port server := &dns.Server{