Refactor gRPC-based image functions
Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
@@ -4,24 +4,35 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
apiimage "github.com/warewulf/warewulf/internal/pkg/api/image"
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/warewulf/warewulf/internal/pkg/image"
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
func CobraRunE(cmd *cobra.Command, imageNames []string) error {
|
||||
if BuildAll {
|
||||
var err error
|
||||
imageNames, err = image.ListSources()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not list images: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(imageNames) == 0 {
|
||||
return fmt.Errorf("no images specified; use --all to build all images")
|
||||
}
|
||||
|
||||
if SyncUser {
|
||||
for _, name := range args {
|
||||
for _, name := range imageNames {
|
||||
if err := image.Syncuser(name, true); err != nil {
|
||||
return fmt.Errorf("syncuser error: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cbp := &wwapiv1.ImageBuildParameter{
|
||||
ImageNames: args,
|
||||
Force: BuildForce,
|
||||
All: BuildAll,
|
||||
for _, imageName := range imageNames {
|
||||
if err := image.Build(imageName, BuildForce); err != nil {
|
||||
return fmt.Errorf("error building image %s: %s", imageName, err)
|
||||
}
|
||||
}
|
||||
return apiimage.ImageBuild(cbp)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,24 +3,26 @@ package delete
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/image"
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/warewulf/warewulf/internal/pkg/image"
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
cdp := &wwapiv1.ImageDeleteParameter{
|
||||
ImageNames: args,
|
||||
}
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, imageNames []string) (err error) {
|
||||
if !SetYes {
|
||||
yes := util.Confirm(fmt.Sprintf("Are you sure you want to delete image %s", args))
|
||||
yes := util.Confirm(fmt.Sprintf("Are you sure you want to delete image %s", imageNames))
|
||||
if !yes {
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
return image.ImageDelete(cdp)
|
||||
|
||||
for _, imageName := range imageNames {
|
||||
if err := image.Delete(imageName); err != nil {
|
||||
return fmt.Errorf("error deleting image %s: %s", imageName, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,32 +1,90 @@
|
||||
package imprt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/containers/image/v5/types"
|
||||
"github.com/spf13/cobra"
|
||||
apiimage "github.com/warewulf/warewulf/internal/pkg/api/image"
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/warewulf/warewulf/internal/pkg/image"
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
source := args[0]
|
||||
|
||||
// Shim in a name if none given.
|
||||
name := ""
|
||||
if len(args) == 2 {
|
||||
name = args[1]
|
||||
}
|
||||
|
||||
cip := &wwapiv1.ImageImportParameter{
|
||||
Source: args[0],
|
||||
Name: name,
|
||||
Force: SetForce,
|
||||
Update: SetUpdate,
|
||||
Build: SetBuild,
|
||||
SyncUser: SyncUser,
|
||||
OciNoHttps: OciNoHttps,
|
||||
OciUsername: OciUsername,
|
||||
OciPassword: OciPassword,
|
||||
Platform: Platform,
|
||||
if name == "" {
|
||||
name = path.Base(source)
|
||||
wwlog.Info("Setting image name: %s", name)
|
||||
}
|
||||
if !image.ValidName(name) {
|
||||
return fmt.Errorf("image name contains illegal characters: %s", name)
|
||||
}
|
||||
|
||||
_, err = apiimage.ImageImport(cip)
|
||||
return
|
||||
fullPath := image.SourceDir(name)
|
||||
|
||||
// image already exists and should be removed first
|
||||
if util.IsDir(fullPath) {
|
||||
if SetUpdate {
|
||||
wwlog.Info("Updating existing image")
|
||||
} else if SetForce {
|
||||
wwlog.Info("Overwriting existing image")
|
||||
if err := os.RemoveAll(fullPath); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("image name exists, specify --force, --update, or choose a different name: %s", name)
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(source, "docker://") || strings.HasPrefix(source, "docker-daemon://") ||
|
||||
strings.HasPrefix(source, "file://") || util.IsFile(source) {
|
||||
var sCtx *types.SystemContext
|
||||
sCtx, err := image.GetSystemContext(OciNoHttps, OciUsername, OciPassword, Platform)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if util.IsFile(source) && !filepath.IsAbs(source) {
|
||||
source, err = filepath.Abs(source)
|
||||
if err != nil {
|
||||
return fmt.Errorf("when resolving absolute path of %s, err: %v", source, err)
|
||||
}
|
||||
}
|
||||
err = image.ImportDocker(source, name, sCtx)
|
||||
if err != nil {
|
||||
_ = image.DeleteSource(name)
|
||||
return fmt.Errorf("could not import image: %s", err.Error())
|
||||
}
|
||||
} else if util.IsDir(source) {
|
||||
if err := image.ImportDirectory(source, name); err != nil {
|
||||
_ = image.DeleteSource(name)
|
||||
return fmt.Errorf("could not import image: %s", err.Error())
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("invalid dir or uri: %s", source)
|
||||
}
|
||||
|
||||
if SyncUser {
|
||||
if err := image.Syncuser(name, true); err != nil {
|
||||
return fmt.Errorf("syncuser error: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if SetBuild {
|
||||
wwlog.Info("Building image: %s", name)
|
||||
if err := image.Build(name, true); err != nil {
|
||||
return fmt.Errorf("could not build image %s: %s", name, err.Error())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,54 +1,77 @@
|
||||
package list
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/warewulf/warewulf/internal/app/wwctl/table"
|
||||
apiimage "github.com/warewulf/warewulf/internal/pkg/api/image"
|
||||
"github.com/warewulf/warewulf/internal/pkg/image"
|
||||
"github.com/warewulf/warewulf/internal/pkg/kernel"
|
||||
"github.com/warewulf/warewulf/internal/pkg/node"
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
)
|
||||
|
||||
var imageList = apiimage.ImageList
|
||||
|
||||
func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err error) {
|
||||
return func(cmd *cobra.Command, args []string) (err error) {
|
||||
t := table.New(cmd.OutOrStdout())
|
||||
showSize := vars.size || vars.chroot || vars.compressed
|
||||
if showSize || vars.full || vars.kernel {
|
||||
imageInfo, err := imageList()
|
||||
sources, err := image.ListSources()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
nodeDB, err := node.New()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nodes, err := nodeDB.FindAllNodes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nodemap := make(map[string]int)
|
||||
for _, n := range nodes {
|
||||
nodemap[n.ImageName]++
|
||||
}
|
||||
|
||||
if vars.full {
|
||||
t.AddHeader("IMAGE NAME", "NODES", "KERNEL VERSION", "CREATION TIME", "MODIFICATION TIME", "SIZE")
|
||||
for i := 0; i < len(imageInfo); i++ {
|
||||
if len(args) > 0 && !util.InSlice(args, imageInfo[i].Name) {
|
||||
for _, name := range sources {
|
||||
if len(args) > 0 && !util.InSlice(args, name) {
|
||||
continue
|
||||
}
|
||||
createTime := time.Unix(int64(imageInfo[i].CreateDate), 0)
|
||||
modTime := time.Unix(int64(imageInfo[i].ModDate), 0)
|
||||
sz := util.ByteToString(int64(imageInfo[i].ImgSize))
|
||||
kernelVersion := ""
|
||||
if k := kernel.FindKernels(name).Default(); k != nil {
|
||||
kernelVersion = k.Version()
|
||||
}
|
||||
createTime := time.Unix(0, 0)
|
||||
if sourceStat, err := os.Stat(image.SourceDir(name)); err == nil {
|
||||
createTime = sourceStat.ModTime()
|
||||
}
|
||||
modTime := time.Unix(0, 0)
|
||||
if imageStat, err := os.Stat(image.ImageFile(name)); err == nil {
|
||||
modTime = imageStat.ModTime()
|
||||
}
|
||||
sz := util.ByteToString(int64(image.ImageSize(name)))
|
||||
if vars.compressed {
|
||||
sz = util.ByteToString(int64(imageInfo[i].ImgSizeComp))
|
||||
sz = util.ByteToString(int64(image.CompressedImageSize(name)))
|
||||
}
|
||||
if vars.chroot {
|
||||
size, err := util.DirSize(image.SourceDir(imageInfo[i].Name))
|
||||
size, err := util.DirSize(image.SourceDir(name))
|
||||
if err != nil {
|
||||
wwlog.Error("%s", err)
|
||||
size = int64(0)
|
||||
size = 0
|
||||
}
|
||||
sz = util.ByteToString(int64(size))
|
||||
sz = util.ByteToString(size)
|
||||
}
|
||||
t.AddLine(
|
||||
imageInfo[i].Name,
|
||||
strconv.FormatUint(uint64(imageInfo[i].NodeCount), 10),
|
||||
imageInfo[i].KernelVersion,
|
||||
name,
|
||||
strconv.Itoa(nodemap[name]),
|
||||
kernelVersion,
|
||||
createTime.Format(time.RFC822),
|
||||
modTime.Format(time.RFC822),
|
||||
sz,
|
||||
@@ -56,38 +79,41 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
|
||||
}
|
||||
} else if vars.kernel {
|
||||
t.AddHeader("IMAGE NAME", "NODES", "KERNEL VERSION")
|
||||
for i := 0; i < len(imageInfo); i++ {
|
||||
if len(args) > 0 && !util.InSlice(args, imageInfo[i].Name) {
|
||||
for _, name := range sources {
|
||||
if len(args) > 0 && !util.InSlice(args, name) {
|
||||
continue
|
||||
}
|
||||
kernelVersion := ""
|
||||
if k := kernel.FindKernels(name).Default(); k != nil {
|
||||
kernelVersion = k.Version()
|
||||
}
|
||||
t.AddLine(
|
||||
imageInfo[i].Name,
|
||||
strconv.FormatUint(uint64(imageInfo[i].NodeCount), 10),
|
||||
imageInfo[i].KernelVersion,
|
||||
name,
|
||||
strconv.Itoa(nodemap[name]),
|
||||
kernelVersion,
|
||||
)
|
||||
}
|
||||
} else if showSize {
|
||||
t.AddHeader("IMAGE NAME", "NODES", "SIZE")
|
||||
for i := 0; i < len(imageInfo); i++ {
|
||||
if len(args) > 0 && !util.InSlice(args, imageInfo[i].Name) {
|
||||
for _, name := range sources {
|
||||
if len(args) > 0 && !util.InSlice(args, name) {
|
||||
continue
|
||||
}
|
||||
sz := util.ByteToString(int64(imageInfo[i].ImgSize))
|
||||
sz := util.ByteToString(int64(image.ImageSize(name)))
|
||||
if vars.compressed {
|
||||
sz = util.ByteToString(int64(imageInfo[i].ImgSizeComp))
|
||||
sz = util.ByteToString(int64(image.CompressedImageSize(name)))
|
||||
}
|
||||
if vars.chroot {
|
||||
size, err := util.DirSize(image.SourceDir(imageInfo[i].Name))
|
||||
size, err := util.DirSize(image.SourceDir(name))
|
||||
if err != nil {
|
||||
wwlog.Error("%s", err)
|
||||
size = int64(0)
|
||||
size = 0
|
||||
}
|
||||
sz = util.ByteToString(size)
|
||||
}
|
||||
|
||||
t.AddLine(
|
||||
imageInfo[i].Name,
|
||||
strconv.FormatUint(uint64(imageInfo[i].NodeCount), 10),
|
||||
name,
|
||||
strconv.Itoa(nodemap[name]),
|
||||
sz,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -4,63 +4,47 @@ import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/warewulf/warewulf/internal/pkg/testenv"
|
||||
"github.com/warewulf/warewulf/internal/pkg/warewulfd"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
)
|
||||
|
||||
func Test_List(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
stdout string
|
||||
inDb string
|
||||
mockFunc func()
|
||||
name string
|
||||
args []string
|
||||
stdout string
|
||||
inDb string
|
||||
}{
|
||||
{
|
||||
name: "image list test",
|
||||
args: []string{"-l"},
|
||||
args: []string{"-s"},
|
||||
stdout: `
|
||||
IMAGE NAME NODES KERNEL VERSION CREATION TIME MODIFICATION TIME SIZE
|
||||
---------- ----- -------------- ------------- ----------------- ----
|
||||
test 1 kernel 01 Jan 70 00:00 UTC 01 Jan 70 00:00 UTC 0 B
|
||||
IMAGE NAME NODES SIZE
|
||||
---------- ----- ----
|
||||
test 1 0 B
|
||||
`,
|
||||
inDb: `
|
||||
nodeprofiles:
|
||||
default: {}
|
||||
nodes:
|
||||
n01:
|
||||
image name: test
|
||||
profiles:
|
||||
- default
|
||||
`,
|
||||
mockFunc: func() {
|
||||
imageList = func() (imageInfo []*wwapiv1.ImageInfo, err error) {
|
||||
imageInfo = append(imageInfo, &wwapiv1.ImageInfo{
|
||||
Name: "test",
|
||||
NodeCount: 1,
|
||||
KernelVersion: "kernel",
|
||||
CreateDate: uint64(time.Unix(0, 0).Unix()),
|
||||
ModDate: uint64(time.Unix(0, 0).Unix()),
|
||||
})
|
||||
return
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
warewulfd.SetNoDaemon()
|
||||
for _, tt := range tests {
|
||||
env := testenv.New(t)
|
||||
defer env.RemoveAll()
|
||||
env.WriteFile("etc/warewulf/nodes.conf", tt.inDb)
|
||||
env.MkdirAll("var/lib/warewulf/chroots/test/rootfs")
|
||||
|
||||
t.Logf("Running test: %s\n", tt.name)
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.mockFunc()
|
||||
buf := new(bytes.Buffer)
|
||||
baseCmd := GetCommand()
|
||||
baseCmd.SetArgs(tt.args)
|
||||
|
||||
@@ -4,37 +4,34 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
api "github.com/warewulf/warewulf/internal/pkg/api/image"
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/warewulf/warewulf/internal/pkg/image"
|
||||
"github.com/warewulf/warewulf/internal/pkg/warewulfd"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
crp := &wwapiv1.ImageRenameParameter{
|
||||
ImageName: args[0],
|
||||
TargetName: args[1],
|
||||
Build: SetBuild,
|
||||
if !image.DoesSourceExist(args[0]) {
|
||||
return fmt.Errorf("%s source dir does not exist", args[0])
|
||||
}
|
||||
|
||||
if !image.DoesSourceExist(crp.ImageName) {
|
||||
return fmt.Errorf("%s source dir does not exist", crp.ImageName)
|
||||
if image.DoesSourceExist(args[1]) {
|
||||
return fmt.Errorf("an other image with the name %s already exists", args[1])
|
||||
}
|
||||
|
||||
if image.DoesSourceExist(crp.TargetName) {
|
||||
return fmt.Errorf("an other image with the name %s already exists", crp.TargetName)
|
||||
if !image.ValidName(args[1]) {
|
||||
return fmt.Errorf("image name contains illegal characters: %s", args[1])
|
||||
}
|
||||
|
||||
if !image.ValidName(crp.TargetName) {
|
||||
return fmt.Errorf("image name contains illegal characters : %s", crp.TargetName)
|
||||
}
|
||||
|
||||
err = api.ImageRename(crp)
|
||||
err = image.Rename(args[0], args[1], SetBuild)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not rename image: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
wwlog.Info("Image %s successfully renamed as %s", crp.ImageName, crp.TargetName)
|
||||
return
|
||||
wwlog.Info("Image %s successfully renamed as %s", args[0], args[1])
|
||||
|
||||
if err = warewulfd.DaemonStatus(); err != nil {
|
||||
return nil
|
||||
}
|
||||
return warewulfd.DaemonReload()
|
||||
}
|
||||
|
||||
@@ -3,35 +3,61 @@ package show
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/image"
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/warewulf/warewulf/internal/pkg/image"
|
||||
"github.com/warewulf/warewulf/internal/pkg/kernel"
|
||||
"github.com/warewulf/warewulf/internal/pkg/node"
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
csp := &wwapiv1.ImageShowParameter{
|
||||
ImageName: args[0],
|
||||
func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
|
||||
imageName := args[0]
|
||||
|
||||
if !image.ValidName(imageName) {
|
||||
return fmt.Errorf("%s is not a valid image name", imageName)
|
||||
}
|
||||
|
||||
var r *wwapiv1.ImageShowResponse
|
||||
r, err = image.ImageShow(csp)
|
||||
rootFsDir := image.RootFsDir(imageName)
|
||||
if !util.IsDir(rootFsDir) {
|
||||
return fmt.Errorf("%s is not a valid image", imageName)
|
||||
}
|
||||
kernel := kernel.FindKernels(imageName).Default()
|
||||
kernelVersion := ""
|
||||
if kernel != nil {
|
||||
kernelVersion = kernel.Version()
|
||||
}
|
||||
|
||||
nodeDB, err := node.New()
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
nodes, err := nodeDB.FindAllNodes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var nodeList []string
|
||||
for _, n := range nodes {
|
||||
if n.ImageName == imageName {
|
||||
nodeList = append(nodeList, n.Id())
|
||||
}
|
||||
}
|
||||
|
||||
if !ShowAll {
|
||||
fmt.Printf("%s\n", r.Rootfs)
|
||||
fmt.Printf("%s\n", rootFsDir)
|
||||
} else {
|
||||
kernelVersion := r.KernelVersion
|
||||
if kernelVersion == "" {
|
||||
kernelVersion = "not found"
|
||||
}
|
||||
fmt.Printf("Name: %s\n", r.Name)
|
||||
fmt.Printf("Name: %s\n", imageName)
|
||||
fmt.Printf("KernelVersion: %s\n", kernelVersion)
|
||||
fmt.Printf("Rootfs: %s\n", r.Rootfs)
|
||||
fmt.Printf("Nr nodes: %d\n", len(r.Nodes))
|
||||
fmt.Printf("Nodes: %s\n", r.Nodes)
|
||||
fmt.Printf("Rootfs: %s\n", rootFsDir)
|
||||
fmt.Printf("Nr nodes: %d\n", len(nodeList))
|
||||
fmt.Printf("Nodes: %v\n", nodeList)
|
||||
}
|
||||
return
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
image_build "github.com/warewulf/warewulf/internal/pkg/api/image"
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/warewulf/warewulf/internal/pkg/image"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
)
|
||||
@@ -25,13 +23,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
wwlog.Info("Syncuser is completed. Rebuild image or add `--build` flag for automatic rebuild after syncuser.")
|
||||
} else if write && build {
|
||||
// if write = true and build = true, then it'll trigger the image build after sync
|
||||
cbp := &wwapiv1.ImageBuildParameter{
|
||||
ImageNames: []string{imageName},
|
||||
Force: true,
|
||||
All: false,
|
||||
}
|
||||
err := image_build.ImageBuild(cbp)
|
||||
if err != nil {
|
||||
if err := image.Build(imageName, true); err != nil {
|
||||
return fmt.Errorf("error during image build: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user