Skip to content

Commit

Permalink
[sonic-cfggen] Remove machine.conf info and add get_system_mac support (
Browse files Browse the repository at this point in the history
#1397)

[sonic-cfggen] Remove machine.conf info and add get_system_mac support
  • Loading branch information
taoyl-ms committed Feb 20, 2018
1 parent f657df7 commit 04b6944
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 54 deletions.
2 changes: 1 addition & 1 deletion dockers/docker-orchagent/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sonic-cfggen -d -t /usr/share/sonic/templates/switch.json.j2 > /etc/swss/config.
sonic-cfggen -d -t /usr/share/sonic/templates/ipinip.json.j2 > /etc/swss/config.d/ipinip.json
sonic-cfggen -d -t /usr/share/sonic/templates/ports.json.j2 > /etc/swss/config.d/ports.json

export platform=`sonic-cfggen -v platform`
export platform=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`

rm -f /var/run/rsyslogd.pid

Expand Down
2 changes: 1 addition & 1 deletion dockers/docker-snmp-sv2/snmpd-config-updater
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def log_error(msg):

# Determine whether we are running on an Arista platform
def is_platform_arista():
proc = subprocess.Popen(["sonic-cfggen", "-v", "platform"],
proc = subprocess.Popen(["sonic-cfggen", "-H", "-v", "DEVICE_METADATA.localhost.platform"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)

(stdout, stderr) = proc.communicate()
Expand Down
2 changes: 1 addition & 1 deletion files/build_templates/docker_image_ctl.j2
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function postStartAction()
}

# Obtain our platform as we will mount directories with these names in each docker
PLATFORM=`sonic-cfggen -v platform`
PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`

{%- if docker_container_name == "database" %}
# Don't mount HWSKU in {{docker_container_name}} container.
Expand Down
4 changes: 2 additions & 2 deletions files/build_templates/swss.service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ExecStartPre=/usr/bin/mst start
ExecStartPre=/usr/bin/mlnx-fw-upgrade.sh
ExecStartPre=/etc/init.d/sxdkernel start
ExecStartPre=/sbin/modprobe i2c-dev
ExecStartPre=/bin/bash -c "/usr/share/sonic/device/$(sonic-cfggen -v platform)/hw-management start"
ExecStartPre=/bin/bash -c "/usr/share/sonic/device/$(sonic-cfggen -H -v DEVICE_METADATA.localhost.platform)/hw-management start"
{% elif sonic_asic_platform == 'cavium' %}
ExecStartPre=/etc/init.d/xpnet.sh start
{% endif %}
Expand All @@ -43,7 +43,7 @@ ExecStop=/usr/bin/{{docker_container_name}}.sh stop
ExecStopPost=/usr/bin/syncd.sh stop

{% if sonic_asic_platform == 'mellanox' %}
ExecStopPost=/bin/bash -c "/usr/share/sonic/device/$(sonic-cfggen -v platform)/hw-management stop"
ExecStopPost=/bin/bash -c "/usr/share/sonic/device/$(sonic-cfggen -H -v DEVICE_METADATA.localhost.platform)/hw-management stop"
ExecStopPost=/etc/init.d/sxdkernel stop
ExecStopPost=/usr/bin/mst stop
{% elif sonic_asic_platform == 'cavium' %}
Expand Down
2 changes: 1 addition & 1 deletion files/image_config/caclmgrd/caclmgrd-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Only start control plance ACL manager daemon if not an Arista platform.
# Arista devices will use their own service ACL manager daemon(s) instead.
if [ "$(sonic-cfggen -v "platform" | grep -c "arista")" -gt 0 ]; then
if [ "$(sonic-cfggen -H -v "DEVICE_METADATA.localhost.platform" | grep -c "arista")" -gt 0 ]; then
echo "Not starting caclmgrd - unsupported platform"
exit 0
fi
Expand Down
27 changes: 7 additions & 20 deletions files/image_config/platform/rc.local
Original file line number Diff line number Diff line change
Expand Up @@ -79,43 +79,30 @@ if [ -f /host/image-$sonic_version/platform/firsttime ]; then
firsttime_exit
fi

# setup initial switch mac
SONIC_ASIC_TYPE=$(sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type)
SYSTEM_MAC_ADDRESS=$(ip link show eth0 | grep ether | awk '{print $2}')

# Align last byte of MAC if necessary
if [ "$SONIC_ASIC_TYPE" = "mellanox" ] || [ "$SONIC_ASIC_TYPE" = "centec" ]; then
last_byte=$(python -c "print '$SYSTEM_MAC_ADDRESS'[-2:]")
aligned_last_byte=$(python -c "print format(int(int('$last_byte', 16) & 0b11000000), '02x')") # put mask and take away the 0x prefix
SYSTEM_MAC_ADDRESS=$(python -c "print '$SYSTEM_MAC_ADDRESS'[:-2] + '$aligned_last_byte'") # put aligned byte into the end of MAC
fi

if [ -f /etc/sonic/init_cfg.json ]; then
sonic-cfggen -j /etc/sonic/init_cfg.json -a '{"DEVICE_METADATA":{"localhost": {"mac": "'$SYSTEM_MAC_ADDRESS'"}}}' --print-data > /tmp/init_cfg.json
mv /tmp/init_cfg.json /etc/sonic/init_cfg.json
else
sonic-cfggen -a '{"DEVICE_METADATA":{"localhost": {"mac": "'$SYSTEM_MAC_ADDRESS'"}}}' --print-data > /etc/sonic/init_cfg.json
if [ ! -f /etc/sonic/init_cfg.json ]; then
# Generate an empty init_cfg.json
echo '{}' > /etc/sonic/init_cfg.json
fi

# Try to take old configuration saved during installation
if test_config; then
rm -f /host/old_config/sonic_version.yml
mv -f /host/old_config/* /etc/sonic/
if [ ! -f /etc/sonic/config_db.json ]; then
sonic-cfggen -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
fi
elif [ -f /host/minigraph.xml ]; then
mv /host/minigraph.xml /etc/sonic/
# Combine information in minigraph and init_cfg.json to form initiate config DB dump file.
# TODO: After moving all information from minigraph to DB, sample config DB dump should be provide
sonic-cfggen -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
else
# Use default minigraph.xml
cp /usr/share/sonic/device/$platform/minigraph.xml /etc/sonic/
sonic-cfggen -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
fi

HWSKU=`sonic-cfggen -m /etc/sonic/minigraph.xml -v "DEVICE_METADATA['localhost']['hwsku']"`
HWSKU=`sonic-cfggen -m /etc/sonic/minigraph.xml -v DEVICE_METADATA.localhost.hwsku`
if [ -f /usr/share/sonic/device/$platform/$HWSKU/buffers.json.j2 ]; then
# generate and merge buffers configuration into config file
sonic-cfggen -m -t /usr/share/sonic/device/$platform/$HWSKU/buffers.json.j2 > /tmp/buffers.json
Expand Down
2 changes: 1 addition & 1 deletion files/image_config/ssh/sshd-config-updater
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def log_error(msg):

# Determine whether we are running on an Arista platform
def is_platform_arista():
proc = subprocess.Popen(["sonic-cfggen", "-v", "platform"],
proc = subprocess.Popen(["sonic-cfggen", "-H", "-v", "DEVICE_METADATA.localhost.platform"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)

(stdout, stderr) = proc.communicate()
Expand Down
6 changes: 3 additions & 3 deletions files/image_config/updategraph/updategraph
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if [ "$src" = "dhcp" ]; then
if [ "`cat /tmp/dhcp_graph_url`" = "N/A" ]; then
echo "'N/A' found in DHCP response. Skipping graph update and generating an empty configuration."
echo '{"DEVICE_METADATA":' > /tmp/device_meta.json
sonic-cfggen -m /etc/sonic/minigraph.xml --var-json DEVICE_METADATA >> /tmp/device_meta.json
sonic-cfggen -H -m /etc/sonic/minigraph.xml --var-json DEVICE_METADATA >> /tmp/device_meta.json
echo '}' >> /tmp/device_meta.json
if [ -f /etc/sonic/init_cfg.json ]; then
sonic-cfggen -j /tmp/device_meta.json -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
Expand Down Expand Up @@ -88,9 +88,9 @@ done

echo "Regenerating config DB from minigraph..."
if [ -f /etc/sonic/init_cfg.json ]; then
sonic-cfggen -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
else
sonic-cfggen -m --print-data > /etc/sonic/config_db.json
sonic-cfggen -H -m --print-data > /etc/sonic/config_db.json
fi

# Mark as disabled after graph is successfully downloaded
Expand Down
8 changes: 1 addition & 7 deletions platform/p4/docker-sonic-p4/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ fi

mkdir -p /etc/swss/config.d/

# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/ipinip.json.j2 > /etc/swss/config.d/ipinip.json
# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/mirror.json.j2 > /etc/swss/config.d/mirror.json
# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/ports.json.j2 > /etc/swss/config.d/ports.json

# export platform=`sonic-cfggen -v platform`

rm -f /var/run/rsyslogd.pid

echo "Start rsyslogd"
Expand Down Expand Up @@ -94,4 +88,4 @@ VLAN=`sonic-cfggen -d -v 'VLAN.keys() | join(" ") if VLAN'`
if [ "$VLAN" != "" ]; then
echo "Start arp_update"
supervisorctl start arp_update
fi
fi
6 changes: 0 additions & 6 deletions platform/vs/docker-sonic-vs/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ fi

mkdir -p /etc/swss/config.d/

# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/ipinip.json.j2 > /etc/swss/config.d/ipinip.json
# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/mirror.json.j2 > /etc/swss/config.d/mirror.json
# sonic-cfggen -m /etc/sonic/minigraph.xml -d -t /usr/share/sonic/templates/ports.json.j2 > /etc/swss/config.d/ports.json

# export platform=`sonic-cfggen -v platform`

rm -f /var/run/rsyslogd.pid

supervisorctl start rsyslogd
Expand Down
26 changes: 16 additions & 10 deletions src/sonic-config-engine/sonic-cfggen
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ from minigraph import parse_xml
from minigraph import parse_device_desc_xml
from sonic_platform import get_machine_info
from sonic_platform import get_platform_info
from sonic_platform import get_system_mac
from swsssdk import ConfigDBConnector

def is_ipv4(value):
Expand Down Expand Up @@ -135,6 +136,7 @@ def main():
parser.add_argument("-j", "--json", help="json file that contains additional variables", action='append', default=[])
parser.add_argument("-a", "--additional-data", help="addition data, in json string")
parser.add_argument("-d", "--from-db", help="read config from configdb", action='store_true')
parser.add_argument("-H", "--platform-info", help="read platform and hardware info", action='store_true')
parser.add_argument("-s", "--redis-unix-sock-file", help="unix sock file for redis connection")
group = parser.add_mutually_exclusive_group()
group.add_argument("-t", "--template", help="render the data with the template file")
Expand All @@ -144,25 +146,22 @@ def main():
group.add_argument("--print-data", help="print all data", action='store_true')
args = parser.parse_args()

data = {}
machine_info = get_machine_info()
if machine_info != None:
deep_update(data, machine_info)
platform_info = get_platform_info(machine_info)
if platform_info != None:
data['platform'] = platform_info
platform = get_platform_info(get_machine_info())

db_kwargs = {}
if args.redis_unix_sock_file != None:
db_kwargs['unix_socket_path'] = args.redis_unix_sock_file

data = {}


if args.minigraph != None:
minigraph = args.minigraph
if data.has_key('platform'):
if platform:
if args.port_config != None:
deep_update(data, parse_xml(minigraph, data['platform'], args.port_config))
deep_update(data, parse_xml(minigraph, platform, args.port_config))
else:
deep_update(data, parse_xml(minigraph, data['platform']))
deep_update(data, parse_xml(minigraph, platform))
else:
if args.port_config != None:
deep_update(data, parse_xml(minigraph, port_config_file=args.port_config))
Expand All @@ -189,6 +188,13 @@ def main():
configdb.connect()
deep_update(data, FormatConverter.db_to_output(configdb.get_config()))

if args.platform_info:
hardware_data = {'DEVICE_METADATA': {'localhost': {
'platform': platform,
'mac': get_system_mac()
}}}
deep_update(data, hardware_data)

if args.template != None:
template_file = os.path.abspath(args.template)
env = jinja2.Environment(loader=jinja2.FileSystemLoader('/'), trim_blocks=True)
Expand Down
24 changes: 24 additions & 0 deletions src/sonic-config-engine/sonic_platform.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python
import os
import yaml
import subprocess

DOCUMENTATION = '''
---
Expand Down Expand Up @@ -29,3 +31,25 @@ def get_platform_info(machine_info):
elif machine_info.has_key('aboot_platform'):
return machine_info['aboot_platform']
return None

def get_sonic_version_info():
if not os.path.isfile('/etc/sonic/version.yml'):
return None
data = {}
with open('/etc/sonic/version.yml') as stream:
data = yaml.load(stream)
return data

def get_system_mac():
proc = subprocess.Popen("ip link show eth0 | grep ether | awk '{print $2}'", shell=True, stdout=subprocess.PIPE)
(mac, err) = proc.communicate()
mac = mac.strip()

# Align last byte of MAC if necessary
version_info = get_sonic_version_info()
if version_info and (version_info['asic_type'] == 'mellanox' or version_info['asic_type'] == 'centec'):
last_byte = mac[-2:]
aligned_last_byte = format(int(int(last_byte, 16) & 0b11000000), '02x')
mac = mac[:-2] + aligned_last_byte
return mac

2 changes: 1 addition & 1 deletion src/sonic-utilities

0 comments on commit 04b6944

Please sign in to comment.