@Test
  public void testCloneMachineFromAnotherMachine() throws Exception {
    VirtualBoxManager manager = (VirtualBoxManager) context.getProviderSpecificContext().getApi();
    ComputeServiceContext localHostContext =
        computeServiceForLocalhostAndGuest(
            hostId, "localhost", guestId, "localhost", new Credentials("toor", "password"));

    IMachine master = getMasterNode(manager, localHostContext);

    if (master.getCurrentSnapshot() != null) {
      ISession session = manager.openMachineSession(master);
      session.getConsole().deleteSnapshot(master.getCurrentSnapshot().getId());
      session.unlockMachine();
    }

    IMachine clone =
        new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(
                manager,
                localHostContext,
                settingsFile,
                osTypeId,
                vmId,
                forceOverwrite,
                cloneName,
                hostId,
                snapshotName,
                snapshotDesc,
                controllerIDE)
            .apply(master);
    assertEquals(clone.getNetworkAdapter(0L).getAttachmentType(), Bridged);
  }
예제 #2
0
  private void createAndLaunchVirtualMachine(int i) throws InterruptedException {
    String instanceName = vmName + "_" + i;
    IMachine adminNode = manager.getVBox().findMachine(adminNodeName);

    IMachine clonedVM =
        manager.getVBox().createMachine(settingsFile, instanceName, osTypeId, vmId, forceOverwrite);
    List<CloneOptions> options = new ArrayList<CloneOptions>();
    options.add(CloneOptions.Link);
    IProgress progress =
        adminNode
            .getCurrentSnapshot()
            .getMachine()
            .cloneTo(clonedVM, CloneMode.MachineState, options);
    if (progress.getCompleted()) logger().debug("clone done");

    manager.getVBox().registerMachine(clonedVM);

    System.out.println("\nLaunching VM named " + clonedVM.getName() + " ...");
    launchVMProcess(clonedVM, manager.getSessionObject());
    String ipAddress = null;
    while (ipAddress == null || ipAddress.equals("")) {
      try {
        ipAddress = clonedVM.getGuestPropertyValue("/VirtualBox/GuestInfo/Net/0/V4/IP");
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    System.out.println(ipAddress + " is the IP address of " + clonedVM.getName());

    // TODO
    // IPSocket socket = new IPSocket(ipAddress, 22);
    // checkSSH(IPSocket socket)

  }