Skip to content

Commit

Permalink
Add tools to generate network configuration from config.json
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQueret committed Aug 7, 2017
1 parent 41ed3f2 commit d2fa13c
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions generate_network_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# -*- encoding: UTF-8 -*-
#
# Form based authentication for CherryPy. Requires the
# Session tool to be loaded.
#

import os
import argparse
from config import Config

if __name__ == '__main__':

# Get configuration file in argument
parser = argparse.ArgumentParser(description='ODR Encoder Manager (Tools to generate network config file)')
parser.add_argument('-c','--config', help='configuration filename',required=True)
cli_args = parser.parse_args()

# Check if configuration exist and is readable
if os.path.isfile(cli_args.config) and os.access(cli_args.config, os.R_OK):
print "Use configuration file %s" % (cli_args.config)
else:
print "Configuration file is missing or is not readable - %s" % (cli_args.config)
sys.exit(1)

# Load configuration
conf = Config(cli_args.config)

# Write network/interfaces file
networkInterfaces = "# This file is generated by ODR-EncoderManager\n"
networkInterfaces += "# Please use WebGUI to make changes\n"
networkInterfaces += "\n"
networkInterfaces += "auto lo\n"
networkInterfaces += "iface lo inet loopback\n"
networkInterfaces += "\n"
for card in conf.config['global']['network']['cards']:
if (card['ip'].strip() != "") and (card['netmask'].strip() != ""):
networkInterfaces += "allow-hotplug %s\n" % (card['card'])
if card['dhcp'] == "true":
networkInterfaces += "iface %s inet %s\n" % (card['card'], 'dhcp')
else:
networkInterfaces += "iface %s inet %s\n" % (card['card'], 'static')
networkInterfaces += " address %s\n" % (card['ip'])
networkInterfaces += " netmask %s\n" % (card['netmask'])
if card['gateway'].strip() != "":
networkInterfaces += " gateway %s\n" % (card['gateway'])
networkInterfaces += "\n"

try:
with open(conf.config['global']['networkInterfaces_file'], 'w') as supfile:
supfile.write(networkInterfaces)
except Exception,e:
raise ValueError( 'Error when writing network/interfaces file', str(e) )

0 comments on commit d2fa13c

Please sign in to comment.