From 12b7b374c926aec673bba53cc5d16564055c6df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Thu, 14 Nov 2024 01:45:54 -0500 Subject: [PATCH] incusd/instances/publish: Fix base metadata MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #1371 Signed-off-by: Stéphane Graber --- .../server/instance/drivers/driver_lxc.go | 51 ++++++++++--------- .../server/instance/drivers/driver_qemu.go | 51 ++++++++++--------- 2 files changed, 54 insertions(+), 48 deletions(-) diff --git a/internal/server/instance/drivers/driver_lxc.go b/internal/server/instance/drivers/driver_lxc.go index e800dda3d22..8247e584d56 100644 --- a/internal/server/instance/drivers/driver_lxc.go +++ b/internal/server/instance/drivers/driver_lxc.go @@ -5188,6 +5188,30 @@ func (d *lxc) Export(w io.Writer, properties map[string]string, expiration time. return nil } + // Get the instance's architecture. + var arch string + if d.IsSnapshot() { + parentName, _, _ := api.GetParentAndSnapshotName(d.name) + parent, err := instance.LoadByProjectAndName(d.state, d.project.Name, parentName) + if err != nil { + _ = tarWriter.Close() + d.logger.Error("Failed exporting instance", ctxMap) + return meta, err + } + + arch, _ = osarch.ArchitectureName(parent.Architecture()) + } else { + arch, _ = osarch.ArchitectureName(d.architecture) + } + + if arch == "" { + arch, err = osarch.ArchitectureName(d.state.OS.Architectures[0]) + if err != nil { + d.logger.Error("Failed exporting instance", ctxMap) + return meta, err + } + } + // Look for metadata.yaml. fnam := filepath.Join(cDir, "metadata.yaml") if !util.PathExists(fnam) { @@ -5201,30 +5225,6 @@ func (d *lxc) Export(w io.Writer, properties map[string]string, expiration time. defer func() { _ = os.RemoveAll(tempDir) }() - // Get the instance's architecture. - var arch string - if d.IsSnapshot() { - parentName, _, _ := api.GetParentAndSnapshotName(d.name) - parent, err := instance.LoadByProjectAndName(d.state, d.project.Name, parentName) - if err != nil { - _ = tarWriter.Close() - d.logger.Error("Failed exporting instance", ctxMap) - return meta, err - } - - arch, _ = osarch.ArchitectureName(parent.Architecture()) - } else { - arch, _ = osarch.ArchitectureName(d.architecture) - } - - if arch == "" { - arch, err = osarch.ArchitectureName(d.state.OS.Architectures[0]) - if err != nil { - d.logger.Error("Failed exporting instance", ctxMap) - return meta, err - } - } - // Fill in the metadata. meta.Architecture = arch meta.CreationDate = time.Now().UTC().Unix() @@ -5280,6 +5280,9 @@ func (d *lxc) Export(w io.Writer, properties map[string]string, expiration time. return meta, err } + // Fill in the metadata. + meta.Architecture = arch + meta.CreationDate = time.Now().UTC().Unix() if !expiration.IsZero() { meta.ExpiryDate = expiration.UTC().Unix() } diff --git a/internal/server/instance/drivers/driver_qemu.go b/internal/server/instance/drivers/driver_qemu.go index 38596ecbd6b..2bbe3768ee1 100644 --- a/internal/server/instance/drivers/driver_qemu.go +++ b/internal/server/instance/drivers/driver_qemu.go @@ -6378,6 +6378,30 @@ func (d *qemu) Export(w io.Writer, properties map[string]string, expiration time return nil } + // Get the instance's architecture. + var arch string + if d.IsSnapshot() { + parentName, _, _ := api.GetParentAndSnapshotName(d.name) + parent, err := instance.LoadByProjectAndName(d.state, d.project.Name, parentName) + if err != nil { + _ = tarWriter.Close() + d.logger.Error("Failed exporting instance", ctxMap) + return meta, err + } + + arch, _ = osarch.ArchitectureName(parent.Architecture()) + } else { + arch, _ = osarch.ArchitectureName(d.architecture) + } + + if arch == "" { + arch, err = osarch.ArchitectureName(d.state.OS.Architectures[0]) + if err != nil { + d.logger.Error("Failed exporting instance", ctxMap) + return meta, err + } + } + // Look for metadata.yaml. fnam := filepath.Join(cDir, "metadata.yaml") if !util.PathExists(fnam) { @@ -6391,30 +6415,6 @@ func (d *qemu) Export(w io.Writer, properties map[string]string, expiration time defer func() { _ = os.RemoveAll(tempDir) }() - // Get the instance's architecture. - var arch string - if d.IsSnapshot() { - parentName, _, _ := api.GetParentAndSnapshotName(d.name) - parent, err := instance.LoadByProjectAndName(d.state, d.project.Name, parentName) - if err != nil { - _ = tarWriter.Close() - d.logger.Error("Failed exporting instance", ctxMap) - return meta, err - } - - arch, _ = osarch.ArchitectureName(parent.Architecture()) - } else { - arch, _ = osarch.ArchitectureName(d.architecture) - } - - if arch == "" { - arch, err = osarch.ArchitectureName(d.state.OS.Architectures[0]) - if err != nil { - d.logger.Error("Failed exporting instance", ctxMap) - return meta, err - } - } - // Fill in the metadata. meta.Architecture = arch meta.CreationDate = time.Now().UTC().Unix() @@ -6469,6 +6469,9 @@ func (d *qemu) Export(w io.Writer, properties map[string]string, expiration time return meta, err } + // Fill in the metadata. + meta.Architecture = arch + meta.CreationDate = time.Now().UTC().Unix() if !expiration.IsZero() { meta.ExpiryDate = expiration.UTC().Unix() }