bug 8185: fixed java part of password reset feature. There are still changes needed in backend scripts (bugs 8253 and 8254 are filed separately)

status 8185: resolved fixed
This commit is contained in:
alena 2011-01-27 17:04:16 -08:00
parent bf50d61bf6
commit 52dd679d95
19 changed files with 216 additions and 190 deletions

View File

@ -23,11 +23,9 @@ public class SavePasswordCommand extends NetworkElementCommand {
String password;
String vmIpAddress;
String routerPrivateIpAddress;
String vmName;
protected SavePasswordCommand() {
protected SavePasswordCommand() {
}
@Override
@ -35,10 +33,9 @@ public class SavePasswordCommand extends NetworkElementCommand {
return true;
}
public SavePasswordCommand(String password, String vmIpAddress, String routerPrivateIpAddress, String vmName) {
public SavePasswordCommand(String password, String vmIpAddress, String vmName) {
this.password = password;
this.vmIpAddress = vmIpAddress;
this.routerPrivateIpAddress = routerPrivateIpAddress;
this.vmName = vmName;
}
@ -46,16 +43,14 @@ public class SavePasswordCommand extends NetworkElementCommand {
return password;
}
public String getRouterPrivateIpAddress() {
return routerPrivateIpAddress;
}
public String getVmIpAddress() {
return vmIpAddress;
}
public String getVmName() {
return vmName;
}
public String getVmName() {
return vmName;
}
}

View File

@ -20,8 +20,6 @@ package com.cloud.api.commands;
import java.util.List;
import javax.naming.InsufficientResourcesException;
import org.apache.log4j.Logger;
import com.cloud.api.ApiConstants;
@ -36,7 +34,6 @@ import com.cloud.event.EventTypes;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.exception.StorageUnavailableException;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.user.Account;
import com.cloud.user.UserContext;
@ -96,10 +93,6 @@ public class DeployVMCmd extends BaseAsyncCreateCmd {
@Parameter(name="keypair", type=CommandType.STRING, description="name of the ssh key pair used to login to the virtual machine")
private String sshKeyPairName;
// unexposed parameter needed for serializing/deserializing the command
@Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, expose=false)
private String password;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
@ -159,15 +152,6 @@ public class DeployVMCmd extends BaseAsyncCreateCmd {
return zoneId;
}
// not exposed parameter
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<Long> getNetworkIds() {
return networkIds;
}

View File

@ -17,8 +17,6 @@
*/
package com.cloud.api.commands;
import java.util.Random;
import org.apache.log4j.Logger;
import com.cloud.api.ApiConstants;
@ -106,11 +104,10 @@ public class ResetVMPasswordCmd extends BaseAsyncCmd {
public Long getInstanceId() {
return getId();
}
Random _rand = new Random(System.currentTimeMillis());
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException{
password = Long.toHexString(_rand.nextLong());
password = _mgr.generateRandomPassword();
UserVm result = _userVmService.resetVMPassword(this, password);
if (result != null){
UserVmResponse response = _responseGenerator.createUserVmResponse(result);

View File

@ -0,0 +1,12 @@
package com.cloud.network.vpn;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.network.Network;
import com.cloud.vm.NicProfile;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachineProfile;
public interface PasswordResetElement {
boolean savePassword(Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) throws ResourceUnavailableException;
}

View File

@ -40,6 +40,11 @@ import com.cloud.user.Account;
*/
public interface VirtualMachineProfile<T extends VirtualMachine> {
enum Param {
VmPassword,
ControlNic,
}
String getHostName();
String getInstanceName();
@ -59,7 +64,7 @@ public interface VirtualMachineProfile<T extends VirtualMachine> {
/**
* @return parameter specific for this type of virtual machine.
*/
Object getParameter(String name);
Object getParameter(Param name);
/**
* @return the hypervisor type needed for this virtual machine.
@ -106,7 +111,7 @@ public interface VirtualMachineProfile<T extends VirtualMachine> {
VirtualMachine.Type getType();
void setParameter(String name, Object value);
void setParameter(Param name, Object value);
void setBootLoaderType(BootloaderType bootLoader);
BootloaderType getBootLoaderType();

View File

@ -287,7 +287,7 @@ public class VirtualRoutingResource implements Manager {
protected synchronized Answer execute(final SavePasswordCommand cmd) {
final String password = cmd.getPassword();
final String routerPrivateIPAddress = cmd.getRouterPrivateIpAddress();
final String routerPrivateIPAddress = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
final String vmName = cmd.getVmName();
final String vmIpAddress = cmd.getVmIpAddress();
final String local = vmName;

View File

@ -199,7 +199,6 @@ import com.xensource.xenapi.PBD;
import com.xensource.xenapi.PIF;
import com.xensource.xenapi.Pool;
import com.xensource.xenapi.SR;
import com.xensource.xenapi.Session;
import com.xensource.xenapi.Task;
import com.xensource.xenapi.Types;
import com.xensource.xenapi.Types.BadServerResponse;
@ -1261,7 +1260,7 @@ public abstract class CitrixResourceBase implements ServerResource {
protected Answer execute(final SavePasswordCommand cmd) {
Connection conn = getConnection();
final String password = cmd.getPassword();
final String routerPrivateIPAddress = cmd.getRouterPrivateIpAddress();
final String routerPrivateIPAddress = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
final String vmName = cmd.getVmName();
final String vmIpAddress = cmd.getVmIpAddress();
final String local = vmName;

View File

@ -1482,14 +1482,14 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, ConsoleProx
s_logger.debug("Boot Args for " + profile + ": " + bootArgs);
}
profile.setParameter("control.nic", controlNic);
profile.setParameter(VirtualMachineProfile.Param.ControlNic, controlNic);
return true;
}
@Override
public boolean finalizeDeployment(Commands cmds, VirtualMachineProfile<ConsoleProxyVO> profile, DeployDestination dest, ReservationContext context) {
NicProfile controlNic = (NicProfile)profile.getParameter("control.nic");
NicProfile controlNic = (NicProfile)profile.getParameter(VirtualMachineProfile.Param.ControlNic);
CheckSshCommand check = new CheckSshCommand(profile.getInstanceName(), controlNic.getIp4Address(), 3922, 5, 20);
cmds.addCommand("checkSsh", check);

View File

@ -36,6 +36,7 @@ import com.cloud.network.Networks.TrafficType;
import com.cloud.network.addr.PublicIp;
import com.cloud.network.guru.NetworkGuru;
import com.cloud.network.rules.FirewallRule;
import com.cloud.network.vpn.PasswordResetElement;
import com.cloud.network.vpn.RemoteAccessVpnElement;
import com.cloud.offerings.NetworkOfferingVO;
import com.cloud.user.Account;
@ -160,4 +161,10 @@ public interface NetworkManager extends NetworkService {
Nic getNicInNetwork(long vmId, long networkId);
Nic getNicForTraffic(long vmId, TrafficType type);
Network getDefaultNetworkForVm(long vmId);
Nic getDefaultNic(long vmId);
List<? extends PasswordResetElement> getPasswordResetElements();
}

View File

@ -91,6 +91,7 @@ import com.cloud.network.guru.NetworkGuru;
import com.cloud.network.lb.LoadBalancingRulesManager;
import com.cloud.network.rules.FirewallRule;
import com.cloud.network.rules.RulesManager;
import com.cloud.network.vpn.PasswordResetElement;
import com.cloud.network.vpn.RemoteAccessVpnElement;
import com.cloud.offering.NetworkOffering;
import com.cloud.offering.NetworkOffering.Availability;
@ -2010,4 +2011,50 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
return profile;
}
@Override
public Network getDefaultNetworkForVm(long vmId) {
Nic defaultNic = getDefaultNic(vmId);
if (defaultNic == null) {
return null;
} else {
return _networksDao.findById(defaultNic.getNetworkId());
}
}
@Override
public Nic getDefaultNic(long vmId) {
List<NicVO> nics = _nicDao.listBy(vmId);
Nic defaultNic = null;
if (nics != null) {
for (Nic nic: nics) {
if (nic.isDefaultNic()) {
defaultNic = nic;
break;
}
}
} else {
s_logger.debug("Unable to find default network for the vm; vm doesn't have any nics");
return null;
}
if (defaultNic == null) {
s_logger.debug("Unable to find default network for the vm; vm doesn't have default nic");
}
return defaultNic;
}
@Override
public List<? extends PasswordResetElement> getPasswordResetElements() {
List<PasswordResetElement> elements = new ArrayList<PasswordResetElement>();
for (NetworkElement element : _networkElements) {
if (element instanceof PasswordResetElement) {
elements.add((PasswordResetElement) element);
}
}
return elements;
}
}

View File

@ -44,23 +44,25 @@ import com.cloud.network.dao.NetworkDao;
import com.cloud.network.router.VirtualNetworkApplianceManager;
import com.cloud.network.router.VirtualRouter;
import com.cloud.network.rules.FirewallRule;
import com.cloud.network.vpn.PasswordResetElement;
import com.cloud.offering.NetworkOffering;
import com.cloud.uservm.UserVm;
import com.cloud.utils.component.AdapterBase;
import com.cloud.utils.component.Inject;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.DomainRouterVO;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.UserVmManager;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachineProfile;
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.VirtualMachineProfile;
import com.cloud.vm.dao.DomainRouterDao;
import com.cloud.vm.dao.UserVmDao;
@Local(value=NetworkElement.class)
public class DhcpElement extends AdapterBase implements NetworkElement{
public class DhcpElement extends AdapterBase implements NetworkElement, PasswordResetElement{
private static final Logger s_logger = Logger.getLogger(DhcpElement.class);
private static final Map<Service, Map<Capability, String>> capabilities = setCapabilities();
@ -197,4 +199,13 @@ public class DhcpElement extends AdapterBase implements NetworkElement{
return true;
}
}
@Override
public boolean savePassword(Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) throws ResourceUnavailableException{
@SuppressWarnings("unchecked")
VirtualMachineProfile<UserVm> uservm = (VirtualMachineProfile<UserVm>)vm;
return _routerMgr.savePasswordToRouter(network, nic, uservm);
}
}

View File

@ -28,8 +28,6 @@ import org.apache.log4j.Logger;
import com.cloud.configuration.ConfigurationManager;
import com.cloud.dc.DataCenter;
import com.cloud.dc.DataCenter.NetworkType;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.deploy.DeployDestination;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
@ -50,7 +48,6 @@ import com.cloud.network.lb.LoadBalancingRule;
import com.cloud.network.lb.LoadBalancingRule.LbDestination;
import com.cloud.network.lb.LoadBalancingRulesManager;
import com.cloud.network.router.VirtualNetworkApplianceManager;
import com.cloud.network.router.VirtualRouter;
import com.cloud.network.rules.FirewallRule;
import com.cloud.network.rules.FirewallRule.Purpose;
import com.cloud.network.rules.PortForwardingRule;
@ -59,7 +56,6 @@ import com.cloud.offering.NetworkOffering;
import com.cloud.offerings.dao.NetworkOfferingDao;
import com.cloud.user.AccountManager;
import com.cloud.uservm.UserVm;
import com.cloud.utils.component.AdapterBase;
import com.cloud.utils.component.Inject;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.DomainRouterVO;
@ -67,14 +63,14 @@ import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.UserVmManager;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.VirtualMachineProfile;
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.dao.DomainRouterDao;
import com.cloud.vm.dao.UserVmDao;
@Local(value=NetworkElement.class)
public class VirtualRouterElement extends AdapterBase implements NetworkElement, RemoteAccessVpnElement {
public class VirtualRouterElement extends DhcpElement implements NetworkElement, RemoteAccessVpnElement {
private static final Logger s_logger = Logger.getLogger(VirtualRouterElement.class);
private static final Map<Service, Map<Capability, String>> capabilities = setCapabilities();
@ -110,10 +106,11 @@ public class VirtualRouterElement extends AdapterBase implements NetworkElement,
return true;
}
@Override
public boolean prepare(Network config, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
if (canHandle(config.getGuestType(), dest.getDataCenter())) {
public boolean prepare(Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
if (canHandle(network.getGuestType(), dest.getDataCenter())) {
if (vm.getType() != VirtualMachine.Type.User) {
return false;
}
@ -121,39 +118,12 @@ public class VirtualRouterElement extends AdapterBase implements NetworkElement,
@SuppressWarnings("unchecked")
VirtualMachineProfile<UserVm> uservm = (VirtualMachineProfile<UserVm>)vm;
return _routerMgr.addVirtualMachineIntoNetwork(config, nic, uservm, dest, context, false) != null;
return _routerMgr.addVirtualMachineIntoNetwork(network, nic, uservm, dest, context, false) != null;
} else {
return false;
}
}
@Override
public boolean release(Network config, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, ReservationContext context) {
return true;
}
@Override
public boolean shutdown(Network config, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException {
DomainRouterVO router = _routerDao.findByNetworkConfiguration(config.getId());
if (router == null) {
return true;
}
if (_routerMgr.stopRouter(router.getId()) != null) {
return true;
} else {
return false;
}
}
@Override
public boolean destroy(Network config) throws ConcurrentOperationException, ResourceUnavailableException{
DomainRouterVO router = _routerDao.findByNetworkConfiguration(config.getId());
if (router == null) {
return true;
}
return _routerMgr.destroyRouter(router.getId());
}
@Override
public boolean applyRules(Network config, List<? extends FirewallRule> rules) throws ResourceUnavailableException {
@ -242,7 +212,6 @@ public class VirtualRouterElement extends AdapterBase implements NetworkElement,
return false;
}
}
@Override
public Provider getProvider() {
@ -287,31 +256,4 @@ public class VirtualRouterElement extends AdapterBase implements NetworkElement,
return capabilities;
}
@Override
public boolean restart(Network network, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException{
DataCenter dc = _configMgr.getZone(network.getDataCenterId());
DomainRouterVO router = _routerDao.findByNetworkConfiguration(network.getId());
if (router == null) {
s_logger.trace("Can't find domain router in network " + network.getId());
return true;
}
VirtualRouter result = null;
if (canHandle(network.getGuestType(), dc)) {
if (router.getState() == State.Stopped) {
result = _routerMgr.startRouter(router.getId());
} else {
result = _routerMgr.rebootRouter(router.getId());
}
if (result == null) {
s_logger.warn("Failed to restart domain router " + router + " as a part of netowrk " + network + " restart");
return false;
} else {
return true;
}
}
return true;
}
}

View File

@ -62,11 +62,8 @@ public interface VirtualNetworkApplianceManager extends Manager, VirtualNetworkA
/**
* save a vm password on the router.
*
* @param routerId the ID of the router to save the password to
* @param vmIpAddress the IP address of the User VM that will use the password
* @param password the password to save to the router
*/
boolean savePasswordToRouter(long routerId, String vmIpAddress, String password);
boolean savePasswordToRouter(Network network, NicProfile nic, VirtualMachineProfile<UserVm> profile) throws ResourceUnavailableException;
boolean destroyRouter(long routerId) throws ResourceUnavailableException, ConcurrentOperationException;

View File

@ -165,6 +165,7 @@ import com.cloud.vm.VirtualMachineManager;
import com.cloud.vm.VirtualMachineName;
import com.cloud.vm.VirtualMachineProfile;
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.VirtualMachineProfile.Param;
import com.cloud.vm.dao.DomainRouterDao;
import com.cloud.vm.dao.NicDao;
import com.cloud.vm.dao.UserVmDao;
@ -414,21 +415,24 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
}
@Override
public boolean savePasswordToRouter(final long routerId, final String vmIpAddress, final String password) {
final DomainRouterVO router = _routerDao.findById(routerId);
final String routerPrivateIpAddress = router.getPrivateIpAddress();
final String vmName = router.getName();
final String encodedPassword = rot13(password);
final SavePasswordCommand cmdSavePassword = new SavePasswordCommand(encodedPassword, vmIpAddress, routerPrivateIpAddress, vmName);
if (router != null && router.getHostId() != null) {
final Answer answer = _agentMgr.easySend(router.getHostId(), cmdSavePassword);
return (answer != null && answer.getResult());
} else {
// either the router doesn't exist or router isn't running at all
return false;
public boolean savePasswordToRouter(Network network, NicProfile nic, VirtualMachineProfile<UserVm> profile) throws ResourceUnavailableException{
DomainRouterVO router = _routerDao.findByNetworkConfiguration(network.getId());
if (router == null) {
s_logger.warn("Unable save password, router doesn't exist in network " + network.getId());
throw new CloudRuntimeException("Unable to save password to router");
}
UserVm userVm = profile.getVirtualMachine();
String password = (String)profile.getParameter(Param.VmPassword);
String encodedPassword = rot13(password);
Commands cmds = new Commands(OnError.Continue);
SavePasswordCommand cmd = new SavePasswordCommand(encodedPassword, nic.getIp4Address(), userVm.getName());
cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress());
cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
cmds.addCommand("password", cmd);
return sendCommandsToRouter(router, cmds);
}
@ -1090,14 +1094,14 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
throw new CloudRuntimeException("Didn't start a control port");
}
profile.setParameter("control.nic", controlNic);
profile.setParameter(VirtualMachineProfile.Param.ControlNic, controlNic);
return true;
}
@Override
public boolean finalizeDeployment(Commands cmds, VirtualMachineProfile<DomainRouterVO> profile, DeployDestination dest, ReservationContext context) throws ResourceUnavailableException{
NicProfile controlNic = (NicProfile) profile.getParameter("control.nic");
NicProfile controlNic = (NicProfile) profile.getParameter(VirtualMachineProfile.Param.ControlNic);
_ovsNetworkMgr.RouterCheckAndCreateTunnel(cmds, profile, dest);
_ovsNetworkMgr.applyDefaultFlowToRouter(cmds, profile, dest);
@ -1320,7 +1324,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
_userVmDao.loadDetails((UserVmVO) profile.getVirtualMachine());
String password = profile.getVirtualMachine().getPassword();
String password = (String)profile.getParameter(VirtualMachineProfile.Param.VmPassword);
String userData = profile.getVirtualMachine().getUserData();
String sshPublicKey = profile.getVirtualMachine().getDetail("SSH.PublicKey");
Commands cmds = new Commands(OnError.Stop);
@ -1339,11 +1343,13 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_IP, routerControlIpAddress);
dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
cmds.addCommand("dhcp", dhcpCommand);
if (password != null) {
final String encodedPassword = rot13(password);
cmds.addCommand("password", new SavePasswordCommand(encodedPassword, nic.getIp4Address(), routerControlIpAddress, profile
.getVirtualMachine().getName()));
SavePasswordCommand cmd = new SavePasswordCommand(encodedPassword, nic.getIp4Address(), profile.getVirtualMachine().getName());
cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress());
cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName());
cmds.addCommand("password", cmd);
}
String serviceOffering = _serviceOfferingDao.findById(profile.getServiceOfferingId()).getDisplayText();

View File

@ -1115,7 +1115,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V
s_logger.debug("Boot Args for " + profile + ": " + bootArgs);
}
profile.setParameter("control.nic", controlNic);
profile.setParameter(VirtualMachineProfile.Param.ControlNic, controlNic);
return true;
}
@ -1123,7 +1123,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V
@Override
public boolean finalizeDeployment(Commands cmds, VirtualMachineProfile<SecondaryStorageVmVO> profile, DeployDestination dest,
ReservationContext context) {
NicProfile controlNic = (NicProfile) profile.getParameter("control.nic");
NicProfile controlNic = (NicProfile) profile.getParameter(VirtualMachineProfile.Param.ControlNic);
CheckSshCommand check = new CheckSshCommand(profile.getInstanceName(), controlNic.getIp4Address(), 3922, 5, 20);
cmds.addCommand("checkSsh", check);

View File

@ -124,6 +124,7 @@ import com.cloud.network.ovs.OvsTunnelManager;
import com.cloud.network.router.VirtualNetworkApplianceManager;
import com.cloud.network.rules.RulesManager;
import com.cloud.network.security.SecurityGroupManager;
import com.cloud.network.vpn.PasswordResetElement;
import com.cloud.offerings.dao.NetworkOfferingDao;
import com.cloud.server.Criteria;
import com.cloud.service.ServiceOfferingVO;
@ -310,42 +311,61 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
return userVm;
}
private boolean resetVMPasswordInternal(ResetVMPasswordCmd cmd, String password) throws ResourceUnavailableException, InsufficientCapacityException{
return true;
// Long vmId = cmd.getId();
// Long userId = UserContext.current().getCallerUserId();
// UserVmVO vmInstance = _vmDao.findById(vmId);
//
// if (password == null || password.equals("")) {
// return false;
// }
//
// VMTemplateVO template = _templateDao.findById(vmInstance.getTemplateId());
// if (template.getEnablePassword()) {
// if (vmInstance.getDomainRouterId() == null) {
// /*TODO: add it for external dhcp mode*/
// return true;
// }
// if (_routerMgr.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) == null) {
// if (vmInstance.getState() == State.Stopped) {
// return true;
// }
// return false;
// } else {
// return true;
// }
// } else {
// return false;
// }
// } else {
// if (s_logger.isDebugEnabled()) {
// s_logger.debug("Reset password called for a vm that is not using a password enabled template");
// }
// return false;
// }
private boolean resetVMPasswordInternal(ResetVMPasswordCmd cmd, String password) throws ResourceUnavailableException, InsufficientCapacityException{
Long vmId = cmd.getId();
Long userId = UserContext.current().getCallerUserId();
VMInstanceVO vmInstance = _vmDao.findById(vmId);
if (password == null || password.equals("")) {
return false;
}
VMTemplateVO template = _templateDao.findById(vmInstance.getTemplateId());
if (template.getEnablePassword()) {
Nic defaultNic = _networkMgr.getDefaultNic(vmId);
if (defaultNic == null) {
s_logger.error("Unable to reset password for vm " + vmInstance + " as the instance doesn't have default nic");
return false;
}
Network defaultNetwork = _networkDao.findById(defaultNic.getNetworkId());
NicProfile defaultNicProfile = new NicProfile(defaultNic, defaultNetwork, null, null, null);
VirtualMachineProfile<VMInstanceVO> vmProfile = new VirtualMachineProfileImpl<VMInstanceVO>(vmInstance);
vmProfile.setParameter(VirtualMachineProfile.Param.VmPassword, password);
List<? extends PasswordResetElement> elements = _networkMgr.getPasswordResetElements();
boolean result = true;
for (PasswordResetElement element : elements) {
if (!element.savePassword(defaultNetwork, defaultNicProfile, vmProfile)) {
result = false;
}
}
// Need to reboot the virtual machine so that the password gets redownloaded from the DomR, and reset on the VM
if (!result) {
s_logger.debug("Failed to reset password for the virutal machine; no need to reboot the vm");
return false;
} else {
if (rebootVirtualMachine(userId, vmId) == null) {
if (vmInstance.getState() == State.Stopped) {
s_logger.debug("Vm " + vmInstance + " is stopped, not rebooting it as a part of password reset");
return true;
}
s_logger.warn("Failed to reboot the vm " + vmInstance);
return false;
} else {
s_logger.debug("Vm " + vmInstance + " is rebooted successfully as a part of password reset");
return true;
}
}
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Reset password called for a vm that is not using a password enabled template");
}
return false;
}
}
@Override
@ -2124,7 +2144,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
if (password == null || password.equals("") || (!validPassword(password))) {
throw new InvalidParameterValueException("A valid password for this virtual machine was not provided.");
}
vm.setPassword(password);
// Check if an SSH key pair was selected for the instance and if so use it to encrypt & save the vm password
String sshPublicKey = vm.getDetail("SSH.PublicKey");
@ -2144,13 +2164,21 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
AccountVO owner = _accountDao.findById(vm.getAccountId());
try {
vm = _itMgr.start(vm, null, caller, owner);
Map<VirtualMachineProfile.Param, Object> params = new HashMap<VirtualMachineProfile.Param, Object>();
params.put(VirtualMachineProfile.Param.VmPassword, password);
vm = _itMgr.start(vm, params, caller, owner);
} finally {
updateVmStateForFailedVmCreation(vm.getId());
}
_networkGroupMgr.addInstanceToGroups(vm.getId(), cmd.getSecurityGroupList());
if (template.getEnablePassword()) {
//this value is not being sent to the backend; need only for api dispaly purposes
vm.setPassword(password);
}
return vm;
}

View File

@ -37,9 +37,6 @@ import com.cloud.user.Account;
import com.cloud.user.User;
import com.cloud.utils.Pair;
import com.cloud.utils.component.Manager;
import com.cloud.utils.fsm.StateListener;
import com.cloud.vm.VirtualMachine.Event;
import com.cloud.vm.VirtualMachine.State;
/**
* Manages allocating resources to vms.
@ -52,7 +49,7 @@ public interface VirtualMachineManager extends Manager {
Pair<? extends DiskOfferingVO, Long> rootDiskOffering,
List<Pair<DiskOfferingVO, Long>> dataDiskOfferings,
List<Pair<NetworkVO, NicProfile>> networks,
Map<String, Object> params,
Map<VirtualMachineProfile.Param, Object> params,
DeploymentPlan plan,
HypervisorType hyperType,
Account owner) throws InsufficientCapacityException;
@ -75,7 +72,7 @@ public interface VirtualMachineManager extends Manager {
HypervisorType hyperType,
Account owner) throws InsufficientCapacityException;
<T extends VMInstanceVO> T start(T vm, Map<String, Object> params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException;
<T extends VMInstanceVO> T start(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException;
<T extends VMInstanceVO> boolean stop(T vm, User caller, Account account) throws ResourceUnavailableException;
@ -85,7 +82,7 @@ public interface VirtualMachineManager extends Manager {
boolean stateTransitTo(VMInstanceVO vm, VirtualMachine.Event e, Long hostId);
<T extends VMInstanceVO> T advanceStart(T vm, Map<String, Object> params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException, OperationTimedoutException;
<T extends VMInstanceVO> T advanceStart(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException, OperationTimedoutException;
<T extends VMInstanceVO> boolean advanceStop(T vm, boolean forced, User caller, Account account) throws ResourceUnavailableException, OperationTimedoutException, ConcurrentOperationException;
@ -99,7 +96,7 @@ public interface VirtualMachineManager extends Manager {
<T extends VMInstanceVO> T migrate(T vm, long srcHostId, DeployDestination dest) throws ResourceUnavailableException;
<T extends VMInstanceVO> T reboot(T vm, Map<String, Object> params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException;
<T extends VMInstanceVO> T reboot(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException;
<T extends VMInstanceVO> T advanceReboot(T vm, Map<String, Object> params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException, OperationTimedoutException;
<T extends VMInstanceVO> T advanceReboot(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException, OperationTimedoutException;
}

View File

@ -183,7 +183,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, StateLi
Pair<? extends DiskOfferingVO, Long> rootDiskOffering,
List<Pair<DiskOfferingVO, Long>> dataDiskOfferings,
List<Pair<NetworkVO, NicProfile>> networks,
Map<String, Object> params,
Map<VirtualMachineProfile.Param, Object> params,
DeploymentPlan plan,
HypervisorType hyperType,
Account owner) throws InsufficientCapacityException {
@ -441,7 +441,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, StateLi
}
@Override
public <T extends VMInstanceVO> T start(T vm, Map<String, Object> params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException {
public <T extends VMInstanceVO> T start(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException {
try {
return advanceStart(vm, params, caller, account);
} catch (ConcurrentOperationException e) {
@ -555,7 +555,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, StateLi
}
@Override
public <T extends VMInstanceVO> T advanceStart(T vm, Map<String, Object> params, User caller, Account account) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
public <T extends VMInstanceVO> T advanceStart(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
long vmId = vm.getId();
VirtualMachineGuru<T> vmGuru = getVmGuru(vm);
@ -1086,7 +1086,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, StateLi
}
@Override
public <T extends VMInstanceVO> T reboot(T vm, Map<String, Object> params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException {
public <T extends VMInstanceVO> T reboot(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account) throws InsufficientCapacityException, ResourceUnavailableException {
try {
return advanceReboot(vm, params, caller, account);
} catch (ConcurrentOperationException e) {
@ -1095,8 +1095,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, StateLi
}
@Override
public <T extends VMInstanceVO> T advanceReboot(T vm, Map<String, Object> params, User caller, Account account) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
public <T extends VMInstanceVO> T advanceReboot(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
T rebootedVm = null;
DataCenter dc = _configMgr.getZone(vm.getDataCenterId());

View File

@ -43,7 +43,7 @@ public class VirtualMachineProfileImpl<T extends VMInstanceVO> implements Virtua
T _vm;
ServiceOfferingVO _offering;
VMTemplateVO _template;
Map<String, Object> _params;
Map<Param, Object> _params;
List<NicProfile> _nics = new ArrayList<NicProfile>();
List<VolumeTO> _disks = new ArrayList<VolumeTO>();
StringBuilder _bootArgs = new StringBuilder();
@ -52,14 +52,14 @@ public class VirtualMachineProfileImpl<T extends VMInstanceVO> implements Virtua
VirtualMachine.Type _type;
public VirtualMachineProfileImpl(T vm, VMTemplateVO template, ServiceOfferingVO offering, Account owner, Map<String, Object> params) {
public VirtualMachineProfileImpl(T vm, VMTemplateVO template, ServiceOfferingVO offering, Account owner, Map<Param, Object> params) {
_vm = vm;
_template = template;
_offering = offering;
_params = params;
_owner = owner;
if (_params == null) {
_params = new HashMap<String, Object>();
_params = new HashMap<Param, Object>();
}
_type = vm.getType();
}
@ -91,7 +91,7 @@ public class VirtualMachineProfileImpl<T extends VMInstanceVO> implements Virtua
}
@Override
public void setParameter(String name, Object value) {
public void setParameter(Param name, Object value) {
_params.put(name, value);
}
@ -206,7 +206,7 @@ public class VirtualMachineProfileImpl<T extends VMInstanceVO> implements Virtua
}
@Override
public Object getParameter(String name) {
public Object getParameter(Param name) {
return _params.get(name);
}