diff --git a/server/src/com/cloud/api/commands/RebootVMCmd.java b/server/src/com/cloud/api/commands/RebootVMCmd.java index 8550c16bceb..36c533c9dbe 100644 --- a/server/src/com/cloud/api/commands/RebootVMCmd.java +++ b/server/src/com/cloud/api/commands/RebootVMCmd.java @@ -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 RebootVMCmd extends BaseCmd { +import com.cloud.api.BaseCmd.Manager; + +@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> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); - } - + ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// @@ -64,47 +52,49 @@ public class RebootVMCmd extends BaseCmd { public String getName() { return s_name; } - - public List> getProperties() { - return s_properties; - } - @Override - public List> execute(Map 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> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); - return returnValues; - } +// @Override +// public List> execute(Map 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> returnValues = new ArrayList>(); +// returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); +// return returnValues; +// } + + @Override + public String getResponse() { + // TODO Auto-generated method stub + return null; + } } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 1b9bfd915f7..3188de9dbf7 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -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); /** diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index ae2b26196c6..e9753ee824a 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -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) { diff --git a/server/src/com/cloud/vm/UserVmManager.java b/server/src/com/cloud/vm/UserVmManager.java index 03947bc4801..e63624c1722 100644 --- a/server/src/com/cloud/vm/UserVmManager.java +++ b/server/src/com/cloud/vm/UserVmManager.java @@ -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 */ HashMap getVirtualMachineStatistics(long hostId, String hostName, List 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; diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 5dfa36cad0d..fd02cb56409 100644 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -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) { @@ -3250,6 +3250,36 @@ public class UserVmManagerImpl implements UserVmManager { 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; + } + } }