Validate host tags on VM live scale (#6409)

* Validate host tags on VM live scale

* Remove extra spaces
This commit is contained in:
Bryan Lima 2022-08-31 04:24:00 -03:00 committed by GitHub
parent 201f06697d
commit d4b8e2cbe8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 12 deletions

View File

@ -46,6 +46,7 @@ import com.cloud.storage.Storage.StoragePoolType;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.db.GenericDao;
import java.util.Arrays;
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
import org.apache.commons.lang3.StringUtils;
@Entity
@ -680,7 +681,7 @@ public class HostVO implements Host {
@Override
public String toString() {
return String.format("Host {\"id\": \"%s\", \"name\": \"%s\", \"uuid\": \"%s\", \"type\"=\"%s\"}", id, name, uuid, type);
return String.format("Host %s", ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "id", "name", "uuid", "type"));
}
public void setHypervisorType(HypervisorType hypervisorType) {

View File

@ -35,6 +35,7 @@ import javax.persistence.EnumType;
import com.cloud.offering.ServiceOffering;
import com.cloud.utils.db.GenericDao;
import com.cloud.vm.VirtualMachine;
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
@Entity
@Table(name = "service_offering")
@ -424,7 +425,7 @@ public class ServiceOfferingVO implements ServiceOffering {
@Override
public String toString() {
return String.format("Service offering {\"id\": %s, \"name\": \"%s\", \"uuid\": \"%s\"}", getId(), getName(), getUuid());
return String.format("Service offering %s.", ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "id", "name", "uuid"));
}
public boolean isDynamicScalingEnabled() {

View File

@ -41,6 +41,7 @@ import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.apache.cloudstack.backup.Backup;
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
@ -501,7 +502,7 @@ public class VMInstanceVO implements VirtualMachine, FiniteStateObject<State, Vi
@Override
public String toString() {
return String.format("VM instance {id: \"%s\", name: \"%s\", uuid: \"%s\", type=\"%s\"}", id, getInstanceName(), uuid, type);
return String.format("VM instance %s", ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "id", "instanceName", "uuid", "type"));
}
@Override

View File

@ -1829,6 +1829,9 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager,
}
final List<String> hostTags = cmd.getHostTags();
if (hostTags != null) {
List<VMInstanceVO> activeVMs = _vmDao.listByHostId(hostId);
s_logger.warn(String.format("The following active VMs [%s] are using the host [%s]. Updating the host tags will not affect them.", activeVMs, host));
if (s_logger.isDebugEnabled()) {
s_logger.debug("Updating Host Tags to :" + hostTags);
}

View File

@ -1847,20 +1847,31 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
public boolean upgradeVirtualMachine(Long vmId, Long newServiceOfferingId, Map<String, String> customParameters) throws ResourceUnavailableException,
ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException {
// Verify input parameters
VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId);
Account caller = CallContext.current().getCallingAccount();
_accountMgr.checkAccess(caller, null, true, vmInstance);
if (vmInstance != null) {
if (vmInstance.getState().equals(State.Stopped)) {
upgradeStoppedVirtualMachine(vmId, newServiceOfferingId, customParameters);
return true;
}
if (vmInstance.getState().equals(State.Running)) {
return upgradeRunningVirtualMachine(vmId, newServiceOfferingId, customParameters);
if (vmInstance == null) {
s_logger.error(String.format("VM instance with id [%s] is null, it is not possible to upgrade a null VM.", vmId));
return false;
}
if (State.Stopped.equals(vmInstance.getState())) {
upgradeStoppedVirtualMachine(vmId, newServiceOfferingId, customParameters);
return true;
}
if (State.Running.equals(vmInstance.getState())) {
ServiceOfferingVO newServiceOfferingVO = _serviceOfferingDao.findById(newServiceOfferingId);
HostVO instanceHost = _hostDao.findById(vmInstance.getHostId());
_hostDao.loadHostTags(instanceHost);
if (!instanceHost.checkHostServiceOfferingTags(newServiceOfferingVO)) {
s_logger.error(String.format("Cannot upgrade VM [%s] as the new service offering [%s] does not have the required host tags %s.", vmInstance, newServiceOfferingVO,
instanceHost.getHostTags()));
return false;
}
}
return false;
return upgradeRunningVirtualMachine(vmId, newServiceOfferingId, customParameters);
}
private boolean upgradeRunningVirtualMachine(Long vmId, Long newServiceOfferingId, Map<String, String> customParameters) throws ResourceUnavailableException,