diff --git a/Makefile b/Makefile index 1f3075449ec2c327b18ae2352ea32e2c5bf0998c..f2343e77fdafcada2d157ce7cd057216db13548c 100644 --- a/Makefile +++ b/Makefile @@ -270,7 +270,7 @@ copy_resources_client: @mkdir -p $(RESOURCES_DIR_CLIENT) @echo -n "$(SERVER)" > $(RESOURCES_DIR_CLIENT)/server ifdef CERT - @cp $(CERT) $(RESOURCES_DIR_CLIENT)/ + @cp $(CERT) $(RESOURCES_DIR_CLIENT)/$(CA_CERT_FILE) else @echo -n "" > $(RESOURCES_DIR_CLIENT)/$(CA_CERT_FILE) endif diff --git a/src/client/cmdVM/vmStartAttach.go b/src/client/cmdVM/vmStartAttach.go new file mode 100644 index 0000000000000000000000000000000000000000..619b15387ddc3d617fbe3ccbf0e4e3376bb17ab0 --- /dev/null +++ b/src/client/cmdVM/vmStartAttach.go @@ -0,0 +1,96 @@ +package cmdVM + +import ( + "nexus-client/exec" + g "nexus-client/globals" + u "nexus-client/utils" +) + +type StartAttach struct { + Name string +} + +func (cmd *StartAttach) GetName() string { + return cmd.Name +} + +func (cmd *StartAttach) GetDesc() []string { + return []string{ + "Starts one or more VMs and attaches them in order to use their desktop environment.", + "If not the VM's owner: requires VM_START VM access capability or VM_START_ANY user capability", + "and VM_ATTACH VM access capability or VM_ATTACH_ANY user capability."} +} + +func (cmd *StartAttach) PrintUsage() { + printRegexUsage(cmd) + printRegexUsageDetails() +} + +func (cmd *StartAttach) Run(args []string) int { + client := g.GetInstance().Client + host := g.GetInstance().Host + + argc := len(args) + if argc < 1 { + cmd.PrintUsage() + return 1 + } + + if err := exec.CheckRemoteViewer(); err != nil { + u.PrintlnErr(err.Error()) + return 1 + } + + vms, err := getFilteredVMs("/vms/start", args) + if err != nil { + u.PrintlnErr(err.Error()) + return 1 + } + + if len(vms) == 0 { + u.PrintlnErr("Error: VM(s) not found.") + return 1 + } + + statusCode := 0 + + for _, vm := range vms { + uuid := vm.ID.String() + resp, err := client.R().Put(host + "/vms/" + uuid + "/start") + if err != nil { + u.PrintlnErr("Failed starting VM \"" + vm.Name + "\": " + err.Error()) + statusCode = 1 + } else { + if resp.IsSuccess() { + u.Println("Started VM \"" + vm.Name + "\"") + } else { + u.PrintlnErr("Failed starting VM \"" + vm.Name + "\": " + resp.Status() + ": " + resp.String()) + statusCode = 1 + } + } + + } + + // at this point, the returned filtered credentials only works for VMs that started successfully + creds, err := getFilteredVMCredentials("/vms/attach", args) + if err != nil { + u.PrintlnErr(err.Error()) + return 1 + } + + if len(creds) == 0 { + u.PrintlnErr("Error: No VM(s) to attach.") + return 1 + } + + attachStatusCode, err := AttachToVMs(creds, false) + if err != nil { + u.PrintlnErr(err) + } + + returnCode := 0 + if statusCode != 0 || attachStatusCode != 0 { + returnCode = 1 + } + return returnCode +} diff --git a/src/client/nexus-cli/nexus-cli.go b/src/client/nexus-cli/nexus-cli.go index 179d11c99adc96c32c4e96182484f7f4cd464187..f220d9b37cdff084db710b0b84e5baa1dc4bbcf8 100644 --- a/src/client/nexus-cli/nexus-cli.go +++ b/src/client/nexus-cli/nexus-cli.go @@ -1,23 +1,23 @@ package main import ( - "os" - "path" - "strings" - "nexus-common/utils" - "nexus-common/buildversion" - u "nexus-client/utils" - g "nexus-client/globals" - "nexus-client/defaults" - "nexus-client/version" "nexus-client/cmd" - "nexus-client/cmdMisc" "nexus-client/cmdLogin" + "nexus-client/cmdMisc" + "nexus-client/cmdTemplate" "nexus-client/cmdToken" "nexus-client/cmdUser" "nexus-client/cmdVM" "nexus-client/cmdVersion" - "nexus-client/cmdTemplate" + "nexus-client/defaults" + g "nexus-client/globals" + u "nexus-client/utils" + "nexus-client/version" + "nexus-common/buildversion" + "nexus-common/utils" + "os" + "path" + "strings" "github.com/go-resty/resty/v2" ) @@ -67,6 +67,7 @@ var cmdList = []cmd.Command { &cmdVM.Reboot{"vmreboot"}, &cmdVM.Shutdown{"vmshutdown"}, &cmdVM.Start{"vmstart"}, + &cmdVM.StartAttach{"vmstartattach"}, &cmdVM.StartWithCreds{"vmstartwithcreds"}, } diff --git a/src/client/nexush/nexush.go b/src/client/nexush/nexush.go index e6e2787ebe404669061d480886abb25985fe5ec3..df13c0e3c0e6b9f49cec1fbeeac6b2154472e9dc 100644 --- a/src/client/nexush/nexush.go +++ b/src/client/nexush/nexush.go @@ -1,30 +1,30 @@ package main import ( - "os" - "io" - "fmt" - "path" "errors" - "strings" - "syscall" - "golang.org/x/term" - u "nexus-client/utils" - "nexus-common/utils" - "nexus-common/buildversion" - g "nexus-client/globals" - "nexus-client/defaults" - "nexus-client/version" + "fmt" + "io" "nexus-client/cmd" - "nexus-client/cmdVM" - "nexus-client/cmdMisc" - "nexus-client/cmdUser" "nexus-client/cmdLogin" + "nexus-client/cmdMisc" + "nexus-client/cmdTemplate" "nexus-client/cmdToken" + "nexus-client/cmdUser" + "nexus-client/cmdVM" "nexus-client/cmdVersion" - "nexus-client/cmdTemplate" - "github.com/peterh/liner" + "nexus-client/defaults" + g "nexus-client/globals" + u "nexus-client/utils" + "nexus-client/version" + "nexus-common/buildversion" + "nexus-common/utils" + "os" + "path" + "strings" + "syscall" "github.com/go-resty/resty/v2" + "github.com/peterh/liner" + "golang.org/x/term" ) var cmdList = []cmd.Command { @@ -73,6 +73,7 @@ var cmdList = []cmd.Command { &cmdVM.Reboot{"vmreboot"}, &cmdVM.Shutdown{"vmshutdown"}, &cmdVM.Start{"vmstart"}, + &cmdVM.StartAttach{"vmstartattach"}, &cmdVM.StartWithCreds{"vmstartwithcreds"}, }