Ubuntu Pastebin

Paste from Cory at Thu, 10 Dec 2015 22:48:43 +0000

Download as text
 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
#!/usr/bin/python
# 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.

from charms.reactive import RelationBase
from charms.reactive import hook
from charms.reactive import scopes
from charms.reactive.helpers import data_changed


class MySQLClient(RelationBase):
    # We only expect a single mysql server to be related.  Additionally, if
    # there are multiple units, it would be for replication purposes only,
    # so we would expect a leader to provide our connection info, or at least
    # for all mysql units to agree on the connection info.  Thus, we use a
    # global conversation scope in which all services and units share the
    # same conversation.
    scope = scopes.GLOBAL

    # These remote data fields will be automatically mapped to accessors
    # with a basic documentation string provided.
    auto_accessors = ['host', 'database', 'user', 'password']

    def port(self):
        """
        Get the port.

        If not available, returns the default port of 3306.
        """
        return self.get_remote('port', 3306)

    @hook('{requires:mysql}-relation-{joined,changed}')
    def changed(self):
        conv = self.conversation()
        conv.set_state('{relation_name}.connected')
        if self.connection_string():
            conv.set_state('{relation_name}.available')
            if data_changed(conv.key, self.connection_string()):
                conv.set_state('{relation_name}.changed')

    def acknowledge_change(self):
        conv = self.conversation()
        conv.remove_state('{relation_name}.changed')

    @hook('{requires:mysql}-relation-{broken,departed}')
    def departed(self):
        self.remove_state('{relation_name}.connected')
        self.remove_state('{relation_name}.available')

    def connection_string(self):
        """
        Get the connection string, if available, or None.
        """
        data = {
            'host': self.host(),
            'port': self.port(),
            'database': self.database(),
            'user': self.user(),
            'password': self.password(),
        }
        if all(data.values()):
            return str.format(
                'host={host} port={port} dbname={database} user={user} password={password}',
                **data)
        return None
Download as text