Semaphore/db_lib/AnsiblePlaybook.go

76 lines
1.8 KiB
Go
Raw Normal View History

2023-09-23 17:12:35 +02:00
package db_lib
import (
"fmt"
"github.com/ansible-semaphore/semaphore/db"
2023-09-23 17:12:35 +02:00
"github.com/ansible-semaphore/semaphore/lib"
"github.com/ansible-semaphore/semaphore/util"
"os"
"os/exec"
"strings"
)
type AnsiblePlaybook struct {
TemplateID int
Repository db.Repository
2023-09-23 17:12:35 +02:00
Logger lib.Logger
}
2022-06-14 12:58:52 +02:00
func (p AnsiblePlaybook) makeCmd(command string, args []string, environmentVars *[]string) *exec.Cmd {
cmd := exec.Command(command, args...) //nolint: gas
cmd.Dir = p.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))
2022-03-13 16:11:29 +01:00
cmd.Env = append(cmd.Env, "PYTHONUNBUFFERED=1")
cmd.Env = append(cmd.Env, "ANSIBLE_FORCE_COLOR=True")
2022-06-14 12:58:52 +02:00
if environmentVars != nil {
cmd.Env = append(cmd.Env, *environmentVars...)
}
2023-01-27 19:54:46 +01:00
sensitiveEnvs := []string{
"SEMAPHORE_ACCESS_KEY_ENCRYPTION",
"SEMAPHORE_ADMIN_PASSWORD",
"SEMAPHORE_DB_USER",
"SEMAPHORE_DB_NAME",
"SEMAPHORE_DB_HOST",
"SEMAPHORE_DB_PASS",
"SEMAPHORE_LDAP_PASSWORD",
}
// Remove sensitive env variables from cmd process
for _, env := range sensitiveEnvs {
cmd.Env = append(cmd.Env, env+"=")
}
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)
p.Logger.LogCmd(cmd)
return cmd.Run()
}
2022-06-14 12:58:52 +02:00
func (p AnsiblePlaybook) RunPlaybook(args []string, environmentVars *[]string, cb func(*os.Process)) error {
cmd := p.makeCmd("ansible-playbook", args, environmentVars)
p.Logger.LogCmd(cmd)
cmd.Stdin = strings.NewReader("")
err := cmd.Start()
if err != nil {
return err
}
cb(cmd.Process)
return cmd.Wait()
}
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
}