diff --git a/api/src/main/java/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java index c140a1e1c22a..1cbe28f4ddee 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java @@ -105,6 +105,10 @@ public class DeployVMCmd extends BaseAsyncCreateCustomIdCmd implements SecurityG @Parameter(name = ApiConstants.DISPLAY_NAME, type = CommandType.STRING, description = "an optional user generated name for the virtual machine") private String displayName; + @Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, description="The password of the virtual machine. If null, a random password will be generated for the VM.", + since="4.19.0.0") + protected String password; + //Owner information @Parameter(name = ApiConstants.ACCOUNT, type = CommandType.STRING, description = "an optional account for the virtual machine. Must be used with domainId.") private String accountName; @@ -464,6 +468,10 @@ public Long getZoneId() { return zoneId; } + public String getPassword() { + return password; + } + public List getNetworkIds() { if (MapUtils.isNotEmpty(vAppNetworks)) { if (CollectionUtils.isNotEmpty(networkIds) || ipAddress != null || getIp6Address() != null || MapUtils.isNotEmpty(ipToNetworkList)) { diff --git a/api/src/main/java/org/apache/cloudstack/api/command/user/vm/ResetVMPasswordCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/user/vm/ResetVMPasswordCmd.java index e275a988cd15..1cf4c929b32d 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/user/vm/ResetVMPasswordCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/user/vm/ResetVMPasswordCmd.java @@ -16,6 +16,7 @@ // under the License. package org.apache.cloudstack.api.command.user.vm; +import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.apache.cloudstack.acl.SecurityChecker.AccessType; @@ -56,8 +57,7 @@ public class ResetVMPasswordCmd extends BaseAsyncCmd implements UserCmd { required=true, description="The ID of the virtual machine") private Long id; - // unexposed parameter needed for serializing/deserializing the command - @Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, expose=false) + @Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, description="The new password of the virtual machine. If null, a random password will be generated for the VM.", since="4.19.0") protected String password; @@ -118,7 +118,14 @@ public Long getApiResourceId() { @Override public void execute() throws ResourceUnavailableException, InsufficientCapacityException { - password = _mgr.generateRandomPassword(); + password = getPassword(); + UserVm vm = _responseGenerator.findUserVmById(getId()); + if (StringUtils.isBlank(password)) { + password = _mgr.generateRandomPassword(); + s_logger.debug(String.format("Resetting VM [%s] password to a randomly generated password.", vm.getUuid())); + } else { + s_logger.debug(String.format("Resetting VM [%s] password to password defined by user.", vm.getUuid())); + } CallContext.current().setEventDetails("Vm Id: " + getId()); UserVm result = _userVmService.resetVMPassword(this, password); if (result != null){ diff --git a/api/src/main/java/org/apache/cloudstack/api/response/NetworkResponse.java b/api/src/main/java/org/apache/cloudstack/api/response/NetworkResponse.java index 243579e2f710..d34f949372ae 100644 --- a/api/src/main/java/org/apache/cloudstack/api/response/NetworkResponse.java +++ b/api/src/main/java/org/apache/cloudstack/api/response/NetworkResponse.java @@ -163,6 +163,10 @@ public class NetworkResponse extends BaseResponseWithAssociatedNetwork implement @Param(description = "the domain name of the network owner") private String domain; + @SerializedName(ApiConstants.DOMAIN_PATH) + @Param(description = "path of the Domain the network belongs to", since = "4.19.0.0") + private String domainPath; + @SerializedName("isdefault") @Param(description = "true if network is default, false otherwise") private Boolean isDefault; @@ -420,6 +424,10 @@ public void setDomainName(String domain) { this.domain = domain; } + public void setDomainPath(String domainPath) { + this.domainPath = domainPath; + } + public void setNetworkOfferingAvailability(String networkOfferingAvailability) { this.networkOfferingAvailability = networkOfferingAvailability; } diff --git a/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDao.java b/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDao.java index bdb2534b62d0..42c00231aac1 100755 --- a/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDao.java +++ b/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDao.java @@ -164,5 +164,5 @@ public interface VMInstanceDao extends GenericDao, StateDao< void updateSystemVmTemplateId(long templateId, Hypervisor.HypervisorType hypervisorType); - List listByHostOrLastHostOrHostPod(long hostId, long podId); + List listByHostOrLastHostOrHostPod(List hostIds, long podId); } diff --git a/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java b/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java index 0e3d4bdde8f1..916687baeb4d 100755 --- a/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java +++ b/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java @@ -989,19 +989,19 @@ public void updateSystemVmTemplateId(long templateId, Hypervisor.HypervisorType } @Override - public List listByHostOrLastHostOrHostPod(long hostId, long podId) { + public List listByHostOrLastHostOrHostPod(List hostIds, long podId) { SearchBuilder sb = createSearchBuilder(); - sb.or().op("hostId", sb.entity().getHostId(), Op.EQ); - sb.or("lastHostId", sb.entity().getLastHostId(), Op.EQ); - sb.and().op("hostIdNull", sb.entity().getHostId(), SearchCriteria.Op.NULL); + sb.and().op("hostId", sb.entity().getHostId(), Op.IN); + sb.or("lastHostId", sb.entity().getLastHostId(), Op.IN); + sb.or().op("hostIdNull", sb.entity().getHostId(), SearchCriteria.Op.NULL); sb.and("lastHostIdNull", sb.entity().getHostId(), SearchCriteria.Op.NULL); sb.and("podId", sb.entity().getPodIdToDeployIn(), Op.EQ); sb.cp(); sb.cp(); sb.done(); SearchCriteria sc = sb.create(); - sc.setParameters("hostId", String.valueOf(hostId)); - sc.setParameters("lastHostId", String.valueOf(hostId)); + sc.setParameters("hostId", hostIds.toArray()); + sc.setParameters("lastHostId", hostIds.toArray()); sc.setParameters("podId", String.valueOf(podId)); return listBy(sc); } diff --git a/framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDao.java b/framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDao.java index be81b5fe04d4..96d57ee04258 100644 --- a/framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDao.java +++ b/framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDao.java @@ -55,4 +55,6 @@ public interface ManagementServerHostDao extends GenericDao listOrphanMsids(); ManagementServerHostVO findOneInUpState(Filter filter); + + ManagementServerHostVO findOneByLongestRuntime(); } diff --git a/framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java b/framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java index 74f8481a31d9..715dfe26bae6 100644 --- a/framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java +++ b/framework/cluster/src/main/java/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java @@ -25,6 +25,7 @@ import java.util.TimeZone; +import org.apache.commons.collections.CollectionUtils; import org.apache.log4j.Logger; import com.cloud.cluster.ClusterInvalidSessionException; @@ -204,6 +205,7 @@ protected ManagementServerHostDaoImpl() { StateSearch = createSearchBuilder(); StateSearch.and("state", StateSearch.entity().getState(), SearchCriteria.Op.IN); + StateSearch.and("runid", StateSearch.entity().getRunid(), SearchCriteria.Op.GT); StateSearch.done(); } @@ -272,4 +274,14 @@ public ManagementServerHostVO findOneInUpState(Filter filter) { return null; } + @Override + public ManagementServerHostVO findOneByLongestRuntime() { + SearchCriteria sc = StateSearch.create(); + sc.setParameters("state", ManagementServerHost.State.Up); + sc.setParameters("runid", 0); + Filter filter = new Filter(ManagementServerHostVO.class, "runid", true, 0L, 1L); + List msHosts = listBy(sc, filter); + return CollectionUtils.isNotEmpty(msHosts) ? msHosts.get(0) : null; + } + } diff --git a/server/src/main/java/com/cloud/api/ApiResponseHelper.java b/server/src/main/java/com/cloud/api/ApiResponseHelper.java index 7ed31e21bc38..5b723a51cc88 100644 --- a/server/src/main/java/com/cloud/api/ApiResponseHelper.java +++ b/server/src/main/java/com/cloud/api/ApiResponseHelper.java @@ -2572,6 +2572,10 @@ public NetworkResponse createNetworkResponse(ResponseView view, Network network) Domain domain = ApiDBUtils.findDomainById(domainNetworkDetails.first()); if (domain != null) { response.setDomainId(domain.getUuid()); + + StringBuilder domainPath = new StringBuilder("ROOT"); + (domainPath.append(domain.getPath())).deleteCharAt(domainPath.length() - 1); + response.setDomainPath(domainPath.toString()); } } response.setSubdomainAccess(domainNetworkDetails.second()); diff --git a/server/src/main/java/com/cloud/api/query/QueryManagerImpl.java b/server/src/main/java/com/cloud/api/query/QueryManagerImpl.java index 1808230d8855..1cac5630b5ea 100644 --- a/server/src/main/java/com/cloud/api/query/QueryManagerImpl.java +++ b/server/src/main/java/com/cloud/api/query/QueryManagerImpl.java @@ -197,6 +197,7 @@ import com.cloud.exception.InvalidParameterValueException; import com.cloud.exception.PermissionDeniedException; import com.cloud.ha.HighAvailabilityManager; +import com.cloud.host.Host; import com.cloud.hypervisor.Hypervisor; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.network.RouterHealthCheckResult; @@ -1090,7 +1091,12 @@ private Pair, Integer> searchForUserVMsInternal(ListVMsCmd cm sb.and("stateNIN", sb.entity().getState(), SearchCriteria.Op.NIN); sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ); sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ); - sb.and("clusterId", sb.entity().getClusterId(), SearchCriteria.Op.EQ); + if (clusterId != null) { + sb.and().op("clusterId", sb.entity().getClusterId(), SearchCriteria.Op.EQ); + sb.or("clusterHostId", sb.entity().getHostId(), Op.IN); + sb.or("clusterLastHostId", sb.entity().getLastHostId(), Op.IN); + sb.cp(); + } sb.and("hypervisorType", sb.entity().getHypervisorType(), SearchCriteria.Op.EQ); sb.and("hostIdEQ", sb.entity().getHostId(), SearchCriteria.Op.EQ); sb.and("templateId", sb.entity().getTemplateId(), SearchCriteria.Op.EQ); @@ -1286,6 +1292,10 @@ private Pair, Integer> searchForUserVMsInternal(ListVMsCmd cm if (clusterId != null) { sc.setParameters("clusterId", clusterId); + List hosts = _hostJoinDao.findByClusterId((Long)clusterId, Host.Type.Routing); + List hostIds = hosts.stream().map(HostJoinVO::getId).collect(Collectors.toList()); + sc.setParameters("clusterHostId", hostIds.toArray()); + sc.setParameters("clusterLastHostId", hostIds.toArray()); } if (hostId != null) { diff --git a/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java b/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java index 695521e5272a..4e838510010f 100644 --- a/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java +++ b/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java @@ -124,7 +124,7 @@ public class FirstFitPlanner extends AdapterBase implements DeploymentClusterPla protected String allocationAlgorithm = "random"; protected String globalDeploymentPlanner = "FirstFitPlanner"; - protected String[] implicitHostTags; + protected String[] implicitHostTags = new String[0]; @Override public List orderClusters(VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoid) throws InsufficientServerCapacityException { @@ -214,11 +214,11 @@ private void reorderClustersBasedOnImplicitTags(List clusterList, int requ Long uniqueTags; for (Long clusterId : clusterList) { uniqueTags = (long) 0; - List hostList = capacityDao.listHostsWithEnoughCapacity(requiredCpu, requiredRam, clusterId, Host.Type.Routing.toString()); - if (!hostList.isEmpty() && implicitHostTags.length > 0) { - uniqueTags = new Long(hostTagsDao.getDistinctImplicitHostTags(hostList, implicitHostTags).size()); - uniqueTags = uniqueTags + getHostsByCapability(hostList, Host.HOST_UEFI_ENABLE); - } + List hostList = capacityDao.listHostsWithEnoughCapacity(requiredCpu, requiredRam, clusterId, Host.Type.Routing.toString()); + if (!hostList.isEmpty() && implicitHostTags.length > 0) { + uniqueTags = new Long(hostTagsDao.getDistinctImplicitHostTags(hostList, implicitHostTags).size()); + uniqueTags = uniqueTags + getHostsByCapability(hostList, Host.HOST_UEFI_ENABLE); + } UniqueTagsInClusterMap.put(clusterId, uniqueTags); } Collections.sort(clusterList, new Comparator() { diff --git a/server/src/main/java/com/cloud/network/as/AutoScaleManagerImpl.java b/server/src/main/java/com/cloud/network/as/AutoScaleManagerImpl.java index de8a3ff3c83c..2f69ac6e9ba7 100644 --- a/server/src/main/java/com/cloud/network/as/AutoScaleManagerImpl.java +++ b/server/src/main/java/com/cloud/network/as/AutoScaleManagerImpl.java @@ -1964,7 +1964,7 @@ private void checkAutoScaleVmGroupName(String groupName) { private boolean startNewVM(long vmId) { try { CallContext.current().setEventDetails("Vm Id: " + vmId); - userVmMgr.startVirtualMachine(vmId, null, null, null); + userVmMgr.startVirtualMachine(vmId, null, new HashMap<>(), null); } catch (final ResourceUnavailableException ex) { s_logger.warn("Exception: ", ex); throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage()); diff --git a/server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java b/server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java index 903b851f9184..959a0dc3bb2c 100644 --- a/server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java +++ b/server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java @@ -31,6 +31,9 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; +import com.cloud.cluster.ManagementServerHostVO; +import com.cloud.cluster.dao.ManagementServerHostDao; +import com.cloud.utils.db.GlobalLock; import org.apache.cloudstack.acl.SecurityChecker.AccessType; import org.apache.cloudstack.context.CallContext; import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine; @@ -44,6 +47,8 @@ import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao; import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO; import org.apache.cloudstack.user.ResourceReservation; +import org.apache.cloudstack.utils.identity.ManagementServerNode; +import org.apache.commons.collections.CollectionUtils; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @@ -162,6 +167,8 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim private VpcDao _vpcDao; @Inject private VlanDao _vlanDao; + @Inject + private ManagementServerHostDao managementServerHostDao; protected GenericSearchBuilder templateSizeSearch; protected GenericSearchBuilder snapshotSizeSearch; @@ -1171,6 +1178,26 @@ public ResourceCountCheckTask() { @Override protected void runInContext() { + GlobalLock lock = GlobalLock.getInternLock("ResourceCheckTask"); + try { + if (lock.lock(30)) { + try { + ManagementServerHostVO msHost = managementServerHostDao.findOneByLongestRuntime(); + if (msHost == null || (msHost.getMsid() != ManagementServerNode.getManagementServerId())) { + s_logger.trace("Skipping the resource counters recalculation task on this management server"); + return; + } + runResourceCheckTaskInternal(); + } finally { + lock.unlock(); + } + } + } finally { + lock.releaseRef(); + } + } + + private void runResourceCheckTaskInternal() { s_logger.info("Started resource counters recalculation periodic task."); List domains; List accounts; @@ -1192,9 +1219,12 @@ protected void runInContext() { } for (ResourceType type : ResourceType.values()) { - recalculateDomainResourceCountInContext(Domain.ROOT_DOMAIN, type); - for (Domain domain : domains) { - recalculateDomainResourceCount(domain.getId(), type); + if (CollectionUtils.isEmpty(domains)) { + recalculateDomainResourceCountInContext(Domain.ROOT_DOMAIN, type); + } else { + for (Domain domain : domains) { + recalculateDomainResourceCount(domain.getId(), type); + } } // run through the accounts in the root domain @@ -1202,6 +1232,7 @@ protected void runInContext() { recalculateAccountResourceCountInContext(account.getId(), type); } } + s_logger.info("Finished resource counters recalculation periodic task."); } private void recalculateDomainResourceCountInContext(long domainId, ResourceType type) { diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java index 23be3facd255..e4dc03a72fdd 100644 --- a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java @@ -135,6 +135,7 @@ import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.log4j.Logger; +import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.w3c.dom.Document; @@ -1174,9 +1175,8 @@ private UserVm rebootVirtualMachine(long userId, long vmId, boolean enterSetup, private UserVm forceRebootVirtualMachine(long vmId, long hostId, boolean enterSetup) { try { if (stopVirtualMachine(vmId, false) != null) { - Map params = null; + Map params = new HashMap<>(); if (enterSetup) { - params = new HashMap(); params.put(VirtualMachineProfile.Param.BootIntoSetup, Boolean.TRUE); } return startVirtualMachine(vmId, null, null, hostId, params, null, false).first(); @@ -4878,6 +4878,11 @@ public UserVm startVirtualMachine(DeployVMCmd cmd) throws ResourceUnavailableExc if (cmd.getBootIntoSetup() != null) { additionalParams.put(VirtualMachineProfile.Param.BootIntoSetup, cmd.getBootIntoSetup()); } + + if (StringUtils.isNotBlank(cmd.getPassword())) { + additionalParams.put(VirtualMachineProfile.Param.VmPassword, cmd.getPassword()); + } + return startVirtualMachine(vmId, podId, clusterId, hostId, diskOfferingMap, additionalParams, cmd.getDeploymentPlanner()); } @@ -5268,21 +5273,21 @@ public void finalizeStop(VirtualMachineProfile profile, Answer answer) { } @Override - public Pair> startVirtualMachine(long vmId, Long hostId, Map additionalParams, String deploymentPlannerToUse) - throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException, ResourceAllocationException { + public Pair> startVirtualMachine(long vmId, Long hostId, @NotNull Map additionalParams, + String deploymentPlannerToUse) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException, ResourceAllocationException { return startVirtualMachine(vmId, null, null, hostId, additionalParams, deploymentPlannerToUse); } @Override public Pair> startVirtualMachine(long vmId, Long podId, Long clusterId, Long hostId, - Map additionalParams, String deploymentPlannerToUse) + @NotNull Map additionalParams, String deploymentPlannerToUse) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException, ResourceAllocationException { return startVirtualMachine(vmId, podId, clusterId, hostId, additionalParams, deploymentPlannerToUse, true); } @Override public Pair> startVirtualMachine(long vmId, Long podId, Long clusterId, Long hostId, - Map additionalParams, String deploymentPlannerToUse, boolean isExplicitHost) + @NotNull Map additionalParams, String deploymentPlannerToUse, boolean isExplicitHost) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException, ResourceAllocationException { // Input validation final Account callerAccount = CallContext.current().getCallingAccount(); @@ -5381,15 +5386,7 @@ public Pair> startVirtualMach // Check that the password was passed in and is valid template = _templateDao.findByIdIncludingRemoved(vm.getTemplateId()); - String password = "saved_password"; - if (template.isEnablePassword()) { - if (vm.getDetail("password") != null) { - password = DBEncryptionUtil.decrypt(vm.getDetail("password")); - } else { - password = _mgr.generateRandomPassword(); - vm.setPassword(password); - } - } + String password = getCurrentVmPasswordOrDefineNewPassword(String.valueOf(additionalParams.getOrDefault(VirtualMachineProfile.Param.VmPassword, "")), vm, template); if (!validPassword(password)) { throw new InvalidParameterValueException("A valid password for this virtual machine was not provided."); @@ -5402,7 +5399,7 @@ public Pair> startVirtualMach params = createParameterInParameterMap(params, additionalParams, VirtualMachineProfile.Param.VmPassword, password); } - if(null != additionalParams && additionalParams.containsKey(VirtualMachineProfile.Param.BootIntoSetup)) { + if(additionalParams.containsKey(VirtualMachineProfile.Param.BootIntoSetup)) { if (! HypervisorType.VMware.equals(vm.getHypervisorType())) { throw new InvalidParameterValueException(ApiConstants.BOOT_INTO_SETUP + " makes no sense for " + vm.getHypervisorType()); } @@ -5445,6 +5442,39 @@ public Pair> startVirtualMach return vmParamPair; } + /** + * If the template is password enabled and the VM already has a password, returns it. + * If the template is password enabled and the VM does not have a password, sets the password to the password defined by the user and returns it. If no password is informed, + * sets it to a random password and returns it. + * If the template is not password enabled, returns saved_password. + * @param newPassword The new password informed by the user in order to set the password of the VM. + * @param vm The VM to retrieve the password from. + * @param template The template to be checked if the password is enabled or not. + * @return The password of the VM or saved_password. + */ + protected String getCurrentVmPasswordOrDefineNewPassword(String newPassword, UserVmVO vm, VMTemplateVO template) { + String password = "saved_password"; + + if (template.isEnablePassword()) { + if (vm.getDetail("password") != null) { + s_logger.debug(String.format("Decrypting VM [%s] current password.", vm)); + password = DBEncryptionUtil.decrypt(vm.getDetail("password")); + } else if (StringUtils.isNotBlank(newPassword)) { + s_logger.debug(String.format("A password for VM [%s] was informed. Setting VM password to value defined by user.", vm)); + password = newPassword; + vm.setPassword(password); + } else { + s_logger.debug(String.format("Setting VM [%s] password to a randomly generated password.", vm)); + password = _mgr.generateRandomPassword(); + vm.setPassword(password); + } + } else if (StringUtils.isNotBlank(newPassword)) { + s_logger.debug(String.format("A password was informed; however, the template [%s] is not password enabled. Ignoring the parameter.", template)); + } + + return password; + } + private Map createParameterInParameterMap(Map params, Map parameterMap, VirtualMachineProfile.Param parameter, Object parameterValue) { if (s_logger.isTraceEnabled()) { diff --git a/server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java b/server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java index 12665a7db7b4..752ad5a9fba9 100644 --- a/server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java +++ b/server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java @@ -357,8 +357,11 @@ private List getAdditionalNameFilters(Cluster cluster) { return additionalNameFilter; } - private List getHostManagedVms(Host host) { - List instances = vmDao.listByHostOrLastHostOrHostPod(host.getId(), host.getPodId()); + private List getHostsManagedVms(List hosts) { + if (CollectionUtils.isEmpty(hosts)) { + return new ArrayList<>(); + } + List instances = vmDao.listByHostOrLastHostOrHostPod(hosts.stream().map(HostVO::getId).collect(Collectors.toList()), hosts.get(0).getPodId()); List managedVms = instances.stream().map(VMInstanceVO::getInstanceName).collect(Collectors.toList()); return managedVms; } @@ -1026,6 +1029,24 @@ private UserVm importVirtualMachineInternal(final UnmanagedInstanceTO unmanagedI return userVm; } + private HashMap getUnmanagedInstancesForHost(HostVO host, String instanceName, List managedVms) { + HashMap unmanagedInstances = new HashMap<>(); + if (host.isInMaintenanceStates()) { + return unmanagedInstances; + } + + GetUnmanagedInstancesCommand command = new GetUnmanagedInstancesCommand(); + command.setInstanceName(instanceName); + command.setManagedInstancesNames(managedVms); + Answer answer = agentManager.easySend(host.getId(), command); + if (!(answer instanceof GetUnmanagedInstancesAnswer)) { + return unmanagedInstances; + } + GetUnmanagedInstancesAnswer unmanagedInstancesAnswer = (GetUnmanagedInstancesAnswer) answer; + unmanagedInstances = unmanagedInstancesAnswer.getUnmanagedInstances(); + return unmanagedInstances; + } + private Cluster basicAccessChecks(Long clusterId) { final Account caller = CallContext.current().getCallingAccount(); if (caller.getType() != Account.Type.ADMIN) { @@ -1055,24 +1076,11 @@ public ListResponse listUnmanagedInstances(ListUnmana } List hosts = resourceManager.listHostsInClusterByStatus(clusterId, Status.Up); List additionalNameFilters = getAdditionalNameFilters(cluster); + List managedVms = new ArrayList<>(additionalNameFilters); + managedVms.addAll(getHostsManagedVms(hosts)); List responses = new ArrayList<>(); for (HostVO host : hosts) { - if (host.isInMaintenanceStates()) { - continue; - } - List managedVms = new ArrayList<>(); - managedVms.addAll(additionalNameFilters); - managedVms.addAll(getHostManagedVms(host)); - - GetUnmanagedInstancesCommand command = new GetUnmanagedInstancesCommand(); - command.setInstanceName(cmd.getName()); - command.setManagedInstancesNames(managedVms); - Answer answer = agentManager.easySend(host.getId(), command); - if (!(answer instanceof GetUnmanagedInstancesAnswer)) { - continue; - } - GetUnmanagedInstancesAnswer unmanagedInstancesAnswer = (GetUnmanagedInstancesAnswer) answer; - HashMap unmanagedInstances = new HashMap<>(unmanagedInstancesAnswer.getUnmanagedInstances()); + HashMap unmanagedInstances = getUnmanagedInstancesForHost(host, cmd.getName(), managedVms); Set keys = unmanagedInstances.keySet(); for (String key : keys) { UnmanagedInstanceTO instance = unmanagedInstances.get(key); @@ -1099,7 +1107,7 @@ public UserVmResponse importUnmanagedInstance(ImportUnmanagedInstanceCmd cmd) { throw new InvalidParameterValueException("Instance name cannot be empty"); } if (cmd.getDomainId() != null && StringUtils.isEmpty(cmd.getAccountName())) { - throw new InvalidParameterValueException("domainid parameter must be specified with account parameter"); + throw new InvalidParameterValueException(String.format("%s parameter must be specified with %s parameter", ApiConstants.DOMAIN_ID, ApiConstants.ACCOUNT)); } final Account owner = accountService.getActiveAccountById(cmd.getEntityOwnerId()); long userId = CallContext.current().getCallingUserId(); @@ -1107,7 +1115,7 @@ public UserVmResponse importUnmanagedInstance(ImportUnmanagedInstanceCmd cmd) { if (CollectionUtils.isNotEmpty(userVOs)) { userId = userVOs.get(0).getId(); } - VMTemplateVO template = null; + VMTemplateVO template; final Long templateId = cmd.getTemplateId(); if (templateId == null) { template = templateDao.findByName(VM_IMPORT_DEFAULT_TEMPLATE_NAME); @@ -1170,59 +1178,49 @@ public UserVmResponse importUnmanagedInstance(ImportUnmanagedInstanceCmd cmd) { List hosts = resourceManager.listHostsInClusterByStatus(clusterId, Status.Up); UserVm userVm = null; List additionalNameFilters = getAdditionalNameFilters(cluster); + List managedVms = new ArrayList<>(additionalNameFilters); + managedVms.addAll(getHostsManagedVms(hosts)); for (HostVO host : hosts) { - if (host.isInMaintenanceStates()) { - continue; - } - List managedVms = new ArrayList<>(); - managedVms.addAll(additionalNameFilters); - managedVms.addAll(getHostManagedVms(host)); - GetUnmanagedInstancesCommand command = new GetUnmanagedInstancesCommand(instanceName); - command.setManagedInstancesNames(managedVms); - Answer answer = agentManager.easySend(host.getId(), command); - if (!(answer instanceof GetUnmanagedInstancesAnswer)) { - continue; - } - GetUnmanagedInstancesAnswer unmanagedInstancesAnswer = (GetUnmanagedInstancesAnswer) answer; - HashMap unmanagedInstances = unmanagedInstancesAnswer.getUnmanagedInstances(); + HashMap unmanagedInstances = getUnmanagedInstancesForHost(host, cmd.getName(), managedVms); if (MapUtils.isEmpty(unmanagedInstances)) { continue; } Set names = unmanagedInstances.keySet(); for (String name : names) { - if (instanceName.equals(name)) { - UnmanagedInstanceTO unmanagedInstance = unmanagedInstances.get(name); - if (unmanagedInstance == null) { - throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Unable to retrieve details for unmanaged VM: %s", name)); + if (!instanceName.equals(name)) { + continue; + } + UnmanagedInstanceTO unmanagedInstance = unmanagedInstances.get(name); + if (unmanagedInstance == null) { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Unable to retrieve details for unmanaged VM: %s", name)); + } + if (template.getName().equals(VM_IMPORT_DEFAULT_TEMPLATE_NAME)) { + String osName = unmanagedInstance.getOperatingSystem(); + GuestOS guestOS = null; + if (StringUtils.isNotEmpty(osName)) { + guestOS = guestOSDao.findOneByDisplayName(osName); } - if (template.getName().equals(VM_IMPORT_DEFAULT_TEMPLATE_NAME)) { - String osName = unmanagedInstance.getOperatingSystem(); - GuestOS guestOS = null; - if (StringUtils.isNotEmpty(osName)) { - guestOS = guestOSDao.findOneByDisplayName(osName); - } - GuestOSHypervisor guestOSHypervisor = null; + GuestOSHypervisor guestOSHypervisor = null; + if (guestOS != null) { + guestOSHypervisor = guestOSHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), host.getHypervisorType().toString(), host.getHypervisorVersion()); + } + if (guestOSHypervisor == null && StringUtils.isNotEmpty(unmanagedInstance.getOperatingSystemId())) { + guestOSHypervisor = guestOSHypervisorDao.findByOsNameAndHypervisor(unmanagedInstance.getOperatingSystemId(), host.getHypervisorType().toString(), host.getHypervisorVersion()); + } + if (guestOSHypervisor == null) { if (guestOS != null) { - guestOSHypervisor = guestOSHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), host.getHypervisorType().toString(), host.getHypervisorVersion()); - } - if (guestOSHypervisor == null && StringUtils.isNotEmpty(unmanagedInstance.getOperatingSystemId())) { - guestOSHypervisor = guestOSHypervisorDao.findByOsNameAndHypervisor(unmanagedInstance.getOperatingSystemId(), host.getHypervisorType().toString(), host.getHypervisorVersion()); - } - if (guestOSHypervisor == null) { - if (guestOS != null) { - throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Unable to find hypervisor guest OS ID: %s details for unmanaged VM: %s for hypervisor: %s version: %s. templateid parameter can be used to assign template for VM", guestOS.getUuid(), name, host.getHypervisorType().toString(), host.getHypervisorVersion())); - } - throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Unable to retrieve guest OS details for unmanaged VM: %s with OS name: %s, OS ID: %s for hypervisor: %s version: %s. templateid parameter can be used to assign template for VM", name, osName, unmanagedInstance.getOperatingSystemId(), host.getHypervisorType().toString(), host.getHypervisorVersion())); + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Unable to find hypervisor guest OS ID: %s details for unmanaged VM: %s for hypervisor: %s version: %s. templateid parameter can be used to assign template for VM", guestOS.getUuid(), name, host.getHypervisorType().toString(), host.getHypervisorVersion())); } - template.setGuestOSId(guestOSHypervisor.getGuestOsId()); + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Unable to retrieve guest OS details for unmanaged VM: %s with OS name: %s, OS ID: %s for hypervisor: %s version: %s. templateid parameter can be used to assign template for VM", name, osName, unmanagedInstance.getOperatingSystemId(), host.getHypervisorType().toString(), host.getHypervisorVersion())); } - userVm = importVirtualMachineInternal(unmanagedInstance, instanceName, zone, cluster, host, - template, displayName, hostName, CallContext.current().getCallingAccount(), owner, userId, - serviceOffering, dataDiskOfferingMap, - nicNetworkMap, nicIpAddressMap, - details, cmd.getMigrateAllowed(), forced); - break; + template.setGuestOSId(guestOSHypervisor.getGuestOsId()); } + userVm = importVirtualMachineInternal(unmanagedInstance, instanceName, zone, cluster, host, + template, displayName, hostName, CallContext.current().getCallingAccount(), owner, userId, + serviceOffering, dataDiskOfferingMap, + nicNetworkMap, nicIpAddressMap, + details, cmd.getMigrateAllowed(), forced); + break; } if (userVm != null) { break; diff --git a/server/src/test/java/com/cloud/network/as/AutoScaleManagerImplTest.java b/server/src/test/java/com/cloud/network/as/AutoScaleManagerImplTest.java index 0c65f1a46944..aaf0f254d418 100644 --- a/server/src/test/java/com/cloud/network/as/AutoScaleManagerImplTest.java +++ b/server/src/test/java/com/cloud/network/as/AutoScaleManagerImplTest.java @@ -99,7 +99,6 @@ import com.cloud.vm.UserVmVO; import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachineManager; -import com.cloud.vm.VirtualMachineProfile; import com.cloud.vm.VmStats; import com.cloud.vm.dao.DomainRouterDao; import com.cloud.vm.dao.UserVmDao; @@ -1495,8 +1494,6 @@ public void testDoScaleUp() throws ResourceUnavailableException, InsufficientCap when(autoScaleVmGroupDao.updateState(vmGroupId, AutoScaleVmGroup.State.ENABLED, AutoScaleVmGroup.State.SCALING)).thenReturn(true); when(autoScaleVmGroupDao.updateState(vmGroupId, AutoScaleVmGroup.State.SCALING, AutoScaleVmGroup.State.ENABLED)).thenReturn(true); Mockito.doReturn(virtualMachineId).when(autoScaleManagerImplSpy).createNewVM(asVmGroupMock); - Pair> startVm = Mockito.mock(Pair.class); - when(userVmMgr.startVirtualMachine(virtualMachineId, null, null, null)).thenReturn(startVm); when(asVmGroupMock.getLoadBalancerId()).thenReturn(loadBalancerId); when(lbVmMapDao.listByLoadBalancerId(loadBalancerId)).thenReturn(Arrays.asList(loadBalancerVMMapMock)); diff --git a/server/src/test/java/com/cloud/vm/UserVmManagerImplTest.java b/server/src/test/java/com/cloud/vm/UserVmManagerImplTest.java index a58ce358ddaa..7886e70920cd 100644 --- a/server/src/test/java/com/cloud/vm/UserVmManagerImplTest.java +++ b/server/src/test/java/com/cloud/vm/UserVmManagerImplTest.java @@ -38,6 +38,7 @@ import com.cloud.network.dao.NetworkDao; import com.cloud.network.dao.NetworkVO; import com.cloud.offering.ServiceOffering; +import com.cloud.server.ManagementService; import com.cloud.service.ServiceOfferingVO; import com.cloud.service.dao.ServiceOfferingDao; import com.cloud.storage.DiskOfferingVO; @@ -213,6 +214,12 @@ public class UserVmManagerImplTest { @Mock AccountVO account; + @Mock + VMTemplateVO vmTemplateVoMock; + + @Mock + ManagementService managementServiceMock; + @Mock private ServiceOfferingVO serviceOffering; @@ -1068,4 +1075,58 @@ public void testUpdateVncPasswordIfItHasChangedNewPassword() { Mockito.verify(userVmDao).findById(vmId); Mockito.verify(userVmDao).update(vmId, userVmVoMock); } + + @Test + public void getCurrentVmPasswordOrDefineNewPasswordTestTemplateIsNotPasswordEnabledReturnPreDefinedString() { + String expected = "saved_password"; + + Mockito.doReturn(false).when(vmTemplateVoMock).isEnablePassword(); + + String result = userVmManagerImpl.getCurrentVmPasswordOrDefineNewPassword("", userVmVoMock, vmTemplateVoMock); + + Assert.assertEquals(expected, result); + } + + @Test + public void getCurrentVmPasswordOrDefineNewPasswordTestVmHasPasswordReturnCurrentPassword() { + String expected = "current_password"; + + Mockito.doReturn(true).when(vmTemplateVoMock).isEnablePassword(); + Mockito.doReturn(expected).when(userVmVoMock).getDetail("password"); + + String result = userVmManagerImpl.getCurrentVmPasswordOrDefineNewPassword("", userVmVoMock, vmTemplateVoMock); + + Assert.assertEquals(expected, result); + } + + @Test + public void getCurrentVmPasswordOrDefineNewPasswordTestUserDefinedPasswordReturnNewPasswordAndSetVmPassword() { + String expected = "new_password"; + + Mockito.doReturn(true).when(vmTemplateVoMock).isEnablePassword(); + Mockito.doReturn(null).when(userVmVoMock).getDetail("password"); + Mockito.doCallRealMethod().when(userVmVoMock).setPassword(Mockito.any()); + Mockito.doCallRealMethod().when(userVmVoMock).getPassword(); + + String result = userVmManagerImpl.getCurrentVmPasswordOrDefineNewPassword(expected, userVmVoMock, vmTemplateVoMock); + + Assert.assertEquals(expected, result); + Assert.assertEquals(expected, userVmVoMock.getPassword()); + } + + @Test + public void getCurrentVmPasswordOrDefineNewPasswordTestUserDefinedPasswordReturnRandomPasswordAndSetVmPassword() { + String expected = "random_password"; + + Mockito.doReturn(true).when(vmTemplateVoMock).isEnablePassword(); + Mockito.doReturn(null).when(userVmVoMock).getDetail("password"); + Mockito.doReturn(expected).when(managementServiceMock).generateRandomPassword(); + Mockito.doCallRealMethod().when(userVmVoMock).setPassword(Mockito.any()); + Mockito.doCallRealMethod().when(userVmVoMock).getPassword(); + + String result = userVmManagerImpl.getCurrentVmPasswordOrDefineNewPassword("", userVmVoMock, vmTemplateVoMock); + + Assert.assertEquals(expected, result); + Assert.assertEquals(expected, userVmVoMock.getPassword()); + } } diff --git a/ui/src/config/section/compute.js b/ui/src/config/section/compute.js index e09fab347920..8b30cce877de 100644 --- a/ui/src/config/section/compute.js +++ b/ui/src/config/section/compute.js @@ -368,6 +368,7 @@ export default { label: 'label.action.reset.password', message: 'message.action.instance.reset.password', dataView: true, + args: ['password'], show: (record) => { return ['Stopped'].includes(record.state) && record.passwordenabled }, response: (result) => { return { diff --git a/ui/src/config/section/network.js b/ui/src/config/section/network.js index 153e6c5df5f0..070b266a3ba0 100644 --- a/ui/src/config/section/network.js +++ b/ui/src/config/section/network.js @@ -34,20 +34,20 @@ export default { permission: ['listNetworks'], resourceType: 'Network', columns: () => { - var fields = ['name', 'state', 'type', 'vpcname', 'cidr', 'ip6cidr', 'broadcasturi', 'account', 'domain', 'zonename'] + var fields = ['name', 'state', 'type', 'vpcname', 'cidr', 'ip6cidr', 'broadcasturi', 'domainpath', 'account', 'zonename'] if (!isAdmin()) { fields = fields.filter(function (e) { return e !== 'broadcasturi' }) } return fields }, details: () => { - var fields = ['name', 'id', 'description', 'type', 'traffictype', 'vpcid', 'vlan', 'broadcasturi', 'cidr', 'ip6cidr', 'netmask', 'gateway', 'aclname', 'ispersistent', 'restartrequired', 'reservediprange', 'redundantrouter', 'networkdomain', 'egressdefaultpolicy', 'zonename', 'account', 'domain', 'associatednetwork', 'associatednetworkid', 'ip6firewall', 'ip6routing', 'ip6routes', 'dns1', 'dns2', 'ip6dns1', 'ip6dns2', 'publicmtu', 'privatemtu'] + var fields = ['name', 'id', 'description', 'type', 'traffictype', 'vpcid', 'vlan', 'broadcasturi', 'cidr', 'ip6cidr', 'netmask', 'gateway', 'aclname', 'ispersistent', 'restartrequired', 'reservediprange', 'redundantrouter', 'networkdomain', 'egressdefaultpolicy', 'zonename', 'account', 'domainpath', 'associatednetwork', 'associatednetworkid', 'ip6firewall', 'ip6routing', 'ip6routes', 'dns1', 'dns2', 'ip6dns1', 'ip6dns2', 'publicmtu', 'privatemtu'] if (!isAdmin()) { fields = fields.filter(function (e) { return e !== 'broadcasturi' }) } return fields }, - filters: ['all', 'account', 'domain', 'shared'], + filters: ['all', 'account', 'domainpath', 'shared'], searchFilters: ['keyword', 'zoneid', 'domainid', 'account', 'type', 'tags'], related: [{ name: 'vm',