From 68e8ab190e7066f62e8967697c595dc239db3264 Mon Sep 17 00:00:00 2001 From: ston1th Date: Mon, 24 Dec 2018 14:59:39 +0100 Subject: [PATCH] initial commit --- .drone.sh | 5 +++++ .drone.yaml | 54 +++++++++++++++++++++++++++++++++++++++++++++ Dockerfile.db | 7 ++++++ Dockerfile.srv | 7 ++++++ db.go | 30 +++++++++++++++++++++++++ srv.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 162 insertions(+) create mode 100755 .drone.sh create mode 100644 .drone.yaml create mode 100644 Dockerfile.db create mode 100644 Dockerfile.srv create mode 100644 db.go create mode 100644 srv.go diff --git a/.drone.sh b/.drone.sh new file mode 100755 index 0000000..73556e3 --- /dev/null +++ b/.drone.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +VERSION=${DRONE_BUILD_NUMBER} +CGO_ENABLED=0 GOOS=linux go build -a -gcflags '-e' -ldflags "-X main.version=$VERSION -s -w" -o db db.go +CGO_ENABLED=0 GOOS=linux go build -a -gcflags '-e' -ldflags "-X main.version=$VERSION -s -w" -o srv srv.go diff --git a/.drone.yaml b/.drone.yaml new file mode 100644 index 0000000..a0741e9 --- /dev/null +++ b/.drone.yaml @@ -0,0 +1,54 @@ +workspace: + base: /go + path: src/github.com/drone/drone + +pipeline: + test: + image: golang:alpine + commands: + - go test -cover db.go + - go test -cover srv.go + + build: + image: golang:alpine + commands: sh .drone.sh + when: + event: [ push, tag ] + + publish_srv: + image: plugins/docker + repo: ciapp/srv + registry: kube-registry.kube-system.svc.cluster.local:5000 + dockerfile: Dockerfile.srv + tag: [ latest ] + when: + branch: master + event: push + + publish_db: + image: plugins/docker + repo: ciapp/db + registry: kube-registry.kube-system.svc.cluster.local:5000 + dockerfile: Dockerfile.db + tag: [ latest ] + when: + branch: master + event: push + + release_srv: + image: plugins/docker + repo: ciapp/srv + registry: kube-registry.kube-system.svc.cluster.local:5000 + dockerfile: Dockerfile.srv + tag: [ v1, v2 ] + when: + event: tag + + release_db: + image: plugins/docker + repo: ciapp/db + registry: kube-registry.kube-system.svc.cluster.local:5000 + dockerfile: Dockerfile.db + tag: [ v1, v2 ] + when: + event: tag diff --git a/Dockerfile.db b/Dockerfile.db new file mode 100644 index 0000000..38d8ca7 --- /dev/null +++ b/Dockerfile.db @@ -0,0 +1,7 @@ +FROM scratch +ADD db /db +RUN echo -e "root:x:0:0:root:/:\nappuser:x:1000:1000:appuser:/:" > /etc/passwd +RUN echo -e "root:x:0:root\nappuser:x:1000:" > /etc/group +USER appuser +EXPOSE 8080 +ENTRYPOINT ["/db"] diff --git a/Dockerfile.srv b/Dockerfile.srv new file mode 100644 index 0000000..38d8ca7 --- /dev/null +++ b/Dockerfile.srv @@ -0,0 +1,7 @@ +FROM scratch +ADD db /db +RUN echo -e "root:x:0:0:root:/:\nappuser:x:1000:1000:appuser:/:" > /etc/passwd +RUN echo -e "root:x:0:root\nappuser:x:1000:" > /etc/group +USER appuser +EXPOSE 8080 +ENTRYPOINT ["/db"] diff --git a/db.go b/db.go new file mode 100644 index 0000000..fef60ed --- /dev/null +++ b/db.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "os" + "time" +) + +var version string + +func main() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL) + h, err := os.Hostname() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + fmt.Fprintf(w, "%s (%s)\n", h, version) + }) + s := &http.Server{ + ReadTimeout: 5 * time.Second, + WriteTimeout: 10 * time.Second, + IdleTimeout: 120 * time.Second, + Addr: ":8080", + } + log.Fatal(s.ListenAndServe()) +} diff --git a/srv.go b/srv.go new file mode 100644 index 0000000..c51d44b --- /dev/null +++ b/srv.go @@ -0,0 +1,59 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "net" + "net/http" + "os" + "os/user" + "strconv" + "time" +) + +var version string + +func main() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL) + h, err := os.Hostname() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + u, err := user.LookupId(strconv.Itoa(os.Getuid())) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + //url := "http://" + os.Getenv("DB_SERVICE_HOST") + ":" + os.Getenv("DB_SERVICE_PORT") + //url := "http://testapp-db.default.svc.cluster.local:8080" + url := "http://testapp-db:8080" + c := &http.Client{Transport: &http.Transport{ + DialContext: (&net.Dialer{ + Timeout: 5 * time.Second, + }).DialContext, + }} + resp, err := c.Get(url) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + fmt.Fprintf(w, "Version: %s\nHostname: %s\nUser: %s (%s) (%s)\nDB URL: %s\nDB GET: %s\n", + version, h, u.Username, u.Uid, u.Gid, url, string(body)) + resp.Body.Close() + }) + s := &http.Server{ + ReadTimeout: 5 * time.Second, + WriteTimeout: 10 * time.Second, + IdleTimeout: 120 * time.Second, + Addr: ":8080", + } + log.Fatal(s.ListenAndServe()) +}