diff --git a/README.md b/README.md index 4d2fa46..d835453 100644 --- a/README.md +++ b/README.md @@ -111,9 +111,15 @@ Finally, the service can be enabled : Here again, it will depend on your Linux distribution. We'll take as example [Systemd](https://freedesktop.org/wiki/Software/systemd/), -which is nowadays the default init system in Ubuntu/Debian. +which is nowadays the default init system in most distributions. Create the following file at `/etc/systemd/system/dockness.service`: - TODO + [Unit] + Description=Dockness + [Service] + ExecStart=/path/to/dockness -port 10053 + + [Install] + WantedBy=multi-user.target diff --git a/dockness.go b/dockness.go index 0fd4172..1d9b6b5 100644 --- a/dockness.go +++ b/dockness.go @@ -1,12 +1,14 @@ package main import ( + "encoding/json" "flag" + "fmt" "github.com/docker/machine/commands/mcndirs" "github.com/docker/machine/libmachine" - mcnhost "github.com/docker/machine/libmachine/host" - mcnlog "github.com/docker/machine/libmachine/log" + "github.com/docker/machine/libmachine/drivers" "github.com/miekg/dns" + //"github.com/pkg/profile" "log" "net" "os" @@ -16,57 +18,56 @@ import ( var api *libmachine.Client var ttl uint -var machines = make(map[string]*mcnhost.Host) + +func findIp(machineName string) (string, error) { + machine, err := api.Filestore.Load(machineName) + if err != nil { + return "", fmt.Errorf("couldn't load machine : %s", err) + } + + var baseDriver drivers.BaseDriver + err = json.Unmarshal(machine.RawDriver, &baseDriver) + if err != nil { + return "", fmt.Errorf("couldn't load driver %s", machine.DriverName) + } + + return baseDriver.IPAddress, nil +} func lookup(w dns.ResponseWriter, r *dns.Msg) { m := new(dns.Msg) - m.SetReply(r) + if m.Question[0].Qtype != dns.TypeA { + return + } + + domLevels := strings.Split(m.Question[0].Name, ".") + domLevelsLen := len(domLevels) + if domLevelsLen < 3 { + log.Printf("Couldn't parse the DNS question '%s'", m.Question[0].Name) + return + } - var rr dns.RR - for _, q := range m.Question { - if q.Qtype != dns.TypeA { - continue - } - - domLevels := strings.Split(q.Name, ".") - domLevelsLen := len(domLevels) - if domLevelsLen < 3 { - mcnlog.Debugf("Couldn't parse the DNS question '%s'", q.Name) - continue - } - machineName := domLevels[len(domLevels)-3] - - if machines[machineName] == nil { - machine, err := api.Load(machineName) - if err != nil { - mcnlog.Debugf("Couldn't load machine '%s' : %s", machineName, err) - continue - } - machines[machineName] = machine - } - - ip, err := machines[machineName].Driver.GetIP() - if err != nil { - mcnlog.Debugf("Couldn't find IP for machine '%s' : %s", machineName, err) - continue - } - - mcnlog.Debugf("Found IP %s for machine '%s'", ip, machineName) - - rr = &dns.A{ - Hdr: dns.RR_Header{ - Name: q.Name, - Rrtype: dns.TypeA, - Class: dns.ClassINET, - Ttl: uint32(ttl), - }, - A: net.ParseIP(ip).To4(), - } - - m.Answer = append(m.Answer, rr) + machineName := domLevels[len(domLevels)-3] + ip, err := findIp(machineName) + if err != nil { + log.Printf("Couldn't find IP for machine '%s' : %s", machineName, err) + } // else { + //log.Printf("Found IP %s for machine '%s'", ip, machineName) + //} + + rr := &dns.A{ + Hdr: dns.RR_Header{ + Name: m.Question[0].Name, + Rrtype: dns.TypeA, + Class: dns.ClassINET, + Ttl: uint32(ttl), + }, + A: net.ParseIP(ip).To4(), } + m.Answer = append(m.Answer, rr) + w.WriteMsg(m) } @@ -74,10 +75,14 @@ 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") - debug := flag.Bool("debug", false, "Enable debugging") + _ = flag.Bool("debug", false, "Enable debugging") flag.Parse() - mcnlog.SetDebug(*debug) + //if *debug { + // p := profile.Start(profile.MemProfile, profile.ProfilePath(".")) + // defer p.Stop() + //} + api = libmachine.NewClient(mcndirs.GetBaseDir(), mcndirs.GetMachineCertDir()) defer api.Close()