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
|
# Copyright (c) 2016, 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
import mforms
import traceback
import time
import json
from wb_common import OperationCancelledError
fabric_unavailable_message = ""
fabric_client = ""
ModuleInfo = DefineModule(name= "WBFabric", author= "Oracle Corp.", version="1.0")
def perform_fabric_operation(conn, name, callback = None, callback_params = None):
"""
Current Fabric operations are done using the next cycle:
- Open Connection
- Execute Specific Operation
- Close Connection
This method allows performing these operations using this life cycle, removing from the
specific operation the burden of common operations such as password retrieval and exception
handling.
The specific operation should be done on a function received as callback.
To pass data from the caller to the actual Fabric operation method use the
callback_params, this method will also include the connection_id on such params.
"""
error = ""
# Retrieves the Fabric node connection data
host = conn.parameterValues["hostName"]
port = conn.parameterValues["port"]
user = conn.parameterValues["userName"]
try:
# Retrieves the required password
accepted, password = mforms.Utilities.find_or_ask_for_password("Fabric Node Connection", '%s@%s' % (host, port), user, False)
if accepted:
# Opens a connection to the Fabric instance
conn_id = grt.modules.WbFabricInterface.openConnection(conn, password)
if conn_id > 0:
# Executes the callback function which will interact with Fabric using the
# created connection.
if callback:
if callback_params:
callback_params['conn_id'] = conn_id
else:
callback_params = {'conn_id':conn_id}
error = callback(callback_params)
# Finally closes the connection
grt.modules.WbFabricInterface.closeConnection(conn_id)
except OperationCancelledError, e:
error = "Operation Cancelled"
log_warning("WBFabric Module", "User cancelled %s\n" % name)
except Exception, e:
error = str(e)
log_warning("WBFabric Module", "Error %s : %s\n" % (conn.name, traceback.format_exc()))
return error
def _execute_fabric_command(conn_id, fabric_command):
"""
This function will be used to actually execute a valid Fabric command
and process the result.
The data resulting from Fabric operations is returned in JSON format.
The Fabric commands will return 2 recordsets which are returned as 2 lists on the
returned json data:
- The first element is a status record, it is processed here and if there were errors
an exception is thrown.
- The second is the actual list of data returned by the executed function.
"""
return_data = None
out = grt.modules.WbFabricInterface.execute(conn_id, fabric_command)
all_data = json.loads(out)
status = all_data[0][0]
if status['message']:
raise Exception(status['message'])
elif len(all_data) == 2:
return_data = all_data[1]
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
def _update_fabric_connections(params):
error = ''
conn = params['conn']
conn_id = params['conn_id']
# Pulls the HA groups
groups = _execute_fabric_command(conn_id, 'call 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_fabric_command(conn_id, 'call 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
managed_conn = None
if existing_connections.has_key(address):
managed_conn = existing_connections[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)
added_servers += 1
# Update connection settings
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]
# 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))
return error
@ModuleInfo.export(grt.STRING, grt.classes.db_mgmt_Connection)
def testConnection(conn):
"""
Attempts a connection to the Fabric server; returns an error if required.
Since no additional operations are needed on such a connection, the specific operation callback is None.
"""
error = perform_fabric_operation(conn, 'testing Fabric Connection')
return error
@ModuleInfo.export(grt.STRING, grt.classes.db_mgmt_Connection)
def updateConnections(conn):
"""
Connects to the Fabric server and updates the Fabric connections in WB
"""
error = perform_fabric_operation(conn, 'updating Fabric Servers', _update_fabric_connections, {'conn':conn} )
return error
|