backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / cmd / manager.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2011, 2012 Nippon Telegraph and Telephone Corporation.
4 # Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co jp>
5 #
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
9 #
10 #    http://www.apache.org/licenses/LICENSE-2.0
11 #
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
15 # implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18
19 import os
20 import sys
21
22 from ryu.lib import hub
23 hub.patch(thread=False)
24
25 from ryu import cfg
26
27 import logging
28 from ryu import log
29 log.early_init_log(logging.DEBUG)
30
31 from ryu import flags
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
37
38
39 CONF = cfg.CONF
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'),
51 ])
52
53
54 def _parse_user_flags():
55     """
56     Parses user-flags file and loads it to register user defined options.
57     """
58     try:
59         idx = list(sys.argv).index('--user-flags')
60         user_flags_file = sys.argv[idx + 1]
61     except (ValueError, IndexError):
62         user_flags_file = ''
63
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)
67
68
69 def main(args=None, prog=None):
70     _parse_user_flags()
71     try:
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)
78
79     log.init_log()
80     logger = logging.getLogger(__name__)
81
82     if CONF.enable_debugger:
83         msg = 'debugging is available (--enable-debugger option is turned on)'
84         logger.info(msg)
85     else:
86         hub.patch(thread=True)
87
88     if CONF.pid_file:
89         with open(CONF.pid_file, 'w') as pid_file:
90             pid_file.write(str(os.getpid()))
91
92     app_lists = CONF.app_lists + CONF.app
93     # keep old behavior, run ofp if no application is specified.
94     if not app_lists:
95         app_lists = ['ryu.controller.ofp_handler']
96
97     app_mgr = AppManager.get_instance()
98     app_mgr.load_apps(app_lists)
99     contexts = app_mgr.create_contexts()
100     services = []
101     services.extend(app_mgr.instantiate_apps(**contexts))
102
103     webapp = wsgi.start_service(app_mgr)
104     if webapp:
105         thr = hub.spawn(webapp)
106         services.append(thr)
107
108     try:
109         hub.joinall(services)
110     except KeyboardInterrupt:
111         logger.debug("Keyboard Interrupt received. "
112                      "Closing RYU application manager...")
113     finally:
114         app_mgr.close()
115
116
117 if __name__ == "__main__":
118     main()