Skip to content

Commit

Permalink
🧹 Try to update os provider to latest
Browse files Browse the repository at this point in the history
Fixes #149

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker committed Nov 6, 2023
1 parent 05495a9 commit b1bbf3b
Show file tree
Hide file tree
Showing 2 changed files with 38 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
36 changes: 36 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
}
}

updateOsProvider(ui)

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

return nil
}

func updateOsProvider(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 os provider")
return
}
outdated := false
for _, provider := range allProviders {
if provider.Name == "os" {
latestVersion, err := providers.LatestVersion(provider.Name)
if err != nil {
ui.Error(err.Error())
ui.Message("failed to determine latest version for os provider, not going to update it")
return
}
if latestVersion != provider.Version {
outdated = true
}
}
}

if outdated {
installed, err := providers.Install("os", "")
if err != nil {
ui.Error(err.Error())
ui.Message("failed to install/update os provider")
}
if installed != nil {
ui.Message("successfully installed " + installed.Name + " provider" + " version=" + installed.Version + " path=" + installed.Path)
}
}
}

0 comments on commit b1bbf3b

Please sign in to comment.