private CommandContext createAddVmStepContext(String currentVmName) {
    CommandContext commandCtx = null;

    try {
      Map<String, String> values = new HashMap<>();
      values.put(VdcObjectType.VM.name().toLowerCase(), currentVmName);
      Step addVmStep =
          ExecutionHandler.addSubStep(
              getExecutionContext(),
              getExecutionContext().getJob().getStep(StepEnum.EXECUTING),
              StepEnum.ADD_VM_TO_POOL,
              ExecutionMessageDirector.resolveStepMessage(StepEnum.ADD_VM_TO_POOL, values));
      ExecutionContext ctx = new ExecutionContext();
      ctx.setStep(addVmStep);
      ctx.setMonitored(true);
      commandCtx = cloneContextAndDetachFromParent().withExecutionContext(ctx);
    } catch (RuntimeException e) {
      log.error(
          "Failed to create command context of adding VM '{}' to Pool '{}': {}",
          currentVmName,
          getParameters().getVmPool().getName(),
          e.getMessage());
      log.debug("Exception", e);
    }
    return commandCtx;
  }
 @Override
 protected void executeCommand() {
   ExecutionContext context = new ExecutionContext();
   context.setJob(job);
   context.setStep(step);
   context.setMonitored(true);
   context.setExecutionMethod(ExecutionMethod.AsStep);
   ExecutionHandler.endStep(context, step, getParameters().getStatus());
   setSucceeded(true);
 }
 protected CommandContext createMigrateVmContext(ExecutionContext parentContext, VM vm) {
   ExecutionContext ctx = new ExecutionContext();
   try {
     Map<String, String> values = new HashMap<>();
     values.put(VdcObjectType.VM.name().toLowerCase(), vm.getName());
     values.put(VdcObjectType.VDS.name().toLowerCase(), vm.getRunOnVdsName());
     Step step =
         ExecutionHandler.addSubStep(
             getExecutionContext(),
             parentContext.getJob().getStep(StepEnum.EXECUTING),
             StepEnum.MIGRATE_VM,
             ExecutionMessageDirector.resolveStepMessage(StepEnum.MIGRATE_VM, values));
     ctx.setJob(parentContext.getJob());
     ctx.setStep(step);
     ctx.setMonitored(true);
   } catch (RuntimeException e) {
     log.error("Failed to create ExecutionContext for MigrateVmCommand", e);
   }
   return cloneContextAndDetachFromParent().withExecutionContext(ctx);
 }
Esempio n. 4
0
  @Override
  protected void executeCommand() {
    Guid oVirtId = getParameters().getVdsForUniqueId();
    if (oVirtId != null) {

      // if fails to remove deprecated entry, we might attempt to add new oVirt host with an
      // existing unique-id.
      if (!removeDeprecatedOvirtEntry(oVirtId)) {
        log.error(
            "Failed to remove duplicated oVirt entry with id '{}'. Abort adding oVirt Host type",
            oVirtId);
        throw new EngineException(EngineError.HOST_ALREADY_EXISTS);
      }
    }

    TransactionSupport.executeInNewTransaction(
        () -> {
          addVdsStaticToDb();
          addVdsDynamicToDb();
          addVdsStatisticsToDb();
          getCompensationContext().stateChanged();
          return null;
        });

    if (getParameters().isProvisioned()) {
      HostProviderProxy proxy = ProviderProxyFactory.getInstance().create(getHostProvider());
      proxy.provisionHost(
          getParameters().getvds(),
          getParameters().getHostGroup(),
          getParameters().getComputeResource(),
          getParameters().getHostMac(),
          getParameters().getDiscoverName(),
          getParameters().getPassword(),
          getParameters().getDiscoverIp());

      addCustomValue("HostGroupName", getParameters().getHostGroup().getName());
      auditLogDirector.log(this, AuditLogType.VDS_PROVISION);
    }

    // set vds spm id
    if (getCluster().getStoragePoolId() != null) {
      VdsActionParameters tempVar = new VdsActionParameters(getVdsIdRef());
      tempVar.setSessionId(getParameters().getSessionId());
      tempVar.setCompensationEnabled(true);
      VdcReturnValueBase addVdsSpmIdReturn =
          runInternalAction(
              VdcActionType.AddVdsSpmId,
              tempVar,
              cloneContext().withoutLock().withoutExecutionContext());
      if (!addVdsSpmIdReturn.getSucceeded()) {
        setSucceeded(false);
        getReturnValue().setFault(addVdsSpmIdReturn.getFault());
        return;
      }
    }
    TransactionSupport.executeInNewTransaction(
        () -> {
          initializeVds(true);
          alertIfPowerManagementNotConfigured(getParameters().getVdsStaticData());
          testVdsPowerManagementStatus(getParameters().getVdsStaticData());
          setSucceeded(true);
          setActionReturnValue(getVdsIdRef());

          // If the installation failed, we don't want to compensate for the failure since it will
          // remove the
          // host, but instead the host should be left in an "install failed" status.
          getCompensationContext().cleanupCompensationDataAfterSuccessfulCommand();
          return null;
        });
    // do not install vds's which added in pending mode or for provisioning (currently power
    // clients). they are installed as part of the approve process or automatically after provision
    if (Config.<Boolean>getValue(ConfigValues.InstallVds)
        && !getParameters().isPending()
        && !getParameters().isProvisioned()) {
      final InstallVdsParameters installVdsParameters =
          new InstallVdsParameters(getVdsId(), getParameters().getPassword());
      installVdsParameters.setAuthMethod(getParameters().getAuthMethod());
      installVdsParameters.setOverrideFirewall(getParameters().getOverrideFirewall());
      installVdsParameters.setActivateHost(getParameters().getActivateHost());
      installVdsParameters.setNetworkProviderId(
          getParameters().getVdsStaticData().getOpenstackNetworkProviderId());
      installVdsParameters.setNetworkMappings(getParameters().getNetworkMappings());
      installVdsParameters.setEnableSerialConsole(getParameters().getEnableSerialConsole());
      if (getParameters().getHostedEngineDeployConfiguration() != null) {
        Map<String, String> vdsDeployParams =
            hostedEngineHelper.createVdsDeployParams(
                getVdsId(), getParameters().getHostedEngineDeployConfiguration().getDeployAction());
        installVdsParameters.setHostedEngineConfiguration(vdsDeployParams);
      }
      Map<String, String> values = new HashMap<>();
      values.put(VdcObjectType.VDS.name().toLowerCase(), getParameters().getvds().getName());
      Step installStep =
          executionHandler.addSubStep(
              getExecutionContext(),
              getExecutionContext().getJob().getStep(StepEnum.EXECUTING),
              StepEnum.INSTALLING_HOST,
              ExecutionMessageDirector.resolveStepMessage(StepEnum.INSTALLING_HOST, values));
      final ExecutionContext installCtx = new ExecutionContext();
      installCtx.setJob(getExecutionContext().getJob());
      installCtx.setStep(installStep);
      installCtx.setMonitored(true);
      installCtx.setShouldEndJob(true);
      ThreadPoolUtil.execute(
          () ->
              runInternalAction(
                  VdcActionType.InstallVdsInternal,
                  installVdsParameters,
                  cloneContextAndDetachFromParent().withExecutionContext(installCtx)));
      ExecutionHandler.setAsyncJob(getExecutionContext(), true);
    }
  }