Add generic bash blocking command

This commit is contained in:
stefanprodan
2019-06-04 14:01:25 +03:00
parent 2bc8194d96
commit 9f359327f0
3 changed files with 14 additions and 10 deletions

View File

@@ -10,7 +10,7 @@ import (
"time"
)
var VERSION = "0.4.0-beta.2"
var VERSION = "0.4.0-beta.5"
var (
logLevel string
port string

View File

@@ -6,19 +6,19 @@ import (
"os/exec"
)
const TaskTypeBats = "bats"
const TaskTypeBash = "bash"
type BatsTask struct {
type BashTask struct {
TaskBase
command string
logCmdOutput bool
}
func (task *BatsTask) Hash() string {
func (task *BashTask) Hash() string {
return hash(task.canary + task.command)
}
func (task *BatsTask) Run(ctx context.Context) (bool, error) {
func (task *BashTask) Run(ctx context.Context) (bool, error) {
cmd := exec.CommandContext(ctx, "bash", "-c", task.command)
out, err := cmd.CombinedOutput()
@@ -34,6 +34,6 @@ func (task *BatsTask) Run(ctx context.Context) (bool, error) {
return true, nil
}
func (task *BatsTask) String() string {
func (task *BashTask) String() string {
return task.command
}

View File

@@ -46,10 +46,12 @@ func ListenAndServe(port string, timeout time.Duration, logger *zap.SugaredLogge
}
// run bats command (blocking task)
if typ == TaskTypeBats {
bats := BatsTask{
if typ == TaskTypeBash {
logger.With("canary", payload.Name).Infof("bats command %s", payload.Metadata["cmd"])
bats := BashTask{
command: payload.Metadata["cmd"],
logCmdOutput: taskRunner.logCmdOutput,
logCmdOutput: true,
TaskBase: TaskBase{
canary: fmt.Sprintf("%s.%s", payload.Name, payload.Namespace),
logger: logger,
@@ -63,6 +65,7 @@ func ListenAndServe(port string, timeout time.Duration, logger *zap.SugaredLogge
if !ok {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
@@ -73,7 +76,7 @@ func ListenAndServe(port string, timeout time.Duration, logger *zap.SugaredLogge
if typ == TaskTypeHelm {
helm := HelmTask{
command: payload.Metadata["cmd"],
logCmdOutput: taskRunner.logCmdOutput,
logCmdOutput: true,
TaskBase: TaskBase{
canary: fmt.Sprintf("%s.%s", payload.Name, payload.Namespace),
logger: logger,
@@ -87,6 +90,7 @@ func ListenAndServe(port string, timeout time.Duration, logger *zap.SugaredLogge
if !ok {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)