Refactoring the rebootvm cmd

This commit is contained in:
abhishek 2010-08-20 11:48:24 -07:00
parent be75946b1f
commit dc45e9b08b
5 changed files with 111 additions and 90 deletions

View File

@ -18,30 +18,18 @@
package com.cloud.api.commands; package com.cloud.api.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger; 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.Parameter;
import com.cloud.api.ServerApiException; import com.cloud.api.BaseCmd.Manager;
import com.cloud.user.Account;
import com.cloud.utils.Pair; @Implementation(method="rebootVirtualMachine", manager=Manager.UserVmManager)
import com.cloud.vm.UserVmVO; public class RebootVMCmd extends BaseAsyncCmd {
public class RebootVMCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(RebootVMCmd.class.getName()); public static final Logger s_logger = Logger.getLogger(RebootVMCmd.class.getName());
private static final String s_name = "rebootvirtualmachineresponse"; private static final String s_name = "rebootvirtualmachineresponse";
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 ///////////////////// //////////////// API parameters /////////////////////
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
@ -64,47 +52,49 @@ public class RebootVMCmd extends BaseCmd {
public String getName() { public String getName() {
return s_name; return s_name;
} }
public List<Pair<Enum, Boolean>> getProperties() {
return s_properties;
}
@Override // @Override
public List<Pair<String, Object>> execute(Map<String, Object> params) { // public List<Pair<String, Object>> execute(Map<String, Object> params) {
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); // Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); // Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName()); // Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName());
//
//Verify input parameters // //Verify input parameters
UserVmVO vmInstance = getManagementServer().findUserVMInstanceById(vmId.longValue()); // UserVmVO vmInstance = getManagementServer().findUserVMInstanceById(vmId.longValue());
if (vmInstance == null) { // if (vmInstance == null) {
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId); // throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId);
} // }
//
if (account != null) { // if (account != null) {
if (!isAdmin(account.getType()) && (account.getId().longValue() != vmInstance.getAccountId())) { // 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"); // 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())) { // } else if (!getManagementServer().isChildDomain(account.getDomainId(), vmInstance.getDomainId())) {
// the domain in which the VM lives is not in the admin's domain tree // // the domain in which the VM lives is not in the admin's domain tree
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to reboot virtual machine with id " + vmId + ", invalid id given."); // throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to reboot virtual machine with id " + vmId + ", invalid id given.");
} // }
} // }
//
// If command is executed via 8096 port, set userId to the id of System account (1) // // If command is executed via 8096 port, set userId to the id of System account (1)
if (userId == null) { // if (userId == null) {
userId = Long.valueOf(1); // userId = Long.valueOf(1);
} // }
//
long jobId = getManagementServer().rebootVirtualMachineAsync(userId.longValue(), vmId.longValue()); // long jobId = getManagementServer().rebootVirtualMachineAsync(userId.longValue(), vmId.longValue());
if(jobId == 0) { // if(jobId == 0) {
s_logger.warn("Unable to schedule async-job for RebootVM comamnd"); // s_logger.warn("Unable to schedule async-job for RebootVM comamnd");
} else { // } else {
if(s_logger.isDebugEnabled()) // if(s_logger.isDebugEnabled())
s_logger.debug("RebootVM command has been accepted, job id: " + jobId); // s_logger.debug("RebootVM command has been accepted, job id: " + jobId);
} // }
//
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>(); // List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); // returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
return returnValues; // return returnValues;
} // }
@Override
public String getResponse() {
// TODO Auto-generated method stub
return null;
}
} }

View File

@ -698,7 +698,7 @@ public interface ManagementServer {
* @param vmId * @param vmId
* @return true if successfully rebooted, false otherwise * @return true if successfully rebooted, false otherwise
*/ */
boolean rebootVirtualMachine(long userId, long vmId); // boolean rebootVirtualMachine(long userId, long vmId);
/** /**
* Reboots a Virtual Machine * Reboots a Virtual Machine
@ -706,7 +706,7 @@ public interface ManagementServer {
* @param vmId * @param vmId
* @return the async-call job id * @return the async-call job id
*/ */
long rebootVirtualMachineAsync(long userId, long vmId); // long rebootVirtualMachineAsync(long userId, long vmId);
/** /**

View File

@ -2631,31 +2631,31 @@ public class ManagementServerImpl implements ManagementServer {
return _asyncMgr.submitAsyncJob(job, true); return _asyncMgr.submitAsyncJob(job, true);
} }
@Override // @Override
public boolean rebootVirtualMachine(long userId, long vmId) { // public boolean rebootVirtualMachine(long userId, long vmId) {
return _vmMgr.rebootVirtualMachine(userId, vmId); // return _vmMgr.rebootVirtualMachine(userId, vmId);
} // }
@Override // @Override
public long rebootVirtualMachineAsync(long userId, long vmId) { // public long rebootVirtualMachineAsync(long userId, long vmId) {
//
UserVmVO userVm = _userVmDao.findById(vmId); // UserVmVO userVm = _userVmDao.findById(vmId);
//
long eventId = EventUtils.saveScheduledEvent(userId, userVm.getAccountId(), EventTypes.EVENT_VM_REBOOT, "rebooting Vm with Id: "+vmId); // long eventId = EventUtils.saveScheduledEvent(userId, userVm.getAccountId(), EventTypes.EVENT_VM_REBOOT, "rebooting Vm with Id: "+vmId);
//
VMOperationParam param = new VMOperationParam(userId, userVm.getAccountId(), vmId, null, eventId); // VMOperationParam param = new VMOperationParam(userId, userVm.getAccountId(), vmId, null, eventId);
Gson gson = GsonHelper.getBuilder().create(); // Gson gson = GsonHelper.getBuilder().create();
//
AsyncJobVO job = new AsyncJobVO(); // AsyncJobVO job = new AsyncJobVO();
job.setUserId(UserContext.current().getUserId()); // job.setUserId(UserContext.current().getUserId());
job.setAccountId(userVm.getAccountId()); // job.setAccountId(userVm.getAccountId());
job.setCmd("RebootVM"); // job.setCmd("RebootVM");
job.setCmdInfo(gson.toJson(param)); // job.setCmdInfo(gson.toJson(param));
//
// use the same result result object name as StartVMCmd // // use the same result result object name as StartVMCmd
job.setCmdOriginator(StartVMCmd.getResultObjectName()); // job.setCmdOriginator(StartVMCmd.getResultObjectName());
return _asyncMgr.submitAsyncJob(job, true); // return _asyncMgr.submitAsyncJob(job, true);
} // }
@Override @Override
public boolean destroyVirtualMachine(long userId, long vmId) { public boolean destroyVirtualMachine(long userId, long vmId) {

View File

@ -22,6 +22,7 @@ import java.util.List;
import com.cloud.agent.api.VmStatsEntry; import com.cloud.agent.api.VmStatsEntry;
import com.cloud.api.ServerApiException; import com.cloud.api.ServerApiException;
import com.cloud.api.commands.RebootVMCmd;
import com.cloud.api.commands.RecoverVMCmd; import com.cloud.api.commands.RecoverVMCmd;
import com.cloud.api.commands.ResetVMPasswordCmd; import com.cloud.api.commands.ResetVMPasswordCmd;
import com.cloud.api.commands.StartVMCmd; import com.cloud.api.commands.StartVMCmd;
@ -191,7 +192,7 @@ public interface UserVmManager extends Manager, VirtualMachineManager<UserVmVO>
*/ */
HashMap<Long, VmStatsEntry> getVirtualMachineStatistics(long hostId, String hostName, List<Long> vmIds) throws InternalErrorException; HashMap<Long, VmStatsEntry> getVirtualMachineStatistics(long hostId, String hostName, List<Long> vmIds) throws InternalErrorException;
boolean rebootVirtualMachine(long userId, long vmId); boolean rebootVirtualMachine(RebootVMCmd cmd);
OperationResponse executeRebootVM(RebootVMExecutor executor, VMOperationParam param); OperationResponse executeRebootVM(RebootVMExecutor executor, VMOperationParam param);
boolean recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationException, InternalErrorException; boolean recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationException, InternalErrorException;

View File

@ -66,6 +66,7 @@ import com.cloud.agent.manager.AgentManager;
import com.cloud.alert.AlertManager; import com.cloud.alert.AlertManager;
import com.cloud.api.BaseCmd; import com.cloud.api.BaseCmd;
import com.cloud.api.ServerApiException; import com.cloud.api.ServerApiException;
import com.cloud.api.commands.RebootVMCmd;
import com.cloud.api.commands.RecoverVMCmd; import com.cloud.api.commands.RecoverVMCmd;
import com.cloud.api.commands.ResetVMPasswordCmd; import com.cloud.api.commands.ResetVMPasswordCmd;
import com.cloud.api.commands.StartVMCmd; import com.cloud.api.commands.StartVMCmd;
@ -1080,8 +1081,7 @@ public class UserVmManagerImpl implements UserVmManager {
} }
} }
@Override private boolean rebootVirtualMachine(long userId, long vmId) {
public boolean rebootVirtualMachine(long userId, long vmId) {
UserVmVO vm = _vmDao.findById(vmId); UserVmVO vm = _vmDao.findById(vmId);
if (vm == null || vm.getState() == State.Destroyed || vm.getState() == State.Expunging || vm.getRemoved() != null) { if (vm == null || vm.getState() == State.Destroyed || vm.getState() == State.Expunging || vm.getRemoved() != null) {
@ -3250,6 +3250,36 @@ public class UserVmManagerImpl implements UserVmManager {
return startVirtualMachine(userId, id, null, null, eventId); return startVirtualMachine(userId, id, null, null, eventId);
} }
@Override
public boolean rebootVirtualMachine(RebootVMCmd cmd) {
Account account = (Account)UserContext.current().getAccountObject();
Long userId = UserContext.current().getUserId();
Long vmId = cmd.getId();
//Verify input parameters
UserVmVO vmInstance = _vmDao.findById(vmId.longValue());
if (vmInstance == null) {
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId);
}
userId = accountAndUserValidation(vmId, account, userId, vmInstance);
EventUtils.saveScheduledEvent(userId, vmInstance.getAccountId(), EventTypes.EVENT_VM_REBOOT, "Rebooting Vm with Id: "+vmId);
boolean status = rebootVirtualMachine(userId, vmId);
if(status)
{
EventUtils.saveEvent(userId, vmInstance.getAccountId(), EventTypes.EVENT_VM_REBOOT, "Successfully rebooted vm with id:"+vmId);
return status;
}
else
{
EventUtils.saveEvent(userId, vmInstance.getAccountId(), EventTypes.EVENT_VM_REBOOT, "Failed to reboot vm with id:"+vmId);
return status;
}
}
} }