Skip to content
Snippets Groups Projects
Commit 4107d267 authored by Florent Gluck's avatar Florent Gluck
Browse files

Added 2 commands:

vmlistexportdir     List VMs that can have a directory exported (regex matching).
vmexportdir         Export one or more VMs' directory into one or more tar archives. Create one archive per VM (regex matching).
parent 50e2da64
No related branches found
No related tags found
No related merge requests found
Showing
with 152 additions and 37 deletions
...@@ -47,6 +47,7 @@ List of supported Commands: ...@@ -47,6 +47,7 @@ List of supported Commands:
vmlistedit List VMs that can be edited (regex matching). vmlistedit List VMs that can be edited (regex matching).
vmlisteditaccess List VMs that can have their VM access edited (regex matching). vmlisteditaccess List VMs that can have their VM access edited (regex matching).
vmlistdel List VMs that can be deleted (regex matching). vmlistdel List VMs that can be deleted (regex matching).
vmlistexportdir List VMs that can have a directory exported (regex matching).
vmcred2pdf Create a PDF with the credentials required to attach to running VMs (regex matching). vmcred2pdf Create a PDF with the credentials required to attach to running VMs (regex matching).
vmstart Start one or more VMs (regex matching). vmstart Start one or more VMs (regex matching).
vmstop Stop one or more VMs (regex matching). vmstop Stop one or more VMs (regex matching).
...@@ -56,6 +57,7 @@ List of supported Commands: ...@@ -56,6 +57,7 @@ List of supported Commands:
vmdel Delete one or more VMs (regex matching). vmdel Delete one or more VMs (regex matching).
vmsetaccess Set a user's VM access in one or more VMs (regex matching). vmsetaccess Set a user's VM access in one or more VMs (regex matching).
vmdelaccess Delete a user's VM access in one or more VMs (regex matching). vmdelaccess Delete a user's VM access in one or more VMs (regex matching).
vmexportdir Export one or more VMs' directory into one or more tar archives. Create one archive per VM (regex matching).
tpllist List available templates (regex matching). tpllist List available templates (regex matching).
tplcreate Create a template, either from an existing VM or from a .qcow file. tplcreate Create a template, either from an existing VM or from a .qcow file.
tpldel Delete one or more templates (regex matching). tpldel Delete one or more templates (regex matching).
...@@ -273,6 +275,11 @@ Generate `exam_vms.pdf` with the credentials required to connect to all running ...@@ -273,6 +275,11 @@ Generate `exam_vms.pdf` with the credentials required to connect to all running
nexus-client vmcred2pdf "exam prog sys" output.pdf nexus-client vmcred2pdf "exam prog sys" output.pdf
``` ```
Extract and download the `/home` directory of all VMs matching "exam prog sys" (each directory is saved in a `.tar` archive named after the VM's ID):
```
nexus-client vmlistexportdir "exam prog sys" /home
```
List all available templates: List all available templates:
``` ```
nexus-client tpllist . nexus-client tpllist .
......
...@@ -52,17 +52,13 @@ func (vm *VM)String() string { ...@@ -52,17 +52,13 @@ func (vm *VM)String() string {
return string(output) return string(output)
} }
func printUsage(c cmd.Command, action string, showLongOutputFlag bool) { func printRegexUsage(c cmd.Command) {
u.PrintlnErr(c.GetDesc()) u.PrintlnErr(c.GetDesc())
longOutputFlag := "" u.PrintlnErr("Usage: ",c.GetName()," [ID ...] [regex ...]")
if showLongOutputFlag {
longOutputFlag = " [-l]"
} }
u.PrintlnErr("Usage: ",c.GetName(),longOutputFlag+" [ID ...] [regex ...]")
if showLongOutputFlag { func printRegexUsageDetails() {
u.PrintlnErr("Use \"-l\" to specify detailed VMs output.") u.PrintlnErr("The action only applies to VMs matching the specified IDs or regexes.")
}
u.PrintlnErr("Only VMs matching the specified IDs or regexes will be "+action+".")
const usage string = `Any number of IDs or regexes can be specified. const usage string = `Any number of IDs or regexes can be specified.
The regex only matches the VM's name and is case-insensitive. The regex only matches the VM's name and is case-insensitive.
Regex examples: Regex examples:
......
...@@ -27,7 +27,8 @@ func (cmd *Attach)GetDesc() string { ...@@ -27,7 +27,8 @@ func (cmd *Attach)GetDesc() string {
} }
func (cmd *Attach)PrintUsage() { func (cmd *Attach)PrintUsage() {
printUsage(cmd, "attached to", false) printRegexUsage(cmd)
printRegexUsageDetails()
} }
func (cmd *Attach)Run(args []string) int { func (cmd *Attach)Run(args []string) int {
...@@ -52,7 +53,7 @@ func (cmd *Attach)Run(args []string) int { ...@@ -52,7 +53,7 @@ func (cmd *Attach)Run(args []string) int {
} }
if len(vms) == 0 { if len(vms) == 0 {
u.PrintlnErr("No VMs to attach to!") u.PrintlnErr("No match.")
return 1 return 1
} }
......
...@@ -21,14 +21,7 @@ func (cmd *Cred2pdf)GetDesc() string { ...@@ -21,14 +21,7 @@ func (cmd *Cred2pdf)GetDesc() string {
func (cmd *Cred2pdf)PrintUsage() { func (cmd *Cred2pdf)PrintUsage() {
u.PrintlnErr(cmd.GetDesc()) u.PrintlnErr(cmd.GetDesc())
u.PrintlnErr("Usage: ",cmd.GetName()," [ID ...] [regex ...] pdfile") u.PrintlnErr("Usage: ",cmd.GetName()," [ID ...] [regex ...] pdfile")
u.PrintlnErr("Only VMs that can be attached to and matching the specified IDs or regexes will be listed.") printRegexUsageDetails()
const usage string = `Any number of IDs or regexes can be specified.
The regex only matches the VM's name and is case-insensitive.
Regex examples:
"" -> matches any VMs
"." -> matches any VMs
"bla" -> matches any VMs containing "bla" in their name`
u.PrintlnErr(usage)
} }
func (cmd *Cred2pdf)Run(args []string) int { func (cmd *Cred2pdf)Run(args []string) int {
...@@ -47,7 +40,7 @@ func (cmd *Cred2pdf)Run(args []string) int { ...@@ -47,7 +40,7 @@ func (cmd *Cred2pdf)Run(args []string) int {
} }
if len(vms) == 0 { if len(vms) == 0 {
u.PrintlnErr("No VMs to retrieve credentials from!") u.PrintlnErr("No match.")
return 1 return 1
} }
......
...@@ -18,7 +18,8 @@ func (cmd *Del)GetDesc() string { ...@@ -18,7 +18,8 @@ func (cmd *Del)GetDesc() string {
} }
func (cmd *Del)PrintUsage() { func (cmd *Del)PrintUsage() {
printUsage(cmd, "deleted", false) printRegexUsage(cmd)
printRegexUsageDetails()
} }
func (cmd *Del)Run(args []string) int { func (cmd *Del)Run(args []string) int {
...@@ -38,7 +39,7 @@ func (cmd *Del)Run(args []string) int { ...@@ -38,7 +39,7 @@ func (cmd *Del)Run(args []string) int {
} }
if len(vms) == 0 { if len(vms) == 0 {
u.PrintlnErr("No VMs to delete!") u.PrintlnErr("No match.")
return 1 return 1
} }
......
...@@ -59,7 +59,7 @@ func (cmd *DelAccess)Run(args []string) int { ...@@ -59,7 +59,7 @@ func (cmd *DelAccess)Run(args []string) int {
} }
if len(vms) == 0 { if len(vms) == 0 {
u.PrintlnErr("No VMs to delete user's VM access!") u.PrintlnErr("No match.")
return 1 return 1
} }
......
...@@ -72,7 +72,7 @@ func (cmd *Edit)Run(args []string) int { ...@@ -72,7 +72,7 @@ func (cmd *Edit)Run(args []string) int {
} }
if len(vms) == 0 { if len(vms) == 0 {
u.PrintlnErr("No VMs to edit!") u.PrintlnErr("No match.")
return 1 return 1
} }
......
package cmdVM
import (
u "nexus-client/utils"
g "nexus-client/globals"
)
type ExportDir struct {
Name string
}
type vmExportDirParams struct {
Dir string
}
func (cmd *ExportDir)GetName() string {
return cmd.Name
}
func (cmd *ExportDir)GetDesc() string {
return "Export one or more VMs' directory into one or more tar archives. Create one archive per VM (regex matching)."
}
func (cmd *ExportDir)PrintUsage() {
u.PrintlnErr(cmd.GetDesc())
u.PrintlnErr("Usage: ",cmd.GetName()," [ID ...] [regex ...] dir")
u.PrintlnErr("\"dir\" is the directory in the VM to export into a tar archive named after the VM ID.")
printRegexUsageDetails()
}
func (cmd *ExportDir)Run(args []string) int {
client := g.GetInstance().Client
host := g.GetInstance().Host
argc := len(args)
if argc < 2 {
cmd.PrintUsage()
return 1
}
dir := args[argc-1]
vms, err := getFilteredVMs("/vms/exportdir", args[:argc-1])
if err != nil {
u.PrintlnErr("Error: "+err.Error())
return 1
}
if len(vms) == 0 {
u.PrintlnErr("No match.")
return 1
}
params := &vmExportDirParams { Dir: dir }
statusCode := 0
client.SetAllowGetMethodPayload(true)
for _, vm := range(vms) {
uuid := vm.ID.String()
outputFile := uuid+".tar"
resp, err := client.R().SetOutput(outputFile).SetBody(params).Get(host+"/vms/"+uuid+"/exportdir")
if err != nil {
u.PrintlnErr("Failed exporting "+dir+" from VM \""+vm.Name+"\" | "+uuid+" : "+err.Error())
statusCode = 1
} else {
if resp.IsSuccess() {
u.Println("Successfully exported "+dir+" from VM \""+vm.Name+"\" ("+uuid+") into "+outputFile)
} else {
u.PrintlnErr("Failed exporting "+dir+" from VM \""+vm.Name+"\" | "+uuid+" : "+resp.Status()+": "+resp.String())
statusCode = 1
}
}
}
return statusCode
}
package cmdVM package cmdVM
import (
u "nexus-client/utils"
)
type List struct { type List struct {
Name string Name string
} }
...@@ -13,7 +17,10 @@ func (cmd *List)GetDesc() string { ...@@ -13,7 +17,10 @@ func (cmd *List)GetDesc() string {
} }
func (cmd *List)PrintUsage() { func (cmd *List)PrintUsage() {
printUsage(cmd, "listed", true) u.PrintlnErr(cmd.GetDesc())
u.PrintlnErr("Usage: ",cmd.GetName(), " [-l] [ID ...] [regex ...]")
u.PrintlnErr("Use \"-l\" to specify detailed VMs output.")
printRegexUsageDetails()
} }
func (cmd *List)Run(args []string) int { func (cmd *List)Run(args []string) int {
......
...@@ -13,7 +13,8 @@ func (cmd *ListAttach)GetDesc() string { ...@@ -13,7 +13,8 @@ func (cmd *ListAttach)GetDesc() string {
} }
func (cmd *ListAttach)PrintUsage() { func (cmd *ListAttach)PrintUsage() {
printUsage(cmd, "listed", false) printRegexUsage(cmd)
printRegexUsageDetails()
} }
func (cmd *ListAttach)Run(args []string) int { func (cmd *ListAttach)Run(args []string) int {
......
...@@ -13,7 +13,8 @@ func (cmd *ListDel)GetDesc() string { ...@@ -13,7 +13,8 @@ func (cmd *ListDel)GetDesc() string {
} }
func (cmd *ListDel)PrintUsage() { func (cmd *ListDel)PrintUsage() {
printUsage(cmd, "listed", false) printRegexUsage(cmd)
printRegexUsageDetails()
} }
func (cmd *ListDel)Run(args []string) int { func (cmd *ListDel)Run(args []string) int {
......
...@@ -13,7 +13,8 @@ func (cmd *ListEdit)GetDesc() string { ...@@ -13,7 +13,8 @@ func (cmd *ListEdit)GetDesc() string {
} }
func (cmd *ListEdit)PrintUsage() { func (cmd *ListEdit)PrintUsage() {
printUsage(cmd, "listed", false) printRegexUsage(cmd)
printRegexUsageDetails()
} }
func (cmd *ListEdit)Run(args []string) int { func (cmd *ListEdit)Run(args []string) int {
......
...@@ -13,7 +13,8 @@ func (cmd *ListEditAccess)GetDesc() string { ...@@ -13,7 +13,8 @@ func (cmd *ListEditAccess)GetDesc() string {
} }
func (cmd *ListEditAccess)PrintUsage() { func (cmd *ListEditAccess)PrintUsage() {
printUsage(cmd, "listed", false) printRegexUsage(cmd)
printRegexUsageDetails()
} }
func (cmd *ListEditAccess)Run(args []string) int { func (cmd *ListEditAccess)Run(args []string) int {
......
package cmdVM
type ListExportDir struct{
Name string
}
func (cmd *ListExportDir)GetName() string {
return cmd.Name
}
func (cmd *ListExportDir)GetDesc() string {
return "List VMs that can have a directory exported (regex matching)."
}
func (cmd *ListExportDir)PrintUsage() {
printRegexUsage(cmd)
printRegexUsageDetails()
}
func (cmd *ListExportDir)Run(args []string) int {
return printFilteredVMs(cmd, args, "/vms/exportdir")
}
...@@ -13,7 +13,8 @@ func (cmd *ListStart)GetDesc() string { ...@@ -13,7 +13,8 @@ func (cmd *ListStart)GetDesc() string {
} }
func (cmd *ListStart)PrintUsage() { func (cmd *ListStart)PrintUsage() {
printUsage(cmd, "listed", false) printRegexUsage(cmd)
printRegexUsageDetails()
} }
func (cmd *ListStart)Run(args []string) int { func (cmd *ListStart)Run(args []string) int {
......
...@@ -13,7 +13,8 @@ func (cmd *ListStop)GetDesc() string { ...@@ -13,7 +13,8 @@ func (cmd *ListStop)GetDesc() string {
} }
func (cmd *ListStop)PrintUsage() { func (cmd *ListStop)PrintUsage() {
printUsage(cmd, "listed", false) printRegexUsage(cmd)
printRegexUsageDetails()
} }
func (cmd *ListStop)Run(args []string) int { func (cmd *ListStop)Run(args []string) int {
......
...@@ -56,7 +56,7 @@ func (cmd *SetAccess)Run(args []string) int { ...@@ -56,7 +56,7 @@ func (cmd *SetAccess)Run(args []string) int {
} }
if len(vms) == 0 { if len(vms) == 0 {
u.PrintlnErr("No VMs to set user's VM access!") u.PrintlnErr("No match.")
return 1 return 1
} }
......
...@@ -18,7 +18,8 @@ func (cmd *Start)GetDesc() string { ...@@ -18,7 +18,8 @@ func (cmd *Start)GetDesc() string {
} }
func (cmd *Start)PrintUsage() { func (cmd *Start)PrintUsage() {
printUsage(cmd, "started", false) printRegexUsage(cmd)
printRegexUsageDetails()
} }
func (cmd *Start)Run(args []string) int { func (cmd *Start)Run(args []string) int {
...@@ -38,7 +39,7 @@ func (cmd *Start)Run(args []string) int { ...@@ -38,7 +39,7 @@ func (cmd *Start)Run(args []string) int {
} }
if len(vms) == 0 { if len(vms) == 0 {
u.PrintlnErr("No VMs to start!") u.PrintlnErr("No match.")
return 1 return 1
} }
......
...@@ -18,7 +18,8 @@ func (cmd *Stop)GetDesc() string { ...@@ -18,7 +18,8 @@ func (cmd *Stop)GetDesc() string {
} }
func (cmd *Stop)PrintUsage() { func (cmd *Stop)PrintUsage() {
printUsage(cmd, "stopped", false) printRegexUsage(cmd)
printRegexUsageDetails()
} }
func (cmd *Stop)Run(args []string) int { func (cmd *Stop)Run(args []string) int {
...@@ -38,7 +39,7 @@ func (cmd *Stop)Run(args []string) int { ...@@ -38,7 +39,7 @@ func (cmd *Stop)Run(args []string) int {
} }
if len(vms) == 0 { if len(vms) == 0 {
u.PrintlnErr("No VMs to stop!") u.PrintlnErr("No match.")
return 1 return 1
} }
......
...@@ -31,6 +31,7 @@ var cmdList = []cmd.Command { ...@@ -31,6 +31,7 @@ var cmdList = []cmd.Command {
&cmdVM.ListEdit{"vmlistedit"}, &cmdVM.ListEdit{"vmlistedit"},
&cmdVM.ListEditAccess{"vmlisteditaccess"}, &cmdVM.ListEditAccess{"vmlisteditaccess"},
&cmdVM.ListDel{"vmlistdel"}, &cmdVM.ListDel{"vmlistdel"},
&cmdVM.ListExportDir{"vmlistexportdir"},
&cmdVM.Cred2pdf{"vmcred2pdf"}, &cmdVM.Cred2pdf{"vmcred2pdf"},
...@@ -42,6 +43,7 @@ var cmdList = []cmd.Command { ...@@ -42,6 +43,7 @@ var cmdList = []cmd.Command {
&cmdVM.Del{"vmdel"}, &cmdVM.Del{"vmdel"},
&cmdVM.SetAccess{"vmsetaccess"}, &cmdVM.SetAccess{"vmsetaccess"},
&cmdVM.DelAccess{"vmdelaccess"}, &cmdVM.DelAccess{"vmdelaccess"},
&cmdVM.ExportDir{"vmexportdir"},
&cmdTemplate.List{"tpllist"}, &cmdTemplate.List{"tpllist"},
&cmdTemplate.Create{"tplcreate"}, &cmdTemplate.Create{"tplcreate"},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment