From bf85cb1fed896be469830044627902456addd989 Mon Sep 17 00:00:00 2001 From: Natalia Serrano Date: Tue, 15 Apr 2025 17:03:31 +0200 Subject: [PATCH] refs #1916 move ogGetIpAddress to NetLib --- CHANGELOG.md | 6 ++++++ ogclient/lib/python3/NetLib.py | 24 +++++++++++++++++++++++- ogclient/lib/python3/ogGlobals.py | 27 --------------------------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad1cf43..18e8db1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.6.0] - 2025-04-15 + +### Changed + +- ogGetIpAddress() is no longer required in ogGlobals--move it back into NetLib + ## [0.5.0] - 2025-04-15 ### Changed diff --git a/ogclient/lib/python3/NetLib.py b/ogclient/lib/python3/NetLib.py index a6cacc4..438d72d 100644 --- a/ogclient/lib/python3/NetLib.py +++ b/ogclient/lib/python3/NetLib.py @@ -142,7 +142,29 @@ def ogGetHostname(): #@note Usa las variables utilizadas por el initrd "/etc/net-ethX.conf #*/ ## def ogGetIpAddress(): - return ogGlobals.ogGetIpAddress() + if "IPV4ADDR" in os.environ: + ip = os.environ["IPV4ADDR"] + if '/' in ip: ip = ip.split ('/')[0] + return ip + + extra_args = [] + if "DEVICE" in os.environ: + extra_args = [ "dev", os.environ["DEVICE"] ] + ipas = subprocess.run (['ip', '-json', 'address', 'show', 'up'] + extra_args, capture_output=True, text=True).stdout + + ipasj = json.loads (ipas) + addresses = [] + for e in ipasj: + if 'lo' == e['ifname']: continue + if 'addr_info' not in e: continue + addrs = e['addr_info'] + for a in addrs: + if 'inet' != a['family']: continue + addresses.append ({ 'local': a['local'], 'prefixlen': a['prefixlen'] }) + + if 1 != len (addresses): + raise Exception ('more than one local IP address found') + return addresses[0] #/** diff --git a/ogclient/lib/python3/ogGlobals.py b/ogclient/lib/python3/ogGlobals.py index beb3d97..c212c9e 100644 --- a/ogclient/lib/python3/ogGlobals.py +++ b/ogclient/lib/python3/ogGlobals.py @@ -6,33 +6,6 @@ import os.path import locale import importlib.util -## required for defining OGLOGFILE -def ogGetIpAddress(): - if "IPV4ADDR" in os.environ: - ip = os.environ["IPV4ADDR"] - if '/' in ip: ip = ip.split ('/')[0] - return ip - - extra_args = [] - if "DEVICE" in os.environ: - extra_args = [ "dev", os.environ["DEVICE"] ] - ipas = subprocess.run (['ip', '-json', 'address', 'show', 'up'] + extra_args, capture_output=True, text=True).stdout - - ipasj = json.loads (ipas) - addresses = [] - for e in ipasj: - if 'lo' == e['ifname']: continue - if 'addr_info' not in e: continue - addrs = e['addr_info'] - for a in addrs: - if 'inet' != a['family']: continue - addresses.append ({ 'local': a['local'], 'prefixlen': a['prefixlen'] }) - - if 1 != len (addresses): - raise Exception ('more than one local IP address found') - return addresses[0] -ip = ogGetIpAddress() - def load_lang (name): global lang if name in sys.modules: