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
|
package main
import (
"archive/tar"
"bufio"
"fmt"
"io"
"os"
"os/exec"
"time"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
cli "github.com/lxc/incus/v6/internal/cmd"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/ask"
"github.com/lxc/incus/v6/shared/osarch"
)
type cmdGenerateMetadata struct {
global *cmdGlobal
}
// Command generates the command definition.
func (c *cmdGenerateMetadata) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = "generate-metadata <path>"
cmd.Short = "Generate a metadata tarball"
cmd.Long = cli.FormatSection("Description",
`Generate a metadata tarball
This command produces an incus.tar.xz tarball for use with an existing QCOW2 or squashfs disk image.
This command will prompt for all of the metadata tarball fields:
- Operating system name
- Release
- Variant
- Architecture
- Description
`)
cmd.RunE = c.Run
return cmd
}
// Run runs the actual command logic.
func (c *cmdGenerateMetadata) Run(cmd *cobra.Command, args []string) error {
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 1, 1)
if exit {
return err
}
// Setup asker.
asker := ask.NewAsker(bufio.NewReader(os.Stdin))
// Create the tarball.
metaFile, err := os.Create(args[0])
if err != nil {
return err
}
defer metaFile.Close()
// Generate the metadata.
timestamp := time.Now().UTC()
metadata := api.ImageMetadata{
Properties: map[string]string{},
CreationDate: timestamp.Unix(),
}
// Question - os
metaOS, err := asker.AskString("Operating system name: ", "", nil)
if err != nil {
return err
}
metadata.Properties["os"] = metaOS
// Question - release
metaRelease, err := asker.AskString("Release name: ", "", nil)
if err != nil {
return err
}
metadata.Properties["release"] = metaRelease
// Question - variant
metaVariant, err := asker.AskString("Variant name [default=\"default\"]: ", "default", nil)
if err != nil {
return err
}
metadata.Properties["variant"] = metaVariant
// Question - architecture
var incusArch string
metaArchitecture, err := asker.AskString("Architecture name: ", "", func(value string) error {
id, err := osarch.ArchitectureId(value)
if err != nil {
return err
}
incusArch, err = osarch.ArchitectureName(id)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
metadata.Properties["architecture"] = metaArchitecture
metadata.Architecture = incusArch
// Question - description
defaultDescription := fmt.Sprintf("%s %s (%s) (%s) (%s)", metaOS, metaRelease, metaVariant, metaArchitecture, timestamp.Format("200601021504"))
metaDescription, err := asker.AskString(fmt.Sprintf("Description [default=\"%s\"]: ", defaultDescription), defaultDescription, nil)
if err != nil {
return err
}
metadata.Properties["description"] = metaDescription
// Generate YAML.
body, err := yaml.Marshal(&metadata)
if err != nil {
return err
}
// Prepare the tarball.
tarPipeReader, tarPipeWriter := io.Pipe()
tarWriter := tar.NewWriter(tarPipeWriter)
// Compress the tarball.
chDone := make(chan error)
go func() {
cmd := exec.Command("xz", "-9", "-c")
cmd.Stdin = tarPipeReader
cmd.Stdout = metaFile
err := cmd.Run()
chDone <- err
}()
// Add metadata.yaml.
hdr := &tar.Header{
Name: "metadata.yaml",
Size: int64(len(body)),
Mode: 0o644,
Uname: "root",
Gname: "root",
ModTime: time.Now(),
}
err = tarWriter.WriteHeader(hdr)
if err != nil {
return err
}
_, err = tarWriter.Write(body)
if err != nil {
return err
}
// Close the tarball.
err = tarWriter.Close()
if err != nil {
return err
}
err = tarPipeWriter.Close()
if err != nil {
return err
}
err = <-chDone
if err != nil {
return err
}
return nil
}
|