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" "github.com/semaphoreui/semaphore/util"
) )
type ShellTaskParams struct {
}
type TerraformTaskParams struct { type TerraformTaskParams struct {
Plan bool `json:"plan"` Plan bool `json:"plan"`
AutoApprove bool `json:"auto_approve"` AutoApprove bool `json:"auto_approve"`

View File

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

View File

@ -27,8 +27,16 @@ func getEnvironmentVars() []string {
return res return res
} }
type LocalAppRunningArgs struct {
CliArgs []string
EnvironmentVars *[]string
Inputs map[string]string
TaskParams interface{}
Callback func(*os.Process)
}
type LocalApp interface { type LocalApp interface {
SetLogger(logger task_logger.Logger) task_logger.Logger SetLogger(logger task_logger.Logger) task_logger.Logger
InstallRequirements(environmentVars *[]string) error 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 ( import (
"fmt" "fmt"
"os"
"os/exec" "os/exec"
"strings" "strings"
"time" "time"
@ -107,8 +106,8 @@ func (t *ShellApp) makeShellCmd(args []string, environmentVars *[]string) *exec.
return t.makeCmd(command, append(appArgs, args...), environmentVars) 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 { func (t *ShellApp) Run(args LocalAppRunningArgs) error {
cmd := t.makeShellCmd(args, environmentVars) cmd := t.makeShellCmd(args.CliArgs, args.EnvironmentVars)
t.Logger.LogCmd(cmd) t.Logger.LogCmd(cmd)
//cmd.Stdin = &t.reader //cmd.Stdin = &t.reader
cmd.Stdin = strings.NewReader("") cmd.Stdin = strings.NewReader("")
@ -116,6 +115,6 @@ func (t *ShellApp) Run(args []string, environmentVars *[]string, inputs map[stri
if err != nil { if err != nil {
return err return err
} }
cb(cmd.Process) args.Callback(cmd.Process)
return cmd.Wait() return cmd.Wait()
} }

View File

@ -148,8 +148,8 @@ func (t *TerraformApp) Apply(args []string, environmentVars *[]string, inputs ma
return cmd.Wait() return cmd.Wait()
} }
func (t *TerraformApp) Run(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error { func (t *TerraformApp) Run(args LocalAppRunningArgs) error {
err := t.Plan(args, environmentVars, inputs, cb) err := t.Plan(args.CliArgs, args.EnvironmentVars, args.Inputs, args.Callback)
if err != nil { if err != nil {
return err return err
} }
@ -173,7 +173,7 @@ func (t *TerraformApp) Run(args []string, environmentVars *[]string, inputs map[
return nil return nil
case terraformReaderConfirmed: case terraformReaderConfirmed:
t.Logger.SetStatus(task_logger.TaskRunningStatus) 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: default:
return fmt.Errorf("unknown plan result") 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") args = append(args, "-vvvv")
} }
if t.Task.Diff { if params.Diff {
args = append(args, "--diff") args = append(args, "--diff")
} }
if t.Task.DryRun { if params.DryRun {
args = append(args, "--check") args = append(args, "--check")
} }
@ -419,20 +426,30 @@ func (t *LocalJob) Run(username string, incomingVersion *string) (err error) {
var args []string var args []string
var inputs map[string]string var inputs map[string]string
var params interface{}
switch t.Template.App { switch t.Template.App {
case db.AppAnsible: case db.AppAnsible:
args, inputs, err = t.getPlaybookArgs(username, incomingVersion) args, inputs, err = t.getPlaybookArgs(username, incomingVersion)
params = db.AnsibleTaskParams{}
case db.AppTerraform, db.AppTofu: case db.AppTerraform, db.AppTofu:
args, err = t.getTerraformArgs(username, incomingVersion) args, err = t.getTerraformArgs(username, incomingVersion)
params = db.TerraformTaskParams{}
default: default:
args, err = t.getShellArgs(username, incomingVersion) args, err = t.getShellArgs(username, incomingVersion)
params = db.ShellTaskParams{}
} }
if err != nil { if err != nil {
return return
} }
err = t.Task.GetParams(params)
if err != nil {
return
}
if t.Inventory.SSHKey.Type == db.AccessKeySSH && t.Inventory.SSHKeyID != nil { if t.Inventory.SSHKey.Type == db.AccessKeySSH && t.Inventory.SSHKeyID != nil {
environmentVariables = append(environmentVariables, fmt.Sprintf("SSH_AUTH_SOCK=%s", t.sshKeyInstallation.SSHAgent.SocketFile)) 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{
t.Process = p CliArgs: args,
EnvironmentVars: &environmentVariables,
Inputs: inputs,
TaskParams: params,
Callback: func(p *os.Process) {
t.Process = p
},
}) })
} }