-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve docker architecture and scalability
Add compose base Signed-off-by: Vasek - Tom C <[email protected]>
- Loading branch information
Showing
25 changed files
with
511 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,46 @@ | ||
package codebase | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"dagger.io/dockersdk/dockerfile" | ||
"dagger.io/dockersdk/codebase/dockercompose" | ||
"dagger.io/dockersdk/codebase/dockerfile" | ||
) | ||
|
||
const CodebasePath = "/app" | ||
|
||
|
||
type Codebase struct { | ||
dockerfile *dockerfile.Dockerfile | ||
dockerfile *dockerfile.Dockerfile | ||
dockercompose *dockercompose.DockerCompose | ||
} | ||
|
||
func New() (*Codebase, error) { | ||
dockerfile, exists, err := getDockerfile(CodebasePath) | ||
func New(ctx context.Context) (*Codebase, error) { | ||
dir, err := os.ReadDir(CodebasePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read source directory: %w", err) | ||
} | ||
|
||
dockerfile, dockerfileExists, err := getDockerfile(CodebasePath, dir) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get Dockerfile: %w", err) | ||
} | ||
|
||
if !exists { | ||
return nil, fmt.Errorf("Dockerfile not found in %s", CodebasePath) | ||
dockercompose, composeExistsExists, err := getDockerCompose(ctx, CodebasePath, dir) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get docker-compose file: %w", err) | ||
} | ||
|
||
// TODO: Check for sub directories/dockerfile | ||
if !dockerfileExists && !composeExistsExists { | ||
return nil, fmt.Errorf("Dockerfile or docker-compose.yml not found in user project") | ||
} | ||
|
||
return &Codebase{dockerfile: dockerfile}, nil | ||
} | ||
// TODO: Check for sub directories/dockerfile | ||
|
||
func (c *Codebase) Dockerfile() *dockerfile.Dockerfile { | ||
return c.dockerfile | ||
return &Codebase{ | ||
dockerfile: dockerfile, | ||
dockercompose: dockercompose, | ||
}, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package dockercompose | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/compose-spec/compose-go/loader" | ||
"github.com/compose-spec/compose-go/types" | ||
) | ||
|
||
type DockerCompose struct { | ||
filename string | ||
project *types.Project | ||
} | ||
|
||
func NewDockerCompose(ctx context.Context, filename string, content []byte) (*DockerCompose, error) { | ||
project, err := loader.LoadWithContext(ctx, types.ConfigDetails{ | ||
ConfigFiles: []types.ConfigFile{ | ||
{ | ||
Config: map[string]interface{}{ | ||
"name": "dockersdk", | ||
}, | ||
}, | ||
{ | ||
Content: content, | ||
}, | ||
}, | ||
}, loader.WithSkipValidation) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load %s: %w", filename, err) | ||
} | ||
|
||
return &DockerCompose{ | ||
filename: filename, | ||
project: project, | ||
}, nil | ||
} | ||
|
||
func (d *DockerCompose) Services() []*Service { | ||
services := make([]*Service, len(d.project.Services)) | ||
|
||
for i, service := range d.project.Services { | ||
services[i] = NewService(&service) | ||
} | ||
|
||
return services | ||
} | ||
|
||
func (d *DockerCompose) String() string { | ||
yaml, err := d.project.MarshalYAML() | ||
if err != nil { | ||
return "could not display docker-compose.yml" | ||
} | ||
|
||
return string(yaml) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dockercompose | ||
|
||
import "github.com/compose-spec/compose-go/types" | ||
|
||
type Service struct { | ||
s *types.ServiceConfig | ||
} | ||
|
||
func NewService(service *types.ServiceConfig) *Service { | ||
return &Service{ | ||
s: service, | ||
} | ||
} | ||
|
||
func (s *Service) Name() string { | ||
return s.s.Name | ||
} | ||
|
||
func (s *Service) Image() string { | ||
return s.s.Image | ||
} | ||
|
||
func (s *Service) Workdir() string { | ||
return s.s.WorkingDir | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package codebase | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"dagger.io/dockersdk/codebase/dockercompose" | ||
"dagger.io/dockersdk/codebase/dockerfile" | ||
) | ||
|
||
func getDockerfile(dirPath string, dir []os.DirEntry) (*dockerfile.Dockerfile, bool, error) { | ||
for _, entry := range dir { | ||
if entry.Name() == "Dockerfile" || strings.HasSuffix(entry.Name(), ".Dockerfile") { | ||
file, err := os.Open(dirPath + "/" + entry.Name()) | ||
if err != nil { | ||
return nil, true, fmt.Errorf("failed to open Dockerfile: %w", err) | ||
} | ||
defer file.Close() | ||
|
||
dockerfile, err := dockerfile.NewDockerfile(entry.Name(), file) | ||
if err != nil { | ||
return nil, true, fmt.Errorf("failed to parse Dockerfile: %w", err) | ||
} | ||
|
||
return dockerfile, true, nil | ||
} | ||
} | ||
|
||
return nil, false, nil | ||
} | ||
|
||
func getDockerCompose(ctx context.Context,dirPath string, dir []os.DirEntry) (*dockercompose.DockerCompose, bool, error) { | ||
for _, entry := range dir { | ||
if entry.Name() == "docker-compose.yml" || entry.Name() == "docker-compose.yaml" { | ||
fileContent, err := os.ReadFile(dirPath + "/" + entry.Name()) | ||
if err != nil { | ||
return nil, true, fmt.Errorf("failed to get %s content: %w", entry.Name(), err) | ||
} | ||
|
||
compose, err := dockercompose.NewDockerCompose(ctx, entry.Name(), fileContent) | ||
if err != nil { | ||
return nil, true, fmt.Errorf("failed to parse docker-compose.yml: %w", err) | ||
} | ||
|
||
return compose, true, nil | ||
} | ||
} | ||
|
||
return nil, false, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package codebase | ||
|
||
import ( | ||
"fmt" | ||
|
||
"dagger.io/dockersdk/module" | ||
"dagger.io/dockersdk/module/docker" | ||
) | ||
|
||
func (c *Codebase) ToModule(name string) *module.Module { | ||
dockerModule := docker.New("Docker") | ||
|
||
if c.dockerfile != nil { | ||
dockerModule = dockerModule.WithDockerfile(c.dockerfile) | ||
} | ||
|
||
if c.dockercompose != nil { | ||
fmt.Println(c.dockercompose) | ||
} | ||
|
||
if c.dockercompose != nil { | ||
for _, service := range c.dockercompose.Services() { | ||
dockerModule = dockerModule.WithService(service) | ||
} | ||
} | ||
|
||
return module.Build(name, dockerModule) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.