Skip to content

Commit

Permalink
Implement Executor for script run
Browse files Browse the repository at this point in the history
Signed-off-by: Yoshiki Fujikane <[email protected]>
  • Loading branch information
ffjlabo committed Dec 15, 2023
1 parent e6118ce commit 9351080
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/app/piped/executor/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pipe-cd/pipecd/pkg/app/piped/executor/ecs"
"github.com/pipe-cd/pipecd/pkg/app/piped/executor/kubernetes"
"github.com/pipe-cd/pipecd/pkg/app/piped/executor/lambda"
"github.com/pipe-cd/pipecd/pkg/app/piped/executor/scriptrun"
"github.com/pipe-cd/pipecd/pkg/app/piped/executor/terraform"
"github.com/pipe-cd/pipecd/pkg/app/piped/executor/wait"
"github.com/pipe-cd/pipecd/pkg/app/piped/executor/waitapproval"
Expand Down Expand Up @@ -112,4 +113,5 @@ func init() {
wait.Register(defaultRegistry)
waitapproval.Register(defaultRegistry)
customsync.Register(defaultRegistry)
scriptrun.Register(defaultRegistry)
}
67 changes: 67 additions & 0 deletions pkg/app/piped/executor/scriptrun/scriptrun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package scriptrun

import (
"os"
"os/exec"

"github.com/pipe-cd/pipecd/pkg/app/piped/executor"
"github.com/pipe-cd/pipecd/pkg/model"
)

type registerer interface {
Register(stage model.Stage, f executor.Factory) error
RegisterRollback(kind model.RollbackKind, f executor.Factory) error
}

type Executor struct {
executor.Input
}

func (e *Executor) Execute(sig executor.StopSignal) model.StageStatus {
e.LogPersister.Infof("Start executing the script run stage")

opts := e.Input.StageConfig.ScriptRunStageOptions
if opts == nil {
e.LogPersister.Infof("option for script run stage not found")
return model.StageStatus_STAGE_FAILURE
}

if opts.Run == "" {
return model.StageStatus_STAGE_SUCCESS
}

envs := make([]string, 0, len(opts.Env))
for key, value := range opts.Env {
envs = append(envs, key+"="+value)
}

cmd := exec.Command("/bin/sh", "-l", "-c", opts.Run)
cmd.Env = append(os.Environ(), envs...)
cmd.Stdout = e.LogPersister
cmd.Stderr = e.LogPersister

e.LogPersister.Infof("executing script:")
e.LogPersister.Infof(opts.Run)

if err := cmd.Run(); err != nil {
return model.StageStatus_STAGE_FAILURE
}
return model.StageStatus_STAGE_SUCCESS
}

type RollbackExecutor struct {
executor.Input
}

func (e *RollbackExecutor) Execute(sig executor.StopSignal) model.StageStatus {
return model.StageStatus_STAGE_NOT_STARTED_YET
}

// Register registers this executor factory into a given registerer.
func Register(r registerer) {
r.Register(model.StageScriptRun, func(in executor.Input) executor.Executor {
return &Executor{
Input: in,
}
})
}

0 comments on commit 9351080

Please sign in to comment.