summaryrefslogtreecommitdiff
path: root/zuul/lib/connections.py
blob: c4d458158429a097bcb17f2dd287b189186e80f2 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# Copyright 2015 Rackspace Australia
#
# 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 logging
import re
from collections import OrderedDict
from urllib.parse import urlparse

from zuul import model
from zuul.driver.sql.sqlconnection import SQLConnection
from zuul.driver.sql.sqlreporter import SQLReporter
import zuul.driver.zuul
import zuul.driver.gerrit
import zuul.driver.git
import zuul.driver.github
import zuul.driver.smtp
import zuul.driver.timer
import zuul.driver.sql
import zuul.driver.bubblewrap
import zuul.driver.nullwrap
import zuul.driver.mqtt
import zuul.driver.pagure
import zuul.driver.gitlab
import zuul.driver.elasticsearch
from zuul.connection import BaseConnection
from zuul.driver import SourceInterface


class DefaultConnection(BaseConnection):
    pass


class ConnectionRegistry(object):
    """A registry of connections"""

    log = logging.getLogger("zuul.ConnectionRegistry")

    def __init__(self, check_bwrap=False):
        self.connections = OrderedDict()
        self.drivers = {}

        self.registerDriver(zuul.driver.zuul.ZuulDriver())
        self.registerDriver(zuul.driver.gerrit.GerritDriver())
        self.registerDriver(zuul.driver.git.GitDriver())
        self.registerDriver(zuul.driver.github.GithubDriver())
        self.registerDriver(zuul.driver.smtp.SMTPDriver())
        self.registerDriver(zuul.driver.timer.TimerDriver())
        self.registerDriver(zuul.driver.sql.SQLDriver())
        self.registerDriver(
            zuul.driver.bubblewrap.BubblewrapDriver(check_bwrap))
        self.registerDriver(zuul.driver.nullwrap.NullwrapDriver())
        self.registerDriver(zuul.driver.mqtt.MQTTDriver())
        self.registerDriver(zuul.driver.pagure.PagureDriver())
        self.registerDriver(zuul.driver.gitlab.GitlabDriver())
        self.registerDriver(zuul.driver.elasticsearch.ElasticsearchDriver())

    def registerDriver(self, driver):
        if driver.name in self.drivers:
            raise Exception("Driver %s already registered" % driver.name)
        self.drivers[driver.name] = driver

    def registerScheduler(self, sched):
        for driver_name, driver in self.drivers.items():
            driver.registerScheduler(sched)
        for connection_name, connection in self.connections.items():
            connection.registerScheduler(sched)

    def load(self, zk_client, component_registry):
        for connection in self.connections.values():
            connection.onLoad(zk_client, component_registry)

    def reconfigureDrivers(self, tenant):
        for driver in self.drivers.values():
            if hasattr(driver, 'reconfigure'):
                driver.reconfigure(tenant)

    def stop(self):
        for connection_name, connection in self.connections.items():
            connection.onStop()
        for driver in self.drivers.values():
            driver.stop()

    def configure(self, config, source_only=False, require_sql=False):
        # Register connections from the config
        connections = OrderedDict()

        if 'database' in config.sections() and not source_only:
            driver = self.drivers['sql']
            con_config = dict(config.items('database'))

            connection = driver.getConnection('database', con_config)
            connections['database'] = connection

        for section_name in config.sections():
            con_match = re.match(r'^connection ([\'\"]?)(.*)(\1)$',
                                 section_name, re.I)
            if not con_match:
                continue
            con_name = con_match.group(2)
            con_config = dict(config.items(section_name))

            if 'driver' not in con_config:
                raise Exception("No driver specified for connection %s."
                                % con_name)

            con_driver = con_config['driver']
            if (con_driver not in self.drivers) or con_driver == 'sql':
                raise Exception("Unknown driver, %s, for connection %s"
                                % (con_config['driver'], con_name))

            driver = self.drivers[con_driver]

            # The merger and the reporter only needs source driver.
            # This makes sure Reporter like the SQLDriver are only created by
            # the scheduler process
            if source_only and not isinstance(driver, SourceInterface):
                continue

            connection = driver.getConnection(con_name, con_config)
            connections[con_name] = connection

        # If the [gerrit] or [smtp] sections still exist, load them in as a
        # connection named 'gerrit' or 'smtp' respectfully

        if 'gerrit' in config.sections():
            if 'gerrit' in connections:
                self.log.warning(
                    "The legacy [gerrit] section will be ignored in favour"
                    " of the [connection gerrit].")
            else:
                driver = self.drivers['gerrit']
                connections['gerrit'] = \
                    driver.getConnection(
                        'gerrit', dict(config.items('gerrit')))

        if 'smtp' in config.sections():
            if 'smtp' in connections:
                self.log.warning(
                    "The legacy [smtp] section will be ignored in favour"
                    " of the [connection smtp].")
            else:
                driver = self.drivers['smtp']
                connections['smtp'] = \
                    driver.getConnection(
                        'smtp', dict(config.items('smtp')))

        # Create default connections for drivers which need no
        # connection information (e.g., 'timer' or 'zuul').
        if not source_only:
            for driver in self.drivers.values():
                if not hasattr(driver, 'getConnection'):
                    connections[driver.name] = DefaultConnection(
                        driver, driver.name, {})

        if require_sql:
            if 'database' not in connections:
                raise Exception("Database configuration is required")

        self.connections = connections

    def getSqlConnection(self) -> SQLConnection:
        """
        Gets the SQL connection. This is either the connection
        described in the [database] section, or the first configured
        connection.

        :return: The SQL connection.

        """
        connection = self.connections.get('database')
        if not connection:
            raise Exception("No SQL connections")
        return connection

    def getSqlReporter(self, pipeline: model.Pipeline) -> SQLReporter:
        """
        Gets the SQL reporter. Such reporter is based on
        `getSqlConnection`.

        :param pipeline: Pipeline
        :return: The SQL reporter

        """
        connection = self.getSqlConnection()
        return connection.driver.getReporter(connection, pipeline)

    def getSource(self, connection_name):
        connection = self.connections[connection_name]
        return connection.driver.getSource(connection)

    def getSources(self):
        sources = []
        for connection in self.connections.values():
            if hasattr(connection.driver, 'getSource'):
                sources.append(connection.driver.getSource(connection))
        return sources

    def getReporter(self, connection_name, pipeline, config=None):
        connection = self.connections[connection_name]
        return connection.driver.getReporter(connection, pipeline, config)

    def getTrigger(self, connection_name, config=None):
        connection = self.connections[connection_name]
        return connection.driver.getTrigger(connection, config)

    def getTriggerEventClass(self, driver_name: str):
        driver = self.drivers[driver_name]
        return driver.getTriggerEventClass()

    def getSourceByHostname(self, hostname):
        for connection in self.connections.values():
            if hasattr(connection, 'canonical_hostname'):
                if connection.canonical_hostname == hostname:
                    return self.getSource(connection.connection_name)
            if hasattr(connection, 'server'):
                if connection.server == hostname:
                    return self.getSource(connection.connection_name)
            if hasattr(connection, 'baseurl'):
                if urlparse(connection.baseurl).hostname == hostname:
                    return self.getSource(connection.connection_name)
        return None

    def getSourceByCanonicalHostname(self, canonical_hostname):
        for connection in self.connections.values():
            if hasattr(connection, 'canonical_hostname'):
                if connection.canonical_hostname == canonical_hostname:
                    return self.getSource(connection.connection_name)
        return None