-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tools to generate network configuration from config.json
- Loading branch information
1 parent
41ed3f2
commit d2fa13c
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ) |