mirror of https://github.com/apache/cloudstack.git
Refactoring the startvm command
This commit is contained in:
parent
8f54cac031
commit
aa6e9ab2bb
|
|
@ -18,30 +18,18 @@
|
|||
|
||||
package com.cloud.api.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.api.BaseCmd;
|
||||
import com.cloud.api.BaseAsyncCmd;
|
||||
import com.cloud.api.Implementation;
|
||||
import com.cloud.api.Parameter;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.vm.UserVmVO;
|
||||
|
||||
public class StopVMCmd extends BaseCmd {
|
||||
import com.cloud.api.BaseCmd.Manager;
|
||||
|
||||
@Implementation(method="stopVirtualMachine", manager=Manager.UserVmManager)
|
||||
public class StopVMCmd extends BaseAsyncCmd {
|
||||
public static final Logger s_logger = Logger.getLogger(StopVMCmd.class.getName());
|
||||
|
||||
private static final String s_name = "stopvirtualmachineresponse";
|
||||
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
|
||||
|
||||
static {
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.TRUE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USER_ID, Boolean.FALSE));
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//////////////// API parameters /////////////////////
|
||||
|
|
@ -66,46 +54,48 @@ public class StopVMCmd extends BaseCmd {
|
|||
return s_name;
|
||||
}
|
||||
|
||||
public List<Pair<Enum, Boolean>> getProperties() {
|
||||
return s_properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pair<String, Object>> execute(Map<String, Object> params) {
|
||||
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
|
||||
Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName());
|
||||
Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
|
||||
|
||||
// Verify input parameters
|
||||
UserVmVO vmInstance = getManagementServer().findUserVMInstanceById(vmId.longValue());
|
||||
if (vmInstance == null) {
|
||||
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId);
|
||||
}
|
||||
|
||||
if (account != null) {
|
||||
if (!isAdmin(account.getType()) && (account.getId().longValue() != vmInstance.getAccountId())) {
|
||||
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId + " for this account");
|
||||
} else if (!getManagementServer().isChildDomain(account.getDomainId(), vmInstance.getDomainId())) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid virtual machine id (" + vmId + ") given, unable to sop virtual machine.");
|
||||
}
|
||||
}
|
||||
|
||||
// If command is executed via 8096 port, set userId to the id of System account (1)
|
||||
if (userId == null) {
|
||||
userId = Long.valueOf(1);
|
||||
}
|
||||
|
||||
long jobId = getManagementServer().stopVirtualMachineAsync(userId.longValue(), vmId.longValue());
|
||||
if (jobId == 0) {
|
||||
s_logger.warn("Unable to schedule async-job for StopVM comamnd");
|
||||
} else {
|
||||
if(s_logger.isDebugEnabled())
|
||||
s_logger.debug("StopVM command has been accepted, job id: " + jobId);
|
||||
}
|
||||
|
||||
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
|
||||
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
|
||||
|
||||
return returnValues;
|
||||
}
|
||||
// @Override
|
||||
// public List<Pair<String, Object>> execute(Map<String, Object> params) {
|
||||
// Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
|
||||
// Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName());
|
||||
// Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
|
||||
//
|
||||
// // Verify input parameters
|
||||
// UserVmVO vmInstance = getManagementServer().findUserVMInstanceById(vmId.longValue());
|
||||
// if (vmInstance == null) {
|
||||
// throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId);
|
||||
// }
|
||||
//
|
||||
// if (account != null) {
|
||||
// if (!isAdmin(account.getType()) && (account.getId().longValue() != vmInstance.getAccountId())) {
|
||||
// throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId + " for this account");
|
||||
// } else if (!getManagementServer().isChildDomain(account.getDomainId(), vmInstance.getDomainId())) {
|
||||
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid virtual machine id (" + vmId + ") given, unable to sop virtual machine.");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // If command is executed via 8096 port, set userId to the id of System account (1)
|
||||
// if (userId == null) {
|
||||
// userId = Long.valueOf(1);
|
||||
// }
|
||||
//
|
||||
// long jobId = getManagementServer().stopVirtualMachineAsync(userId.longValue(), vmId.longValue());
|
||||
// if (jobId == 0) {
|
||||
// s_logger.warn("Unable to schedule async-job for StopVM comamnd");
|
||||
// } else {
|
||||
// if(s_logger.isDebugEnabled())
|
||||
// s_logger.debug("StopVM command has been accepted, job id: " + jobId);
|
||||
// }
|
||||
//
|
||||
// List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
|
||||
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
|
||||
//
|
||||
// return returnValues;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public String getResponse() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,6 +59,7 @@ import com.cloud.agent.api.to.DiskCharacteristicsTO;
|
|||
import com.cloud.agent.api.to.VolumeTO;
|
||||
import com.cloud.alert.AlertManager;
|
||||
import com.cloud.api.BaseCmd;
|
||||
import com.cloud.api.commands.StopVMCmd;
|
||||
import com.cloud.api.commands.UpdateStoragePoolCmd;
|
||||
import com.cloud.async.AsyncInstanceCreateStatus;
|
||||
import com.cloud.async.AsyncJobExecutor;
|
||||
|
|
@ -1940,8 +1941,10 @@ public class StorageManagerImpl implements StorageManager {
|
|||
//if the instance is of type uservm, call the user vm manager
|
||||
if(vmInstance.getType().equals(VirtualMachine.Type.User))
|
||||
{
|
||||
//create a dummy event
|
||||
long eventId = saveScheduledEvent(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM, EventTypes.EVENT_VM_STOP, "stopping user vm with Id: "+vmInstance.getId());
|
||||
|
||||
if(!_userVmMgr.stopVirtualMachine(userId, vmInstance.getId()))
|
||||
if(!_userVmMgr.stopVirtualMachine(userId, vmInstance.getId(),eventId))
|
||||
{
|
||||
s_logger.warn("There was an error stopping the user vm id: "+vmInstance.getId()+" ,cannot enable storage maintenance");
|
||||
primaryStorage.setStatus(Status.ErrorInMaintenance);
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import java.util.List;
|
|||
|
||||
import com.cloud.agent.api.VmStatsEntry;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.api.commands.StopVMCmd;
|
||||
import com.cloud.api.commands.UpdateVMCmd;
|
||||
import com.cloud.api.commands.UpgradeVMCmd;
|
||||
import com.cloud.async.executor.DestroyVMExecutor;
|
||||
|
|
@ -159,9 +160,11 @@ public interface UserVmManager extends Manager, VirtualMachineManager<UserVmVO>
|
|||
* Stops the virtual machine
|
||||
* @param userId the id of the user performing the action
|
||||
* @param vmId
|
||||
* @param eventId -- id of the scheduled event for stopping vm
|
||||
* @return true if stopped; false if problems.
|
||||
*/
|
||||
boolean stopVirtualMachine(long userId, long vmId);
|
||||
boolean stopVirtualMachine(long userId, long vmId, long eventId);
|
||||
boolean stopVirtualMachine(StopVMCmd cmd) throws ServerApiException;
|
||||
OperationResponse executeStopVM(StopVMExecutor executor, VMOperationParam param);
|
||||
void completeStopCommand(long userId, UserVmVO vm, Event e, long startEventId);
|
||||
|
||||
|
|
|
|||
|
|
@ -66,6 +66,7 @@ import com.cloud.agent.api.storage.CreatePrivateTemplateCommand;
|
|||
import com.cloud.alert.AlertManager;
|
||||
import com.cloud.api.BaseCmd;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.api.commands.StopVMCmd;
|
||||
import com.cloud.api.commands.UpdateVMCmd;
|
||||
import com.cloud.api.commands.UpgradeVMCmd;
|
||||
import com.cloud.async.AsyncJobExecutor;
|
||||
|
|
@ -914,8 +915,9 @@ public class UserVmManagerImpl implements UserVmManager {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean stopVirtualMachine(long userId, long vmId) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
public boolean stopVirtualMachine(long userId, long vmId, long eventId) {
|
||||
boolean status = false;
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Stopping vm=" + vmId);
|
||||
}
|
||||
|
||||
|
|
@ -926,8 +928,21 @@ public class UserVmManagerImpl implements UserVmManager {
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
EventUtils.saveStartedEvent(userId, vm.getAccountId(), EventTypes.EVENT_VM_STOP, "stopping Vm with Id: "+vmId, eventId);
|
||||
|
||||
return stop(userId, vm, 0);
|
||||
status = stop(userId, vm, 0);
|
||||
|
||||
if(status)
|
||||
{
|
||||
EventUtils.saveEvent(userId, vm.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_VM_STOP, "Successfully stopped VM instance : " + vmId);
|
||||
return status;
|
||||
}
|
||||
else
|
||||
{
|
||||
EventUtils.saveEvent(userId, vm.getAccountId(), EventVO.LEVEL_ERROR, EventTypes.EVENT_VM_STOP, "Error stopping VM instance : " + vmId);
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -3121,4 +3136,24 @@ public class UserVmManagerImpl implements UserVmManager {
|
|||
EventUtils.saveEvent(userId, accountId, EventVO.LEVEL_INFO, type, description, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean stopVirtualMachine(StopVMCmd cmd) throws ServerApiException{
|
||||
|
||||
//Input validation
|
||||
Account account = (Account)UserContext.current().getAccountObject();
|
||||
Long userId = UserContext.current().getUserId();
|
||||
Long id = cmd.getId();
|
||||
|
||||
UserVmVO vmInstance = _userVmDao.findById(id.longValue());
|
||||
if (vmInstance == null) {
|
||||
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + id);
|
||||
}
|
||||
|
||||
long eventId = EventUtils.saveScheduledEvent(userId, vmInstance.getAccountId(), EventTypes.EVENT_VM_STOP, "stopping Vm with Id: "+id);
|
||||
|
||||
userId = accountAndUserValidation(id, account, userId, vmInstance);
|
||||
|
||||
return stopVirtualMachine(userId, id, eventId);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue