2024-06-12 21:53:00 +02:00
|
|
|
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"
|
2024-06-17 20:37:45 +02:00
|
|
|
"time"
|
2024-06-12 21:53:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type BashApp struct {
|
2024-06-17 20:37:45 +02:00
|
|
|
Logger task_logger.Logger
|
2024-06-12 21:53:00 +02:00
|
|
|
Template db.Template
|
|
|
|
Repository db.Repository
|
2024-06-17 20:37:45 +02:00
|
|
|
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-06-12 21:53:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *BashApp) makeCmd(command string, args []string, environmentVars *[]string) *exec.Cmd {
|
|
|
|
cmd := exec.Command(command, args...) //nolint: gas
|
|
|
|
cmd.Dir = t.GetFullPath()
|
|
|
|
|
|
|
|
cmd.Env = 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, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if environmentVars != nil {
|
|
|
|
cmd.Env = append(cmd.Env, *environmentVars...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove sensitive env variables from cmd process
|
|
|
|
for _, env := range getSensitiveEnvs() {
|
|
|
|
cmd.Env = append(cmd.Env, env+"=")
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *BashApp) runCmd(command string, args []string) error {
|
|
|
|
cmd := t.makeCmd(command, args, nil)
|
|
|
|
t.Logger.LogCmd(cmd)
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *BashApp) GetFullPath() (path string) {
|
|
|
|
path = t.Repository.GetFullPath(t.Template.ID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *BashApp) SetLogger(logger task_logger.Logger) task_logger.Logger {
|
|
|
|
t.Logger = logger
|
2024-06-17 20:37:45 +02:00
|
|
|
t.Logger.AddStatusListener(func(status task_logger.TaskStatus) {
|
|
|
|
|
|
|
|
})
|
|
|
|
t.reader.logger = logger
|
2024-06-12 21:53:00 +02:00
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *BashApp) InstallRequirements() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *BashApp) Run(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error {
|
|
|
|
cmd := t.makeCmd("bash", args, environmentVars)
|
|
|
|
t.Logger.LogCmd(cmd)
|
2024-06-17 20:37:45 +02:00
|
|
|
//cmd.Stdin = &t.reader
|
2024-06-12 21:53:00 +02:00
|
|
|
cmd.Stdin = strings.NewReader("")
|
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cb(cmd.Process)
|
|
|
|
return cmd.Wait()
|
|
|
|
}
|