Skip to content

Commit

Permalink
Small fix to refresh ip
Browse files Browse the repository at this point in the history
  • Loading branch information
Costya-Y committed Jun 4, 2024
1 parent 0311674 commit 08e6a2a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
17 changes: 11 additions & 6 deletions cloudshell/cp/proxmox/actions/vm_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ipaddress
import logging
import time
from contextlib import suppress
from typing import TYPE_CHECKING, Union

from cloudshell.cp.core.request_actions.models import (
Expand All @@ -12,7 +13,8 @@
)

from cloudshell.cp.proxmox.actions.vm_network import VMNetworkActions
from cloudshell.cp.proxmox.exceptions import InstanceIsNotRunningException
from cloudshell.cp.proxmox.exceptions import InstanceIsNotRunningException, \
VMIPNotFoundException
from cloudshell.cp.proxmox.models.deploy_app import (
BaseProxmoxDeployApp,
)
Expand Down Expand Up @@ -85,11 +87,14 @@ def _prepare_vm_network_data(
self._cancellation_manager)

vnics_data = self._get_instance_interfaces_with_retries(instance_id=instance_id)
primary_ip = network_actions.get_vm_ip(instance_id,
vnics_data,
ip_regex=app_model.ip_regex,
timeout=3,
)
primary_ip = None
with suppress(VMIPNotFoundException):
primary_ip = network_actions.get_vm_ip(
self._ph,
instance_id,
ip_regex=app_model.ip_regex,
timeout=3,
)

for mac, iface in vnics_data.items():
vlan_id = iface.get("tag", "")
Expand Down
11 changes: 6 additions & 5 deletions cloudshell/cp/proxmox/actions/vm_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ def is_quali_network(self, network_name: str) -> bool:

def _find_vm_ip(
self,
instance_network_details: dict[str, dict],
api: ProxmoxHandler,
vm: int,
is_ip_pass_regex: callable[[str | None], bool],
) -> str | None:
logger.debug(f"Searching for the Instance IPv4 address of the")
logger.debug(f"Searching for the IPv4 address of the {vm} instance")

for vnic in instance_network_details.values():
for vnic in api.get_instance_ifaces_info(vm).values():
ip = vnic.get("ipv4")
name = vnic.get("name")
logger.debug(f"Checking {name} with ip {ip}")
Expand All @@ -52,8 +53,8 @@ def _find_vm_ip(

def get_vm_ip(
self,
api: ProxmoxHandler,
vm_id: int,
instance_network_details: dict[str, dict],
ip_regex: str | None = None,
timeout: int = 0,
) -> str:
Expand All @@ -63,7 +64,7 @@ def get_vm_ip(

while True:
with self._cancellation_manager:
ip = self._find_vm_ip(instance_network_details, is_ip_pass_regex)
ip = self._find_vm_ip(api, vm_id, is_ip_pass_regex)
if ip:
break
if datetime.now() > timeout_time:
Expand Down
35 changes: 21 additions & 14 deletions cloudshell/cp/proxmox/flows/refresh_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,30 @@


def refresh_ip(
si: ProxmoxHandler,
deployed_app: BaseProxmoxDeployedApp,
resource_conf: ProxmoxResourceConfig,
cancellation_manager: CancellationContextManager,
si: ProxmoxHandler,
deployed_app: BaseProxmoxDeployedApp,
resource_conf: ProxmoxResourceConfig,
cancellation_manager: CancellationContextManager,
) -> str:
instance_id = int(deployed_app.vmdetails.uid)
if si.get_instance_status(instance_id) != PowerState.RUNNING:
raise VmIsNotPowered(instance_id)
ip = ""
timeout = deployed_app.refresh_ip_timeout
if not deployed_app.wait_for_ip:
timeout = 1
try:
instance_id = int(deployed_app.vmdetails.uid)
if si.get_instance_status(instance_id) != PowerState.RUNNING:
raise VmIsNotPowered(instance_id)

actions = VMNetworkActions(resource_conf, cancellation_manager)
instance_network_details = si.get_instance_ifaces_info(instance_id)
ip = actions.get_vm_ip(
actions = VMNetworkActions(resource_conf, cancellation_manager)
ip = actions.get_vm_ip(
si,
instance_id,
instance_network_details,
ip_regex=deployed_app.ip_regex,
timeout=deployed_app.refresh_ip_timeout,
timeout=timeout,
)
if ip != deployed_app.private_ip:
deployed_app.update_private_ip(deployed_app.name, ip)
if ip != deployed_app.private_ip:
deployed_app.update_private_ip(deployed_app.name, ip)
except Exception:
if deployed_app.wait_for_ip:
raise
return ip
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.38
1.4.48

0 comments on commit 08e6a2a

Please sign in to comment.