(cmd *cobra.Command, args []string)
| 500 | } |
| 501 | |
| 502 | func runGuestsExec(cmd *cobra.Command, args []string) error { |
| 503 | vmid, err := parseVMID(args[0]) |
| 504 | if err != nil { |
| 505 | return printError(err) |
| 506 | } |
| 507 | |
| 508 | command := args[1] |
| 509 | timeout, _ := cmd.Flags().GetDuration("timeout") |
| 510 | |
| 511 | session, initErr := initCLISession(cmd) |
| 512 | if initErr != nil { |
| 513 | return printError(initErr) |
| 514 | } |
| 515 | |
| 516 | if session == nil { |
| 517 | return nil |
| 518 | } |
| 519 | |
| 520 | ctx := context.Background() |
| 521 | |
| 522 | vm, err := session.findVM(ctx, vmid) |
| 523 | if err != nil { |
| 524 | return printError(err) |
| 525 | } |
| 526 | |
| 527 | if vm.Type != api.VMTypeQemu && vm.Type != api.VMTypeLXC { |
| 528 | return printError(fmt.Errorf("guest %d is type %q; exec is only supported for QEMU VMs and LXC containers", vmid, vm.Type)) |
| 529 | } |
| 530 | |
| 531 | if vm.Status != api.VMStatusRunning { |
| 532 | return printError(fmt.Errorf("guest %d is not running (status: %s)", vmid, vm.Status)) |
| 533 | } |
| 534 | |
| 535 | execCtx, cancel := context.WithTimeout(ctx, timeout) |
| 536 | defer cancel() |
| 537 | |
| 538 | start := time.Now() |
| 539 | |
| 540 | var stdout, stderr string |
| 541 | var exitCode int |
| 542 | |
| 543 | if vm.Type == api.VMTypeLXC { |
| 544 | stdout, stderr, exitCode, err = session.execLXC(execCtx, vm, []string{"/bin/sh", "-c", command}, timeout) |
| 545 | if err != nil { |
| 546 | return printError(fmt.Errorf("exec failed on LXC %d: %w", vmid, err)) |
| 547 | } |
| 548 | } else { |
| 549 | if !vm.AgentEnabled || !vm.AgentRunning { |
| 550 | return printError(fmt.Errorf("guest agent is not available on VM %d (enabled: %v, running: %v)", vmid, vm.AgentEnabled, vm.AgentRunning)) |
| 551 | } |
| 552 | |
| 553 | client, clientErr := session.clientForVM(vm) |
| 554 | if clientErr != nil { |
| 555 | return printError(clientErr) |
| 556 | } |
| 557 | |
| 558 | cmdParts := buildExecCommand(vm.OSType, command) |
| 559 | stdout, stderr, exitCode, err = client.ExecuteGuestAgentCommand(execCtx, vm, cmdParts, timeout) |
nothing calls this directly
no test coverage detected