diff --git a/Dockerfile b/Dockerfile index a42094e..6eb05e4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,10 @@ -FROM golang:latest - +FROM golang:latest as builder WORKDIR /go/src/sungrow COPY . . -RUN go get -d -v ./... -RUN go install -v ./... +RUN CGO_ENABLED=0 go build -v -o sungrow . -CMD ["sungrow"] \ No newline at end of file +FROM gcr.io/distroless/static:latest +COPY --from=builder /go/src/sungrow/sungrow /usr/bin/ + +CMD ["/usr/bin/sungrow"] \ No newline at end of file diff --git a/main.go b/main.go index 7bf7daf..b1f3476 100644 --- a/main.go +++ b/main.go @@ -6,9 +6,9 @@ import ( "flag" "fmt" "io" + "log" "net" "net/http" - "os" "time" "github.com/goburrow/modbus" @@ -64,11 +64,6 @@ func init() { } } -func die(f string, args ...interface{}) { - fmt.Fprintf(os.Stderr, f+"\n", args...) - os.Exit(1) -} - func statusHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, "not implemented", http.StatusNotFound) } @@ -76,7 +71,7 @@ func statusHandler(w http.ResponseWriter, r *http.Request) { func readRegs(client modbus.Client, start, qty uint16) { data, err := client.ReadInputRegisters(start, qty) if err != nil { - die("Couldn't read input registers %d-%d: %v", start+1, start+qty, err) + log.Fatalf("Couldn't read input registers %d-%d: %v", start+1, start+qty, err) } for addr, reg := range sungrowInputRegs { if addr <= start || addr > start+qty { @@ -105,7 +100,7 @@ func main() { // Is the inverter reachable? sgc, err := dialSungrow(*inverterAddr) if err != nil { - die("Couldn't dial inverter: %v", err) + log.Fatalf("Couldn't dial inverter: %v", err) } defer sgc.Close() @@ -114,14 +109,14 @@ func main() { // to connect to, and connect to it immediately. ln, err := net.Listen("tcp", "localhost:0") if err != nil { - die("Couldn't open listener: %v", err) + log.Fatalf("Couldn't open listener: %v", err) } defer ln.Close() go func() { // Only one connection needed! conn, err := ln.Accept() if err != nil { - die("Couldn't accept connection: %v", err) + log.Fatalf("Couldn't accept connection: %v", err) } go io.Copy(sgc, conn) io.Copy(conn, sgc) @@ -140,7 +135,7 @@ func main() { scrape(client) // Start http interface only after first successful scrape go func() { - die("http.ListenAndServe: %v", http.ListenAndServe(*httpAddr, nil)) + log.Fatalf("http.ListenAndServe: %v", http.ListenAndServe(*httpAddr, nil)) }() for range time.Tick(*scrapeInterval) { scrape(client)