Core of image's duplication feature
This commit is contained in:
48
internal/app/wwctl/container/copy/main.go
Normal file
48
internal/app/wwctl/container/copy/main.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package copy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/hpcng/warewulf/internal/pkg/container"
|
||||
"github.com/hpcng/warewulf/internal/pkg/wwlog"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
|
||||
if len(args) > 2 {
|
||||
wwlog.Warn("cp only requires 2 arguments but you provided %d arguments. Hence, they will be ignored.", len(args))
|
||||
}
|
||||
|
||||
cdp := &wwapiv1.ContainerCopyParameter{
|
||||
ContainerSource: args[0],
|
||||
ContainerDestination: args[1],
|
||||
}
|
||||
|
||||
if !container.DoesSourceExist(cdp.ContainerSource) {
|
||||
wwlog.Error("Container's source doesn't exists: %s", cdp.ContainerSource)
|
||||
return
|
||||
}
|
||||
|
||||
if !container.ValidName(cdp.ContainerDestination) {
|
||||
wwlog.Error("Container name contains illegal characters : %s", cdp.ContainerDestination)
|
||||
return
|
||||
}
|
||||
|
||||
if container.DoesSourceExist(cdp.ContainerDestination) {
|
||||
wwlog.Error("An other container with name: %s already exists in sources.", cdp.ContainerDestination)
|
||||
return
|
||||
}
|
||||
|
||||
err = container.Duplicate(cdp.ContainerSource, cdp.ContainerDestination)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not duplicate image: %s", err.Error())
|
||||
wwlog.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
wwlog.Info("Container %s successfully duplicated as %s", cdp.ContainerSource, cdp.ContainerDestination)
|
||||
return
|
||||
|
||||
}
|
||||
33
internal/app/wwctl/container/copy/root.go
Normal file
33
internal/app/wwctl/container/copy/root.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package copy
|
||||
|
||||
import (
|
||||
"github.com/hpcng/warewulf/internal/pkg/container"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
baseCmd = &cobra.Command{
|
||||
DisableFlagsInUseLine: true,
|
||||
Use: "cp CONTAINER NEW_NAME",
|
||||
Short: "Copy an existing container",
|
||||
Long: "This command will duplicate an imported container 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, _ := container.ListSources()
|
||||
return list, cobra.ShellCompDirectiveNoFileComp
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Nothing to do here
|
||||
}
|
||||
|
||||
// GetRootCommand returns the root cobra.Command for the application.
|
||||
func GetCommand() *cobra.Command {
|
||||
return baseCmd
|
||||
}
|
||||
Reference in New Issue
Block a user