From 86b4cfbb4963c7f9f7439c22b62fb773b789da45 Mon Sep 17 00:00:00 2001 From: Alex <alexellis2@gmail.com> Date: Sun, 8 Jan 2017 17:10:17 +0000 Subject: [PATCH] Add hub stats function --- sample-functions/DockerHubStats/.gitignore | 1 + sample-functions/DockerHubStats/Dockerfile | 13 ++++++ sample-functions/DockerHubStats/Makefile | 9 ++++ sample-functions/DockerHubStats/README.md | 11 +++++ sample-functions/DockerHubStats/main.go | 53 ++++++++++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 sample-functions/DockerHubStats/.gitignore create mode 100644 sample-functions/DockerHubStats/Dockerfile create mode 100644 sample-functions/DockerHubStats/Makefile create mode 100644 sample-functions/DockerHubStats/README.md create mode 100644 sample-functions/DockerHubStats/main.go diff --git a/sample-functions/DockerHubStats/.gitignore b/sample-functions/DockerHubStats/.gitignore new file mode 100644 index 00000000..5b8e3815 --- /dev/null +++ b/sample-functions/DockerHubStats/.gitignore @@ -0,0 +1 @@ +DockerHubStats diff --git a/sample-functions/DockerHubStats/Dockerfile b/sample-functions/DockerHubStats/Dockerfile new file mode 100644 index 00000000..6ab0b2bc --- /dev/null +++ b/sample-functions/DockerHubStats/Dockerfile @@ -0,0 +1,13 @@ +FROM golang:1.7.3-alpine +MAINTAINER alexellis2@gmail.com +ENTRYPOINT [] + +RUN apk --no-cache add make +WORKDIR /go/src/github.com/alexellis/faas/sample-functions/DockerHubStats +COPY . /go/src/github.com/alexellis/faas/sample-functions/DockerHubStats + +RUN make +COPY fwatchdog /usr/bin/fwatchdog +ENV fprocess "/go/bin/DockerHubStats" + +CMD [ "/usr/bin/fwatchdog"] diff --git a/sample-functions/DockerHubStats/Makefile b/sample-functions/DockerHubStats/Makefile new file mode 100644 index 00000000..7e48bf78 --- /dev/null +++ b/sample-functions/DockerHubStats/Makefile @@ -0,0 +1,9 @@ + +.PHONY: install image + +install: + @go install . + +image: + @docker build -t alexellis2/dockerhub-stats . + diff --git a/sample-functions/DockerHubStats/README.md b/sample-functions/DockerHubStats/README.md new file mode 100644 index 00000000..a0449f8a --- /dev/null +++ b/sample-functions/DockerHubStats/README.md @@ -0,0 +1,11 @@ +Building: + +``` +# docker service rm hubstats ; docker service create --network=functions --name=hubstats alexellis2/dockerhub-stats +``` + +Query the function through the gateway: + +``` +# curl -X POST -d "alexellis2" -v http://localhost:8080/function/hubstats +``` \ No newline at end of file diff --git a/sample-functions/DockerHubStats/main.go b/sample-functions/DockerHubStats/main.go new file mode 100644 index 00000000..846c4666 --- /dev/null +++ b/sample-functions/DockerHubStats/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "strings" +) + +type dockerHubStatsType struct { + Count int `json:"count"` +} + +func sanitizeInput(input string) string { + parts := strings.Split(input, "\n") + return strings.Trim(parts[0], " ") +} + +func requestHubStats(org string) dockerHubStatsType { + client := http.Client{} + res, err := client.Get("https://hub.docker.com/v2/repositories/" + org) + if err != nil { + log.Fatalln("Unable to reach Docker Hub server.") + } + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Fatalln("Unable to parse response from server.") + } + + dockerHubStats := dockerHubStatsType{} + json.Unmarshal(body, &dockerHubStats) + return dockerHubStats +} + +func main() { + input, err := ioutil.ReadAll(os.Stdin) + if err != nil { + log.Fatal("Unable to read standard input:", err) + } + org := string(input) + if len(input) == 0 { + log.Fatalln("A username or organisation is required.") + } + + org = sanitizeInput(org) + dockerHubStats := requestHubStats(org) + + fmt.Printf("The organisation or user %s has %d repositories on the Docker hub.\n", org, dockerHubStats.Count) +} -- GitLab