mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Refactoring the rebootvm cmd
This commit is contained in:
parent
be75946b1f
commit
dc45e9b08b
@ -18,29 +18,17 @@
|
||||
|
||||
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;
|
||||
import com.cloud.api.BaseCmd.Manager;
|
||||
|
||||
public class RebootVMCmd extends BaseCmd {
|
||||
@Implementation(method="rebootVirtualMachine", manager=Manager.UserVmManager)
|
||||
public class RebootVMCmd extends BaseAsyncCmd {
|
||||
public static final Logger s_logger = Logger.getLogger(RebootVMCmd.class.getName());
|
||||
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 /////////////////////
|
||||
@ -65,46 +53,48 @@ public class RebootVMCmd 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 userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
|
||||
// Long vmId = (Long)params.get(BaseCmd.Properties.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())) {
|
||||
// // 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.");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 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().rebootVirtualMachineAsync(userId.longValue(), vmId.longValue());
|
||||
// if(jobId == 0) {
|
||||
// s_logger.warn("Unable to schedule async-job for RebootVM comamnd");
|
||||
// } else {
|
||||
// if(s_logger.isDebugEnabled())
|
||||
// s_logger.debug("RebootVM 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 userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
|
||||
Long vmId = (Long)params.get(BaseCmd.Properties.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())) {
|
||||
// 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.");
|
||||
}
|
||||
}
|
||||
|
||||
// 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().rebootVirtualMachineAsync(userId.longValue(), vmId.longValue());
|
||||
if(jobId == 0) {
|
||||
s_logger.warn("Unable to schedule async-job for RebootVM comamnd");
|
||||
} else {
|
||||
if(s_logger.isDebugEnabled())
|
||||
s_logger.debug("RebootVM 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;
|
||||
public String getResponse() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -698,7 +698,7 @@ public interface ManagementServer {
|
||||
* @param vmId
|
||||
* @return true if successfully rebooted, false otherwise
|
||||
*/
|
||||
boolean rebootVirtualMachine(long userId, long vmId);
|
||||
// boolean rebootVirtualMachine(long userId, long vmId);
|
||||
|
||||
/**
|
||||
* Reboots a Virtual Machine
|
||||
@ -706,7 +706,7 @@ public interface ManagementServer {
|
||||
* @param vmId
|
||||
* @return the async-call job id
|
||||
*/
|
||||
long rebootVirtualMachineAsync(long userId, long vmId);
|
||||
// long rebootVirtualMachineAsync(long userId, long vmId);
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@ -2631,31 +2631,31 @@ public class ManagementServerImpl implements ManagementServer {
|
||||
return _asyncMgr.submitAsyncJob(job, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean rebootVirtualMachine(long userId, long vmId) {
|
||||
return _vmMgr.rebootVirtualMachine(userId, vmId);
|
||||
}
|
||||
// @Override
|
||||
// public boolean rebootVirtualMachine(long userId, long vmId) {
|
||||
// return _vmMgr.rebootVirtualMachine(userId, vmId);
|
||||
// }
|
||||
|
||||
@Override
|
||||
public long rebootVirtualMachineAsync(long userId, long vmId) {
|
||||
|
||||
UserVmVO userVm = _userVmDao.findById(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);
|
||||
Gson gson = GsonHelper.getBuilder().create();
|
||||
|
||||
AsyncJobVO job = new AsyncJobVO();
|
||||
job.setUserId(UserContext.current().getUserId());
|
||||
job.setAccountId(userVm.getAccountId());
|
||||
job.setCmd("RebootVM");
|
||||
job.setCmdInfo(gson.toJson(param));
|
||||
|
||||
// use the same result result object name as StartVMCmd
|
||||
job.setCmdOriginator(StartVMCmd.getResultObjectName());
|
||||
return _asyncMgr.submitAsyncJob(job, true);
|
||||
}
|
||||
// @Override
|
||||
// public long rebootVirtualMachineAsync(long userId, long vmId) {
|
||||
//
|
||||
// UserVmVO userVm = _userVmDao.findById(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);
|
||||
// Gson gson = GsonHelper.getBuilder().create();
|
||||
//
|
||||
// AsyncJobVO job = new AsyncJobVO();
|
||||
// job.setUserId(UserContext.current().getUserId());
|
||||
// job.setAccountId(userVm.getAccountId());
|
||||
// job.setCmd("RebootVM");
|
||||
// job.setCmdInfo(gson.toJson(param));
|
||||
//
|
||||
// // use the same result result object name as StartVMCmd
|
||||
// job.setCmdOriginator(StartVMCmd.getResultObjectName());
|
||||
// return _asyncMgr.submitAsyncJob(job, true);
|
||||
// }
|
||||
|
||||
@Override
|
||||
public boolean destroyVirtualMachine(long userId, long vmId) {
|
||||
|
||||
@ -22,6 +22,7 @@ import java.util.List;
|
||||
|
||||
import com.cloud.agent.api.VmStatsEntry;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.api.commands.RebootVMCmd;
|
||||
import com.cloud.api.commands.RecoverVMCmd;
|
||||
import com.cloud.api.commands.ResetVMPasswordCmd;
|
||||
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;
|
||||
|
||||
boolean rebootVirtualMachine(long userId, long vmId);
|
||||
boolean rebootVirtualMachine(RebootVMCmd cmd);
|
||||
OperationResponse executeRebootVM(RebootVMExecutor executor, VMOperationParam param);
|
||||
|
||||
boolean recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationException, InternalErrorException;
|
||||
|
||||
@ -66,6 +66,7 @@ import com.cloud.agent.manager.AgentManager;
|
||||
import com.cloud.alert.AlertManager;
|
||||
import com.cloud.api.BaseCmd;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.api.commands.RebootVMCmd;
|
||||
import com.cloud.api.commands.RecoverVMCmd;
|
||||
import com.cloud.api.commands.ResetVMPasswordCmd;
|
||||
import com.cloud.api.commands.StartVMCmd;
|
||||
@ -1080,8 +1081,7 @@ public class UserVmManagerImpl implements UserVmManager {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean rebootVirtualMachine(long userId, long vmId) {
|
||||
private boolean rebootVirtualMachine(long userId, long vmId) {
|
||||
UserVmVO vm = _vmDao.findById(vmId);
|
||||
|
||||
if (vm == null || vm.getState() == State.Destroyed || vm.getState() == State.Expunging || vm.getRemoved() != null) {
|
||||
@ -3251,5 +3251,35 @@ public class UserVmManagerImpl implements UserVmManager {
|
||||
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user