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

incus-simplestreams: Handle removal of combined images #995

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Changes from all commits
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
93 changes: 61 additions & 32 deletions cmd/incus-simplestreams/main_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"fmt"
"os"

"github.com/spf13/cobra"
Expand All @@ -12,6 +13,8 @@ import (

type cmdRemove struct {
global *cmdGlobal

flagVerbose bool
}

// Command generates the command definition.
Expand All @@ -25,10 +28,24 @@ func (c *cmdRemove) Command() *cobra.Command {
This command locates the image from its fingerprint and removes it from the index.
`)
cmd.RunE = c.Run
cmd.Flags().BoolVarP(&c.flagVerbose, "verbose", "v", false, "Show all information messages")

return cmd
}

func (c *cmdRemove) remove(path string) error {
if c.flagVerbose {
fmt.Printf("deleting: %s\n", path)
}

err := os.Remove(path)
if err != nil && !os.IsNotExist(err) {
return err
}

return nil
}

// Run runs the actual command logic.
func (c *cmdRemove) Run(cmd *cobra.Command, args []string) error {
// Quick checks.
Expand Down Expand Up @@ -67,47 +84,59 @@ func (c *cmdRemove) Run(cmd *cobra.Command, args []string) error {
for kVersion, version := range product.Versions {
// Get the metadata entry.
metaEntry, ok := version.Items["incus.tar.xz"]
if !ok {
// Image isn't using our normal structure.
continue
}

if metaEntry.CombinedSha256DiskKvmImg == image.Fingerprint {
// Deleting a VM image.
err = os.Remove(version.Items["disk-kvm.img"].Path)
if err != nil && !os.IsNotExist(err) {
return err
if ok {
if metaEntry.CombinedSha256DiskKvmImg == image.Fingerprint {
// Deleting a VM image.
err = c.remove(version.Items["disk-kvm.img"].Path)
if err != nil {
return err
}

delete(version.Items, "disk-kvm.img")
metaEntry.CombinedSha256DiskKvmImg = ""
} else if metaEntry.CombinedSha256SquashFs == image.Fingerprint {
// Deleting a container image.
err = c.remove(version.Items["squashfs"].Path)
if err != nil && !os.IsNotExist(err) {
return err
}

delete(version.Items, "squashfs")
metaEntry.CombinedSha256SquashFs = ""
} else {
continue
}

delete(version.Items, "disk-kvm.img")
metaEntry.CombinedSha256DiskKvmImg = ""
} else if metaEntry.CombinedSha256SquashFs == image.Fingerprint {
// Deleting a container image.
err = os.Remove(version.Items["squashfs"].Path)
if err != nil && !os.IsNotExist(err) {
return err
}
// Update the metadata entry.
version.Items["incus.tar.xz"] = metaEntry

delete(version.Items, "squashfs")
metaEntry.CombinedSha256SquashFs = ""
} else {
continue
// Delete the version if it's now empty.
if len(version.Items) == 1 {
err = c.remove(metaEntry.Path)
if err != nil {
return err
}

delete(product.Versions, kVersion)
}
}

// Update the metadata entry.
version.Items["incus.tar.xz"] = metaEntry
metaEntry, ok = version.Items["incus_combined.tar.gz"]
if ok {
if metaEntry.HashSha256 == image.Fingerprint {
err = c.remove(metaEntry.Path)
if err != nil {
return err
}

// Delete the version if it's now empty.
if len(version.Items) == 1 {
err = os.Remove(metaEntry.Path)
if err != nil && !os.IsNotExist(err) {
return err
delete(version.Items, "incus_combined.tar.gz")
}

delete(product.Versions, kVersion)
// Delete the version if it's now empty.
if len(version.Items) == 0 {
delete(product.Versions, kVersion)
}
}

break
}

if len(product.Versions) == 0 {
Expand Down
Loading