Skip to content

Commit

Permalink
Merge pull request #3 from ozontech-forks/add_reboot_command
Browse files Browse the repository at this point in the history
add reboot command (chaos-mesh#119)
  • Loading branch information
nikitasavchenko555 authored Jan 26, 2023
2 parents d020862 + 2113b75 commit f14b152
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
16 changes: 16 additions & 0 deletions cmd/attack/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func NewHostAttackCommand(uid *string) *cobra.Command {
}

cmd.AddCommand(NewHostShutdownCommand(dep, options))
cmd.AddCommand(NewHostRebootCommand(dep, options))

return cmd
}
Expand All @@ -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()
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

const (
HostShutdownAction = "shutdown"
HostRebootAction = "reboot"
)

type HostCommand struct {
Expand All @@ -40,8 +41,7 @@ func (h HostCommand) RecoverData() string {
func NewHostCommand() *HostCommand {
return &HostCommand{
CommonAttackConfig: CommonAttackConfig{
Kind: HostAttack,
Action: HostShutdownAction,
Kind: HostAttack,
},
}
}
20 changes: 18 additions & 2 deletions pkg/server/chaosd/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package chaosd

import (
"github.com/pingcap/errors"
perr "github.com/pkg/errors"

"github.com/chaos-mesh/chaosd/pkg/core"
Expand All @@ -22,16 +23,31 @@ import (
type HostManager interface {
Name() string
Shutdown() error
Reboot() error
}

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
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/server/chaosd/host_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var Host HostManager = UnixHost{}

const CmdShutdown = "shutdown"

const CmdReboot = "reboot"

func (h UnixHost) Name() string {
return "unix"
}
Expand All @@ -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
}

0 comments on commit f14b152

Please sign in to comment.