Skip to content

Commit

Permalink
🧹 Try to update os provider to latest (#150)
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker authored Nov 14, 2023
1 parent 05495a9 commit 3f5b3b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions examples/packer-docker/docker-ubuntu.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ packer {
variable "image_prefix" {
type = string
description = "Prefix to be applied to image name"
default = "mondoo-gcp-ubuntu-2004-secure-base"
default = "mondoo-ubuntu-2004-secure-base"
}

locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
Expand All @@ -28,7 +28,7 @@ source "docker" "ubuntu" {
}

build {
name = "mondoo-docker-ubuntu-2204-secure-base"
name = "mondoo-docker-ubuntu-2004-secure-base"
sources = [
"source.docker.ubuntu"
]
Expand Down
35 changes: 35 additions & 0 deletions provisioner/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ func (p *Provisioner) executeCnspec(ui packer.Ui, comm packer.Communicator) erro
}
}

updateProviders(ui)

var result *scan.ScanResult
var err error
if p.config.Incognito {
Expand Down Expand Up @@ -598,3 +600,36 @@ func (p *Provisioner) executeCnspec(ui packer.Ui, comm packer.Communicator) erro

return nil
}

func updateProviders(ui packer.Ui) {
allProviders, err := providers.ListActive()
if err != nil {
ui.Error(err.Error())
ui.Message("failed to list providers, not going to update cnspec providers")
return
}
updatedProviders := []*providers.Provider{}
for _, provider := range allProviders {
if provider.Name == "mock" || provider.Name == "core" {
continue
}
latestVersion, err := providers.LatestVersion(provider.Name)
if err != nil {
ui.Error(err.Error())
ui.Message("failed to determine latest version for " + provider.Name + " provider, not going to update it")
continue
}
if latestVersion != provider.Version {
installed, err := providers.Install(provider.Name, "")
if err != nil {
ui.Error(err.Error())
ui.Message("failed to install/update " + provider.Name + " provider")
continue
}
updatedProviders = append(updatedProviders, installed)
}
}
for _, p := range updatedProviders {
ui.Message("successfully installed " + p.Name + " provider" + " version=" + p.Version + " path=" + p.Path)
}
}

0 comments on commit 3f5b3b8

Please sign in to comment.