From bdb3ce7ea8b4ddea69f36d668bb4160a8d0599f5 Mon Sep 17 00:00:00 2001 From: Pearl Dsilva Date: Fri, 22 Aug 2025 13:06:37 -0400 Subject: [PATCH] extract code and make few configs dynamic --- .../com/cloud/consoleproxy/AgentHookBase.java | 5 +- .../consoleproxy/ConsoleProxyManager.java | 6 +- .../consoleproxy/ConsoleProxyManagerImpl.java | 57 +++++++++++-------- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/server/src/main/java/com/cloud/consoleproxy/AgentHookBase.java b/server/src/main/java/com/cloud/consoleproxy/AgentHookBase.java index 2b41101e6c9..ff79115b904 100644 --- a/server/src/main/java/com/cloud/consoleproxy/AgentHookBase.java +++ b/server/src/main/java/com/cloud/consoleproxy/AgentHookBase.java @@ -20,6 +20,7 @@ package com.cloud.consoleproxy; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Date; +import java.util.Objects; import org.apache.cloudstack.consoleproxy.ConsoleAccessManager; import org.apache.cloudstack.framework.config.dao.ConfigurationDao; @@ -214,7 +215,9 @@ public abstract class AgentHookBase implements AgentHook { HostVO consoleProxyHost = findConsoleProxyHost(startupCmd); - assert (consoleProxyHost != null); + if (Objects.isNull(consoleProxyHost)) { + throw new IllegalStateException("Console proxy host is null"); + } Long datacenterId = consoleProxyHost.getDataCenterId(); String consoleProxyUrlDomain = ConsoleProxyManager.ConsoleProxyUrlDomain.valueIn(datacenterId); diff --git a/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManager.java b/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManager.java index feb29e9605d..c87dacfaec5 100644 --- a/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManager.java +++ b/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManager.java @@ -57,7 +57,7 @@ public interface ConsoleProxyManager extends Manager, ConsoleProxyService { "If true, The source IP to access novnc console must be same as the IP in request to management server for console URL. Needs to reconnect CPVM to management server when this changes (via restart CPVM, or management server, or cloud service in CPVM)", false); ConfigKey ConsoleProxyServiceOffering = new ConfigKey<>(String.class, "consoleproxy.service.offering", "Console Proxy", null, - "Uuid of the service offering used by console proxy; if NULL - system offering will be used", false, ConfigKey.Scope.Zone, null); + "Uuid of the service offering used by console proxy; if NULL - system offering will be used", true, ConfigKey.Scope.Zone, null); ConfigKey ConsoleProxyCapacityStandby = new ConfigKey<>(String.class, "consoleproxy.capacity.standby", "Console Proxy", String.valueOf(DEFAULT_STANDBY_CAPACITY), "The minimal number of console proxy viewer sessions that system is able to serve immediately(standby capacity)", false, ConfigKey.Scope.Zone, null); @@ -69,7 +69,7 @@ public interface ConsoleProxyManager extends Manager, ConsoleProxyService { "Console proxy command port that is used to communicate with management server", false, ConfigKey.Scope.Zone, null); ConfigKey ConsoleProxyRestart = new ConfigKey<>(Boolean.class, "consoleproxy.restart", "Console Proxy", "true", - "Console proxy restart flag, defaults to true", false, ConfigKey.Scope.Zone, null); + "Console proxy restart flag, defaults to true", true, ConfigKey.Scope.Zone, null); ConfigKey ConsoleProxyUrlDomain = new ConfigKey<>(String.class, "consoleproxy.url.domain", "Console Proxy", "", "Console proxy url domain - domainName,privateip", false, ConfigKey.Scope.Zone, null); @@ -81,7 +81,7 @@ public interface ConsoleProxyManager extends Manager, ConsoleProxyService { "Timeout(in milliseconds) that console proxy tries to maintain a viewer session before it times out the session for no activity", false, ConfigKey.Scope.Zone, null); ConfigKey ConsoleProxyDisableRpFilter = new ConfigKey<>(Boolean.class, "consoleproxy.disable.rpfilter", "Console Proxy", "true", - "disable rp_filter on console proxy VM public interface", false, ConfigKey.Scope.Zone, null); + "disable rp_filter on console proxy VM public interface", true, ConfigKey.Scope.Zone, null); ConfigKey ConsoleProxyLaunchMax = new ConfigKey<>(Integer.class, "consoleproxy.launch.max", "Console Proxy", "10", "maximum number of console proxy instances per zone can be launched", false, ConfigKey.Scope.Zone, null); diff --git a/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java b/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java index dbbdff9342d..98fa8a4dcfd 100644 --- a/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java +++ b/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java @@ -1627,33 +1627,44 @@ public class ConsoleProxyManagerImpl extends ManagerBase implements ConsoleProxy String warningMessage = String.format("Unable to find a service offering by the UUID or ID for console proxy VM with the value [%s] set in the configuration [%s]", cpvmSrvcOffIdStr, configKey); ServiceOfferingVO serviceOfferingVO = null; if (cpvmSrvcOffIdStr != null) { - serviceOfferingVO = serviceOfferingDao.findByUuid(cpvmSrvcOffIdStr); - if (serviceOfferingVO == null) { - try { - logger.debug(warningMessage); - serviceOfferingVO = serviceOfferingDao.findById(Long.parseLong(cpvmSrvcOffIdStr)); - } catch (NumberFormatException ex) { - logger.warn(String.format("Unable to find a service offering by the ID for console proxy VM with the value [%s] set in the configuration [%s]. The value is not a valid integer number. Error: [%s].", cpvmSrvcOffIdStr, configKey, ex.getMessage()), ex); - } - } - if (serviceOfferingVO == null) { - logger.warn(warningMessage); - } + serviceOfferingVO = getServiceOfferingByUuidOrId(cpvmSrvcOffIdStr, warningMessage, configKey); } if (serviceOfferingVO == null || !serviceOfferingVO.isSystemUse()) { - int ramSize = NumbersUtil.parseInt(configurationDao.getValue("console.ram.size"), DEFAULT_PROXY_VM_RAMSIZE); - int cpuFreq = NumbersUtil.parseInt(configurationDao.getValue("console.cpu.mhz"), DEFAULT_PROXY_VM_CPUMHZ); - List offerings = serviceOfferingDao.createSystemServiceOfferings("System Offering For Console Proxy", - ServiceOffering.consoleProxyDefaultOffUniqueName, 1, ramSize, cpuFreq, 0, 0, false, null, - Storage.ProvisioningType.THIN, true, null, true, VirtualMachine.Type.ConsoleProxy, true); - - if (offerings == null || offerings.size() < 2) { - String msg = "Data integrity problem : System Offering For Console Proxy has been removed?"; - logger.error(msg); - throw new ConfigurationException(msg); - } + logger.debug("Service offering for console proxy VM is not set or not a system service offering. Creating a default service offering."); + createServiceOfferingForConsoleProxy(); } return serviceOfferingVO; } + + private ServiceOfferingVO getServiceOfferingByUuidOrId(String cpvmSrvcOffIdStr, String warningMessage, String configKey) { + ServiceOfferingVO serviceOfferingVO = serviceOfferingDao.findByUuid(cpvmSrvcOffIdStr); + if (serviceOfferingVO == null) { + try { + logger.debug(warningMessage); + serviceOfferingVO = serviceOfferingDao.findById(Long.parseLong(cpvmSrvcOffIdStr)); + } catch (NumberFormatException ex) { + logger.warn(String.format("Unable to find a service offering by the ID for console proxy VM with the value [%s] set in the configuration [%s]. The value is not a valid integer number. Error: [%s].", cpvmSrvcOffIdStr, configKey, ex.getMessage()), ex); + } + } + if (serviceOfferingVO == null) { + logger.warn(warningMessage); + } + + return serviceOfferingVO; + } + + private void createServiceOfferingForConsoleProxy() throws ConfigurationException { + int ramSize = NumbersUtil.parseInt(configurationDao.getValue("console.ram.size"), DEFAULT_PROXY_VM_RAMSIZE); + int cpuFreq = NumbersUtil.parseInt(configurationDao.getValue("console.cpu.mhz"), DEFAULT_PROXY_VM_CPUMHZ); + List offerings = serviceOfferingDao.createSystemServiceOfferings("System Offering For Console Proxy", + ServiceOffering.consoleProxyDefaultOffUniqueName, 1, ramSize, cpuFreq, 0, 0, false, null, + Storage.ProvisioningType.THIN, true, null, true, VirtualMachine.Type.ConsoleProxy, true); + + if (offerings == null || offerings.size() < 2) { + String msg = "Data integrity problem : System Offering For Console Proxy has been removed?"; + logger.error(msg); + throw new ConfigurationException(msg); + } + } }