Merge ~jslarraz/ubuntu-qa-tools:add-vm-name-resolution into ubuntu-qa-tools:master

Proposed by Jorge Sancho Larraz
Status: Rejected
Rejected by: Jorge Sancho Larraz
Proposed branch: ~jslarraz/ubuntu-qa-tools:add-vm-name-resolution
Merge into: ubuntu-qa-tools:master
Diff against target: 124 lines (+69/-9)
1 file modified
vm-tools/uvt (+69/-9)
Reviewer Review Type Date Requested Status
Ubuntu Bug Control Pending
Review via email: mp+460382@code.launchpad.net

Commit message

uvt: add cmd_ssh and perform address resolution using virsh when possible

Description of the change

When trying to create a snap with uvt I was not able to make address resolution using libnss-libvirt to work.

This MR modifies uvt to perform the address resolution internally using `virsh domifaddr` instead of relying on the OS. It will fallback to the old behavior if this resolution does not work.

This MR also add a new command called `uvt ssh <vm-name> (pretty much a shortcut for `uvt cmd <vm-name> bash`) that opens an interactive session in the target machine.

Both changes together may remove the need of network setup (install libnss-libvirt + config /etc/nsswitch.conf) from the setup step on certain configurations. It will additionally enable us to package uvt as a snap

To post a comment you must log in.
Revision history for this message
Marc Deslauriers (mdeslaur) wrote :

I just ripped out domifaddr support because we wanted to standardize on the same configuration for everyone and domifaddr support was limited and wouldn't allow resolving from the host.

https://git.launchpad.net/ubuntu-qa-tools/commit/vm-tools/uvt?id=43be4941455b466d7be5e263ee6f345be7377277

Perhaps you can add your patch to your snap instead of the main repo?

Revision history for this message
Jorge Sancho Larraz (jslarraz) wrote (last edit ):

Thanks for the info Marc, I'll patch it at build time then

Unmerged commits

9a36ce0... by Jorge Sancho Larraz

uvt: add uvt ssh <vm-name> command

8011732... by Jorge Sancho Larraz

uvt: perform address resolution using virsh when possible

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/vm-tools/uvt b/vm-tools/uvt
index c7b0143..884b5bf 100755
--- a/vm-tools/uvt
+++ b/vm-tools/uvt
@@ -468,6 +468,58 @@ def cmd_cmd():
468 print("Error: VM '%s' command failed. Aborting." % machine, file=sys.stderr)468 print("Error: VM '%s' command failed. Aborting." % machine, file=sys.stderr)
469 sys.exit(1)469 sys.exit(1)
470470
471def cmd_ssh():
472 '''Run a command inside a virtual machine'''
473
474 usage = "usage: %prog ssh [options] <vm>"
475
476 epilog = "\n" + \
477 "Eg:\n" + \
478 "$ uvt ssh sec-jammy-amd64\n\n" + \
479 "This will open an interactive session on the single VM named 'sec-jammy-amd64'\n"
480
481 optparse.OptionParser.format_epilog = lambda self, formatter: self.epilog
482 parser = optparse.OptionParser(usage = usage, epilog = epilog)
483
484 parser.add_option("-s", "--start", dest="start", default=False, action='store_true',
485 help="Start the VM (and shutdown if it wasn't running")
486
487 parser.add_option("-t", "--timeout", dest="timeout", default=90, metavar="TIMEOUT",
488 help="wait TIMEOUT seconds for VM to come up if -s is used (default: %default)")
489
490 parser.add_option("-f", "--force-ssh", dest="force_ssh", default=False, action='store_true',
491 help="force the SSH keys to be taken")
492
493 parser.add_option("-r", "--root", dest="root", default=False, action='store_true',
494 help="login to the VM as root")
495
496 parser.add_option("-u", "--user", dest="user", default=None, metavar="USER",
497 help="login to the VM as user")
498
499 parser.add_option("-q", "--quiet", dest="quiet", default=False, action='store_true',
500 help="only report hostnames and output")
501
502 (opt, args) = parser.parse_args()
503 machine = args[0]
504
505 if opt.user is not None and opt.root:
506 print("Error: may specify only one of --root and --user.\n", file=sys.stderr)
507 sys.exit(1)
508
509 print("----- %s -----" % machine)
510 if check_vm_exists(machine) == False:
511 print("Error: VM '%s' does not exist, skipping." % machine, file=sys.stderr)
512 return
513
514 result = vm_run_command(machine, "bash", root=opt.root, start=opt.start,
515 start_timeout=opt.timeout, force_keys=opt.force_ssh,
516 quiet=opt.quiet, output=True, interactive=True,
517 verbose=False, user=opt.user)
518
519 if result == False:
520 print("Error: VM '%s' command failed. Aborting." % machine, file=sys.stderr)
521 sys.exit(1)
522
471def cmd_repo():523def cmd_repo():
472 '''Adds or removes a local repo to a VM'''524 '''Adds or removes a local repo to a VM'''
473525
@@ -1415,8 +1467,8 @@ def vm_run_command(vm_name, command, root=False, start=False,
1415 print("Could not start VM: %s" % vm_name)1467 print("Could not start VM: %s" % vm_name)
1416 return False1468 return False
14171469
1418 dns_name = vm_ping(vm_name)1470 vm_addr = vm_ping(vm_name)
1419 if dns_name == "":1471 if vm_addr == "":
1420 print("Could not ping VM: %s" % vm_name)1472 print("Could not ping VM: %s" % vm_name)
1421 return False1473 return False
14221474
@@ -1440,7 +1492,7 @@ def vm_run_command(vm_name, command, root=False, start=False,
1440 ssh_command += ['-q']1492 ssh_command += ['-q']
1441 ssh_command += ['-o', 'BatchMode=yes']1493 ssh_command += ['-o', 'BatchMode=yes']
14421494
1443 ssh_command += [dns_name, command]1495 ssh_command += [vm_addr, command]
14441496
1445 if interactive:1497 if interactive:
1446 rc, out = runcmd(ssh_command, stderr = None, stdout = None, stdin = None)1498 rc, out = runcmd(ssh_command, stderr = None, stdout = None, stdin = None)
@@ -1519,9 +1571,9 @@ def vm_stop(vm_name):
1519 # a shutdown menu.1571 # a shutdown menu.
15201572
1521 # If we can connect using ssh, issue a shutdown command1573 # If we can connect using ssh, issue a shutdown command
1522 dns_host = vm_ping(vm_name)1574 vm_addr = vm_ping(vm_name)
1523 if dns_host != "" and ssh_connect(dns_host):1575 if vm_addr != "" and ssh_connect(vm_addr):
1524 vm_run_command(dns_host, "shutdown -h now", root=True,1576 vm_run_command(vm_addr, "shutdown -h now", root=True,
1525 force_keys=True, output=False, ignore_rc=True)1577 force_keys=True, output=False, ignore_rc=True)
1526 else:1578 else:
1527 rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],1579 rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
@@ -1563,9 +1615,16 @@ def vm_start_wait(vm_name, timeout=1800, quiet=False, clone_name=None):
15631615
1564def vm_ping(vm_name):1616def vm_ping(vm_name):
1565 '''Attempts to ping a VM'''1617 '''Attempts to ping a VM'''
1566 rc, out = runcmd(["ping", "-c1", "-w1", vm_name])1618
1567 if rc == 0 and ssh_connect(vm_name) == True:1619 # Try to resolve address first
1568 return vm_name1620 rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
1621 "domifaddr", vm_name])
1622 addr_search = re.search("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", out)
1623 vm_addr = addr_search.group(0) if ((rc == 0) and (addr_search is not None)) else vm_name
1624
1625 rc, out = runcmd(["ping", "-c1", "-w1", vm_addr])
1626 if rc == 0 and ssh_connect(vm_addr) == True:
1627 return vm_addr
1569 return ""1628 return ""
15701629
1571def ssh_connect(host, resolve=True):1630def ssh_connect(host, resolve=True):
@@ -3608,6 +3667,7 @@ commands = {
3608 'update' : cmd_update,3667 'update' : cmd_update,
3609 'clone' : cmd_clone,3668 'clone' : cmd_clone,
3610 'cmd' : cmd_cmd,3669 'cmd' : cmd_cmd,
3670 'ssh' : cmd_ssh,
3611 'list' : cmd_list,3671 'list' : cmd_list,
3612 'config' : cmd_config,3672 'config' : cmd_config,
3613 'dump' : cmd_dump,3673 'dump' : cmd_dump,

Subscribers

People subscribed via source and target branches