2023-09-23 17:12:35 +02:00
|
|
|
package db_lib
|
2022-01-30 14:04:09 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2024-04-12 12:32:54 +02:00
|
|
|
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"github.com/ansible-semaphore/semaphore/pkg/task_logger"
|
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
|
|
|
"github.com/creack/pty"
|
2022-01-30 14:04:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type AnsiblePlaybook struct {
|
2022-02-06 16:35:58 +01:00
|
|
|
TemplateID int
|
|
|
|
Repository db.Repository
|
2024-04-12 12:32:54 +02:00
|
|
|
Logger task_logger.Logger
|
2022-01-30 14:04:09 +01:00
|
|
|
}
|
|
|
|
|
2022-06-14 12:58:52 +02:00
|
|
|
func (p AnsiblePlaybook) makeCmd(command string, args []string, environmentVars *[]string) *exec.Cmd {
|
2022-01-30 14:04:09 +01:00
|
|
|
cmd := exec.Command(command, args...) //nolint: gas
|
|
|
|
cmd.Dir = p.GetFullPath()
|
2022-02-05 18:34:34 +01:00
|
|
|
|
2024-07-10 13:25:53 +02:00
|
|
|
cmd.Env = removeSensitiveEnvs(os.Environ())
|
|
|
|
|
2022-02-05 18:34:34 +01:00
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("HOME=%s", util.Config.TmpPath))
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("PWD=%s", cmd.Dir))
|
2022-03-13 16:11:29 +01:00
|
|
|
cmd.Env = append(cmd.Env, "PYTHONUNBUFFERED=1")
|
|
|
|
cmd.Env = append(cmd.Env, "ANSIBLE_FORCE_COLOR=True")
|
2024-06-19 20:10:23 +02:00
|
|
|
|
|
|
|
// TODO: Following option doesn't work when password authentication used.
|
|
|
|
// So, we need to check args for --ask-pass, --ask-become-pass or remove this code completely.
|
|
|
|
// What reason to use this code: prevent hanging of semaphore when host key confirmation required.
|
2024-06-19 09:59:42 +02:00
|
|
|
//cmd.Env = append(cmd.Env, "ANSIBLE_SSH_ARGS=\"-o BatchMode=yes\"")
|
2024-06-01 21:25:40 +02:00
|
|
|
|
2022-06-14 12:58:52 +02:00
|
|
|
if environmentVars != nil {
|
|
|
|
cmd.Env = append(cmd.Env, *environmentVars...)
|
|
|
|
}
|
2022-02-05 18:34:34 +01:00
|
|
|
|
2022-01-30 14:04:09 +01:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p AnsiblePlaybook) runCmd(command string, args []string) error {
|
2022-06-14 12:58:52 +02:00
|
|
|
cmd := p.makeCmd(command, args, nil)
|
2022-01-30 14:04:09 +01:00
|
|
|
p.Logger.LogCmd(cmd)
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
2024-04-05 14:36:04 +02:00
|
|
|
func (p AnsiblePlaybook) RunPlaybook(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error {
|
2022-06-14 12:58:52 +02:00
|
|
|
cmd := p.makeCmd("ansible-playbook", args, environmentVars)
|
2022-01-30 14:04:09 +01:00
|
|
|
p.Logger.LogCmd(cmd)
|
2024-04-05 14:36:04 +02:00
|
|
|
|
|
|
|
ptmx, err := pty.Start(cmd)
|
|
|
|
|
2022-01-30 14:04:09 +01:00
|
|
|
if err != nil {
|
2024-04-05 14:36:04 +02:00
|
|
|
panic(err)
|
2022-01-30 14:04:09 +01:00
|
|
|
}
|
2024-04-05 14:36:04 +02:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
b := make([]byte, 100)
|
|
|
|
|
|
|
|
var e error
|
|
|
|
|
|
|
|
for {
|
|
|
|
var n int
|
|
|
|
n, e = ptmx.Read(b)
|
|
|
|
if e != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
s := strings.TrimSpace(string(b[0:n]))
|
|
|
|
|
|
|
|
for k, v := range inputs {
|
|
|
|
if strings.HasPrefix(s, k) {
|
|
|
|
_, _ = ptmx.WriteString(v + "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
defer func() { _ = ptmx.Close() }()
|
2022-02-06 16:35:58 +01:00
|
|
|
cb(cmd.Process)
|
|
|
|
return cmd.Wait()
|
2022-01-30 14:04:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p AnsiblePlaybook) RunGalaxy(args []string) error {
|
|
|
|
return p.runCmd("ansible-galaxy", args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p AnsiblePlaybook) GetFullPath() (path string) {
|
|
|
|
path = p.Repository.GetFullPath(p.TemplateID)
|
|
|
|
return
|
|
|
|
}
|