Created
December 20, 2015 10:00
-
-
Save rishiloyola/24d9a5ab5dd6e1122b2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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