__init__.py 20 KB
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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
##############################################################################
#
# Copyright (c) 2012 Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly advised to contract a Free Software
# Service Company
#
# 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; either version 3
# of the License, or (at your option) any later version.
#
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################
"""
Client implementation for portal_task_distribution.

Example use:
  import erp5.util.taskdistribution
  tool = erp5.util.taskdistribution.TaskDistributionTool(...)
  test_result = tool.createTestResult(...)
  test_result.addWatch('foo', open('foo'))
  while True:
      test_line = test_result.start()
      if not test_line:
          break
      # Run the test_line.name test
      test_line.stop()
"""
import httplib
import logging
import select
import socket
import threading
import time
import xmlrpclib

__all__ = ['TaskDistributionTool', 'TestResultProxy', 'TestResultLineProxy', 'patchRPCParser']

# Depending on used xmlrpc backend, different exceptions can be thrown.
SAFE_RPC_EXCEPTION_LIST = [socket.error, xmlrpclib.ProtocolError,
    xmlrpclib.Fault, httplib.BadStatusLine, httplib.ResponseNotReady]
parser, _ = xmlrpclib.getparser()
if xmlrpclib.ExpatParser and isinstance(parser, xmlrpclib.ExpatParser):
    SAFE_RPC_EXCEPTION_LIST.append(xmlrpclib.expat.ExpatError)
else:
    import sys
    print >> sys.stderr, 'Warning: unhandled xmlrpclib parser %r, some ' \
        'exceptions might get through safeRpcCall' % (parser, )
    del sys
SAFE_RPC_EXCEPTION_LIST = tuple(SAFE_RPC_EXCEPTION_LIST)
del parser, _

def null_callable(*args, **kw):
    pass

class NullLogger(object):
    def __getattr__(self, name):
        return null_callable
null_logger = NullLogger()

def patchRPCParser(error_handler):
    """
    Patch xmlrpcmlib's parser class, so it logs data content in case of errors,
    to ease debugging.
    Warning: this installs a monkey patch on a generic class, so it's last
    comes wins. Must *not* be enabled by default.

    error_handler (callable)
      Receives the erroneous data as first parameter, and the exception
      instance as second parameter.
      If it returns a false value (ie, handler did not recover from the error),
      exception is re-raised.
    """
    parser, _ = xmlrpclib.getparser()
    parser_klass = parser.__class__
    original_feed = parser_klass.feed
    def verbose_feed(self, data):
        try:
            return original_feed(self, data)
        except Exception, exc:
            if not error_handler(data, exc):
                raise
    parser_klass.feed = verbose_feed

def binarize_args(arg):
  # Converts recursively basestring arg into xmlrpclib.Binary, as they can
  # contain non-XML allowed characters
  if isinstance(arg, basestring):
    if isinstance(arg, unicode):
      arg = arg.encode('utf-8')
    return xmlrpclib.Binary(arg)
  if isinstance(arg, (list, tuple, set)):
    return map(binarize_args, arg)
  if isinstance(arg, dict):
    return {k: binarize_args(v) for k, v in arg.iteritems()}
  return arg

class RPCRetry(object):
    def __init__(self, proxy, retry_time, logger):
        super(RPCRetry, self).__init__()
        self._proxy = proxy
        self._retry_time = retry_time
        self._logger = logger

    def _RPC(self, func_id, args=()):
        return getattr(self._proxy, func_id)(*args)

    def _retryRPC(self, func_id, args=()):
        retry_time = self._retry_time
        while True:
            try:
                return self._RPC(func_id, args)
            except SAFE_RPC_EXCEPTION_LIST:
                self._logger.warning('Got exception, retrying: %s%r '
                    'in %is', func_id, tuple(args), retry_time, exc_info=1)
                time.sleep(retry_time)
                # find a balance between not overloading a server and
                # getting back working services quickly when rpc calls should
                # rework (do not wait 2 days if server is back)
                retry_time = min(retry_time * 1.5, self._retry_time * 10)

class TestResultLineProxy(RPCRetry):
    """
    Represents a single test in a suite.

    Properties:
    name (str) (ro)
      Test name, as provided to TaskDistributionTool.createTestResult .
    """
    def __init__(self, proxy, retry_time, logger, test_result_line_path,
            test_name):
        super(TestResultLineProxy, self).__init__(proxy, retry_time, logger)
        self._test_result_line_path = test_result_line_path
        self._name = test_name

    def __repr__(self):
        return '<%s(%r, %r) at %x>' % (self.__class__.__name__,
            self._test_result_line_path, self._name, id(self))

    @property
    def name(self):
        return self._name

    def isTestCaseAlive(self):
        """
        Tell if test result line is still alive on site.
        """
        try:
          return bool(self._retryRPC('isTestCaseAlive', [self._test_result_line_path]))
        except:
          raise ValueError('isTestCaseAlive Failed.')

    def stop(self, test_count=None, error_count=None, failure_count=None,
            skip_count=None, duration=None, date=None, command=None,
            stdout=None, stderr=None, html_test_result=None, **kw):
        """
        Notify server of test completion.

        Without any parameter, notifies of a test failure which prevents any
        precise reading (step count, how many succeeded, etc).

        BBB: extra named arguments are deprecated (if some are really needed,
        they must be declared as explicit parameters, with proper default
        value).
        """
        status_dict = dict(x for x in (
            ('test_count', test_count),
            ('error_count',  error_count),
            ('failure_count',  failure_count),
            ('skip_count',  skip_count),
            ('duration',  duration),
            ('date',  date),
            ('command',  command),
            ('stdout',  stdout),
            ('stderr',  stderr),
            ('html_test_result',  html_test_result),
        ) if x[1] is not None)
        if kw:
            self._logger.info('Extra parameters provided: %r', kw)
            status_dict.update(kw)
        self._retryRPC('stopUnitTest', (self._test_result_line_path,
            binarize_args(status_dict)))

class TestResultProxy(RPCRetry):
    """
    Represents a test suite run.

    Allows fetching work to do (eg a single test in an entire run), monitoring
    log files, informing server of problems and monitoring server-side
    cancellation.

    Properties
    watcher_period (float) (rw)
      How long log watcher sleeps between successive uploading latest data
      chunks.
    revision (str) (ro)
      Revision to test. Might be different from the revision requested, when a
      test batch is running on an older revision.
    """
    _watcher_can_run = True
    _watcher_thread = None

    def __init__(self, proxy, retry_time, logger, test_result_path, node_title,
            revision):
        super(TestResultProxy, self).__init__(proxy, retry_time, logger)
        self._test_result_path = test_result_path
        self._node_title = node_title
        self._revision = revision
        self._watcher_period = 60
        self._watcher_dict = {}
        self._watcher_condition = threading.Condition()
    def __repr__(self):
        return '<%s(%r, %r, %r) at %x>' % (self.__class__.__name__,
            self._test_result_path, self._node_title, self._revision, id(self))

    @property
    def test_result_path(self):
        return self._test_result_path

    @property
    def revision(self):
        return self._revision

    def start(self, exclude_list=()):
        """
        Ask for a test to run, among the list of tests composing this test
        result.
        Return an TestResultLineProxy instance, or None if there is nothing to
        do.
        """
        result = self._retryRPC('startUnitTest', (self._test_result_path,
            binarize_args(exclude_list)))
        if result:
            line_url, test_name = result
            result = TestResultLineProxy(self._proxy, self._retry_time,
                self._logger, line_url, test_name)
        return result

    def reportFailure(self, date=None, command=None, stdout=None, stderr=None):
        """
        Report a test-node-level problem, preventing the test from continuing
        on this node.
        """
        self._stopWatching()
        status_dict = {
            'date': date,
        }
        if command is not None:
            status_dict['command'] = command
        if stdout is not None:
            status_dict['stdout'] = stdout
        if stderr is not None:
            status_dict['stderr'] = stderr
        self._retryRPC('reportTaskFailure', args=(self._test_result_path,
            binarize_args(status_dict), self._node_title))

    def reportStatus(self, command, stdout, stderr):
        """
        Report some progress.

        Used internally by file monitoring, you shouldn't have to use this
        directly.
        """
        try:
            self._RPC('reportTaskStatus', (self._test_result_path, {
                'command': command,
                'stdout': stdout,
                'stderr': stderr,
            }, self._node_title))
        except SAFE_RPC_EXCEPTION_LIST:
            self._logger.warning('Got exception in reportTaskStatus, giving up',
                exc_info=1)

    def isAlive(self):
        """
        Tell if test is still alive on site.

        Useful to probe for test cancellation by user, so a new test run can
        be started without waiting for current one to finish.
        """
        try:
            return self._RPC('isTaskAlive', (self._test_result_path, ))
        except SAFE_RPC_EXCEPTION_LIST:
            self._logger.warning('Got exception in isTaskAlive, assuming alive',
                exc_info=1)
            return 1

    @property
    def watcher_period(self):
        return self._watcher_period

    @watcher_period.setter
    def watcher_period(self, period):
        cond = self._watcher_condition
        with cond:
            self._watcher_period = period
            cond.notify()

    def addWatch(self, name, stream, max_history_bytes=None):
        """
        Monitor given file, sending a few latest lines to remote server.
        name (any)
          Arbitrary identifier for stream. Must be usable as a dict key.
        stream (file object)
          File to monitor from its current offset.
        max_history_bytes (int, None)
          How many bytes to send to remote server at most for each wakeup.
          If None, send all lines.
        """
        watcher_dict = self._watcher_dict
        if not watcher_dict:
            self._startWatching()
        elif name in watcher_dict:
            raise ValueError('Name already known: %r' % (name, ))
        watcher_dict[name] = (stream, max_history_bytes)

    def removeWatch(self, name):
        """
        Stop monitoring given stream.
        """
        watcher_dict = self._watcher_dict
        del watcher_dict[name]
        if not watcher_dict:
            self._stopWatching()

    def _startWatching(self):
        if self._watcher_thread is not None:
            raise ValueError('Thread already started')
        self._watcher_thread = thread = threading.Thread(target=self._watcher)
        thread.daemon = True
        thread.start()

    def _watcher(self):
        cond = self._watcher_condition
        while self._watcher_can_run and self.isAlive():
            caption_list = []
            append = caption_list.append
            for name, (stream, max_history_bytes) in \
                    self._watcher_dict.iteritems():
                append('==> %s <==' % (name, ))
                start = stream.tell()
                stream.seek(0, 2)
                end = stream.tell()
                if start == end:
                    caption = time.strftime(
                        '(no new lines at %Y/%m/%d %H:%M:%S)', time.gmtime())
                else:
                    to_read = end - start
                    if to_read < 0:
                        # File got truncated, treat the whole content as new.
                        to_read = end
                    if max_history_bytes is not None:
                        to_read = min(to_read, max_history_bytes)
                    stream.seek(-to_read, 1)
                    caption = stream.read(to_read)
                append(caption)
            self.reportStatus('', '\n'.join(caption_list), '')
            with cond:
                cond.wait(self._watcher_period)

    def _stopWatching(self):
        cond = self._watcher_condition
        with cond:
            self._watcher_can_run = False
            cond.notify()
        if self._watcher_thread is not None:
          self._watcher_thread.join()

    def stop(self):
        """
        
        """
        return self._retryRPC('stopTest', [self._test_result_path])
        
    def getRunningTestCase(self):
      """
      Return the relative path of the next test with the running state
      """
      return self._retryRPC('getRunningTestCase', [self._test_result_path])

class ServerProxy(xmlrpclib.ServerProxy):

    def __init__(self, *args, **kw):
        xmlrpclib.ServerProxy.__init__(self, *args, **kw)
        transport = self.__transport
        def make_connection(*args, **kw):
            conn = transport.__class__.make_connection(transport, *args, **kw)
            conn.timeout = 120
            return conn
        transport.make_connection = make_connection
        self.__rpc_lock = threading.Lock()

    def __request(self, *args, **kw):
        with self.__rpc_lock:
            return xmlrpclib.ServerProxy.__request(self, *args, **kw)

class TaskDistributionTool(RPCRetry):
    def __init__(self, portal_url, retry_time=64, logger=None):
        """
        portal_url (str, None)
          Portal URL of ERP5 site to use as a task distributor.
          If None, single node setup is assumed.
        """
        if logger is None:
            logger = null_logger
        if portal_url is None:
            proxy = DummyTaskDistributionTool()
        else:
            proxy = ServerProxy(
                portal_url,
                allow_none=True,
            ).portal_task_distribution
        super(TaskDistributionTool, self).__init__(proxy, retry_time, logger)
        protocol_revision = self._retryRPC('getProtocolRevision')
        if protocol_revision != 1:
            raise ValueError('Unsupported protocol revision: %r',
                protocol_revision)

    def createTestResult(self, revision, test_name_list, node_title,
            allow_restart=False, test_title=None, project_title=None):
        """
        (maybe) create a new test run.
        revision (str)
          An opaque string describing code being tested.
        test_name_list (list of str)
          List of tests being part of this test run. May be empty.
        node_title (str)
          Human-readable test node identifier, so an adnmin can know which
          node does what.
        allow_restart (bool)
          When true, a tet result is always created, even if a former finished
          one is found for same name and revision pair.
        test_title (str)
          Human-readable title for test. Must be identical for successive runs.
          Allows browsing its result history.
        project_title (str)
          Existing project title, so test result gets associated to it.

        Returns None if no test run is needed (a test run for given name and
        revision has already been completed).
        Otherwise, returns a TestResultProxy instance.
        """
        result = self._retryRPC('createTestResult', ('', revision,
            test_name_list, allow_restart, test_title, node_title,
            project_title))
        if result:
            test_result_path, revision = result
            result = TestResultProxy(self._proxy, self._retry_time,
                self._logger, test_result_path, node_title, revision)
        return result

class TaskDistributor(RPCRetry):

    def __init__(self,portal_url,retry_time=64,logger=None):
        if logger is None:
           logger = null_logger
        if portal_url is None:
            proxy = DummyTaskDistributionTool()
        else:
            proxy = ServerProxy(portal_url, allow_none=True)
        super(TaskDistributor, self).__init__(proxy, retry_time,logger)
        protocol_revision = self._retryRPC('getProtocolRevision')
        if protocol_revision != 1:
            raise ValueError('Unsupported protocol revision: %r',
                protocol_revision)

    def startTestSuite(self,node_title,computer_guid='unknown'):
      """
        Returns None if no test suite is needed.
        therwise, returns a JSON with all the test suite parameters.
      """
      result = self._retryRPC('startTestSuite',(node_title,computer_guid,))
      return result

    def getTestType(self):
      """
        Return the Test Type
      """
      result = self._retryRPC('getTestType')
      return result

    def subscribeNode(self, node_title, computer_guid):
      """
        Susbscribes node with the node title and the computer guid.
      """
      result = self._retryRPC('subscribeNode', (node_title,computer_guid,))
      return result


    def generateConfiguration(self, test_suite_title):
      """
        Generates a configuration from a test_suite_title
      """
      return self._retryRPC('generateConfiguration', (test_suite_title,))


    def isMasterTestnode(self, test_node_title):
      """
        Returns True or False if the testnode is the master
      """
      return self._retryRPC('isMasterTestnode', (test_node_title,))

    def getSlaposAccountKey(self):
      """
        Returns the slapos account key related to the distributor
      """
      return self._retryRPC('getSlaposAccountKey')
    
    def getSlaposAccountCertificate(self):
      """
        Returns the slapos account certificate related to the distributor
      """
      return self._retryRPC('getSlaposAccountCertificate')

    def getSlaposUrl(self):
      """
        Returns the url of slapos master related to the distributor
      """
      return self._retryRPC('getSlaposUrl')
      
    def getSlaposHateoasUrl(self):
      """
        Returns the url of API REST using hateoas of
        slapos master related to the distributor
      """
      return self._retryRPC('getSlaposHateoasUrl')


class DummyTaskDistributionTool(object):
    """
    Fake remote server.

    Useful when willing to locally run all tests without reporting to any
    server.

    This class should remain internal to this module.
    """
    test_name_list = None

    def __init__(self):
        self._lock = threading.Lock()

    def getProtocolRevision(self):
        return 1

    def createTestResult(self, name, revision, test_name_list, *args):
        self.test_name_list = test_name_list[:]
        return None, revision

    def startUnitTest(self, test_result_path, exclude_list=()):
        with self._lock:
            for i, test in enumerate(self.test_name_list):
                if test not in exclude_list:
                    del self.test_name_list[i]
                    return None, test

    def stopUnitTest(self, *args):
        pass

    reportTaskFailure = reportTaskStatus = stopUnitTest

    def isTaskAlive(self, *args):
        return int(bool(self.test_name_list))