refactor(be): add local app params class

This commit is contained in:
Denis Gukov 2024-11-24 15:32:24 +05:00
parent 3161fa9ae2
commit 79008d1458
6 changed files with 48 additions and 15 deletions

View File

@ -9,6 +9,9 @@ import (
"github.com/semaphoreui/semaphore/util"
)
type ShellTaskParams struct {
}
type TerraformTaskParams struct {
Plan bool `json:"plan"`
AutoApprove bool `json:"auto_approve"`

View File

@ -61,8 +61,8 @@ func (t *AnsibleApp) SetLogger(logger task_logger.Logger) task_logger.Logger {
return logger
}
func (t *AnsibleApp) Run(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error {
return t.Playbook.RunPlaybook(args, environmentVars, inputs, cb)
func (t *AnsibleApp) Run(args LocalAppRunningArgs) error {
return t.Playbook.RunPlaybook(args.CliArgs, args.EnvironmentVars, args.Inputs, args.Callback)
}
func (t *AnsibleApp) Log(msg string) {

View File

@ -27,8 +27,16 @@ func getEnvironmentVars() []string {
return res
}
type LocalAppRunningArgs struct {
CliArgs []string
EnvironmentVars *[]string
Inputs map[string]string
TaskParams interface{}
Callback func(*os.Process)
}
type LocalApp interface {
SetLogger(logger task_logger.Logger) task_logger.Logger
InstallRequirements(environmentVars *[]string) error
Run(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error
Run(args LocalAppRunningArgs) error
}

View File

@ -2,7 +2,6 @@ package db_lib
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
@ -107,8 +106,8 @@ func (t *ShellApp) makeShellCmd(args []string, environmentVars *[]string) *exec.
return t.makeCmd(command, append(appArgs, args...), environmentVars)
}
func (t *ShellApp) Run(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error {
cmd := t.makeShellCmd(args, environmentVars)
func (t *ShellApp) Run(args LocalAppRunningArgs) error {
cmd := t.makeShellCmd(args.CliArgs, args.EnvironmentVars)
t.Logger.LogCmd(cmd)
//cmd.Stdin = &t.reader
cmd.Stdin = strings.NewReader("")
@ -116,6 +115,6 @@ func (t *ShellApp) Run(args []string, environmentVars *[]string, inputs map[stri
if err != nil {
return err
}
cb(cmd.Process)
args.Callback(cmd.Process)
return cmd.Wait()
}

View File

@ -148,8 +148,8 @@ func (t *TerraformApp) Apply(args []string, environmentVars *[]string, inputs ma
return cmd.Wait()
}
func (t *TerraformApp) Run(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error {
err := t.Plan(args, environmentVars, inputs, cb)
func (t *TerraformApp) Run(args LocalAppRunningArgs) error {
err := t.Plan(args.CliArgs, args.EnvironmentVars, args.Inputs, args.Callback)
if err != nil {
return err
}
@ -173,7 +173,7 @@ func (t *TerraformApp) Run(args []string, environmentVars *[]string, inputs map[
return nil
case terraformReaderConfirmed:
t.Logger.SetStatus(task_logger.TaskRunningStatus)
return t.Apply(args, environmentVars, inputs, cb)
return t.Apply(args.CliArgs, args.EnvironmentVars, args.Inputs, args.Callback)
default:
return fmt.Errorf("unknown plan result")
}

View File

@ -323,15 +323,22 @@ func (t *LocalJob) getPlaybookArgs(username string, incomingVersion *string) (ar
}
}
if t.Task.Debug {
var params db.AnsibleTaskParams
err = t.Task.GetParams(&params)
if err != nil {
return
}
if params.Debug {
args = append(args, "-vvvv")
}
if t.Task.Diff {
if params.Diff {
args = append(args, "--diff")
}
if t.Task.DryRun {
if params.DryRun {
args = append(args, "--check")
}
@ -419,20 +426,30 @@ func (t *LocalJob) Run(username string, incomingVersion *string) (err error) {
var args []string
var inputs map[string]string
var params interface{}
switch t.Template.App {
case db.AppAnsible:
args, inputs, err = t.getPlaybookArgs(username, incomingVersion)
params = db.AnsibleTaskParams{}
case db.AppTerraform, db.AppTofu:
args, err = t.getTerraformArgs(username, incomingVersion)
params = db.TerraformTaskParams{}
default:
args, err = t.getShellArgs(username, incomingVersion)
params = db.ShellTaskParams{}
}
if err != nil {
return
}
err = t.Task.GetParams(params)
if err != nil {
return
}
if t.Inventory.SSHKey.Type == db.AccessKeySSH && t.Inventory.SSHKeyID != nil {
environmentVariables = append(environmentVariables, fmt.Sprintf("SSH_AUTH_SOCK=%s", t.sshKeyInstallation.SSHAgent.SocketFile))
}
@ -454,8 +471,14 @@ func (t *LocalJob) Run(username string, incomingVersion *string) (err error) {
}
}
return t.App.Run(args, &environmentVariables, inputs, func(p *os.Process) {
return t.App.Run(db_lib.LocalAppRunningArgs{
CliArgs: args,
EnvironmentVars: &environmentVariables,
Inputs: inputs,
TaskParams: params,
Callback: func(p *os.Process) {
t.Process = p
},
})
}