use smaller container as base

This commit is contained in:
Josh Deprez 2021-01-05 20:47:41 +11:00
parent 4396ec6592
commit 1ecb5ecdba
2 changed files with 12 additions and 16 deletions

View file

@ -1,9 +1,10 @@
FROM golang:latest FROM golang:latest as builder
WORKDIR /go/src/sungrow WORKDIR /go/src/sungrow
COPY . . COPY . .
RUN go get -d -v ./... RUN CGO_ENABLED=0 go build -v -o sungrow .
RUN go install -v ./...
CMD ["sungrow"] FROM gcr.io/distroless/static:latest
COPY --from=builder /go/src/sungrow/sungrow /usr/bin/
CMD ["/usr/bin/sungrow"]

17
main.go
View file

@ -6,9 +6,9 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"log"
"net" "net"
"net/http" "net/http"
"os"
"time" "time"
"github.com/goburrow/modbus" "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) { func statusHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "not implemented", http.StatusNotFound) 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) { func readRegs(client modbus.Client, start, qty uint16) {
data, err := client.ReadInputRegisters(start, qty) data, err := client.ReadInputRegisters(start, qty)
if err != nil { 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 { for addr, reg := range sungrowInputRegs {
if addr <= start || addr > start+qty { if addr <= start || addr > start+qty {
@ -105,7 +100,7 @@ func main() {
// Is the inverter reachable? // Is the inverter reachable?
sgc, err := dialSungrow(*inverterAddr) sgc, err := dialSungrow(*inverterAddr)
if err != nil { if err != nil {
die("Couldn't dial inverter: %v", err) log.Fatalf("Couldn't dial inverter: %v", err)
} }
defer sgc.Close() defer sgc.Close()
@ -114,14 +109,14 @@ func main() {
// to connect to, and connect to it immediately. // to connect to, and connect to it immediately.
ln, err := net.Listen("tcp", "localhost:0") ln, err := net.Listen("tcp", "localhost:0")
if err != nil { if err != nil {
die("Couldn't open listener: %v", err) log.Fatalf("Couldn't open listener: %v", err)
} }
defer ln.Close() defer ln.Close()
go func() { go func() {
// Only one connection needed! // Only one connection needed!
conn, err := ln.Accept() conn, err := ln.Accept()
if err != nil { if err != nil {
die("Couldn't accept connection: %v", err) log.Fatalf("Couldn't accept connection: %v", err)
} }
go io.Copy(sgc, conn) go io.Copy(sgc, conn)
io.Copy(conn, sgc) io.Copy(conn, sgc)
@ -140,7 +135,7 @@ func main() {
scrape(client) scrape(client)
// Start http interface only after first successful scrape // Start http interface only after first successful scrape
go func() { go func() {
die("http.ListenAndServe: %v", http.ListenAndServe(*httpAddr, nil)) log.Fatalf("http.ListenAndServe: %v", http.ListenAndServe(*httpAddr, nil))
}() }()
for range time.Tick(*scrapeInterval) { for range time.Tick(*scrapeInterval) {
scrape(client) scrape(client)