Rename "container" to "image"
- Updated `wwctl upgrade` to handle updates - Maintained `.Container` and `.ContainerName` in tstruct - Added `ContainerName()` methods to node and profile objects - Added `--container`, `-C` aliases to wwctl commands (`<node|profile> <add|set>`) - Added `wwctl container` alias - Added support for `container_exit.sh` if `image_exit.sh` is not found Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
44
internal/app/wwctl/image/rename/main.go
Normal file
44
internal/app/wwctl/image/rename/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package rename
|
||||
|
||||
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/wwlog"
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
if len(args) != 2 {
|
||||
return fmt.Errorf("rename requires 2 arguments: %d provided", len(args))
|
||||
}
|
||||
|
||||
crp := &wwapiv1.ImageRenameParameter{
|
||||
ImageName: args[0],
|
||||
TargetName: args[1],
|
||||
Build: SetBuild,
|
||||
}
|
||||
|
||||
if !image.DoesSourceExist(crp.ImageName) {
|
||||
return fmt.Errorf("%s source dir does not exist", crp.ImageName)
|
||||
}
|
||||
|
||||
if image.DoesSourceExist(crp.TargetName) {
|
||||
return fmt.Errorf("an other image with the name %s already exists", crp.TargetName)
|
||||
}
|
||||
|
||||
if !image.ValidName(crp.TargetName) {
|
||||
return fmt.Errorf("image name contains illegal characters : %s", crp.TargetName)
|
||||
}
|
||||
|
||||
err = api.ImageRename(crp)
|
||||
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
|
||||
}
|
||||
55
internal/app/wwctl/image/rename/main_test.go
Normal file
55
internal/app/wwctl/image/rename/main_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package rename
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
imageList "github.com/warewulf/warewulf/internal/app/wwctl/image/list"
|
||||
"github.com/warewulf/warewulf/internal/pkg/testenv"
|
||||
"github.com/warewulf/warewulf/internal/pkg/warewulfd"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
)
|
||||
|
||||
func Test_Rename(t *testing.T) {
|
||||
env := testenv.New(t)
|
||||
env.WriteFile(path.Join(testenv.WWChrootdir, "test-image/rootfs/file"), `test`)
|
||||
defer env.RemoveAll()
|
||||
warewulfd.SetNoDaemon()
|
||||
|
||||
// first we will verify that there is an existing image
|
||||
t.Run("image list", func(t *testing.T) {
|
||||
verifyImageListOutput(t, "test-image")
|
||||
})
|
||||
|
||||
// then rename it
|
||||
t.Run("image rename", func(t *testing.T) {
|
||||
baseCmd := GetCommand()
|
||||
baseCmd.SetOut(os.Stdout)
|
||||
baseCmd.SetErr(os.Stdout)
|
||||
baseCmd.SetArgs([]string{"test-image", "test-image-rename"})
|
||||
err := baseCmd.Execute()
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
// retrieve again
|
||||
t.Run("Image list", func(t *testing.T) {
|
||||
verifyImageListOutput(t, "test-image-rename")
|
||||
})
|
||||
}
|
||||
|
||||
func verifyImageListOutput(t *testing.T, content string) {
|
||||
baseCmd := imageList.GetCommand()
|
||||
buf := new(bytes.Buffer)
|
||||
baseCmd.SetOut(buf)
|
||||
baseCmd.SetErr(buf)
|
||||
wwlog.SetLogWriterErr(buf)
|
||||
wwlog.SetLogWriterInfo(buf)
|
||||
err := baseCmd.Execute()
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.NotEmpty(t, buf.String(), "output should not be empty")
|
||||
assert.Contains(t, buf.String(), content)
|
||||
}
|
||||
34
internal/app/wwctl/image/rename/root.go
Normal file
34
internal/app/wwctl/image/rename/root.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package rename
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warewulf/warewulf/internal/pkg/image"
|
||||
)
|
||||
|
||||
var baseCmd = &cobra.Command{
|
||||
DisableFlagsInUseLine: true,
|
||||
Use: "rename IMAGE NEW_NAME",
|
||||
Aliases: []string{"mv"},
|
||||
Short: "Rename an existing image",
|
||||
Long: "This command will rename an existing image.",
|
||||
RunE: CobraRunE,
|
||||
Args: cobra.MinimumNArgs(2),
|
||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
if len(args) != 0 {
|
||||
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
list, _ := image.ListSources()
|
||||
return list, cobra.ShellCompDirectiveNoFileComp
|
||||
},
|
||||
}
|
||||
|
||||
var SetBuild bool
|
||||
|
||||
func init() {
|
||||
baseCmd.PersistentFlags().BoolVarP(&SetBuild, "build", "b", false, "Build image after rename")
|
||||
}
|
||||
|
||||
// GetRootCommand returns the root cobra.Command for the application.
|
||||
func GetCommand() *cobra.Command {
|
||||
return baseCmd
|
||||
}
|
||||
Reference in New Issue
Block a user