3 # Copyright (C) 2011, 2012 Nippon Telegraph and Telephone Corporation.
4 # Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co jp>
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
22 from ryu.lib import hub
23 hub.patch(thread=False)
29 log.early_init_log(logging.DEBUG)
32 from ryu import version
33 from ryu.app import wsgi
34 from ryu.base.app_manager import AppManager
35 from ryu.controller import controller
36 from ryu.topology import switches
40 CONF.register_cli_opts([
41 cfg.ListOpt('app-lists', default=[],
42 help='application module name to run'),
43 cfg.MultiStrOpt('app', positional=True, default=[],
44 help='application module name to run'),
45 cfg.StrOpt('pid-file', default=None, help='pid file name'),
46 cfg.BoolOpt('enable-debugger', default=False,
47 help='don\'t overwrite Python standard threading library'
48 '(use only for debugging)'),
49 cfg.StrOpt('user-flags', default=None,
50 help='Additional flags file for user applications'),
54 def _parse_user_flags():
56 Parses user-flags file and loads it to register user defined options.
59 idx = list(sys.argv).index('--user-flags')
60 user_flags_file = sys.argv[idx + 1]
61 except (ValueError, IndexError):
64 if user_flags_file and os.path.isfile(user_flags_file):
65 from ryu.utils import _import_module_file
66 _import_module_file(user_flags_file)
69 def main(args=None, prog=None):
72 CONF(args=args, prog=prog,
73 project='ryu', version='ryu-manager %s' % version,
74 default_config_files=['/usr/local/etc/ryu/ryu.conf'])
75 except cfg.ConfigFilesNotFoundError:
76 CONF(args=args, prog=prog,
77 project='ryu', version='ryu-manager %s' % version)
80 logger = logging.getLogger(__name__)
82 if CONF.enable_debugger:
83 msg = 'debugging is available (--enable-debugger option is turned on)'
86 hub.patch(thread=True)
89 with open(CONF.pid_file, 'w') as pid_file:
90 pid_file.write(str(os.getpid()))
92 app_lists = CONF.app_lists + CONF.app
93 # keep old behavior, run ofp if no application is specified.
95 app_lists = ['ryu.controller.ofp_handler']
97 app_mgr = AppManager.get_instance()
98 app_mgr.load_apps(app_lists)
99 contexts = app_mgr.create_contexts()
101 services.extend(app_mgr.instantiate_apps(**contexts))
103 webapp = wsgi.start_service(app_mgr)
105 thr = hub.spawn(webapp)
109 hub.joinall(services)
110 except KeyboardInterrupt:
111 logger.debug("Keyboard Interrupt received. "
112 "Closing RYU application manager...")
117 if __name__ == "__main__":