From 48dfc808aabc4297dc927deb14662632f236444b Mon Sep 17 00:00:00 2001 From: cwen0 Date: Mon, 2 Nov 2020 11:54:15 +0800 Subject: [PATCH] combine chaosd and chaos binary Signed-off-by: cwen0 --- Makefile | 20 ++++-- cmd/chaos/ctl/command/destroy_command.go | 31 +++++++++ cmd/chaos/ctl/command/process_command.go | 68 ++++++++++++------- .../ctl/command/server_command.go} | 46 +++++++------ cmd/chaos/ctl/command/util.go | 20 ++++++ cmd/chaos/ctl/ctl.go | 2 + images/chaosd/Dockerfile | 1 - pkg/core/process.go | 2 - pkg/server/server.go | 3 - pkg/store/dbstore/store.go | 14 ++++ revive.toml | 49 +++++++++++++ 11 files changed, 196 insertions(+), 60 deletions(-) create mode 100644 cmd/chaos/ctl/command/destroy_command.go rename cmd/{chaosd/main.go => chaos/ctl/command/server_command.go} (52%) create mode 100644 revive.toml diff --git a/Makefile b/Makefile index 67b5e27..775c192 100644 --- a/Makefile +++ b/Makefile @@ -37,9 +37,15 @@ failpoint-disable: $(GOBIN)/failpoint-ctl $(GOBIN)/failpoint-ctl: $(GO) get github.com/pingcap/failpoint/failpoint-ctl@v0.0.0-20200210140405-f8f9fb234798 +$(GOBIN)/revive: + $(GO) get github.com/mgechev/revive@v1.0.2-0.20200225072153-6219ca02fffb + +$(GOBIN)/goimports: + $(GO) get golang.org/x/tools/cmd/goimports@v0.0.0-20200309202150-20ab64c0d93f + build: binary -binary: chaosd chaos bin/pause bin/suicide +binary: chaosd bin/pause bin/suicide taily-build: if [ "$(shell docker ps --filter=name=$@ -q)" = "" ]; then \ @@ -61,10 +67,7 @@ image-binary: image-build-base endif chaosd: - $(CGOENV) go build -ldflags '$(LDFLAGS)' -o bin/chaosd ./cmd/chaosd/main.go - -chaos: - $(GOENV) go build -ldflags '$(LDFLAGS)' -o bin/chaos ./cmd/chaos/main.go + $(CGOENV) go build -ldflags '$(LDFLAGS)' -o bin/chaosd ./cmd/chaos/main.go image-build-base: DOCKER_BUILDKIT=0 docker build --ulimit nofile=65536:65536 -t pingcap/chaos-build-base ${DOCKER_BUILD_ARGS} images/build-base @@ -78,7 +81,6 @@ bin/pause: ./hack/pause.c bin/suicide: ./hack/suicide.c cc ./hack/suicide.c -o bin/suicide - image-chaos-mesh-protoc: docker build -t pingcap/chaos-daemon-protoc ${DOCKER_BUILD_ARGS} ./images/protoc @@ -107,6 +109,10 @@ groupimports: $(GOBIN)/goimports vet: $(CGOENV) go vet ./... +lint: $(GOBIN)/revive + @echo "linting" + $< -formatter friendly -config revive.toml $$($(PACKAGE_LIST)) + boilerplate: ./hack/verify-boilerplate.sh @@ -115,4 +121,4 @@ tidy: GO111MODULE=on go mod tidy git diff -U --exit-code go.mod go.sum -.PHONY: all build check fmt vet tidy binary chaosd chaos image-binary image-chaosd \ No newline at end of file +.PHONY: all build check fmt vet lint tidy binary chaosd chaos image-binary image-chaosd diff --git a/cmd/chaos/ctl/command/destroy_command.go b/cmd/chaos/ctl/command/destroy_command.go new file mode 100644 index 0000000..ac5bdd3 --- /dev/null +++ b/cmd/chaos/ctl/command/destroy_command.go @@ -0,0 +1,31 @@ +// Copyright 2020 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 "github.com/spf13/cobra" + +func NewDestroyCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "destroy UID", + Short: "Destroy a chaos experiment", + Args: cobra.MinimumNArgs(1), + Run: destroyCommandF, + } + + return cmd +} + +func destroyCommandF(cmd *cobra.Command, args []string) { + +} diff --git a/cmd/chaos/ctl/command/process_command.go b/cmd/chaos/ctl/command/process_command.go index 02c94e6..d344c7b 100644 --- a/cmd/chaos/ctl/command/process_command.go +++ b/cmd/chaos/ctl/command/process_command.go @@ -23,10 +23,26 @@ import ( "github.com/chaos-mesh/chaos-daemon/pkg/core" ) -var ( +type processFlag struct { process string + single int +} + +const ( + killProcessAction = "kill" + stopProcessAction = "stop" ) +func (f *processFlag) valid(action string) error { + if len(f.process) == 0 { + return errors.New("process not provided") + } + + return nil +} + +var pFlag *processFlag + func NewProcessAttackCommand() *cobra.Command { cmd := &cobra.Command{ Use: "process ", @@ -44,11 +60,14 @@ func NewProcessAttackCommand() *cobra.Command { func NewProcessKillCommand() *cobra.Command { cmd := &cobra.Command{ Use: "kill [options]", - Short: "kill process, default signal SIGKILL", + Short: "kill process, default signal 9", Run: processKillCommandFunc, } - cmd.Flags().StringVarP(&process, "process", "p", "", "The process name or the process ID") + cmd.Flags().StringVarP(&pFlag.process, "process", "p", "", "The process name or the process ID") + cmd.Flags().IntVarP(&pFlag.single, "single", "s", 9, "The signal number to send") + cmd.Flags().StringVarP(&conf.Runtime, "runtime", "r", "docker", "current container runtime") + cmd.Flags().StringVarP(&conf.Platform, "platform", "f", "local", "platform to deploy, default: local, supported platform: local, kubernetes") return cmd } @@ -61,42 +80,41 @@ func NewProcessStopCommand() *cobra.Command { Run: processStopCommandFunc, } - cmd.Flags().StringVarP(&process, "process", "p", "", "The process name or the process ID") + cmd.Flags().StringVarP(&pFlag.process, "process", "p", "", "The process name or the process ID") + pFlag.single = int(syscall.SIGSTOP) + + cmd.Flags().StringVarP(&conf.Runtime, "runtime", "r", "docker", "current container runtime") + cmd.Flags().StringVarP(&conf.Platform, "platform", "f", "local", "platform to deploy, default: local, supported platform: local, kubernetes") return cmd } func processKillCommandFunc(cmd *cobra.Command, args []string) { - processAttackF(cmd, syscall.SIGKILL) + if err := pFlag.valid(killProcessAction); err != nil { + ExitWithError(ExitBadArgs, err) + } + + processAttackF(cmd, pFlag) } func processStopCommandFunc(cmd *cobra.Command, args []string) { - processAttackF(cmd, syscall.SIGSTOP) -} - -func processAttackF(cmd *cobra.Command, sig syscall.Signal) { - if len(process) == 0 { - ExitWithError(ExitBadArgs, errors.New("process not provided")) + if err := pFlag.valid(stopProcessAction); err != nil { + ExitWithError(ExitBadArgs, err) } - cli := mustClientFromCmd(cmd) + processAttackF(cmd, pFlag) +} - resp, apiErr, err := cli.CreateProcessAttack(&core.ProcessCommand{ - Process: process, - Signal: sig, +func processAttackF(cmd *cobra.Command, f *processFlag) { + chaos := mustChaosdFromCmd(cmd, conf) + + uid, err := chaos.ProcessAttack(&core.ProcessCommand{ + Process: pFlag.process, + Signal: syscall.Signal(f.single), }) - if err != nil { ExitWithError(ExitError, err) } - if apiErr != nil { - ExitWithMsg(ExitError, fmt.Sprintf("Failed to attack process %s, %s", process, apiErr.Message)) - } - - if resp.Status != 200 { - ExitWithMsg(ExitError, fmt.Sprintf("Failed to attack process %s, %s", process, resp.Message)) - } - - NormalExit(fmt.Sprintf("Attack process %s successfully, uid: %s", process, resp.UID)) + NormalExit(fmt.Sprintf("Attack process %s successfully, uid: %s", f.process, uid)) } diff --git a/cmd/chaosd/main.go b/cmd/chaos/ctl/command/server_command.go similarity index 52% rename from cmd/chaosd/main.go rename to cmd/chaos/ctl/command/server_command.go index 14affb6..aaed829 100644 --- a/cmd/chaosd/main.go +++ b/cmd/chaos/ctl/command/server_command.go @@ -11,18 +11,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package command import ( - "os" - + "github.com/spf13/cobra" "go.uber.org/fx" - "go.uber.org/zap" - - _ "github.com/jinzhu/gorm/dialects/sqlite" - "github.com/pingcap/errors" - "github.com/pingcap/log" - flag "github.com/spf13/pflag" "github.com/chaos-mesh/chaos-daemon/pkg/bpm" "github.com/chaos-mesh/chaos-daemon/pkg/config" @@ -32,27 +25,36 @@ import ( "github.com/chaos-mesh/chaos-daemon/pkg/version" ) -func main() { - cfg := config.NewConfig() - err := cfg.Parse(os.Args[1:]) +func NewServerCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "server