From b1a766f9b69dc34a52fdb60e5925f37cf7eda4d0 Mon Sep 17 00:00:00 2001 From: Tine Date: Sat, 17 Feb 2024 20:19:18 +0100 Subject: [PATCH] ci(build): deployment fixes --- .dockerignore | 3 --- build/Dockerfile | 6 ++--- cmd/zdravko/main.go | 10 ++++--- deploy/Dockerfile | 15 ++++++----- deploy/entrypoint.sh | 14 ++++++++++ deploy/fly.toml | 5 ++-- deploy/litefs.yaml | 3 --- internal/activities/healthcheck.go | 43 +++++++++++++++++++++++++++--- internal/workflows/healthcheck.go | 6 ++--- pkg/worker/worker.go | 2 +- 10 files changed, 78 insertions(+), 29 deletions(-) delete mode 100644 .dockerignore create mode 100755 deploy/entrypoint.sh diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 817f140..0000000 --- a/.dockerignore +++ /dev/null @@ -1,3 +0,0 @@ -build -deploy -docs diff --git a/build/Dockerfile b/build/Dockerfile index 51d548a..0608953 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -12,12 +12,12 @@ RUN go mod download COPY . ./ # Build -RUN CGO_ENABLED=1 GOOS=linux go build -o /zdravko cmd/zdravko/main.go +RUN CGO_ENABLED=1 GOOS=linux go build -o /bin/zdravko cmd/zdravko/main.go ### # Final production -FROM gcr.io/distroless/base-debian12:nonroot as production -COPY --from=builder /zdravko /zdravko +FROM debian:12-slim as production +COPY --from=builder /bin/zdravko /bin/zdravko COPY LICENSE /LICENSE COPY README.md /README.md diff --git a/cmd/zdravko/main.go b/cmd/zdravko/main.go index c0ae0f7..ddf6269 100644 --- a/cmd/zdravko/main.go +++ b/cmd/zdravko/main.go @@ -86,10 +86,12 @@ func main() { for sig := range c { log.Printf("Received signal: %v", sig) for _, s := range servers { - println("Stopping", s.Name()) - err := s.Stop() - if err != nil { - log.Fatalf("Unable to stop server %s: %v", s.Name(), err) + if s != nil { + println("Stopping", s.Name()) + err := s.Stop() + if err != nil { + log.Fatalf("Unable to stop server %s: %v", s.Name(), err) + } } } } diff --git a/deploy/Dockerfile b/deploy/Dockerfile index 7b889fe..d6da698 100644 --- a/deploy/Dockerfile +++ b/deploy/Dockerfile @@ -1,11 +1,14 @@ # syntax=docker/dockerfile:1 -ARG ALPINE_VERSION=3.14 +ARG ZDRAVKO_VERSION=main + +FROM ghcr.io/mentos1386/zdravko:${ZDRAVKO_VERSION} +RUN apt-get update -y \ + && apt-get install -y ca-certificates fuse3 sqlite3 -FROM alpine:${ALPINE_VERSION} as temporal -RUN apk add ca-certificates fuse3 sqlite COPY --from=flyio/litefs:0.5 /usr/local/bin/litefs /usr/local/bin/litefs -COPY litefs.yaml /etc/litefs.yaml -COPY --from=ghcr.io/mentos1386/zdravko /zdravko /zdravko +COPY deploy/litefs.yaml /etc/litefs.yml +COPY deploy/entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh -ENTRYPOINT litefs mount +ENTRYPOINT ["/entrypoint.sh"] diff --git a/deploy/entrypoint.sh b/deploy/entrypoint.sh new file mode 100755 index 0000000..7fc58a2 --- /dev/null +++ b/deploy/entrypoint.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +PROCESS=${FLY_PROCESS_GROUP} + +if [ "$PROCESS" = "server" ]; then + echo "Starting server process" + exec litefs mount -- $@ +elif [ "$PROCESS" = "worker" ]; then + echo "Starting worker process" + exec $@ +else + echo "Unknown process" + exit 1 +fi diff --git a/deploy/fly.toml b/deploy/fly.toml index 3b27f88..24b6617 100644 --- a/deploy/fly.toml +++ b/deploy/fly.toml @@ -5,6 +5,7 @@ primary_region = 'waw' [build] dockerfile = "Dockerfile" + context = "deploy" [env] ROOT_URL = 'https://zdravko.mnts.dev' @@ -17,8 +18,8 @@ primary_region = 'waw' TEMPORAL_SERVER_HOST = 'server.process.zdravko.internal:7233' [processes] - server = "zdravko --temporal=true --server=true --worker=false" - worker = "zdravko --temporal=false --server=false --worker=true" + server = "/zdravko --temporal=true --server=true --worker=false" + worker = "/zdravko --temporal=false --server=false --worker=true" [[mounts]] source = "zdravko_data" diff --git a/deploy/litefs.yaml b/deploy/litefs.yaml index 9fd51dd..f5f57f4 100644 --- a/deploy/litefs.yaml +++ b/deploy/litefs.yaml @@ -7,9 +7,6 @@ fuse: data: dir: "/var/lib/litefs" -exec: - - cmd: "/zdravko --temporal=true --server=true --worker=false" - lease: type: "consul" diff --git a/internal/activities/healthcheck.go b/internal/activities/healthcheck.go index e618148..6b1b9ad 100644 --- a/internal/activities/healthcheck.go +++ b/internal/activities/healthcheck.go @@ -6,16 +6,16 @@ import ( "net/http" ) -type HealtcheckHttpActivityParam struct { +type HealtcheckHttpParam struct { Url string Method string } -type HealthcheckHttpActivityResult struct { +type HealthcheckHttpResult struct { StatusCode int } -func HealthcheckHttpActivityDefinition(ctx context.Context, param HealtcheckHttpActivityParam) (*HealthcheckHttpActivityResult, error) { +func HealthcheckHttp(ctx context.Context, param HealtcheckHttpParam) (*HealthcheckHttpResult, error) { if param.Method == "" { param.Method = "GET" } @@ -38,5 +38,40 @@ func HealthcheckHttpActivityDefinition(ctx context.Context, param HealtcheckHttp log.Printf("HealthcheckHttpActivityDefinition produced statuscode %d for url %s", response.StatusCode, param.Url) - return &HealthcheckHttpActivityResult{StatusCode: response.StatusCode}, nil + return &HealthcheckHttpResult{StatusCode: response.StatusCode}, nil +} + +type HealtcheckHttpAddToHistoryParam struct { + Id string + Success bool + StatusCode int +} + +type HealthcheckHttpAddToHistoryResult struct { +} + +func HealthcheckHttpWriteResult(ctx context.Context, param HealtcheckHttpParam) (*HealthcheckHttpResult, error) { + if param.Method == "" { + param.Method = "GET" + } + + var ( + response *http.Response + err error + ) + + switch param.Method { + case "GET": + response, err = http.Get(param.Url) + case "POST": + response, err = http.Post(param.Url, "application/json", nil) + } + + if err != nil { + return nil, err + } + + log.Printf("HealthcheckHttpActivityDefinition produced statuscode %d for url %s", response.StatusCode, param.Url) + + return &HealthcheckHttpResult{StatusCode: response.StatusCode}, nil } diff --git a/internal/workflows/healthcheck.go b/internal/workflows/healthcheck.go index 8c25455..4aac2c7 100644 --- a/internal/workflows/healthcheck.go +++ b/internal/workflows/healthcheck.go @@ -19,13 +19,13 @@ func HealthcheckHttpWorkflowDefinition(ctx workflow.Context, param HealthcheckHt } ctx = workflow.WithActivityOptions(ctx, options) - activityParam := activities.HealtcheckHttpActivityParam{ + activityParam := activities.HealtcheckHttpParam{ Url: param.Url, Method: param.Method, } - var result *activities.HealthcheckHttpActivityResult - err := workflow.ExecuteActivity(ctx, activities.HealthcheckHttpActivityDefinition, activityParam).Get(ctx, &result) + var result *activities.HealthcheckHttpResult + err := workflow.ExecuteActivity(ctx, activities.HealthcheckHttp, activityParam).Get(ctx, &result) if err != nil { return err } diff --git a/pkg/worker/worker.go b/pkg/worker/worker.go index ed4f027..820e8ac 100644 --- a/pkg/worker/worker.go +++ b/pkg/worker/worker.go @@ -37,7 +37,7 @@ func (w *Worker) Start() error { w.worker.RegisterWorkflow(workflows.HealthcheckHttpWorkflowDefinition) // Register Activities - w.worker.RegisterActivity(activities.HealthcheckHttpActivityDefinition) + w.worker.RegisterActivity(activities.HealthcheckHttp) return w.worker.Run(worker.InterruptCh()) }