2016-04-08 21:41:20 +02:00
|
|
|
package tasks
|
|
|
|
|
|
|
|
import (
|
2024-03-10 20:07:19 +01:00
|
|
|
"os"
|
2024-05-22 19:00:28 +02:00
|
|
|
"path"
|
2016-04-08 21:41:20 +02:00
|
|
|
"strconv"
|
|
|
|
|
2024-07-01 10:42:40 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db_lib"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2016-04-08 21:41:20 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
|
|
|
)
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
func (t *LocalJob) installInventory() (err error) {
|
|
|
|
if t.Inventory.SSHKeyID != nil {
|
2023-09-23 17:47:27 +02:00
|
|
|
t.sshKeyInstallation, err = t.Inventory.SSHKey.Install(db.AccessKeyRoleAnsibleUser, t.Logger)
|
2016-04-08 21:41:20 +02:00
|
|
|
if err != nil {
|
2021-09-12 00:18:26 +02:00
|
|
|
return
|
2016-04-08 21:41:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
if t.Inventory.BecomeKeyID != nil {
|
2023-09-23 17:47:27 +02:00
|
|
|
t.becomeKeyInstallation, err = t.Inventory.BecomeKey.Install(db.AccessKeyRoleAnsibleBecomeUser, t.Logger)
|
2021-09-12 00:18:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 19:00:28 +02:00
|
|
|
if t.Inventory.Type == db.InventoryFile {
|
|
|
|
err = t.cloneInventoryRepo()
|
|
|
|
} else if t.Inventory.Type == db.InventoryStatic || t.Inventory.Type == db.InventoryStaticYaml {
|
2021-09-12 00:18:26 +02:00
|
|
|
err = t.installStaticInventory()
|
2016-04-08 21:41:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-12 00:18:26 +02:00
|
|
|
return
|
2016-04-08 21:41:20 +02:00
|
|
|
}
|
|
|
|
|
2024-05-21 18:47:03 +02:00
|
|
|
func (t *LocalJob) tmpInventoryFilename() string {
|
2024-05-22 19:00:28 +02:00
|
|
|
return "inventory_" + strconv.Itoa(t.Task.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LocalJob) tmpInventoryFullPath() string {
|
|
|
|
pathname := path.Join(util.Config.TmpPath, t.tmpInventoryFilename())
|
2023-08-29 00:51:04 +02:00
|
|
|
if t.Inventory.Type == db.InventoryStaticYaml {
|
2024-05-22 19:00:28 +02:00
|
|
|
pathname += ".yml"
|
2022-05-24 17:54:33 +02:00
|
|
|
}
|
2024-05-22 19:00:28 +02:00
|
|
|
return pathname
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LocalJob) cloneInventoryRepo() error {
|
|
|
|
if t.Inventory.Repository == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("cloning inventory repository")
|
|
|
|
|
|
|
|
repo := db_lib.GitRepository{
|
|
|
|
Logger: t.Logger,
|
|
|
|
TmpDirName: t.tmpInventoryFilename(),
|
|
|
|
Repository: *t.Inventory.Repository,
|
|
|
|
Client: db_lib.CreateDefaultGitClient(),
|
|
|
|
}
|
|
|
|
|
2024-07-01 10:42:40 +02:00
|
|
|
// Try to pull the repo before trying to clone it
|
|
|
|
if repo.CanBePulled() {
|
|
|
|
err := repo.Pull()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := os.RemoveAll(repo.GetFullPath())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-22 19:00:28 +02:00
|
|
|
|
2024-07-01 10:42:40 +02:00
|
|
|
return repo.Clone()
|
2024-05-21 18:47:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LocalJob) installStaticInventory() error {
|
|
|
|
t.Log("installing static inventory")
|
|
|
|
|
2024-09-26 22:44:18 +02:00
|
|
|
fullPath := t.tmpInventoryFullPath()
|
2022-05-24 17:54:33 +02:00
|
|
|
|
2016-04-08 21:41:20 +02:00
|
|
|
// create inventory file
|
2024-09-26 22:44:18 +02:00
|
|
|
return os.WriteFile(fullPath, []byte(t.Inventory.Inventory), 0664)
|
2016-04-08 21:41:20 +02:00
|
|
|
}
|
2024-05-22 19:00:28 +02:00
|
|
|
|
2024-05-21 18:47:03 +02:00
|
|
|
func (t *LocalJob) destroyInventoryFile() {
|
2024-09-26 22:44:18 +02:00
|
|
|
fullPath := t.tmpInventoryFullPath()
|
|
|
|
if err := os.Remove(fullPath); err != nil {
|
2024-05-21 18:47:03 +02:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2023-09-23 17:47:27 +02:00
|
|
|
|
|
|
|
func (t *LocalJob) destroyKeys() {
|
|
|
|
err := t.sshKeyInstallation.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Can't destroy inventory user key, error: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.becomeKeyInstallation.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Can't destroy inventory become user key, error: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2024-10-02 17:49:33 +02:00
|
|
|
for _, vault := range t.vaultFileInstallations {
|
|
|
|
err = vault.Destroy()
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Can't destroy inventory vault password file, error: " + err.Error())
|
|
|
|
}
|
2023-09-23 17:47:27 +02:00
|
|
|
}
|
|
|
|
}
|