Skip to content

Instantly share code, notes, and snippets.

@rishiloyola
Created December 20, 2015 10:00
Show Gist options
  • Save rishiloyola/24d9a5ab5dd6e1122b2e to your computer and use it in GitHub Desktop.
Save rishiloyola/24d9a5ab5dd6e1122b2e to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strconv"
"time"
"github.com/fsouza/go-dockerclient"
"github.com/samuel/go-zookeeper/zk"
)
func main() {
dkrClient, err := docker.NewClientFromEnv()
handleError(err)
zkConn, _, err := zk.Connect([]string{"127.0.0.1:2181"}, time.Second)
handleError(err)
time.Sleep(time.Second)
prevcontainers := 0
for {
containers, err := dkrClient.ListContainers(docker.ListContainersOptions{All: false, Filters: map[string][]string{"name": {"pushpin1"}, "status": {"running"}}})
handleError(err)
if len(containers) == prevcontainers {
continue
} else {
if len(containers) == 1 {
_, err := zkConn.Create("/pushpin/server", []byte("localhost:"+strconv.FormatInt(containers[0].Ports[0].PrivatePort, 10)), zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
handleError(err)
fmt.Println("[zookeeper] : Registered pushpin server")
} else {
err := zkConn.Delete("/pushpin/server", -1)
handleError(err)
fmt.Println("[zookeeper] : Deleted pushpin server")
}
}
prevcontainers = len(containers)
}
}
//handleError stops the program execution
func handleError(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment