Search in sources :

Example 1 with EngineContext

use of org.ovirt.engine.core.bll.context.EngineContext in project ovirt-engine by oVirt.

the class VdsEventListener method vdsUpEvent.

@Override
public boolean vdsUpEvent(final VDS vds) {
    HostStoragePoolParametersBase params = new HostStoragePoolParametersBase(vds);
    CommandContext commandContext = new CommandContext(new EngineContext()).withoutExecutionContext();
    commandContext.getExecutionContext().setJobRequired(true);
    boolean isSucceeded = backend.runInternalAction(ActionType.InitVdsOnUp, params, commandContext).getSucceeded();
    if (isSucceeded) {
        ThreadPoolUtil.execute(() -> {
            try {
                // migrate vms that its their default vds and failback
                // is on
                List<VmStatic> vmsToMigrate = vmStaticDao.getAllWithFailbackByVds(vds.getId());
                if (!vmsToMigrate.isEmpty()) {
                    CommandContext ctx = new CommandContext(new EngineContext());
                    ctx.getExecutionContext().setMonitored(true);
                    backend.runInternalMultipleActions(ActionType.MigrateVmToServer, new ArrayList<>(createMigrateVmToServerParametersList(vmsToMigrate, vds, null)), ctx);
                }
            } catch (RuntimeException e) {
                log.error("Failed to initialize Vds on up: {}", e.getMessage());
                log.error("Exception", e);
            }
        });
    }
    return isSucceeded;
}
Also used : HostStoragePoolParametersBase(org.ovirt.engine.core.common.action.HostStoragePoolParametersBase) CommandContext(org.ovirt.engine.core.bll.context.CommandContext) EngineContext(org.ovirt.engine.core.bll.context.EngineContext) VmStatic(org.ovirt.engine.core.common.businessentities.VmStatic)

Example 2 with EngineContext

use of org.ovirt.engine.core.bll.context.EngineContext in project ovirt-engine by oVirt.

the class CommandContextsCacheImpl method buildCommandContext.

private CommandContext buildCommandContext(CommandEntity cmdEntity) {
    ExecutionContext executionContext = new ExecutionContext();
    PersistedCommandContext persistedCommandContext = cmdEntity.getCommandContext();
    if (!Guid.isNullOrEmpty(persistedCommandContext.getJobId())) {
        executionContext.setJob(jobRepository.getJobWithSteps(persistedCommandContext.getJobId()));
    } else if (!Guid.isNullOrEmpty(persistedCommandContext.getStepId())) {
        executionContext.setStep(jobRepository.getStep(persistedCommandContext.getStepId(), false));
    }
    executionContext.setExecutionMethod(persistedCommandContext.getExecutionMethod());
    executionContext.setCompleted(persistedCommandContext.isCompleted());
    executionContext.setJobRequired(persistedCommandContext.isJobRequired());
    executionContext.setMonitored(persistedCommandContext.isMonitored());
    executionContext.setShouldEndJob(persistedCommandContext.shouldEndJob());
    executionContext.setTasksMonitored(persistedCommandContext.isTasksMonitored());
    return new CommandContext(new EngineContext()).withExecutionContext(executionContext);
}
Also used : ExecutionContext(org.ovirt.engine.core.bll.job.ExecutionContext) CommandContext(org.ovirt.engine.core.bll.context.CommandContext) PersistedCommandContext(org.ovirt.engine.core.common.utils.PersistedCommandContext) EngineContext(org.ovirt.engine.core.bll.context.EngineContext) PersistedCommandContext(org.ovirt.engine.core.common.utils.PersistedCommandContext)

Example 3 with EngineContext

use of org.ovirt.engine.core.bll.context.EngineContext in project ovirt-engine by oVirt.

the class CommandsRepository method retrieveCommand.

private CommandBase<?> retrieveCommand(CommandEntity cmdEntity, CommandContext cmdContext) {
    CommandBase<?> command = null;
    if (cmdEntity != null) {
        if (cmdContext == null) {
            cmdContext = new CommandContext(new EngineContext()).withExecutionContext(new ExecutionContext());
        }
        command = CommandsFactory.createCommand(cmdEntity.getCommandType(), cmdEntity.getCommandParameters(), cmdContext);
        if (command != null) {
            command.setCommandStatus(cmdEntity.getCommandStatus(), false);
            command.setCommandData(cmdEntity.getData());
            command.setReturnValue(cmdEntity.getReturnValue());
            if (!Guid.isNullOrEmpty(cmdEntity.getParentCommandId()) && !cmdEntity.getParentCommandId().equals(cmdEntity.getId()) && command.getParameters().getParentParameters() == null) {
                CommandBase<?> parentCommand = retrieveCommand(cmdEntity.getParentCommandId());
                if (parentCommand != null) {
                    command.getParameters().setParentParameters(parentCommand.getParameters());
                }
            }
        }
    }
    return command;
}
Also used : ExecutionContext(org.ovirt.engine.core.bll.job.ExecutionContext) CommandContext(org.ovirt.engine.core.bll.context.CommandContext) EngineContext(org.ovirt.engine.core.bll.context.EngineContext)

Example 4 with EngineContext

use of org.ovirt.engine.core.bll.context.EngineContext in project ovirt-engine by oVirt.

the class ImportHostedEngineStorageDomainCommandTest method prepareCommand.

protected void prepareCommand() {
    parameters.setStoragePoolId(HE_SP_ID);
    parameters.setVdsId(HE_VDS_ID);
    parameters.setStorageDomainId(HE_SD_ID);
    // vds
    VDS vds = new VDS();
    vds.setId(Guid.Empty);
    vds.setStoragePoolId(HE_SP_ID);
    when(vdsDao.get(any())).thenReturn(vds);
    List<BaseDisk> baseDisks = Collections.singletonList(new BaseDisk());
    when(baseDiskDao.getDisksByAlias(any())).thenReturn(baseDisks);
    // Data center
    StoragePool pool = new StoragePool();
    pool.setStatus(StoragePoolStatus.Up);
    pool.setId(HE_SP_ID);
    when(storagePoolDao.get(HE_SP_ID)).thenReturn(pool);
    // compensation
    CompensationContext compensationContext = mock(CompensationContext.class);
    when(cmd.getCompensationContext()).thenReturn(compensationContext);
    when(cmd.getContext()).thenReturn(new CommandContext(new EngineContext()));
}
Also used : StoragePool(org.ovirt.engine.core.common.businessentities.StoragePool) VDS(org.ovirt.engine.core.common.businessentities.VDS) CommandContext(org.ovirt.engine.core.bll.context.CommandContext) CompensationContext(org.ovirt.engine.core.bll.context.CompensationContext) BaseDisk(org.ovirt.engine.core.common.businessentities.storage.BaseDisk) EngineContext(org.ovirt.engine.core.bll.context.EngineContext)

Example 5 with EngineContext

use of org.ovirt.engine.core.bll.context.EngineContext in project ovirt-engine by oVirt.

the class VMConsoleProxyServlet method availableConsoles.

private List<Map<String, String>> availableConsoles(String userIdAsString) {
    List<Map<String, String>> jsonVms = new ArrayList<>();
    Guid userGuid = null;
    try {
        if (StringUtils.isNotEmpty(userIdAsString)) {
            userGuid = Guid.createGuidFromString(userIdAsString);
        }
    } catch (IllegalArgumentException e) {
        log.debug("Could not read User GUID");
    }
    if (userGuid != null) {
        ActionReturnValue loginResult = backend.runInternalAction(ActionType.LoginOnBehalf, new LoginOnBehalfParameters(userGuid));
        if (!loginResult.getSucceeded()) {
            throw new RuntimeException("Unable to create session using LoginOnBehalf");
        }
        String engineSessionId = loginResult.getActionReturnValue();
        try {
            QueryReturnValue retVms = backend.runInternalQuery(QueryType.GetAllVmsForUserAndActionGroup, new GetEntitiesWithPermittedActionParameters(ActionGroup.CONNECT_TO_SERIAL_CONSOLE), new EngineContext().withSessionId(engineSessionId));
            if (retVms != null) {
                List<VM> vmsList = retVms.getReturnValue();
                for (VM vm : vmsList) {
                    Map<String, String> jsonVm = new HashMap<>();
                    if (vm.getRunOnVds() != null) {
                        // TODO: avoid one query per loop. Bulk query?
                        QueryReturnValue retValue = backend.runInternalQuery(QueryType.GetVdsByVdsId, new IdQueryParameters(vm.getRunOnVds()));
                        if (retValue != null && retValue.getReturnValue() != null) {
                            VDS vds = retValue.getReturnValue();
                            jsonVm.put("vmid", vm.getId().toString());
                            jsonVm.put("vmname", vm.getName());
                            jsonVm.put("host", vds.getHostName());
                            /* there is only one serial console, no need and no way to distinguish them */
                            jsonVm.put("console", "default");
                            jsonVms.add(jsonVm);
                        }
                    }
                }
            }
        } finally {
            backend.runInternalAction(ActionType.LogoutSession, new ActionParametersBase(engineSessionId));
        }
    }
    return jsonVms;
}
Also used : GetEntitiesWithPermittedActionParameters(org.ovirt.engine.core.common.queries.GetEntitiesWithPermittedActionParameters) VDS(org.ovirt.engine.core.common.businessentities.VDS) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Guid(org.ovirt.engine.core.compat.Guid) ActionParametersBase(org.ovirt.engine.core.common.action.ActionParametersBase) QueryReturnValue(org.ovirt.engine.core.common.queries.QueryReturnValue) IdQueryParameters(org.ovirt.engine.core.common.queries.IdQueryParameters) ActionReturnValue(org.ovirt.engine.core.common.action.ActionReturnValue) LoginOnBehalfParameters(org.ovirt.engine.core.common.action.LoginOnBehalfParameters) VM(org.ovirt.engine.core.common.businessentities.VM) EngineContext(org.ovirt.engine.core.bll.context.EngineContext) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

EngineContext (org.ovirt.engine.core.bll.context.EngineContext)6 CommandContext (org.ovirt.engine.core.bll.context.CommandContext)5 ExecutionContext (org.ovirt.engine.core.bll.job.ExecutionContext)3 VDS (org.ovirt.engine.core.common.businessentities.VDS)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CompensationContext (org.ovirt.engine.core.bll.context.CompensationContext)1 ActionParametersBase (org.ovirt.engine.core.common.action.ActionParametersBase)1 ActionReturnValue (org.ovirt.engine.core.common.action.ActionReturnValue)1 HostStoragePoolParametersBase (org.ovirt.engine.core.common.action.HostStoragePoolParametersBase)1 LoginOnBehalfParameters (org.ovirt.engine.core.common.action.LoginOnBehalfParameters)1 StoragePool (org.ovirt.engine.core.common.businessentities.StoragePool)1 VM (org.ovirt.engine.core.common.businessentities.VM)1 VmStatic (org.ovirt.engine.core.common.businessentities.VmStatic)1 BaseDisk (org.ovirt.engine.core.common.businessentities.storage.BaseDisk)1 GetEntitiesWithPermittedActionParameters (org.ovirt.engine.core.common.queries.GetEntitiesWithPermittedActionParameters)1 IdQueryParameters (org.ovirt.engine.core.common.queries.IdQueryParameters)1 QueryReturnValue (org.ovirt.engine.core.common.queries.QueryReturnValue)1 PersistedCommandContext (org.ovirt.engine.core.common.utils.PersistedCommandContext)1