Skip to content

Commit

Permalink
incusd/instances/publish: Fix base metadata
Browse files Browse the repository at this point in the history
Closes #1371

Signed-off-by: Stéphane Graber <[email protected]>
  • Loading branch information
stgraber committed Nov 14, 2024
1 parent 101c68e commit aa4c961
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 232 deletions.
177 changes: 61 additions & 116 deletions internal/server/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5186,85 +5186,35 @@ func (d *lxc) Export(w io.Writer, properties map[string]string, expiration time.
return nil
}

// Look for metadata.yaml.
meta := api.ImageMetadata{}

fnam := filepath.Join(cDir, "metadata.yaml")
if !util.PathExists(fnam) {
// Generate a new metadata.yaml.
tempDir, err := os.MkdirTemp("", "incus_metadata_")
// 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 nil, err
}

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()
meta.Properties = properties
if !expiration.IsZero() {
meta.ExpiryDate = expiration.UTC().Unix()
}

data, err := yaml.Marshal(&meta)
if err != nil {
_ = tarWriter.Close()
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}
arch, _ = osarch.ArchitectureName(parent.Architecture())
} else {
arch, _ = osarch.ArchitectureName(d.architecture)
}

// Write the actual file.
fnam = filepath.Join(tempDir, "metadata.yaml")
err = os.WriteFile(fnam, data, 0644)
if arch == "" {
arch, err = osarch.ArchitectureName(d.state.OS.Architectures[0])
if err != nil {
_ = tarWriter.Close()
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}
}

fi, err := os.Lstat(fnam)
if err != nil {
_ = tarWriter.Close()
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}
// Generate metadata.yaml.
meta := api.ImageMetadata{}
fnam := filepath.Join(cDir, "metadata.yaml")

tmpOffset := len(path.Dir(fnam)) + 1
err = tarWriter.WriteFile(fnam[tmpOffset:], fnam, fi, false)
if err != nil {
_ = tarWriter.Close()
d.logger.Debug("Error writing to tarfile", logger.Ctx{"err": err})
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}
} else {
if util.PathExists(fnam) {
// Parse the metadata.
content, err := os.ReadFile(fnam)
if err != nil {
Expand All @@ -5279,65 +5229,60 @@ func (d *lxc) Export(w io.Writer, properties map[string]string, expiration time.
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}
}

if !expiration.IsZero() {
meta.ExpiryDate = expiration.UTC().Unix()
}
// Fill in the metadata.
meta.Architecture = arch
meta.CreationDate = time.Now().UTC().Unix()

if properties != nil {
meta.Properties = properties
}
if meta.Properties == nil {
meta.Properties = properties
}

if properties != nil || !expiration.IsZero() {
// Generate a new metadata.yaml.
tempDir, err := os.MkdirTemp("", "incus_metadata_")
if err != nil {
_ = tarWriter.Close()
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}
if !expiration.IsZero() {
meta.ExpiryDate = expiration.UTC().Unix()
}

defer func() { _ = os.RemoveAll(tempDir) }()
// Write the new metadata.yaml.
tempDir, err := os.MkdirTemp("", "incus_metadata_")
if err != nil {
_ = tarWriter.Close()
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}

data, err := yaml.Marshal(&meta)
if err != nil {
_ = tarWriter.Close()
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}
defer func() { _ = os.RemoveAll(tempDir) }()

// Write the actual file.
fnam = filepath.Join(tempDir, "metadata.yaml")
err = os.WriteFile(fnam, data, 0644)
if err != nil {
_ = tarWriter.Close()
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}
}
data, err := yaml.Marshal(&meta)
if err != nil {
_ = tarWriter.Close()
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}

// Include metadata.yaml in the tarball.
fi, err := os.Lstat(fnam)
if err != nil {
_ = tarWriter.Close()
d.logger.Debug("Error statting during export", logger.Ctx{"fileName": fnam})
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}
fnam = filepath.Join(tempDir, "metadata.yaml")
err = os.WriteFile(fnam, data, 0644)
if err != nil {
_ = tarWriter.Close()
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}

if properties != nil || !expiration.IsZero() {
tmpOffset := len(path.Dir(fnam)) + 1
err = tarWriter.WriteFile(fnam[tmpOffset:], fnam, fi, false)
} else {
err = tarWriter.WriteFile(fnam[offset:], fnam, fi, false)
}
// Add metadata.yaml to the tarball.
fi, err := os.Lstat(fnam)
if err != nil {
_ = tarWriter.Close()
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}

if err != nil {
_ = tarWriter.Close()
d.logger.Debug("Error writing to tarfile", logger.Ctx{"err": err})
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}
tmpOffset := len(filepath.Dir(fnam)) + 1
err = tarWriter.WriteFile(fnam[tmpOffset:], fnam, fi, false)
if err != nil {
_ = tarWriter.Close()
d.logger.Debug("Error writing to tarfile", logger.Ctx{"err": err})
d.logger.Error("Failed exporting instance", ctxMap)
return nil, err
}

// Include all the rootfs files.
Expand Down
Loading

0 comments on commit aa4c961

Please sign in to comment.