Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
- better performance
- fix memory leak due to mcnlog misusage (closes #3)
- add systemd example to the readme
  • Loading branch information
bamarni committed Jul 5, 2016
1 parent 8ee88f6 commit a05a71f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 50 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
101 changes: 53 additions & 48 deletions dockness.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -16,68 +18,71 @@ 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)
}

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()

Expand Down

0 comments on commit a05a71f

Please sign in to comment.