mirror of
https://github.com/semaphoreui/semaphore.git
synced 2024-11-23 20:35:24 +01:00
refactor(backup): use source models
This commit is contained in:
parent
acd29797d4
commit
0d75e2e28d
@ -67,17 +67,16 @@ func (e BackupAccessKey) Verify(backup *BackupFormat) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e BackupAccessKey) Restore(store db.Store, b *BackupDB) error {
|
func (e BackupAccessKey) Restore(store db.Store, b *BackupDB) error {
|
||||||
key, err := store.CreateAccessKey(
|
|
||||||
db.AccessKey{
|
key := e.AccessKey
|
||||||
Name: e.Name,
|
key.ProjectID = &b.meta.ID
|
||||||
ProjectID: &b.meta.ID,
|
|
||||||
Type: e.Type,
|
newKey, err := store.CreateAccessKey(key)
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
b.keys = append(b.keys, key)
|
b.keys = append(b.keys, newKey)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,19 +141,16 @@ func (e BackupRepository) Restore(store db.Store, b *BackupDB) error {
|
|||||||
} else {
|
} else {
|
||||||
SSHKeyID = (*k).ID
|
SSHKeyID = (*k).ID
|
||||||
}
|
}
|
||||||
repository, err := store.CreateRepository(
|
|
||||||
db.Repository{
|
repo := e.Repository
|
||||||
ProjectID: b.meta.ID,
|
repo.ProjectID = b.meta.ID
|
||||||
Name: e.Name,
|
repo.SSHKeyID = SSHKeyID
|
||||||
GitBranch: e.GitBranch,
|
|
||||||
GitURL: e.GitURL,
|
newRepo, err := store.CreateRepository(repo)
|
||||||
SSHKeyID: SSHKeyID,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
b.repositories = append(b.repositories, repository)
|
b.repositories = append(b.repositories, newRepo)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,15 +158,19 @@ func (e BackupTemplate) Verify(backup *BackupFormat) error {
|
|||||||
if err := verifyDuplicate[BackupTemplate](e.Name, backup.Templates); err != nil {
|
if err := verifyDuplicate[BackupTemplate](e.Name, backup.Templates); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if getEntryByName[BackupRepository](&e.Repository, backup.Repositories) == nil {
|
if getEntryByName[BackupRepository](&e.Repository, backup.Repositories) == nil {
|
||||||
return fmt.Errorf("repository does not exist in repositories[].name")
|
return fmt.Errorf("repository does not exist in repositories[].name")
|
||||||
}
|
}
|
||||||
|
|
||||||
if getEntryByName[BackupInventory](e.Inventory, backup.Inventories) == nil {
|
if getEntryByName[BackupInventory](e.Inventory, backup.Inventories) == nil {
|
||||||
return fmt.Errorf("inventory does not exist in inventories[].name")
|
return fmt.Errorf("inventory does not exist in inventories[].name")
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.VaultKey != nil && getEntryByName[BackupAccessKey](e.VaultKey, backup.Keys) == nil {
|
if e.VaultKey != nil && getEntryByName[BackupAccessKey](e.VaultKey, backup.Keys) == nil {
|
||||||
return fmt.Errorf("vault_key does not exist in keys[].name")
|
return fmt.Errorf("vault_key does not exist in keys[].name")
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.Vaults != nil {
|
if e.Vaults != nil {
|
||||||
for _, vault := range e.Vaults {
|
for _, vault := range e.Vaults {
|
||||||
if getEntryByName[BackupAccessKey](&vault.VaultKey, backup.Keys) == nil {
|
if getEntryByName[BackupAccessKey](&vault.VaultKey, backup.Keys) == nil {
|
||||||
@ -178,15 +178,19 @@ func (e BackupTemplate) Verify(backup *BackupFormat) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.View != nil && getEntryByName[BackupView](e.View, backup.Views) == nil {
|
if e.View != nil && getEntryByName[BackupView](e.View, backup.Views) == nil {
|
||||||
return fmt.Errorf("view does not exist in views[].name")
|
return fmt.Errorf("view does not exist in views[].name")
|
||||||
}
|
}
|
||||||
|
|
||||||
if string(e.Type) == "deploy" && e.BuildTemplate == nil {
|
if string(e.Type) == "deploy" && e.BuildTemplate == nil {
|
||||||
return fmt.Errorf("type is deploy but build_template is null")
|
return fmt.Errorf("type is deploy but build_template is null")
|
||||||
}
|
}
|
||||||
|
|
||||||
if string(e.Type) != "deploy" && e.BuildTemplate != nil {
|
if string(e.Type) != "deploy" && e.BuildTemplate != nil {
|
||||||
return fmt.Errorf("type is not deploy but build_template is not null")
|
return fmt.Errorf("type is not deploy but build_template is not null")
|
||||||
}
|
}
|
||||||
|
|
||||||
if buildTemplate := getEntryByName[BackupTemplate](e.BuildTemplate, backup.Templates); string(e.Type) == "deploy" && buildTemplate == nil {
|
if buildTemplate := getEntryByName[BackupTemplate](e.BuildTemplate, backup.Templates); string(e.Type) == "deploy" && buildTemplate == nil {
|
||||||
return fmt.Errorf("deploy is build but build_template does not exist in templates[].name")
|
return fmt.Errorf("deploy is build but build_template does not exist in templates[].name")
|
||||||
}
|
}
|
||||||
@ -207,18 +211,21 @@ func (e BackupTemplate) Restore(store db.Store, b *BackupDB) error {
|
|||||||
} else {
|
} else {
|
||||||
InventoryID = k.GetID()
|
InventoryID = k.GetID()
|
||||||
}
|
}
|
||||||
|
|
||||||
var EnvironmentID int
|
var EnvironmentID int
|
||||||
if k := findEntityByName[db.Environment](e.Environment, b.environments); k == nil {
|
if k := findEntityByName[db.Environment](e.Environment, b.environments); k == nil {
|
||||||
return fmt.Errorf("environment does not exist in environments[].name")
|
return fmt.Errorf("environment does not exist in environments[].name")
|
||||||
} else {
|
} else {
|
||||||
EnvironmentID = k.GetID()
|
EnvironmentID = k.GetID()
|
||||||
}
|
}
|
||||||
|
|
||||||
var RepositoryID int
|
var RepositoryID int
|
||||||
if k := findEntityByName[db.Repository](&e.Repository, b.repositories); k == nil {
|
if k := findEntityByName[db.Repository](&e.Repository, b.repositories); k == nil {
|
||||||
return fmt.Errorf("repository does not exist in repositories[].name")
|
return fmt.Errorf("repository does not exist in repositories[].name")
|
||||||
} else {
|
} else {
|
||||||
RepositoryID = k.GetID()
|
RepositoryID = k.GetID()
|
||||||
}
|
}
|
||||||
|
|
||||||
var BuildTemplateID *int
|
var BuildTemplateID *int
|
||||||
if string(e.Type) != "deploy" {
|
if string(e.Type) != "deploy" {
|
||||||
BuildTemplateID = nil
|
BuildTemplateID = nil
|
||||||
@ -227,38 +234,32 @@ func (e BackupTemplate) Restore(store db.Store, b *BackupDB) error {
|
|||||||
} else {
|
} else {
|
||||||
BuildTemplateID = &(k.ID)
|
BuildTemplateID = &(k.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
var ViewID *int
|
var ViewID *int
|
||||||
if k := findEntityByName[db.View](e.View, b.views); k == nil {
|
if k := findEntityByName[db.View](e.View, b.views); k == nil {
|
||||||
ViewID = nil
|
ViewID = nil
|
||||||
} else {
|
} else {
|
||||||
ViewID = &k.ID
|
ViewID = &k.ID
|
||||||
}
|
}
|
||||||
template, err := store.CreateTemplate(
|
|
||||||
db.Template{
|
template := e.Template
|
||||||
ProjectID: b.meta.ID,
|
template.ProjectID = b.meta.ID
|
||||||
InventoryID: &InventoryID,
|
template.RepositoryID = RepositoryID
|
||||||
EnvironmentID: &EnvironmentID,
|
template.EnvironmentID = &EnvironmentID
|
||||||
RepositoryID: RepositoryID,
|
template.InventoryID = &InventoryID
|
||||||
ViewID: ViewID,
|
template.ViewID = ViewID
|
||||||
Autorun: e.Autorun,
|
template.BuildTemplateID = BuildTemplateID
|
||||||
AllowOverrideArgsInTask: e.AllowOverrideArgsInTask,
|
|
||||||
SuppressSuccessAlerts: e.SuppressSuccessAlerts,
|
newTemplate, err := store.CreateTemplate(template)
|
||||||
Name: e.Name,
|
|
||||||
Playbook: e.Playbook,
|
|
||||||
Arguments: e.Arguments,
|
|
||||||
Type: e.Type,
|
|
||||||
BuildTemplateID: BuildTemplateID,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
b.templates = append(b.templates, template)
|
b.templates = append(b.templates, newTemplate)
|
||||||
if e.Cron != nil {
|
if e.Cron != nil {
|
||||||
_, err := store.CreateSchedule(
|
_, err := store.CreateSchedule(
|
||||||
db.Schedule{
|
db.Schedule{
|
||||||
ProjectID: b.meta.ID,
|
ProjectID: b.meta.ID,
|
||||||
TemplateID: template.ID,
|
TemplateID: newTemplate.ID,
|
||||||
CronFormat: *e.Cron,
|
CronFormat: *e.Cron,
|
||||||
RepositoryID: &RepositoryID,
|
RepositoryID: &RepositoryID,
|
||||||
},
|
},
|
||||||
@ -275,14 +276,13 @@ func (e BackupTemplate) Restore(store db.Store, b *BackupDB) error {
|
|||||||
} else {
|
} else {
|
||||||
VaultKeyID = k.ID
|
VaultKeyID = k.ID
|
||||||
}
|
}
|
||||||
_, err := store.CreateTemplateVault(
|
|
||||||
db.TemplateVault{
|
tplVault := vault.TemplateVault
|
||||||
ProjectID: b.meta.ID,
|
tplVault.ProjectID = b.meta.ID
|
||||||
TemplateID: template.ID,
|
tplVault.TemplateID = newTemplate.ID
|
||||||
VaultKeyID: VaultKeyID,
|
tplVault.VaultKeyID = VaultKeyID
|
||||||
Name: vault.Name,
|
|
||||||
},
|
_, err := store.CreateTemplateVault(tplVault)
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -327,43 +327,46 @@ func (backup *BackupFormat) Verify() error {
|
|||||||
|
|
||||||
func (backup *BackupFormat) Restore(user db.User, store db.Store) (*db.Project, error) {
|
func (backup *BackupFormat) Restore(user db.User, store db.Store) (*db.Project, error) {
|
||||||
var b = BackupDB{}
|
var b = BackupDB{}
|
||||||
project, err := store.CreateProject(
|
project := backup.Meta.Project
|
||||||
db.Project{
|
|
||||||
Name: backup.Meta.Name,
|
newProject, err := store.CreateProject(project)
|
||||||
Alert: backup.Meta.Alert,
|
|
||||||
MaxParallelTasks: backup.Meta.MaxParallelTasks,
|
|
||||||
AlertChat: backup.Meta.AlertChat,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
b.meta = project
|
|
||||||
|
b.meta = newProject
|
||||||
|
|
||||||
for i, o := range backup.Environments {
|
for i, o := range backup.Environments {
|
||||||
if err := o.Restore(store, &b); err != nil {
|
if err := o.Restore(store, &b); err != nil {
|
||||||
return nil, fmt.Errorf("error at environments[%d]: %s", i, err.Error())
|
return nil, fmt.Errorf("error at environments[%d]: %s", i, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, o := range backup.Views {
|
for i, o := range backup.Views {
|
||||||
if err := o.Restore(store, &b); err != nil {
|
if err := o.Restore(store, &b); err != nil {
|
||||||
return nil, fmt.Errorf("error at views[%d]: %s", i, err.Error())
|
return nil, fmt.Errorf("error at views[%d]: %s", i, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, o := range backup.Keys {
|
for i, o := range backup.Keys {
|
||||||
if err := o.Restore(store, &b); err != nil {
|
if err := o.Restore(store, &b); err != nil {
|
||||||
return nil, fmt.Errorf("error at keys[%d]: %s", i, err.Error())
|
return nil, fmt.Errorf("error at keys[%d]: %s", i, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, o := range backup.Repositories {
|
for i, o := range backup.Repositories {
|
||||||
if err := o.Restore(store, &b); err != nil {
|
if err := o.Restore(store, &b); err != nil {
|
||||||
return nil, fmt.Errorf("error at repositories[%d]: %s", i, err.Error())
|
return nil, fmt.Errorf("error at repositories[%d]: %s", i, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, o := range backup.Inventories {
|
for i, o := range backup.Inventories {
|
||||||
if err := o.Restore(store, &b); err != nil {
|
if err := o.Restore(store, &b); err != nil {
|
||||||
return nil, fmt.Errorf("error at inventories[%d]: %s", i, err.Error())
|
return nil, fmt.Errorf("error at inventories[%d]: %s", i, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
deployTemplates := make([]int, 0)
|
deployTemplates := make([]int, 0)
|
||||||
for i, o := range backup.Templates {
|
for i, o := range backup.Templates {
|
||||||
if string(o.Type) == "deploy" {
|
if string(o.Type) == "deploy" {
|
||||||
@ -382,12 +385,12 @@ func (backup *BackupFormat) Restore(user db.User, store db.Store) (*db.Project,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if _, err = store.CreateProjectUser(db.ProjectUser{
|
if _, err = store.CreateProjectUser(db.ProjectUser{
|
||||||
ProjectID: project.ID,
|
ProjectID: newProject.ID,
|
||||||
UserID: user.ID,
|
UserID: user.ID,
|
||||||
Role: db.ProjectOwner,
|
Role: db.ProjectOwner,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &project, nil
|
return &newProject, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user