2024-06-12 21:53:00 +02:00
|
|
|
package db_lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2024-06-17 20:37:45 +02:00
|
|
|
"time"
|
2024-10-21 13:03:12 +02:00
|
|
|
|
2024-10-26 14:56:17 +02:00
|
|
|
"github.com/semaphoreui/semaphore/db"
|
|
|
|
"github.com/semaphoreui/semaphore/pkg/task_logger"
|
|
|
|
"github.com/semaphoreui/semaphore/util"
|
2024-06-12 21:53:00 +02:00
|
|
|
)
|
|
|
|
|
2024-07-05 18:46:47 +02:00
|
|
|
type ShellApp 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-07-05 18:46:47 +02:00
|
|
|
App db.TemplateApp
|
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
|
|
|
}
|
|
|
|
|
2024-07-05 18:46:47 +02:00
|
|
|
func (t *ShellApp) makeCmd(command string, args []string, environmentVars *[]string) *exec.Cmd {
|
2024-06-12 21:53:00 +02:00
|
|
|
cmd := exec.Command(command, args...) //nolint: gas
|
|
|
|
cmd.Dir = t.GetFullPath()
|
|
|
|
|
2024-10-22 08:35:55 +02:00
|
|
|
cmd.Env = getEnvironmentVars()
|
2024-06-12 21:53:00 +02:00
|
|
|
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 {
|
2024-06-12 21:53:00 +02:00
|
|
|
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) {
|
2024-06-12 21:53:00 +02:00
|
|
|
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 {
|
2024-06-12 21:53:00 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-10 13:36:59 +02:00
|
|
|
func (t *ShellApp) InstallRequirements(environmentVars *[]string) error {
|
2024-06-12 21:53:00 +02:00
|
|
|
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 {
|
2024-07-16 08:35:36 +02:00
|
|
|
case db.AppBash:
|
2024-07-06 11:00:56 +02:00
|
|
|
command = "bash"
|
2024-07-16 08:35:36 +02:00
|
|
|
case db.AppPython:
|
2024-07-10 20:07:09 +02:00
|
|
|
command = "python3"
|
2024-07-16 08:35:36 +02:00
|
|
|
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-10 20:06:38 +02:00
|
|
|
}
|
|
|
|
|
2024-07-22 13:51:29 +02:00
|
|
|
if app, ok := util.Config.Apps[string(t.App)]; ok {
|
|
|
|
if app.AppPath != "" {
|
|
|
|
command = app.AppPath
|
|
|
|
}
|
2024-07-10 20:06:38 +02:00
|
|
|
if app.AppArgs != nil {
|
2024-07-10 11:32:44 +02:00
|
|
|
appArgs = app.AppArgs
|
2024-07-10 10:12:01 +02:00
|
|
|
}
|
2024-07-06 11:00:56 +02:00
|
|
|
}
|
2024-07-10 20:06:38 +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)
|
2024-06-12 21:53:00 +02:00
|
|
|
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()
|
|
|
|
}
|