Use our own Docker client
This commit is contained in:
1 parent
5ce4b25f38
commit
5cb2347f77
4 files changed
+102
-54
No files matched your search
@@ -0,0 +1,85 @@
|
|||||||
|
package docker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/fsouza/go-dockerclient"
|
||||||
|
. "github.com/weaveworks/weave/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Regexp that matches container IDs
|
||||||
|
var containerIDregexp = regexp.MustCompile("^[a-f0-9]+$")
|
||||||
|
|
||||||
|
// IsContainerID returns True if the string provided is a valid container id
|
||||||
|
func IsContainerID(idStr string) bool {
|
||||||
|
return containerIDregexp.MatchString(idStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// An observer for container events
|
||||||
|
type ContainerObserver interface {
|
||||||
|
ContainerDied(ident string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
*docker.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient creates a new Docker client
|
||||||
|
func NewClient(apiPath string) (*Client, error) {
|
||||||
|
dc, err := docker.NewClient(apiPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Client{dc}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start starts the client
|
||||||
|
func (c *Client) Start(apiPath string) error {
|
||||||
|
client, err := docker.NewClient(apiPath)
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("[docker] Unable to connect to Docker API on %s: %s", apiPath, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
env, err := client.Version()
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("[docker] Unable to connect to Docker API on %s: %s", apiPath, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
Info.Printf("[docker] Using Docker API on %s: %v", apiPath, env)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddObserver adds an observer for docker events
|
||||||
|
func (c *Client) AddObserver(ob ContainerObserver) error {
|
||||||
|
events := make(chan *docker.APIEvents)
|
||||||
|
if err := c.AddEventListener(events); err != nil {
|
||||||
|
Error.Printf("[docker] Unable to add listener to Docker API: %s", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for event := range events {
|
||||||
|
switch event.Status {
|
||||||
|
case "die":
|
||||||
|
id := event.ID
|
||||||
|
ob.ContainerDied(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsContainerRunning returns true if the container is up and running
|
||||||
|
func (c *Client) IsContainerRunning(idStr string) (bool, error) {
|
||||||
|
// check with Docker whether the container is really running
|
||||||
|
container, err := c.InspectContainer(idStr)
|
||||||
|
if err != nil {
|
||||||
|
if _, notThere := err.(*docker.NoSuchContainer); notThere {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return container.State.Running, nil
|
||||||
|
}
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
package updater
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/fsouza/go-dockerclient"
|
|
||||||
. "github.com/weaveworks/weave/common"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ContainerObserver interface {
|
|
||||||
ContainerDied(ident string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkError(err error, apiPath string) {
|
|
||||||
if err != nil {
|
|
||||||
Error.Fatalf("[updater] Unable to connect to Docker API on %s: %s",
|
|
||||||
apiPath, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Start(apiPath string, ob ContainerObserver) error {
|
|
||||||
client, err := docker.NewClient(apiPath)
|
|
||||||
checkError(err, apiPath)
|
|
||||||
|
|
||||||
env, err := client.Version()
|
|
||||||
checkError(err, apiPath)
|
|
||||||
|
|
||||||
events := make(chan *docker.APIEvents)
|
|
||||||
err = client.AddEventListener(events)
|
|
||||||
checkError(err, apiPath)
|
|
||||||
|
|
||||||
Info.Printf("[updater] Using Docker API on %s: %v", apiPath, env)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for event := range events {
|
|
||||||
handleEvent(ob, event, client)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleEvent(ob ContainerObserver, event *docker.APIEvents, client *docker.Client) error {
|
|
||||||
switch event.Status {
|
|
||||||
case "die":
|
|
||||||
id := event.ID
|
|
||||||
ob.ContainerDied(id)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
. "github.com/weaveworks/weave/common"
|
. "github.com/weaveworks/weave/common"
|
||||||
"github.com/weaveworks/weave/common/updater"
|
"github.com/weaveworks/weave/common/docker"
|
||||||
weavedns "github.com/weaveworks/weave/nameserver"
|
weavedns "github.com/weaveworks/weave/nameserver"
|
||||||
weavenet "github.com/weaveworks/weave/net"
|
weavenet "github.com/weaveworks/weave/net"
|
||||||
)
|
)
|
||||||
@@ -18,6 +18,7 @@ var version = "(unreleased version)"
|
|||||||
func main() {
|
func main() {
|
||||||
var (
|
var (
|
||||||
justVersion bool
|
justVersion bool
|
||||||
|
dockerCli *docker.Client
|
||||||
ifaceName string
|
ifaceName string
|
||||||
apiPath string
|
apiPath string
|
||||||
domain string
|
domain string
|
||||||
@@ -95,14 +96,18 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
Error.Fatal("[main] Unable to initialize the Zone database", err)
|
Error.Fatal("[main] Unable to initialize the Zone database", err)
|
||||||
}
|
}
|
||||||
err = zone.Start()
|
if err := zone.Start(); err != nil {
|
||||||
if err != nil {
|
|
||||||
Error.Fatal("[main] Unable to start the Zone database", err)
|
Error.Fatal("[main] Unable to start the Zone database", err)
|
||||||
}
|
}
|
||||||
defer zone.Stop()
|
defer zone.Stop()
|
||||||
|
|
||||||
|
dockerCli, err = docker.NewClient(apiPath)
|
||||||
|
if err != nil {
|
||||||
|
Error.Fatal("[main] Unable to start docker client", err)
|
||||||
|
}
|
||||||
|
|
||||||
if watch {
|
if watch {
|
||||||
err := updater.Start(apiPath, zone)
|
err := dockerCli.AddObserver(zone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error.Fatal("[main] Unable to start watcher", err)
|
Error.Fatal("[main] Unable to start watcher", err)
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-3
@@ -15,7 +15,7 @@ import (
|
|||||||
"github.com/davecheney/profile"
|
"github.com/davecheney/profile"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
. "github.com/weaveworks/weave/common"
|
. "github.com/weaveworks/weave/common"
|
||||||
"github.com/weaveworks/weave/common/updater"
|
"github.com/weaveworks/weave/common/docker"
|
||||||
"github.com/weaveworks/weave/ipam"
|
"github.com/weaveworks/weave/ipam"
|
||||||
"github.com/weaveworks/weave/ipam/address"
|
"github.com/weaveworks/weave/ipam/address"
|
||||||
weavenet "github.com/weaveworks/weave/net"
|
weavenet "github.com/weaveworks/weave/net"
|
||||||
@@ -222,10 +222,15 @@ func createAllocator(router *weave.Router, apiPath string, ipRangeStr string, de
|
|||||||
|
|
||||||
allocator.SetInterfaces(router.NewGossip("IPallocation", allocator))
|
allocator.SetInterfaces(router.NewGossip("IPallocation", allocator))
|
||||||
allocator.Start()
|
allocator.Start()
|
||||||
err := updater.Start(apiPath, allocator)
|
|
||||||
|
dockerCli, err := docker.NewClient(apiPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Unable to start watcher", err)
|
Error.Fatal("Unable to start docker client", err)
|
||||||
}
|
}
|
||||||
|
if err = dockerCli.AddObserver(allocator); err != nil {
|
||||||
|
Error.Fatal("Unable to start watcher", err)
|
||||||
|
}
|
||||||
|
|
||||||
return allocator, defaultSubnet
|
return allocator, defaultSubnet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user