Merge pull request #2037 from semaphoreui/clear_inventory

feat(be): remove old inventory files
This commit is contained in:
Denis Gukov 2024-05-22 17:00:42 +02:00 committed by GitHub
commit 5948f4596c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 9 deletions

View File

@ -2,6 +2,7 @@ package tasks
import (
"github.com/ansible-semaphore/semaphore/db"
log "github.com/sirupsen/logrus"
"os"
"strconv"
@ -30,17 +31,28 @@ func (t *LocalJob) installInventory() (err error) {
return
}
func (t *LocalJob) installStaticInventory() error {
t.Log("installing static inventory")
func (t *LocalJob) tmpInventoryFilename() string {
path := util.Config.TmpPath + "/inventory_" + strconv.Itoa(t.Task.ID)
if t.Inventory.Type == db.InventoryStaticYaml {
path += ".yml"
}
return path
}
func (t *LocalJob) installStaticInventory() error {
t.Log("installing static inventory")
path := t.tmpInventoryFilename()
// create inventory file
return os.WriteFile(path, []byte(t.Inventory.Inventory), 0664)
}
func (t *LocalJob) destroyInventoryFile() {
path := t.tmpInventoryFilename()
if err := os.Remove(path); err != nil {
log.Error(err)
}
}
func (t *LocalJob) destroyKeys() {
err := t.sshKeyInstallation.Destroy()

View File

@ -3,9 +3,9 @@ package tasks
import (
"encoding/json"
"fmt"
"maps"
"os"
"strconv"
"maps"
"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/db_lib"
@ -204,10 +204,7 @@ func (t *LocalJob) getPlaybookArgs(username string, incomingVersion *string) (ar
case db.InventoryFile:
inventory = t.Inventory.Inventory
case db.InventoryStatic, db.InventoryStaticYaml:
inventory = util.Config.TmpPath + "/inventory_" + strconv.Itoa(t.Task.ID)
if t.Inventory.Type == db.InventoryStaticYaml {
inventory += ".yml"
}
inventory = t.tmpInventoryFilename()
default:
err = fmt.Errorf("invalid inventory type")
return
@ -333,6 +330,7 @@ func (t *LocalJob) Run(username string, incomingVersion *string) (err error) {
defer func() {
t.destroyKeys()
t.destroyInventoryFile()
}()
var args []string