示例#1
0
 def normalize_inet_options(self):
     if 'method' in self.args:
         if self.args['method'] == 'static':
             if not xivo_config.plausible_static(self.args, None):
                 raise HttpReqError(415, "invalid static arguments for command")
         elif self.args['method'] == 'dhcp':
             for x in ('address', 'netmask', 'broadcast', 'gateway', 'mtu'):
                 if x in self.args:
                     del self.args[x]
     else:
         raise HttpReqError(415, "missing argument 'method'")
示例#2
0
 def normalize_inet_options(self):
     if "method" in self.args:
         if self.args["method"] == "static":
             if not xivo_config.plausible_static(self.args, None):
                 raise HttpReqError(415, "invalid static arguments for command")
         elif self.args["method"] == "dhcp":
             for x in ("address", "netmask", "broadcast", "gateway", "mtu"):
                 if x in self.args:
                     del self.args[x]
     else:
         raise HttpReqError(415, "missing argument 'method'")
示例#3
0
    def modify_eth_ipv4(self, args, options):
        """
        POST /modify_eth_ipv4

        >>> modify_eth_ipv4({'address':     '192.168.0.1',
                             'netmask':     '255.255.255.0',
                             'broadcast':   '192.168.0.255',
                             'gateway':     '192.168.0.254',
                             'mtu':         1500,
                             'auto':        True,
                             'up':          True,
                             'options':     [['dns-search', 'toto.tld tutu.tld'],
                                             ['dns-nameservers', '127.0.0.1 192.168.0.254']]},
                            {'ifname':  'eth0'})
        """
        self.args = args
        self.options = options

        eth = self._get_valid_eth_ipv4()

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

        if self.args.has_key('up'):
            if self.args['up']:
                eth['flags'] |= dumbnet.INTF_FLAG_UP
            else:
                eth['flags'] &= ~dumbnet.INTF_FLAG_UP
            del self.args['up']

        if not self.args.has_key('broadcast') and eth.has_key('broadcast'):
            del eth['broadcast']

        if not self.args.has_key('gateway') and eth.has_key('gateway'):
            del eth['gateway']

        if not self.args.has_key('mtu') and eth.has_key('mtu'):
            del eth['mtu']

        eth.update(self.args)

        eth['auto'] = self.args.get('auto', True)

        if not xivo_config.plausible_static(eth, None):
            raise HttpReqError(415, "invalid arguments for command")
        elif not os.access(self.CONFIG['interfaces_path'], (os.X_OK | os.W_OK)):
            raise HttpReqError(415, "path not found or not writable or not executable: %r" % self.CONFIG['interfaces_path'])
        elif 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'])

        conf = {'netIfaces':    {},
                'vlans':        {},
                'ipConfs':      {}}

        ret = False
        netifacesbakfile = None

        try:
            if self.CONFIG['netiface_down_cmd'] \
               and subprocess.call(self.CONFIG['netiface_down_cmd'].strip().split() + [eth['name']]) == 0 \
               and not (eth['flags'] & dumbnet.INTF_FLAG_UP):
                ret = True

            for iface in netifaces.interfaces():
                conf['netIfaces'][iface] = 'reserved'

            eth['ifname'] = eth['name']
            conf['netIfaces'][eth['name']] = eth['name']
            conf['vlans'][eth['name']] = {eth.get('vlan-id', 0): eth['name']}
            conf['ipConfs'][eth['name']] = eth

            filecontent, netifacesbakfile = self.get_interface_filecontent(conf)

            try:
                system.file_writelines_flush_sync(self.CONFIG['interfaces_file'], filecontent)

                if self.CONFIG['netiface_up_cmd'] \
                   and (eth['flags'] & dumbnet.INTF_FLAG_UP) \
                   and subprocess.call(self.CONFIG['netiface_up_cmd'].strip().split() + [eth['name']]) == 0:
                    ret = True

                if not ret:
                    if eth.has_key('gateway') and not (eth['flags'] & dumbnet.INTF_FLAG_UP):
                        del eth['gateway']
                    self.netcfg.set(eth)
            except Exception, e:
                if netifacesbakfile:
                    copy2(netifacesbakfile, self.CONFIG['interfaces_file'])
                raise e.__class__(str(e))
            return True