Skip to content

Commit

Permalink
wip: add entrypoints to services and targets
Browse files Browse the repository at this point in the history
  • Loading branch information
YuukanOO committed May 15, 2024
1 parent cae1223 commit 5a33528
Show file tree
Hide file tree
Showing 47 changed files with 2,730 additions and 1,656 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
examples
Makefile
Dockerfile
compose.yml
docs
Expand Down
4 changes: 2 additions & 2 deletions cmd/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
)

const (
databaseFilename = "seelf.db?_foreign_keys=yes&_txlock=immediate"
databaseConnectionString = "seelf.db?_journal=WAL&_timeout=5000&_foreign_keys=yes&_txlock=immediate"
defaultConfigFilename = "conf.yml"
defaultPort = 8080
defaultHost = ""
Expand Down Expand Up @@ -189,7 +189,7 @@ func (c *configuration) IsSecure() bool {

// Gets the connection string to be used.
func (c *configuration) ConnectionString() string {
return "file:" + path.Join(c.Data.Path, databaseFilename)
return "file:" + path.Join(c.Data.Path, databaseConnectionString)
}

// Returns the address to bind the HTTP server to.
Expand Down
1 change: 1 addition & 0 deletions examples/multiple-ports/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Makefile
Dockerfile
compose.yml
3 changes: 3 additions & 0 deletions examples/multiple-ports/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
archive:
rm -rf multiple-ports.tar.gz
tar czf multiple-ports.tar.gz *
10 changes: 5 additions & 5 deletions examples/multiple-ports/compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
services:
app:
build: .
command: -web 8080,8081 -tcp 8082,8083 -udp 8084,8085
command: -web 8080,8081,8082 -tcp 8083,8084,8085 -udp 8086,8087,8088
ports:
- "8080:8080"
- "8081:8081"
- "8082:8082/tcp"
- "8081-8082:8081-8082"
- "8083:8083/tcp"
- "8084:8084/udp"
- "8085:8085/udp"
- "8084-8085:8084-8085/tcp"
- "8086:8086/udp"
- "8087-8088:8087-8088/udp"
Binary file added examples/multiple-ports/multiple-ports.tar.gz
Binary file not shown.
1 change: 1 addition & 0 deletions examples/sveltekit-hello/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Makefile
Dockerfile
compose.yml
node_modules
6 changes: 3 additions & 3 deletions internal/deployment/app/cleanup_app/cleanup_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func Test_CleanupApp(t *testing.T) {
target := must.Panic(domain.NewTarget("my-target",
domain.NewTargetUrlRequirement(must.Panic(domain.UrlFrom("http://localhost")), true),
domain.NewProviderConfigRequirement(nil, true), "uid"))
target.Configured(target.CurrentVersion(), nil)
target.Configured(target.CurrentVersion(), nil, nil)
target.RequestCleanup(false, "uid")

uc, provider := sut(initialData{
Expand All @@ -90,7 +90,7 @@ func Test_CleanupApp(t *testing.T) {
target := must.Panic(domain.NewTarget("my-target",
domain.NewTargetUrlRequirement(must.Panic(domain.UrlFrom("http://localhost")), true),
domain.NewProviderConfigRequirement(nil, true), "uid"))
target.Configured(target.CurrentVersion(), nil)
target.Configured(target.CurrentVersion(), nil, nil)

uc, provider := sut(initialData{
targets: []*domain.Target{&target},
Expand All @@ -108,7 +108,7 @@ func Test_CleanupApp(t *testing.T) {
target := must.Panic(domain.NewTarget("my-target",
domain.NewTargetUrlRequirement(must.Panic(domain.UrlFrom("http://localhost")), true),
domain.NewProviderConfigRequirement(nil, true), "uid"))
target.Configured(target.CurrentVersion(), nil)
target.Configured(target.CurrentVersion(), nil, nil)
app := must.Panic(domain.NewApp("my-app",
domain.NewEnvironmentConfigRequirement(domain.NewEnvironmentConfig(target.ID()), true, true),
domain.NewEnvironmentConfigRequirement(domain.NewEnvironmentConfig(target.ID()), true, true), "uid"))
Expand Down
4 changes: 2 additions & 2 deletions internal/deployment/app/cleanup_target/cleanup_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Test_CleanupTarget(t *testing.T) {
target := must.Panic(domain.NewTarget("my-target",
domain.NewTargetUrlRequirement(must.Panic(domain.UrlFrom("http://localhost")), true),
domain.NewProviderConfigRequirement(nil, true), "uid"))
target.Configured(target.CurrentVersion(), errors.New("some error"))
target.Configured(target.CurrentVersion(), nil, errors.New("some error"))

uc, provider := sut(&target)

Expand All @@ -50,7 +50,7 @@ func Test_CleanupTarget(t *testing.T) {
target := must.Panic(domain.NewTarget("my-target",
domain.NewTargetUrlRequirement(must.Panic(domain.UrlFrom("http://localhost")), true),
domain.NewProviderConfigRequirement(nil, true), "uid"))
target.Configured(target.CurrentVersion(), nil)
target.Configured(target.CurrentVersion(), nil, nil)

uc, provider := sut(&target)

Expand Down
6 changes: 4 additions & 2 deletions internal/deployment/app/configure_target/configure_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func Handler(
return bus.Unit, nil
}

var assigned domain.TargetEntrypointsAssigned

// Same as for the deployment, since the configuration can take some time, retrieve the latest
// target version before updating its state.
defer func() {
Expand All @@ -56,12 +58,12 @@ func Handler(
return
}

target.Configured(cmd.Version, finalErr)
target.Configured(cmd.Version, assigned, finalErr)

finalErr = writer.Write(ctx, &target)
}()

finalErr = provider.Setup(ctx, target)
assigned, finalErr = provider.Setup(ctx, target)

return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type dummyProvider struct {
called bool
}

func (d *dummyProvider) Setup(context.Context, domain.Target) error {
func (d *dummyProvider) Setup(context.Context, domain.Target) (domain.TargetEntrypointsAssigned, error) {
d.called = true
return d.err
return nil, d.err
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package configure_target

import (
"context"

"github.com/YuukanOO/seelf/internal/deployment/domain"
"github.com/YuukanOO/seelf/pkg/bus"
)

func OnDeploymentStateChangedHandler(
reader domain.TargetsReader,
writer domain.TargetsWriter,
) bus.SignalHandler[domain.DeploymentStateChanged] {
return func(ctx context.Context, evt domain.DeploymentStateChanged) error {
if evt.State.Status() != domain.DeploymentStatusSucceeded {
return nil
}

target, err := reader.GetByID(ctx, evt.Config.Target())

if err != nil {
return err
}

target.MustExpose(evt.ID.AppID(), evt.Config.Environment(), evt.State.Services().Get(domain.Services{}))

return writer.Write(ctx, &target)
}
}
9 changes: 5 additions & 4 deletions internal/deployment/app/create_target/create_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func (*dummyProvider) Prepare(ctx context.Context, payload any, existing ...doma
return dummyConfig{}, nil
}

func (dummyConfig) Fingerprint() string { return "dummy" }
func (c dummyConfig) Equals(o domain.ProviderConfig) bool { return c == o }
func (dummyConfig) Kind() string { return "dummy" }
func (dummyConfig) String() string { return "dummy" }
func (dummyConfig) Fingerprint() string { return "dummy" }
func (c dummyConfig) Equals(o domain.ProviderConfig) bool { return c == o }
func (dummyConfig) Kind() string { return "dummy" }
func (dummyConfig) String() string { return "dummy" }
func (dummyConfig) FilterEntrypoints(e []domain.Entrypoint) []domain.Entrypoint { return e }
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_DeleteTarget(t *testing.T) {
target := must.Panic(domain.NewTarget("my-target",
domain.NewTargetUrlRequirement(must.Panic(domain.UrlFrom("http://localhost")), true),
domain.NewProviderConfigRequirement(nil, true), "uid"))
target.Configured(target.CurrentVersion(), nil)
target.Configured(target.CurrentVersion(), nil, nil)
testutil.IsNil(t, target.RequestCleanup(false, "uid"))

uc, provider := sut(&target)
Expand Down
6 changes: 3 additions & 3 deletions internal/deployment/app/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func Test_Deploy(t *testing.T) {
target := must.Panic(domain.NewTarget("my-target",
domain.NewTargetUrlRequirement(must.Panic(domain.UrlFrom("http://localhost")), true),
domain.NewProviderConfigRequirement(nil, true), "some-uid"))
target.Configured(target.CurrentVersion(), nil)
target.Configured(target.CurrentVersion(), nil, nil)

app := must.Panic(domain.NewApp("my-app",
domain.NewEnvironmentConfigRequirement(domain.NewEnvironmentConfig(target.ID()), true, true),
Expand Down Expand Up @@ -113,7 +113,7 @@ func Test_Deploy(t *testing.T) {
target := must.Panic(domain.NewTarget("my-target",
domain.NewTargetUrlRequirement(must.Panic(domain.UrlFrom("http://localhost")), true),
domain.NewProviderConfigRequirement(nil, true), "some-uid"))
target.Configured(target.CurrentVersion(), nil)
target.Configured(target.CurrentVersion(), nil, nil)

app := must.Panic(domain.NewApp("my-app",
domain.NewEnvironmentConfigRequirement(domain.NewEnvironmentConfig(target.ID()), true, true),
Expand Down Expand Up @@ -146,7 +146,7 @@ func Test_Deploy(t *testing.T) {
target := must.Panic(domain.NewTarget("my-target",
domain.NewTargetUrlRequirement(must.Panic(domain.UrlFrom("http://localhost")), true),
domain.NewProviderConfigRequirement(nil, true), "some-uid"))
target.Configured(target.CurrentVersion(), nil)
target.Configured(target.CurrentVersion(), nil, nil)

app := must.Panic(domain.NewApp("my-app",
domain.NewEnvironmentConfigRequirement(domain.NewEnvironmentConfig(target.ID()), true, true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ func Handler(
return bus.Unit, err
}

target.Configured(target.CurrentVersion(), provider.Setup(ctx, target))
assigned, err := provider.Setup(ctx, target)

target.Configured(target.CurrentVersion(), assigned, err)

if err := writer.Write(ctx, &target); err != nil {
return bus.Unit, err
Expand Down
18 changes: 14 additions & 4 deletions internal/deployment/app/get_deployment/get_deployment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package get_deployment

import (
"fmt"
"time"

"github.com/YuukanOO/seelf/internal/deployment/app"
Expand Down Expand Up @@ -56,16 +57,25 @@ type (

Services []Service

Entrypoint struct {
Name string `json:"name"`
Router string `json:"router"`
Subdomain monad.Maybe[string] `json:"subdomain"`
Port uint `json:"port"`
Url monad.Maybe[string] `json:"url"`
PublishedPort monad.Maybe[uint] `json:"published_port"`
}

Service struct {
Name string `json:"name"`
Image string `json:"image"`
Subdomain monad.Maybe[string] `json:"subdomain"`
Url monad.Maybe[string] `json:"url"`
Name string `json:"name"`
Image string `json:"image"`
Entrypoints []Entrypoint `json:"entrypoints"`
}
)

func (Query) Name_() string { return "deployment.query.get_deployment" }

func (s *Services) Scan(value any) error {
fmt.Println("TODO: implement a compatility layer to extract the root subdomain into a new entrypoint HTTP")
return storage.ScanJSON(value, s)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Test_ReconfigureTarget(t *testing.T) {
target := must.Panic(domain.NewTarget("my-target",
domain.NewTargetUrlRequirement(must.Panic(domain.UrlFrom("http://localhost")), true),
domain.NewProviderConfigRequirement(nil, true), "uid"))
target.Configured(target.CurrentVersion(), nil)
target.Configured(target.CurrentVersion(), nil, nil)

uc := sut(&target)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Test_RequestTargetCleanup(t *testing.T) {
target := must.Panic(domain.NewTarget("my-target",
domain.NewTargetUrlRequirement(must.Panic(domain.UrlFrom("http://docker.localhost")), true),
domain.NewProviderConfigRequirement(dummyProviderConfig{}, true), "uid"))
target.Configured(target.CurrentVersion(), nil)
target.Configured(target.CurrentVersion(), nil, nil)
app := must.Panic(domain.NewApp("my-app",
domain.NewEnvironmentConfigRequirement(domain.NewEnvironmentConfig(target.ID()), true, true),
domain.NewEnvironmentConfigRequirement(domain.NewEnvironmentConfig(target.ID()), true, true), "uid"))
Expand All @@ -63,7 +63,7 @@ func Test_RequestTargetCleanup(t *testing.T) {
target := must.Panic(domain.NewTarget("my-target",
domain.NewTargetUrlRequirement(must.Panic(domain.UrlFrom("http://docker.localhost")), true),
domain.NewProviderConfigRequirement(dummyProviderConfig{}, true), "uid"))
target.Configured(target.CurrentVersion(), nil)
target.Configured(target.CurrentVersion(), nil, nil)

uc := sut(initialData{
targets: []*domain.Target{&target},
Expand Down
9 changes: 5 additions & 4 deletions internal/deployment/app/update_target/update_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func (*dummyProvider) Prepare(ctx context.Context, payload any, existing ...doma
return dummyConfig{payload.(string)}, nil
}

func (dummyConfig) Kind() string { return "dummy" }
func (c dummyConfig) Fingerprint() string { return c.data }
func (c dummyConfig) Equals(other domain.ProviderConfig) bool { return false }
func (c dummyConfig) String() string { return c.data }
func (dummyConfig) Kind() string { return "dummy" }
func (c dummyConfig) Fingerprint() string { return c.data }
func (c dummyConfig) Equals(other domain.ProviderConfig) bool { return false }
func (c dummyConfig) String() string { return c.data }
func (dummyConfig) FilterEntrypoints(e []domain.Entrypoint) []domain.Entrypoint { return e }
10 changes: 6 additions & 4 deletions internal/deployment/domain/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ type (
DeploymentStateChanged struct {
bus.Notification

ID DeploymentID
State DeploymentState
ID DeploymentID
Config DeploymentConfig
State DeploymentState
}
)

Expand Down Expand Up @@ -221,8 +222,9 @@ func (d *Deployment) HasEnded(services Services, deploymentErr error) error {

func (d *Deployment) stateChanged() {
d.apply(DeploymentStateChanged{
ID: d.id,
State: d.state,
ID: d.id,
Config: d.config,
State: d.state,
})
}

Expand Down
4 changes: 2 additions & 2 deletions internal/deployment/domain/deployment_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c DeploymentConfig) EnvironmentVariablesFor(service string) (m monad.Maybe
}

// Returns the subdomain that will be used to expose a specific service.
func (c DeploymentConfig) SubDomain(service string, hasExposedServices bool) string {
func (c DeploymentConfig) SubDomain(service string, isDefault bool) string {
subdomain := string(c.appname)

if !c.environment.IsProduction() {
Expand All @@ -78,7 +78,7 @@ func (c DeploymentConfig) SubDomain(service string, hasExposedServices bool) str

// If the default domain has already been taken by another service, build a
// unique subdomain with the service name being exposed.
if hasExposedServices {
if !isDefault {
subdomain = service + "." + subdomain
}

Expand Down
8 changes: 4 additions & 4 deletions internal/deployment/domain/deployment_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ func Test_Config(t *testing.T) {
t.Run("should generate a subdomain equals to app name if env is production", func(t *testing.T) {
conf, _ := app.ConfigSnapshotFor(domain.Production)

testutil.Equals(t, "my-app", conf.SubDomain("app", false))
testutil.Equals(t, "db.my-app", conf.SubDomain("db", true))
testutil.Equals(t, "my-app", conf.SubDomain("app", true))
testutil.Equals(t, "db.my-app", conf.SubDomain("db", false))
})

t.Run("should generate a subdomain suffixed by the env if not production", func(t *testing.T) {
conf, _ := app.ConfigSnapshotFor(domain.Staging)

testutil.Equals(t, "my-app-staging", conf.SubDomain("app", false))
testutil.Equals(t, "db.my-app-staging", conf.SubDomain("db", true))
testutil.Equals(t, "my-app-staging", conf.SubDomain("app", true))
testutil.Equals(t, "db.my-app-staging", conf.SubDomain("db", false))
})

t.Run("should expose a unique project name", func(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions internal/deployment/domain/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ func Test_Deployment(t *testing.T) {
})

t.Run("could be marked has ended with services", func(t *testing.T) {
var (
err error
services domain.Services
)
var err error

dpl := must.Panic(app.NewDeployment(number, nonVcsMeta, domain.Production, uid))
services, _ = services.Append(dpl.Config(), "aservice", "an/image", false)
services := domain.Services{
dpl.Config().NewService("aservice", "an/image"),
}

dpl.HasStarted()

err = dpl.HasEnded(services, nil)
Expand Down
Loading

0 comments on commit 5a33528

Please sign in to comment.