delete the old template as soon as the new one is verified

This commit is contained in:
Max Erenberg 2022-07-17 01:48:40 -04:00
parent 77d9fd3865
commit cb15d253ba
2 changed files with 23 additions and 33 deletions

View File

@ -134,6 +134,7 @@ type DistroInfo struct {
type TemplateVersionInfo struct {
TemplateId string
Distro string
Version string
}
@ -154,6 +155,7 @@ func (c *CloudBuilder) getExistingTemplateVersions() map[string]TemplateVersionI
if !ok || version > otherVersion.Version {
mostRecentVersions[distro] = TemplateVersionInfo{
TemplateId: template.Id,
Distro: distro,
Version: version,
}
}
@ -213,6 +215,18 @@ func (c *CloudBuilder) destroyVirtualMachine(vmID string) error {
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 {
f1, err := strconv.ParseFloat(version1, 32)
if err != nil {
@ -261,7 +275,6 @@ func (c *CloudBuilder) Start() (err error) {
},
}
existingTemplates := c.getExistingTemplateVersions()
newVersions := make(map[string]string)
for _, distroLower := range c.cfg.DistrosToCheck {
distroInfo, ok := distrosInfo[distroLower]
if !ok {
@ -276,9 +289,9 @@ func (c *CloudBuilder) Start() (err error) {
}
log.Debug().Str("newVersion", newVersion).Str("codename", codename).Msg(distro)
curTemplate, ok := existingTemplates[distro]
curTemplate, curTemplateExists := existingTemplates[distro]
curVersion := curTemplate.Version
if ok && c.versionStringCompare(newVersion, curVersion) <= 0 {
if curTemplateExists && c.versionStringCompare(newVersion, curVersion) <= 0 {
log.Debug().
Str("distro", distro).
Str("version", curVersion).
@ -286,7 +299,6 @@ func (c *CloudBuilder) Start() (err error) {
continue
}
newVersions[distro] = newVersion
log.Info().
Str("distro", distroLower).
Msg("Template is nonexistent or out of date, creating a new one")
@ -327,12 +339,14 @@ func (c *CloudBuilder) Start() (err error) {
return
}
if err = c.sendEmailNotification(template.Name); err != nil {
return
// Delete the old template for this distro, if there was one
if c.cfg.DeleteOldTemplates && curTemplateExists {
if err = c.deleteOldTemplate(curTemplate); err != nil {
return
}
}
}
if c.cfg.DeleteOldTemplates {
if err = c.DeleteOldTemplates(existingTemplates, newVersions); err != nil {
if err = c.sendEmailNotification(template.Name); err != nil {
return
}
}

View File

@ -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
}