diff --git a/cmd/attack/host.go b/cmd/attack/host.go index 18f515d9..d05268d6 100644 --- a/cmd/attack/host.go +++ b/cmd/attack/host.go @@ -41,6 +41,7 @@ func NewHostAttackCommand(uid *string) *cobra.Command { } cmd.AddCommand(NewHostShutdownCommand(dep, options)) + cmd.AddCommand(NewHostRebootCommand(dep, options)) return cmd } @@ -51,6 +52,21 @@ func NewHostShutdownCommand(dep fx.Option, options *core.HostCommand) *cobra.Com Short: "shutdowns system, this action will trigger shutdown of the host machine", Run: func(*cobra.Command, []string) { + options.Action = core.HostShutdownAction + utils.FxNewAppWithoutLog(dep, fx.Invoke(hostAttackF)).Run() + }, + } + + return cmd +} + +func NewHostRebootCommand(dep fx.Option, options *core.HostCommand) *cobra.Command { + cmd := &cobra.Command{ + Use: "reboot", + Short: "reboot system, this action will trigger reboot of the host machine", + + Run: func(*cobra.Command, []string) { + options.Action = core.HostRebootAction utils.FxNewAppWithoutLog(dep, fx.Invoke(hostAttackF)).Run() }, } diff --git a/pkg/core/host.go b/pkg/core/host.go index b8e19f45..5d5e019b 100644 --- a/pkg/core/host.go +++ b/pkg/core/host.go @@ -19,6 +19,7 @@ import ( const ( HostShutdownAction = "shutdown" + HostRebootAction = "reboot" ) type HostCommand struct { @@ -40,8 +41,7 @@ func (h HostCommand) RecoverData() string { func NewHostCommand() *HostCommand { return &HostCommand{ CommonAttackConfig: CommonAttackConfig{ - Kind: HostAttack, - Action: HostShutdownAction, + Kind: HostAttack, }, } } diff --git a/pkg/server/chaosd/host.go b/pkg/server/chaosd/host.go index f751fe9a..51c119ed 100644 --- a/pkg/server/chaosd/host.go +++ b/pkg/server/chaosd/host.go @@ -14,6 +14,7 @@ package chaosd import ( + "github.com/pingcap/errors" perr "github.com/pkg/errors" "github.com/chaos-mesh/chaosd/pkg/core" @@ -22,6 +23,7 @@ import ( type HostManager interface { Name() string Shutdown() error + Reboot() error } type hostAttack struct{} @@ -29,9 +31,23 @@ type hostAttack struct{} var HostAttack AttackType = hostAttack{} func (hostAttack) Attack(options core.AttackConfig, _ Environment) error { - if err := Host.Shutdown(); err != nil { - return perr.WithStack(err) + hostOption, ok := options.(*core.HostCommand) + if !ok { + return errors.New("the type is not HostOption") } + + if hostOption.Action == core.HostShutdownAction { + if err := Host.Shutdown(); err != nil { + return perr.WithStack(err) + } + } + + if hostOption.Action == core.HostRebootAction { + if err := Host.Reboot(); err != nil { + return perr.WithStack(err) + } + } + return nil } diff --git a/pkg/server/chaosd/host_unix.go b/pkg/server/chaosd/host_unix.go index 9d9fdb5f..7cff6115 100644 --- a/pkg/server/chaosd/host_unix.go +++ b/pkg/server/chaosd/host_unix.go @@ -29,6 +29,8 @@ var Host HostManager = UnixHost{} const CmdShutdown = "shutdown" +const CmdReboot = "reboot" + func (h UnixHost) Name() string { return "unix" } @@ -41,3 +43,12 @@ func (h UnixHost) Shutdown() error { } return err } + +func (h UnixHost) Reboot() error { + cmd := exec.Command(CmdReboot) + output, err := cmd.CombinedOutput() + if err != nil { + log.Error(string(output), zap.Error(err)) + } + return err +}