Search in sources :

Example 6 with UserDataServiceProvider

use of com.cloud.network.element.UserDataServiceProvider in project cloudstack by apache.

the class UserVmManagerImpl method resetVMSSHKeyInternal.

private boolean resetVMSSHKeyInternal(Long vmId, String sshPublicKey, String password) throws ResourceUnavailableException, InsufficientCapacityException {
    Long userId = CallContext.current().getCallingUserId();
    VMInstanceVO vmInstance = _vmDao.findById(vmId);
    VMTemplateVO template = _templateDao.findByIdIncludingRemoved(vmInstance.getTemplateId());
    Nic defaultNic = _networkModel.getDefaultNic(vmId);
    if (defaultNic == null) {
        s_logger.error("Unable to reset SSH Key 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, _networkModel.isSecurityGroupSupportedInNetwork(defaultNetwork), _networkModel.getNetworkTag(template.getHypervisorType(), defaultNetwork));
    VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vmInstance);
    if (template.getEnablePassword()) {
        vmProfile.setParameter(VirtualMachineProfile.Param.VmPassword, password);
    }
    UserDataServiceProvider element = _networkMgr.getSSHKeyResetProvider(defaultNetwork);
    if (element == null) {
        throw new CloudRuntimeException("Can't find network element for " + Service.UserData.getName() + " provider needed for SSH Key reset");
    }
    boolean result = element.saveSSHKey(defaultNetwork, defaultNicProfile, vmProfile, sshPublicKey);
    // 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 SSH Key for the virutal machine; no need to reboot the vm");
        return false;
    } else {
        if (vmInstance.getState() == State.Stopped) {
            s_logger.debug("Vm " + vmInstance + " is stopped, not rebooting it as a part of SSH Key reset");
            return true;
        }
        if (rebootVirtualMachine(userId, vmId) == null) {
            s_logger.warn("Failed to reboot the vm " + vmInstance);
            return false;
        } else {
            s_logger.debug("Vm " + vmInstance + " is rebooted successfully as a part of SSH Key reset");
            return true;
        }
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) VMTemplateVO(com.cloud.storage.VMTemplateVO) UserDataServiceProvider(com.cloud.network.element.UserDataServiceProvider)

Aggregations

UserDataServiceProvider (com.cloud.network.element.UserDataServiceProvider)6 Network (com.cloud.network.Network)5 PhysicalNetwork (com.cloud.network.PhysicalNetwork)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 VMTemplateVO (com.cloud.storage.VMTemplateVO)2 UserVm (com.cloud.uservm.UserVm)2 Test (org.junit.Test)2 Capability (com.cloud.network.Network.Capability)1 DhcpServiceProvider (com.cloud.network.element.DhcpServiceProvider)1 DnsServiceProvider (com.cloud.network.element.DnsServiceProvider)1