Skip to content

Commit

Permalink
1.6.0 dev (#4)
Browse files Browse the repository at this point in the history
* feature: support nsexec

Signed-off-by: tiny-x <xf.yefei@gmail.com>

* fix: ingore container flag

Signed-off-by: tiny-x <xf.yefei@gmail.com>

* fix: network exp

Signed-off-by: tiny-x <xf.yefei@gmail.com>

* feature: adapte cgroup root path flag

Signed-off-by: tiny-x <xf.yefei@gmail.com>

* feature: improvement exec in container

Signed-off-by: tiny-x <xf.yefei@gmail.com>

* fix: linux exec

Signed-off-by: tiny-x <xf.yefei@gmail.com>

* feature: update version to 1.6.0 & fix cgroup default value

Signed-off-by: tiny-x <xf.yefei@gmail.com>

* chore: update version to 1.6.0

Signed-off-by: tiny-x <xf.yefei@gmail.com>

* feature: optimization of the log

Signed-off-by: tiny-x <xf.yefei@gmail.com>

* fix: set timeout flag

Signed-off-by: tiny-x <xf.yefei@gmail.com>

* feature: improvement comand exec

Signed-off-by: tiny-x <xf.yefei@gmail.com>

* Merge remote-tracking branch 'origin/main' into 1.6.0-dev

Signed-off-by: tiny-x <xf.yefei@gmail.com>

Merge remote-tracking branch 'origin/main' into 1.6.0-dev

Signed-off-by: tiny-x <xf.yefei@gmail.com>

* fix: network timeout

Signed-off-by: tiny-x <xf.yefei@gmail.com>

* fix: container jvm exec

Signed-off-by: tiny-x <xf.yefei@gmail.com>
  • Loading branch information
tiny-x authored Apr 29, 2022
1 parent 73b7a53 commit e822315
Show file tree
Hide file tree
Showing 24 changed files with 1,133 additions and 435 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ GO=env $(GO_ENV) $(GO_MODULE) go
UNAME := $(shell uname)

ifeq ($(BLADE_VERSION), )
BLADE_VERSION=1.5.0
BLADE_VERSION=1.6.0
endif

BUILD_TARGET=target
Expand All @@ -16,8 +16,11 @@ BUILD_TARGET_PKG_DIR=$(BUILD_TARGET)/chaosblade-$(BLADE_VERSION)
BUILD_TARGET_YAML=$(BUILD_TARGET_PKG_DIR)/yaml
BUILD_IMAGE_PATH=build/image/blade

OS_YAML_FILE_NAME=chaosblade-cri-spec-$(BLADE_VERSION).yaml
OS_YAML_FILE_PATH=$(BUILD_TARGET_YAML)/$(OS_YAML_FILE_NAME)
CRI_OS_YAML_FILE_NAME=chaosblade-cri-spec-$(BLADE_VERSION).yaml
CRI_OS_YAML_FILE_PATH=$(BUILD_TARGET_YAML)/$(CRI_OS_YAML_FILE_NAME)

DOCKER_OS_YAML_FILE_NAME=chaosblade-docker-spec-$(BLADE_VERSION).yaml
DOCKER_OS_YAML_FILE_PATH=$(BUILD_TARGET_YAML)/$(DOCKER_OS_YAML_FILE_NAME)

CHAOSBLADE_PATH=build/cache/chaosblade

Expand All @@ -34,7 +37,8 @@ pre_build:
mkdir -p $(BUILD_TARGET_YAML)

build_yaml: build/spec.go
$(GO) run $< $(OS_YAML_FILE_PATH) $(CHAOSBLADE_PATH)/yaml/chaosblade-jvm-spec-$(BLADE_VERSION).yaml
$(GO) run $< $(CRI_OS_YAML_FILE_PATH) cri $(CHAOSBLADE_PATH)/yaml/chaosblade-jvm-spec-$(BLADE_VERSION).yaml
$(GO) run $< $(DOCKER_OS_YAML_FILE_PATH) docker $(CHAOSBLADE_PATH)/yaml/chaosblade-jvm-spec-$(BLADE_VERSION).yaml

# test
test:
Expand Down
25 changes: 17 additions & 8 deletions build/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"fmt"
"log"
"os"

Expand All @@ -28,24 +29,32 @@ import (

// main creates a yaml file of the experiments in the project
func main() {
if len(os.Args) < 2 {
if len(os.Args) < 3 {
log.Panicln("less yaml file path")
}
if len(os.Args) == 3 {
exec.JvmSpecFileForYaml = os.Args[2]
if len(os.Args) == 4 {
exec.JvmSpecFileForYaml = os.Args[3]
}
err := util.CreateYamlFile(getModels(), os.Args[1])
err := util.CreateYamlFile(getModels(os.Args[2]), os.Args[1])
if err != nil {
log.Panicf("create yaml file error, %v", err)
}
}

// getModels returns the supported experiment specs
func getModels() *spec.Models {
func getModels(scope string) *spec.Models {
models := make([]*spec.Models, 0)
dockerModelSpec := exec.NewCriExpModelSpec()
for _, modelSpec := range dockerModelSpec.ExpModels() {
model := util.ConvertSpecToModels(modelSpec, spec.ExpPrepareModel{}, dockerModelSpec.Scope())
var modelSpecs *exec.DockerExpModelSpec
if scope == "cri" {
modelSpecs = exec.NewCriExpModelSpec()
} else if scope == "docker" {
modelSpecs = exec.NewDockerExpModelSpec()
} else {
log.Panicln(fmt.Sprintf("un support scope: %s", scope))
}

for _, modelSpec := range modelSpecs.ExpModels() {
model := util.ConvertSpecToModels(modelSpec, spec.ExpPrepareModel{}, modelSpec.Scope())
models = append(models, model)
}
return util.MergeModels(models...)
Expand Down
5 changes: 3 additions & 2 deletions exec/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
package exec

import (
"context"
"fmt"
"github.com/chaosblade-io/chaosblade-spec-go/log"
"path"

"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
"github.com/sirupsen/logrus"

"github.com/chaosblade-io/chaosblade-exec-cri/version"
)
Expand All @@ -38,7 +39,7 @@ func getJvmModels() []spec.ExpModelCommandSpec {
modelCommandSpecs := make([]spec.ExpModelCommandSpec, 0)
models, err := util.ParseSpecsToModel(jvmSpecFile, nil)
if err != nil {
logrus.Warningf("parse java spec failed, so skip it, %s", err)
log.Warnf(context.Background(), "parse java spec failed, so skip it, %s", err)
return modelCommandSpecs
}
for idx := range models.Models {
Expand Down
6 changes: 3 additions & 3 deletions exec/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package exec

import (
"context"
"github.com/chaosblade-io/chaosblade-spec-go/log"
"io/ioutil"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/sirupsen/logrus"
)

var cli *Client
Expand All @@ -33,14 +33,14 @@ type Client struct {
}

// waitAndGetOutput returns the result
func (c *Client) waitAndGetOutput(containerId string) (string, error) {
func (c *Client) waitAndGetOutput(ctx context.Context, containerId string) (string, error) {
containerWait()
resp, err := c.client.ContainerLogs(context.Background(), containerId, types.ContainerLogsOptions{
ShowStderr: true,
ShowStdout: true,
})
if err != nil {
logrus.Warningf("Get container: %s log err: %s", containerId, err)
log.Warnf(ctx, "Get container: %s log err: %s", containerId, err)
return "", err
}
defer resp.Close()
Expand Down
10 changes: 5 additions & 5 deletions exec/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package exec

import (
"context"
"github.com/chaosblade-io/chaosblade-spec-go/log"

"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
)

const (
Expand Down Expand Up @@ -113,21 +113,21 @@ func (e *removeActionExecutor) Exec(uid string, ctx context.Context, model *spec
flags := model.ActionFlags
client, err := GetClientByRuntime(model)
if err != nil {
util.Errorf(uid, util.GetRunFuncName(), spec.ContainerExecFailed.Sprintf("GetClient", err))
log.Errorf(ctx, spec.ContainerExecFailed.Sprintf("GetClient", err))
return spec.ResponseFailWithFlags(spec.ContainerExecFailed, "GetClient", err)
}
containerId := flags[ContainerIdFlag.Name]
containerName := flags[ContainerNameFlag.Name]
containerLabelSelector := parseContainerLabelSelector(flags[ContainerNameFlag.Name])
container, response := GetContainer(client, uid, containerId, containerName, containerLabelSelector)
container, response := GetContainer(ctx, client, uid, containerId, containerName, containerLabelSelector)
if !response.Success {
return response
}
forceFlag := flags[ForceFlag]

err = client.RemoveContainer(container.ContainerId, judgeForce(forceFlag))
err = client.RemoveContainer(ctx, container.ContainerId, judgeForce(forceFlag))
if err != nil {
util.Errorf(uid, util.GetRunFuncName(), spec.ContainerExecFailed.Sprintf("ContainerRemove", err))
log.Errorf(ctx, spec.ContainerExecFailed.Sprintf("ContainerRemove", err))
return spec.ResponseFailWithFlags(spec.ContainerExecFailed, "ContainerRemove", err)
}
return spec.ReturnSuccess(uid)
Expand Down
15 changes: 8 additions & 7 deletions exec/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package container

import (
"context"
"fmt"
"time"

Expand All @@ -35,15 +36,15 @@ const (
)

type Container interface {
GetContainerById(containerId string) (ContainerInfo, error, int32)
GetContainerByName(containerName string) (ContainerInfo, error, int32)
GetPidById(ctx context.Context, containerId string) (int32, error, int32)
GetContainerById(ctx context.Context, containerId string) (ContainerInfo, error, int32)
GetContainerByName(ctx context.Context, containerName string) (ContainerInfo, error, int32)
GetContainerByLabelSelector(containerLabelSelector map[string]string) (ContainerInfo, error, int32)
RemoveContainer(containerId string, force bool) error
CopyToContainer(containerId, srcFile, dstPath, extractDirName string, override bool) error
RemoveContainer(ctx context.Context, containerId string, force bool) error
CopyToContainer(ctx context.Context, containerId, srcFile, dstPath, extractDirName string, override bool) error

ExecContainer(containerId, command string) (output string, err error)
ExecContainerPrivileged(containerId, command string) (output string, err error)
ExecuteAndRemove(config *containertype.Config, hostConfig *containertype.HostConfig,
ExecContainer(ctx context.Context, containerId, command string) (output string, err error)
ExecuteAndRemove(ctx context.Context, config *containertype.Config, hostConfig *containertype.HostConfig,
networkConfig *network.NetworkingConfig, containerName string, removed bool, timeout time.Duration,
command string, containerInfo ContainerInfo) (containerId string, output string, err error, code int32)
}
Expand Down
115 changes: 115 additions & 0 deletions exec/container/container_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* 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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package container

import (
"bytes"
"context"
"errors"
"fmt"
"github.com/chaosblade-io/chaosblade-spec-go/log"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
"os"
"os/exec"
"path"
"strings"
)

func CopyToContainer(ctx context.Context, pid uint32, srcFile, dstPath, extractDirName string, override bool) error {

args := fmt.Sprintf("-t %d -p -m -- /bin/sh -c", pid)
argsArray := strings.Split(args, " ")
nsbin := path.Join(util.GetProgramPath(), "bin", spec.NSExecBin)

command := fmt.Sprintf("cat > %s", path.Join(dstPath, path.Base(srcFile)))
log.Infof(ctx, "run copy cmd: ", nsbin, args, command)

cmd := exec.Command(nsbin, append(argsArray, command)...)

var outMsg bytes.Buffer
var errMsg bytes.Buffer
cmd.Stdout = &outMsg
cmd.Stderr = &errMsg

open, err := os.Open(srcFile)
defer open.Close()
if err != nil {
return err
}
cmd.Stdin = open
if err := cmd.Start(); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
return err
}
log.Debugf(ctx, "Command Result, output: %s, errMsg: %s", outMsg.String(), errMsg.String())

if errMsg.Len() != 0 {
return errors.New(errMsg.String())
}

// tar -zxf
command = fmt.Sprintf("-t %d -p -m -- tar -zxf %s -C %s", pid, path.Join(dstPath, path.Base(srcFile)), dstPath)
log.Infof(ctx, "run tar cmd: %s %s", nsbin, command)
cmd = exec.Command(nsbin, strings.Split(command, " ")...)
//
var outMsg2 bytes.Buffer
var errMsg2 bytes.Buffer
cmd.Stdout = &outMsg2
cmd.Stderr = &errMsg2
err = cmd.Run()
log.Debugf(ctx, "Tar Command Result, output: %s, errMsg: %s, err: %v", outMsg.String(), errMsg.String(), err)

if err != nil {
return err
}

if errMsg2.Len() != 0 {
return errors.New(errMsg.String())
}

return nil
}

func ExecContainer(ctx context.Context, pid int32, command string) (output string, err error) {

args := fmt.Sprintf("-t %d -p -m -n -- /bin/sh -c", pid)
argsArray := strings.Split(args, " ")
nsbin := path.Join(util.GetProgramPath(), "bin", spec.NSExecBin)

log.Infof(ctx, "cxec container cmd: %s %s %s", nsbin, args, command)

cmd := exec.Command(nsbin, append(argsArray, command)...)

var outMsg bytes.Buffer
var errMsg bytes.Buffer
cmd.Stdout = &outMsg
cmd.Stderr = &errMsg
err = cmd.Run()

log.Debugf(ctx, "Command Result, output: %s, errMsg: %s, err: %v", outMsg.String(), errMsg.String(), err)

if err != nil {
return "", err
}
if errMsg.Len() > 0 {
return errMsg.String(), nil
}

return outMsg.String(), nil
}
Loading

0 comments on commit e822315

Please sign in to comment.