mirror of https://github.com/chaos-mesh/chaosd.git
feat: support shutdown (#26)
This commit is contained in:
parent
522e4e49d6
commit
6cedaf5f19
|
|
@ -25,6 +25,7 @@ func NewAttackCommand() *cobra.Command {
|
|||
NewProcessAttackCommand(),
|
||||
NewNetworkAttackCommand(),
|
||||
NewStressAttackCommand(),
|
||||
NewHostAttackCommand(),
|
||||
)
|
||||
|
||||
return cmd
|
||||
|
|
|
|||
|
|
@ -0,0 +1,66 @@
|
|||
// Copyright 2021 Chaos Mesh Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/chaos-mesh/chaosd/pkg/core"
|
||||
)
|
||||
|
||||
var hFlag core.HostCommand
|
||||
|
||||
func NewHostAttackCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "host <subcommand>",
|
||||
Short: "Host attack related commands",
|
||||
}
|
||||
|
||||
cmd.AddCommand(NewHostShutdownCommand())
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func NewHostShutdownCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "shutdown",
|
||||
Short: "shutdowns system, this action will trigger shutdown of the host machine",
|
||||
|
||||
Run: processShutdownCommandFunc,
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func processShutdownCommandFunc(cmd *cobra.Command, args []string) {
|
||||
hFlag.Action = core.HostShutdownAction
|
||||
hostAttackF(cmd, &hFlag)
|
||||
}
|
||||
|
||||
func hostAttackF(cmd *cobra.Command, f *core.HostCommand) {
|
||||
if err := hFlag.Validate(); err != nil {
|
||||
ExitWithError(ExitBadArgs, err)
|
||||
}
|
||||
|
||||
chaos := mustChaosdFromCmd(cmd, &conf)
|
||||
|
||||
uid, err := chaos.HostAttack(f)
|
||||
if err != nil {
|
||||
ExitWithError(ExitError, err)
|
||||
}
|
||||
|
||||
NormalExit(fmt.Sprintf("Attack host successfully, uid: %s", uid))
|
||||
}
|
||||
|
|
@ -30,6 +30,7 @@ const (
|
|||
ProcessAttack = "process"
|
||||
NetworkAttack = "network"
|
||||
StressAttack = "stress"
|
||||
HostAttack = "host"
|
||||
)
|
||||
|
||||
// ExperimentStore defines operations for working with experiments
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
// Copyright 2021 Chaos Mesh Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package core
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
const (
|
||||
HostShutdownAction = "shutdown"
|
||||
)
|
||||
|
||||
type HostCommand struct {
|
||||
Action string
|
||||
}
|
||||
|
||||
func (h *HostCommand) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *HostCommand) String() string {
|
||||
data, _ := json.Marshal(h)
|
||||
|
||||
return string(data)
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
// Copyright 2021 Chaos Mesh Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package chaosd
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pingcap/errors"
|
||||
"github.com/pingcap/log"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/chaos-mesh/chaosd/pkg/core"
|
||||
)
|
||||
|
||||
type HostManager interface {
|
||||
Name() string
|
||||
Shutdown() error
|
||||
}
|
||||
|
||||
func (s *Server) HostAttack(attack *core.HostCommand) (uid string, err error) {
|
||||
uid = uuid.New().String()
|
||||
|
||||
if err = s.exp.Set(context.Background(), &core.Experiment{
|
||||
Uid: uid,
|
||||
Status: core.Created,
|
||||
Kind: core.HostAttack,
|
||||
Action: attack.Action,
|
||||
RecoverCommand: attack.String(),
|
||||
}); err != nil {
|
||||
err = errors.WithStack(err)
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err != nil {
|
||||
if err := s.exp.Update(context.Background(), uid, core.Error, err.Error(), attack.String()); err != nil {
|
||||
log.Error("failed to update experiment", zap.Error(err))
|
||||
}
|
||||
return
|
||||
}
|
||||
if err := s.exp.Update(context.Background(), uid, core.Success, "", attack.String()); err != nil {
|
||||
log.Error("failed to update experiment", zap.Error(err))
|
||||
}
|
||||
}()
|
||||
|
||||
if err = Host.Shutdown(); err != nil {
|
||||
err = errors.WithStack(err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
// +build aix darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris
|
||||
|
||||
// Copyright 2021 Chaos Mesh Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package chaosd
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
|
||||
"github.com/pingcap/log"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type UnixHost struct{}
|
||||
|
||||
var Host HostManager = UnixHost{}
|
||||
|
||||
const CmdShutdown = "shutdown"
|
||||
|
||||
func (h UnixHost) Name() string {
|
||||
return "unix"
|
||||
}
|
||||
|
||||
func (h UnixHost) Shutdown() error {
|
||||
cmd := exec.Command(CmdShutdown)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Error(string(output), zap.Error(err))
|
||||
}
|
||||
return err
|
||||
}
|
||||
Loading…
Reference in New Issue