Semaphore/db_lib/ShellApp.go

122 lines
2.6 KiB
Go
Raw Normal View History

package db_lib
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
2024-10-21 13:03:12 +02:00
"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/pkg/task_logger"
"github.com/ansible-semaphore/semaphore/util"
)
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()
2024-10-21 13:03:12 +02:00
cmd.Env = []string{}
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
}
func (t *ShellApp) InstallRequirements(environmentVars *[]string) 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.AppBash:
2024-07-06 11:00:56 +02:00
command = "bash"
case db.AppPython:
2024-07-10 20:07:09 +02:00
command = "python3"
case db.AppPowerShell:
2024-07-06 11:00:56 +02:00
command = "powershell"
appArgs = []string{"-File"}
2024-07-10 10:12:01 +02:00
default:
2024-07-22 13:51:29 +02:00
command = string(t.App)
}
2024-07-22 13:51:29 +02:00
if app, ok := util.Config.Apps[string(t.App)]; ok {
if app.AppPath != "" {
command = app.AppPath
}
if app.AppArgs != nil {
appArgs = app.AppArgs
2024-07-10 10:12:01 +02:00
}
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()
}