__init__.py 20.5 KB
Newer Older
1
# Copyright 2011-2014 GRNET S.A. All rights reserved.
Giorgos Verigakis's avatar
Giorgos Verigakis committed
2
3
4
5
6
7
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
Stavros Sachtouris's avatar
Stavros Sachtouris committed
8
#      copyright notice, self.list of conditions and the following
Giorgos Verigakis's avatar
Giorgos Verigakis committed
9
10
11
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
Stavros Sachtouris's avatar
Stavros Sachtouris committed
12
#      copyright notice, self.list of conditions and the following
Giorgos Verigakis's avatar
Giorgos Verigakis committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

34
from urllib2 import quote, unquote
35
from urlparse import urlparse
36
from threading import Thread
37
from json import dumps, loads
38
from time import time
39
from httplib import ResponseNotReady, HTTPException
40
41
from time import sleep
from random import random
42
from logging import getLogger
43
44

from objpool.http import PooledHTTPConnection
45

Giorgos Verigakis's avatar
Giorgos Verigakis committed
46

47
48
TIMEOUT = 60.0   # seconds
HTTP_METHODS = ['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'COPY', 'MOVE']
49

50
51
52
log = getLogger(__name__)
sendlog = getLogger('%s.send' % __name__)
recvlog = getLogger('%s.recv' % __name__)
53

54
55
56
57
58
59

def _encode(v):
    if v and isinstance(v, unicode):
        return quote(v.encode('utf-8'))
    return v

60

Giorgos Verigakis's avatar
Giorgos Verigakis committed
61
class ClientError(Exception):
62
    def __init__(self, message, status=0, details=None):
63
        log.debug('ClientError: msg[%s], sts[%s], dtl[%s]' % (
64
65
66
            message,
            status,
            details))
67
        try:
68
            message += '' if message and message[-1] == '\n' else '\n'
69
            serv_stat, sep, new_msg = message.partition('{')
70
            new_msg = sep + new_msg[:-1 if new_msg.endswith('\n') else 0]
71
            json_msg = loads(new_msg)
72
            key = json_msg.keys()[0]
73
            serv_stat = serv_stat.strip()
74

75
            json_msg = json_msg[key]
76
77
78
79
80
81
            message = '%s %s (%s)\n' % (
                serv_stat,
                key,
                json_msg['message']) if (
                    'message' in json_msg) else '%s %s' % (serv_stat, key)
            status = json_msg.get('code', status)
82
83
84
            if 'details' in json_msg:
                if not details:
                    details = []
85
                if not isinstance(details, list):
86
87
88
                    details = [details]
                if json_msg['details']:
                    details.append(json_msg['details'])
89
        except Exception:
90
            pass
91
        finally:
92
93
            while message.endswith('\n\n'):
                message = message[:-1]
94
95
96
            super(ClientError, self).__init__(message)
            self.status = status if isinstance(status, int) else 0
            self.details = details if details else []
Giorgos Verigakis's avatar
Giorgos Verigakis committed
97

98

99
100
101
102
class Logged(object):

    LOG_TOKEN = False
    LOG_DATA = False
103
    LOG_PID = False
104
    _token = None
105
106
107


class RequestManager(Logged):
108
109
110
111
112
113
114
115
116
117
118
119
    """Handle http request information"""

    def _connection_info(self, url, path, params={}):
        """ Set self.url to scheme://netloc/?params
        :param url: (str or unicode) The service url

        :param path: (str or unicode) The service path (url/path)

        :param params: (dict) Parameters to add to final url

        :returns: (scheme, netloc)
        """
120
        url = _encode(str(url)) if url else 'http://127.0.0.1/'
121
122
123
        url += '' if url.endswith('/') else '/'
        if path:
            url += _encode(path[1:] if path.startswith('/') else path)
124
125
        delim = '?'
        for key, val in params.items():
126
            val = quote('' if val in (None, False) else _encode('%s' % val))
127
128
            url += '%s%s%s' % (delim, key, ('=%s' % val) if val else '')
            delim = '&'
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        parsed = urlparse(url)
        self.url = url
        self.path = parsed.path or '/'
        if parsed.query:
            self.path += '?%s' % parsed.query
        return (parsed.scheme, parsed.netloc)

    def __init__(
            self, method, url, path,
            data=None, headers={}, params={}):
        method = method.upper()
        assert method in HTTP_METHODS, 'Invalid http method %s' % method
        if headers:
            assert isinstance(headers, dict)
143
        self.headers = dict(headers)
144
145
146
        self.method, self.data = method, data
        self.scheme, self.netloc = self._connection_info(url, path, params)

147
    def dump_log(self):
148
        plog = ('\t[%s]' % self) if self.LOG_PID else ''
149
150
151
        sendlog.info('- -  -   -     -        -             -')
        sendlog.info('%s %s://%s%s%s' % (
            self.method, self.scheme, self.netloc, self.path, plog))
152
        for key, val in self.headers.items():
153
154
155
            if key.lower() in ('x-auth-token', ) and not self.LOG_TOKEN:
                self._token, val = val, '...'
            sendlog.info('  %s: %s%s' % (key, val, plog))
156
        if self.data:
Stavros Sachtouris's avatar
Stavros Sachtouris committed
157
            sendlog.info('data size: %s%s' % (len(self.data), plog))
158
            if self.LOG_DATA:
159
160
                sendlog.info(self.data.replace(self._token, '...') if (
                    self._token) else self.data)
161
        else:
Stavros Sachtouris's avatar
Stavros Sachtouris committed
162
            sendlog.info('data size: 0%s' % plog)
163

Stavros Sachtouris's avatar
Stavros Sachtouris committed
164
165
166
    def _encode_headers(self):
        headers = self.headers
        for k, v in self.headers.items():
167
            headers[k] = quote('' if v is None else '%s' % v)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
168
169
        self.headers = headers

170
171
172
173
174
175
    def perform(self, conn):
        """
        :param conn: (httplib connection object)

        :returns: (HTTPResponse)
        """
Stavros Sachtouris's avatar
Stavros Sachtouris committed
176
        self._encode_headers()
177
        self.dump_log()
178
179
180
181
182
        conn.request(
            method=str(self.method.upper()),
            url=str(self.path),
            headers=self.headers,
            body=self.data)
183
        sendlog.info('')
184
        keep_trying = TIMEOUT
Stavros Sachtouris's avatar
Stavros Sachtouris committed
185
        while keep_trying > 0:
186
187
188
            try:
                return conn.getresponse()
            except ResponseNotReady:
Stavros Sachtouris's avatar
Stavros Sachtouris committed
189
190
191
                wait = 0.03 * random()
                sleep(wait)
                keep_trying -= wait
192
        plog = ('\t[%s]' % self) if self.LOG_PID else ''
193
        logmsg = 'Kamaki Timeout %s %s%s' % (self.method, self.path, plog)
194
        recvlog.debug(logmsg)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
195
        raise ClientError('HTTPResponse takes too long - kamaki timeout')
196
197


198
class ResponseManager(Logged):
199
200
    """Manage the http request and handle the response data, headers, etc."""

201
    def __init__(self, request, poolsize=None, connection_retry_limit=0):
202
203
        """
        :param request: (RequestManager)
204
205
206
207

        :param poolsize: (int) the size of the connection pool

        :param connection_retry_limit: (int)
208
        """
209
        self.CONNECTION_TRY_LIMIT = 1 + connection_retry_limit
210
211
212
213
214
215
216
217
218
        self.request = request
        self._request_performed = False
        self.poolsize = poolsize

    def _get_response(self):
        if self._request_performed:
            return

        pool_kw = dict(size=self.poolsize) if self.poolsize else dict()
219
220
221
222
223
224
225
        for retries in range(1, self.CONNECTION_TRY_LIMIT + 1):
            try:
                with PooledHTTPConnection(
                        self.request.netloc, self.request.scheme,
                        **pool_kw) as connection:
                    self.request.LOG_TOKEN = self.LOG_TOKEN
                    self.request.LOG_DATA = self.LOG_DATA
226
                    self.request.LOG_PID = self.LOG_PID
227
                    r = self.request.perform(connection)
228
229
230
231
232
                    plog = ''
                    if self.LOG_PID:
                        recvlog.info('\n%s <-- %s <-- [req: %s]\n' % (
                            self, r, self.request))
                        plog = '\t[%s]' % self
233
234
235
236
                    self._request_performed = True
                    self._status_code, self._status = r.status, unquote(
                        r.reason)
                    recvlog.info(
237
238
                        '%d %s%s' % (
                            self.status_code, self.status, plog))
239
240
                    self._headers = dict()
                    for k, v in r.getheaders():
241
242
243
                        if k.lower in ('x-auth-token', ) and (
                                not self.LOG_TOKEN):
                            self._token, v = v, '...'
244
                        v = unquote(v).decode('utf-8')
245
                        self._headers[k] = v
246
                        recvlog.info('  %s: %s%s' % (k, v, plog))
247
                    self._content = r.read()
248
249
                    recvlog.info('data size: %s%s' % (
                        len(self._content) if self._content else 0, plog))
250
                    if self.LOG_DATA and self._content:
251
252
253
                        data = '%s%s' % (self._content, plog)
                        if self._token:
                            data = data.replace(self._token, '...')
254
255
                        recvlog.info(data)
                    recvlog.info('-             -        -     -   -  - -')
256
257
258
259
260
261
262
263
264
265
266
                break
            except Exception as err:
                if isinstance(err, HTTPException):
                    if retries >= self.CONNECTION_TRY_LIMIT:
                        raise ClientError(
                            'Connection to %s failed %s times (%s: %s )' % (
                                self.request.url, retries, type(err), err))
                else:
                    from traceback import format_stack
                    recvlog.debug(
                        '\n'.join(['%s' % type(err)] + format_stack()))
267
                    raise
268
269
                    raise ClientError(
                        'Failed while http-connecting to %s (%s)' % (
270
                            self.request.url, err))
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

    @property
    def status_code(self):
        self._get_response()
        return self._status_code

    @property
    def status(self):
        self._get_response()
        return self._status

    @property
    def headers(self):
        self._get_response()
        return self._headers

    @property
    def content(self):
        self._get_response()
        return self._content

    @property
    def text(self):
        """
        :returns: (str) content
        """
        self._get_response()
        return '%s' % self._content

    @property
    def json(self):
        """
        :returns: (dict) squeezed from json-formated content
        """
        self._get_response()
        try:
            return loads(self._content)
        except ValueError as err:
Stavros Sachtouris's avatar
Stavros Sachtouris committed
309
            raise ClientError('Response not formated in JSON - %s' % err)
310
311


312
class SilentEvent(Thread):
313
    """Thread-run method(*args, **kwargs)"""
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
    def __init__(self, method, *args, **kwargs):
        super(self.__class__, self).__init__()
        self.method = method
        self.args = args
        self.kwargs = kwargs

    @property
    def exception(self):
        return getattr(self, '_exception', False)

    @property
    def value(self):
        return getattr(self, '_value', None)

    def run(self):
        try:
            self._value = self.method(*(self.args), **(self.kwargs))
        except Exception as e:
332
333
334
335
336
            recvlog.debug('Thread %s got exception %s\n<%s %s' % (
                self,
                type(e),
                e.status if isinstance(e, ClientError) else '',
                e))
337
338
            self._exception = e

339

340
class Client(Logged):
341

342
    MAX_THREADS = 1
343
    DATE_FORMATS = ['%a %b %d %H:%M:%S %Y', ]
344
    CONNECTION_RETRY_LIMIT = 0
345

346
    def __init__(self, base_url, token):
347
        assert base_url, 'No base_url for client %s' % self
Giorgos Verigakis's avatar
Giorgos Verigakis committed
348
        self.base_url = base_url
349
        self.token = token
350
        self.headers, self.params = dict(), dict()
351
        self.poolsize = None
352

353
    def _init_thread_limit(self, limit=1):
354
        assert isinstance(limit, int) and limit > 0, 'Thread limit not a +int'
355
356
357
358
359
        self._thread_limit = limit
        self._elapsed_old = 0.0
        self._elapsed_new = 0.0

    def _watch_thread_limit(self, threadlist):
360
361
362
        self._thread_limit = getattr(self, '_thread_limit', 1)
        self._elapsed_new = getattr(self, '_elapsed_new', 0.0)
        self._elapsed_old = getattr(self, '_elapsed_old', 0.0)
363
        recvlog.debug('# running threads: %s' % len(threadlist))
364
        if self._elapsed_old and self._elapsed_old >= self._elapsed_new and (
365
                self._thread_limit < self.MAX_THREADS):
366
            self._thread_limit += 1
367
        elif self._elapsed_old <= self._elapsed_new and self._thread_limit > 1:
368
369
370
371
372
373
374
375
376
377
378
379
380
            self._thread_limit -= 1

        self._elapsed_old = self._elapsed_new
        if len(threadlist) >= self._thread_limit:
            self._elapsed_new = 0.0
            for thread in threadlist:
                begin_time = time()
                thread.join()
                self._elapsed_new += time() - begin_time
            self._elapsed_new = self._elapsed_new / len(threadlist)
            return []
        return threadlist

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
    def async_run(self, method, kwarg_list):
        """Fire threads of operations

        :param method: the method to run in each thread

        :param kwarg_list: (list of dicts) the arguments to pass in each method
            call

        :returns: (list) the results of each method call w.r. to the order of
            kwarg_list
        """
        flying, results = {}, {}
        self._init_thread_limit()
        for index, kwargs in enumerate(kwarg_list):
            self._watch_thread_limit(flying.values())
            flying[index] = SilentEvent(method=method, **kwargs)
            flying[index].start()
            unfinished = {}
            for key, thread in flying.items():
                if thread.isAlive():
                    unfinished[key] = thread
                elif thread.exception:
                    raise thread.exception
                else:
                    results[key] = thread.value
            flying = unfinished
        sendlog.info('- - - wait for threads to finish')
        for key, thread in flying.items():
            if thread.isAlive():
                thread.join()
411
            if thread.exception:
412
                raise thread.exception
413
            results[key] = thread.value
414
415
        return results.values()

416
    def _raise_for_status(self, r):
417
        log.debug('raise err from [%s] of type[%s]' % (r, type(r)))
418
        status_msg = getattr(r, 'status', None) or ''
419
        try:
420
            message = '%s %s\n' % (status_msg, r.text)
421
        except:
422
423
424
            message = '%s %s\n' % (status_msg, r)
        status = getattr(r, 'status_code', getattr(r, 'status', 0))
        raise ClientError(message, status=status)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
425

426
    def set_header(self, name, value, iff=True):
427
        """Set a header 'name':'value'"""
428
        if value is not None and iff:
429
            self.headers['%s' % name] = '%s' % value
430
431
432

    def set_param(self, name, value=None, iff=True):
        if iff:
433
            self.params[name] = '%s' % value
434

435
    def request(
436
437
            self, method, path,
            async_headers=dict(), async_params=dict(),
438
            **kwargs):
439
440
441
442
443
444
        """Commit an HTTP request to base_url/path
        Requests are commited to and performed by Request/ResponseManager
        These classes perform a lazy http request. Present method, by default,
        enforces them to perform the http call. Hint: call present method with
        success=None to get a non-performed ResponseManager object.
        """
445
446
447
        assert isinstance(method, str) or isinstance(method, unicode)
        assert method
        assert isinstance(path, str) or isinstance(path, unicode)
448
        try:
449
450
451
452
            headers = dict(self.headers)
            headers.update(async_headers)
            params = dict(self.params)
            params.update(async_params)
453
454
            success = kwargs.pop('success', 200)
            data = kwargs.pop('data', None)
455
            headers.setdefault('X-Auth-Token', self.token)
456
            if 'json' in kwargs:
457
                data = dumps(kwargs.pop('json'))
458
                headers.setdefault('Content-Type', 'application/json')
459
            if data:
460
461
                headers.setdefault('Content-Length', '%s' % len(data))

462
            plog = ('\t[%s]' % self) if self.LOG_PID else ''
463
            sendlog.debug('\n\nCMT %s@%s%s', method, self.base_url, plog)
464
465
466
            req = RequestManager(
                method, self.base_url, path,
                data=data, headers=headers, params=params)
467
            #  req.log()
468
            r = ResponseManager(
469
470
471
                req,
                poolsize=self.poolsize,
                connection_retry_limit=self.CONNECTION_RETRY_LIMIT)
472
473
            r.LOG_TOKEN, r.LOG_DATA, r.LOG_PID = (
                self.LOG_TOKEN, self.LOG_DATA, self.LOG_PID)
474
            r._token = headers['X-Auth-Token']
475
        finally:
476
477
            self.headers = dict()
            self.params = dict()
478
479

        if success is not None:
480
            # Success can either be an int or a collection
481
482
483
            success = (success,) if isinstance(success, int) else success
            if r.status_code not in success:
                self._raise_for_status(r)
484
        return r
Giorgos Verigakis's avatar
Giorgos Verigakis committed
485

Giorgos Verigakis's avatar
Giorgos Verigakis committed
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
    def delete(self, path, **kwargs):
        return self.request('delete', path, **kwargs)

    def get(self, path, **kwargs):
        return self.request('get', path, **kwargs)

    def head(self, path, **kwargs):
        return self.request('head', path, **kwargs)

    def post(self, path, **kwargs):
        return self.request('post', path, **kwargs)

    def put(self, path, **kwargs):
        return self.request('put', path, **kwargs)

501
502
503
504
505
    def copy(self, path, **kwargs):
        return self.request('copy', path, **kwargs)

    def move(self, path, **kwargs):
        return self.request('move', path, **kwargs)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
506
507
508
509
510


class Waiter(object):

    def _wait(
511
512
513
            self, item_id, wait_status, get_status,
            delay=1, max_wait=100, wait_cb=None, wait_for_status=False):
        """Wait while the item is still in wait_status or to reach it
Stavros Sachtouris's avatar
Stavros Sachtouris committed
514
515
516

        :param server_id: integer (str or int)

517
        :param wait_status: (str)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
518
519
520
521
522
523
524
525
526

        :param get_status: (method(self, item_id)) if called, returns
            (status, progress %) If no way to tell progress, return None

        :param delay: time interval between retries

        :param wait_cb: (method(total steps)) returns a generator for
            reporting progress or timeouts i.e., for a progress bar

527
528
        :param wait_for_status: (bool) wait FOR (True) or wait WHILE (False)

Stavros Sachtouris's avatar
Stavros Sachtouris committed
529
530
531
532
533
534
535
536
        :returns: (str) the new mode if successful, (bool) False if timed out
        """
        status, progress = get_status(self, item_id)

        if wait_cb:
            wait_gen = wait_cb(max_wait // delay)
            wait_gen.next()

537
538
539
540
541
542
        if wait_for_status ^ (status != wait_status):
            # if wait_cb:
            #     try:
            #         wait_gen.next()
            #     except Exception:
            #         pass
Stavros Sachtouris's avatar
Stavros Sachtouris committed
543
544
545
            return status
        old_wait = total_wait = 0

546
547
        while (wait_for_status ^ (status == wait_status)) and (
                total_wait <= max_wait):
Stavros Sachtouris's avatar
Stavros Sachtouris committed
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
            if wait_cb:
                try:
                    for i in range(total_wait - old_wait):
                        wait_gen.next()
                except Exception:
                    break
            old_wait = total_wait
            total_wait = progress or total_wait + 1
            sleep(delay)
            status, progress = get_status(self, item_id)

        if total_wait < max_wait:
            if wait_cb:
                try:
                    for i in range(max_wait):
                        wait_gen.next()
                except:
                    pass
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
        return status if (wait_for_status ^ (status != wait_status)) else False

    def wait_for(
            self, item_id, target_status, get_status,
            delay=1, max_wait=100, wait_cb=None):
        self._wait(
            item_id, target_status, get_status, delay, max_wait, wait_cb,
            wait_for_status=True)

    def wait_while(
            self, item_id, target_status, get_status,
            delay=1, max_wait=100, wait_cb=None):
        self._wait(
            item_id, target_status, get_status, delay, max_wait, wait_cb,
            wait_for_status=False)