delete the old template as soon as the new one is verified
This commit is contained in:
parent
77d9fd3865
commit
cb15d253ba
|
@ -134,6 +134,7 @@ type DistroInfo struct {
|
||||||
|
|
||||||
type TemplateVersionInfo struct {
|
type TemplateVersionInfo struct {
|
||||||
TemplateId string
|
TemplateId string
|
||||||
|
Distro string
|
||||||
Version string
|
Version string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,6 +155,7 @@ func (c *CloudBuilder) getExistingTemplateVersions() map[string]TemplateVersionI
|
||||||
if !ok || version > otherVersion.Version {
|
if !ok || version > otherVersion.Version {
|
||||||
mostRecentVersions[distro] = TemplateVersionInfo{
|
mostRecentVersions[distro] = TemplateVersionInfo{
|
||||||
TemplateId: template.Id,
|
TemplateId: template.Id,
|
||||||
|
Distro: distro,
|
||||||
Version: version,
|
Version: version,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -213,6 +215,18 @@ func (c *CloudBuilder) destroyVirtualMachine(vmID string) error {
|
||||||
return c.client.WaitForJobToComplete(deletionJobID)
|
return c.client.WaitForJobToComplete(deletionJobID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CloudBuilder) deleteOldTemplate(oldTemplate TemplateVersionInfo) error {
|
||||||
|
log.Info().
|
||||||
|
Str("oldVersion", oldTemplate.Version).
|
||||||
|
Str("distro", oldTemplate.Distro).
|
||||||
|
Msg("Deleting template")
|
||||||
|
jobID, err := c.client.DeleteTemplate(oldTemplate.TemplateId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.client.WaitForJobToComplete(jobID)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *CloudBuilder) versionStringCompare(version1, version2 string) int {
|
func (c *CloudBuilder) versionStringCompare(version1, version2 string) int {
|
||||||
f1, err := strconv.ParseFloat(version1, 32)
|
f1, err := strconv.ParseFloat(version1, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -261,7 +275,6 @@ func (c *CloudBuilder) Start() (err error) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
existingTemplates := c.getExistingTemplateVersions()
|
existingTemplates := c.getExistingTemplateVersions()
|
||||||
newVersions := make(map[string]string)
|
|
||||||
for _, distroLower := range c.cfg.DistrosToCheck {
|
for _, distroLower := range c.cfg.DistrosToCheck {
|
||||||
distroInfo, ok := distrosInfo[distroLower]
|
distroInfo, ok := distrosInfo[distroLower]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -276,9 +289,9 @@ func (c *CloudBuilder) Start() (err error) {
|
||||||
}
|
}
|
||||||
log.Debug().Str("newVersion", newVersion).Str("codename", codename).Msg(distro)
|
log.Debug().Str("newVersion", newVersion).Str("codename", codename).Msg(distro)
|
||||||
|
|
||||||
curTemplate, ok := existingTemplates[distro]
|
curTemplate, curTemplateExists := existingTemplates[distro]
|
||||||
curVersion := curTemplate.Version
|
curVersion := curTemplate.Version
|
||||||
if ok && c.versionStringCompare(newVersion, curVersion) <= 0 {
|
if curTemplateExists && c.versionStringCompare(newVersion, curVersion) <= 0 {
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Str("distro", distro).
|
Str("distro", distro).
|
||||||
Str("version", curVersion).
|
Str("version", curVersion).
|
||||||
|
@ -286,7 +299,6 @@ func (c *CloudBuilder) Start() (err error) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
newVersions[distro] = newVersion
|
|
||||||
log.Info().
|
log.Info().
|
||||||
Str("distro", distroLower).
|
Str("distro", distroLower).
|
||||||
Msg("Template is nonexistent or out of date, creating a new one")
|
Msg("Template is nonexistent or out of date, creating a new one")
|
||||||
|
@ -327,12 +339,14 @@ func (c *CloudBuilder) Start() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = c.sendEmailNotification(template.Name); err != nil {
|
// Delete the old template for this distro, if there was one
|
||||||
return
|
if c.cfg.DeleteOldTemplates && curTemplateExists {
|
||||||
|
if err = c.deleteOldTemplate(curTemplate); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if c.cfg.DeleteOldTemplates {
|
if err = c.sendEmailNotification(template.Name); err != nil {
|
||||||
if err = c.DeleteOldTemplates(existingTemplates, newVersions); err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
package cloudbuilder
|
|
||||||
|
|
||||||
import "github.com/rs/zerolog/log"
|
|
||||||
|
|
||||||
func (c *CloudBuilder) DeleteOldTemplates(oldTemplates map[string]TemplateVersionInfo, newVersions map[string]string) error {
|
|
||||||
for distro, _ := range newVersions {
|
|
||||||
oldTemplate, ok := oldTemplates[distro]
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
log.Info().
|
|
||||||
Str("oldVersion", oldTemplate.Version).
|
|
||||||
Str("distro", distro).
|
|
||||||
Msg("Deleting template")
|
|
||||||
jobID, err := c.client.DeleteTemplate(oldTemplate.TemplateId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = c.client.WaitForJobToComplete(jobID); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
Loading…
Reference in New Issue