mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Refactoring the resetVmPassword command
This commit is contained in:
parent
78846ec4eb
commit
25d5126f68
@ -18,31 +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.storage.VMTemplateVO;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.vm.UserVmVO;
|
||||
|
||||
public class ResetVMPasswordCmd extends BaseCmd {
|
||||
import com.cloud.api.BaseCmd.Manager;
|
||||
|
||||
@Implementation(method="resetVMPassword", manager=Manager.UserVmManager)
|
||||
public class ResetVMPasswordCmd extends BaseAsyncCmd {
|
||||
public static final Logger s_logger = Logger.getLogger(ResetVMPasswordCmd.class.getName());
|
||||
|
||||
private static final String s_name = "resetpasswordforvirtualmachineresponse";
|
||||
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 /////////////////////
|
||||
@ -67,53 +54,55 @@ public class ResetVMPasswordCmd 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) {
|
||||
// Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName());
|
||||
// Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
|
||||
// Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
|
||||
// String password = null;
|
||||
//
|
||||
// //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 reset password.");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // If command is executed via 8096 port, set userId to the id of System account (1)
|
||||
// if (userId == null) {
|
||||
// userId = Long.valueOf(1);
|
||||
// }
|
||||
//
|
||||
// VMTemplateVO template = getManagementServer().findTemplateById(vmInstance.getTemplateId());
|
||||
// if (template.getEnablePassword()) {
|
||||
// password = getManagementServer().generateRandomPassword();
|
||||
// } else {
|
||||
// password = "saved_password";
|
||||
// }
|
||||
//
|
||||
// long jobId = getManagementServer().resetVMPasswordAsync(userId.longValue(), vmId, password);
|
||||
// if(jobId == 0) {
|
||||
// s_logger.warn("Unable to schedule async-job for ResetVMPassword comamnd");
|
||||
// } else {
|
||||
// if(s_logger.isDebugEnabled())
|
||||
// s_logger.debug("ResetVMPassword 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 Construct response from the executor
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pair<String, Object>> execute(Map<String, Object> params) {
|
||||
Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName());
|
||||
Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
|
||||
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
|
||||
String password = null;
|
||||
|
||||
//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 reset password.");
|
||||
}
|
||||
}
|
||||
|
||||
// If command is executed via 8096 port, set userId to the id of System account (1)
|
||||
if (userId == null) {
|
||||
userId = Long.valueOf(1);
|
||||
}
|
||||
|
||||
VMTemplateVO template = getManagementServer().findTemplateById(vmInstance.getTemplateId());
|
||||
if (template.getEnablePassword()) {
|
||||
password = getManagementServer().generateRandomPassword();
|
||||
} else {
|
||||
password = "saved_password";
|
||||
}
|
||||
|
||||
long jobId = getManagementServer().resetVMPasswordAsync(userId.longValue(), vmId, password);
|
||||
if(jobId == 0) {
|
||||
s_logger.warn("Unable to schedule async-job for ResetVMPassword comamnd");
|
||||
} else {
|
||||
if(s_logger.isDebugEnabled())
|
||||
s_logger.debug("ResetVMPassword 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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -609,8 +609,8 @@ public interface ManagementServer {
|
||||
* @param password the password for the virtual machine
|
||||
* @return true or false, based on the success of the method
|
||||
*/
|
||||
boolean resetVMPassword(long userId, long vmId, String password);
|
||||
long resetVMPasswordAsync(long userId, long vmId, String password);
|
||||
// boolean resetVMPassword(long userId, long vmId, String password);
|
||||
// long resetVMPasswordAsync(long userId, long vmId, String password);
|
||||
|
||||
/**
|
||||
* Attaches the specified volume to the specified VM
|
||||
|
||||
@ -1939,26 +1939,26 @@ public class ManagementServerImpl implements ManagementServer {
|
||||
return PasswordGenerator.generateRandomPassword();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean resetVMPassword(long userId, long vmId, String password) {
|
||||
if (password == null || password.equals("")) {
|
||||
return false;
|
||||
}
|
||||
boolean succeed = _vmMgr.resetVMPassword(userId, vmId, password);
|
||||
|
||||
// Log event
|
||||
UserVmVO userVm = _userVmDao.findById(vmId);
|
||||
if (userVm != null) {
|
||||
if (succeed) {
|
||||
EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_VM_RESETPASSWORD, "successfully reset password for VM : " + userVm.getName(), null);
|
||||
} else {
|
||||
EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_ERROR, EventTypes.EVENT_VM_RESETPASSWORD, "unable to reset password for VM : " + userVm.getName(), null);
|
||||
}
|
||||
} else {
|
||||
s_logger.warn("Unable to find vm = " + vmId + " to reset password");
|
||||
}
|
||||
return succeed;
|
||||
}
|
||||
// @Override
|
||||
// public boolean resetVMPassword(long userId, long vmId, String password) {
|
||||
// if (password == null || password.equals("")) {
|
||||
// return false;
|
||||
// }
|
||||
// boolean succeed = _vmMgr.resetVMPassword(userId, vmId, password);
|
||||
//
|
||||
// // Log event
|
||||
// UserVmVO userVm = _userVmDao.findById(vmId);
|
||||
// if (userVm != null) {
|
||||
// if (succeed) {
|
||||
// EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_VM_RESETPASSWORD, "successfully reset password for VM : " + userVm.getName(), null);
|
||||
// } else {
|
||||
// EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_ERROR, EventTypes.EVENT_VM_RESETPASSWORD, "unable to reset password for VM : " + userVm.getName(), null);
|
||||
// }
|
||||
// } else {
|
||||
// s_logger.warn("Unable to find vm = " + vmId + " to reset password");
|
||||
// }
|
||||
// return succeed;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void attachVolumeToVM(long vmId, long volumeId, Long deviceId, long startEventId) throws InternalErrorException {
|
||||
@ -2186,21 +2186,21 @@ public class ManagementServerImpl implements ManagementServer {
|
||||
return _asyncMgr.submitAsyncJob(job, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long resetVMPasswordAsync(long userId, long vmId, String password) {
|
||||
ResetVMPasswordParam param = new ResetVMPasswordParam(userId, vmId, password);
|
||||
Gson gson = GsonHelper.getBuilder().create();
|
||||
|
||||
UserVm vm = _userVmDao.findById(vmId);
|
||||
AsyncJobVO job = new AsyncJobVO();
|
||||
job.setUserId(UserContext.current().getUserId());
|
||||
job.setAccountId(vm.getAccountId());
|
||||
job.setCmd("ResetVMPassword");
|
||||
job.setCmdInfo(gson.toJson(param));
|
||||
job.setCmdOriginator("virtualmachine");
|
||||
|
||||
return _asyncMgr.submitAsyncJob(job, true);
|
||||
}
|
||||
// @Override
|
||||
// public long resetVMPasswordAsync(long userId, long vmId, String password) {
|
||||
// ResetVMPasswordParam param = new ResetVMPasswordParam(userId, vmId, password);
|
||||
// Gson gson = GsonHelper.getBuilder().create();
|
||||
//
|
||||
// UserVm vm = _userVmDao.findById(vmId);
|
||||
// AsyncJobVO job = new AsyncJobVO();
|
||||
// job.setUserId(UserContext.current().getUserId());
|
||||
// job.setAccountId(vm.getAccountId());
|
||||
// job.setCmd("ResetVMPassword");
|
||||
// job.setCmdInfo(gson.toJson(param));
|
||||
// job.setCmdOriginator("virtualmachine");
|
||||
//
|
||||
// return _asyncMgr.submitAsyncJob(job, true);
|
||||
// }
|
||||
|
||||
private boolean validPassword(String password) {
|
||||
for (int i = 0; i < password.length(); i++) {
|
||||
|
||||
@ -22,6 +22,7 @@ import java.util.List;
|
||||
|
||||
import com.cloud.agent.api.VmStatsEntry;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.api.commands.ResetVMPasswordCmd;
|
||||
import com.cloud.api.commands.StartVMCmd;
|
||||
import com.cloud.api.commands.StopVMCmd;
|
||||
import com.cloud.api.commands.UpdateVMCmd;
|
||||
@ -107,7 +108,7 @@ public interface UserVmManager extends Manager, VirtualMachineManager<UserVmVO>
|
||||
* @param password the password of the virtual machine.
|
||||
* @param true if reset worked successfully, false otherwise
|
||||
*/
|
||||
boolean resetVMPassword(long userId, long vmId, String password);
|
||||
boolean resetVMPassword(ResetVMPasswordCmd cmd);
|
||||
|
||||
/**
|
||||
* Attaches the specified volume to the specified VM
|
||||
|
||||
@ -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.ResetVMPasswordCmd;
|
||||
import com.cloud.api.commands.StartVMCmd;
|
||||
import com.cloud.api.commands.StopVMCmd;
|
||||
import com.cloud.api.commands.UpdateVMCmd;
|
||||
@ -176,6 +177,7 @@ import com.cloud.uservm.UserVm;
|
||||
import com.cloud.utils.DateUtil;
|
||||
import com.cloud.utils.NumbersUtil;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.PasswordGenerator;
|
||||
import com.cloud.utils.component.Adapters;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
import com.cloud.utils.component.Inject;
|
||||
@ -270,17 +272,62 @@ public class UserVmManagerImpl implements UserVmManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean resetVMPassword(long userId, long vmId, String password) {
|
||||
UserVmVO vm = _vmDao.findById(vmId);
|
||||
VMTemplateVO template = _templateDao.findById(vm.getTemplateId());
|
||||
public boolean resetVMPassword(ResetVMPasswordCmd cmd){
|
||||
|
||||
Long userId = UserContext.current().getUserId();
|
||||
boolean result = resetVMPasswordInternal(cmd);
|
||||
|
||||
// Log event
|
||||
UserVmVO userVm = _userVmDao.findById(cmd.getId());
|
||||
if (userVm != null) {
|
||||
if (result) {
|
||||
EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_VM_RESETPASSWORD, "successfully reset password for VM : " + userVm.getName(), null);
|
||||
} else {
|
||||
EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_ERROR, EventTypes.EVENT_VM_RESETPASSWORD, "unable to reset password for VM : " + userVm.getName(), null);
|
||||
}
|
||||
} else {
|
||||
s_logger.warn("Unable to find vm = " + cmd.getId()+ " to reset password");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean resetVMPasswordInternal(ResetVMPasswordCmd cmd) {
|
||||
|
||||
//Input validation
|
||||
Account account = (Account)UserContext.current().getAccountObject();
|
||||
Long userId = UserContext.current().getUserId();
|
||||
Long id = cmd.getId();
|
||||
|
||||
String password = null;
|
||||
|
||||
//Verify input parameters
|
||||
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);
|
||||
}
|
||||
|
||||
userId = accountAndUserValidation(id, account, userId, vmInstance);
|
||||
|
||||
VMTemplateVO template = _templateDao.findById(vmInstance.getTemplateId());
|
||||
if (template.getEnablePassword()) {
|
||||
password = PasswordGenerator.generateRandomPassword();;
|
||||
} else {
|
||||
password = "saved_password";
|
||||
}
|
||||
|
||||
if (password == null || password.equals("")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (template.getEnablePassword()) {
|
||||
if (vm.getDomainRouterId() == null)
|
||||
if (vmInstance.getDomainRouterId() == null)
|
||||
/*TODO: add it for external dhcp mode*/
|
||||
return true;
|
||||
if (_networkMgr.savePasswordToRouter(vm.getDomainRouterId(), vm.getPrivateIpAddress(), password)) {
|
||||
if (_networkMgr.savePasswordToRouter(vmInstance.getDomainRouterId(), vmInstance.getPrivateIpAddress(), password)) {
|
||||
// Need to reboot the virtual machine so that the password gets redownloaded from the DomR, and reset on the VM
|
||||
if (!rebootVirtualMachine(userId, vmId)) {
|
||||
if (vm.getState() == State.Stopped) {
|
||||
if (!rebootVirtualMachine(userId, id)) {
|
||||
if (vmInstance.getState() == State.Stopped) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user