Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 Try to update os provider to latest #150

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it just the os provider that we are interested in? is it possible that other providers are also used by the packer plugin? We do have types in OS that are defined in network, for example... Shouldn't this be just the same function as the one updating the providers in cnspec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The network provider is a good catch. Didn't think about this one. Yes, then we can use the same code as for the cnspec PR.

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