From f6cc003469b75e2bd9e8ff7569a670056a4de179 Mon Sep 17 00:00:00 2001 From: Jeffrey Townsend Date: Wed, 7 Dec 2016 20:14:03 +0000 Subject: [PATCH] Provide common access to machine.conf and the output of onie-syseeprom. Some platforms do not provide direct device access to the ONIE system eeprom. The only way these systems can export their system data under ONL is by scraping it from the output of onie-syseeprom and/or reading fields from machine.conf. These new platform methods can be called in a platform's baseconfig() to populate the cached version of these values for later use, or used to access that cached data at a later time. --- .../src/python/onl/platform/base.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/packages/base/all/vendor-config-onl/src/python/onl/platform/base.py b/packages/base/all/vendor-config-onl/src/python/onl/platform/base.py index 0b8ffbf2..5d43ce15 100644 --- a/packages/base/all/vendor-config-onl/src/python/onl/platform/base.py +++ b/packages/base/all/vendor-config-onl/src/python/onl/platform/base.py @@ -186,6 +186,54 @@ class OnlPlatformBase(object): def baseconfig(self): return True + def onie_machine_get(self): + mc = self.basedir_onl("etc/onie/machine.json") + if not os.path.exists(mc): + data = {} + mcconf = subprocess.check_output("""onie-shell -c "cat /etc/machine.conf" """, shell=True) + for entry in mcconf.split(): + (k,e,v) = entry.partition('=') + if e: + data[k] = v + + if not os.path.exists(os.path.dirname(mc)): + os.makedirs(os.path.dirname(mc)) + + with open(mc, "w") as f: + f.write(json.dumps(data, indent=2)) + else: + data = json.load(open(mc)) + + return data + + def onie_syseeprom_get(self): + se = self.basedir_onl("etc/onie/eeprom.json") + if not os.path.exists(se): + data = {} + extensions = [] + syseeprom = subprocess.check_output("""onie-shell -c onie-syseeprom""", shell=True) + e = re.compile(r'(.*?) (0x[0-9a-fA-F][0-9a-fA-F])[ ]+(\d+) (.*)') + for line in syseeprom.split('\n'): + m = e.match(line) + if m: + value = m.groups(0)[3] + code = m.groups(0)[1].lower() + if code == '0xfd': + extensions.append(value) + else: + data[code] = value + if len(extensions): + data['0xfd'] = extensions + + if not os.path.exists(os.path.dirname(se)): + os.makedirs(os.path.dirname(se)) + + with open(se, "w") as f: + f.write(json.dumps(data, indent=2)) + else: + data = json.load(open(se)) + return data + def platform(self): return self.PLATFORM