File: wbfabric_grt.py

package info (click to toggle)
mysql-workbench 6.2.3%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 102,612 kB
  • ctags: 84,593
  • sloc: ansic: 804,682; cpp: 438,759; yacc: 59,129; python: 54,293; xml: 48,851; sql: 5,512; objc: 1,414; makefile: 505; sh: 455; java: 237; ruby: 6; perl: 5; php: 1
file content (298 lines) | stat: -rw-r--r-- 11,249 bytes parent folder | download
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 of the
# License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301  USA

from wb import DefineModule
import grt
from grt import log_warning, log_info
import mforms
import traceback
import time
import os
import ConfigParser
import tempfile
from workbench.os_utils import OSUtils
import StringIO
import json

from wb_common import OperationCancelledError

fabric_unavailable_message = ""
fabric_client = ""

ModuleInfo = DefineModule(name= "WBFabric", author= "Oracle Corp.", version="1.0")


def execute(command):
    output = StringIO.StringIO()

    ret_code = OSUtils.exec_command(command, output.write)

    out = output.getvalue().strip()

    return ret_code, out


def find_fabric_client():
    if os.name == 'nt':
        command = 'where mysqlfabric'
    else:
        command = 'which mysqlfabric'

    ret, out = execute(command)

    if ret:
        global fabric_unavailable_message
        fabric_unavailable_message = "MySQL Fabric support not available.\nPlease make sure MySQL Utilities version 1.4.3 or higher is installed to use this feature.\nSee log file for more details."
        log_warning("WBFabric Module", "Unable to locate the mysqlfabric utility, the fabric functions will not be available. \n"
                                       "Please make sure MySQL Utilities version 1.4.3 or higher is installed. \n"
                                       "The mysqlfabric utility path should be added into the PATH environment variable: %s\n" % out)
    else:
        global fabric_client
        fabric_client = out
        log_info("WBFabric Module", "MySQL Fabric Client found at: %s\n" % fabric_client)


def create_config(conn):
    # Retrieves the fabric node connection data
    host = conn.parameterValues["hostName"]
    port = conn.parameterValues["port"]
    user = conn.parameterValues["userName"]

    accepted, password = mforms.Utilities.find_or_ask_for_password("Fabric Node Connection", '%s@%s' % (host, port), user, False)
    if accepted:
        doc = ConfigParser.ConfigParser()

        section = 'protocol.xmlrpc'
        doc.add_section(section)
        doc.set(section, 'address', '%s:%s' % (host, port))
        doc.set(section, 'user', user)
        doc.set(section, 'password', password)
        doc.set(section, 'realm', 'MySQL Fabric')

        file = tempfile.NamedTemporaryFile(delete=False)
        file.write("# Fabric connection settings for MySQL Workbench\n\n")
        doc.write(file)

        return file.name
    else:
        raise OperationCancelledError("Password input cancelled")


def parse_standard_output(output):
    lines = output.split('\n')
    dict = {}
    for line in lines:
        if line.find('=') != -1:
            att, val = line.split('=', 2)
            if att.startswith('{'):
                att = att[1:]

            dict[att.strip()] = val.strip()

    return dict


@ModuleInfo.export(grt.STRING, grt.classes.db_mgmt_Connection)
def testConnection(conn):

    error = fabric_unavailable_message

    if not error:
        config = ''
        try:
            config = create_config(conn)

            command = '"%s" --config="%s" manage ping' % (fabric_client, config)

            ret, out = execute(command)

            if ret:
                error = "Unexpected error while connecting to the fabric node: %s" % out
                log_warning("WBFabric Module", "Unexpected error while connecting to the fabric node: %s\n" % out)
            else:
                data = parse_standard_output(out)

                if not data.has_key('success') or data['success'] != 'True':
                    error = "Unexpected error while connecting to fabric node: %s" % out
                    log_warning("WBFabric Module", "Unexpected error while connecting to the fabric node: %s\n" % out)

        except OperationCancelledError, e:
            error = "Operation Cancelled"
            log_warning("WBFabric Module", "User cancelled testing MySQL Fabric connection %s\n" % conn.name)
        except Exception, e:
            error = str(e)
            log_warning("WBFabric Module", "Error testing MySQL Fabric connection %s : %s\n" % (conn.name, traceback.format_exc()))
        finally:
            if os.path.exists(config):
                os.remove(config)

    return error


def execute_formatted_command(config, fabric_command):
    return_data = None

    command = '"%s" --config="%s" %s' % (fabric_client, config, fabric_command)

    ret, out = execute(command)

    if ret:
        raise Exception("Unexpected error on fabric operation: %s, %s\n" % (fabric_command, out))
    else:
        formatted_data = parse_standard_output(out)

        if not formatted_data.has_key('success') or formatted_data['success'] != 'True':
            raise Exception("Unexpected error on fabric operation: %s, %s" % (fabric_command, out))
        else:
            # Swaps the quoting so the format is OK for the json loader
            json_formatted = formatted_data['return'].replace("'", "`").replace('"', "'").replace('`', '"')
            json_formatted = json_formatted.replace('False', 'false')
            json_formatted = json_formatted.replace('True', 'true')
            return_data = json.loads(json_formatted)

    return return_data


def get_managed_connections(conn):
    connections = {}

    for connection in grt.root.wb.rdbmsMgmt.storedConns:
        params = connection.parameterValues

        if params.has_key('fabric_managed') and params['fabric_managed'] == conn.__id__:
            mparams = connection.parameterValues
            address = "%s:%s" % (mparams['hostName'], mparams['port'])
            connections[address] = connection

    return connections


@ModuleInfo.export(grt.STRING, grt.classes.db_mgmt_Connection)
def updateConnections(conn):

    error = fabric_unavailable_message

    if not error:
        config = ''

        try:
            # Creates the configuration object from the connection
            # settings.
            config = create_config(conn)

            # Pulls the HA groups
            groups = execute_formatted_command(config, 'group lookup_groups')

            # Variables for error handling
            fabric_group_count = 0
            matched_groups = []
            added_servers = 0
            managed_connections = 0

            # Sorts the groups
            def group_key(item):
                return item['group_id']

            groups = sorted(groups, key=group_key)

            fabric_group_count = len(groups)

            group_filter = conn.parameterValues["haGroupFilter"].strip()

            # Retrieves the list of the existing managed connections
            existing_connections = get_managed_connections(conn)

            for group in groups:
                include_group = not group_filter or group['group_id'] in group_filter

                if include_group:
                    matched_groups.append(group['group_id'])

                    servers = execute_formatted_command(config, 'group lookup_servers %s' % group['group_id'])

                    # Sorts the servers
                    def server_key(item):
                        return item['address']

                    servers = sorted(servers, key=server_key)

                    # Creates a connection for each retrieved server.
                    for server in servers:
                        address = server['address']
                        host, port = address.split(':')

                        # If the managed servers are located on the fabric node
                        # most probably they will use localhost or 127.0.0.1 as
                        # address on the fabric configuration.

                        # We need to replace that for the fabric node IP in order
                        # to create the connections in WB
                        if host in ['localhost', '127.0.0.1']:
                            host = conn.parameterValues["hostName"]

                        address = '%s:%s' % (host, port)

                        managed_connections += 1

                        if existing_connections.has_key(address):
                            del existing_connections[address]
                        else:
                            child_conn_name = '%s/%s:%s' % (conn.name, host, port)

                            server_user = conn.parameterValues["mysqlUserName"]
                            managed_conn = grt.modules.Workbench.create_connection(host, server_user, '', 1, 0, int(port), child_conn_name)
                            managed_conn.parameterValues["fabric_managed"] = conn.__id__
                            managed_conn.parameterValues["fabric_group_id"] = group["group_id"]

                            # Includes the rest of the server parameters on the connection parameters
                            for att in server.keys():
                                if att != 'address':
                                    managed_conn.parameterValues['fabric_%s' % att] = server[att]

                            added_servers += 1

            # Removes the remaining connections (which no longer exist on the fabric node)
            for connection in existing_connections.values():
                grt.modules.Workbench.deleteConnection(connection)

            conn.parameterValues["managedConnectionsUpdateTime"] = time.strftime('%Y-%m-%d %H:%M:%S')

            if added_servers or existing_connections:
                grt.modules.Workbench.refreshHomeConnections()
            elif managed_connections == 0:
                if fabric_group_count == 0:
                    error = "There are no High Availability Groups defined on the %s fabric node." % conn.name
                elif not matched_groups:
                    error = "There are no High Availability Groups matching the configured group filter on %s." % conn.name
                else:
                    error = "There are no Managed Servers defined for the included groups in %s: %s." % (conn.name, ','.join(matched_groups))

        except OperationCancelledError, e:
            error = "Operation Cancelled"
            log_warning("WBFabric Module", "User cancelled creating connectios to managed servers in the %s fabric node\n" % conn.name)
        except Exception, e:
            error = str(e)
            log_warning("WBFabric Module", "Error creating connectios to managed servers in the %s fabric node : %s\n" % (conn.name, traceback.format_exc()))
        finally:
            if os.path.exists(config):
                os.remove(config)

    return error



find_fabric_client()