2022-01-30 14:04:09 +01:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AnsiblePlaybook struct {
|
2022-02-06 16:35:58 +01:00
|
|
|
TemplateID int
|
|
|
|
Repository db.Repository
|
|
|
|
Logger Logger
|
2022-01-30 14:04:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p AnsiblePlaybook) makeCmd(command string, args []string) *exec.Cmd {
|
|
|
|
cmd := exec.Command(command, args...) //nolint: gas
|
|
|
|
cmd.Dir = p.GetFullPath()
|
2022-02-05 18:34:34 +01:00
|
|
|
|
|
|
|
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-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 {
|
|
|
|
cmd := p.makeCmd(command, args)
|
|
|
|
p.Logger.LogCmd(cmd)
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
2022-02-06 16:35:58 +01:00
|
|
|
func (p AnsiblePlaybook) RunPlaybook(args []string, cb func(*os.Process)) error {
|
|
|
|
cmd := p.makeCmd("ansible-playbook", args)
|
2022-01-30 14:04:09 +01:00
|
|
|
p.Logger.LogCmd(cmd)
|
|
|
|
cmd.Stdin = strings.NewReader("")
|
2022-02-06 16:35:58 +01:00
|
|
|
err := cmd.Start()
|
2022-01-30 14:04:09 +01:00
|
|
|
if err != nil {
|
2022-02-06 16:35:58 +01:00
|
|
|
return err
|
2022-01-30 14:04:09 +01:00
|
|
|
}
|
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
|
|
|
|
}
|