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 16, 2024
1 parent cae1223 commit c18923f
Show file tree
Hide file tree
Showing 61 changed files with 3,332 additions and 1,876 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,46 @@
package configure_target

import (
"context"

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

// When an application cleanup has been requested, unexpose the application from all targets.
func OnAppCleanupRequestedHandler(
reader domain.TargetsReader,
writer domain.TargetsWriter,
) bus.SignalHandler[domain.AppCleanupRequested] {
return func(ctx context.Context, evt domain.AppCleanupRequested) error {
if evt.ProductionConfig.Target() == evt.StagingConfig.Target() {
target, err := reader.GetByID(ctx, evt.ProductionConfig.Target())

if err != nil {
return err
}

target.UnExposeEntrypoints(evt.ID)

return writer.Write(ctx, &target)
}

productionTarget, err := reader.GetByID(ctx, evt.ProductionConfig.Target())

if err != nil {
return err
}

productionTarget.UnExposeEntrypoints(evt.ID, domain.Production)

stagingTarget, err := reader.GetByID(ctx, evt.StagingConfig.Target())

if err != nil {
return err
}

stagingTarget.UnExposeEntrypoints(evt.ID, domain.Staging)

return writer.Write(ctx, &productionTarget, &stagingTarget)
}
}
32 changes: 32 additions & 0 deletions internal/deployment/app/configure_target/on_app_env_changed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package configure_target

import (
"context"

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

// When an application environment has changed and only if the target has changed,
// unexpose the application from the old target.
func OnAppEnvChangedHandler(
reader domain.TargetsReader,
writer domain.TargetsWriter,
) bus.SignalHandler[domain.AppEnvChanged] {
return func(ctx context.Context, evt domain.AppEnvChanged) error {
// No target change, nothing to do
if evt.Config.Target() == evt.OldConfig.Target() {
return nil
}

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

if err != nil {
return err
}

target.UnExposeEntrypoints(evt.ID, evt.Environment)

return writer.Write(ctx, &target)
}
}
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.ExposeEntrypoints(evt.ID.AppID(), evt.Config.Environment(), evt.State.Services().Get(domain.Services{}))

return writer.Write(ctx, &target)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ func OnTargetCreatedHandler(scheduler bus.Scheduler) bus.SignalHandler[domain.Ta
return scheduler.Queue(ctx, Command{
ID: string(evt.ID),
Version: evt.State.Version(),
}, bus.WithGroup(app.TargetConfigurationGroup(evt.ID)))
}, bus.WithGroup(app.TargetConfigurationGroup(evt.ID)), bus.WithPolicy(bus.JobPolicyMerge))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ func OnTargetStateChangedHandler(scheduler bus.Scheduler) bus.SignalHandler[doma
return scheduler.Queue(ctx, Command{
ID: string(evt.ID),
Version: evt.State.Version(),
}, bus.WithGroup(app.TargetConfigurationGroup(evt.ID)))
}, bus.WithGroup(app.TargetConfigurationGroup(evt.ID)), bus.WithPolicy(bus.JobPolicyMerge))
}
}
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
43 changes: 36 additions & 7 deletions internal/deployment/app/get_app_deployments/get_app_deployments.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
package get_app_deployments

import (
"time"

"github.com/YuukanOO/seelf/internal/deployment/app"
"github.com/YuukanOO/seelf/internal/deployment/app/get_deployment"
"github.com/YuukanOO/seelf/pkg/bus"
"github.com/YuukanOO/seelf/pkg/monad"
"github.com/YuukanOO/seelf/pkg/storage"
)

// Retrieve all deployments for an app.
type Query struct {
bus.Query[storage.Paginated[get_deployment.Deployment]]
type (
Query struct {
// Retrieve all deployments for an app.
bus.Query[storage.Paginated[Deployment]]

AppID string `json:"-"`
Page monad.Maybe[int] `form:"page"`
Environment monad.Maybe[string] `form:"environment"`
}

Deployment struct {
AppID string `json:"app_id"`
DeploymentNumber int `json:"deployment_number"`
Environment string `json:"environment"`
Target TargetSummary `json:"target"`
Source get_deployment.Source `json:"source"`
State State `json:"state"`
RequestedAt time.Time `json:"requested_at"`
RequestedBy app.UserSummary `json:"requested_by"`
}

AppID string `json:"-"`
Page monad.Maybe[int] `form:"page"`
Environment monad.Maybe[string] `form:"environment"`
}
TargetSummary struct {
ID string `json:"id"`
Name monad.Maybe[string] `json:"name"` // Since the target could have been deleted, the name is nullable here.
Url monad.Maybe[string] `json:"url"`
}

State struct {
Status uint8 `json:"status"`
ErrCode monad.Maybe[string] `json:"error_code"`
StartedAt monad.Maybe[time.Time] `json:"started_at"`
FinishedAt monad.Maybe[time.Time] `json:"finished_at"`
}
)

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

0 comments on commit c18923f

Please sign in to comment.