Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature:append file content support base64 encoding #45

Merged
merged 1 commit into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions exec/bin/file/appendfile/appendfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"encoding/base64"
"flag"
"fmt"
"github.com/chaosblade-io/chaosblade-exec-os/exec/bin"
Expand All @@ -37,14 +38,15 @@ import (

var content, filepath string
var count, interval int
var escape, appendFileStart, appendFileStop, appendFileNoHup bool
var escape, enableBase64, appendFileStart, appendFileStop, appendFileNoHup bool

func main() {
flag.StringVar(&content, "content", "", "content")
flag.StringVar(&filepath, "filepath", "", "filepath")
flag.IntVar(&count, "count", 1, "append count")
flag.IntVar(&interval, "interval", 1, "append count")
flag.BoolVar(&escape, "escape", false, "symbols to escape")
flag.BoolVar(&enableBase64, "enable-base64", false, "append content enableBase64 encoding")
flag.BoolVar(&appendFileStart, "start", false, "start append file")
flag.BoolVar(&appendFileStop, "stop", false, "stop append file")
flag.BoolVar(&appendFileNoHup, "nohup", false, "nohup to run append file")
Expand All @@ -57,9 +59,9 @@ func main() {
if strings.Contains(content, "@@##") {
content = strings.Replace(content, "@@##", " ", -1)
}
startAppendFile(filepath, content, count, interval, escape)
startAppendFile(filepath, content, count, interval, escape, enableBase64)
} else if appendFileNoHup {
appendFile(filepath, content, count, interval, escape)
appendFile(filepath, content, count, interval, escape, enableBase64)
// Wait for signals
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGKILL)
Expand All @@ -83,7 +85,7 @@ func main() {
var cl = channel.NewLocalChannel()
var appendFileBin = "chaos_appendfile"

func startAppendFile(filepath, content string, count int, interval int, escape bool) {
func startAppendFile(filepath, content string, count int, interval int, escape bool, enableBase64 bool) {
// check pid
newCtx := context.WithValue(context.Background(), channel.ProcessKey,
fmt.Sprintf(`--nohup --filepath %s`, filepath))
Expand All @@ -99,8 +101,8 @@ func startAppendFile(filepath, content string, count int, interval int, escape b
}

ctx := context.Background()
args := fmt.Sprintf(`%s --nohup --filepath "%s" --content "%s" --count %d --interval %d --escape=%t`,
path.Join(util.GetProgramPath(), appendFileBin), filepath, content, count, interval, escape)
args := fmt.Sprintf(`%s --nohup --filepath "%s" --content "%s" --count %d --interval %d --escape=%t --enable-base64=%t`,
path.Join(util.GetProgramPath(), appendFileBin), filepath, content, count, interval, escape, enableBase64)
args = fmt.Sprintf(`%s > /dev/null 2>&1 &`, args)
response := cl.Run(ctx, "nohup", args)
if !response.Success {
Expand All @@ -123,19 +125,18 @@ func startAppendFile(filepath, content string, count int, interval int, escape b
}
}

func appendFile(filepath string, content string, count int, interval int, escape bool) {
func appendFile(filepath string, content string, count int, interval int, escape bool, enableBase64 bool) {

content = parseDate(content)
go func() {
ctx := context.Background()
// first append
if append(count, ctx, content, filepath, escape) {
if append(count, ctx, content, filepath, escape, enableBase64) {
return
}

ticker := time.NewTicker(time.Second * time.Duration(interval))
for _ = range ticker.C {
if append(count, ctx, content, filepath, escape) {
for range ticker.C {
if append(count, ctx, content, filepath, escape, enableBase64) {
return
}
}
Expand Down Expand Up @@ -182,9 +183,17 @@ func parseRandom(content string) string {
return content
}

func append(count int, ctx context.Context, content string, filepath string, escape bool) bool {
func append(count int, ctx context.Context, content string, filepath string, escape bool, enableBase64 bool) bool {
var response *spec.Response

if enableBase64 {
decodeBytes, err := base64.StdEncoding.DecodeString(content)
if err != nil {
bin.PrintErrAndExit(err.Error())
return true
}
content = string(decodeBytes)
}
content = parseDate(content)
for i := 0; i < count; i++ {
content = parseRandom(content)
if escape {
Expand Down
21 changes: 16 additions & 5 deletions exec/file_append.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func NewFileAppendActionSpec() spec.ExpActionCommandSpec {
Desc: "symbols to escape, use --escape, at this --count is invalid",
NoArgs: true,
},
&spec.ExpFlag{
Name: "enable-base64",
Desc: "append content enable base64 encoding",
NoArgs: true,
},
},
ActionExecutor: &FileAppendActionExecutor{},
},
Expand Down Expand Up @@ -110,7 +115,6 @@ func (f *FileAppendActionExecutor) Exec(uid string, ctx context.Context, model *
count := 1
// 1000 ms
interval := 1
var escape bool

content := model.ActionFlags["content"]
countStr := model.ActionFlags["count"]
Expand All @@ -130,18 +134,25 @@ func (f *FileAppendActionExecutor) Exec(uid string, ctx context.Context, model *
}
}

escape = model.ActionFlags["escape"] == "true"
escape := model.ActionFlags["escape"] == "true"
enableBase64 := model.ActionFlags["enable-base64"] == "true"

if !util.IsExist(filepath) {
return spec.ReturnFail(spec.Code[spec.IllegalParameters],
fmt.Sprintf("the %s file does not exist", filepath))
}

return f.start(filepath, content, count, interval, escape, ctx)
return f.start(filepath, content, count, interval, escape, enableBase64, ctx)
}

func (f *FileAppendActionExecutor) start(filepath string, content string, count int, interval int, escape bool, ctx context.Context) *spec.Response {
flags := fmt.Sprintf(`--start --filepath "%s" --content "%s" --count %d --interval %d --escape=%t --debug=%t`, filepath, content, count, interval, escape, util.Debug)
func (f *FileAppendActionExecutor) start(filepath string, content string, count int, interval int, escape bool, enableBase64 bool, ctx context.Context) *spec.Response {
flags := fmt.Sprintf(`--start --filepath "%s" --content "%s" --count %d --interval %d --debug=%t`, filepath, content, count, interval, util.Debug)
if escape {
flags = fmt.Sprintf("%s --escape=true", flags)
}
if enableBase64 {
flags = fmt.Sprintf("%s --enable-base64=true", flags)
}
return f.channel.Run(ctx, path.Join(f.channel.GetScriptPath(), appeneFileBin), flags)
}

Expand Down