Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / rapi_pool.py @ ffa7f6f6

History | View | Annotate | Download (3.5 kB)

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

    
34
from synnefo.lib.pool import ObjectPool
35
from synnefo.logic.rapi import GanetiRapiClient
36

    
37
from logging import getLogger
38
log = getLogger(__name__)
39

    
40
_pools = {}
41
_hashes = {}
42
pool_size = 8
43

    
44

    
45
class GanetiRapiClientPool(ObjectPool):
46
    """Pool of Ganeti RAPI Clients."""
47

    
48
    def __init__(self, host, port, user, passwd, size=None):
49
        log.debug("INIT: Initializing pool of size %d, host %s," \
50
                  " port %d, user %s", size, host, port, user)
51
        ObjectPool.__init__(self, size=size)
52
        self.host = host
53
        self.port = port
54
        self.user = user
55
        self.passwd = passwd
56

    
57
    def _pool_create(self):
58
        log.debug("CREATE: Creating new client from pool %r", self)
59
        client = GanetiRapiClient(self.host, self.port, self.user, self.passwd)
60
        client._pool = self
61
        return client
62

    
63
    def _pool_verify(self, client):
64
        return True
65

    
66
    def _pool_cleanup(self, conn):
67
        return False
68

    
69

    
70
def get_rapi_client(backend_id, backend_hash, host, port, user, passwd):
71
    """Get a ganeti RAPI client from pool."""
72
    log.debug("GET: Getting RAPI client")
73
    m = "%s cannot be None"
74
    if host is None:
75
        raise ValueError(m % "host")
76
    if port is None:
77
        raise ValueError(m % "port")
78

    
79
    # does the pool need to be created?
80
    if backend_hash not in _pools:
81
        log.debug("GET: No Pool. Creating new for host %s", host)
82
        pool = GanetiRapiClientPool(host, port, user, passwd, pool_size)
83
        _pools[backend_hash] = pool
84
        # Delete Pool for old backend_hash
85
        if backend_id in _hashes:
86
            del _pools[_hashes[backend_id]]
87
        _hashes[backend_id] = backend_hash
88

    
89
    obj = _pools[backend_hash].pool_get()
90
    log.debug("GET: Got object %r from pool", obj)
91
    return obj
92

    
93

    
94
def put_rapi_client(client):
95
    pool = client._pool
96
    log.debug("PUT: putting client %r back to pool %r", client, pool)
97
    if pool is None:
98
        log.debug("PUT: client %r does not have a pool", client)
99
        return
100
    pool.pool_put(client)