2023-08-29 00:51:04 +02:00
|
|
|
package tasks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-05-21 18:47:03 +02:00
|
|
|
"maps"
|
2023-08-29 00:51:04 +02:00
|
|
|
"os"
|
2024-06-12 21:53:00 +02:00
|
|
|
|
2024-05-22 19:00:28 +02:00
|
|
|
"path"
|
2023-08-29 00:51:04 +02:00
|
|
|
"strconv"
|
2023-09-23 17:12:35 +02:00
|
|
|
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db_lib"
|
2024-04-12 12:32:54 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/pkg/task_logger"
|
2023-09-23 17:12:35 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2023-08-29 00:51:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type LocalJob struct {
|
|
|
|
// Received constant fields
|
|
|
|
Task db.Task
|
|
|
|
Template db.Template
|
|
|
|
Inventory db.Inventory
|
|
|
|
Repository db.Repository
|
|
|
|
Environment db.Environment
|
2024-05-21 18:47:03 +02:00
|
|
|
Secret string
|
2024-04-12 12:32:54 +02:00
|
|
|
Logger task_logger.Logger
|
2023-08-29 00:51:04 +02:00
|
|
|
|
2024-01-30 16:42:56 +01:00
|
|
|
App db_lib.LocalApp
|
2024-01-30 10:26:11 +01:00
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
// Internal field
|
|
|
|
Process *os.Process
|
2023-09-23 17:12:35 +02:00
|
|
|
|
|
|
|
sshKeyInstallation db.AccessKeyInstallation
|
|
|
|
becomeKeyInstallation db.AccessKeyInstallation
|
|
|
|
vaultFileInstallation db.AccessKeyInstallation
|
2023-08-29 00:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LocalJob) Kill() {
|
|
|
|
if t.Process == nil {
|
2023-09-10 23:18:25 +02:00
|
|
|
return
|
2023-08-29 00:51:04 +02:00
|
|
|
}
|
|
|
|
err := t.Process.Kill()
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LocalJob) Log(msg string) {
|
|
|
|
t.Logger.Log(msg)
|
|
|
|
}
|
|
|
|
|
2024-04-12 12:32:54 +02:00
|
|
|
func (t *LocalJob) SetStatus(status task_logger.TaskStatus) {
|
2023-09-12 19:40:22 +02:00
|
|
|
t.Logger.SetStatus(status)
|
|
|
|
}
|
|
|
|
|
2024-01-30 16:42:56 +01:00
|
|
|
func (t *LocalJob) getEnvironmentExtraVars(username string, incomingVersion *string) (extraVars map[string]interface{}, err error) {
|
|
|
|
|
|
|
|
extraVars = make(map[string]interface{})
|
|
|
|
|
|
|
|
if t.Environment.JSON != "" {
|
|
|
|
err = json.Unmarshal([]byte(t.Environment.JSON), &extraVars)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
taskDetails := make(map[string]interface{})
|
|
|
|
|
|
|
|
taskDetails["id"] = t.Task.ID
|
|
|
|
|
|
|
|
if t.Task.Message != "" {
|
|
|
|
taskDetails["message"] = t.Task.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
taskDetails["username"] = username
|
2024-04-19 15:05:35 +02:00
|
|
|
taskDetails["url"] = t.Task.GetUrl()
|
2024-01-30 16:42:56 +01:00
|
|
|
|
|
|
|
if t.Template.Type != db.TemplateTask {
|
|
|
|
taskDetails["type"] = t.Template.Type
|
|
|
|
if incomingVersion != nil {
|
|
|
|
taskDetails["incoming_version"] = incomingVersion
|
|
|
|
}
|
|
|
|
if t.Template.Type == db.TemplateBuild {
|
|
|
|
taskDetails["target_version"] = t.Task.Version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := make(map[string]interface{})
|
|
|
|
vars["task_details"] = taskDetails
|
|
|
|
extraVars["semaphore_vars"] = vars
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LocalJob) getEnvironmentExtraVarsJSON(username string, incomingVersion *string) (str string, err error) {
|
2023-08-29 00:51:04 +02:00
|
|
|
extraVars := make(map[string]interface{})
|
2024-03-19 09:49:33 +01:00
|
|
|
extraSecretVars := make(map[string]interface{})
|
2023-08-29 00:51:04 +02:00
|
|
|
|
|
|
|
if t.Environment.JSON != "" {
|
|
|
|
err = json.Unmarshal([]byte(t.Environment.JSON), &extraVars)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 09:49:33 +01:00
|
|
|
if t.Secret != "" {
|
|
|
|
err = json.Unmarshal([]byte(t.Secret), &extraSecretVars)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Secret = "{}"
|
|
|
|
|
|
|
|
maps.Copy(extraVars, extraSecretVars)
|
2023-08-29 00:51:04 +02:00
|
|
|
|
|
|
|
taskDetails := make(map[string]interface{})
|
|
|
|
|
|
|
|
taskDetails["id"] = t.Task.ID
|
|
|
|
|
|
|
|
if t.Task.Message != "" {
|
|
|
|
taskDetails["message"] = t.Task.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
taskDetails["username"] = username
|
2024-04-19 15:05:35 +02:00
|
|
|
taskDetails["url"] = t.Task.GetUrl()
|
2023-08-29 00:51:04 +02:00
|
|
|
|
|
|
|
if t.Template.Type != db.TemplateTask {
|
|
|
|
taskDetails["type"] = t.Template.Type
|
|
|
|
if incomingVersion != nil {
|
|
|
|
taskDetails["incoming_version"] = incomingVersion
|
|
|
|
}
|
|
|
|
if t.Template.Type == db.TemplateBuild {
|
|
|
|
taskDetails["target_version"] = t.Task.Version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := make(map[string]interface{})
|
|
|
|
vars["task_details"] = taskDetails
|
|
|
|
extraVars["semaphore_vars"] = vars
|
|
|
|
|
|
|
|
ev, err := json.Marshal(extraVars)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
str = string(ev)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-17 21:41:11 +02:00
|
|
|
func (t *LocalJob) getEnvironmentENV() (res []string, err error) {
|
2023-08-29 00:51:04 +02:00
|
|
|
environmentVars := make(map[string]string)
|
|
|
|
|
|
|
|
if t.Environment.ENV != nil {
|
|
|
|
err = json.Unmarshal([]byte(*t.Environment.ENV), &environmentVars)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range environmentVars {
|
2024-07-17 21:41:11 +02:00
|
|
|
res = append(res, fmt.Sprintf("%s=%s", key, val))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, secret := range t.Environment.Secrets {
|
|
|
|
if secret.Type != db.EnvironmentSecretEnv {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res = append(res, fmt.Sprintf("%s=%s", secret.Name, secret.Secret))
|
2023-08-29 00:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:53:00 +02:00
|
|
|
// nolint: gocyclo
|
2024-07-10 10:12:01 +02:00
|
|
|
func (t *LocalJob) getShellArgs(username string, incomingVersion *string) (args []string, err error) {
|
2024-08-16 18:36:56 +02:00
|
|
|
extraVars, err := t.getEnvironmentExtraVars(username, incomingVersion)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err.Error())
|
|
|
|
t.Log("Error getting environment extra vars")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var templateExtraArgs []string
|
|
|
|
if t.Template.Arguments != nil {
|
|
|
|
err = json.Unmarshal([]byte(*t.Template.Arguments), &templateExtraArgs)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Invalid format of the template extra arguments, must be valid JSON")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var taskExtraArgs []string
|
|
|
|
if t.Template.AllowOverrideArgsInTask && t.Task.Arguments != nil {
|
|
|
|
err = json.Unmarshal([]byte(*t.Task.Arguments), &taskExtraArgs)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Invalid format of the TaskRunner extra arguments, must be valid JSON")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-06-12 21:53:00 +02:00
|
|
|
|
2024-08-16 18:36:56 +02:00
|
|
|
// Script to run
|
2024-06-12 21:53:00 +02:00
|
|
|
args = append(args, t.Template.Playbook)
|
|
|
|
|
2024-08-16 18:36:56 +02:00
|
|
|
// Include Environment Secret Vars
|
|
|
|
for _, secret := range t.Environment.Secrets {
|
|
|
|
if secret.Type == db.EnvironmentSecretVar {
|
|
|
|
args = append(args, fmt.Sprintf("%s=%s", secret.Name, secret.Secret))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include extra args from template
|
|
|
|
args = append(args, templateExtraArgs...)
|
|
|
|
|
|
|
|
// Include ExtraVars and Survey Vars
|
|
|
|
for name, value := range extraVars {
|
|
|
|
if name != "semaphore_vars" {
|
|
|
|
args = append(args, fmt.Sprintf("%s=%s", name, value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include extra args from task
|
|
|
|
args = append(args, taskExtraArgs...)
|
2024-07-02 22:18:30 +02:00
|
|
|
|
2024-06-12 21:53:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-30 16:42:56 +01:00
|
|
|
// nolint: gocyclo
|
|
|
|
func (t *LocalJob) getTerraformArgs(username string, incomingVersion *string) (args []string, err error) {
|
|
|
|
|
|
|
|
args = []string{}
|
|
|
|
|
|
|
|
extraVars, err := t.getEnvironmentExtraVars(username, incomingVersion)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err.Error())
|
2024-04-14 14:22:07 +02:00
|
|
|
t.Log("Could not remove command environment, if existent it will be passed to --extra-vars. This is not fatal but be aware of side effects")
|
2024-01-30 16:42:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:53:00 +02:00
|
|
|
for name, value := range extraVars {
|
|
|
|
if name == "semaphore_vars" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
args = append(args, "-var", fmt.Sprintf("%s=%s", name, value))
|
2024-01-30 16:42:56 +01:00
|
|
|
}
|
|
|
|
|
2024-07-02 21:45:43 +02:00
|
|
|
for _, secret := range t.Environment.Secrets {
|
2024-07-17 21:41:11 +02:00
|
|
|
if secret.Type != db.EnvironmentSecretVar {
|
|
|
|
continue
|
|
|
|
}
|
2024-07-02 21:45:43 +02:00
|
|
|
args = append(args, "-var", fmt.Sprintf("%s=%s", secret.Name, secret.Secret))
|
|
|
|
}
|
|
|
|
|
2024-01-30 16:42:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
// nolint: gocyclo
|
2024-04-05 14:36:04 +02:00
|
|
|
func (t *LocalJob) getPlaybookArgs(username string, incomingVersion *string) (args []string, inputs map[string]string, err error) {
|
|
|
|
|
|
|
|
inputMap := make(map[db.AccessKeyRole]string)
|
|
|
|
inputs = make(map[string]string)
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
playbookName := t.Task.Playbook
|
|
|
|
if playbookName == "" {
|
|
|
|
playbookName = t.Template.Playbook
|
|
|
|
}
|
|
|
|
|
2024-05-22 19:00:28 +02:00
|
|
|
var inventoryFilename string
|
2023-08-29 00:51:04 +02:00
|
|
|
switch t.Inventory.Type {
|
|
|
|
case db.InventoryFile:
|
2024-05-22 19:00:28 +02:00
|
|
|
if t.Inventory.RepositoryID == nil {
|
|
|
|
inventoryFilename = t.Inventory.GetFilename()
|
|
|
|
} else {
|
|
|
|
inventoryFilename = path.Join(t.tmpInventoryFullPath(), t.Inventory.GetFilename())
|
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
case db.InventoryStatic, db.InventoryStaticYaml:
|
2024-05-22 19:00:28 +02:00
|
|
|
inventoryFilename = t.tmpInventoryFullPath()
|
2023-08-29 00:51:04 +02:00
|
|
|
default:
|
2024-04-14 14:22:07 +02:00
|
|
|
err = fmt.Errorf("invalid inventory type")
|
2023-08-29 00:51:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
args = []string{
|
2024-05-22 19:00:28 +02:00
|
|
|
"-i", inventoryFilename,
|
2023-08-29 00:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if t.Inventory.SSHKeyID != nil {
|
|
|
|
switch t.Inventory.SSHKey.Type {
|
|
|
|
case db.AccessKeySSH:
|
2024-04-05 14:36:04 +02:00
|
|
|
if t.sshKeyInstallation.Login != "" {
|
|
|
|
args = append(args, "--user", t.sshKeyInstallation.Login)
|
2023-08-29 00:51:04 +02:00
|
|
|
}
|
|
|
|
case db.AccessKeyLoginPassword:
|
2024-04-05 14:36:04 +02:00
|
|
|
if t.sshKeyInstallation.Login != "" {
|
|
|
|
args = append(args, "--user", t.sshKeyInstallation.Login)
|
|
|
|
}
|
|
|
|
if t.sshKeyInstallation.Password != "" {
|
|
|
|
args = append(args, "--ask-pass")
|
|
|
|
inputMap[db.AccessKeyRoleAnsibleUser] = t.sshKeyInstallation.Password
|
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
case db.AccessKeyNone:
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("access key does not suite for inventory's user credentials")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Inventory.BecomeKeyID != nil {
|
|
|
|
switch t.Inventory.BecomeKey.Type {
|
|
|
|
case db.AccessKeyLoginPassword:
|
2024-04-05 17:25:33 +02:00
|
|
|
if t.becomeKeyInstallation.Login != "" {
|
2024-04-08 15:51:53 +02:00
|
|
|
args = append(args, "--become-user", t.becomeKeyInstallation.Login)
|
2024-04-05 14:36:04 +02:00
|
|
|
}
|
|
|
|
if t.becomeKeyInstallation.Password != "" {
|
|
|
|
args = append(args, "--ask-become-pass")
|
2024-04-05 17:25:33 +02:00
|
|
|
inputMap[db.AccessKeyRoleAnsibleBecomeUser] = t.becomeKeyInstallation.Password
|
2024-04-05 14:36:04 +02:00
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
case db.AccessKeyNone:
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("access key does not suite for inventory's sudo user credentials")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Task.Debug {
|
|
|
|
args = append(args, "-vvvv")
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Task.Diff {
|
|
|
|
args = append(args, "--diff")
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Task.DryRun {
|
|
|
|
args = append(args, "--check")
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Template.VaultKeyID != nil {
|
2024-04-05 14:36:04 +02:00
|
|
|
args = append(args, "--ask-vault-pass")
|
|
|
|
inputMap[db.AccessKeyRoleAnsiblePasswordVault] = t.vaultFileInstallation.Password
|
2023-08-29 00:51:04 +02:00
|
|
|
}
|
|
|
|
|
2024-01-30 16:42:56 +01:00
|
|
|
extraVars, err := t.getEnvironmentExtraVarsJSON(username, incomingVersion)
|
2023-08-29 00:51:04 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err.Error())
|
2024-04-14 14:22:07 +02:00
|
|
|
t.Log("Could not remove command environment, if existent it will be passed to --extra-vars. This is not fatal but be aware of side effects")
|
2023-08-29 00:51:04 +02:00
|
|
|
} else if extraVars != "" {
|
|
|
|
args = append(args, "--extra-vars", extraVars)
|
|
|
|
}
|
|
|
|
|
2024-07-02 22:18:30 +02:00
|
|
|
for _, secret := range t.Environment.Secrets {
|
2024-07-17 21:41:11 +02:00
|
|
|
if secret.Type != db.EnvironmentSecretVar {
|
|
|
|
continue
|
|
|
|
}
|
2024-07-02 22:18:30 +02:00
|
|
|
args = append(args, "--extra-vars", fmt.Sprintf("%s=%s", secret.Name, secret.Secret))
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
var templateExtraArgs []string
|
|
|
|
if t.Template.Arguments != nil {
|
|
|
|
err = json.Unmarshal([]byte(*t.Template.Arguments), &templateExtraArgs)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Invalid format of the template extra arguments, must be valid JSON")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var taskExtraArgs []string
|
|
|
|
if t.Template.AllowOverrideArgsInTask && t.Task.Arguments != nil {
|
|
|
|
err = json.Unmarshal([]byte(*t.Task.Arguments), &taskExtraArgs)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Invalid format of the TaskRunner extra arguments, must be valid JSON")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Task.Limit != "" {
|
|
|
|
t.Log("--limit=" + t.Task.Limit)
|
|
|
|
taskExtraArgs = append(taskExtraArgs, "--limit="+t.Task.Limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, templateExtraArgs...)
|
|
|
|
args = append(args, taskExtraArgs...)
|
|
|
|
args = append(args, playbookName)
|
|
|
|
|
2024-04-05 14:36:04 +02:00
|
|
|
if line, ok := inputMap[db.AccessKeyRoleAnsibleUser]; ok {
|
|
|
|
inputs["SSH password:"] = line
|
|
|
|
}
|
|
|
|
|
|
|
|
if line, ok := inputMap[db.AccessKeyRoleAnsibleBecomeUser]; ok {
|
|
|
|
inputs["BECOME password"] = line
|
|
|
|
}
|
|
|
|
|
|
|
|
if line, ok := inputMap[db.AccessKeyRoleAnsiblePasswordVault]; ok {
|
|
|
|
inputs["Vault password:"] = line
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LocalJob) Run(username string, incomingVersion *string) (err error) {
|
2023-09-12 20:58:44 +02:00
|
|
|
|
2024-04-12 12:32:54 +02:00
|
|
|
t.SetStatus(task_logger.TaskRunningStatus) // It is required for local mode. Don't delete
|
2024-03-12 02:06:21 +01:00
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
err = t.prepareRun()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
t.destroyKeys()
|
2024-05-21 18:47:03 +02:00
|
|
|
t.destroyInventoryFile()
|
2023-08-29 00:51:04 +02:00
|
|
|
}()
|
|
|
|
|
2024-01-30 16:42:56 +01:00
|
|
|
var args []string
|
2024-04-05 14:36:04 +02:00
|
|
|
var inputs map[string]string
|
2024-01-30 16:42:56 +01:00
|
|
|
|
|
|
|
switch t.Template.App {
|
2024-07-16 08:35:36 +02:00
|
|
|
case db.AppAnsible:
|
2024-04-05 14:36:04 +02:00
|
|
|
args, inputs, err = t.getPlaybookArgs(username, incomingVersion)
|
2024-07-16 08:35:36 +02:00
|
|
|
case db.AppTerraform, db.AppTofu:
|
2024-06-12 21:53:00 +02:00
|
|
|
args, err = t.getTerraformArgs(username, incomingVersion)
|
2024-01-30 16:42:56 +01:00
|
|
|
default:
|
2024-07-10 10:12:01 +02:00
|
|
|
args, err = t.getShellArgs(username, incomingVersion)
|
2024-01-30 16:42:56 +01:00
|
|
|
}
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
environmentVariables, err := t.getEnvironmentENV()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-23 17:12:35 +02:00
|
|
|
if t.Inventory.SSHKey.Type == db.AccessKeySSH && t.Inventory.SSHKeyID != nil {
|
2024-04-12 12:21:05 +02:00
|
|
|
environmentVariables = append(environmentVariables, fmt.Sprintf("SSH_AUTH_SOCK=%s", t.sshKeyInstallation.SSHAgent.SocketFile))
|
2023-09-19 21:31:41 +02:00
|
|
|
}
|
|
|
|
|
2024-09-24 17:58:51 +02:00
|
|
|
if t.Template.Type != db.TemplateTask {
|
|
|
|
|
|
|
|
environmentVariables = append(environmentVariables, fmt.Sprintf("SEMAPHORE_TASK_TYPE=%s", t.Template.Type))
|
|
|
|
|
|
|
|
if incomingVersion != nil {
|
|
|
|
environmentVariables = append(
|
|
|
|
environmentVariables,
|
|
|
|
fmt.Sprintf("SEMAPHORE_TASK_INCOMING_VERSION=%s", *incomingVersion))
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Template.Type == db.TemplateBuild && t.Task.Version != nil {
|
|
|
|
environmentVariables = append(
|
|
|
|
environmentVariables,
|
|
|
|
fmt.Sprintf("SEMAPHORE_TASK_TARGET_VERSION=%s", *t.Task.Version))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 14:36:04 +02:00
|
|
|
return t.App.Run(args, &environmentVariables, inputs, func(p *os.Process) {
|
2023-08-29 00:51:04 +02:00
|
|
|
t.Process = p
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LocalJob) prepareRun() error {
|
|
|
|
t.Log("Preparing: " + strconv.Itoa(t.Task.ID))
|
|
|
|
|
|
|
|
if err := checkTmpDir(util.Config.TmpPath); err != nil {
|
|
|
|
t.Log("Creating tmp dir failed: " + err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Repository.GetType() == db.RepositoryLocal {
|
|
|
|
if _, err := os.Stat(t.Repository.GitURL); err != nil {
|
|
|
|
t.Log("Failed in finding static repository at " + t.Repository.GitURL + ": " + err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := t.updateRepository(); err != nil {
|
|
|
|
t.Log("Failed updating repository: " + err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := t.checkoutRepository(); err != nil {
|
|
|
|
t.Log("Failed to checkout repository to required commit: " + err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.installInventory(); err != nil {
|
|
|
|
t.Log("Failed to install inventory: " + err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-30 10:26:11 +01:00
|
|
|
if err := t.App.InstallRequirements(); err != nil {
|
2023-08-29 00:51:04 +02:00
|
|
|
t.Log("Running galaxy failed: " + err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.installVaultKeyFile(); err != nil {
|
|
|
|
t.Log("Failed to install vault password file: " + err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LocalJob) updateRepository() error {
|
2023-09-23 17:12:35 +02:00
|
|
|
repo := db_lib.GitRepository{
|
2023-08-29 00:51:04 +02:00
|
|
|
Logger: t.Logger,
|
|
|
|
TemplateID: t.Template.ID,
|
|
|
|
Repository: t.Repository,
|
2023-09-23 17:12:35 +02:00
|
|
|
Client: db_lib.CreateDefaultGitClient(),
|
2023-08-29 00:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := repo.ValidateRepo()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
err = os.RemoveAll(repo.GetFullPath())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return repo.Clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
if repo.CanBePulled() {
|
|
|
|
err = repo.Pull()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(repo.GetFullPath())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo.Clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LocalJob) checkoutRepository() error {
|
|
|
|
|
2023-09-23 17:12:35 +02:00
|
|
|
repo := db_lib.GitRepository{
|
2023-08-29 00:51:04 +02:00
|
|
|
Logger: t.Logger,
|
|
|
|
TemplateID: t.Template.ID,
|
|
|
|
Repository: t.Repository,
|
2023-09-23 17:12:35 +02:00
|
|
|
Client: db_lib.CreateDefaultGitClient(),
|
2023-08-29 00:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := repo.ValidateRepo()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Task.CommitHash != nil {
|
|
|
|
// checkout to commit if it is provided for TaskRunner
|
|
|
|
return repo.Checkout(*t.Task.CommitHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// store commit to TaskRunner table
|
|
|
|
|
|
|
|
//commitHash, err := repo.GetLastCommitHash()
|
|
|
|
//
|
|
|
|
//if err != nil {
|
|
|
|
// return err
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//commitMessage, _ := repo.GetLastCommitMessage()
|
|
|
|
//
|
|
|
|
//t.task.CommitHash = &commitHash
|
|
|
|
//t.task.CommitMessage = commitMessage
|
|
|
|
//
|
|
|
|
//return t.pool.store.UpdateTask(t.task)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-23 17:12:35 +02:00
|
|
|
func (t *LocalJob) installVaultKeyFile() (err error) {
|
2023-08-29 00:51:04 +02:00
|
|
|
if t.Template.VaultKeyID == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-23 17:47:27 +02:00
|
|
|
t.vaultFileInstallation, err = t.Template.VaultKey.Install(db.AccessKeyRoleAnsiblePasswordVault, t.Logger)
|
2023-09-23 17:12:35 +02:00
|
|
|
|
|
|
|
return
|
2023-08-29 00:51:04 +02:00
|
|
|
}
|