2727import java .util .List ;
2828import java .util .Map ;
2929import java .util .Objects ;
30+ import java .util .Optional ;
3031import java .util .Set ;
3132import java .util .UUID ;
3233import java .util .concurrent .TimeUnit ;
133134import org .apache .cloudstack .veeam .api .dto .Vm ;
134135import org .apache .cloudstack .veeam .api .dto .VmAction ;
135136import org .apache .cloudstack .veeam .api .dto .VnicProfile ;
137+ import org .apache .cloudstack .veeam .utils .CloudConfigUtil ;
136138import org .apache .commons .collections .CollectionUtils ;
137139import org .apache .commons .collections .MapUtils ;
138140import org .apache .commons .lang3 .ObjectUtils ;
@@ -231,6 +233,7 @@ public class ServerAdapter extends ManagerBase {
231233 private static final String VM_TAG_KEY = "veeam_tag" ;
232234 private static final String WORKER_VM_GUEST_CPU_MODE = "host-passthrough" ;
233235 private static final String WORKER_VM_GUEST_OS = "Rocky Linux 9" ;
236+ private static final String WORKER_VM_IP_DETAIL = "worker.vm.ip" ;
234237 private static final String RESTORE_CONFIG = "restore.config" ;
235238
236239 @ Inject
@@ -802,6 +805,19 @@ protected static Map<String, String> getDetailsForInstanceCreation(String userda
802805 return details ;
803806 }
804807
808+ protected void saveInstanceAdditionalDetails (Vm request , UserVm vm ) {
809+ if (request .isWorkerVm ()) {
810+ Optional <String > ip = CloudConfigUtil .extractIpv4Address (
811+ request .getInitialization ().getCustomScript ());
812+ if (ip .isPresent () && StringUtils .isNotBlank (ip .get ())) {
813+ logger .debug ("Saving worker VM IP {} in details for {}" , ip .get (), vm );
814+ vmInstanceDetailsDao .addDetail (vm .getId (), WORKER_VM_IP_DETAIL , ip .get (), false );
815+ }
816+ return ;
817+ }
818+ saveInstanceRestoreConfig (request , vm );
819+ }
820+
805821 protected void saveInstanceRestoreConfig (Vm request , UserVm vm ) {
806822 if (StringUtils .isBlank (request .getAccountId ())) {
807823 return ;
@@ -813,13 +829,18 @@ protected void saveInstanceRestoreConfig(Vm request, UserVm vm) {
813829 if (StringUtils .isBlank (restoreConfig )) {
814830 return ;
815831 }
832+ logger .debug ("Saving restore config: {} in details for {}" , restoreConfig , vm );
816833 vmInstanceDetailsDao .addDetail (vm .getId (), RESTORE_CONFIG , restoreConfig , false );
817834 }
818835
819836 protected void removeInstanceRestoreConfig (UserVm vm ) {
820837 vmInstanceDetailsDao .removeDetail (vm .getId (), RESTORE_CONFIG );
821838 }
822839
840+ protected void removeInstanceWorkerVmIp (UserVm vm ) {
841+ vmInstanceDetailsDao .removeDetail (vm .getId (), WORKER_VM_IP_DETAIL );
842+ }
843+
823844 protected void processInstanceRestoreConfigIfNeeded (UserVm userVm , Volume volume ) {
824845 VMInstanceDetailVO detail = vmInstanceDetailsDao .findDetail (userVm .getId (), RESTORE_CONFIG );
825846 if (detail == null ) {
@@ -853,11 +874,11 @@ protected void processInstanceRestoreConfigIfNeeded(UserVm userVm, Volume volume
853874 sharedFSService .updateSharedFSPostRestore (sharedFS .getId (), volume .getId ());
854875 }
855876
856- protected Pair <String , String > getValidatedInstanceNicDetails (final VMInstanceDetailVO detail , final NetworkVO network ) {
857- if (ObjectUtils .anyNull (detail , network ) || StringUtils .isBlank (detail . getValue () )) {
877+ protected Pair <String , String > getValidatedInstanceNicDetails (final String config , final NetworkVO network ) {
878+ if (ObjectUtils .anyNull (config , network ) || StringUtils .isBlank (config )) {
858879 return new Pair <>(null , null );
859880 }
860- Pair <String , String > result = OvfXmlUtil .getVmNicDetailFromStoredConfig (detail . getValue () , network .getUuid (), logger );
881+ Pair <String , String > result = OvfXmlUtil .getVmNicDetailFromStoredConfig (config , network .getUuid (), logger );
861882 String mac = StringUtils .trimToNull (result .first ());
862883 String ip4Address = StringUtils .trimToNull (result .second ());
863884 NicVO nic = null ;
@@ -888,11 +909,10 @@ protected Pair<String, String> getValidatedInstanceNicDetails(final VMInstanceDe
888909 return new Pair <>(mac , ip4Address );
889910 }
890911
891- protected void updateInstanceSecurityGroupsIfNeeded (final UserVmVO vmVo , final VMInstanceDetailVO detail , final NetworkVO network ) {
892- if (ObjectUtils .anyNull (detail , network ) || StringUtils .isBlank (detail . getValue () )) {
912+ protected void updateInstanceSecurityGroupsIfNeeded (final UserVmVO vmVo , final String config , final NetworkVO network ) {
913+ if (ObjectUtils .anyNull (config , network ) || StringUtils .isBlank (config )) {
893914 return ;
894915 }
895- String config = detail .getValue ();
896916 Vm vm = OvfXmlUtil .parseVmRestoreConfig (config , logger );
897917 if (CollectionUtils .isEmpty (vm .getSecurityGroupIds ())) {
898918 return ;
@@ -1295,13 +1315,13 @@ public Vm createInstance(Vm request) {
12951315 if (request .getTemplate () != null && StringUtils .isNotEmpty (request .getTemplate ().getId ())) {
12961316 templateUuid = request .getTemplate ().getId ();
12971317 }
1298- GuestOS guestOs = getGuestOsForInstance (request , StringUtils . isNotEmpty ( userdata ));
1318+ GuestOS guestOs = getGuestOsForInstance (request , request . isWorkerVm ( ));
12991319 String instanceType = getValidatedInstanceType (request );
13001320 Pair <Vm , UserVm > result = createInstance (zone , clusterId , owner , ownerDetails .first (), ownerDetails .second (),
13011321 ownerDetails .third (), name , displayName , serviceOfferingUuid , cpu , memoryMB , templateUuid , guestOs ,
13021322 userdata , bootOptions .first (), bootOptions .second (), request .getAffinityGroupId (),
13031323 request .getUserDataId (), request .getSshKeyPairNames (), instanceType , request .getDetails ());
1304- saveInstanceRestoreConfig (request , result .second ());
1324+ saveInstanceAdditionalDetails (request , result .second ());
13051325 return result .first ();
13061326 }
13071327
@@ -1665,8 +1685,15 @@ public Nic attachInstanceNic(final String vmUuid, final Nic request) {
16651685 accountCannotAccessNetwork (networkVO , vmVo .getAccountId ())) {
16661686 assignVmToAccount (vmVo , networkVO .getAccountId ());
16671687 }
1668- VMInstanceDetailVO detail = vmInstanceDetailsDao .findDetail (vmVo .getId (), RESTORE_CONFIG );
1669- Pair <String , String > nicDetails = getValidatedInstanceNicDetails (detail , networkVO );
1688+ Map <String , String > details = vmInstanceDetailsDao .listDetailsKeyPairs (
1689+ vmVo .getId (), List .of (RESTORE_CONFIG , WORKER_VM_IP_DETAIL ));
1690+ String restoreConfig = details .get (RESTORE_CONFIG );
1691+ Pair <String , String > nicDetails = getValidatedInstanceNicDetails (restoreConfig , networkVO );
1692+ String workerVmIp = details .get (WORKER_VM_IP_DETAIL );
1693+ if (StringUtils .isNotBlank (workerVmIp )) {
1694+ nicDetails = new Pair <>(null , workerVmIp );
1695+ removeInstanceWorkerVmIp (vmVo );
1696+ }
16701697 AddNicToVMCmd cmd = new AddNicToVMCmd ();
16711698 ComponentContext .inject (cmd );
16721699 cmd .setVmId (vmVo .getId ());
@@ -1681,7 +1708,7 @@ public Nic attachInstanceNic(final String vmUuid, final Nic request) {
16811708 if (nic == null ) {
16821709 throw new CloudRuntimeException ("Failed to attach NIC to VM" );
16831710 }
1684- updateInstanceSecurityGroupsIfNeeded (vmVo , detail , networkVO );
1711+ updateInstanceSecurityGroupsIfNeeded (vmVo , restoreConfig , networkVO );
16851712 return NicVOToNicConverter .toNic (nic , vmUuid , this ::getNetworkById );
16861713 }
16871714
0 commit comments