diff --git a/src/lxc/lxc.py b/src/lxc/lxc.py index 3b32720..142959b 100644 --- a/src/lxc/lxc.py +++ b/src/lxc/lxc.py @@ -176,18 +176,16 @@ class LXC(LinuxMachine): Get IPv4 :return: ipv4 """ + if self.is_running(): + if netmask: + return self.get_in_cache('network', 'ipv4_netmask', + default_value=self.retrieve_ipv4(netmask=netmask, use_ssh=use_ssh)) - if netmask: - ipv4 = self.get_in_cache('network', 'ipv4_netmask') - if ipv4 is None: - self.set_in_cache('network', 'ipv4_netmask', self.retrieve_ipv4(netmask=netmask, use_ssh=use_ssh)) - return self.get_in_cache('network', 'ipv4_netmask') + else: + return self.get_in_cache('network', 'ipv4', + default_value=self.retrieve_ipv4(netmask=netmask, use_ssh=use_ssh)) - else: - ipv4 = self.get_in_cache('network', 'ipv4') - if ipv4 is None: - self.set_in_cache('network', 'ipv4', self.retrieve_ipv4(netmask=netmask, use_ssh=use_ssh)) - return self.get_in_cache('network', 'ipv4') + return self.ipv4 def retrieve_ipv4(self, netmask: bool = False, use_ssh: bool = False): if self.ipv4 == "dhcp": diff --git a/src/utils/machine.py b/src/utils/machine.py index f2bab45..4ed646c 100644 --- a/src/utils/machine.py +++ b/src/utils/machine.py @@ -327,7 +327,10 @@ class LinuxMachine(): self.run_command(f"sed {'-i' if case_sensitive else ''} 's/{search}/{replace}/g' {path}", return_status_code=True) - def get_in_cache(self, category: str, property: str): + def get_in_cache(self, category: str, property: str, default_value: any = None): + if default_value is not None and property not in self.cache[category]: + self.set_in_cache(category, property, default_value) + if category in self.cache: if property in self.cache[category]: return self.cache[category][property]