summaryrefslogtreecommitdiffstats
path: root/conductor/conductor/controller/service.py
blob: e970b654f7ca42216a6fe38b6e757587ae3bf970 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#
# -------------------------------------------------------------------------
#   Copyright (c) 2015-2017 AT&T Intellectual Property
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# -------------------------------------------------------------------------
#

import cotyledon

from conductor.common import db_backend
from conductor.common.models import order_lock
from conductor.common.models import plan
from conductor.common.music import messaging as music_messaging
from conductor.common.music.model import base
from conductor.controller import rpc
from conductor.controller import translator_svc
from conductor import messaging
from conductor import service
from oslo_config import cfg
from oslo_log import log

LOG = log.getLogger(__name__)

CONF = cfg.CONF

CONTROLLER_OPTS = [
    cfg.IntOpt('timeout',
               default=10,
               min=1,
               help='Timeout for planning requests. '
                    'Default value is 10.'),
    cfg.IntOpt('limit',
               default=1,
               min=1,
               help='Maximum number of result sets to return. '
                    'Default value is 1.'),
    cfg.IntOpt('workers',
               default=1,
               min=1,
               help='Number of workers for controller service. '
                    'Default value is 1.'),
    cfg.BoolOpt('concurrent',
                default=False,
                help='Set to True when controller will run in active-active '
                     'mode. When set to False, controller will flush any '
                     'abandoned messages at startup. The controller always '
                     'restarts abandoned template translations at startup.'),
]

CONF.register_opts(CONTROLLER_OPTS, group='controller')

# Pull in service opts. We use them here.
OPTS = service.OPTS
CONF.register_opts(OPTS)


class ControllerServiceLauncher(object):
    """Launcher for the controller service."""
    def __init__(self, conf):
        self.conf = conf

        # Set up Music access.
        self.music = db_backend.get_client()
        self.music.keyspace_create(keyspace=conf.keyspace)

        # Dynamically create a plan class for the specified keyspace
        self.Plan = base.create_dynamic_model(
            keyspace=conf.keyspace, baseclass=plan.Plan, classname="Plan")
        self.OrderLock = base.create_dynamic_model(
            keyspace=conf.keyspace, baseclass=order_lock.OrderLock, classname="OrderLock")

        if not self.Plan:
            raise
        if not self.OrderLock:
            raise

        self.insert_healthcheck_plan()

    def run(self):
        transport = messaging.get_transport(self.conf)
        if transport:
            topic = "controller"
            target = music_messaging.Target(topic=topic)
            endpoints = [rpc.ControllerRPCEndpoint(self.conf, self.Plan), ]
            flush = not self.conf.controller.concurrent
            kwargs = {'transport': transport,
                      'target': target,
                      'endpoints': endpoints,
                      'flush': flush, }
            svcmgr = cotyledon.ServiceManager()
            svcmgr.add(music_messaging.RPCService,
                       workers=self.conf.controller.workers,
                       args=(self.conf,), kwargs=kwargs)

            kwargs = {'plan_class': self.Plan,
                      'order_locks': self.OrderLock}
            svcmgr.add(translator_svc.TranslatorService,
                       workers=self.conf.controller.workers,
                       args=(self.conf,), kwargs=kwargs)
            svcmgr.run()

    def insert_healthcheck_plan(self):
        healthcheck_plan = {
            "id": "healthcheck",
            "created": 1479482603641,
            "message": "",
            "name": "foo",
            "recommend_max": "1",
            "solution": "{\"healthcheck\": \" healthcheck\"}",
            "status": "solved",
            "template": "{\"healthcheck\": \"healthcheck\"}",
            "timeout": 3600,
            "translation": "{\"healthcheck\": \" healthcheck\"}",
            "updated": 1484324150629
        }
        self.music.row_create(self.conf.keyspace, "plans", "id", "healthcheck", healthcheck_plan)