Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.4 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
pool_size = 8
42

    
43

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

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

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

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

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

    
68

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

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

    
85
    obj = _pools[backend_hash].pool_get()
86
    log.debug("GET: Got object %r from pool", obj)
87
    return obj
88

    
89

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