initial commit

This commit is contained in:
ston1th 2018-12-24 14:59:39 +01:00
commit 68e8ab190e
6 changed files with 162 additions and 0 deletions

5
.drone.sh Executable file
View file

@ -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

54
.drone.yaml Normal file
View file

@ -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

7
Dockerfile.db Normal file
View file

@ -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"]

7
Dockerfile.srv Normal file
View file

@ -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"]

30
db.go Normal file
View file

@ -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())
}

59
srv.go Normal file
View file

@ -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())
}