Exemplo n.º 1
0
  /**
   * Get the {@link IMachine} of VirtualBox with the given name.
   *
   * @param name name of the IMachine to get
   * @return IMachine with the searched name
   * @throws VirtualMachineNotFoundException thrown if there is no IMachine with this name
   */
  public IMachine getMachine(final String name) throws VirtualMachineNotFoundException {

    if (StringUtils.isBlank(name)) {
      throw new IllegalArgumentException("name of the virtual machine must not be null or empty");
    }

    List<String> groups = new LinkedList<>();
    groups.add(BETSY_VBOX_GROUP);
    List<IMachine> machines = vBox.getMachinesByGroups(groups);
    for (IMachine machine : machines) {
      if (machine.getName().equals(name)) {
        return machine;
      }
    }

    throw new VirtualMachineNotFoundException(
        "VirtualMachine with name '" + name + "' could not be found in group " + BETSY_VBOX_GROUP);
  }
Exemplo n.º 2
0
  /**
   * Import the EngineExtended's virtualMachine from the given file.
   *
   * @param vmName desired name of the virtualMachine
   * @param engineName name of the engine the new VM belongs to
   * @param importFile file of the appliance to import
   */
  public void importVirtualMachine(
      final String vmName, final String engineName, final Path importFile) {

    if (StringUtils.isBlank(vmName)) {
      throw new IllegalArgumentException("The name of the vm to import must not be null or empty");
    }
    if (StringUtils.isBlank(engineName)) {
      throw new IllegalArgumentException(
          "The name of the engine to import must not be null or empty");
    }
    Objects.requireNonNull(importFile, "The file to import must not be null");

    IMachine importedVm = null;
    ISession session = null;
    try {
      log.info("Importing appliance from file " + importFile);
      IAppliance appliance = vBoxImporter.importAppliance(importFile);
      log.info("Appliance imported (machines: " + appliance.getMachines() + ")");

      if (appliance.getMachines().isEmpty()) {
        throw new IllegalStateException("The appliance " + importFile + " has no machines inside");
      }

      // by definition the appliance container could contain several
      // separated machines which must be imported each at it's own.
      for (String uuid : appliance.getMachines()) {
        importedVm = vBox.findMachine(uuid);

        // acquire session lock
        session = vBoxManager.getSessionObject();
        importedVm.lockMachine(session, LockType.Write);
        IMachine lockedVM = session.getMachine();

        vBoxImporter.adjustApplianceSettings(lockedVM, vmName);

        try {
          session.unlockMachine();
          session = null;
        } catch (VBoxException exception) {
          // ignore if was not locked
          log.debug("Failed to unlock session after import");
        }
      } // END FOR ITERATION
    } catch (VBoxException exception) {
      // session must be unlocked for deleting the vm
      if (session != null) {
        try {
          session.unlockMachine();
        } catch (VBoxException ignore) {
          log.debug("Failed to unlock session after import exception", ignore);
          // ignore if was not locked
        }
      }
      if (importedVm != null) {
        log.debug("Exception during import, delete VM again.");
        // Error --> delete VM again
        this.deleteMachine(importedVm);
      }
      log.warn("Unexpected import exception:", exception);
    }
  }