From 482ffe960f969747fa5fd8e3c1ae92ba4208423a Mon Sep 17 00:00:00 2001 From: simon987 Date: Fri, 3 Jan 2020 12:25:24 -0500 Subject: [PATCH] dockerize app --- Dockerfile | 31 +++++++++++++++++++ config.json | 6 ++-- docker-compose.yml | 22 +++++++++++++ grafana/Dockerfile | 2 ++ grafana/provisioning/dashboards/dashboard.yml | 12 +++++++ .../{ => provisioning/dashboards}/model.json | 4 +-- .../provisioning/datasources/datasource.yml | 10 ++++++ import_from_broker.py | 7 ++++- main.go | 15 +++------ 9 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 grafana/Dockerfile create mode 100644 grafana/provisioning/dashboards/dashboard.yml rename grafana/{ => provisioning/dashboards}/model.json (99%) create mode 100644 grafana/provisioning/datasources/datasource.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..193fcdc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +FROM golang:1.13-alpine as builder + +ENV GO111MODULE=on + +# Create the user and group files to run unprivileged +RUN mkdir /user && \ + echo 'nobody:x:65534:65534:nobody:/:' > /user/passwd && \ + echo 'nobody:x:65534:' > /user/group + +RUN apk update && apk add --no-cache git ca-certificates tzdata +RUN mkdir /build +COPY . /build/ +WORKDIR /build + +COPY ./ ./ +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o architeuthis . + +FROM scratch AS final +LABEL author="simon987 " + +COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo +COPY --from=builder /user/group /user/passwd /etc/ +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=builder /build/architeuthis / +COPY --from=builder /build/config.json / + +WORKDIR / +USER nobody:nobody +ENTRYPOINT ["/architeuthis"] + +EXPOSE 5050 diff --git a/config.json b/config.json index f93db37..e232277 100644 --- a/config.json +++ b/config.json @@ -1,14 +1,14 @@ { - "addr": "localhost:5050", + "addr": "0.0.0.0:5050", "timeout": "15s", "wait": "0.5s", "multiplier": 1, "retries": 3, - "influx_url": "http://localhost:8086", + "influx_url": "http://influxdb:8086", "influx_user": "", "influx_pass": "", "max_error": 0.4, - "redis_url": "localhost:6379", + "redis_url": "redis:6379", "hosts": [ { "host": "*", diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..4c7089a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,22 @@ +version: '3' + +services: + architeuthis: + build: . + ports: + - "5050:5050" + redis: + image: "redis:alpine" + + # (Optional) + influxdb: + image: "influxdb" + environment: + - INFLUXDB_DB=architeuthis + grafana: + build: ./grafana + ports: + - "3000:3000" + environment: + - GF_SECURITY_ADMIN_PASSWORD=changeme + diff --git a/grafana/Dockerfile b/grafana/Dockerfile new file mode 100644 index 0000000..5b5ae26 --- /dev/null +++ b/grafana/Dockerfile @@ -0,0 +1,2 @@ +FROM grafana/grafana +COPY ./provisioning /etc/grafana/provisioning diff --git a/grafana/provisioning/dashboards/dashboard.yml b/grafana/provisioning/dashboards/dashboard.yml new file mode 100644 index 0000000..ad7257a --- /dev/null +++ b/grafana/provisioning/dashboards/dashboard.yml @@ -0,0 +1,12 @@ +apiVersion: 1 + +providers: + - name: 'Architeuthis' + orgId: 1 + folder: '' + type: file + disableDeletion: false + updateIntervalSeconds: 10 + options: + path: /etc/grafana/provisioning/dashboards/model.json + diff --git a/grafana/model.json b/grafana/provisioning/dashboards/model.json similarity index 99% rename from grafana/model.json rename to grafana/provisioning/dashboards/model.json index a2ddcb6..f70b73a 100644 --- a/grafana/model.json +++ b/grafana/provisioning/dashboards/model.json @@ -15,7 +15,7 @@ "editable": true, "gnetId": null, "graphTooltip": 0, - "id": 1, + "id": null, "links": [], "panels": [ { @@ -774,5 +774,5 @@ "timezone": "", "title": "Architeuthis", "uid": "I2xEOnbZk", - "version": 11 + "version": 1 } \ No newline at end of file diff --git a/grafana/provisioning/datasources/datasource.yml b/grafana/provisioning/datasources/datasource.yml new file mode 100644 index 0000000..6925159 --- /dev/null +++ b/grafana/provisioning/datasources/datasource.yml @@ -0,0 +1,10 @@ +datasources: + - access: 'proxy' + editable: true + is_default: true + name: 'influx' + org_id: 1 + type: 'influxdb' + url: 'http://influxdb:8086' + database: 'architeuthis' + version: 1 \ No newline at end of file diff --git a/import_from_broker.py b/import_from_broker.py index aa04d11..1d0df72 100644 --- a/import_from_broker.py +++ b/import_from_broker.py @@ -1,9 +1,14 @@ import asyncio +import sys import requests from proxybroker import Broker, Checker -ARCHITEUTHIS_URL = "http://localhost:5050" +if len(sys.argv) < 2: + print("Architeuthis url required") + quit(0) + +ARCHITEUTHIS_URL = sys.argv[1] def add_to_architeuthis(name, url): diff --git a/main.go b/main.go index 0aa6c86..d5648fe 100644 --- a/main.go +++ b/main.go @@ -9,13 +9,13 @@ import ( "github.com/sirupsen/logrus" "html/template" "net/http" - "strings" "time" ) func New() *Architeuthis { a := new(Architeuthis) + a.reloadConfig() a.redis = redis.NewClient(&redis.Options{ Addr: config.RedisUrl, @@ -61,7 +61,7 @@ func New() *Architeuthis { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - _, _ = fmt.Fprint(w, "{\"name\":\"Architeuthis\",\"version\":2.0}") + _, _ = fmt.Fprint(w, "{\"name\":\"Architeuthis\",\"version\":2.1}") }) mux.HandleFunc("/add_proxy", func(w http.ResponseWriter, r *http.Request) { @@ -276,20 +276,15 @@ func main() { logrus.SetLevel(logrus.TraceLevel) balancer := New() - balancer.reloadConfig() - var err error + var err error = nil balancer.influxdb, err = influx.NewHTTPClient(influx.HTTPConfig{ Addr: config.InfluxUrl, Username: config.InfluxUser, Password: config.InfluxPass, }) - - if config.InfluxUrl != "" { - _, err = http.Post(config.InfluxUrl+"/query", "application/x-www-form-urlencoded", strings.NewReader("q=CREATE DATABASE \"architeuthis\"")) - if err != nil { - panic(err) - } + if err != nil { + panic(err) } balancer.points = make(chan *influx.Point, InfluxDbBufferSize)