Beispiel #1
0
def _write_config_file(optname, xvars):
    backupfilename = None

    if not os.path.isdir(Rcc["%s_backup_path" % optname]):
        os.makedirs(Rcc["%s_backup_path" % optname])

    if os.access(Rcc["%s_file" % optname], os.R_OK):
        backupfilename = "%s.%d" % (Rcc["%s_backup_file" % optname], time())
        copy2(Rcc["%s_file" % optname], backupfilename)

    if os.access(Rcc["%s_custom_tpl_file" % optname], (os.F_OK | os.R_OK)):
        filename = Rcc["%s_custom_tpl_file" % optname]
    else:
        filename = Rcc["%s_tpl_file" % optname]

    template_file = open(filename)
    template_lines = template_file.readlines()
    template_file.close()

    txt = txtsubst(template_lines,
                   xvars,
                   Rcc["%s_file" % optname],
                   'utf8')

    system.file_writelines_flush_sync(Rcc["%s_file" % optname], txt)

    return backupfilename
def _write_config_file(optname, xvars):
    backupfilename = None

    if not os.path.isdir(Rcc["%s_backup_path" % optname]):
        os.makedirs(Rcc["%s_backup_path" % optname])

    if os.access(Rcc["%s_file" % optname], os.R_OK):
        backupfilename = "%s.%d" % (Rcc["%s_backup_file" % optname], time())
        copy2(Rcc["%s_file" % optname], backupfilename)

    if os.access(Rcc["%s_custom_tpl_file" % optname], (os.F_OK | os.R_OK)):
        filename = Rcc["%s_custom_tpl_file" % optname]
    else:
        filename = Rcc["%s_tpl_file" % optname]

    template_file = open(filename)
    template_lines = template_file.readlines()
    template_file.close()

    txt = txtsubst(
        template_lines,
        xvars,
        Rcc["%s_file" % optname],
        'utf8',
    )

    system.file_writelines_flush_sync(Rcc["%s_file" % optname], txt)

    return backupfilename
Beispiel #3
0
def replace_simple_in_file_nolock(rules_file, match_repl_lst):
    """
    Change the lines of @rules_file using replace_simple()
    """
    system.file_writelines_flush_sync(
        rules_file + ".tmp", replace_simple(file(rules_file), match_repl_lst))
    os.rename(rules_file + ".tmp", rules_file)
Beispiel #4
0
    def modify_physical_eth_ipv4(self, args, options):
        """
        POST /modify_physical_eth_ipv4

        >>> modify_physical_eth_ipv4({'method': 'dhcp',
                                      'auto':   True},
                                     {'ifname': 'eth0'})
        """
        self.args = args
        self.options = options

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

        eth = self._get_valid_eth_ipv4()

        # allow dummy interfaces
        if not (eth['physicalif'] or eth['dummyif']):
            raise HttpReqError(415, "invalid interface, it is not a physical interface")

        self.normalize_inet_options()

        if 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'])

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

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

        netifacesbakfile = None

        try:
            if self.CONFIG['netiface_down_cmd']:
                subprocess.call(self.CONFIG['netiface_down_cmd'].strip().split() + [eth['name']])

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

            conf['netIfaces'][eth['name']] = eth['name']
            conf['vlans'][eth['name']] = {0: eth['name']}
            conf['customipConfs'][eth['name']] = self.args

            filecontent, netifacesbakfile = self.get_interface_filecontent(conf)

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

                if self.args.get('up', True) and self.CONFIG['netiface_up_cmd']:
                    subprocess.call(self.CONFIG['netiface_up_cmd'].strip().split() + [eth['name']])
            except Exception, e:
                if netifacesbakfile:
                    copy2(netifacesbakfile, self.CONFIG['interfaces_file'])
                raise e.__class__(str(e))
            return True
Beispiel #5
0
    def change_state_eth_ipv4(self, args, options):
        """
        POST /change_state_eth_ipv4

        >>> change_state_eth_ipv4({'state': True},
                                  {'ifname':    'eth0'})
        """
        self.args = args
        self.options = options

        eth = self._get_valid_eth_ipv4()

        if not xys.validate(self.args, self.CHANGE_STATE_ETH_SCHEMA):
            raise HttpReqError(415, "invalid arguments for command")
        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':            {},
                'customipConfs':    {}}

        ret = False
        netifacesbakfile = None

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

            if self.args['state']:
                eth['auto'] = True
                eth['flags'] |= dumbnet.INTF_FLAG_UP

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

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

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

            filecontent, netifacesbakfile = self.get_interface_filecontent(conf)

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

                if not ret:
                    if not self.args['state'] and eth.has_key('gateway'):
                        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
Beispiel #6
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
Beispiel #7
0
    def replace_virtual_eth_ipv4(self, args, options):
        """
        POST /replace_virtual_eth_ipv4

        >>> replace_virtual_eth_ipv4({'ifname': 'eth0:0',
                                      'method': 'dhcp',
                                      'auto':   True},
                                     {'ifname': 'eth0:0'})
        """
        self.args = args
        self.options = options
        phyifname = None
        phyinfo = None

        if 'ifname' not in self.options \
           or not isinstance(self.options['ifname'], basestring) \
           or not xivo_config.netif_managed(self.options['ifname']):
            raise HttpReqError(415, "invalid interface name, ifname: %r" % self.options['ifname'])
        elif not xys.validate(self.args, self.REPLACE_VIRTUAL_ETH_IPV4_SCHEMA):
            raise HttpReqError(415, "invalid arguments for command")

        info = self.get_netiface_info(self.options['ifname'])

        if info and info['physicalif']:
            raise HttpReqError(415, "invalid interface, it is a physical interface")
        elif network.is_alias_if(self.args['ifname']):
            phyifname = network.phy_name_from_alias_if(self.args['ifname'])
            phyinfo = self.get_netiface_info(phyifname)
            if not phyinfo or True not in (phyinfo['physicalif'], phyinfo['vlanif']):
                raise HttpReqError(415, "invalid interface, it is not an alias interface")
            elif self.args['method'] != 'static':
                raise HttpReqError(415, "invalid method, must be static")

            if 'vlanrawdevice' in self.args:
                del self.args['vlanrawdevice']
            if 'vlanid' in self.args:
                del self.args['vlanid']
        elif network.is_vlan_if(self.args['ifname']):
            if not 'vlanrawdevice' in self.args:
                raise HttpReqError(415, "invalid arguments for command, missing vlanrawdevice")
            if not 'vlanid' in self.args:
                raise HttpReqError(415, "invalid arguments for command, missing vlanid")

            phyifname = self.args['vlanrawdevice']
            phyinfo = self.get_netiface_info(phyifname)
            if not phyinfo or not phyinfo['physicalif']:
                raise HttpReqError(415, "invalid vlanrawdevice, it is not a physical interface")

            vconfig = network.get_vlan_info_from_ifname(self.args['ifname'])

            if 'vlan-id' not in vconfig:
                raise HttpReqError(415, "invalid vlan interface name")
            elif vconfig['vlan-id'] != int(self.args['vlanid']):
                raise HttpReqError(415, "invalid vlanid")
            elif vconfig.get('vlan-raw-device', self.args['vlanrawdevice']) != self.args['vlanrawdevice']:
                raise HttpReqError(415, "invalid vlanrawdevice")

            self.args['vlan-id'] = self.args.pop('vlanid')
            self.args['vlan-raw-device'] = self.args.pop('vlanrawdevice')
        else:
            raise HttpReqError(415, "invalid ifname argument for command")

        if phyinfo.get('type') != 'eth':
            raise HttpReqError(415, "invalid interface type")
        elif phyinfo.get('family') != 'inet':
            raise HttpReqError(415, "invalid address family")

        self.normalize_inet_options()

        if 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'])

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

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

        netifacesbakfile = None

        try:
            if self.CONFIG['netiface_down_cmd']:
                subprocess.call(self.CONFIG['netiface_down_cmd'].strip().split() + [self.options['ifname']])

            for iface in netifaces.interfaces():
                if self.options['ifname'] != iface:
                    conf['netIfaces'][iface] = 'reserved'

            conf['netIfaces'][self.args['ifname']] = self.args['ifname']
            conf['vlans'][self.args['ifname']] = {self.args.get('vlan-id', 0): self.args['ifname']}
            conf['customipConfs'][self.args['ifname']] = self.args

            filecontent, netifacesbakfile = self.get_interface_filecontent(conf)

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

                if self.args.get('up', True) and self.CONFIG['netiface_up_cmd']:
                    subprocess.call(self.CONFIG['netiface_up_cmd'].strip().split() + [self.args['ifname']])
            except Exception, e:
                if netifacesbakfile:
                    copy2(netifacesbakfile, self.CONFIG['interfaces_file'])
                raise e.__class__(str(e))
            return True
Beispiel #8
0
        ifname = self.options['ifname']

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

            conf['netIfaces'][ifname] = 'removed'

            if self.CONFIG['netiface_ip_delete_cmd'] \
               and subprocess.call(self.CONFIG['netiface_ip_delete_cmd'].strip().split() + [ifname]) == 0:
                ret = True

            filecontent, netifacesbakfile = self.get_interface_filecontent(conf)

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

                if ret:
                    return True
                elif not eth:
                    raise HttpReqError(404, "interface not found")

                eth['flags'] &= ~dumbnet.INTF_FLAG_UP
                if eth.has_key('gateway'):
                    del eth['gateway']
                self.netcfg.set(eth)
            except HttpReqError, e:
                raise e.__class__(e.code, e.text)
            except Exception, e:
                if netifacesbakfile:
                    copy2(netifacesbakfile, self.CONFIG['interfaces_file'])
Beispiel #9
0
def replace_simple_in_file_nolock(rules_file, match_repl_lst):
    """
    Change the lines of @rules_file using replace_simple()
    """
    system.file_writelines_flush_sync(rules_file + ".tmp", replace_simple(file(rules_file), match_repl_lst))
    os.rename(rules_file + ".tmp", rules_file)