-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: support nsexec Signed-off-by: tiny-x <[email protected]> * fix: ingore container flag Signed-off-by: tiny-x <[email protected]> * fix: network exp Signed-off-by: tiny-x <[email protected]> * feature: adapte cgroup root path flag Signed-off-by: tiny-x <[email protected]> * feature: improvement exec in container Signed-off-by: tiny-x <[email protected]> * fix: linux exec Signed-off-by: tiny-x <[email protected]> * feature: update version to 1.6.0 & fix cgroup default value Signed-off-by: tiny-x <[email protected]> * chore: update version to 1.6.0 Signed-off-by: tiny-x <[email protected]> * feature: optimization of the log Signed-off-by: tiny-x <[email protected]> * fix: set timeout flag Signed-off-by: tiny-x <[email protected]> * feature: improvement comand exec Signed-off-by: tiny-x <[email protected]> * Merge remote-tracking branch 'origin/main' into 1.6.0-dev Signed-off-by: tiny-x <[email protected]> Merge remote-tracking branch 'origin/main' into 1.6.0-dev Signed-off-by: tiny-x <[email protected]> * fix: network timeout Signed-off-by: tiny-x <[email protected]> * fix: container jvm exec Signed-off-by: tiny-x <[email protected]>
- Loading branch information
Showing
24 changed files
with
1,133 additions
and
435 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.