From c0b46ab0ba6f5ce79f640c7c1960988fc91b47b0 Mon Sep 17 00:00:00 2001 From: Sihui Han Date: Fri, 7 Apr 2017 22:46:08 +0000 Subject: [PATCH 1/3] [sonic-cfggen]:move get_machine_info to lib Signed-off-by: Sihui Han --- src/sonic-config-engine/minigraph.py | 11 +++++++++++ src/sonic-config-engine/sonic-cfggen | 16 ++-------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/sonic-config-engine/minigraph.py b/src/sonic-config-engine/minigraph.py index 709a7a2846ff..875c6a5305a0 100644 --- a/src/sonic-config-engine/minigraph.py +++ b/src/sonic-config-engine/minigraph.py @@ -47,6 +47,17 @@ def default(self, obj): return str(obj) return json.JSONEncoder.default(self, obj) +def get_machine_info(): + if not os.path.isfile('/host/machine.conf'): + return None + machine_vars = {} + with open('/host/machine.conf') as machine_file: + for line in machine_file: + tokens = line.split('=') + if len(tokens) < 2: + continue + machine_vars[tokens[0]] = tokens[1].strip() + return machine_vars def parse_png(png, hname): neighbors = {} diff --git a/src/sonic-config-engine/sonic-cfggen b/src/sonic-config-engine/sonic-cfggen index e47985cf17fc..4c0207c31403 100755 --- a/src/sonic-config-engine/sonic-cfggen +++ b/src/sonic-config-engine/sonic-cfggen @@ -7,10 +7,11 @@ import yaml import jinja2 import netaddr import json + +from minigraph import get_machine_info from minigraph import parse_xml from minigraph import minigraph_encoder - def is_ipv4(value): if not value: return False @@ -44,19 +45,6 @@ def unique_name(l): new_list.append(item) return new_list -def get_machine_info(): - if not os.path.isfile('/host/machine.conf'): - return None - machine_vars = {} - with open('/host/machine.conf') as machine_file: - for line in machine_file: - tokens = line.split('=') - if len(tokens) < 2: - continue - machine_vars[tokens[0]] = tokens[1].strip() - return machine_vars - - def main(): parser=argparse.ArgumentParser(description="Render configuration file from minigraph data and jinja2 template.") parser.add_argument("-m", "--minigraph", help="minigraph xml file") From 602cd02120369d171cc4616f159765989e53284e Mon Sep 17 00:00:00 2001 From: Sihui Han Date: Sat, 8 Apr 2017 00:41:38 +0000 Subject: [PATCH 2/3] [sonic-cfggen]:Add get platform info API Signed-off-by: Sihui Han --- src/sonic-config-engine/minigraph.py | 8 ++++++++ src/sonic-config-engine/sonic-cfggen | 11 +++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/sonic-config-engine/minigraph.py b/src/sonic-config-engine/minigraph.py index 875c6a5305a0..ac6ea4fb45ae 100644 --- a/src/sonic-config-engine/minigraph.py +++ b/src/sonic-config-engine/minigraph.py @@ -59,6 +59,14 @@ def get_machine_info(): machine_vars[tokens[0]] = tokens[1].strip() return machine_vars +def get_platform_info(machine_info): + if machine_info != None: + if machine_info.has_key('onie_platform'): + return machine_info['onie_platform'] + elif machine_info.has_key('aboot_platform'): + return machine_info['aboot_platform'] + return None + def parse_png(png, hname): neighbors = {} devices = {} diff --git a/src/sonic-config-engine/sonic-cfggen b/src/sonic-config-engine/sonic-cfggen index 4c0207c31403..6b5b67ddd824 100755 --- a/src/sonic-config-engine/sonic-cfggen +++ b/src/sonic-config-engine/sonic-cfggen @@ -7,10 +7,10 @@ import yaml import jinja2 import netaddr import json - from minigraph import get_machine_info -from minigraph import parse_xml +from minigraph import get_platform_info from minigraph import minigraph_encoder +from minigraph import parse_xml def is_ipv4(value): if not value: @@ -62,10 +62,9 @@ def main(): machine_info = get_machine_info() if machine_info != None: data.update(machine_info) - if machine_info.has_key('onie_platform'): - data['platform'] = machine_info['onie_platform'] - elif machine_info.has_key('aboot_platform'): - data['platform'] = machine_info['aboot_platform'] + platform_info = get_platform_info(machine_info) + if platform_info != None: + data['platform'] = platform_info if args.minigraph != None: minigraph = args.minigraph From 8eb0ef3d8c6d179a54e6049505716c6365baae19 Mon Sep 17 00:00:00 2001 From: Sihui Han Date: Mon, 10 Apr 2017 21:19:30 +0000 Subject: [PATCH 3/3] Create sonic_platform.py Signed-off-by: Sihui Han --- src/sonic-config-engine/minigraph.py | 20 -------------- src/sonic-config-engine/setup.py | 2 +- src/sonic-config-engine/sonic-cfggen | 4 +-- src/sonic-config-engine/sonic_platform.py | 32 +++++++++++++++++++++++ 4 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 src/sonic-config-engine/sonic_platform.py diff --git a/src/sonic-config-engine/minigraph.py b/src/sonic-config-engine/minigraph.py index ac6ea4fb45ae..8ca3456f8dd0 100644 --- a/src/sonic-config-engine/minigraph.py +++ b/src/sonic-config-engine/minigraph.py @@ -47,26 +47,6 @@ def default(self, obj): return str(obj) return json.JSONEncoder.default(self, obj) -def get_machine_info(): - if not os.path.isfile('/host/machine.conf'): - return None - machine_vars = {} - with open('/host/machine.conf') as machine_file: - for line in machine_file: - tokens = line.split('=') - if len(tokens) < 2: - continue - machine_vars[tokens[0]] = tokens[1].strip() - return machine_vars - -def get_platform_info(machine_info): - if machine_info != None: - if machine_info.has_key('onie_platform'): - return machine_info['onie_platform'] - elif machine_info.has_key('aboot_platform'): - return machine_info['aboot_platform'] - return None - def parse_png(png, hname): neighbors = {} devices = {} diff --git a/src/sonic-config-engine/setup.py b/src/sonic-config-engine/setup.py index b9e6ace7cccf..ac4eb16b30e1 100755 --- a/src/sonic-config-engine/setup.py +++ b/src/sonic-config-engine/setup.py @@ -15,7 +15,7 @@ def get_test_suite(): author='Taoyu Li', author_email='taoyl@microsoft.com', url='https://github.com/Azure/sonic-buildimage', - py_modules=['minigraph', 'openconfig_acl'], + py_modules=['minigraph', 'openconfig_acl', 'sonic_platform'], scripts=['sonic-cfggen', 'translate_acl'], install_requires=['lxml', 'jinja2', 'netaddr', 'ipaddr', 'pyyaml', 'pyangbind'], test_suite='setup.get_test_suite', diff --git a/src/sonic-config-engine/sonic-cfggen b/src/sonic-config-engine/sonic-cfggen index 6b5b67ddd824..822f02d7fcc2 100755 --- a/src/sonic-config-engine/sonic-cfggen +++ b/src/sonic-config-engine/sonic-cfggen @@ -7,10 +7,10 @@ import yaml import jinja2 import netaddr import json -from minigraph import get_machine_info -from minigraph import get_platform_info from minigraph import minigraph_encoder from minigraph import parse_xml +from sonic_platform import get_machine_info +from sonic_platform import get_platform_info def is_ipv4(value): if not value: diff --git a/src/sonic-config-engine/sonic_platform.py b/src/sonic-config-engine/sonic_platform.py new file mode 100644 index 000000000000..fdc192d09d0a --- /dev/null +++ b/src/sonic-config-engine/sonic_platform.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +import os + +DOCUMENTATION = ''' +--- +module: sonic_platform +version_added: "1.9" +short_description: Retrive platform related facts for a device. +description: + - Retrieve platform related facts from config files. +''' + +def get_machine_info(): + if not os.path.isfile('/host/machine.conf'): + return None + machine_vars = {} + with open('/host/machine.conf') as machine_file: + for line in machine_file: + tokens = line.split('=') + if len(tokens) < 2: + continue + machine_vars[tokens[0]] = tokens[1].strip() + return machine_vars + +def get_platform_info(machine_info): + if machine_info != None: + if machine_info.has_key('onie_platform'): + return machine_info['onie_platform'] + elif machine_info.has_key('aboot_platform'): + return machine_info['aboot_platform'] + return None +