From f5835801604db56c36a9456cb1f7cee7567a0a0b Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Thu, 17 Nov 2022 08:35:08 +0100 Subject: [PATCH] Add 'system_pause' command This command adds a new 'system_pause' command that stops or starts the entire system. --- cmd/system_pause.go | 187 ++++++++++++++++++++++++++++++++++++++++++++ cmd/wdcli/main.go | 1 + 2 files changed, 188 insertions(+) create mode 100644 cmd/system_pause.go diff --git a/cmd/system_pause.go b/cmd/system_pause.go new file mode 100644 index 0000000..1c7955e --- /dev/null +++ b/cmd/system_pause.go @@ -0,0 +1,187 @@ +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" + "github.com/tkw1536/goprogram/stream" +) + +// 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.IOStream, "Starting Components") + + // find all the core stacks + if err := status.RunErrorGroup(context.Stdout, 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 { + io := stream.NewIOStream(writer, writer, stream.Null, 0) + return item.Stack(context.Environment.Environment).Up(io) + }, + }, dis.Installable()); err != nil { + return err + } + + logging.LogMessage(context.IOStream, "Starting Up WissKIs") + + // find the instances + wissKIs, err := dis.Instances().All() + if err != nil { + return err + } + + // shut them all down + if err := status.RunErrorGroup(context.Stdout, 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 { + io := stream.NewIOStream(writer, writer, stream.Null, 0) + return item.Barrel().Stack().Up(io) + }, + }, wissKIs); err != nil { + return err + } + + return nil +} + +func (sp systempause) stop(context wisski_distillery.Context, dis *dis.Distillery) error { + logging.LogMessage(context.IOStream, "Shutting Down WissKIs") + + // find the instances + wissKIs, err := dis.Instances().All() + if err != nil { + return err + } + + // shut them all down + if err := status.RunErrorGroup(context.Stdout, 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 { + io := stream.NewIOStream(writer, writer, stream.Null, 0) + return item.Barrel().Stack().Down(io) + }, + }, wissKIs); err != nil { + return err + } + + logging.LogMessage(context.IOStream, "Shutting Down Components") + + // find all the core stacks + if err := status.RunErrorGroup(context.Stdout, 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 { + io := stream.NewIOStream(writer, writer, stream.Null, 0) + return item.Stack(context.Environment.Environment).Down(io) + }, + }, dis.Installable()); err != nil { + return err + } + + return nil +} + +/* + handleStack := func(io stream.IOStream, stack component.StackWithResources) error { + if sp.Start { + return stack.Up(io) + } else { + return stack.Down(io) + } + } + + logging.LogMessage(context.IOStream, "Iterating over components") + if err := status.RunErrorGroup(context.Stdout, status.Group[component.Installable, error]{ + PrefixString: func(item component.Installable, index int) string { + return fmt.Sprintf("[%s %q]: ", verb, item.Name()) + }, + PrefixAlign: true, + + Handler: func(item component.Installable, index int, writer io.Writer) error { + io := stream.NewIOStream(writer, writer, stream.Null, 0) + stack := item.Stack(context.Environment.Environment) + + return handleStack(io, stack) + }, + }, dis.Installable()); err != nil { + return err + } + + logging.LogMessage(context.IOStream, "Shutting Down WissKIs") + + // find the instances + wissKIs, err := dis.Instances().All() + if err != nil { + return err + } + + // and do the actual rebuild + if err := status.StreamGroup(context.IOStream, rb.Parallel, func(instance *wisski.WissKI, io stream.IOStream) error { + return instance.Barrel().Build(io, true) + }, wissKIs, status.SmartMessage(func(item *wisski.WissKI) string { + return fmt.Sprintf("rebuild %q", item.Slug) + })); err != nil { + return err + } +} +*/ diff --git a/cmd/wdcli/main.go b/cmd/wdcli/main.go index b1dab6f..391aeb7 100644 --- a/cmd/wdcli/main.go +++ b/cmd/wdcli/main.go @@ -23,6 +23,7 @@ func init() { // setup commands wdcli.Register(cmd.Bootstrap) wdcli.Register(cmd.SystemUpdate) + wdcli.Register(cmd.SystemPause) // sql commands wdcli.Register(cmd.Mysql)