Skip to content

Commit d321448

Browse files
nikitasavchenko555Nikita Savchenko
andauthored
add reboot command (#119) (#228)
Signed-off-by: Nikita Savchenko <[email protected]> Co-authored-by: Nikita Savchenko <[email protected]>
1 parent 6c378aa commit d321448

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

cmd/attack/host.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func NewHostAttackCommand(uid *string) *cobra.Command {
4141
}
4242

4343
cmd.AddCommand(NewHostShutdownCommand(dep, options))
44+
cmd.AddCommand(NewHostRebootCommand(dep, options))
4445

4546
return cmd
4647
}
@@ -51,6 +52,21 @@ func NewHostShutdownCommand(dep fx.Option, options *core.HostCommand) *cobra.Com
5152
Short: "shutdowns system, this action will trigger shutdown of the host machine",
5253

5354
Run: func(*cobra.Command, []string) {
55+
options.Action = core.HostShutdownAction
56+
utils.FxNewAppWithoutLog(dep, fx.Invoke(hostAttackF)).Run()
57+
},
58+
}
59+
60+
return cmd
61+
}
62+
63+
func NewHostRebootCommand(dep fx.Option, options *core.HostCommand) *cobra.Command {
64+
cmd := &cobra.Command{
65+
Use: "reboot",
66+
Short: "reboot system, this action will trigger reboot of the host machine",
67+
68+
Run: func(*cobra.Command, []string) {
69+
options.Action = core.HostRebootAction
5470
utils.FxNewAppWithoutLog(dep, fx.Invoke(hostAttackF)).Run()
5571
},
5672
}

pkg/core/host.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
const (
2121
HostShutdownAction = "shutdown"
22+
HostRebootAction = "reboot"
2223
)
2324

2425
type HostCommand struct {
@@ -40,8 +41,7 @@ func (h HostCommand) RecoverData() string {
4041
func NewHostCommand() *HostCommand {
4142
return &HostCommand{
4243
CommonAttackConfig: CommonAttackConfig{
43-
Kind: HostAttack,
44-
Action: HostShutdownAction,
44+
Kind: HostAttack,
4545
},
4646
}
4747
}

pkg/server/chaosd/host.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package chaosd
1515

1616
import (
17+
"github.com/pingcap/errors"
1718
perr "github.com/pkg/errors"
1819

1920
"github.com/chaos-mesh/chaosd/pkg/core"
@@ -22,16 +23,31 @@ import (
2223
type HostManager interface {
2324
Name() string
2425
Shutdown() error
26+
Reboot() error
2527
}
2628

2729
type hostAttack struct{}
2830

2931
var HostAttack AttackType = hostAttack{}
3032

3133
func (hostAttack) Attack(options core.AttackConfig, _ Environment) error {
32-
if err := Host.Shutdown(); err != nil {
33-
return perr.WithStack(err)
34+
hostOption, ok := options.(*core.HostCommand)
35+
if !ok {
36+
return errors.New("the type is not HostOption")
3437
}
38+
39+
if hostOption.Action == core.HostShutdownAction {
40+
if err := Host.Shutdown(); err != nil {
41+
return perr.WithStack(err)
42+
}
43+
}
44+
45+
if hostOption.Action == core.HostRebootAction {
46+
if err := Host.Reboot(); err != nil {
47+
return perr.WithStack(err)
48+
}
49+
}
50+
3551
return nil
3652
}
3753

pkg/server/chaosd/host_unix.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ var Host HostManager = UnixHost{}
2929

3030
const CmdShutdown = "shutdown"
3131

32+
const CmdReboot = "reboot"
33+
3234
func (h UnixHost) Name() string {
3335
return "unix"
3436
}
@@ -41,3 +43,12 @@ func (h UnixHost) Shutdown() error {
4143
}
4244
return err
4345
}
46+
47+
func (h UnixHost) Reboot() error {
48+
cmd := exec.Command(CmdReboot)
49+
output, err := cmd.CombinedOutput()
50+
if err != nil {
51+
log.Error(string(output), zap.Error(err))
52+
}
53+
return err
54+
}

0 commit comments

Comments
 (0)