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"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TerraformAppName string
|
|
|
|
|
|
|
|
const (
|
|
|
|
TerraformAppTerraform TerraformAppName = "terraform"
|
|
|
|
TerraformAppTofu TerraformAppName = "tofu"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TerraformApp struct {
|
|
|
|
Logger task_logger.Logger
|
|
|
|
Template db.Template
|
|
|
|
Repository db.Repository
|
|
|
|
reader terraformReader
|
|
|
|
Name TerraformAppName
|
2024-06-17 20:37:45 +02:00
|
|
|
noChanges bool
|
2024-06-12 21:53:00 +02:00
|
|
|
}
|
|
|
|
|
2024-06-17 20:37:45 +02:00
|
|
|
type terraformReaderResult int
|
2024-06-12 21:53:00 +02:00
|
|
|
|
2024-06-17 20:37:45 +02:00
|
|
|
const (
|
|
|
|
terraformReaderConfirmed terraformReaderResult = iota
|
|
|
|
terraformReaderFailed
|
|
|
|
)
|
2024-06-12 21:53:00 +02:00
|
|
|
|
|
|
|
type terraformReader struct {
|
2024-06-17 20:37:45 +02:00
|
|
|
result *terraformReaderResult
|
2024-06-12 21:53:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TerraformApp) 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, *environmentVars...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove sensitive env variables from cmd process
|
2024-07-10 13:23:34 +02:00
|
|
|
cmd.Env = removeSensitiveEnvs(cmd.Env)
|
2024-06-12 21:53:00 +02:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TerraformApp) runCmd(command string, args []string) error {
|
|
|
|
cmd := t.makeCmd(command, args, nil)
|
|
|
|
t.Logger.LogCmd(cmd)
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TerraformApp) GetFullPath() string {
|
|
|
|
return path.Join(t.Repository.GetFullPath(t.Template.ID), strings.TrimPrefix(t.Template.Playbook, "/"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TerraformApp) SetLogger(logger task_logger.Logger) task_logger.Logger {
|
2024-06-17 20:37:45 +02:00
|
|
|
t.Logger = logger
|
2024-06-12 21:53:00 +02:00
|
|
|
|
2024-06-17 20:37:45 +02:00
|
|
|
t.Logger.AddLogListener(func(new time.Time, msg string) {
|
|
|
|
if strings.Contains(msg, "No changes.") {
|
|
|
|
t.noChanges = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Logger.AddStatusListener(func(status task_logger.TaskStatus) {
|
|
|
|
var result terraformReaderResult
|
|
|
|
|
|
|
|
switch status {
|
|
|
|
case task_logger.TaskConfirmed:
|
|
|
|
result = terraformReaderConfirmed
|
|
|
|
t.reader.result = &result
|
|
|
|
case task_logger.TaskFailStatus, task_logger.TaskStoppedStatus:
|
|
|
|
result = terraformReaderFailed
|
|
|
|
t.reader.result = &result
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return logger
|
2024-06-12 21:53:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TerraformApp) InstallRequirements() error {
|
|
|
|
|
2024-06-17 20:37:45 +02:00
|
|
|
cmd := t.makeCmd(string(t.Name), []string{"init"}, nil)
|
|
|
|
t.Logger.LogCmd(cmd)
|
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-06-12 21:53:00 +02:00
|
|
|
}
|
2024-06-17 20:37:45 +02:00
|
|
|
return cmd.Wait()
|
|
|
|
}
|
2024-06-12 21:53:00 +02:00
|
|
|
|
2024-06-17 20:37:45 +02:00
|
|
|
func (t *TerraformApp) Plan(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error {
|
|
|
|
args = append([]string{"plan"}, args...)
|
|
|
|
cmd := t.makeCmd(string(t.Name), args, environmentVars)
|
2024-06-12 21:53:00 +02:00
|
|
|
t.Logger.LogCmd(cmd)
|
2024-06-17 20:37:45 +02:00
|
|
|
cmd.Stdin = strings.NewReader("")
|
2024-06-12 21:53:00 +02:00
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-17 20:37:45 +02:00
|
|
|
cb(cmd.Process)
|
2024-06-12 21:53:00 +02:00
|
|
|
return cmd.Wait()
|
|
|
|
}
|
|
|
|
|
2024-06-17 20:37:45 +02:00
|
|
|
func (t *TerraformApp) Apply(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error {
|
|
|
|
args = append([]string{"apply", "-auto-approve"}, args...)
|
2024-06-12 21:53:00 +02:00
|
|
|
cmd := t.makeCmd(string(t.Name), args, environmentVars)
|
|
|
|
t.Logger.LogCmd(cmd)
|
2024-06-17 20:37:45 +02:00
|
|
|
cmd.Stdin = strings.NewReader("")
|
2024-06-12 21:53:00 +02:00
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cb(cmd.Process)
|
|
|
|
return cmd.Wait()
|
|
|
|
}
|
2024-06-17 20:37:45 +02:00
|
|
|
|
|
|
|
func (t *TerraformApp) Run(args []string, environmentVars *[]string, inputs map[string]string, cb func(*os.Process)) error {
|
|
|
|
err := t.Plan(args, environmentVars, inputs, cb)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.noChanges {
|
|
|
|
t.Logger.SetStatus(task_logger.TaskSuccessStatus)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logger.SetStatus(task_logger.TaskWaitingConfirmation)
|
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
if t.reader.result != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch *t.reader.result {
|
|
|
|
case terraformReaderFailed:
|
|
|
|
return nil
|
|
|
|
case terraformReaderConfirmed:
|
|
|
|
t.Logger.SetStatus(task_logger.TaskRunningStatus)
|
|
|
|
return t.Apply(args, environmentVars, inputs, cb)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown plan result")
|
|
|
|
}
|
|
|
|
}
|