diff --git a/bin/ryu-manager b/bin/ryu-manager index fca93291..d53906b1 100755 --- a/bin/ryu-manager +++ b/bin/ryu-manager @@ -20,7 +20,7 @@ import gevent from gevent import monkey monkey.patch_all() -import gflags +from openstack.common import cfg import logging import sys @@ -29,7 +29,6 @@ log.early_init_log(logging.DEBUG) from ryu import flags from ryu import version -from ryu import utils from ryu.app import wsgi from ryu.base.app_manager import AppManager from ryu.controller import controller @@ -41,31 +40,26 @@ from ryu.controller import controller import ryu.contrib -FLAGS = gflags.FLAGS -gflags.DEFINE_bool('version', False, 'output version information and exit') -gflags.DEFINE_multistring('app_lists', - [], - 'application module name to run') +CONF = cfg.CONF +CONF.register_cli_opts([ + cfg.ListOpt('app_lists', default=[], + help='application module name to run'), + cfg.MultiStrOpt('app', positional=True, default=[], + help='application module name to run') +]) def main(): - utils.find_flagfile() - args = FLAGS(sys.argv) - - if FLAGS.version: - print 'ryu-manager %s' % version - sys.exit(0) + CONF(project='ryu', version='ryu-manager %s' % version) log.init_log() # always enable ofp for now. - FLAGS.app_lists += ['ryu.controller.ofp_handler'] - - if len(args) > 1: - FLAGS.app_lists += args[1:] + CONF.app_lists += ['ryu.controller.ofp_handler'] + CONF.app_lists += CONF.app app_mgr = AppManager() - app_mgr.load_apps(FLAGS.app_lists) + app_mgr.load_apps(CONF.app_lists) contexts = app_mgr.create_contexts() app_mgr.instantiate_apps(**contexts) diff --git a/ryu/app/quantum_adapter.py b/ryu/app/quantum_adapter.py index 6f6090d6..8fc23396 100644 --- a/ryu/app/quantum_adapter.py +++ b/ryu/app/quantum_adapter.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import gflags +from openstack.common import cfg import logging from quantumclient import client as q_client @@ -41,17 +41,17 @@ monkey.patch_all() LOG = logging.getLogger(__name__) -FLAGS = gflags.FLAGS +CONF = cfg.CONF def _get_auth_token(): httpclient = q_client.HTTPClient( - username=FLAGS.quantum_admin_username, - tenant_name=FLAGS.quantum_admin_tenant_name, - password=FLAGS.quantum_admin_password, - auth_url=FLAGS.quantum_admin_auth_url, - timeout=FLAGS.quantum_url_timeout, - auth_strategy=FLAGS.quantum_auth_strategy) + username=CONF.quantum_admin_username, + tenant_name=CONF.quantum_admin_tenant_name, + password=CONF.quantum_admin_password, + auth_url=CONF.quantum_admin_auth_url, + timeout=CONF.quantum_url_timeout, + auth_strategy=CONF.quantum_auth_strategy) try: httpclient.authenticate() except (q_exc.Unauthorized, q_exc.Forbidden, q_exc.EndpointNotFound) as e: @@ -64,12 +64,12 @@ def _get_auth_token(): def _get_quantum_client(token): if token: my_client = q_clientv2.Client( - endpoint_url=FLAGS.quantum_url, - token=token, timeout=FLAGS.quantum_url_timeout) + endpoint_url=CONF.quantum_url, + token=token, timeout=CONF.quantum_url_timeout) else: my_client = q_clientv2.Client( - endpoint_url=FLAGS.quantum_url, - auth_strategy=None, timeout=FLAGS.quantum_url_timeout) + endpoint_url=CONF.quantum_url, + auth_strategy=None, timeout=CONF.quantum_url_timeout) return my_client @@ -129,7 +129,7 @@ class OVSSwitch(object): def __init__(self, dpid, nw, ifaces): # TODO: clean up token = None - if FLAGS.quantum_auth_strategy: + if CONF.quantum_auth_strategy: token = _get_auth_token() q_api = _get_quantum_client(token) @@ -137,7 +137,7 @@ class OVSSwitch(object): self.network_api = nw self.ifaces = ifaces self.q_api = q_api - self.ctrl_addr = FLAGS.quantum_controller_addr + self.ctrl_addr = CONF.quantum_controller_addr self.ovsdb_addr = None self.tunnel_ip = None diff --git a/ryu/app/tunnel_port_updater.py b/ryu/app/tunnel_port_updater.py index 56d9601b..915794d2 100644 --- a/ryu/app/tunnel_port_updater.py +++ b/ryu/app/tunnel_port_updater.py @@ -16,7 +16,7 @@ import collections import gevent -import gflags +from openstack.common import cfg import logging import netaddr @@ -33,8 +33,11 @@ from ryu.lib.ovs import bridge as ovs_bridge LOG = logging.getLogger(__name__) -FLAGS = gflags.FLAGS -gflags.DEFINE_string('tunnel_type', 'gre', 'tunnel type for ovs tunnel port') +CONF = cfg.CONF +CONF.register_opts([ + cfg.StrOpt('tunnel_type', default='gre', + help='tunnel type for ovs tunnel port') +]) _TUNNEL_TYPE_TO_NW_ID = { 'gre': rest_nw_id.NW_ID_VPORT_GRE, @@ -351,7 +354,7 @@ class TunnelPortUpdater(app_manager.RyuApp): def __init__(self, *args, **kwargs): super(TunnelPortUpdater, self).__init__(args, kwargs) - self.tunnel_type = FLAGS.tunnel_type + self.tunnel_type = CONF.tunnel_type self.cs = kwargs['conf_switch'] self.nw = kwargs['network'] self.tunnels = kwargs['tunnels'] diff --git a/ryu/app/wsgi.py b/ryu/app/wsgi.py index 38809922..138563b8 100644 --- a/ryu/app/wsgi.py +++ b/ryu/app/wsgi.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import gflags +from openstack.common import cfg import logging import webob.dec @@ -24,10 +24,11 @@ from routes.util import URLGenerator LOG = logging.getLogger('ryu.app.wsgi') -FLAGS = gflags.FLAGS -gflags.DEFINE_string('wsapi_host', '', 'webapp listen host') -gflags.DEFINE_integer('wsapi_port', 8080, 'webapp listen port') - +CONF = cfg.CONF +CONF.register_cli_opts([ + cfg.StrOpt('wsapi_host', default='', help='webapp listen host'), + cfg.IntOpt('wsapi_port', default=8080, help='webapp listen port') +]) HEX_PATTERN = r'0x[0-9a-z]+' DIGIT_PATTERN = r'[1-9][0-9]*' @@ -87,7 +88,7 @@ class WSGIApplication(object): class WSGIServer(pywsgi.WSGIServer): def __init__(self, application, **config): - super(WSGIServer, self).__init__((FLAGS.wsapi_host, FLAGS.wsapi_port), + super(WSGIServer, self).__init__((CONF.wsapi_host, CONF.wsapi_port), application, **config) def __call__(self): diff --git a/ryu/controller/controller.py b/ryu/controller/controller.py index 32d89dd5..9dc745ad 100644 --- a/ryu/controller/controller.py +++ b/ryu/controller/controller.py @@ -15,7 +15,7 @@ # limitations under the License. import contextlib -import gflags +from openstack.common import cfg import logging import gevent import traceback @@ -42,15 +42,17 @@ from ryu.controller import ofp_event LOG = logging.getLogger('ryu.controller.controller') -FLAGS = gflags.FLAGS -gflags.DEFINE_string('ofp_listen_host', '', 'openflow listen host') -gflags.DEFINE_integer('ofp_tcp_listen_port', ofproto_common.OFP_TCP_PORT, - 'openflow tcp listen port') -gflags.DEFINE_integer('ofp_ssl_listen_port', ofproto_common.OFP_SSL_PORT, - 'openflow ssl listen port') -gflags.DEFINE_string('ctl_privkey', None, 'controller private key') -gflags.DEFINE_string('ctl_cert', None, 'controller certificate') -gflags.DEFINE_string('ca_certs', None, 'CA certificates') +CONF = cfg.CONF +CONF.register_cli_opts([ + cfg.StrOpt('ofp_listen_host', default='', help='openflow listen host'), + cfg.IntOpt('ofp_tcp_listen_port', default=ofproto_common.OFP_TCP_PORT, + help='openflow tcp listen port'), + cfg.IntOpt('ofp_ssl_listen_port', default=ofproto_common.OFP_SSL_PORT, + help='openflow ssl listen port'), + cfg.StrOpt('ctl_privkey', default=None, help='controller private key'), + cfg.StrOpt('ctl_cert', default=None, help='controller certificate'), + cfg.StrOpt('ca_certs', default=None, help='CA certificates') +]) class OpenFlowController(object): @@ -63,26 +65,26 @@ class OpenFlowController(object): self.server_loop() def server_loop(self): - if FLAGS.ctl_privkey and FLAGS.ctl_cert is not None: - if FLAGS.ca_certs is not None: - server = StreamServer((FLAGS.ofp_listen_host, - FLAGS.ofp_ssl_listen_port), + if CONF.ctl_privkey and CONF.ctl_cert is not None: + if CONF.ca_certs is not None: + server = StreamServer((CONF.ofp_listen_host, + CONF.ofp_ssl_listen_port), datapath_connection_factory, - keyfile=FLAGS.ctl_privkey, - certfile=FLAGS.ctl_cert, + keyfile=CONF.ctl_privkey, + certfile=CONF.ctl_cert, cert_reqs=ssl.CERT_REQUIRED, - ca_certs=FLAGS.ca_certs, + ca_certs=CONF.ca_certs, ssl_version=ssl.PROTOCOL_TLSv1) else: - server = StreamServer((FLAGS.ofp_listen_host, - FLAGS.ofp_ssl_listen_port), + server = StreamServer((CONF.ofp_listen_host, + CONF.ofp_ssl_listen_port), datapath_connection_factory, - keyfile=FLAGS.ctl_privkey, - certfile=FLAGS.ctl_cert, + keyfile=CONF.ctl_privkey, + certfile=CONF.ctl_cert, ssl_version=ssl.PROTOCOL_TLSv1) else: - server = StreamServer((FLAGS.ofp_listen_host, - FLAGS.ofp_tcp_listen_port), + server = StreamServer((CONF.ofp_listen_host, + CONF.ofp_tcp_listen_port), datapath_connection_factory) #LOG.debug('loop') diff --git a/ryu/flags.py b/ryu/flags.py index 610b201f..a875d176 100644 --- a/ryu/flags.py +++ b/ryu/flags.py @@ -17,31 +17,30 @@ global flags """ -import gflags +from openstack.common import cfg -FLAGS = gflags.FLAGS +CONF = cfg.CONF -# GLOBAL flags -gflags.DEFINE_boolean('monkey_patch', False, 'do monkey patch') - -# app/quantum_adapter -gflags.DEFINE_string('quantum_url', 'http://localhost:9696', - 'URL for connecting to quantum') -gflags.DEFINE_integer('quantum_url_timeout', 30, - 'timeout value for connecting to quantum in seconds') -gflags.DEFINE_string('quantum_admin_username', 'quantum', - 'username for connecting to quantum in admin context') -gflags.DEFINE_string('quantum_admin_password', 'service_password', - 'password for connecting to quantum in admin context') -gflags.DEFINE_string('quantum_admin_tenant_name', 'service', - 'tenant name for connecting to quantum in admin context') -gflags.DEFINE_string('quantum_admin_auth_url', 'http://localhost:5000/v2.0', - 'auth url for connecting to quantum in admin context') -gflags.DEFINE_string( - 'quantum_auth_strategy', - 'keystone', - 'auth strategy for connecting to quantum in admin context') - -gflags.DEFINE_string('quantum_controller_addr', None, - 'openflow mehod:address:port to set controller of' - 'ovs bridge') +CONF.register_cli_opts([ + # GLOBAL flags + cfg.BoolOpt('monkey_patch', default=False, help='do monkey patch'), + # app/quantum_adapter + cfg.StrOpt('quantum_url', default='http://localhost:9696', + help='URL for connecting to quantum'), + cfg.IntOpt('quantum_url_timeout', default=30, + help='timeout value for connecting to quantum in seconds'), + cfg.StrOpt('quantum_admin_username', default='quantum', + help='username for connecting to quantum in admin context'), + cfg.StrOpt('quantum_admin_password', default='service_password', + help='password for connecting to quantum in admin context'), + cfg.StrOpt('quantum_admin_tenant_name', default='service', + help='tenant name for connecting to quantum in admin context'), + cfg.StrOpt('quantum_admin_auth_url', default='http://localhost:5000/v2.0', + help='auth url for connecting to quantum in admin context'), + cfg.StrOpt('quantum_auth_strategy', default='keystone', + help='auth strategy for connecting to quantum in admin' + 'context'), + cfg.StrOpt('quantum_controller_addr', default=None, + help='openflow mehod:address:port to set controller of' + 'ovs bridge') +]) diff --git a/ryu/lib/ovs/bridge.py b/ryu/lib/ovs/bridge.py index 1fbaa6d5..b68b50b3 100644 --- a/ryu/lib/ovs/bridge.py +++ b/ryu/lib/ovs/bridge.py @@ -19,7 +19,7 @@ slimmed down version of OVSBridge in quantum agent """ import functools -import gflags +from openstack.common import cfg import logging import ryu.exception as ryu_exc @@ -28,8 +28,10 @@ import ryu.lib.ovs.vsctl as ovs_vsctl LOG = logging.getLogger(__name__) -FLAGS = gflags.FLAGS -gflags.DEFINE_integer('ovsdb_timeout', 2, 'ovsdb timeout') +CONF = cfg.CONF +CONF.register_opts([ + cfg.IntOpt('ovsdb_timeout', default=2, help='ovsdb timeout') +]) class OVSBridgeNotFound(ryu_exc.RyuException): @@ -90,7 +92,7 @@ class OVSBridge(object): super(OVSBridge, self).__init__() self.datapath_id = datapath_id self.vsctl = ovs_vsctl.VSCtl(ovsdb_addr) - self.timeout = timeout or FLAGS.ovsdb_timeout + self.timeout = timeout or CONF.ovsdb_timeout self.exception = exception self.br_name = None diff --git a/ryu/lib/ovs/db_client.py b/ryu/lib/ovs/db_client.py index ecda4cf9..09a7948b 100644 --- a/ryu/lib/ovs/db_client.py +++ b/ryu/lib/ovs/db_client.py @@ -14,7 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import gflags import logging import os @@ -23,7 +22,6 @@ from ovs import (jsonrpc, from ovs import util as ovs_util from ovs.db import schema -FLAGS = gflags.FLAGS LOG = logging.getLogger(__name__) diff --git a/ryu/log.py b/ryu/log.py index ef1b1cbd..0efaeff0 100644 --- a/ryu/log.py +++ b/ryu/log.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import gflags +from openstack.common import cfg import inspect import logging import logging.handlers @@ -22,16 +22,18 @@ import os import sys -FLAGS = gflags.FLAGS +CONF = cfg.CONF -gflags.DEFINE_integer('default_log_level', None, 'default log level') -gflags.DEFINE_bool('verbose', False, 'show debug output') - -gflags.DEFINE_bool('use_stderr', True, 'log to standard error') -gflags.DEFINE_string('use_syslog', False, 'output to syslog') -gflags.DEFINE_string('log_dir', None, 'log file directory') -gflags.DEFINE_string('log_file', None, 'log file name') -gflags.DEFINE_string('log_file_mode', '0644', 'default log file permission') +CONF.register_cli_opts([ + cfg.IntOpt('default_log_level', default=None, help='default log level'), + cfg.BoolOpt('verbose', default=False, help='show debug output'), + cfg.BoolOpt('use_stderr', default=True, help='log to standard error'), + cfg.StrOpt('use_syslog', default=False, help='output to syslog'), + cfg.StrOpt('log_dir', default=None, help='log file directory'), + cfg.StrOpt('log_file', default=None, help='log file name'), + cfg.StrOpt('log_file_mode', default='0644', + help='default log file permission') +]) _EARLY_LOG_HANDLER = None @@ -48,10 +50,10 @@ def early_init_log(level=None): def _get_log_file(): - if FLAGS.log_file: - return FLAGS.log_file - if FLAGS.log_dir: - return os.path.join(FLAGS.log_dir, + if CONF.log_file: + return CONF.log_file + if CONF.log_dir: + return os.path.join(CONF.log_dir, os.path.basename(inspect.stack()[-1][1])) + '.log' return None @@ -60,25 +62,25 @@ def init_log(): global _EARLY_LOG_HANDLER log = logging.getLogger() - if FLAGS.use_stderr: + if CONF.use_stderr: log.addHandler(logging.StreamHandler(sys.stderr)) if _EARLY_LOG_HANDLER is not None: log.removeHandler(_EARLY_LOG_HANDLER) _EARLY_LOG_HANDLER = None - if FLAGS.use_syslog: + if CONF.use_syslog: syslog = logging.handlers.SysLogHandler(address='/dev/log') log.addHandler(syslog) log_file = _get_log_file() if log_file is not None: log.addHandler(logging.handlers.WatchedFileHandler(log_file)) - mode = int(FLAGS.log_file_mode, 8) + mode = int(CONF.log_file_mode, 8) os.chmod(log_file, mode) - if FLAGS.verbose: + if CONF.verbose: log.setLevel(logging.DEBUG) - elif FLAGS.default_log_level is not None: - log.setLevel(FLAGS.default_log_level) + elif CONF.default_log_level is not None: + log.setLevel(CONF.default_log_level) else: log.setLevel(logging.INFO) diff --git a/ryu/utils.py b/ryu/utils.py index 18601ec1..d5b44793 100644 --- a/ryu/utils.py +++ b/ryu/utils.py @@ -37,33 +37,6 @@ def import_module(modname): return sys.modules[modname] -RYU_DEFAULT_FLAG_FILE = ('ryu.conf', 'etc/ryu/ryu.conf', '/etc/ryu/ryu.conf') - - -def find_flagfile(default_path=RYU_DEFAULT_FLAG_FILE): - if '--flagfile' in sys.argv: - return - - script_dir = os.path.dirname(inspect.stack()[-1][1]) - - for filename in default_path: - if not os.path.isabs(filename): - if os.path.exists(filename): - # try relative to current path - filename = os.path.abspath(filename) - elif os.path.exists(os.path.join(script_dir, filename)): - # try relative to script dir - filename = os.path.join(script_dir, filename) - - if not os.path.exists(filename): - continue - - flagfile = '--flagfile=%s' % filename - sys.argv.insert(1, flagfile) - LOG.debug('flagfile = %s', filename) - return - - def round_up(x, y): return ((x + y - 1) / y) * y