This file is indexed.

/usr/lib/python2.7/dist-packages/mysql/fabric/services/manage.py is in mysql-utilities 1.6.1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#
# Copyright (c) 2013,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
#

"""Provide key functions to start, stop and check Fabric's availability and
information on available commands.
"""
import getpass
import logging
import logging.handlers
import os.path
import urlparse

from mysql.fabric import (
    config as _config,
    errors as _errors,
    events as _events,
    executor as _executor,
    failure_detector as _failure_detector,
    persistence as _persistence,
    recovery as _recovery,
    services as _services,
    utils as _utils,
    server as _server,
    error_log as _error_log,
    credentials,
    handler as _logging,
    providers as providers,
)

from mysql.fabric.command import (
    Command,
    CommandResult,
    ResultSet,
)

from mysql.fabric.handler import (
    MySQLHandler,
)

from mysql.fabric.node import (
    FabricNode
)

_LOGGER = logging.getLogger(__name__)

# Logging levels.
_LOGGING_LEVELS = {
    "CRITICAL" : logging.CRITICAL,
    "ERROR" : logging.ERROR,
    "WARNING" : logging.WARNING,
    "INFO" : logging.INFO,
    "DEBUG" : logging.DEBUG
}

# Number of concurrent threads that are created to handle requests.
DEFAULT_N_THREADS = 5

# Number of concurrent executors that are created to handle jobs.
DEFAULT_N_EXECUTORS = 5

#Default TTL value
DEFAULT_TTL = 1

# MySQL's port
_MYSQL_PORT = 3306

class Logging(Command):
    """Set logging level.
    """
    group_name = "manage"
    command_name = "logging_level"

    def execute(self, module, level):
        """ Set logging level.

        :param module: Module that will have its logging level changed.
        :param level: The logging level that will be set.
        :return: Return True if the logging level is changed. Otherwise,
        False.
        """
        try:
            __import__(module)
            logger = logging.getLogger(module)
            logger.setLevel(_LOGGING_LEVELS[level.upper()])
        except (KeyError, ImportError) as error:
            _LOGGER.debug(error)
            return CommandResult(str(error))
        return CommandResult(None)


class Ping(Command):
    """Check whether Fabric server is running or not.
    """
    group_name = "manage"
    command_name = "ping"

    def execute(self):
        """Check whether Fabric server is running or not.
        """
        pass


class Start(Command):
    """Start the Fabric server.
    """
    group_name = "manage"
    command_name = "start"

    def dispatch(self, daemonize=False):
        """Start the Fabric server.
        """
        # Configure logging.
        _configure_logging(self.config, daemonize)

        # Configure connections.
        _configure_connections(self.config)

        credentials.check_initial_setup(
            self.config, _persistence.MySQLPersister(),
            check_only=True
        )

        # Daemonize ourselves.
        if daemonize:
            _utils.daemonize()

        # Start Fabric server.
        _start(self.options, self.config)
        _services.ServiceManager().wait()


class Setup(Command):
    """Setup Fabric Storage System.

    Create a database and necessary objects.
    """
    group_name = "manage"
    command_name = "setup"

    def dispatch(self):
        """Setup Fabric Storage System.
        """
        # Configure logging.
        _configure_logging(self.config, False)

        # Configure connections.
        _configure_connections(self.config)

        # Create database and objects.
        _persistence.setup(config=self.config)

        credentials.check_initial_setup(self.config,
                                        _persistence.MySQLPersister())


class Teardown(Command):
    """Teardown Fabric Storage System.

    Drop database and its objects.
    """
    group_name = "manage"
    command_name = "teardown"

    def dispatch(self):
        """Teardown Fabric Storage System.
        """
        # Configure logging.
        _configure_logging(self.config, False)

        # Configure connections.
        _configure_connections(self.config)

        # Drop database and objects.
        _persistence.teardown()


def _create_file_handler(config, info, delay=0):
    """Define a file handler where logging information will be
    sent to.
    """
    from logging.handlers import RotatingFileHandler
    assert info.scheme == 'file'
    if info.netloc:
        raise _errors.ConfigurationError(
            "Malformed file URL '%s'" % (info.geturl(),)
        )
    if os.path.isabs(info.path):
        path = info.path
    else:
        # Relative path, fetch the logdir from the configuration.
        # Using 'logging' section instead of 'DEFAULT' to allow
        # configuration parameters to be overridden in the logging
        # section.
        logdir = config.get('logging', 'logdir')
        path = os.path.join(logdir, info.path)
    return RotatingFileHandler(path, delay=delay)

# A URL provided should either have a path or an address, but not
# both. For example:
#
#   syslog:///dev/log       ---> Address is /dev/log
#   syslog://localhost:555  ---> Address is ('localhost', 555)
#   syslog://my.example.com ---> Address is ('my.example.com', 541)
#
# The following is not allowed:
#
#   syslog://example.com/foo/bar
def _create_syslog_handler(config, info):
    """Define a syslog handler where logging information will be
    sent to.
    """
    from logging.handlers import SYSLOG_UDP_PORT, SysLogHandler
    assert info.scheme == 'syslog'
    if info.netloc and info.path:
        raise _errors.ConfigurationError(
            "Malformed syslog URL '%s'" % (info.geturl(),)
        )
    if info.netloc:
        assert not info.path
        address = info.netloc.split(':')
        if len(address) == 1:
            address.append(SYSLOG_UDP_PORT)
    elif info.path:
        assert not info.netloc
        address = info.path

    return SysLogHandler(address=address)

_LOGGING_HANDLER = {
    'file': _create_file_handler,
    'syslog': _create_syslog_handler,
}

def _configure_logging(config, daemon):
    """Configure the logging system.
    """
    # Set up the logging information.
    logger = logging.getLogger("mysql.fabric")
    handler = None

    # Set up logging handler
    if daemon:
        urlinfo = urlparse.urlparse(config.get('logging', 'url'))
        handler = _LOGGING_HANDLER[urlinfo.scheme](config, urlinfo)
    else:
        handler = logging.StreamHandler()
    mysql_handler = _logging.MySQLHandler()

    formatter = logging.Formatter(
        "[%(levelname)s] %(created)f - %(threadName)s "
        "- %(message)s")
    handler.setFormatter(formatter)

    logger.addHandler(handler)
    logger.addHandler(mysql_handler)

    logger.setLevel(_LOGGING_LEVELS["DEBUG"])
    mysql_handler.setLevel(_LOGGING_LEVELS["DEBUG"])
    try:
        level = config.get("logging", "level")
        handler.setLevel(_LOGGING_LEVELS[level.upper()])
    except KeyError:
        handler.setLevel(_LOGGING_LEVELS["INFO"])

def _configure_connections(config):
    """Configure information on database connection and remote
    servers.
    """
    # Configure the number of concurrent executors.
    try:
        number_executors = config.get('executor', "executors")
        number_executors = int(number_executors)
    except (_config.NoOptionError, _config.NoSectionError, ValueError):
        number_executors = DEFAULT_N_EXECUTORS
    executor = _executor.Executor()
    executor.set_number_executors(number_executors)

    services = {}
    ssl_config = {}

    # XML-RPC service
    try:
        services['protocol.xmlrpc'] = config.get('protocol.xmlrpc', "address")

        try:
            number_threads = config.get('protocol.xmlrpc', "threads")
            number_threads = int(number_threads)
        except (_config.NoOptionError, ValueError):
            number_threads = DEFAULT_N_THREADS

        try:
            for option in ('ssl_ca', 'ssl_key', 'ssl_cert'):
                ssl_config[option] = config.get('protocol.xmlrpc', option)
        except _config.NoOptionError:
            ssl_config = {}
    except _config.NoSectionError:
        raise _errors.ConfigurationError(
            'Configuration for protocol.xmlrpc is required')

    # MySQL-RPC service
    try:
        services['protocol.mysql'] = config.get('protocol.mysql', "address")
    except _config.NoSectionError:
        # No MySQL-RPC configured
        pass

    # Define service configuration
    _services.ServiceManager(services, number_threads, ssl_config)

    # Fetch options to configure the state store.
    address = config.get('storage', 'address')

    try:
        host, port = address.split(':')
        port = int(port)
    except ValueError:
        host = address
        port = _MYSQL_PORT

    user = config.get('storage', 'user')
    database = config.get('storage', 'database')

    try:
        password = config.get('storage', 'password')
    except _config.NoOptionError:
        password = getpass.getpass()

    try:
        connection_timeout = config.get("storage", "connection_timeout")
        connection_timeout = float(connection_timeout)
    except (_config.NoOptionError, _config.NoSectionError, ValueError):
        connection_timeout = None

    try:
        connection_attempts = config.get("storage", "connection_attempts")
        connection_attempts = int(connection_attempts)
    except (_config.NoOptionError, _config.NoSectionError, ValueError):
        connection_attempts = None

    try:
        connection_delay = config.get("storage", "connection_delay")
        connection_delay = int(connection_delay)
    except (_config.NoOptionError, _config.NoSectionError, ValueError):
        connection_delay = None

    try:
        auth_plugin = config.get("storage", "auth_plugin")
    except (_config.NoOptionError, _config.NoSectionError, ValueError):
        auth_plugin = None

    # Define state store configuration.
    _persistence.init(
        host=host, port=port, user=user, password=password, database=database,
        connection_timeout=connection_timeout,
        connection_attempts=connection_attempts,
        connection_delay=connection_delay,
        auth_plugin=auth_plugin
    )

def _setup_ttl(config):
    """Read the configured TTL and set its value.
    """
    #configure the TTL to be used for the connectors.
    try:
        ttl = config.get('connector', "ttl")
        ttl = int(ttl)
        _utils.TTL = ttl
    except (_config.NoOptionError, _config.NoSectionError, ValueError):
        _utils.TTL = DEFAULT_TTL

def _start(options, config):
    """Start Fabric server.
    """
    #Configure TTL
    _setup_ttl(config)

    # Configure modules that are not dynamic loaded.
    _server.configure(config)
    _error_log.configure(config)
    _failure_detector.configure(config)

    # Load information on all providers.
    providers.find_providers()

    # Load all services into the service manager
    _services.ServiceManager().load_services(options, config)

    # Initilize the state store.
    _persistence.init_thread()

    # Check the maximum number of threads.
    _utils.check_number_threads()

    # Configure Fabric Node.
    fabric = FabricNode()
    reported = _utils.get_time()
    _LOGGER.info(
        "Fabric node starting.",
        extra={
            'subject' : str(fabric.uuid),
            'category' : MySQLHandler.NODE,
            'type' : MySQLHandler.START,
            'reported' : reported
        }
    )
    fabric.startup = reported

    # Start the executor, failure detector and then service manager. In this
    # scenario, the recovery is sequentially executed after starting the
    # executor and before starting the service manager.
    _events.Handler().start()
    _recovery.recovery()
    _failure_detector.FailureDetector.register_groups()
    _services.ServiceManager().start()


class Stop(Command):
    """Stop the Fabric server.
    """
    group_name = "manage"
    command_name = "stop"

    def execute(self):
        """Stop the Fabric server.
        """
        _shutdown()


def _shutdown():
    """Shutdown Fabric server.
    """
    _failure_detector.FailureDetector.unregister_groups()
    _services.ServiceManager().shutdown()
    _events.Handler().shutdown()
    _events.Handler().wait()
    _LOGGER.info(
        "Fabric node stopped.",
        extra={
            'subject' : 'Node',
            'category' : MySQLHandler.NODE,
            'type' : MySQLHandler.STOP
        }
    )


class FabricLookups(Command):
    """Return a list of Fabric servers.
    """
    group_name = "dump"
    command_name = "fabric_nodes"

    def execute(self, protocol=None):
        """Return a list with all the available Fabric Servers.

        :return: List with existing Fabric Servers.
        :rtype: ["host:port", ...]
        """
        service = _services.ServiceManager()
        rset = ResultSet(names=('host', 'port'),types=(str, int))
        for _, address in service.address(protocol).items():
            rset.append_row(address.split(":"))
        return CommandResult(None, results=rset)