From e8e738ee78685c7adc52a214990faf8a3ee355b8 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Wed, 2 Dec 2020 15:55:35 +0300 Subject: [PATCH] HW15 WIP --- hw12_13_14_15_calendar/Makefile | 6 +- .../cicd/calendar/Dockerfile | 6 +- .../cicd/docker-compose.yml | 91 ++++++++++++++----- hw12_13_14_15_calendar/cicd/goose/Dockerfile | 5 + .../cicd/scheduler/Dockerfile | 4 +- hw12_13_14_15_calendar/cicd/sender/Dockerfile | 4 +- .../cmd/calendar/main_test.go | 46 +++++----- hw12_13_14_15_calendar/cmd/scheduler/main.go | 5 +- hw12_13_14_15_calendar/cmd/sender/main.go | 5 +- .../migrations/20200924000000_init.sql | 15 ++- .../pkg/api/public/grpcclient.go | 3 +- .../integration/calendar.go => test/main.go} | 50 +++++----- 12 files changed, 139 insertions(+), 101 deletions(-) create mode 100644 hw12_13_14_15_calendar/cicd/goose/Dockerfile rename hw12_13_14_15_calendar/{cmd/integration/calendar.go => test/main.go} (91%) diff --git a/hw12_13_14_15_calendar/Makefile b/hw12_13_14_15_calendar/Makefile index d8038d2..a5cfb81 100644 --- a/hw12_13_14_15_calendar/Makefile +++ b/hw12_13_14_15_calendar/Makefile @@ -28,10 +28,10 @@ generate: compose-build: sudo -S docker-compose -f ./cicd/docker-compose.yml build -compose-up: - sudo -S docker-compose -f ./cicd/docker-compose.yml up -d +up: + sudo -S docker-compose -f ./cicd/docker-compose.yml up -d --build -compose-down: +down: sudo -S docker-compose -f ./cicd/docker-compose.yml down calendar-start: diff --git a/hw12_13_14_15_calendar/cicd/calendar/Dockerfile b/hw12_13_14_15_calendar/cicd/calendar/Dockerfile index 290f3f3..1c4afea 100644 --- a/hw12_13_14_15_calendar/cicd/calendar/Dockerfile +++ b/hw12_13_14_15_calendar/cicd/calendar/Dockerfile @@ -3,7 +3,7 @@ RUN mkdir -p /app WORKDIR /app COPY . . RUN go get -d ./cmd/calendar/. -RUN GOOS=linux GOARCH=amd64 go build -a -o calendar ./cmd/calendar/. +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o calendar ./cmd/calendar/. FROM alpine:latest ENV APP_GRPC_ADDRESS=0.0.0.0 @@ -14,7 +14,7 @@ ENV APP_API_ADDRESS=0.0.0.0 ENV APP_API_PORT=50053 ENV APP_LOGGER_FILE=/calendar.log ENV APP_LOGGER_LEVEL=INFO -ENV APP_STORAGE_INMEMORY=true +ENV APP_STORAGE_INMEMORY=false ENV APP_STORAGE_SQLHOST=psql ENV APP_STORAGE_SQLPORT=5432 ENV APP_STORAGE_SQLDBASE=calendar @@ -25,4 +25,4 @@ COPY --from=builder /app/calendar ./sbin EXPOSE ${APP_GRPC_PORT} EXPOSE ${APP_HTTP_PORT} EXPOSE ${APP_API_PORT} -CMD ["calendar"] +ENTRYPOINT ["calendar"] diff --git a/hw12_13_14_15_calendar/cicd/docker-compose.yml b/hw12_13_14_15_calendar/cicd/docker-compose.yml index 7a67bcd..924ba16 100644 --- a/hw12_13_14_15_calendar/cicd/docker-compose.yml +++ b/hw12_13_14_15_calendar/cicd/docker-compose.yml @@ -1,5 +1,68 @@ version: '3' services: + calendar: + build: + context: .. + dockerfile: ./cicd/calendar/Dockerfile + volumes: + - ../calendar.log:/calendar.log + ports: + - "8888:8888" + - "50051:50051" + - "50053:50053" + expose: + - 8888 + - 50051 + - 50053 + depends_on: + - psql + - goose + + scheduler: + build: + context: .. + dockerfile: ./cicd/scheduler/Dockerfile + volumes: + - ../calendar.log:/calendar.log + depends_on: + - rabbitmq + - calendar + restart: on-failure + + sender: + build: + context: .. + dockerfile: ./cicd/sender/Dockerfile + volumes: + - ../calendar.log:/calendar.log + depends_on: + - rabbitmq + restart: on-failure + + goose: + build: + context: .. + dockerfile: ./cicd/goose/Dockerfile + environment: + GOOSE_DRIVER: "postgres" + GOOSE_DBSTRING: "host=psql port=5432 user=calendar password=12345678 dbname=calendar sslmode=disable" + depends_on: + - psql + restart: on-failure + + psql: + image: postgres:11-alpine + hostname: "psql" + container_name: psql + environment: + POSTGRES_USER: calendar + POSTGRES_PASSWORD: 12345678 + POSTGRES_DB: calendar + ports: + - "5432:5432" + expose: + - 5432 + rabbitmq: image: rabbitmq:3-management-alpine hostname: "rabbitmq" @@ -12,28 +75,6 @@ services: ports: - "5672:5672" - "15672:15672" - #calendar: - # build: - # context: .. - # dockerfile: ./cicd/calendar/Dockerfile - # ports: - # - "8888:8888" - # volumes: - # - ../calendar.log:/calendar.log - #scheduler: - # build: - # context: .. - # dockerfile: ./cicd/scheduler/Dockerfile - # volumes: - # - ../calendar.log:/calendar.log - # depends_on: - # - rabbitmq - # - calendar - #sender: - # build: - # context: .. - # dockerfile: ./cicd/sender/Dockerfile - # volumes: - # - ../calendar.log:/calendar.log - # depends_on: - # - rabbitmq \ No newline at end of file + expose: + - 15672 + - 5672 \ No newline at end of file diff --git a/hw12_13_14_15_calendar/cicd/goose/Dockerfile b/hw12_13_14_15_calendar/cicd/goose/Dockerfile new file mode 100644 index 0000000..13f1d89 --- /dev/null +++ b/hw12_13_14_15_calendar/cicd/goose/Dockerfile @@ -0,0 +1,5 @@ +FROM golang:1.14 +WORKDIR / +RUN go get -u github.com/pressly/goose/cmd/goose +COPY . . +ENTRYPOINT ["/go/bin/goose", "-dir", "migrations", "up"] diff --git a/hw12_13_14_15_calendar/cicd/scheduler/Dockerfile b/hw12_13_14_15_calendar/cicd/scheduler/Dockerfile index 69fa4c4..170b415 100644 --- a/hw12_13_14_15_calendar/cicd/scheduler/Dockerfile +++ b/hw12_13_14_15_calendar/cicd/scheduler/Dockerfile @@ -3,7 +3,7 @@ RUN mkdir -p /app WORKDIR /app COPY . . RUN go get -d ./cmd/scheduler/. -RUN GOOS=linux GOARCH=amd64 go build -a -o scheduler ./cmd/scheduler/. +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o scheduler ./cmd/scheduler/. FROM alpine:latest ENV APP_RABBITMQ_ADDRESS=rabbitmq @@ -19,4 +19,4 @@ ENV APP_LOGGER_FILE=/calendar.log ENV APP_LOGGER_LEVEL=INFO WORKDIR / COPY --from=builder /app/scheduler ./sbin -CMD ["scheduler"] +ENTRYPOINT ["scheduler"] diff --git a/hw12_13_14_15_calendar/cicd/sender/Dockerfile b/hw12_13_14_15_calendar/cicd/sender/Dockerfile index f97bc8a..4b8cb5d 100644 --- a/hw12_13_14_15_calendar/cicd/sender/Dockerfile +++ b/hw12_13_14_15_calendar/cicd/sender/Dockerfile @@ -3,7 +3,7 @@ RUN mkdir -p /app WORKDIR /app COPY . . RUN go get -d ./cmd/sender/. -RUN GOOS=linux GOARCH=amd64 go build -a -o sender ./cmd/sender/. +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o sender ./cmd/sender/. FROM alpine:latest ENV APP_RABBITMQ_ADDRESS=rabbitmq @@ -17,4 +17,4 @@ ENV APP_LOGGER_FILE=/calendar.log ENV APP_LOGGER_LEVEL=INFO WORKDIR / COPY --from=builder /app/sender ./sbin -CMD ["sender"] +ENTRYPOINT ["sender"] diff --git a/hw12_13_14_15_calendar/cmd/calendar/main_test.go b/hw12_13_14_15_calendar/cmd/calendar/main_test.go index 9828b81..baf13bc 100644 --- a/hw12_13_14_15_calendar/cmd/calendar/main_test.go +++ b/hw12_13_14_15_calendar/cmd/calendar/main_test.go @@ -9,10 +9,10 @@ import ( "github.com/stretchr/testify/require" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/api/public" "log" + "os" "sync" "testing" "time" - "os" ) var _ = func() bool { @@ -39,12 +39,13 @@ var testEvent02 = public.CreateReq{ NotifyTime: dur2pbduration(5 * time.Minute), UserID: 2222, } + func TestMain(m *testing.M) { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) go func(ctx context.Context) { main() }(ctx) - time.Sleep(1*time.Second) + time.Sleep(1 * time.Second) c := m.Run() @@ -116,29 +117,29 @@ func TestPublicGRPCEndpoint(t *testing.T) { require.NoError(t, err) require.GreaterOrEqual(t, len(list.Events), 2) var e1, e2 bool - for _,v := range list.Events { - if v.ID==resp1.ID { - e1=true + for _, v := range list.Events { + if v.ID == resp1.ID { + e1 = true } - if v.ID==resp2.ID { - e2=true + if v.ID == resp2.ID { + e2 = true } } require.True(t, e1) require.True(t, e2) }) - t.Run("test public GRPC.Create and GRPC.GetByDate", func(t *testing.T){ + t.Run("test public GRPC.Create and GRPC.GetByDate", func(t *testing.T) { defer wg.Done() resp1, err := publicAPI.Create(ctx, &testEvent01) require.NoError(t, err) - list, err := publicAPI.GetByDate(ctx,&public.GetByDateReq{Date: testEvent01.Date, Range: public.QueryRange_DAY}) + list, err := publicAPI.GetByDate(ctx, &public.GetByDateReq{Date: testEvent01.Date, Range: public.QueryRange_DAY}) require.NoError(t, err) require.GreaterOrEqual(t, len(list.Events), 2) var e1 bool - for _,v := range list.Events { - if v.ID==resp1.ID { - e1=true + for _, v := range list.Events { + if v.ID == resp1.ID { + e1 = true } } require.True(t, e1) @@ -211,29 +212,29 @@ func TestPublicAPIEndpoint(t *testing.T) { require.NoError(t, err) require.GreaterOrEqual(t, len(list.Events), 2) var e1, e2 bool - for _,v := range list.Events { - if v.ID==resp1.ID { - e1=true + for _, v := range list.Events { + if v.ID == resp1.ID { + e1 = true } - if v.ID==resp2.ID { - e2=true + if v.ID == resp2.ID { + e2 = true } } require.True(t, e1) require.True(t, e2) }) - t.Run("test public GRPC.Create and GRPC.GetByDate", func(t *testing.T){ + t.Run("test public GRPC.Create and GRPC.GetByDate", func(t *testing.T) { defer wg.Done() resp1, err := publicAPI.Create(ctx, &testEvent01) require.NoError(t, err) - list, err := publicAPI.GetByDate(ctx,&public.GetByDateReq{Date: testEvent01.Date, Range: public.QueryRange_DAY}) + list, err := publicAPI.GetByDate(ctx, &public.GetByDateReq{Date: testEvent01.Date, Range: public.QueryRange_DAY}) require.NoError(t, err) require.GreaterOrEqual(t, len(list.Events), 2) var e1 bool - for _,v := range list.Events { - if v.ID==resp1.ID { - e1=true + for _, v := range list.Events { + if v.ID == resp1.ID { + e1 = true } } require.True(t, e1) @@ -242,7 +243,6 @@ func TestPublicAPIEndpoint(t *testing.T) { wg.Wait() } - func time2pbtimestamp(t time.Time) *timestamp.Timestamp { r, err := ptypes.TimestampProto(t) if err != nil { diff --git a/hw12_13_14_15_calendar/cmd/scheduler/main.go b/hw12_13_14_15_calendar/cmd/scheduler/main.go index 7093f26..b94c8d9 100644 --- a/hw12_13_14_15_calendar/cmd/scheduler/main.go +++ b/hw12_13_14_15_calendar/cmd/scheduler/main.go @@ -2,14 +2,13 @@ package main import ( "flag" + _ "github.com/go-sql-driver/mysql" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/sheduler" + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/config" "log" "os" "os/signal" "syscall" - - _ "github.com/go-sql-driver/mysql" - "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/config" ) var configFile string diff --git a/hw12_13_14_15_calendar/cmd/sender/main.go b/hw12_13_14_15_calendar/cmd/sender/main.go index 62cafba..1b89096 100644 --- a/hw12_13_14_15_calendar/cmd/sender/main.go +++ b/hw12_13_14_15_calendar/cmd/sender/main.go @@ -2,14 +2,13 @@ package main import ( "flag" + _ "github.com/go-sql-driver/mysql" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/sender" + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/config" "log" "os" "os/signal" "syscall" - - _ "github.com/go-sql-driver/mysql" - "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/config" ) var configFile string diff --git a/hw12_13_14_15_calendar/migrations/20200924000000_init.sql b/hw12_13_14_15_calendar/migrations/20200924000000_init.sql index 5d48a9b..834aaae 100644 --- a/hw12_13_14_15_calendar/migrations/20200924000000_init.sql +++ b/hw12_13_14_15_calendar/migrations/20200924000000_init.sql @@ -1,16 +1,15 @@ -- +goose Up -- +goose StatementBegin CREATE TABLE events ( - id int(16) NOT NULL AUTO_INCREMENT, + id serial NOT NULL, title varchar(255) NOT NULL, - date datetime NOT NULL, - latency int(16) NOT NULL, - note text, - userID int(16), - notifyTime int(16), + date timestamptz NOT NULL, + latency int8 NOT NULL, + note text NULL, + userID int8 NOT NULL, + notifyTime timestamptz NULL, notified bool - PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8;; +); -- +goose StatementEnd -- +goose Down diff --git a/hw12_13_14_15_calendar/pkg/api/public/grpcclient.go b/hw12_13_14_15_calendar/pkg/api/public/grpcclient.go index a81ecc9..1a2a34b 100644 --- a/hw12_13_14_15_calendar/pkg/api/public/grpcclient.go +++ b/hw12_13_14_15_calendar/pkg/api/public/grpcclient.go @@ -3,8 +3,9 @@ package public import ( "context" "fmt" - "google.golang.org/grpc" "net" + + "google.golang.org/grpc" ) func NewClient(ctx context.Context, addr, port string) (GrpcClient, error) { diff --git a/hw12_13_14_15_calendar/cmd/integration/calendar.go b/hw12_13_14_15_calendar/test/main.go similarity index 91% rename from hw12_13_14_15_calendar/cmd/integration/calendar.go rename to hw12_13_14_15_calendar/test/main.go index 5be34ea..6b52288 100644 --- a/hw12_13_14_15_calendar/cmd/integration/calendar.go +++ b/hw12_13_14_15_calendar/test/main.go @@ -35,11 +35,11 @@ var testEvent02 = public.CreateReq{ func main() { if err := TestPublicAPIEndpoint(); err != nil { - log.Fatalf("TestPublicAPIEndpoint FAIL: %w",err) + log.Fatalf("TestPublicAPIEndpoint FAIL: %w", err) } if err := TestPublicGRPCEndpoint(); err != nil { - log.Fatalf("TestPublicGRPCEndpoint FAIL: %w",err) + log.Fatalf("TestPublicGRPCEndpoint FAIL: %w", err) } } @@ -69,7 +69,6 @@ func TestPublicGRPCEndpoint() error { require.Equal(t, testEvent01.Date.Seconds, resp2.Events[0].Date.Seconds) require.Equal(t, testEvent01.Note, resp2.Events[0].Note) }) - t.Run("test public GRPC.Create, GRPC.Update and GRPC.GetById", func(t *testing.T) { defer wg.Done() resp1, err := publicAPI.Create(ctx, &testEvent01) @@ -85,7 +84,6 @@ func TestPublicGRPCEndpoint() error { require.Equal(t, testEvent02.Date.Seconds, resp2.Events[0].Date.Seconds) require.Equal(t, testEvent02.Note, resp2.Events[0].Note) }) - t.Run("test public GRPC.Create, GRPC.Delete and GRPC.GetById", func(t *testing.T) { defer wg.Done() resp1, err := publicAPI.Create(ctx, &testEvent01) @@ -97,7 +95,6 @@ func TestPublicGRPCEndpoint() error { require.Error(t, err) require.Nil(t, resp2) }) - t.Run("test public GRPC.Create and GRPC.List", func(t *testing.T) { defer wg.Done() resp1, err := publicAPI.Create(ctx, &testEvent01) @@ -110,34 +107,32 @@ func TestPublicGRPCEndpoint() error { require.NoError(t, err) require.GreaterOrEqual(t, len(list.Events), 2) var e1, e2 bool - for _,v := range list.Events { - if v.ID==resp1.ID { - e1=true + for _, v := range list.Events { + if v.ID == resp1.ID { + e1 = true } - if v.ID==resp2.ID { - e2=true + if v.ID == resp2.ID { + e2 = true } } require.True(t, e1) require.True(t, e2) }) - - t.Run("test public GRPC.Create and GRPC.GetByDate", func(t *testing.T){ + t.Run("test public GRPC.Create and GRPC.GetByDate", func(t *testing.T) { defer wg.Done() resp1, err := publicAPI.Create(ctx, &testEvent01) require.NoError(t, err) - list, err := publicAPI.GetByDate(ctx,&public.GetByDateReq{Date: testEvent01.Date, Range: public.QueryRange_DAY}) + list, err := publicAPI.GetByDate(ctx, &public.GetByDateReq{Date: testEvent01.Date, Range: public.QueryRange_DAY}) require.NoError(t, err) require.GreaterOrEqual(t, len(list.Events), 2) var e1 bool - for _,v := range list.Events { - if v.ID==resp1.ID { - e1=true + for _, v := range list.Events { + if v.ID == resp1.ID { + e1 = true } } require.True(t, e1) }) - wg.Wait() } @@ -205,29 +200,29 @@ func TestPublicAPIEndpoint() error { require.NoError(t, err) require.GreaterOrEqual(t, len(list.Events), 2) var e1, e2 bool - for _,v := range list.Events { - if v.ID==resp1.ID { - e1=true + for _, v := range list.Events { + if v.ID == resp1.ID { + e1 = true } - if v.ID==resp2.ID { - e2=true + if v.ID == resp2.ID { + e2 = true } } require.True(t, e1) require.True(t, e2) }) - t.Run("test public GRPC.Create and GRPC.GetByDate", func(t *testing.T){ + t.Run("test public GRPC.Create and GRPC.GetByDate", func(t *testing.T) { defer wg.Done() resp1, err := publicAPI.Create(ctx, &testEvent01) require.NoError(t, err) - list, err := publicAPI.GetByDate(ctx,&public.GetByDateReq{Date: testEvent01.Date, Range: public.QueryRange_DAY}) + list, err := publicAPI.GetByDate(ctx, &public.GetByDateReq{Date: testEvent01.Date, Range: public.QueryRange_DAY}) require.NoError(t, err) require.GreaterOrEqual(t, len(list.Events), 2) var e1 bool - for _,v := range list.Events { - if v.ID==resp1.ID { - e1=true + for _, v := range list.Events { + if v.ID == resp1.ID { + e1 = true } } require.True(t, e1) @@ -236,7 +231,6 @@ func TestPublicAPIEndpoint() error { wg.Wait() } - func time2pbtimestamp(t time.Time) *timestamp.Timestamp { r, err := ptypes.TimestampProto(t) if err != nil {