wisski-cloud-distillery/cmd/system_pause.go
2022-11-30 13:17:12 +01:00

137 lines
3.9 KiB
Go

package cmd
import (
"fmt"
"io"
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/internal/cli"
"github.com/FAU-CDI/wisski-distillery/internal/dis"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/tkw1536/goprogram/exit"
"github.com/tkw1536/goprogram/status"
)
// SystemPause is the 'system_pause' command
var SystemPause wisski_distillery.Command = systempause{}
type systempause struct {
Stop bool `short:"d" long:"stop" description:"Stop all the components"`
Start bool `short:"u" long:"start" description:"Start all the components"`
}
func (systempause) Description() wisski_distillery.Description {
return wisski_distillery.Description{
Requirements: cli.Requirements{
NeedsDistillery: true,
},
Command: "system_pause",
Description: "Stops or Starts the entire WissKI Distillery system",
}
}
var errStopStartExcluded = exit.Error{
Message: "Exactly one of `--stop` and `--start` must be provied",
ExitCode: exit.ExitCommandArguments,
}
func (s systempause) AfterParse() error {
if s.Stop == s.Start {
return errStopStartExcluded
}
return nil
}
func (sp systempause) Run(context wisski_distillery.Context) error {
if sp.Start {
return sp.start(context, context.Environment)
} else {
return sp.stop(context, context.Environment)
}
}
func (sp systempause) start(context wisski_distillery.Context, dis *dis.Distillery) error {
logging.LogMessage(context.Stderr, "Starting Components")
// find all the core stacks
if err := status.RunErrorGroup(context.Stderr, status.Group[component.Installable, error]{
PrefixString: func(item component.Installable, index int) string {
return fmt.Sprintf("[up %q]: ", item.Name())
},
PrefixAlign: true,
Handler: func(item component.Installable, index int, writer io.Writer) error {
return item.Stack(context.Environment.Environment).Up(context.Context, writer)
},
}, dis.Installable()); err != nil {
return err
}
logging.LogMessage(context.Stderr, "Starting Up WissKIs")
// find the instances
wissKIs, err := dis.Instances().All(context.Context)
if err != nil {
return err
}
// shut them all down
if err := status.RunErrorGroup(context.Stderr, status.Group[*wisski.WissKI, error]{
PrefixString: func(item *wisski.WissKI, index int) string {
return fmt.Sprintf("[up %q]: ", item.Slug)
},
PrefixAlign: true,
Handler: func(item *wisski.WissKI, index int, writer io.Writer) error {
return item.Barrel().Stack().Up(context.Context, writer)
},
}, wissKIs); err != nil {
return err
}
return nil
}
func (sp systempause) stop(context wisski_distillery.Context, dis *dis.Distillery) error {
logging.LogMessage(context.Stderr, "Shutting Down WissKIs")
// find the instances
wissKIs, err := dis.Instances().All(context.Context)
if err != nil {
return err
}
// shut them all down
if err := status.RunErrorGroup(context.Stderr, status.Group[*wisski.WissKI, error]{
PrefixString: func(item *wisski.WissKI, index int) string {
return fmt.Sprintf("[down %q]: ", item.Slug)
},
PrefixAlign: true,
Handler: func(item *wisski.WissKI, index int, writer io.Writer) error {
return item.Barrel().Stack().Down(context.Context, writer)
},
}, wissKIs); err != nil {
return err
}
logging.LogMessage(context.Stderr, "Shutting Down Components")
// find all the core stacks
if err := status.RunErrorGroup(context.Stderr, status.Group[component.Installable, error]{
PrefixString: func(item component.Installable, index int) string {
return fmt.Sprintf("[down %q]: ", item.Name())
},
PrefixAlign: true,
Handler: func(item component.Installable, index int, writer io.Writer) error {
return item.Stack(context.Environment.Environment).Down(context.Context, writer)
},
}, dis.Installable()); err != nil {
return err
}
return nil
}