From d4dce83782b67236d2c8d99ffc9bdc89328bb5de Mon Sep 17 00:00:00 2001 From: Emile Date: Sun, 6 Oct 2019 20:11:44 +0200 Subject: get the status of all other containers --- src/docker.go | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/go.mod | 18 +++++++++ src/go.sum | 40 ++++++++++++++++++++ src/http.go | 25 +++++++++++++ src/main.go | 42 +++++++++++++++++++++ src/migrate.sh | 9 +++++ 6 files changed, 248 insertions(+) create mode 100644 src/docker.go create mode 100644 src/go.mod create mode 100644 src/go.sum create mode 100644 src/http.go create mode 100644 src/main.go create mode 100755 src/migrate.sh (limited to 'src') diff --git a/src/docker.go b/src/docker.go new file mode 100644 index 0000000..9ff9209 --- /dev/null +++ b/src/docker.go @@ -0,0 +1,114 @@ +package main + +import ( + "context" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "strings" + + //"docker.io/go-docker" + //"docker.io/go-docker/api/types" + "github.com/docker/docker/api/types" + "github.com/docker/docker/client" +) + +const ( + fixedDockerVersion = "1.38" +) + +var ( + dockerCtx context.Context + dockerCLI *client.Client +) + +func setupContext() { + if dockerCtx == nil { + dockerCtx = context.Background() + } +} + +func setupDockerCLI() (err error) { + os.Setenv("DOCKER_API_VERSION", "1.27") + if dockerCLI == nil { + dockerCLI, err = client.NewEnvClient() + } + return err +} + +func listDockerContainers() { + log.Println("listing the docker containers...") + // setup the context and the docker cli connection + setupContext() + setupDockerCLI() + + // get a list of all networks + networks, err := dockerCLI.NetworkList(context.Background(), types.NetworkListOptions{}) + if err != nil { + panic(err) + } + + // get a list of all containers + containers, err := dockerCLI.ContainerList(context.Background(), types.ContainerListOptions{}) + if err != nil { + panic(err) + } + + // find the container names of the companion containers + var companionContainerNames []string + + for _, container := range containers { + if strings.Contains(container.Image, "companion") { + companionContainerNames = append(companionContainerNames, container.Names[0][1:]) + } + } + + fmt.Printf("%#v\n\n", companionContainerNames) + + // find the IPs of the companion containers + var companionContainerIPs []string + + for _, network := range networks { + for _, container := range network.Containers { + for _, name := range companionContainerNames { + if container.Name == name { + companionContainerIPs = append(companionContainerIPs, container.IPv4Address) + } + } + } + } + + fmt.Printf("%#v\n\n", companionContainerIPs) + + // print all ips found + for _, rawip := range companionContainerIPs { + if strings.Compare(rawip[0:3], "172") == 0 { + ip := rawip[:len(rawip)-3] + fmt.Printf("ip: %s\n\n", ip) + + // get the stats from the container + stats, err := getStats(ip, 8080) + if err != nil { + fmt.Println(err) + } + fmt.Printf("%s\n", stats) + } + } +} + +func getStats(containerName string, containerPort uint16) (string, error) { + url := fmt.Sprintf("http://%s:%d/api/getStats", containerName, containerPort) + log.Printf("url: %s", url) + resp, err := http.Get(url) + if err != nil { + return "", fmt.Errorf("could not make the http get request: %v", err) + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", fmt.Errorf("could not read the request body: %v", err) + } + return string(body), nil +} diff --git a/src/go.mod b/src/go.mod new file mode 100644 index 0000000..acc949c --- /dev/null +++ b/src/go.mod @@ -0,0 +1,18 @@ +module circus-scoreboard + +go 1.13 + +require ( + docker.io/go-docker v1.0.0 + github.com/Microsoft/go-winio v0.4.14 // indirect + github.com/docker/distribution v2.7.1+incompatible // indirect + github.com/docker/docker v1.13.1 + github.com/docker/go-connections v0.4.0 // indirect + github.com/docker/go-units v0.4.0 // indirect + github.com/gogo/protobuf v1.3.0 // indirect + github.com/gorilla/mux v1.7.3 + github.com/moby/moby v1.13.1 + github.com/opencontainers/go-digest v1.0.0-rc1 // indirect + github.com/opencontainers/image-spec v1.0.1 // indirect + golang.org/x/net v0.0.0-20191003171128-d98b1b443823 // indirect +) diff --git a/src/go.sum b/src/go.sum new file mode 100644 index 0000000..2e4aa17 --- /dev/null +++ b/src/go.sum @@ -0,0 +1,40 @@ +docker.io/go-docker v1.0.0 h1:VdXS/aNYQxyA9wdLD5z8Q8Ro688/hG8HzKxYVEVbE6s= +docker.io/go-docker v1.0.0/go.mod h1:7tiAn5a0LFmjbPDbyTPOaTTOuG1ZRNXdPA6RvKY+fpY= +github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU= +github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug= +github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= +github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo= +github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= +github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= +github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= +github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/gogo/protobuf v1.3.0 h1:G8O7TerXerS4F6sx9OV7/nRfJdnXgHZu/S/7F2SN+UE= +github.com/gogo/protobuf v1.3.0/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= +github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= +github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/moby/moby v1.13.1 h1:mC5WwQwCXt/dYxZ1cIrRsnJAWw7VdtcTZUIGr4tXzOM= +github.com/moby/moby v1.13.1/go.mod h1:fDXVQ6+S340veQPv35CzDahGBmHsiclFwfEygB/TWMc= +github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ= +github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= +github.com/opencontainers/image-spec v1.0.1 h1:JMemWkRwHx4Zj+fVxWoMCFm/8sYGGrUVojFA6h/TRcI= +github.com/opencontainers/image-spec v1.0.1/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20191003171128-d98b1b443823 h1:Ypyv6BNJh07T1pUSrehkLemqPKXhus2MkfktJ91kRh4= +golang.org/x/net v0.0.0-20191003171128-d98b1b443823/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b h1:ag/x1USPSsqHud38I9BAC88qdNLDHHtQ4mlgQIZPPNA= +golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/src/http.go b/src/http.go new file mode 100644 index 0000000..e29f13e --- /dev/null +++ b/src/http.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "net/http" + + "github.com/gorilla/mux" +) + +// setupHTTPServer defines a new http server +func setupHTTPServer() http.Server { + r := mux.NewRouter() + + r.HandleFunc("/", indexHandler) + + return http.Server{ + Addr: fmt.Sprintf("0.0.0.0:%d", *port), + Handler: r, + } +} + +// indexHandler handles the "/" endpoint +func indexHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "This is the index Handler speaking! Prepare to HACK THE PLANET!") +} diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..a52c043 --- /dev/null +++ b/src/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "flag" + "log" + "time" +) + +var ( + port *int +) + +func main() { + + // parse the command line flags + parseFlags() + + // periocically get the stats from all companion containers + go func() { + for { + // get stats + log.Printf("Fetching the stats") + listDockerContainers() + log.Printf("Done fetching the stats") + time.Sleep(1 * time.Second) + } + }() + + // setup a http server displaying the scoreboard + log.Println("setting up the http server") + httpServer := setupHTTPServer() + + // start the http server + log.Println("Starting the http server") + log.Fatalln(httpServer.ListenAndServe()) +} + +// parseFlags parses the command line flags +func parseFlags() { + port = flag.Int("port", 8080, "Port the http server should run on") + flag.Parse() +} diff --git a/src/migrate.sh b/src/migrate.sh new file mode 100755 index 0000000..dcc29f6 --- /dev/null +++ b/src/migrate.sh @@ -0,0 +1,9 @@ +files=( $(find . -name '*.go' -not -path './vendor/*') ) + +for rule in \ + 's|"github.com/docker/docker/api|"docker.io/go-docker/api|' \ + 's|^([[:space:]]+)"github.com/docker/docker/client|\1client "docker.io/go-docker|' \ + 's|"github.com/docker/docker/client|"docker.io/go-docker|' \ +; do + sed -i -E "$rule" ${files[*]} +done -- cgit 1.4.1