2017-02-23 06:12:16 +01:00
|
|
|
package db
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2024-03-09 20:23:38 +01:00
|
|
|
type InventoryType string
|
|
|
|
|
2021-09-12 00:18:26 +02:00
|
|
|
const (
|
2024-03-25 01:24:04 +01:00
|
|
|
//InventoryNone InventoryType = "none"
|
2024-03-09 20:23:38 +01:00
|
|
|
InventoryStatic InventoryType = "static"
|
|
|
|
InventoryStaticYaml InventoryType = "static-yaml"
|
|
|
|
// InventoryFile means that it is path to the Ansible inventory file
|
2024-06-12 21:53:00 +02:00
|
|
|
InventoryFile InventoryType = "file"
|
|
|
|
InventoryTerraformWorkspace InventoryType = "terraform-workspace"
|
|
|
|
InventoryTofuWorkspace InventoryType = "tofu-workspace"
|
2021-09-12 00:18:26 +02:00
|
|
|
)
|
2022-02-03 08:05:13 +01:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// Inventory is the model of an ansible inventory file
|
2016-04-04 15:44:34 +02:00
|
|
|
type Inventory struct {
|
2024-10-05 22:16:25 +02:00
|
|
|
ID int `db:"id" json:"id" backup:"-"`
|
2016-04-11 12:02:10 +02:00
|
|
|
Name string `db:"name" json:"name" binding:"required"`
|
2024-10-05 22:16:25 +02:00
|
|
|
ProjectID int `db:"project_id" json:"project_id" backup:"-"`
|
2016-04-04 15:44:34 +02:00
|
|
|
Inventory string `db:"inventory" json:"inventory"`
|
|
|
|
|
2016-04-08 21:41:20 +02:00
|
|
|
// accesses hosts in inventory
|
2024-10-05 22:16:25 +02:00
|
|
|
SSHKeyID *int `db:"ssh_key_id" json:"ssh_key_id" backup:"-"`
|
|
|
|
SSHKey AccessKey `db:"-" json:"-" backup:"-"`
|
2016-04-08 21:41:20 +02:00
|
|
|
|
2024-10-05 22:16:25 +02:00
|
|
|
BecomeKeyID *int `db:"become_key_id" json:"become_key_id" backup:"-"`
|
|
|
|
BecomeKey AccessKey `db:"-" json:"-" backup:"-"`
|
2021-09-01 21:17:28 +02:00
|
|
|
|
2021-08-30 16:24:20 +02:00
|
|
|
// static/file
|
2024-03-09 20:23:38 +01:00
|
|
|
Type InventoryType `db:"type" json:"type"`
|
2024-03-10 13:13:44 +01:00
|
|
|
|
2024-03-18 15:33:40 +01:00
|
|
|
// HolderID is an ID of template which holds the inventory
|
2024-05-22 19:00:28 +02:00
|
|
|
// It is not used now but can be used in feature for
|
|
|
|
// inventories which can not be used more than one template
|
|
|
|
// at once.
|
2024-10-05 22:16:25 +02:00
|
|
|
HolderID *int `db:"holder_id" json:"holder_id" backup:"-"`
|
2024-05-22 19:00:28 +02:00
|
|
|
|
|
|
|
// RepositoryID is an ID of repo where inventory stored.
|
|
|
|
// If null than inventory will be got from template repository.
|
2024-10-05 22:16:25 +02:00
|
|
|
RepositoryID *int `db:"repository_id" json:"repository_id" backup:"-"`
|
|
|
|
Repository *Repository `db:"-" json:"-" backup:"-"`
|
2024-05-22 19:00:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e Inventory) GetFilename() string {
|
|
|
|
if e.Type != InventoryFile {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.Inventory
|
|
|
|
|
|
|
|
//return strings.TrimPrefix(e.Inventory, "/")
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
2021-10-12 12:25:43 +02:00
|
|
|
|
|
|
|
func FillInventory(d Store, inventory *Inventory) (err error) {
|
|
|
|
if inventory.SSHKeyID != nil {
|
|
|
|
inventory.SSHKey, err = d.GetAccessKey(inventory.ProjectID, *inventory.SSHKeyID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if inventory.BecomeKeyID != nil {
|
|
|
|
inventory.BecomeKey, err = d.GetAccessKey(inventory.ProjectID, *inventory.BecomeKeyID)
|
|
|
|
}
|
|
|
|
|
2024-05-22 19:00:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if inventory.RepositoryID != nil {
|
|
|
|
var repo Repository
|
|
|
|
repo, err = d.GetRepository(inventory.ProjectID, *inventory.RepositoryID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-06-21 20:46:35 +02:00
|
|
|
|
|
|
|
err = repo.SSHKey.DeserializeSecret()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-22 19:00:28 +02:00
|
|
|
inventory.Repository = &repo
|
|
|
|
}
|
|
|
|
|
2021-10-12 12:25:43 +02:00
|
|
|
return
|
|
|
|
}
|