- try:
- try:
- shortops = "h"
- longops = [ "output-directory=",
- "pif=", "pif-uuid=",
- "session=",
- "force=",
- "force-interface=",
- "management",
- "test-mode",
- "device=", "mode=", "ip=", "netmask=", "gateway=",
- "help" ]
- arglist, args = getopt.gnu_getopt(argv[1:], shortops, longops)
- except getopt.GetoptError, msg:
- raise Usage(msg)
-
- force_rewrite_config = {}
-
- for o,a in arglist:
- if o == "--output-directory":
- output_directory = a
- elif o == "--pif":
- pif = a
- elif o == "--pif-uuid":
- pif_uuid = a
- elif o == "--session":
- session = a
- elif o == "--force-interface" or o == "--force":
- force_interface = a
- elif o == "--management":
- force_management = True
- elif o in ["--device", "--mode", "--ip", "--netmask", "--gateway"]:
- force_rewrite_config[o[2:]] = a
- elif o == "-h" or o == "--help":
- print __doc__ % {'command-name': os.path.basename(argv[0])}
- return 0
-
- if not debug_mode():
- syslog.openlog(os.path.basename(argv[0]))
- log("Called as " + str.join(" ", argv))
- if len(args) < 1:
- raise Usage("Required option <action> not present")
- if len(args) > 1:
- raise Usage("Too many arguments")
-
- action = args[0]
- # backwards compatibility
- if action == "rewrite-configuration": action = "rewrite"
-
- if output_directory and ( session or pif ):
- raise Usage("--session/--pif cannot be used with --output-directory")
- if ( session or pif ) and pif_uuid:
- raise Usage("--session/--pif and --pif-uuid are mutually exclusive.")
- if ( session and not pif ) or ( not session and pif ):
- raise Usage("--session and --pif must be used together.")
- if force_interface and ( session or pif or pif_uuid ):
- raise Usage("--force is mutually exclusive with --session, --pif and --pif-uuid")
- if len(force_rewrite_config) and not (force_interface and action == "rewrite"):
- raise Usage("\"--force rewrite\" needed for --device, --mode, --ip, --netmask, and --gateway")
-
- global db
- if force_interface:
- log("Force interface %s %s" % (force_interface, action))
-
- if action == "rewrite":
- action_force_rewrite(force_interface, force_rewrite_config)
- else:
- db = DatabaseCache(cache_file=dbcache_file)
- host = db.extras['host']
- pif = db.get_pif_by_bridge(host, force_interface)
- management_pif = db.get_management_pif(host)
-
- if action == "up":
- action_up(pif)
- elif action == "down":
- action_down(pif)
- else:
- raise Usage("Unknown action %s" % action)
- else:
- db = DatabaseCache(session_ref=session)
-
- if pif_uuid:
- pif = db.get_pif_by_uuid(pif_uuid)
-
- if not pif:
- raise Usage("No PIF given")
-
- if force_management:
- # pif is going to be the management pif
- management_pif = pif
- else:
- # pif is not going to be the management pif.
- # Search DB cache for pif on same host with management=true
- pifrec = db.get_pif_record(pif)
- host = pifrec['host']
- management_pif = db.get_management_pif(host)
-
- log_pif_action(action, pif)
-
- if not check_allowed(pif):
- return 0
-
- if action == "up":
- action_up(pif)
- elif action == "down":
- action_down(pif)
- elif action == "rewrite":
- action_rewrite(pif)
- else:
- raise Usage("Unknown action %s" % action)
-
- # Save cache.
- pifrec = db.get_pif_record(pif)
- db.save(dbcache_file, {'host': pifrec['host']})
-
- except Usage, err:
- print >>sys.stderr, err.msg
- print >>sys.stderr, "For help use --help."
- return 2
- except Error, err:
- log(err.msg)
- return 1
-
- return 0
-\f
-# The following code allows interface-reconfigure to keep Centos
-# network configuration files up-to-date, even though the vswitch
-# never uses them. In turn, that means that "rpm -e vswitch" does not
-# have to update any configuration files.
-
-def configure_ethtool(oc, f):
- # Options for "ethtool -s"
- settings = None
- setting_opts = ["autoneg", "speed", "duplex"]
- # Options for "ethtool -K"
- offload = None
- offload_opts = ["rx", "tx", "sg", "tso", "ufo", "gso"]
-
- for opt in [opt for opt in setting_opts + offload_opts if oc.has_key("ethtool-" + opt)]:
- val = oc["ethtool-" + opt]
-
- if opt in ["speed"]:
- if val in ["10", "100", "1000"]:
- val = "speed " + val
- else:
- log("Invalid value for ethtool-speed = %s. Must be 10|100|1000." % val)
- val = None
- elif opt in ["duplex"]:
- if val in ["half", "full"]:
- val = "duplex " + val
- else:
- log("Invalid value for ethtool-duplex = %s. Must be half|full." % val)
- val = None
- elif opt in ["autoneg"] + offload_opts:
- if val in ["true", "on"]:
- val = opt + " on"
- elif val in ["false", "off"]:
- val = opt + " off"
- else:
- log("Invalid value for ethtool-%s = %s. Must be on|true|off|false." % (opt, val))
- val = None
-
- if opt in setting_opts:
- if val and settings:
- settings = settings + " " + val
- else:
- settings = val
- elif opt in offload_opts:
- if val and offload:
- offload = offload + " " + val
- else:
- offload = val
-
- if settings:
- f.write("ETHTOOL_OPTS=\"%s\"\n" % settings)
- if offload:
- f.write("ETHTOOL_OFFLOAD_OPTS=\"%s\"\n" % offload)
-
-def configure_mtu(oc, f):
- if not oc.has_key('mtu'):
- return
-
- try:
- mtu = int(oc['mtu'])
- f.write("MTU=%d\n" % mtu)
- except ValueError, x:
- log("Invalid value for mtu = %s" % mtu)
-
-def configure_static_routes(interface, oc, f):
- """Open a route-<interface> file for static routes.
-
- Opens the static routes configuration file for interface and writes one
- line for each route specified in the network's other config "static-routes" value.
- E.g. if
- interface ( RO): xenbr1
- other-config (MRW): static-routes: 172.16.0.0/15/192.168.0.3,172.18.0.0/16/192.168.0.4;...
-
- Then route-xenbr1 should be
- 172.16.0.0/15 via 192.168.0.3 dev xenbr1
- 172.18.0.0/16 via 192.168.0.4 dev xenbr1
- """
- fname = "route-%s" % interface
- if oc.has_key('static-routes'):
- # The key is present - extract comma seperates entries
- lines = oc['static-routes'].split(',')
- else:
- # The key is not present, i.e. there are no static routes
- lines = []
-
- child = ConfigurationFile(fname)
- child.write("# DO NOT EDIT: This file (%s) was autogenerated by %s\n" % \
- (os.path.basename(child.path()), os.path.basename(sys.argv[0])))
-
- try:
- for l in lines:
- network, masklen, gateway = l.split('/')
- child.write("%s/%s via %s dev %s\n" % (network, masklen, gateway, interface))
-
- f.attach_child(child)
- child.close()
-
- except ValueError, e:
- log("Error in other-config['static-routes'] format for network %s: %s" % (interface, e))
-
-def __open_ifcfg(interface):
- """Open a network interface configuration file.
-
- Opens the configuration file for interface, writes a header and
- common options and returns the file object.