Skip to content

Commit 862f66a

Browse files
authored
fix: generate database ID earlier and pull Docker image before container run (#55)
2 parents 5dc1697 + aa80006 commit 862f66a

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

server/internal/api/convert.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ func apiToDatabaseSpec(id, tID *string, apiSpec *api.DatabaseSpec) (*database.Sp
417417
if err != nil {
418418
return nil, fmt.Errorf("failed to parse database ID: %w", err)
419419
}
420+
if databaseID == uuid.Nil {
421+
databaseID = uuid.New()
422+
}
420423
var tenantID *uuid.UUID
421424
if tID != nil {
422425
parsedTenantID, err := parseUUID(*tID)

server/internal/database/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewService(orchestrator Orchestrator, store *Store, hostSvc *host.Service)
3333

3434
func (s *Service) CreateDatabase(ctx context.Context, spec *Spec) (*Database, error) {
3535
if spec.DatabaseID == uuid.Nil {
36-
spec.DatabaseID = uuid.New()
36+
return nil, fmt.Errorf("DatabaseID must be set before creating a Database")
3737
}
3838
specExists, err := s.store.Spec.ExistsByKey(spec.DatabaseID).Exec(ctx)
3939
if err != nil {

server/internal/docker/docker.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/docker/docker/api/types"
1616
"github.com/docker/docker/api/types/container"
1717
"github.com/docker/docker/api/types/filters"
18+
"github.com/docker/docker/api/types/image"
1819
"github.com/docker/docker/api/types/mount"
1920
"github.com/docker/docker/api/types/network"
2021
"github.com/docker/docker/api/types/swarm"
@@ -84,6 +85,14 @@ type ContainerRunOptions struct {
8485
}
8586

8687
func (d *Docker) ContainerRun(ctx context.Context, opts ContainerRunOptions) (string, error) {
88+
if opts.Config != nil {
89+
if opts.Config.Image == "" {
90+
return "", errors.New("image must be specified in container config")
91+
}
92+
if err := d.ensureDockerImage(ctx, opts.Config.Image); err != nil {
93+
return "", fmt.Errorf("failed to ensure docker image %q: %w", opts.Config.Image, err)
94+
}
95+
}
8796
resp, err := d.client.ContainerCreate(ctx, opts.Config, opts.Host, opts.Net, opts.Platform, opts.Name)
8897
if err != nil {
8998
return "", fmt.Errorf("failed to create container: %w", err)
@@ -453,6 +462,22 @@ func (d *Docker) Shutdown() error {
453462
return nil
454463
}
455464

465+
func (d *Docker) ensureDockerImage(ctx context.Context, img string) error {
466+
// Pull the image
467+
reader, err := d.client.ImagePull(ctx, img, image.PullOptions{})
468+
if err != nil {
469+
return fmt.Errorf("failed to pull image %q: %w", img, err)
470+
}
471+
defer reader.Close()
472+
473+
// Read the output from the pull operation
474+
if _, err := io.Copy(io.Discard, reader); err != nil {
475+
return fmt.Errorf("failed to read image pull output: %w", err)
476+
}
477+
478+
return nil
479+
}
480+
456481
type NetworkInfo struct {
457482
Name string
458483
ID string

0 commit comments

Comments
 (0)