1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
package main
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
"sort"
"github.com/spf13/cobra"
cli "github.com/lxc/incus/v6/internal/cmd"
"github.com/lxc/incus/v6/shared/simplestreams"
)
type cmdPrune struct {
global *cmdGlobal
flagDryRun bool
flagRetention int
flagVerbose bool
}
// Command generates the command definition.
func (c *cmdPrune) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = "prune"
cmd.Short = "Clean up obsolete files and data"
cmd.Long = cli.FormatSection("Description",
`Cleans up obsolete tarball files and removes outdated versions of a product
The prune command scans the project directory for tarball files that do not have corresponding references
in the 'images.json' file. Any tarball file that is not listed in images.json is considered orphaned
and will be deleted.
Additionally this command will delete older images, keeping a configurable number of older images per product.`)
cmd.RunE = c.Run
cmd.Flags().BoolVarP(&c.flagDryRun, "dry-run", "d", false, "Preview changes without executing actual operations")
cmd.Flags().IntVarP(&c.flagRetention, "retention", "r", 2, "Number of older versions of the product to preserve"+"``")
cmd.Flags().BoolVarP(&c.flagVerbose, "verbose", "v", false, "Show all information messages")
return cmd
}
// Run runs the actual command logic.
func (c *cmdPrune) Run(cmd *cobra.Command, args []string) error {
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 0, 0)
if exit {
return err
}
if c.flagDryRun {
c.flagVerbose = true
}
err = c.prune()
if err != nil {
return err
}
return nil
}
func (c *cmdPrune) pruneFiles(products *simplestreams.Products, filesToPreserve []string) error {
deletedFiles := []string{}
err := filepath.WalkDir("./images", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// Omit the path if it is a directory or if it exists in the images.json file.
if d.IsDir() || slices.Contains(filesToPreserve, path) {
return nil
}
if c.flagVerbose {
deletedFiles = append(deletedFiles, path)
}
if !c.flagDryRun {
e := os.Remove(path)
if e != nil {
return e
}
}
return nil
})
if err != nil {
return err
}
if c.flagVerbose && len(deletedFiles) > 0 {
fmt.Printf("Following files were removed:\n")
for _, file := range deletedFiles {
fmt.Println(file)
}
}
return nil
}
func (c *cmdPrune) prune() error {
body, err := os.ReadFile("streams/v1/images.json")
if err != nil {
return err
}
products := simplestreams.Products{}
err = json.Unmarshal(body, &products)
if err != nil {
return err
}
filesToPreserve := []string{}
deletedItems := []string{}
deletedVersions := []string{}
for kProduct, product := range products.Products {
versionNames := []string{}
for kVersion, version := range product.Versions {
for kItem, item := range version.Items {
_, err := os.Stat(item.Path)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
if c.flagVerbose {
deletedItems = append(deletedItems, fmt.Sprintf("%s:%s:%s", kProduct, kVersion, item.Path))
}
// Corresponding file doesn't exist on disk. Remove item from products.
delete(version.Items, kItem)
}
filesToPreserve = append(filesToPreserve, item.Path)
}
if len(version.Items) == 0 {
delete(product.Versions, kVersion)
continue
}
versionNames = append(versionNames, kVersion)
}
if len(product.Versions) == 0 {
delete(products.Products, kProduct)
continue
}
sort.Strings(versionNames)
updatedVersions := map[string]simplestreams.ProductVersion{}
iteration := 0
for i := len(versionNames) - 1; i >= 0; i-- {
version := versionNames[i]
if iteration <= c.flagRetention {
updatedVersions[version] = product.Versions[version]
} else if c.flagVerbose {
deletedVersions = append(deletedVersions, fmt.Sprintf("%s:%s", kProduct, version))
}
iteration += 1
}
p := products.Products[kProduct]
p.Versions = updatedVersions
products.Products[kProduct] = p
}
if c.flagVerbose {
if len(deletedItems) > 0 {
fmt.Printf("Following items were removed from images.json:\n")
for _, item := range deletedItems {
fmt.Println(item)
}
}
if len(deletedVersions) > 0 {
fmt.Printf("Following versions were removed:\n")
for _, version := range deletedVersions {
fmt.Println(version)
}
}
}
if !c.flagDryRun {
// Write back the images file.
body, err = json.Marshal(&products)
if err != nil {
return err
}
err = os.WriteFile("streams/v1/images.json", body, 0o644)
if err != nil {
return err
}
// Re-generate the index.
err = writeIndex(&products)
if err != nil {
return err
}
}
err = c.pruneFiles(&products, filesToPreserve)
if err != nil {
return err
}
return nil
}
|