コード例 #1
0
ファイル: resolvconf.py プロジェクト: Eyepea/xivo-skaro
def ResolvConf(args, options):    # pylint: disable-msg=W0613
    """
    POST /resolv_conf

    >>> resolv_conf({'nameservers': '192.168.0.254'})
    >>> resolv_conf({'nameservers': ['192.168.0.254', '10.0.0.254']})
    >>> resolv_conf({'search': ['toto.tld', 'tutu.tld']
                     'nameservers': ['192.168.0.254', '10.0.0.254']})
    """

    if 'nameservers' in args:
        args['nameservers'] = helpers.extract_scalar(args['nameservers'])
        nameservers = helpers.unique_case_tuple(args['nameservers'])

        if len(nameservers) == len(args['nameservers']):
            args['nameservers'] = list(nameservers)
        else:
            raise HttpReqError(415, "duplicated nameservers in %r" % list(args['nameservers']))

    if 'search' in args:
        args['search'] = helpers.extract_scalar(args['search'])
        search = helpers.unique_case_tuple(args['search'])

        if len(search) == len(args['search']):
            args['search'] = list(search)
        else:
            raise HttpReqError(415, "duplicated search in %r" % list(args['search']))

        if len(''.join(args['search'])) > 255:
            raise HttpReqError(415, "maximum length exceeded for option search: %r" % list(args['search']))

    if not xys.validate(args, RESOLVCONF_SCHEMA):
        raise HttpReqError(415, "invalid arguments for command")

    if not os.access(Rcc['resolvconf_path'], (os.X_OK | os.W_OK)):
        raise HttpReqError(415, "path not found or not writable or not executable: %r" % Rcc['resolvconf_path'])

    if not RESOLVCONFLOCK.acquire_read(Rcc['lock_timeout']):
        raise HttpReqError(503, "unable to take RESOLVCONFLOCK for reading after %s seconds" % Rcc['lock_timeout'])

    resolvconfbakfile = None

    try:
        try:
            resolvconfbakfile = _write_config_file('resolvconf',
                                                   _resolv_conf_variables(args))
            return True
        except Exception, e:
            if resolvconfbakfile:
                copy2(resolvconfbakfile, Rcc['resolvconf_file'])
            raise e.__class__(str(e))
    finally:
        RESOLVCONFLOCK.release()
コード例 #2
0
ファイル: dnetintf.py プロジェクト: Eyepea/xivo-skaro
    def netiface(self, args, options):
        """
        GET /netiface

        >>> netiface({}, {})
        >>> netiface({}, {'ifname': 'eth0'})
        >>> netiface({}, {'ifname': {0: 'eth0', 1: 'eth1'}})
        >>> netiface({}, {'ifname': ['eth0', 'eth1']})
        """
        self.args = args
        self.options = options

        if 'ifname' in self.options:
            ifaces = helpers.extract_scalar(self.options['ifname'])
            if not ifaces:
                raise HttpReqError(415, "invalid option 'ifname'")
        else:
            return self.discover_netifaces({}, {})

        if not self.LOCK.acquire_read(self.CONFIG['lock_timeout']):
            raise HttpReqError(503, "unable to take LOCK for reading after %s seconds" % self.CONFIG['lock_timeout'])

        try:
            self.inetxparser.reloadfile()
            if len(ifaces) == 1:
                return self.get_netiface_info(ifaces[0])

            return dict((iface, self.get_netiface_info(iface)) for iface in ifaces)
        finally:
            self.LOCK.release()
コード例 #3
0
ファイル: dnetintf.py プロジェクト: Eyepea/xivo-skaro
    def _netiface_from_address(self, function):
        """
        Find best interface from destination or source address.
        """
        if 'address' in self.options:
            addresses = helpers.extract_scalar(self.options['address'])
        else:
            addresses = None

        if not addresses:
            raise HttpReqError(415, "invalid option 'address'")
        else:
            try:
                addresses = [dumbnet.addr(x) for x in addresses]
            except ValueError, e:
                raise HttpReqError(415, "%s: %s" % (e, x))
コード例 #4
0
ファイル: packages.py プロジェクト: Eyepea/xivo-skaro
    def _get_dependency_level(self):
        if 'level' in self.options:
            self.options['level'] = helpers.extract_scalar(self.options['level'])

            if not helpers.exists_in_list(self.options['level'],
                                          PACKAGES_DEPENDENCY_LEVELS):
                raise HttpReqError(415, "invalid option 'level'")

            self.opts['level'] = helpers.extract_exists_in_list(self.options['level'],
                                                                PACKAGES_LIST[self.reqpkg].keys())

            if not self.opts['level']:
                return
        else:
            self.opts['level'] = helpers.extract_exists_in_list(PACKAGES_LIST[self.reqpkg].keys(),
                                                                PACKAGES_DEPENDENCY_LEVELS)
コード例 #5
0
ファイル: lshw.py プロジェクト: Eyepea/xivo-skaro
def Lshw(args, options):    # pylint: disable-msg=W0613
    """
    GET /lshw
    
    Just returns the list hardware

    >>> lshw({}, {'class':  'network'})
    >>> lshw({}, {'class':  {0: 'network', 1: 'system'}})
    >>> lshw({}, {'class':  ['network', 'system']})
    """

    opts = {}

    if 'class' in options:
        options['class'] = helpers.extract_scalar(options['class'])

        if helpers.exists_in_list(options['class'], LSHW_CLASS_LIST):
            opts['class'] = options['class']
        else:
            raise HttpReqError(415, "invalid option 'class'")

    if 'disable' in options:
        options['disable'] = helpers.extract_scalar(options['disable'])

        if helpers.exists_in_list(options['disable'], LSHW_TEST_LIST):
            opts['disable'] = options['disable']
        else:
            raise HttpReqError(415, "invalid option 'disable'")

    if 'enable' in options:
        options['enable'] = helpers.extract_scalar(options['enable'])

        if helpers.exists_in_list(options['enable'], LSHW_TEST_LIST):
            opts['enable'] = options['enable']
        else:
            raise HttpReqError(415, "invalid option 'enable'")

    if 'sanitize' in options:
        opts['sanitize'] = None

    if 'numeric' in options:
        opts['numeric'] = None

    if not LSHWLOCK.acquire_read(LSHW_LOCK_TIMEOUT):
        raise HttpReqError(503, "unable to take LSHWLOCK for reading after %s seconds" % LSHW_LOCK_TIMEOUT)

    lshw_cmd = [LSHW_BIN, '-xml']

    for key, value in opts.iteritems():
        if isinstance(value, (tuple, list)):
            for x in value:
                lshw_cmd.append("-%s" % key)
                lshw_cmd.append(x)
        else:
            lshw_cmd.append("-%s" % key)

            if value is not None:
                lshw_cmd.append(value)

    try:
        lshw = subprocess.Popen(lshw_cmd,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,
                                close_fds=True)

        if lshw.wait():
            log.error(lshw.stderr.read())
            raise LshwExecutionError("A problem occurred while executing command. (command: %r, error: %r)"
                                     % (lshw_cmd, lshw.stderr.read()))

        data = lshw.stdout.read()
        match = LSHW_RE_XML_DECLARATION(data)

        # XXX Workaround when missing XML top-level
        if match:
            xml = "%s<lshw>%s</lshw>" % (match.group(1), match.group(2))
        else:
            xml = "<lshw>%s</lshw>" % data

        return xml2dict.Parse(xml)
    finally:
        LSHWLOCK.release()