Semaphore/db_lib/ShellApp.go

120 lines
2.6 KiB
Go
Raw Normal View History

package db_lib
import (
"fmt"
"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/pkg/task_logger"
"github.com/ansible-semaphore/semaphore/util"
"os"
"os/exec"
"strings"
"time"
)
2024-07-05 18:46:47 +02:00
type ShellApp struct {
Logger task_logger.Logger
Template db.Template
Repository db.Repository
2024-07-05 18:46:47 +02:00
App db.TemplateApp
reader bashReader
}
type bashReader struct {
input *string
logger task_logger.Logger
}
func (r *bashReader) Read(p []byte) (n int, err error) {
r.logger.SetStatus(task_logger.TaskWaitingConfirmation)
for {
time.Sleep(time.Second * 3)
if r.input != nil {
break
}
}
copy(p, *r.input+"\n")
r.logger.SetStatus(task_logger.TaskRunningStatus)
return len(*r.input) + 1, nil
}
2024-07-05 18:46:47 +02:00
func (t *ShellApp) makeCmd(command string, args []string, environmentVars *[]string) *exec.Cmd {
cmd := exec.Command(command, args...) //nolint: gas
cmd.Dir = t.GetFullPath()
cmd.Env = removeSensitiveEnvs(os.Environ())
cmd.Env = append(cmd.Env, fmt.Sprintf("HOME=%s", util.Config.TmpPath))
cmd.Env = append(cmd.Env, fmt.Sprintf("PWD=%s", cmd.Dir))
if environmentVars != nil {
cmd.Env = append(cmd.Env, *environmentVars...)
}
return cmd
}
2024-07-05 18:46:47 +02:00
func (t *ShellApp) runCmd(command string, args []string) error {
cmd := t.makeCmd(command, args, nil)
t.Logger.LogCmd(cmd)
return cmd.Run()
}
2024-07-05 18:46:47 +02:00
func (t *ShellApp) GetFullPath() (path string) {
path = t.Repository.GetFullPath(t.Template.ID)
return
}
2024-07-05 18:46:47 +02:00
func (t *ShellApp) SetLogger(logger task_logger.Logger) task_logger.Logger {
t.Logger = logger
t.Logger.AddStatusListener(func(status task_logger.TaskStatus) {
})
t.reader.logger = logger
return logger
}
2024-07-05 18:46:47 +02:00
func (t *ShellApp) InstallRequirements() error {
return nil
}
2024-07-06 11:00:56 +02:00
func (t *ShellApp) makeShellCmd(args []string, environmentVars *[]string) *exec.Cmd {
var command string
var appArgs []string
switch t.App {
case db.TemplateBash:
command = "bash"
case db.TemplatePython:
2024-07-10 20:07:09 +02:00
command = "python3"
2024-07-06 11:00:56 +02:00
case db.TemplatePowerShell:
command = "powershell"
appArgs = []string{"-File"}
2024-07-10 10:12:01 +02:00
default:
}
if app, ok := util.Config.Apps[string(t.App)]; ok && app.AppPath != "" {
command = app.AppPath
if app.AppArgs != nil {
appArgs = app.AppArgs
2024-07-10 10:12:01 +02:00
}
} else {
command = string(t.App)
2024-07-06 11:00:56 +02:00
}
2024-07-06 11:00:56 +02:00
return t.makeCmd(command, append(appArgs, args...), environmentVars)
}
2024-07-05 18:46:47 +02:00
func (t *ShellApp) Run(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error {
2024-07-06 11:00:56 +02:00
cmd := t.makeShellCmd(args, environmentVars)
t.Logger.LogCmd(cmd)
//cmd.Stdin = &t.reader
cmd.Stdin = strings.NewReader("")
err := cmd.Start()
if err != nil {
return err
}
cb(cmd.Process)
return cmd.Wait()
}