Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5218ef1

Browse files
committed
fix(engine): build server without disabling CGO on alpine
1 parent bff80c8 commit 5218ef1

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

engine/Dockerfile.dblab-server

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# See Guides to learn how to start a container: https://postgres.ai/docs/how-to-guides/administration/engine-manage
2+
FROM golang:1.17-alpine as builder
3+
RUN apk update && apk add --no-cache make gcc g++
4+
5+
WORKDIR /app
6+
ADD . /app
7+
8+
RUN make build-engine
29

310
FROM docker:19.03.15
411

@@ -14,7 +21,7 @@ ENV PATH="${PATH}:/usr/share/bcc/tools"
1421

1522
WORKDIR /home/dblab
1623

17-
COPY ./bin/dblab-server ./bin/dblab-server
24+
COPY --from=builder /app/bin/dblab-server ./bin/dblab-server
1825
COPY ./configs/standard ./standard
1926
COPY ./api ./api
2027
COPY ./scripts ./scripts

engine/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LDFLAGS = -ldflags "-s -w \
1818
-X gitlab.com/postgres-ai/database-lab/v3/version.buildTime=${BUILD_TIME}"
1919

2020
# Go tooling command aliases
21-
GOBUILD = GO111MODULE=on CGO_ENABLED=0 GOARCH=${GOARCH} go build ${LDFLAGS}
21+
GOBUILD = GO111MODULE=on CGO_ENABLED=1 GOARCH=${GOARCH} go build ${LDFLAGS}
2222
GOTEST = GO111MODULE=on go test -race
2323

2424
CLIENT_PLATFORMS=darwin linux freebsd windows
@@ -44,6 +44,9 @@ build: ## Build binary files of all Database Lab components (Engine, CI Checker,
4444
${GOBUILD} -o bin/${RUN_CI_BINARY} ./cmd/runci/main.go
4545
${GOBUILD} -o bin/${CLI_BINARY} ./cmd/cli/main.go
4646

47+
build-engine: ## Build binary files of all Database Lab components (Engine, CI Checker, CLI)
48+
${GOBUILD} -o bin/${SERVER_BINARY} ./cmd/database-lab/main.go
49+
4750
build-ci-checker: ## Build the Database Lab CI Checker binary
4851
${GOBUILD} -o bin/${RUN_CI_BINARY} ./cmd/runci/main.go
4952

@@ -72,4 +75,4 @@ fmt: ## Format code
7275
clean: ## Remove compiled binaries from the local bin/ directory
7376
rm -f bin/*
7477

75-
.PHONY: help all build test run-lint install-lint lint fmt clean build-image build-dle build-ci-checker build-client build-ci-checker
78+
.PHONY: help all build test run-lint install-lint lint fmt clean build-image build-dle build-engine build-client build-ci-checker

engine/internal/schema/schema.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ import (
1616
"github.com/docker/docker/client"
1717
"github.com/docker/docker/pkg/stdcopy"
1818

19+
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools"
1920
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/cont"
2021
"gitlab.com/postgres-ai/database-lab/v3/pkg/log"
2122
"gitlab.com/postgres-ai/database-lab/v3/pkg/models"
2223
"gitlab.com/postgres-ai/database-lab/v3/pkg/util"
2324
"gitlab.com/postgres-ai/database-lab/v3/pkg/util/networks"
2425
)
2526

27+
const schemaDiffImage = "supabase/pgadmin-schema-diff"
28+
2629
// Diff defines a schema generator.
2730
type Diff struct {
2831
cli *client.Client
@@ -34,6 +37,7 @@ func NewDiff(cli *client.Client) *Diff {
3437
}
3538

3639
func connStr(clone *models.Clone) string {
40+
// TODO: fix params
3741
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s",
3842
clone.DB.Username,
3943
"test", // clone.DB.Password,
@@ -50,18 +54,13 @@ func (d *Diff) GenerateDiff(actual, origin *models.Clone, instanceID string) (st
5054

5155
ctx := context.Background()
5256

53-
if _, err := d.watchCloneStatus(ctx, origin, origin.Status.Code); err != nil {
54-
return "", fmt.Errorf("failed to watch the clone status: %w", err)
55-
}
56-
57-
reader, err := d.cli.ImagePull(ctx, "supabase/pgadmin-schema-diff", types.ImagePullOptions{})
58-
if err != nil {
59-
return "", fmt.Errorf("failed to pull image: %w", err)
57+
if origin.Status.Code != models.StatusOK {
58+
if _, err := d.watchCloneStatus(ctx, origin, origin.Status.Code); err != nil {
59+
return "", fmt.Errorf("failed to watch the clone status: %w", err)
60+
}
6061
}
6162

62-
defer func() { _ = reader.Close() }()
63-
64-
if _, err := io.Copy(os.Stdout, reader); err != nil && err != io.EOF {
63+
if err := tools.PullImage(ctx, d.cli, schemaDiffImage); err != nil {
6564
return "", fmt.Errorf("failed to pull image: %w", err)
6665
}
6766

@@ -70,9 +69,8 @@ func (d *Diff) GenerateDiff(actual, origin *models.Clone, instanceID string) (st
7069
Labels: map[string]string{
7170
cont.DBLabControlLabel: cont.DBLabSchemaDiff,
7271
cont.DBLabInstanceIDLabel: instanceID,
73-
// cont.DBLabEngineNameLabel: d.engineProps.ContainerName,
7472
},
75-
Image: "supabase/pgadmin-schema-diff",
73+
Image: schemaDiffImage,
7674
Cmd: []string{
7775
connStr(actual),
7876
connStr(origin),
@@ -96,6 +94,15 @@ func (d *Diff) GenerateDiff(actual, origin *models.Clone, instanceID string) (st
9694
return "", fmt.Errorf("failed to create diff container: %w", err)
9795
}
9896

97+
defer func() {
98+
if err := d.cli.ContainerRemove(ctx, diffCont.ID, types.ContainerRemoveOptions{
99+
RemoveVolumes: true,
100+
Force: true,
101+
}); err != nil {
102+
log.Err("failed to remove the diff container:", diffCont.ID, err)
103+
}
104+
}()
105+
99106
statusCh, errCh := d.cli.ContainerWait(ctx, diffCont.ID, container.WaitConditionNotRunning)
100107
select {
101108
case err := <-errCh:
@@ -117,12 +124,12 @@ func (d *Diff) GenerateDiff(actual, origin *models.Clone, instanceID string) (st
117124
return "", fmt.Errorf("failed to copy container output: %w", err)
118125
}
119126

120-
stringsB, err := filterOutput(buf)
127+
filteredOutput, err := filterOutput(buf)
121128
if err != nil {
122129
return "", fmt.Errorf("failed to filter output: %w", err)
123130
}
124131

125-
return stringsB.String(), nil
132+
return filteredOutput.String(), nil
126133
}
127134

128135
// watchCloneStatus checks the clone status for changing.
@@ -157,13 +164,13 @@ func (d *Diff) watchCloneStatus(ctx context.Context, clone *models.Clone, initia
157164
}
158165

159166
func filterOutput(b *bytes.Buffer) (*strings.Builder, error) {
160-
strB := &strings.Builder{}
167+
filteredBuilder := &strings.Builder{}
161168

162169
for {
163170
line, err := b.ReadBytes('\n')
164171
if err != nil {
165172
if err == io.EOF {
166-
return strB, nil
173+
return filteredBuilder, nil
167174
}
168175

169176
return nil, err
@@ -173,6 +180,6 @@ func filterOutput(b *bytes.Buffer) (*strings.Builder, error) {
173180
continue
174181
}
175182

176-
strB.Write(line)
183+
filteredBuilder.Write(line)
177184
}
178185
}

engine/internal/srv/routes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ func (s *Server) schemaDiff(w http.ResponseWriter, r *http.Request) {
563563
Username: clone.DB.Username,
564564
DBName: clone.DB.DBName,
565565
//Password: pwd,
566-
Password: "test",
566+
Password: "test", // TODO: update
567567
},
568568
Snapshot: &types.SnapshotCloneFieldRequest{ID: clone.Snapshot.ID},
569569
})
@@ -597,7 +597,7 @@ func (s *Server) schemaDiff(w http.ResponseWriter, r *http.Request) {
597597

598598
log.Dbg("Optimized Diff:", optimizedDiff)
599599

600-
if _, err := w.Write([]byte(generateDiff)); err != nil {
600+
if _, err := w.Write([]byte(optimizedDiff)); err != nil {
601601
api.SendError(w, r, err)
602602
return
603603
}

0 commit comments

Comments
 (0)