aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve McIntyre <steve.mcintyre@linaro.org>2016-03-12 11:57:53 +0000
committerSteve McIntyre <steve.mcintyre@linaro.org>2016-03-12 11:57:53 +0000
commit460358ab289eec4da133cf2918095598287c4786 (patch)
tree8ad374a66e09fc87406d173d8fe079bc6eb01dc7
parent0850e9b47c23af820c309ad945927626f5905900 (diff)
Massive rewrite of admin.py: use argparse instead of optparse
Gives significantly better command line handling: * git-like subcommands instead of --arguments for commands * better enforcement of arguments for subcommands, and better help Change-Id: I131e132da6a12744f5e89e21f01163449b90dd70
-rwxr-xr-xadmin.py851
1 files changed, 405 insertions, 446 deletions
diff --git a/admin.py b/admin.py
index 3699ccc..1e833d1 100755
--- a/admin.py
+++ b/admin.py
@@ -21,7 +21,7 @@
#
import os, sys
-import optparse
+import argparse
import datetime, time
vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
@@ -91,704 +91,663 @@ def call_vland(msgtype, msg):
config = VlanConfig(filenames=('./vland.cfg',))
-usage = 'Usage: %prog --command [command options]'
-parser = optparse.OptionParser(usage=usage, description=banner)
+parser = argparse.ArgumentParser()
+####################
# System commands
-system_group = optparse.OptionGroup(parser, "System commands")
-system_group.add_option("--status",
- dest="status",
- action = "store_true",
- default = False,
- help = "Describe current system status")
-system_group.add_option("--statistics",
- dest="statistics",
- action = "store_true",
- default = False,
- help = "Print some system statistics")
-system_group.add_option("--version",
- dest="version",
- action = "store_true",
- default = False,
- help = "Describe the version of this admin interface")
-system_group.add_option("--vland_version",
- dest="vland_version",
- action = "store_true",
- default = False,
- help = "Describe the version of the running VLANd")
-system_group.add_option("--auto_import_switch",
- dest = "auto_import_switch",
- action = "store",
- type = "string",
- help = "Attempt to import a switch's configuration into the VLANd system",
- nargs = 1,
- metavar = "<switch name>")
-system_group.add_option("--probe_switches",
- dest = "probe_switches",
- action = "store_true",
- default = False,
- help = "Probe all the configured switches")
-system_group.add_option("--shutdown",
- dest = "shutdown",
- action = "store_true",
- default = False,
- help = "Shut down a running VLANd")
-parser.add_option_group(system_group)
-# May add shutdown, self-check etc. later
+####################
+sp = parser.add_subparsers(title='Sub-commands',
+ help="Commands",
+ dest='subparser_name')
+p_status = sp.add_parser("status",
+ help="Describe current system status")
+p_status.set_defaults(which = "status")
+p_statistics = sp.add_parser("statistics",
+ help="Print some system statistics")
+p_statistics.set_defaults(which = "statistics")
+p_version = sp.add_parser("version",
+ help="Describe the version of this admin interface")
+p_version.set_defaults(which = "version")
+p_vland_version = sp.add_parser("vland_version",
+ help="Describe the version of the running VLANd")
+p_vland_version.set_defaults(which = "vland_version")
+p_probe_switches = sp.add_parser("probe_switches",
+ help="Probe all the configured switches")
+p_probe_switches.set_defaults(which = "probe_switches")
+p_auto_import = sp.add_parser("auto_import_switch",
+ help="Attempt to import a switch's configuration into the VLANd system")
+p_auto_import.add_argument('--name',
+ required = True,
+ help="Import the switch named SWITCH_NAME in vland.cfg")
+p_auto_import.set_defaults(which = "auto_import_switch")
+p_shutdown = sp.add_parser("shutdown",
+ help="Shut down a running VLANd")
+p_shutdown.set_defaults(which = "shutdown")
+####################
# Switch commands
-switch_group = optparse.OptionGroup(parser, "Switch commands")
-switch_group.add_option("--list_all_switches",
- dest = "list_all_switches",
- action = "store_true",
- default = False,
- help = "List all the existing switches in the system")
-switch_group.add_option("--create_switch",
- dest = "create_switch",
- action = "store",
- type = "string",
- help = "Add a new switch to the system",
- nargs = 1,
- metavar = "<name>")
-switch_group.add_option("--delete_switch",
- dest = "delete_switch",
- action = "store",
- type = "int",
- help = "Remove an existing switch from the system",
- default = None,
- nargs = 1,
- metavar = "<switch_id>")
-switch_group.add_option("--show_switch",
- dest = "show_switch",
- action = "store",
- type = "int",
- help = "Show the details of an existing switch in the system",
- default = None,
- nargs = 1,
- metavar = "<switch_id>")
-switch_group.add_option("--lookup_switch_by_name",
- dest = "lookup_switch_by_name",
- action = "store",
- type = "string",
- help = "Lookup a switch ID by name",
- nargs = 1,
- metavar = "<name>")
-switch_group.add_option("--list_switch_ports",
- dest = "list_switch_ports",
- action = "store",
- type = "int",
- help = "List the IDs of the ports on an existing switch in the system",
- default = None,
- nargs = 1,
- metavar = "<switch_id>")
-parser.add_option_group(switch_group)
+####################
+p_list_all_switches = sp.add_parser("list_all_switches",
+ help="List all the existing switches in the system")
+p_list_all_switches.set_defaults(which = "list_all_switches")
+p_create_switch = sp.add_parser("create_switch",
+ help = "Add a new switch to the system")
+p_create_switch.set_defaults(which = "create_switch")
+p_create_switch.add_argument('--name',
+ required = True,
+ help="The name of the new switch, as described in vland.cfg")
+p_delete_switch = sp.add_parser("delete_switch",
+ help = "Remove an existing switch from the system")
+p_delete_switch.set_defaults(which = "delete_switch")
+p_delete_switch.add_argument('--switch_id',
+ required = True,
+ help = "The ID of the switch to remove")
+p_show_switch = sp.add_parser("show_switch",
+ help = "Show the details of an existing switch in the system")
+p_show_switch.set_defaults(which = "show_switch")
+p_show_switch.add_argument('--switch_id',
+ required = True,
+ help = "The ID of the switch to show")
+p_lookup_switch_by_name = sp.add_parser("lookup_switch_by_name",
+ help = "Lookup a switch ID by name")
+p_lookup_switch_by_name.set_defaults(which = "lookup_switch_by_name")
+p_lookup_switch_by_name.add_argument('--name',
+ required = True,
+ help = "The switch name to search for")
-# Port commands
-port_group = optparse.OptionGroup(parser, "Port commands")
-port_group.add_option("--list_all_ports",
- dest = "list_all_ports",
- action = "store_true",
- default = False,
- help = "List all the existing ports in the system")
-port_group.add_option("--create_port",
- dest = "create_port",
- action = "store",
- type = "string",
- help = "Add a new port to the system",
- nargs = 3,
- metavar = "<switch_id> <name> <number>")
-port_group.add_option("--delete_port",
- dest = "delete_port",
- action = "store",
- type = "int",
- help = "Remove an existing port from the system",
- default = None,
- nargs = 1,
- metavar = "<port_id>")
-port_group.add_option("--show_port",
- dest = "show_port",
- action = "store",
- type = "int",
- help = "Show the details of an existing port in the system",
- default = None,
- nargs = 1,
- metavar = "<port_id>")
-port_group.add_option("--lookup_port_by_switch_and_name",
- dest = "lookup_port_by_switch_and_name",
- action = "store",
- type = "string",
- help = "Lookup a port ID by switch and port name",
- nargs = 2,
- metavar = "<switch_id> <name>")
-port_group.add_option("--lookup_port_by_switch_and_number",
- dest = "lookup_port_by_switch_and_number",
- action = "store",
- type = "string",
- help = "Lookup a port ID by switch and number",
- nargs = 2,
- metavar = "<switch_id> <name>")
-port_group.add_option("--lookup_ports_by_switch",
- dest = "lookup_ports_by_switch",
- action = "store",
- type = "string",
- help = "Lookup port ID(s) by switch",
- nargs = 1,
- metavar = "<switch_id>")
-port_group.add_option("--lookup_ports_by_current_vlan",
- dest = "lookup_ports_by_current_vlan",
- action = "store",
- type = "string",
- help = "Lookup port ID(s) by current VLAN",
- nargs = 1,
- metavar = "<vlan_id>")
-port_group.add_option("--lookup_ports_by_base_vlan",
- dest = "lookup_ports_by_base_vlan",
- action = "store",
- type = "string",
- help = "Lookup port ID(s) by base VLAN",
- nargs = 1,
- metavar = "<vlan_id>")
-port_group.add_option("--lookup_ports_by_trunk",
- dest = "lookup_ports_by_trunk",
- action = "store",
- type = "string",
- help = "Lookup port ID(s) by trunk",
- nargs = 1,
- metavar = "<trunk_id>")
-port_group.add_option("--set_port_mode",
- dest = "set_port_mode",
- action = "store",
- type = "string",
- help = "Set the mode of a port to 'trunk' or 'access'",
- nargs = 2,
- metavar = "<port_id> <mode>")
-port_group.add_option("--lock_port",
- dest = "lock_port",
- action = "store",
- type = "string",
- help = "Lock the settings on a port",
- nargs = 1,
- metavar = "<port_id>")
-port_group.add_option("--unlock_port",
- dest = "unlock_port",
- action = "store",
- type = "string",
- help = "Unock the settings on a port",
- nargs = 1,
- metavar = "<port_id>")
-port_group.add_option("--set_port_current_vlan",
- dest = "set_port_current_vlan",
- action = "store",
- type = "int",
- help = "Set the current VLAN assignment for a port",
- nargs = 2,
- metavar = "<port_id> <vlan_id>")
-port_group.add_option("--get_port_current_vlan",
- dest = "get_port_current_vlan",
- action = "store",
- type = "int",
- help = "Get the current VLAN assignment for a port",
- nargs = 1,
- metavar = "<port_id>")
-port_group.add_option("--set_port_base_vlan",
- dest = "set_port_base_vlan",
- action = "store",
- type = "int",
- help = "Set the base VLAN assignment for a port",
- nargs = 2,
- metavar = "<port_id> <vlan_id>")
-port_group.add_option("--get_port_base_vlan",
- dest = "get_port_base_vlan",
- action = "store",
- type = "int",
- help = "Get the base VLAN assignment for a port",
- nargs = 1,
- metavar = "<port_id>")
-port_group.add_option("--restore_port_to_base_vlan",
- dest = "restore_port_to_base_vlan",
- action = "store",
- type = "int",
- help = "Reset the port back to its base VLAN",
- nargs = 1,
- metavar = "<port_id>")
-parser.add_option_group(port_group)
+####################
+# Ports commands
+####################
+p_list_all_ports = sp.add_parser("list_all_ports",
+ help="List all the existing ports in the system")
+p_list_all_ports.set_defaults(which = "list_all_ports")
+p_create_port = sp.add_parser("create_port",
+ help = "Add a new port to the system")
+p_create_port.set_defaults(which = "create_port")
+p_create_port.add_argument('--switch_id',
+ required = True,
+ help="The ID of the switch containing the new port")
+p_create_port.add_argument('--name',
+ required = True,
+ help="The name of the new port")
+p_create_port.add_argument('--number',
+ required = True,
+ help="The human-friendly number for the new port")
+p_delete_port = sp.add_parser("delete_port",
+ help = "Remove an existing port from the system")
+p_delete_port.set_defaults(which = "delete_port")
+p_delete_port.add_argument('--port_id',
+ required = True,
+ help = "The ID of the port to remove")
+p_show_port = sp.add_parser("show_port",
+ help = "Show the details of an existing port in the system")
+p_show_port.set_defaults(which = "show_port")
+p_show_port.add_argument('--port_id',
+ required = True,
+ help = "The ID of the port to show")
+p_lookup_port_by_switch_and_name = sp.add_parser("lookup_port_by_switch_and_name",
+ help = "Lookup a port ID by switch ID and port name")
+p_lookup_port_by_switch_and_name.set_defaults(which = "lookup_port_by_switch_and_name")
+p_lookup_port_by_switch_and_name.add_argument('--switch_id',
+ required = True,
+ help = "The switch ID to search for")
+p_lookup_port_by_switch_and_name.add_argument('--name',
+ required = True,
+ help = "The port name to search for")
+p_lookup_port_by_switch_and_number = sp.add_parser("lookup_port_by_switch_and_number",
+ help = "Lookup a port ID by switch ID and port number")
+p_lookup_port_by_switch_and_number.set_defaults(which = "lookup_port_by_switch_and_number")
+p_lookup_port_by_switch_and_number.add_argument('--switch_id',
+ required = True,
+ help = "The switch ID to search for")
+p_lookup_port_by_switch_and_number.add_argument('--number',
+ required = True,
+ help = "The port number to search for")
+p_lookup_ports_by_switch = sp.add_parser("lookup_ports_by_switch",
+ help = "Lookup port ID(s) by switch ID")
+p_lookup_ports_by_switch.set_defaults(which = "lookup_ports_by_switch")
+p_lookup_ports_by_switch.add_argument('--switch_id',
+ required = True,
+ help = "The switch ID to search for")
+p_lookup_ports_by_current_vlan = sp.add_parser("lookup_ports_by_current_vlan",
+ help = "Lookup port ID(s) by current VLAN ID")
+p_lookup_ports_by_current_vlan.set_defaults(which = "lookup_ports_by_current_vlan")
+p_lookup_ports_by_current_vlan.add_argument('--vlan_id',
+ required = True,
+ help = "The VLAN ID to search for")
+p_lookup_ports_by_base_vlan = sp.add_parser("lookup_ports_by_base_vlan",
+ help = "Lookup port ID(s) by base vlan ID")
+p_lookup_ports_by_base_vlan.set_defaults(which = "lookup_ports_by_base_vlan")
+p_lookup_ports_by_base_vlan.add_argument('--vlan_id',
+ required = True,
+ help = "The VLAN ID to search for")
+p_lookup_ports_by_trunk = sp.add_parser("lookup_ports_by_trunk",
+ help = "Lookup port ID(s) by trunk ID")
+p_lookup_ports_by_trunk.set_defaults(which = "lookup_ports_by_trunk")
+p_lookup_ports_by_trunk.add_argument('--trunk_id',
+ required = True,
+ help = "The trunk ID to search for")
+p_set_port_mode = sp.add_parser("set_port_mode",
+ help = "Set the mode of a port to 'trunk' or 'access'")
+p_set_port_mode.set_defaults(which = "set_port_mode")
+p_set_port_mode.add_argument('--port_id',
+ required = True,
+ help = "The ID of the port to modify")
+p_set_port_mode.add_argument('--mode',
+ required = True,
+ help = "The mode to select ('trunk' or 'access')")
+p_get_port_mode = sp.add_parser("get_port_mode",
+ help = "Get the mode of a port ('trunk' or 'access')")
+p_get_port_mode.set_defaults(which = "get_port_mode")
+p_get_port_mode.add_argument('--port_id',
+ required = True,
+ help = "The ID of the port to query")
+p_lock_port = sp.add_parser("lock_port",
+ help = "Lock the settings on a port")
+p_lock_port.set_defaults(which = "lock_port")
+p_lock_port.add_argument('--port_id',
+ required = True,
+ help = "The ID of the port to lock")
+p_unlock_port = sp.add_parser("unlock_port",
+ help = "Unlock the settings on a port")
+p_unlock_port.set_defaults(which = "unlock_port")
+p_unlock_port.add_argument('--port_id',
+ required = True,
+ help = "The ID of the port to unlock")
+p_set_port_current_vlan = sp.add_parser("set_port_current_vlan",
+ help = "Set the current VLAN assignment for a port")
+p_set_port_current_vlan.set_defaults(which = "set_port_current_vlan")
+p_set_port_current_vlan.add_argument('--port_id',
+ required = True,
+ help = "The ID of the port to modify")
+p_set_port_current_vlan.add_argument('--vlan_id',
+ required = True,
+ help = "The VLAN ID to be used")
+p_get_port_current_vlan = sp.add_parser("get_port_current_vlan",
+ help = "Get the current VLAN assignment for a port")
+p_get_port_current_vlan.set_defaults(which = "get_port_current_vlan")
+p_get_port_current_vlan.add_argument('--port_id',
+ required = True,
+ help = "The ID of the port to query")
+p_set_port_base_vlan = sp.add_parser("set_port_base_vlan",
+ help = "Set the base VLAN assignment for a port")
+p_set_port_base_vlan.set_defaults(which = "set_port_base_vlan")
+p_set_port_base_vlan.add_argument('--port_id',
+ required = True,
+ help = "The ID of the port to modify")
+p_set_port_base_vlan.add_argument('--vlan_id',
+ required = True,
+ help = "The VLAN ID to be used")
+p_get_port_base_vlan = sp.add_parser("get_port_base_vlan",
+ help = "Get the base VLAN assignment for a port")
+p_get_port_base_vlan.set_defaults(which = "get_port_base_vlan")
+p_get_port_base_vlan.add_argument('--port_id',
+ required = True,
+ help = "The ID of the port to query")
+p_restore_port_to_base_vlan = sp.add_parser("restore_port_to_base_vlan",
+ help = "Reset a port back to its base VLAN")
+p_restore_port_to_base_vlan.set_defaults(which = "restore_port_to_base_vlan")
+p_restore_port_to_base_vlan.add_argument('--port_id',
+ required = True,
+ help = "The ID of the port to modify")
+####################
# VLAN commands
-vlan_group = optparse.OptionGroup(parser, "VLAN commands")
-vlan_group.add_option("--list_all_vlans",
- dest = "list_all_vlans",
- action = "store_true",
- default = False,
- help = "List all the existing vlans in the system")
-vlan_group.add_option("--create_vlan",
- dest = "create_vlan",
- action = "store",
- type = "string",
- help = "Add a new vlan to the system",
- nargs = 3,
- metavar = "<name> <tag> <is_base_vlan>")
-vlan_group.add_option("--delete_vlan",
- dest = "delete_vlan",
- action = "store",
- type = "int",
- help = "Remove an existing vlan from the system",
- default = None,
- nargs = 1,
- metavar = "<vlan_id>")
-vlan_group.add_option("--show_vlan",
- dest = "show_vlan",
- action = "store",
- type = "int",
- help = "Show the details of an existing vlan in the system",
- default = None,
- nargs = 1,
- metavar = "<vlan_id>")
-vlan_group.add_option("--lookup_vlan_by_tag",
- dest = "lookup_vlan_by_tag",
- action = "store",
- type = "int",
- help = "Find the vlan ID of an existing vlan in the system",
- default = None,
- nargs = 1,
- metavar = "<tag>")
-vlan_group.add_option("--show_vlan_tag",
- dest = "show_vlan_tag",
- action = "store",
- type = "int",
- help = "Print the vlan tag of an existing vlan in the system",
- default = None,
- nargs = 1,
- metavar = "<vlan_id>")
-parser.add_option_group(vlan_group)
+####################
+p_list_all_vlans = sp.add_parser("list_all_vlans",
+ help="List all the existing VLANs in the system")
+p_list_all_vlans.set_defaults(which = "list_all_vlans")
+p_create_vlan = sp.add_parser("create_vlan",
+ help = "Add a new VLAN to the system")
+p_create_vlan.set_defaults(which = "create_vlan")
+p_create_vlan.add_argument('--name',
+ required = True,
+ help="The name of the new VLAN")
+p_create_vlan.add_argument('--tag',
+ required = True,
+ help="The tag for the new VLAN (-1 to automatically select)")
+p_create_vlan.add_argument('--is_base_vlan',
+ required = True,
+ help="Is the new VLAN a base VLAN (true/false)")
+p_delete_vlan = sp.add_parser("delete_vlan",
+ help = "Remove an existing VLAN from the system")
+p_delete_vlan.set_defaults(which = "delete_vlan")
+p_delete_vlan.add_argument('--vlan_id',
+ required = True,
+ help = "The ID of the VLAN to remove")
+p_show_vlan = sp.add_parser("show_vlan",
+ help = "Show the details of an existing VLAN in the system")
+p_show_vlan.set_defaults(which = "show_vlan")
+p_show_vlan.add_argument('--vlan_id',
+ required = True,
+ help = "The ID of the VLAN to show")
+p_lookup_vlan_by_tag = sp.add_parser("lookup_vlan_by_tag",
+ help = "Find the VLAN ID of an existing VLAN in the system")
+p_lookup_vlan_by_tag.set_defaults(which = "lookup_vlan_by_tag")
+p_lookup_vlan_by_tag.add_argument('--tag',
+ required = True,
+ help = "The VLAN tag to search for")
+p_show_vlan_tag = sp.add_parser("show_vlan_tag",
+ help = "Print the VLAN tag of an existing VLAN in the system")
+p_show_vlan_tag.set_defaults(which = "show_vlan_tag")
+p_show_vlan_tag.add_argument('--vlan_id',
+ required = True,
+ help = "The VLAN ID to search for")
-# Port commands
-trunk_group = optparse.OptionGroup(parser, "Trunk commands")
-trunk_group.add_option("--list_all_trunks",
- dest = "list_all_trunks",
- action = "store_true",
- default = False,
- help = "List all the existing trunks in the system")
-trunk_group.add_option("--create_trunk",
- dest = "create_trunk",
- action = "store",
- type = "string",
- help = "Add a new trunk to the system, linking two ports",
- nargs = 2,
- metavar = "<port_id1> <port_id2>")
-trunk_group.add_option("--delete_trunk",
- dest = "delete_trunk",
- action = "store",
- type = "int",
- help = "Remove an existing trunk from the system",
- default = None,
- nargs = 1,
- metavar = "<trunk_id>")
-trunk_group.add_option("--show_trunk",
- dest = "show_trunk",
- action = "store",
- type = "int",
- help = "Show the details of an existing trunk in the system",
- default = None,
- nargs = 1,
- metavar = "<trunk_id>")
-parser.add_option_group(trunk_group)
+####################
+# Trunk commands
+####################
+p_list_all_trunks = sp.add_parser("list_all_trunks",
+ help="List all the existing trunks in the system")
+p_list_all_trunks.set_defaults(which = "list_all_trunks")
+p_create_trunk = sp.add_parser("create_trunk",
+ help = "Add a new trunk to the system, linking two ports")
+p_create_trunk.set_defaults(which = "create_trunk")
+p_create_trunk.add_argument('--port_id1',
+ required = True,
+ help="The ID of the first port to be used")
+p_create_trunk.add_argument('--port_id2',
+ required = True,
+ help="The ID of the second port to be used")
+p_delete_trunk = sp.add_parser("delete_trunk",
+ help = "Remove an existing trunk from the system")
+p_delete_trunk.set_defaults(which = "delete_trunk")
+p_delete_trunk.add_argument('--trunk_id',
+ required = True,
+ help = "The ID of the trunk to remove")
+p_show_trunk = sp.add_parser("show_trunk",
+ help = "Show the details of an existing trunk in the system")
+p_show_trunk.set_defaults(which = "show_trunk")
+p_show_trunk.add_argument('--trunk_id',
+ required = True,
+ help = "The ID of the trunk to show")
+args = parser.parse_args()
-(opts, args) = parser.parse_args()
-
-if opts.list_all_switches:
+# Now work out what to do
+if args.which == 'status':
+ print 'Config:'
+ print ' knows about %d switch(es)' % len(config.switches)
+ default_vlan_id = call_vland('db_query',
+ {'command':'db.get_vlan_id_by_tag',
+ 'data':
+ {'tag': config.vland.default_vlan_tag}})
+ print 'The default vlan tag (%d) is vlan ID %d' % (config.vland.default_vlan_tag, default_vlan_id)
+ stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
+ print 'VLANd is running %s' % stat['running']
+ lastmod = datetime.datetime.strptime(stat['last_modified'], '%Y-%m-%dT%H:%M:%S.%f')
+ print 'DB Last modified %s' % lastmod.strftime('%Y-%m-%d %H:%M:%S %Z')
+ print 'DB via VLANd:'
+ switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
+ print ' knows about %d switch(es)' % len(switches)
+ ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
+ print ' knows about %d port(s)' % len(ports)
+ vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
+ print ' DB knows about %d vlan(s)' % len(vlans)
+elif args.which == 'shutdown':
+ print 'Asking VLANd to shutdown'
+ shutdown = call_vland('daemon_query',
+ {'command':'daemon.shutdown',
+ 'data': None})
+ for field in shutdown:
+ print '%s: %s' % (field, shutdown[field])
+elif args.which == 'vland_version':
+ ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
+ print 'VLANd version %s' % ver['version']
+elif args.which == 'statistics':
+ stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
+ print 'VLANd uptime: %d seconds' % stats['uptime']
+elif args.which == 'version':
+ print 'VLANd admin interface version %s' % version
+elif args.which == 'auto_import_switch':
+ print 'Attempting to import switch %s' % args.name
+ if args.name not in config.switches:
+ raise InputError("Can't find switch %s in config" % args.name)
+ imp = call_vland('vlan_update',
+ {'command':'api.auto_import_switch',
+ 'data':
+ {'switch': args.name}})
+ print 'VLANd imported switch %s successfully: new switch_id %d, %d new ports, %d new VLANs' % (args.name, imp['switch_id'], imp['num_ports_added'], imp['num_vlans_added'])
+elif args.which == 'probe_switches':
+ print 'Asking VLANd to probe all the configured switches'
+ probe = call_vland('daemon_query',
+ {'command':'daemon.probe_switches',
+ 'data': None})
+ for field in probe:
+ print '%s: %s' % (field, probe[field])
+elif args.which == 'list_all_switches':
result = call_vland('db_query', {'command':'db.all_switches', 'data':None})
for line in result:
dump_switch(line)
-elif opts.list_all_ports:
+elif args.which == 'list_all_ports':
result = call_vland('db_query', {'command':'db.all_ports', 'data':None})
for line in result:
dump_port(line)
-elif opts.list_all_vlans:
+elif args.which == 'list_all_vlans':
result = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
for line in result:
dump_vlan(line)
-elif opts.list_all_trunks:
+elif args.which == 'list_all_trunks':
result = call_vland('db_query', {'command':'db.all_trunks', 'data':None})
for line in result:
dump_trunk(line)
-elif opts.create_switch is not None:
+elif args.which == 'create_switch':
try:
switch_id = call_vland('db_update',
{'command':'db.create_switch',
'data':
- {'name':opts.create_switch}})
+ {'name':args.name}})
print 'Created switch_id %d' % switch_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.create_port is not None:
+elif args.which == 'create_port':
try:
port_id = call_vland('db_update',
{'command':'db.create_port',
'data':
- {'switch_id': opts.create_port[0],
- 'name': opts.create_port[1],
- 'number': opts.create_port[2]}})
+ {'switch_id': args.switch_id,
+ 'name': args.name,
+ 'number': args.number}})
print 'Created port_id %d' % port_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.create_vlan is not None:
+elif args.which == 'create_vlan':
try:
(vlan_id, vlan_tag) = call_vland('vlan_update',
{'command':'api.create_vlan',
'data':
- {'name': opts.create_vlan[0],
- 'tag': opts.create_vlan[1],
- 'is_base_vlan': is_positive(opts.create_vlan[2])}})
+ {'name': args.name,
+ 'tag': args.tag,
+ 'is_base_vlan': is_positive(args.is_base_vlan)}})
print 'Created VLAN tag %d as vlan_id %d' % (vlan_tag, vlan_id)
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.create_trunk is not None:
+elif args.which == 'create_trunk':
try:
trunk_id = call_vland('db_update',
{'command':'db.create_trunk',
'data':
- {'port_id1': opts.create_trunk[0],
- 'port_id2': opts.create_trunk[1]}})
+ {'port_id1': args.port_id1,
+ 'port_id2': args.port_id2}})
print 'Created trunk_id %d' % trunk_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.delete_switch is not None:
+elif args.which == 'delete_switch':
try:
switch_id = call_vland('db_update',
{'command':'db.delete_switch',
- 'data': {'switch_id': opts.delete_switch}})
- print 'Deleted switch_id %d' % switch_id
+ 'data': {'switch_id': args.switch_id}})
+ print 'Deleted switch_id %s' % switch_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.delete_port is not None:
+elif args.which == 'delete_port':
try:
port_id = call_vland('db_update',
{'command':'db.delete_port',
- 'data': {'port_id': opts.delete_port}})
+ 'data': {'port_id': args.port_id}})
+ print 'Deleted port_id %s' % port_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.delete_vlan is not None:
+elif args.which == 'delete_vlan':
try:
vlan_id = call_vland('vlan_update',
{'command':'api.delete_vlan',
- 'data': {'vlan_id': opts.delete_vlan}})
+ 'data': {'vlan_id': args.vlan_id}})
print 'Deleted vlan_id %d' % vlan_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.delete_trunk is not None:
+elif args.which == 'delete_trunk':
try:
port_id = call_vland('db_update',
{'command':'db.delete_trunk',
- 'data': {'trunk_id': opts.delete_trunk}})
+ 'data': {'trunk_id': args.trunk_id}})
+ print 'Deleted trunk_id %s' % trunk_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.lookup_switch_by_name is not None:
+elif args.which == 'lookup_switch_by_name':
try:
switch_id = call_vland('db_query',
{'command':'db.get_switch_id_by_name',
- 'data':{'name':opts.lookup_switch_by_name}})
+ 'data':{'name':args.name}})
if switch_id is not None:
print '%d' % switch_id
else:
- print 'No switch found for name %s' % opts.lookup_switch_by_name
+ print 'No switch found for name %s' % args.name
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.show_switch is not None:
+elif args.which == 'show_switch':
try:
this_switch = call_vland('db_query',
{'command':'db.get_switch_by_id',
'data':
- {'switch_id': opts.show_switch}})
+ {'switch_id': args.switch_id}})
if this_switch is not None:
dump_switch(this_switch)
else:
- print 'No switch found for switch_id %d' % opts.show_switch
- except InputError as inst:
- print 'Failed: %s' % inst
-elif opts.list_switch_ports is not None:
- try:
- ports = call_vland('db_query',
- {'command':'db.get_ports_by_switch',
- 'data':
- {'switch_id': opts.list_switch_ports}})
- if ports is not None:
- for p in ports:
- print p
- else:
- print 'No ports found for switch_id %d' % opts.list_switch_ports
+ print 'No switch found for switch_id %s' % args.switch_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.show_port is not None:
+elif args.which == 'show_port':
try:
this_port = call_vland('db_query',
{'command':'db.get_port_by_id',
'data':
- {'port_id': opts.show_port}})
+ {'port_id': args.port_id}})
if this_port is not None:
dump_port(this_port)
else:
- print 'No port found for port_id %d' % opts.show_port
+ print 'No port found for port_id %s' % args.port_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.lookup_port_by_switch_and_name is not None:
+elif args.which == 'lookup_port_by_switch_and_name':
try:
p = call_vland('db_query',
{'command':'db.get_port_by_switch_and_name',
'data':
- {'switch_id': opts.lookup_port_by_switch_and_name[0],
- 'name': opts.lookup_port_by_switch_and_name[1]}})
+ {'switch_id': args.switch_id,
+ 'name': args.name}})
if p is not None:
print p
else:
- print 'No port found for switch_id %d, name %s' % (int(opts.lookup_port_by_switch_and_name[0]), opts.lookup_port_by_switch_and_name[1])
+ print 'No port found for switch_id %s, name %s' % (args.switch_id, args.name)
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.lookup_port_by_switch_and_number is not None:
+elif args.which == 'lookup_port_by_switch_and_number':
try:
p = call_vland('db_query',
{'command':'db.get_port_by_switch_and_number',
'data':
- {'switch_id': opts.lookup_port_by_switch_and_number[0],
- 'number': opts.lookup_port_by_switch_and_number[1]}})
+ {'switch_id': args.switch_id,
+ 'number': args.number}})
if p is not None:
print p
else:
- print 'No port found for switch_id %d, port number %d' % (int(opts.lookup_port_by_switch_and_number[0]), int(opts.lookup_port_by_switch_and_number[1]))
+ print 'No port found for switch_id %s, port number %s' % (args.switch_id, args.number)
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.lookup_ports_by_switch is not None:
+elif args.which == 'lookup_ports_by_switch':
try:
p = call_vland('db_query',
{'command':'db.get_ports_by_switch',
'data':
- {'switch_id': opts.lookup_ports_by_switch}})
+ {'switch_id': args.switch_id}})
if p is not None:
for port_id in p:
print port_id
else:
- print 'No ports found for switch_id %d' % int(opts.lookup_ports_by_switch)
+ print 'No ports found for switch_id %s' % args.switch_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.lookup_ports_by_current_vlan is not None:
+elif args.which == 'lookup_ports_by_current_vlan':
try:
p = call_vland('db_query',
{'command':'db.get_ports_by_current_vlan',
'data':
- {'vlan_id': opts.lookup_ports_by_current_vlan}})
+ {'vlan_id': args.vlan_id}})
if p is not None:
for port_id in p:
print port_id
else:
- print 'No ports found for current vlan_id %d' % int(opts.lookup_ports_by_current_vlan)
+ print 'No ports found for current vlan_id %s' % args.vlan_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.lookup_ports_by_base_vlan is not None:
+elif args.which == 'lookup_ports_by_base_vlan':
try:
p = call_vland('db_query',
{'command':'db.get_ports_by_base_vlan',
'data':
- {'vlan_id': opts.lookup_ports_by_base_vlan}})
+ {'vlan_id': args.vlan_id}})
if p is not None:
for port_id in p:
print port_id
else:
- print 'No ports found for base vlan_id %d' % int(opts.lookup_ports_by_base_vlan)
+ print 'No ports found for base vlan_id %s' % args.vlan_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.lookup_ports_by_trunk is not None:
+elif args.which == 'lookup_ports_by_trunk':
try:
p = call_vland('db_query',
{'command':'db.get_ports_by_trunk',
'data':
- {'trunk_id': opts.lookup_ports_by_trunk}})
+ {'trunk_id': args.trunk_id}})
if p is not None:
for port_id in p:
print port_id
else:
- print 'No ports found for trunk_id %d' % int(opts.lookup_ports_by_trunk)
+ print 'No ports found for trunk_id %s' % args.trunk_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.set_port_mode is not None:
+elif args.which == 'set_port_mode':
try:
port_id = call_vland('vlan_update',
{'command':'api.set_port_mode',
'data':
- {'port_id': opts.set_port_mode[0],
- 'mode': opts.set_port_mode[1]}})
+ {'port_id': args.port_id,
+ 'mode': args.mode}})
print "Updated mode for port_id %d" % port_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.lock_port is not None:
+elif args.which == 'lock_port':
try:
port_id = call_vland('db_update',
{'command':'db.set_port_is_locked',
'data':
- {'port_id': opts.lock_port,
+ {'port_id': args.port_id,
'is_locked': True}})
print "Locked port_id %d" % port_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.unlock_port is not None:
+elif args.which == 'unlock_port':
try:
port_id = call_vland('db_update',
{'command':'db.set_port_is_locked',
'data':
- {'port_id': opts.unlock_port,
+ {'port_id': args.port_id,
'is_locked': False}})
print "Unlocked port_id %d" % port_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.set_port_current_vlan is not None:
+elif args.which == 'set_port_current_vlan':
try:
port_id = call_vland('vlan_update',
{'command':'api.set_current_vlan',
'data':
- {'port_id': opts.set_port_current_vlan[0],
- 'vlan_id': opts.set_port_current_vlan[1]}})
+ {'port_id': args.port_id,
+ 'vlan_id': args.vlan_id}})
print "Set current VLAN on port_id %d" % port_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.get_port_current_vlan is not None:
+elif args.which == 'get_port_current_vlan':
try:
vlan_id = call_vland('db_query',
{'command':'db.get_current_vlan_id_by_port',
'data':
- {'port_id': opts.get_port_current_vlan}})
+ {'port_id': args.port_id}})
if vlan_id is not None:
print vlan_id
else:
- print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan
+ print "No current_vlan_id found for port_id %s" % args.port_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.set_port_base_vlan is not None:
+elif args.which == 'set_port_base_vlan':
try:
port_id = call_vland('db_update',
{'command':'db.set_base_vlan',
'data':
- {'port_id': opts.set_port_base_vlan[0],
- 'base_vlan_id': opts.set_port_base_vlan[1]}})
+ {'port_id': args.port_id,
+ 'base_vlan_id': args.vlan_id}})
print "Set base VLAN on port_id %d" % port_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.get_port_base_vlan is not None:
+elif args.which == 'get_port_base_vlan':
try:
vlan_id = call_vland('db_query',
{'command':'db.get_base_vlan_id_by_port',
'data':
- {'port_id': opts.get_port_base_vlan}})
+ {'port_id': args.port_id}})
if vlan_id is not None:
print vlan_id
else:
print "No base_vlan_id found for port_id %d" % port_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.restore_port_to_base_vlan is not None:
+elif args.which == 'restore_port_to_base_vlan':
try:
port_id = call_vland('vlan_update',
{'command': 'api.restore_base_vlan',
'data':
- {'port_id': opts.restore_port_to_base_vlan}})
+ {'port_id': args.port_id}})
print "Restored port_id %d back to base VLAN" % port_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.show_vlan is not None:
+elif args.which == 'show_vlan':
try:
v = call_vland('db_query',
{'command':'db.get_vlan_by_id',
'data':
- {'vlan_id': opts.show_vlan}})
+ {'vlan_id': args.vlan_id}})
if v is not None:
dump_vlan(v)
else:
- print 'No vlan found for vlan_id %d' % opts.show_vlan
+ print 'No VLAN found for vlan_id %s' % args.vlan_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.lookup_vlan_by_tag is not None:
+elif args.which == 'lookup_vlan_by_tag':
try:
vlan_id = call_vland('db_query',
{'command':'db.get_vlan_id_by_tag',
'data':
- {'tag': opts.lookup_vlan_by_tag}})
+ {'tag': args.tag}})
if vlan_id is not None:
print vlan_id
else:
- print 'No vlan found for vlan tag %d' % opts.lookup_vlan_by_tag
+ print 'No VLAN found for vlan tag %s' % args.tag
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.show_vlan_tag is not None:
+elif args.which == 'show_vlan_tag':
try:
vlan_tag = call_vland('db_query',
{'command':'db.get_vlan_tag_by_id',
'data':
- {'vlan_id': opts.show_vlan_tag}})
+ {'vlan_id': args.vlan_id}})
if vlan_tag is not None:
print vlan_tag
else:
- print 'No vlan found for vlan id %d' % opts.show_vlan_tag
+ print 'No VLAN found for vlan id %s' % args.vlan_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.show_trunk is not None:
+elif args.which == 'show_trunk':
try:
this_trunk = call_vland('db_query',
{'command':'db.get_trunk_by_id',
'data':
- {'trunk_id': opts.show_trunk}})
+ {'trunk_id': args.trunk_id}})
if this_trunk is not None:
dump_trunk(this_trunk)
else:
- print 'No port found for port_id %d' % opts.show_port
+ print 'No port found for port_id %s' % args.trunk_id
except InputError as inst:
print 'Failed: %s' % inst
-elif opts.status:
- print 'Config:'
- print ' knows about %d switch(es)' % len(config.switches)
- default_vlan_id = call_vland('db_query',
- {'command':'db.get_vlan_id_by_tag',
- 'data':
- {'tag': config.vland.default_vlan_tag}})
- print 'The default vlan tag (%d) is vlan_id %d' % (config.vland.default_vlan_tag, default_vlan_id)
- stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None})
- print 'VLANd is running %s' % stat['running']
- lastmod = datetime.datetime.strptime(stat['last_modified'], '%Y-%m-%dT%H:%M:%S.%f')
- print 'DB Last modified %s' % lastmod.strftime('%Y-%m-%d %H:%M:%S %Z')
- print 'DB via VLANd:'
- switches = call_vland('db_query', {'command':'db.all_switches', 'data':None})
- print ' knows about %d switch(es)' % len(switches)
- ports = call_vland('db_query', {'command':'db.all_ports', 'data':None})
- print ' knows about %d port(s)' % len(ports)
- vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None})
- print ' DB knows about %d vlan(s)' % len(vlans)
-elif opts.vland_version:
- ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None})
- print 'VLANd version %s' % ver['version']
-elif opts.statistics:
- stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None})
- print 'VLANd uptime: %d seconds' % stats['uptime']
-elif opts.version:
- print 'VLANd admin interface version %s' % version
-elif opts.auto_import_switch:
- print 'Attempting to import switch %s' % opts.auto_import_switch
- if opts.auto_import_switch not in config.switches:
- raise InputError("Can't find switch %s in config" % opts.auto_import_switch)
- imp = call_vland('vlan_update',
- {'command':'api.auto_import_switch',
- 'data':
- {'switch': opts.auto_import_switch}})
- print 'VLANd imported switch %s successfully: new switch_id %d, %d new ports, %d new VLANs' % (opts.auto_import_switch, imp['switch_id'], imp['num_ports_added'], imp['num_vlans_added'])
-elif opts.probe_switches:
- print 'Asking VLANd to probe all the configured switches'
- probe = call_vland('daemon_query',
- {'command':'daemon.probe_switches',
- 'data': None})
- for field in probe:
- print '%s: %s' % (field, probe[field])
-elif opts.shutdown:
- print 'Asking VLANd to shutdown'
- shutdown = call_vland('daemon_query',
- {'command':'daemon.shutdown',
- 'data': None})
- for field in shutdown:
- print '%s: %s' % (field, shutdown[field])
+
else:
print 'No recognised command given. Try -h for help'