Fix UnboundlocalError in translator.py
[optf/has.git] / conductor / conductor / controller / service.py
1 #
2 # -------------------------------------------------------------------------
3 #   Copyright (c) 2015-2017 AT&T Intellectual Property
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 #
17 # -------------------------------------------------------------------------
18 #
19
20 import cotyledon
21 from oslo_config import cfg
22 from oslo_log import log
23
24 from conductor.common.models import plan
25 from conductor.common.music import api
26 from conductor.common.music import messaging as music_messaging
27 from conductor.common.music.model import base
28 from conductor.controller import rpc
29 from conductor.controller import translator_svc
30 from conductor import messaging
31 from conductor import service
32
33 LOG = log.getLogger(__name__)
34
35 CONF = cfg.CONF
36
37 CONTROLLER_OPTS = [
38     cfg.IntOpt('timeout',
39                default=10,
40                min=1,
41                help='Timeout for planning requests. '
42                     'Default value is 10.'),
43     cfg.IntOpt('limit',
44                default=1,
45                min=1,
46                help='Maximum number of result sets to return. '
47                     'Default value is 1.'),
48     cfg.IntOpt('workers',
49                default=1,
50                min=1,
51                help='Number of workers for controller service. '
52                     'Default value is 1.'),
53     cfg.BoolOpt('concurrent',
54                 default=False,
55                 help='Set to True when controller will run in active-active '
56                      'mode. When set to False, controller will flush any '
57                      'abandoned messages at startup. The controller always '
58                      'restarts abandoned template translations at startup.'),
59     cfg.IntOpt('weight1',
60                default=1),
61     cfg.IntOpt('weight2',
62                default=1),
63 ]
64
65 CONF.register_opts(CONTROLLER_OPTS, group='controller')
66
67 # Pull in service opts. We use them here.
68 OPTS = service.OPTS
69 CONF.register_opts(OPTS)
70
71
72 class ControllerServiceLauncher(object):
73     """Launcher for the controller service."""
74
75     def __init__(self, conf):
76         self.conf = conf
77
78         # Set up Music access.
79         self.music = api.API()
80         self.music.keyspace_create(keyspace=conf.keyspace)
81
82         # Dynamically create a plan class for the specified keyspace
83         self.Plan = base.create_dynamic_model(
84             keyspace=conf.keyspace, baseclass=plan.Plan, classname="Plan")
85
86         if not self.Plan:
87             raise
88
89     def run(self):
90         transport = messaging.get_transport(self.conf)
91         if transport:
92             topic = "controller"
93             target = music_messaging.Target(topic=topic)
94             endpoints = [rpc.ControllerRPCEndpoint(self.conf, self.Plan), ]
95             flush = not self.conf.controller.concurrent
96             kwargs = {'transport': transport,
97                       'target': target,
98                       'endpoints': endpoints,
99                       'flush': flush, }
100             svcmgr = cotyledon.ServiceManager()
101             svcmgr.add(music_messaging.RPCService,
102                        workers=self.conf.controller.workers,
103                        args=(self.conf,), kwargs=kwargs)
104
105             kwargs = {'plan_class': self.Plan, }
106             svcmgr.add(translator_svc.TranslatorService,
107                        workers=self.conf.controller.workers,
108                        args=(self.conf,), kwargs=kwargs)
109             svcmgr.run()