Commit ef2e708f authored by Anael Beutot's avatar Anael Beutot
Browse files

Moved singleton metaclass in utils.py.

parent 051833c8
Loading
Loading
Loading
Loading
+2 −15
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ import pyev

from cloudcontrol.node.exc import TunnelError, DRBDAllocationError, DRBDError
from cloudcontrol.node.jobs import BaseThreadedJob
from cloudcontrol.node.utils import SocketBuffer, subproc_call
from cloudcontrol.node.utils import SocketBuffer, subproc_call, Singleton


logger = logging.getLogger(__name__)
@@ -573,23 +573,10 @@ class TCPTunnel(object):
        # logger.debug('Proccessed write event')


class Meta(type):
    """DRBDAllocator metaclass used for singleton implementation."""
    def __init__(cls, name, bases, dict):
        super(Meta, cls).__init__(cls, bases, dict)
        cls._instance = None

    def __call__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = super(Meta, cls).__call__(*args, **kwargs)

        return cls._instance


class DRBDAllocator(object):
    """Keeps a list of allocated DRBD devices."""

    __metaclass__ = Meta
    __metaclass__ = Singleton

    RMMOD = '/sbin/rmmod'
    MODPROBE = '/sbin/modprobe'
+13 −0
Original line number Diff line number Diff line
@@ -81,3 +81,16 @@ class SocketBuffer(deque):

    def is_empty(self):
        return self.current_len == 0


class Singleton(type):
    """Singleton metaclass."""
    def __init__(cls, name, bases, dict):
        super(Singleton, cls).__init__(cls, bases, dict)
        cls._instance = None

    def __call__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = super(Singleton, cls).__call__(*args, **kwargs)

        return cls._instance