-
Florent Gluck authoredFlorent Gluck authored
vmImportDir.go 2.36 KiB
package cmdVM
import (
u "nexus-client/utils"
libclient "nexus-libclient/vm"
"os"
)
type ImportDir struct {
Name string
}
func (cmd *ImportDir) GetName() string {
return cmd.Name
}
func (cmd *ImportDir) GetDesc() []string {
return []string{
"Copies a local directory (or file) and all its content into one or more VMs.",
"If not the VM's owner: requires VM_WRITEFS VM access capability or VM_WRITEFS_ANY user capability."}
}
func (cmd *ImportDir) PrintUsage() {
for _, desc := range cmd.GetDesc() {
u.PrintlnErr(desc)
}
u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
u.PrintlnErr("USAGE: ", cmd.GetName(), " [ID ...] [regex ...] localDir vmDir")
u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
const usage string = `localDir Directory (or file) on the local filesystem to be copied into the VM.
vmdir Directory in the VM where localDir will be copied.
IMPORTANT: to avoid disk corruption, files can only be imported into non-running VMs.`
u.PrintlnErr(usage)
u.PrintlnErr("")
printRegexUsageDetails()
}
func (cmd *ImportDir) Run(args []string) int {
argc := len(args)
if argc < 3 {
cmd.PrintUsage()
return 1
}
vmDir := args[argc-1]
localDir := args[argc-2]
vms, err := getFilteredVMs(libclient.GetImportFileVMs, args[:argc-2])
if err != nil {
u.PrintlnErr(err)
return 1
}
if len(vms) == 0 {
u.PrintlnErr("Error: VM(s) not found.")
return 1
}
statusCode := 0
tmpTarGzFile, err := u.GetRandomTempFilename()
if err != nil {
u.PrintlnErr(err)
return 1
}
tmpTarGzFile += ".tar.gz"
defer os.Remove(tmpTarGzFile)
if err := u.TarGzDir(localDir, tmpTarGzFile); err != nil {
u.PrintlnErr(err)
return 1
}
for _, vm := range vms {
err := libclient.VMImportArchive(vm.ID.String(), vmDir, tmpTarGzFile)
if err != nil {
u.PrintlnErr("Failed copying \"" + localDir + "\" into \"" + vmDir + "\" in VM \"" + vm.Name + "\": " + err.Error())
statusCode = 1
} else {
u.Println("Copied \"" + localDir + "\" into \"" + vmDir + "\" in VM \"" + vm.Name + "\"")
}
}
return statusCode
}