Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sonic-cfggen]:move get_machine_info function to lib #489

Merged
merged 3 commits into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/sonic-config-engine/minigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def default(self, obj):
return str(obj)
return json.JSONEncoder.default(self, obj)


def parse_png(png, hname):
neighbors = {}
devices = {}
Expand Down
2 changes: 1 addition & 1 deletion src/sonic-config-engine/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Copy link
Collaborator

@qiluo-msft qiluo-msft Apr 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'minigraph', 'openconfig_acl', 'sonic_platform' [](start = 18, length = 47)

How about just one module? @taoyl-ms any comment? #Closed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm for splitting into multiple modules based on functionality. Actually I've always been feeling not comfortable to have port config logic in minigraph but have not had chance to refactor it.

scripts=['sonic-cfggen', 'translate_acl'],
install_requires=['lxml', 'jinja2', 'netaddr', 'ipaddr', 'pyyaml', 'pyangbind'],
test_suite='setup.get_test_suite',
Expand Down
25 changes: 6 additions & 19 deletions src/sonic-config-engine/sonic-cfggen
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import yaml
import jinja2
import netaddr
import json
from minigraph import parse_xml
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:
Expand Down Expand Up @@ -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")
Expand All @@ -74,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
Expand Down
32 changes: 32 additions & 0 deletions src/sonic-config-engine/sonic_platform.py
Original file line number Diff line number Diff line change
@@ -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