-
-
Save jayunit100/3350a477c36ea2fc7a80919c1f8d84be to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| commit 3ab46ba63478aea0fb3b5ab3fa4bd57f8060c6d8 | |
| Author: Jay Vyas <[email protected]> | |
| Date: Wed Aug 2 15:44:48 2017 -0400 | |
| SIDECAR: Source code for a sidecare web application | |
| diff --git a/docker/blackducksoftware/hub-sidecar/build.sh b/docker/blackducksoftware/hub-sidecar/build.sh | |
| new file mode 100755 | |
| index 0000000000..604956120b | |
| --- /dev/null | |
| +++ b/docker/blackducksoftware/hub-sidecar/build.sh | |
| @@ -0,0 +1,5 @@ | |
| +#!/usr/bin/env bash | |
| +# TODO We can deploy this from a dockerfile, as a binary, once we have everything we want in it. | |
| +export GOPATH=$GOPATH:/Users/jvyas/work/rest-backend/docker/blackducksoftware/hub-sidecar | |
| +go test ./src/sidecar | |
| +go run ./src/main/main.go | |
| diff --git a/docker/blackducksoftware/hub-sidecar/pkg/darwin_amd64/github.com/codegangsta/negroni.a b/docker/blackducksoftware/hub-sidecar/pkg/darwin_amd64/github.com/codegangsta/negroni.a | |
| new file mode 100644 | |
| index 0000000000..f410d6f040 | |
| Binary files /dev/null and b/docker/blackducksoftware/hub-sidecar/pkg/darwin_amd64/github.com/codegangsta/negroni.a differ | |
| diff --git a/docker/blackducksoftware/hub-sidecar/pkg/darwin_amd64/github.com/gorilla/mux.a b/docker/blackducksoftware/hub-sidecar/pkg/darwin_amd64/github.com/gorilla/mux.a | |
| new file mode 100644 | |
| index 0000000000..41a5e6e259 | |
| Binary files /dev/null and b/docker/blackducksoftware/hub-sidecar/pkg/darwin_amd64/github.com/gorilla/mux.a differ | |
| diff --git a/docker/blackducksoftware/hub-sidecar/src/glide.lock b/docker/blackducksoftware/hub-sidecar/src/glide.lock | |
| new file mode 100644 | |
| index 0000000000..9eba5cca13 | |
| --- /dev/null | |
| +++ b/docker/blackducksoftware/hub-sidecar/src/glide.lock | |
| @@ -0,0 +1,16 @@ | |
| +hash: 3417eb2fa76f94abdbb1644459a4153818618ddf81b902e23119234048ddd007 | |
| +updated: 2017-06-20T17:11:00.134089058-04:00 | |
| +imports: | |
| +- name: github.com/codegangsta/negroni | |
| + version: fde5e16d32adc7ad637e9cd9ad21d4ebc6192535 | |
| +- name: github.com/gorilla/context | |
| + version: 08b5f424b9271eedf6f9f0ce86cb9396ed337a42 | |
| +- name: github.com/gorilla/mux | |
| + version: bcd8bc72b08df0f70df986b97f95590779502d31 | |
| +- name: github.com/sirupsen/logrus | |
| + version: 3d4380f53a34dcdc95f0c1db702615992b38d9a4 | |
| +- name: golang.org/x/sys | |
| + version: fb4cac33e3196ff7f507ab9b2d2a44b0142f5b5a | |
| + subpackages: | |
| + - unix | |
| +testImports: [] | |
| diff --git a/docker/blackducksoftware/hub-sidecar/src/glide.yaml b/docker/blackducksoftware/hub-sidecar/src/glide.yaml | |
| new file mode 100644 | |
| index 0000000000..cbb8920b00 | |
| --- /dev/null | |
| +++ b/docker/blackducksoftware/hub-sidecar/src/glide.yaml | |
| @@ -0,0 +1,6 @@ | |
| +package: . | |
| +import: | |
| +- package: github.com/codegangsta/negroni | |
| + version: ^0.2.0 | |
| +- package: github.com/gorilla/mux | |
| + version: ^1.4.0 | |
| diff --git a/docker/blackducksoftware/hub-sidecar/src/main/main.go b/docker/blackducksoftware/hub-sidecar/src/main/main.go | |
| new file mode 100644 | |
| index 0000000000..abe4c9b59f | |
| --- /dev/null | |
| +++ b/docker/blackducksoftware/hub-sidecar/src/main/main.go | |
| @@ -0,0 +1,29 @@ | |
| +package main | |
| + | |
| +import ( | |
| + "github.com/gorilla/mux" | |
| + log "github.com/sirupsen/logrus" | |
| + "github.com/codegangsta/negroni" | |
| + "sidecar" | |
| +) | |
| + | |
| +var Version = "<None: Please build w/ 'go build ldflags -X sidecar.buildstamp `date` -X sidecar.gitinfo `git status`'>" | |
| + | |
| +func main() { | |
| + | |
| + // Note that mux will parse { var } variables for free for us. | |
| + r := mux.NewRouter() | |
| + | |
| + log.Info("Router created ") | |
| + | |
| + // Allow / or /info for simplicity to get the major data. | |
| + r.Path("/status").Methods("GET").HandlerFunc(sidecar.StatusHandler) | |
| + r.Path("/env").Methods("GET").HandlerFunc(sidecar.EnvHandler) | |
| + | |
| + | |
| + log.Info("=========== Now launching negroni...this might take a second...") | |
| + n := negroni.Classic() | |
| + n.UseHandler(r) | |
| + n.Run(":3000") | |
| + log.Info("Done ! Web app is now running.") | |
| +} | |
| \ No newline at end of file | |
| diff --git a/docker/blackducksoftware/hub-sidecar/src/sidecar/sidecar.go b/docker/blackducksoftware/hub-sidecar/src/sidecar/sidecar.go | |
| new file mode 100644 | |
| index 0000000000..9c4d85a330 | |
| --- /dev/null | |
| +++ b/docker/blackducksoftware/hub-sidecar/src/sidecar/sidecar.go | |
| @@ -0,0 +1,100 @@ | |
| +/* | |
| +* Welcome to hub sidecar! | |
| +* This swims with the other hub container ducks and keeps an eye on them. | |
| + */ | |
| +package sidecar | |
| + | |
| +import ( | |
| + "os" | |
| + "net" | |
| + "net/http" | |
| + | |
| + "strings" | |
| + | |
| + "encoding/json" | |
| + | |
| + log "github.com/sirupsen/logrus" | |
| + "fmt" | |
| +) | |
| + | |
| +var services = []string{"zookeeper","cfssl","postgres","job-runner","webserver"} | |
| + | |
| +//pathToStaticContents returns the path to static files that we ship with the hub. | |
| +func pathToStaticContents() string { | |
| + var static_content = os.Getenv("STATIC_FILES") | |
| + | |
| + // Take a wild guess. This will work in dev environment. | |
| + if static_content == "" { | |
| + log.Info("*********** WARNING: DIDNT FIND ENV VAR 'STATIC_FILES', we assume you are running in development.") | |
| + static_content = "../../static/" | |
| + } else { | |
| + log.Info("*********** Read ENV 'STATIC_FILES', path to assets : " + static_content) | |
| + } | |
| + | |
| + //Die if no the static files are missing. | |
| + _, err := os.Stat(static_content) | |
| + if err != nil { | |
| + log.Info("*********** os.Stat failed on " + static_content + " This means no static files are available. Dying...") | |
| + os.Exit(2) | |
| + } | |
| + return static_content | |
| +} | |
| + | |
| +func LookupHubServices() (map[string]interface{}, error) { | |
| + // services status information | |
| + srvStatus := make(map[string]string) | |
| + // Count total failures for summary data. | |
| + srvFailures := 0 | |
| + for _,svc := range services { | |
| + ip, err := net.LookupHost(svc) | |
| + if err != nil{ | |
| + srvFailures += 1 | |
| + } | |
| + statusForThisService := fmt.Sprintf("%v (error = %v)", ip, err) | |
| + srvStatus[svc] = statusForThisService | |
| + } | |
| + | |
| + // Top level data structure that we return w/ all sorts of metadata in it. | |
| + status := make(map[string]interface{}) | |
| + if srvFailures == len(services) { | |
| + status["services_summary"] = "No services are resolving... Your cluster networking is possibly failing." | |
| + } else if srvFailures > 0 { | |
| + status["services_summary"] = fmt.Sprintf("Some services are resolving ... Total failures %v.", srvFailures) | |
| + } else { | |
| + status["services_summary"] = "All hub services are resolvable." | |
| + } | |
| + status["services_detail"] = srvStatus | |
| + return status, nil | |
| +} | |
| + | |
| +func StatusHandler(rw http.ResponseWriter, req *http.Request) { | |
| + log.Info("StatusHandler") | |
| + servicesStatus,err := LookupHubServices() | |
| + if err != nil{ | |
| + panic(fmt.Errorf("Low level failure in DNS lookup.")) | |
| + } | |
| + statusJson, _ := json.Marshal(servicesStatus) | |
| + rw.Write(statusJson) | |
| +} | |
| + | |
| +func EnvHandler(rw http.ResponseWriter, req *http.Request) { | |
| + log.Info("EnvHandler") | |
| + | |
| + environment := make(map[string]string) | |
| + for _, item := range os.Environ() { | |
| + splits := strings.Split(item, "=") | |
| + key := splits[0] | |
| + val := strings.Join(splits[1:], "=") | |
| + environment[key] = val | |
| + } | |
| + | |
| + envJSON := HandleError(json.MarshalIndent(environment, "", " ")).([]byte) | |
| + rw.Write(envJSON) | |
| +} | |
| + | |
| +func HandleError(result interface{}, err error) (r interface{}) { | |
| + if err != nil { | |
| + print("ERROR : " + err.Error()) | |
| + } | |
| + return result | |
| +} | |
| diff --git a/docker/blackducksoftware/hub-sidecar/src/sidecar/sidecar_test.go b/docker/blackducksoftware/hub-sidecar/src/sidecar/sidecar_test.go | |
| new file mode 100644 | |
| index 0000000000..bc97e632c2 | |
| --- /dev/null | |
| +++ b/docker/blackducksoftware/hub-sidecar/src/sidecar/sidecar_test.go | |
| @@ -0,0 +1,18 @@ | |
| +package sidecar | |
| + | |
| +import ( | |
| + "testing" | |
| +) | |
| + | |
| +func TestLookup(t *testing.T) { | |
| + summary,_ := LookupHubServices() | |
| + if summary["services_detail"] == nil { | |
| + t.Fail() | |
| + } | |
| + if summary["services_summary"] == nil { | |
| + t.Fail() | |
| + } | |
| + if len(summary["services_detail"].(map[string]string)) < 5 { | |
| + t.Fail() | |
| + } | |
| +} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment