forked from chenhy97/chaosblade-exec-docker
/
executor_execin.go
127 lines (114 loc) · 4.58 KB
/
executor_execin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright 1999-2019 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 exec
import (
"context"
"fmt"
"path"
"strconv"
"strings"
"github.com/sirupsen/logrus"
"github.com/chaosblade-io/chaosblade-spec-go/channel"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-exec-docker/version"
)
var defaultBladeTarFilePath = fmt.Sprintf("/opt/chaosblade-%s.tar.gz", version.BladeVersion)
// RunCmdInContainerExecutor is an executor interface which executes command in the target container directly
type RunCmdInContainerExecutor interface {
spec.Executor
DeployChaosBlade(ctx context.Context, containerId string, srcFile, extractDirName string, override bool) error
}
// RunCmdInContainerExecutorByCP is an executor implementation which used copy chaosblade tool to the target container and executed
type RunCmdInContainerExecutorByCP struct {
BaseDockerClientExecutor
}
func NewRunCmdInContainerExecutorByCP() RunCmdInContainerExecutor {
return &RunCmdInContainerExecutorByCP{
BaseDockerClientExecutor{
CommandFunc: commonFunc,
},
}
}
func (r *RunCmdInContainerExecutorByCP) Name() string {
return "runCmdInContainerExecutorByCP"
}
func (r *RunCmdInContainerExecutorByCP) Exec(uid string, ctx context.Context, expModel *spec.ExpModel) *spec.Response {
containerId := expModel.ActionFlags[ContainerIdFlag.Name]
if containerId == "" {
return spec.ReturnFail(spec.Code[spec.IllegalParameters], "less container id parameter")
}
if err := r.SetClient(expModel); err != nil {
return spec.ReturnFail(spec.Code[spec.DockerInvokeError], err.Error())
}
command := r.CommandFunc(uid, ctx, expModel)
if _, ok := spec.IsDestroy(ctx); !ok {
// Create
bladeTarFilePath := expModel.ActionFlags[ChaosBladeTarFilePathFlag.Name]
if bladeTarFilePath == "" {
bladeTarFilePath = defaultBladeTarFilePath
}
overrideValue := expModel.ActionFlags[DeployBladeOverrideFlag.Name]
override, err := strconv.ParseBool(overrideValue)
if err != nil {
override = false
}
response := channel.NewLocalChannel().Run(context.Background(), "tar",
fmt.Sprintf("tf %s| head -1 | cut -f1 -d/", bladeTarFilePath))
if !response.Success {
return spec.ReturnFail(spec.Code[spec.IllegalParameters], response.Err)
}
if response.Result == nil {
return spec.ReturnFail(spec.Code[spec.IllegalParameters],
fmt.Sprintf("extract directory from %s failed", bladeTarFilePath))
}
extractedDirName := strings.TrimSpace(response.Result.(string))
if extractedDirName == "" {
return spec.ReturnFail(spec.Code[spec.IllegalParameters],
fmt.Sprintf("extract empty directory name from %s failed", bladeTarFilePath))
}
err = r.DeployChaosBlade(ctx, containerId, bladeTarFilePath, extractedDirName, override)
if err != nil {
return spec.ReturnFail(spec.Code[spec.DockerInvokeError], err.Error())
}
}
output, err := r.Client.execContainer(containerId, command)
var defaultResponse *spec.Response
if err != nil {
defaultResponse = spec.ReturnFail(spec.Code[spec.K8sInvokeError], err.Error())
}
return ConvertContainerOutputToResponse(output, err, defaultResponse)
}
func (r *RunCmdInContainerExecutorByCP) SetChannel(channel spec.Channel) {
}
func (r *RunCmdInContainerExecutorByCP) DeployChaosBlade(ctx context.Context, containerId string,
srcFile, extractDirName string, override bool) error {
// check if the blade tool exists
output, err := r.Client.execContainerPrivileged(containerId, fmt.Sprintf("[ -e %s ] && echo True || echo False", BladeBin))
logrus.Debugf("output: %s, %v", output, err)
if err == nil && strings.Contains(output, "True") && !override {
return nil
}
err = r.Client.CopyToContainer(context.TODO(), containerId, srcFile, DstChaosBladeDir, override)
if err != nil {
return err
}
dstBladeDir := path.Join(DstChaosBladeDir, extractDirName)
expectBladeDir := path.Join(DstChaosBladeDir, "chaosblade")
renameCmd := fmt.Sprintf("rm -rf %s && mv %s %s", expectBladeDir, dstBladeDir, expectBladeDir)
logrus.Debugf("renameCmd: %s", renameCmd)
_, err = r.Client.execContainerPrivileged(containerId, renameCmd)
return err
}