From 728c7c43c8e4538e5d01a88f253f8446b832bbad Mon Sep 17 00:00:00 2001 From: Christian Zunker Date: Fri, 3 Nov 2023 14:36:36 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20Try=20to=20update=20os=20provide?= =?UTF-8?q?r=20to=20latest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #149 Signed-off-by: Christian Zunker --- examples/packer-docker/docker-ubuntu.pkr.hcl | 4 +-- provisioner/provisioner.go | 37 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/examples/packer-docker/docker-ubuntu.pkr.hcl b/examples/packer-docker/docker-ubuntu.pkr.hcl index 7c4bb64..8b7a3fa 100644 --- a/examples/packer-docker/docker-ubuntu.pkr.hcl +++ b/examples/packer-docker/docker-ubuntu.pkr.hcl @@ -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:]", "") } @@ -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" ] diff --git a/provisioner/provisioner.go b/provisioner/provisioner.go index e8195bd..23d9a3d 100644 --- a/provisioner/provisioner.go +++ b/provisioner/provisioner.go @@ -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 { @@ -598,3 +600,38 @@ 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) + } + } + return +}