Rework actions to be loaded dynamically

This commit is contained in:
Tom Wiesing 2023-11-08 10:29:09 +01:00
parent e49f89d4ee
commit 08ab7b4383
No known key found for this signature in database
22 changed files with 934 additions and 81 deletions

View file

@ -0,0 +1,38 @@
package actions
import (
"context"
"io"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
)
// Routeable is a component that is servable
type WebsocketAction interface {
component.Component
Action() Action
Act(ctx context.Context, in io.Reader, out io.Writer, params ...string) error
}
type WebsocketInstanceAction interface {
component.Component
Action() InstanceAction
Act(ctx context.Context, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error
}
// Action represents information about an action
type Action struct {
Name string
Scope scopes.Scope
ScopeParam string
NumParams int
}
type InstanceAction struct {
Action
}

View file

@ -0,0 +1,42 @@
package actions
import (
"context"
"io"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter"
)
type Backup struct {
component.Base
dependencies struct {
Exporter *exporter.Exporter
}
}
var (
_ WebsocketAction = (*Backup)(nil)
)
func (*Backup) Action() Action {
return Action{
Name: "backup",
Scope: scopes.ScopeUserAdmin,
NumParams: 0,
}
}
func (b *Backup) Act(ctx context.Context, in io.Reader, out io.Writer, params ...string) error {
return b.dependencies.Exporter.MakeExport(
ctx,
out,
exporter.ExportTask{
Dest: "",
Instance: nil,
StagingOnly: false,
},
)
}

View file

@ -0,0 +1,32 @@
package actions
import (
"context"
"io"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
)
type Cron struct {
component.Base
}
var (
_ WebsocketInstanceAction = (*Cron)(nil)
)
func (*Cron) Action() InstanceAction {
return InstanceAction{
Action: Action{
Name: "cron",
Scope: scopes.ScopeUserAdmin,
NumParams: 0,
},
}
}
func (c *Cron) Act(ctx context.Context, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
return instance.Drush().Cron(ctx, out)
}

View file

@ -0,0 +1,54 @@
package actions
import (
"context"
"encoding/json"
"fmt"
"io"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/provision"
)
type Provision struct {
component.Base
dependencies struct {
Provision *provision.Provision
}
}
var (
_ WebsocketAction = (*Provision)(nil)
)
func (*Provision) Action() Action {
return Action{
Name: "provision",
Scope: scopes.ScopeUserAdmin,
NumParams: 1,
}
}
func (p *Provision) Act(ctx context.Context, in io.Reader, out io.Writer, params ...string) error {
// read the flags of the instance to be provisioned
var flags provision.Flags
if err := json.Unmarshal([]byte(params[0]), &flags); err != nil {
return err
}
instance, err := p.dependencies.Provision.Provision(
out,
ctx,
flags,
)
if err != nil {
return err
}
fmt.Fprintf(out, "URL: %s\n", instance.URL().String())
fmt.Fprintf(out, "Username: %s\n", instance.DrupalUsername)
fmt.Fprintf(out, "Password: %s\n", instance.DrupalPassword)
return nil
}

View file

@ -0,0 +1,36 @@
package actions
import (
"context"
"io"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances/purger"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
)
type Purge struct {
component.Base
dependencies struct {
Purger *purger.Purger
}
}
var (
_ WebsocketInstanceAction = (*Stop)(nil)
)
func (*Purge) Action() InstanceAction {
return InstanceAction{
Action: Action{
Name: "purge",
Scope: scopes.ScopeUserAdmin,
NumParams: 0,
},
}
}
func (p *Purge) Act(ctx context.Context, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
return p.dependencies.Purger.Purge(ctx, out, instance.Slug)
}

View file

@ -0,0 +1,39 @@
package actions
import (
"context"
"encoding/json"
"io"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
)
type Rebuild struct {
component.Base
}
var (
_ WebsocketInstanceAction = (*Rebuild)(nil)
)
func (*Rebuild) Action() InstanceAction {
return InstanceAction{
Action: Action{
Name: "rebuild",
Scope: scopes.ScopeUserAdmin,
NumParams: 1,
},
}
}
func (r *Rebuild) Act(ctx context.Context, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
// read the flags of the instance to be provisioned
var system models.System
if err := json.Unmarshal([]byte(params[0]), &system); err != nil {
return err
}
return instance.SystemManager().Apply(ctx, out, system, true)
}

View file

@ -0,0 +1,45 @@
package actions
import (
"context"
"io"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
)
type Snapshot struct {
component.Base
dependencies struct {
Exporter *exporter.Exporter
}
}
var (
_ WebsocketInstanceAction = (*Snapshot)(nil)
)
func (*Snapshot) Action() InstanceAction {
return InstanceAction{
Action: Action{
Name: "snapshot",
Scope: scopes.ScopeUserAdmin,
NumParams: 0,
},
}
}
func (s *Snapshot) Act(ctx context.Context, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
return s.dependencies.Exporter.MakeExport(
ctx,
out,
exporter.ExportTask{
Dest: "",
Instance: instance,
StagingOnly: false,
},
)
}

View file

@ -0,0 +1,32 @@
package actions
import (
"context"
"io"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
)
type Start struct {
component.Base
}
var (
_ WebsocketInstanceAction = (*Start)(nil)
)
func (*Start) Action() InstanceAction {
return InstanceAction{
Action: Action{
Name: "start",
Scope: scopes.ScopeUserAdmin,
NumParams: 0,
},
}
}
func (*Start) Act(ctx context.Context, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
return instance.Barrel().Stack().Up(ctx, out)
}

View file

@ -0,0 +1,32 @@
package actions
import (
"context"
"io"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
)
type Stop struct {
component.Base
}
var (
_ WebsocketInstanceAction = (*Stop)(nil)
)
func (*Stop) Action() InstanceAction {
return InstanceAction{
Action: Action{
Name: "stop",
Scope: scopes.ScopeUserAdmin,
NumParams: 0,
},
}
}
func (*Stop) Act(ctx context.Context, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
return instance.Barrel().Stack().Down(ctx, out)
}

View file

@ -0,0 +1,32 @@
package actions
import (
"context"
"io"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/scopes"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
)
type Update struct {
component.Base
}
var (
_ WebsocketInstanceAction = (*Update)(nil)
)
func (*Update) Action() InstanceAction {
return InstanceAction{
Action: Action{
Name: "update",
Scope: scopes.ScopeUserAdmin,
NumParams: 0,
},
}
}
func (u *Update) Act(ctx context.Context, instance *wisski.WissKI, in io.Reader, out io.Writer, params ...string) error {
return instance.Composer().Update(ctx, out)
}