Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.5 kB)

1 3524241a Christos Stavrakakis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 3524241a Christos Stavrakakis
#
3 3524241a Christos Stavrakakis
# Redistribution and use in source and binary forms, with or
4 3524241a Christos Stavrakakis
# without modification, are permitted provided that the following
5 3524241a Christos Stavrakakis
# conditions are met:
6 3524241a Christos Stavrakakis
#
7 3524241a Christos Stavrakakis
#   1. Redistributions of source code must retain the above
8 3524241a Christos Stavrakakis
#      copyright notice, this list of conditions and the following
9 3524241a Christos Stavrakakis
#      disclaimer.
10 3524241a Christos Stavrakakis
#
11 3524241a Christos Stavrakakis
#   2. Redistributions in binary form must reproduce the above
12 3524241a Christos Stavrakakis
#      copyright notice, this list of conditions and the following
13 3524241a Christos Stavrakakis
#      disclaimer in the documentation and/or other materials
14 3524241a Christos Stavrakakis
#      provided with the distribution.
15 3524241a Christos Stavrakakis
#
16 3524241a Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 3524241a Christos Stavrakakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 3524241a Christos Stavrakakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 3524241a Christos Stavrakakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 3524241a Christos Stavrakakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 3524241a Christos Stavrakakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 3524241a Christos Stavrakakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 3524241a Christos Stavrakakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 3524241a Christos Stavrakakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 3524241a Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 3524241a Christos Stavrakakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 3524241a Christos Stavrakakis
# POSSIBILITY OF SUCH DAMAGE.
28 3524241a Christos Stavrakakis
#
29 3524241a Christos Stavrakakis
# The views and conclusions contained in the software and
30 3524241a Christos Stavrakakis
# documentation are those of the authors and should not be
31 3524241a Christos Stavrakakis
# interpreted as representing official policies, either expressed
32 3524241a Christos Stavrakakis
# or implied, of GRNET S.A.
33 3524241a Christos Stavrakakis
34 1a736ca8 Christos Stavrakakis
from objpool import ObjectPool
35 3524241a Christos Stavrakakis
from synnefo.logic.rapi import GanetiRapiClient
36 3524241a Christos Stavrakakis
37 3524241a Christos Stavrakakis
from logging import getLogger
38 3524241a Christos Stavrakakis
log = getLogger(__name__)
39 3524241a Christos Stavrakakis
40 3524241a Christos Stavrakakis
_pools = {}
41 ffa7f6f6 Christos Stavrakakis
_hashes = {}
42 3524241a Christos Stavrakakis
pool_size = 8
43 3524241a Christos Stavrakakis
44 3524241a Christos Stavrakakis
45 3524241a Christos Stavrakakis
class GanetiRapiClientPool(ObjectPool):
46 3524241a Christos Stavrakakis
    """Pool of Ganeti RAPI Clients."""
47 3524241a Christos Stavrakakis
48 3524241a Christos Stavrakakis
    def __init__(self, host, port, user, passwd, size=None):
49 cc92b70f Christos Stavrakakis
        log.debug("INIT: Initializing pool of size %d, host %s,"
50 3524241a Christos Stavrakakis
                  " port %d, user %s", size, host, port, user)
51 3524241a Christos Stavrakakis
        ObjectPool.__init__(self, size=size)
52 3524241a Christos Stavrakakis
        self.host = host
53 3524241a Christos Stavrakakis
        self.port = port
54 3524241a Christos Stavrakakis
        self.user = user
55 3524241a Christos Stavrakakis
        self.passwd = passwd
56 3524241a Christos Stavrakakis
57 3524241a Christos Stavrakakis
    def _pool_create(self):
58 3524241a Christos Stavrakakis
        log.debug("CREATE: Creating new client from pool %r", self)
59 3524241a Christos Stavrakakis
        client = GanetiRapiClient(self.host, self.port, self.user, self.passwd)
60 3524241a Christos Stavrakakis
        client._pool = self
61 3524241a Christos Stavrakakis
        return client
62 3524241a Christos Stavrakakis
63 3524241a Christos Stavrakakis
    def _pool_verify(self, client):
64 3524241a Christos Stavrakakis
        return True
65 3524241a Christos Stavrakakis
66 3524241a Christos Stavrakakis
    def _pool_cleanup(self, conn):
67 3524241a Christos Stavrakakis
        return False
68 3524241a Christos Stavrakakis
69 3524241a Christos Stavrakakis
70 3524241a Christos Stavrakakis
def get_rapi_client(backend_id, backend_hash, host, port, user, passwd):
71 3524241a Christos Stavrakakis
    """Get a ganeti RAPI client from pool."""
72 3524241a Christos Stavrakakis
    log.debug("GET: Getting RAPI client")
73 3524241a Christos Stavrakakis
    m = "%s cannot be None"
74 3524241a Christos Stavrakakis
    if host is None:
75 3524241a Christos Stavrakakis
        raise ValueError(m % "host")
76 3524241a Christos Stavrakakis
    if port is None:
77 3524241a Christos Stavrakakis
        raise ValueError(m % "port")
78 3524241a Christos Stavrakakis
79 3524241a Christos Stavrakakis
    # does the pool need to be created?
80 3524241a Christos Stavrakakis
    if backend_hash not in _pools:
81 3524241a Christos Stavrakakis
        log.debug("GET: No Pool. Creating new for host %s", host)
82 3524241a Christos Stavrakakis
        pool = GanetiRapiClientPool(host, port, user, passwd, pool_size)
83 3524241a Christos Stavrakakis
        _pools[backend_hash] = pool
84 ffa7f6f6 Christos Stavrakakis
        # Delete Pool for old backend_hash
85 ffa7f6f6 Christos Stavrakakis
        if backend_id in _hashes:
86 ffa7f6f6 Christos Stavrakakis
            del _pools[_hashes[backend_id]]
87 ffa7f6f6 Christos Stavrakakis
        _hashes[backend_id] = backend_hash
88 3524241a Christos Stavrakakis
89 3524241a Christos Stavrakakis
    obj = _pools[backend_hash].pool_get()
90 3524241a Christos Stavrakakis
    log.debug("GET: Got object %r from pool", obj)
91 3524241a Christos Stavrakakis
    return obj
92 3524241a Christos Stavrakakis
93 3524241a Christos Stavrakakis
94 3524241a Christos Stavrakakis
def put_rapi_client(client):
95 3524241a Christos Stavrakakis
    pool = client._pool
96 3524241a Christos Stavrakakis
    log.debug("PUT: putting client %r back to pool %r", client, pool)
97 3524241a Christos Stavrakakis
    if pool is None:
98 3524241a Christos Stavrakakis
        log.debug("PUT: client %r does not have a pool", client)
99 3524241a Christos Stavrakakis
        return
100 3524241a Christos Stavrakakis
    pool.pool_put(client)