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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
package simplestreams
import (
"fmt"
"strings"
"time"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/osarch"
)
var lxdCompatCombinedItems = []string{"lxd_combined.tar.gz"}
var lxdCompatItems = []string{"lxd.tar.xz"}
// Products represents the base of download.json.
type Products struct {
ContentID string `json:"content_id"`
DataType string `json:"datatype"`
Format string `json:"format"`
License string `json:"license,omitempty"`
Products map[string]Product `json:"products"`
Updated string `json:"updated,omitempty"`
}
// Product represents a single product inside download.json.
type Product struct {
Aliases string `json:"aliases"`
Architecture string `json:"arch"`
OperatingSystem string `json:"os"`
LXDRequirements map[string]string `json:"lxd_requirements,omitempty"`
Requirements map[string]string `json:"requirements,omitempty"`
Release string `json:"release"`
ReleaseCodename string `json:"release_codename,omitempty"`
ReleaseTitle string `json:"release_title"`
Supported bool `json:"supported,omitempty"`
SupportedEOL string `json:"support_eol,omitempty"`
Version string `json:"version,omitempty"`
Versions map[string]ProductVersion `json:"versions"`
// Non-standard fields (only used on some image servers).
Variant string `json:"variant,omitempty"`
}
// ProductVersion represents a particular version of a product.
type ProductVersion struct {
Items map[string]ProductVersionItem `json:"items"`
Label string `json:"label,omitempty"`
PublicName string `json:"pubname,omitempty"`
}
// ProductVersionItem represents a file/item of a particular ProductVersion.
type ProductVersionItem struct {
LXDHashSha256DiskImg string `json:"combined_disk1-img_sha256,omitempty"`
LXDHashSha256DiskKvmImg string `json:"combined_disk-kvm-img_sha256,omitempty"`
LXDHashSha256DiskUefiImg string `json:"combined_uefi1-img_sha256,omitempty"`
LXDHashSha256RootXz string `json:"combined_rootxz_sha256,omitempty"`
LXDHashSha256 string `json:"combined_sha256,omitempty"`
LXDHashSha256SquashFs string `json:"combined_squashfs_sha256,omitempty"`
FileType string `json:"ftype"`
HashMd5 string `json:"md5,omitempty"`
Path string `json:"path"`
HashSha256 string `json:"sha256,omitempty"`
Size int64 `json:"size"`
DeltaBase string `json:"delta_base,omitempty"`
}
// ToLXD converts the products data into a list of LXD images and associated downloadable files.
func (s *Products) ToLXD() ([]api.Image, map[string][][]string) {
downloads := map[string][][]string{}
images := []api.Image{}
nameLayout := "20060102"
eolLayout := "2006-01-02"
for _, product := range s.Products {
// Skip unsupported architectures
architecture, err := osarch.ArchitectureId(product.Architecture)
if err != nil {
continue
}
architectureName, err := osarch.ArchitectureName(architecture)
if err != nil {
continue
}
for name, version := range product.Versions {
// Short of anything better, use the name as date (see format above)
if len(name) < 8 {
continue
}
creationDate, err := time.Parse(nameLayout, name[0:8])
if err != nil {
continue
}
// Image processing function
addImage := func(meta *ProductVersionItem, root *ProductVersionItem) error {
// Look for deltas
deltas := []ProductVersionItem{}
if root != nil && shared.StringInSlice(root.FileType, []string{"squashfs", "disk-kvm.img"}) {
for _, item := range version.Items {
if item.FileType == fmt.Sprintf("%s.vcdiff", root.FileType) {
deltas = append(deltas, item)
}
}
}
// Figure out the fingerprint
fingerprint := ""
if root != nil {
if root.FileType == "root.tar.xz" {
if meta.LXDHashSha256RootXz != "" {
fingerprint = meta.LXDHashSha256RootXz
} else {
fingerprint = meta.LXDHashSha256
}
} else if root.FileType == "squashfs" {
fingerprint = meta.LXDHashSha256SquashFs
} else if root.FileType == "disk-kvm.img" {
fingerprint = meta.LXDHashSha256DiskKvmImg
} else if root.FileType == "disk1.img" {
fingerprint = meta.LXDHashSha256DiskImg
} else if root.FileType == "uefi1.img" {
fingerprint = meta.LXDHashSha256DiskUefiImg
}
} else {
fingerprint = meta.HashSha256
}
if fingerprint == "" {
return fmt.Errorf("No LXD image fingerprint found")
}
// Figure out the size
size := meta.Size
if root != nil {
size += root.Size
}
// Determine filename
if meta.Path == "" {
return fmt.Errorf("Missing path field on metadata entry")
}
fields := strings.Split(meta.Path, "/")
filename := fields[len(fields)-1]
// Generate the actual image entry
description := fmt.Sprintf("%s %s %s", product.OperatingSystem, product.ReleaseTitle, product.Architecture)
if version.Label != "" {
description = fmt.Sprintf("%s (%s)", description, version.Label)
}
description = fmt.Sprintf("%s (%s)", description, name)
image := api.Image{}
image.Architecture = architectureName
image.Public = true
image.Size = size
image.CreatedAt = creationDate
image.UploadedAt = creationDate
image.Filename = filename
image.Fingerprint = fingerprint
image.Properties = map[string]string{
"os": product.OperatingSystem,
"release": product.Release,
"version": product.Version,
"architecture": product.Architecture,
"label": version.Label,
"serial": name,
"description": description,
}
for _, requirements := range []map[string]string{product.LXDRequirements, product.Requirements} {
if requirements != nil {
for lxdReq, lxdReqVal := range requirements {
image.Properties["requirements."+lxdReq] = lxdReqVal
}
break // Stop at first set of requirements found.
}
}
if product.Variant != "" {
image.Properties["variant"] = product.Variant
}
image.Type = "container"
if root != nil {
image.Properties["type"] = root.FileType
if root.FileType == "disk1.img" || root.FileType == "disk-kvm.img" || root.FileType == "uefi1.img" {
image.Type = "virtual-machine"
}
} else {
image.Properties["type"] = "tar.gz"
}
// Clear unset properties
for k, v := range image.Properties {
if v == "" {
delete(image.Properties, k)
}
}
// Add the provided aliases
if product.Aliases != "" {
image.Aliases = []api.ImageAlias{}
for _, entry := range strings.Split(product.Aliases, ",") {
image.Aliases = append(image.Aliases, api.ImageAlias{Name: entry})
}
}
// Attempt to parse the EOL
image.ExpiresAt = time.Unix(0, 0).UTC()
if product.SupportedEOL != "" {
eolDate, err := time.Parse(eolLayout, product.SupportedEOL)
if err == nil {
image.ExpiresAt = eolDate
}
}
// Set the file list
var imgDownloads [][]string
if root == nil {
imgDownloads = [][]string{{meta.Path, meta.HashSha256, "meta", fmt.Sprintf("%d", meta.Size)}}
} else {
imgDownloads = [][]string{
{meta.Path, meta.HashSha256, "meta", fmt.Sprintf("%d", meta.Size)},
{root.Path, root.HashSha256, "root", fmt.Sprintf("%d", root.Size)}}
}
// Add the deltas
for _, delta := range deltas {
srcImage, ok := product.Versions[delta.DeltaBase]
if !ok {
// Delta for a since expired image
continue
}
// Locate source image fingerprint
var srcFingerprint string
for _, item := range srcImage.Items {
if !shared.StringInSlice(item.FileType, lxdCompatItems) {
continue
}
srcFingerprint = item.LXDHashSha256SquashFs
break
}
if srcFingerprint == "" {
// Couldn't find the image
continue
}
// Add the delta
imgDownloads = append(imgDownloads, []string{
delta.Path,
delta.HashSha256,
fmt.Sprintf("root.delta-%s", srcFingerprint),
fmt.Sprintf("%d", delta.Size)})
}
// Add the image
downloads[fingerprint] = imgDownloads
images = append(images, image)
return nil
}
// Locate a valid LXD image
for _, item := range version.Items {
if shared.StringInSlice(item.FileType, lxdCompatCombinedItems) {
err := addImage(&item, nil)
if err != nil {
continue
}
break // Stop at first compatible item found.
} else if shared.StringInSlice(item.FileType, lxdCompatItems) {
// Locate the root files
for _, subItem := range version.Items {
if shared.StringInSlice(subItem.FileType, []string{"disk1.img", "disk-kvm.img", "uefi1.img", "root.tar.xz", "squashfs"}) {
err := addImage(&item, &subItem)
if err != nil {
continue
}
}
}
break // Stop at first compatible item found.
}
}
}
}
return images, downloads
}
|