Ejemplo n.º 1
0
    def __init__(self, root=None):
        Observable.__init__(self)
        if not root:
            root = bus.etc_path
        self._logger = logging.getLogger(__name__)

        self._chkdir(root)
        priv_path = os.path.join(root, 'private.d')
        self._chkdir(priv_path)
        pub_path = os.path.join(root, 'public.d')
        self._chkdir(pub_path)

        self._root_path = root
        self._priv_path = priv_path
        self._pub_path = pub_path
        if not bus.config:
            bus.config = ConfigParser()
        self.ini = ScalarizrIni(bus.config)

        self._explored_keys = dict()
        self.explore_key(self.DEFAULT_KEY, 'Scalarizr crypto key', True)
        self.explore_key(self.FARM_KEY, 'Farm crypto key', True)

        self._loaded_ini_names = set()

        self.define_events(
                # Fires when modules must apply user-data to configuration
                # @param cnf: This configuration object
                'apply_user_data'
        )
Ejemplo n.º 2
0
 def __init__(self):
     Observable.__init__(self)
     self.filters = {'data': [], 'protocol': []}
     self.define_events(
         # Fires before message is send
         "before_send",
         # Fires after message is send
         "send",
         # Fires when error occures
         "send_error")
Ejemplo n.º 3
0
 def __init__(self):
     Observable.__init__(self)
     self.filters = {
             'data' : [],
             'protocol' : []
     }
     self.define_events(
             # Fires before message is send
             "before_send",
             # Fires after message is send
             "send",
             # Fires when error occures
             "send_error"
     )
Ejemplo n.º 4
0
class Storage:
    maintain_volume_table = False
    providers = {}
    default_vol_provider = None
    default_snap_provider = None

    _fs_drivers = {}

    _obs = Observable()
    _obs.define_events('attach', 'detach', 'destroy')
    on, un, fire = _obs.on, _obs.un, _obs.fire

    volumes = dict()

    @staticmethod
    def volume_table():
        if Storage.maintain_volume_table:
            from scalarizr.bus import bus
            conn = bus.db
            cur = conn.cursor()
            try:
                cur.execute('SELECT * FROM storage')
                return cur.fetchall()
            finally:
                cur.close()
        else:
            return ()

    @staticmethod
    def lookup_filesystem(fstype):
        self = Storage
        if fstype in self._fs_drivers:
            return self._fs_drivers[fstype]
        try:
            mod = __import__('scalarizr.storage.fs.%s' % fstype, globals(),
                             locals(), ('__filesystem__', ), -1)
            self._fs_drivers[fstype] = mod.__filesystem__()
            return self.lookup_filesystem(fstype)
        except ImportError:
            raise LookupError('Unknown filesystem %s' % fstype)

    @staticmethod
    def explore_filesystem(fstype, FileSystemClass):
        self = Storage
        self._fs_drivers[fstype] = FileSystemClass()

    @staticmethod
    def explore_provider(PvdClass,
                         default_for_vol=False,
                         default_for_snap=False):
        self = Storage
        type = PvdClass.type
        self.providers[type] = PvdClass
        if default_for_vol:
            self.default_vol_provider = type
        if default_for_snap:
            self.default_snap_provider = type

    @staticmethod
    def lookup_provider(pvd_type=None, for_snap=False):
        '''
        XXX: for_snap confuse
        '''
        self = Storage

        if not pvd_type:
            pvd_type = self.default_snap_provider if for_snap else self.default_vol_provider
        try:
            pvd = self.providers[pvd_type]
        except KeyError:
            try:
                __import__('scalarizr.storage.' + pvd_type,
                           globals=globals(),
                           locals=locals())
                pvd = self.providers[pvd_type]
            except (ImportError, KeyError):
                raise LookupError('Unknown volume provider "%s"' %
                                  (pvd_type, ))
        if hasattr(pvd, '__bases__'):
            pvd = pvd()
            self.providers[pvd_type] = pvd
        return pvd

    @staticmethod
    def get(vol_id):
        try:
            return Storage.volumes[vol_id]
        except KeyError:
            raise StorageError('Unknown volume with id="%s"' % vol_id)

    @staticmethod
    def create(*args, **kwargs):
        '''
        @raise LookupError: When volume provider cannot be resolved
        @raise StorageError: General error for all cases
        '''
        self = Storage
        args = list(args) if args else list()
        kwargs = kwargs.copy() if kwargs else dict()
        from_snap = False

        if args:
            if isinstance(args[0], dict):
                kwargs = args[0]
            else:
                kwargs['device'] = args[0]

        id = kwargs.get('id')
        if id in self.volumes:
            return self.volumes[id]

        if 'snapshot' in kwargs:
            # Save original kwargs
            from_snap = True
            orig_kwargs = kwargs.copy()

            # Set kwargs to snapshot kwargs
            kwargs = kwargs['snapshot'].config() if isinstance(
                kwargs['snapshot'], Snapshot) else kwargs['snapshot']
            if not isinstance(kwargs, dict):
                args = None
                kwargs = dict(device=kwargs)
            if kwargs['id']:
                kwargs['snapshot_id'] = kwargs['id']
            # Update kwargs with original one
            del orig_kwargs['snapshot']
            kwargs.update(orig_kwargs)

        if 'disks' in kwargs:
            disks = []
            errors = []
            threads = []

            def _create(self, i, item):
                try:
                    disk = self.create(**item) if isinstance(
                        item, dict) else self.create(item)
                    disks.append((i, disk))
                except:
                    e = sys.exc_info()[0]
                    errors.append(e)

            for i, item in enumerate(kwargs['disks']):
                if isinstance(item, Volume):
                    disks.append((i, item))
                    continue

                t = threading.Thread(target=_create, args=(self, i, item))
                t.start()
                threads.append(t)

            for t in threads:
                t.join()

            if errors:
                raise StorageError('Failed to attach disks:\n%s' %
                                   '\n'.join([str(e) for e in errors]))

            disks.sort(lambda x, y: cmp(x[0], y[0]))
            disks = [disk for i, disk in disks]

            kwargs['disks'] = disks

        if 'disk' in kwargs:
            disk = kwargs['disk']
            if not isinstance(disk, Volume):
                kwargs['disk'] = self.create(
                    **disk) if isinstance(disk, dict) else self.create(disk)

        # Find provider
        pvd = self.lookup_provider(kwargs.get('type'), from_snap)
        attaching = kwargs.get('device') and not os.path.exists(
            kwargs['device'])
        vol = getattr(
            pvd, 'create_from_snapshot' if from_snap else 'create').__call__(
                **kwargs)
        if attaching:
            Storage.fire('attach', vol)

        self.volumes[vol.id] = vol
        return vol

    @staticmethod
    def detach(vol, force=False):
        Storage.fire('detach', vol)
        id = vol.id
        pvd = Storage.lookup_provider(vol.type)
        ret = pvd.detach(vol, force)
        vol.detached = True
        if id in Storage.volumes:
            del Storage.volumes[id]
        return ret

    @staticmethod
    def destroy(vol, force=False, **kwargs):
        id = vol.id
        Storage.fire('destroy', vol)
        pvd = Storage.lookup_provider(vol.type)
        pvd.destroy(vol, force, **kwargs)
        if id in Storage.volumes:
            del Storage.volumes[id]

    @staticmethod
    def backup_config(cnf, filename):
        logger.debug('Backuping volume config {id: %s, type: %s ...} into %s',
                     cnf.get('id'), cnf.get('type'), filename)
        fp = open(filename, 'w+')
        try:
            fp.write(json.dumps(cnf, indent=4))
        finally:
            fp.close()

    @staticmethod
    def restore_config(filename):
        fp = open(filename, 'r')
        try:
            ret = json.load(fp)
            return dict(
                zip(list(key.encode("ascii") for key in ret.keys()),
                    ret.values()))
        finally:
            fp.close()

    @staticmethod
    def blank_config(cnf):
        cnf = cnf.copy()
        cnf.pop('id', None)
        pvd = Storage.lookup_provider(cnf['type'])
        pvd.blank_config(cnf)
        return cnf
Ejemplo n.º 5
0
 def __init__(self):
     Observable.__init__(self, 'before_send', 'send', 'send_error')
     self.filters = {'data': [], 'protocol': []}