Пример #1
0
def get_from_path(items, path):
    """Returns a list of items matching the specified path.

    Takes an XPath-like expression e.g. prop1/prop2/prop3, and for each item
    in items, looks up items[prop1][prop2][prop3].  Like XPath, if any of the
    intermediate results are lists it will treat each list item individually.
    A 'None' in items or any child expressions will be ignored, this function
    will not throw because of None (anywhere) in items.  The returned list
    will contain no None values.

    """
    if path is None:
        raise exception.Error('Invalid mini_xpath')

    (first_token, sep, remainder) = path.partition('/')

    if first_token == '':
        raise exception.Error('Invalid mini_xpath')

    results = []

    if items is None:
        return results

    if not isinstance(items, list):
        # Wrap single objects in a list
        items = [items]

    for item in items:
        if item is None:
            continue
        get_method = getattr(item, 'get', None)
        if get_method is None:
            continue
        child = get_method(first_token)
        if child is None:
            continue
        if isinstance(child, list):
            # Flatten intermediate lists
            for x in child:
                results.append(x)
        else:
            results.append(child)

    if not sep:
        # No more tokens
        return results
    else:
        return get_from_path(results, remainder)
Пример #2
0
def get_my_linklocal(interface):
    try:
        if_str = execute('ip', '-f', 'inet6', '-o', 'addr', 'show', interface)
        condition = '\s+inet6\s+([0-9a-f:]+)/\d+\s+scope\s+link'
        links = [re.search(condition, x) for x in if_str[0].split('\n')]
        address = [w.group(1) for w in links if w is not None]
        if address[0] is not None:
            return address[0]
        else:
            raise exception.Error(
                _('Link Local address is not found.:%s') % if_str)
    except Exception as ex:
        raise exception.Error(
            _("Couldn't get Link Local IP of %(interface)s"
              " :%(ex)s") % locals())
Пример #3
0
 def create_export(self, local_path, share_name, recreate=False):
     """Create new export, delete old one if exists."""
     parser = ConfigParser.ConfigParser()
     parser.read(self.config)
     # delete old one
     if parser.has_section(share_name):
         if recreate:
             parser.remove_section(share_name)
         else:
             raise exception.Error('Section exists')
     # Create new one
     parser.add_section(share_name)
     parser.set(share_name, 'path', local_path)
     parser.set(share_name, 'browseable', 'yes')
     parser.set(share_name, 'guest ok', 'yes')
     parser.set(share_name, 'read only', 'no')
     parser.set(share_name, 'writable', 'yes')
     parser.set(share_name, 'create mask', '0755')
     parser.set(share_name, 'hosts deny', '0.0.0.0/0')  # denying all ips
     parser.set(share_name, 'hosts allow', '127.0.0.1')
     # NOTE(rushiagr): ensure that local_path dir is existing
     if not os.path.exists(local_path):
         os.makedirs(local_path)
     self._execute('chown', 'nobody', '-R', local_path, run_as_root=True)
     self._update_config(parser)
     return '//%s/%s' % (self.configuration.share_export_ip, share_name)
Пример #4
0
    def register(self, ext):
        # Do nothing if the extension doesn't check out
        if not self._check_extension(ext):
            return

        alias = ext.alias
        LOG.info(_('Loaded extension: %s'), alias)

        if alias in self.extensions:
            raise exception.Error("Found duplicate extension: %s" % alias)
        self.extensions[alias] = ext
Пример #5
0
    def __get_backend(self):
        if not self.__backend:
            backend_name = CONF[self.__pivot]
            if backend_name not in self.__backends:
                raise exception.Error(_('Invalid backend: %s') % backend_name)

            backend = self.__backends[backend_name]
            if isinstance(backend, tuple):
                name = backend[0]
                fromlist = backend[1]
            else:
                name = backend
                fromlist = backend

            self.__backend = __import__(name, None, None, fromlist)
            LOG.debug('backend %s', self.__backend)
        return self.__backend
Пример #6
0
    def _ensure_daemon_started(self):
        """
        FYI: smbd starts at least two processes.
        """
        out, _ = self._execute(*'ps -C smbd -o args='.split(),
                               check_exit_code=False)
        processes = [
            process.strip() for process in out.split('\n') if process.strip()
        ]

        cmd = 'smbd -s %s -D' % (self.config, )

        running = False
        for process in processes:
            if not process.endswith(cmd):
                # alternatively exit
                raise exception.Error('smbd already started with wrong config')
            running = True

        if not running:
            self._execute(*cmd.split(), run_as_root=True)
Пример #7
0
 def __init__(self, execute, config_object):
     super(NFSHelper, self).__init__(execute, config_object)
     try:
         self._execute('exportfs', check_exit_code=True, run_as_root=True)
     except exception.ProcessExecutionError:
         raise exception.Error('NFS server not found')