diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index 4a0b0b093f4..07efe960554 100644 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -416,6 +416,10 @@ public class ApiResponseHelper { userVmResponse.setTemplateDisplayText("ISO Boot"); userVmResponse.setPasswordEnabled(false); } + + if (userVm.getPassword() != null) { + userVmResponse.setPassword(userVm.getPassword()); + } // ISO Info if (userVm.getIsoId() != null) { diff --git a/server/src/com/cloud/api/commands/DeployVMCmd.java b/server/src/com/cloud/api/commands/DeployVMCmd.java index 14a4355c426..3dde8099753 100644 --- a/server/src/com/cloud/api/commands/DeployVMCmd.java +++ b/server/src/com/cloud/api/commands/DeployVMCmd.java @@ -39,6 +39,7 @@ import com.cloud.exception.InvalidParameterValueException; import com.cloud.exception.PermissionDeniedException; import com.cloud.exception.ResourceAllocationException; import com.cloud.exception.StorageUnavailableException; +import com.cloud.storage.VMTemplateVO; import com.cloud.user.Account; import com.cloud.user.UserContext; import com.cloud.uservm.UserVm; @@ -200,13 +201,17 @@ public class DeployVMCmd extends BaseAsyncCmd { @Override public void execute() throws ServerApiException, InvalidParameterValueException, PermissionDeniedException, InsufficientAddressCapacityException, InsufficientCapacityException, ConcurrentOperationException, StorageUnavailableException{ try { - UserVm result = _mgr.deployVirtualMachine(this); - UserVmResponse response = ApiResponseHelper.createUserVmResponse(result); - // FIXME: where will the password come from in this case? -// if (templatePasswordEnabled) { -// response.setPassword(getPassword()); -// } + String password = null; + if (templateId != null ) { + VMTemplateVO template = ApiDBUtils.findTemplateById(templateId); + if (template.getEnablePassword()) { + password = _mgr.generateRandomPassword(); + } + } + UserVm result = _mgr.deployVirtualMachine(this, password); + UserVmResponse response = ApiResponseHelper.createUserVmResponse(result); + response.setPassword(password); response.setResponseName(getName()); this.setResponseObject(response); } catch (ResourceAllocationException ex) { diff --git a/server/src/com/cloud/api/commands/ResetVMPasswordCmd.java b/server/src/com/cloud/api/commands/ResetVMPasswordCmd.java index 792c38f5788..84647ef1f65 100644 --- a/server/src/com/cloud/api/commands/ResetVMPasswordCmd.java +++ b/server/src/com/cloud/api/commands/ResetVMPasswordCmd.java @@ -36,6 +36,7 @@ import com.cloud.exception.PermissionDeniedException; import com.cloud.exception.StorageUnavailableException; import com.cloud.user.Account; import com.cloud.uservm.UserVm; +import com.cloud.utils.PasswordGenerator; @Implementation(description="Resets the password for virtual machine. " + "The virtual machine must be in a \"Stopped\" state and the template must already " + @@ -103,13 +104,12 @@ public class ResetVMPasswordCmd extends BaseAsyncCmd { @Override public void execute() throws ServerApiException, InvalidParameterValueException, PermissionDeniedException, InsufficientAddressCapacityException, InsufficientCapacityException, ConcurrentOperationException, StorageUnavailableException{ - UserVm result = _userVmService.resetVMPassword(this); + + password = _mgr.generateRandomPassword(); + UserVm result = _userVmService.resetVMPassword(this, password); + UserVmResponse response = ApiResponseHelper.createUserVmResponse(result); - // FIXME: where will the password come from in this case? -// if (templatePasswordEnabled) { -// response.setPassword(getPassword()); -// } response.setResponseName(getName()); this.setResponseObject(response); } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 1e146b7c897..e86dc14b6c8 100755 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -301,7 +301,7 @@ public interface ManagementServer { * @throws StorageUnavailableException * @throws ConcurrentOperationException */ - UserVm deployVirtualMachine(DeployVMCmd cmd) throws ResourceAllocationException, InsufficientStorageCapacityException, ExecutionException, StorageUnavailableException, ConcurrentOperationException; + UserVm deployVirtualMachine(DeployVMCmd cmd, String password) throws ResourceAllocationException, InsufficientStorageCapacityException, ExecutionException, StorageUnavailableException, ConcurrentOperationException; /** * Finds a domain router by user and data center diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 2f2b5ab10c8..1a42309ecb0 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -1101,7 +1101,7 @@ public class ManagementServerImpl implements ManagementServer { } @Override - public UserVm deployVirtualMachine(DeployVMCmd cmd) throws ResourceAllocationException, + public UserVm deployVirtualMachine(DeployVMCmd cmd, String password) throws ResourceAllocationException, InsufficientStorageCapacityException, ExecutionException, StorageUnavailableException, ConcurrentOperationException { Account ctxAccount = UserContext.current().getAccount(); @@ -1114,7 +1114,6 @@ public class ManagementServerImpl implements ManagementServer { long templateId = cmd.getTemplateId(); Long diskOfferingId = cmd.getDiskOfferingId(); String domain = null; // FIXME: this was hardcoded to null in DeployVMCmd in the old framework, do we need it? - String password = generateRandomPassword(); String displayName = cmd.getDisplayName(); String group = cmd.getGroup(); String userData = cmd.getUserData(); diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 327113d47b8..a6fa2b8efcc 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -284,62 +284,53 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, VirtualM } @Override - public UserVm resetVMPassword(ResetVMPasswordCmd cmd){ - + public UserVm resetVMPassword(ResetVMPasswordCmd cmd, String password){ + Account account = UserContext.current().getAccount(); Long userId = UserContext.current().getUserId(); - boolean result = resetVMPasswordInternal(cmd); - - // Log event - UserVmVO userVm = _vmDao.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.getHostName(), null); - } else { - EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_ERROR, EventTypes.EVENT_VM_RESETPASSWORD, "unable to reset password for VM : " + userVm.getHostName(), null); - } + Long vmId = cmd.getId(); + UserVmVO userVm = _vmDao.findById(cmd.getId()); + + //Do parameters input validation + if (userVm == null) { + throw new InvalidParameterValueException("unable to find a virtual machine with id " + cmd.getId()); + } + + VMTemplateVO template = _templateDao.findById(userVm.getTemplateId()); + if (template == null || !template.getEnablePassword()) { + throw new InvalidParameterValueException("Fail to reset password for the virtual machine, the template is not password enabled"); + } + + userId = accountAndUserValidation(vmId, account, userId, userVm); + + boolean result = resetVMPasswordInternal(cmd, password); + + if (result) { + userVm.setPassword(password); + EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_VM_RESETPASSWORD, "successfully reset password for VM : " + userVm.getHostName(), null); } else { - s_logger.warn("Unable to find vm = " + cmd.getId()+ " to reset password"); + EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_ERROR, EventTypes.EVENT_VM_RESETPASSWORD, "unable to reset password for VM : " + userVm.getHostName(), null); } - + return userVm; } - private boolean resetVMPasswordInternal(ResetVMPasswordCmd cmd) { - //Input validation - Account account = UserContext.current().getAccount(); - Long userId = UserContext.current().getUserId(); - Long id = cmd.getId(); - - String password = null; - - //Verify input parameters - UserVmVO vmInstance = _vmDao.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(6);; - } else { - password = "saved_password"; - } + private boolean resetVMPasswordInternal(ResetVMPasswordCmd cmd, String password) { + Long vmId = cmd.getId(); + Long userId = UserContext.current().getUserId(); + UserVmVO vmInstance = _vmDao.findById(vmId); if (password == null || password.equals("")) { return false; - } else { - cmd.setPassword(password); } + VMTemplateVO template = _templateDao.findById(vmInstance.getTemplateId()); if (template.getEnablePassword()) { if (vmInstance.getDomainRouterId() == null) /*TODO: add it for external dhcp mode*/ return true; 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, id)) { + if (!rebootVirtualMachine(userId, vmId)) { if (vmInstance.getState() == State.Stopped) { return true; } diff --git a/server/src/com/cloud/vm/UserVmService.java b/server/src/com/cloud/vm/UserVmService.java index f7e1fc20981..ee790b1a281 100755 --- a/server/src/com/cloud/vm/UserVmService.java +++ b/server/src/com/cloud/vm/UserVmService.java @@ -61,7 +61,7 @@ public interface UserVmService { * @param cmd - the command specifying vmId, password * @return the VM if reset worked successfully, null otherwise */ - UserVm resetVMPassword(ResetVMPasswordCmd cmd); + UserVm resetVMPassword(ResetVMPasswordCmd cmd, String password); /** * Attaches the specified volume to the specified VM