예제 #1
0
 def config_file_path(self):
     current_distro = distro_detect()
     if current_distro.name in ['rhel', 'fedora']:
         return "/etc/sysconfig/network-scripts"
     elif current_distro.name == 'SuSE':
         return "/etc/sysconfig/network"
     else:
         msg = 'Distro not supported by API. Could not get interface filename.'
         LOG.error(msg)
예제 #2
0
 def config_file_path(self):
     current_distro = distro_detect()
     if current_distro.name in ["rhel", "fedora"]:
         return "/etc/sysconfig/network-scripts"
     elif current_distro.name == "SuSE":
         return "/etc/sysconfig/network"
     else:
         msg = "Distro not supported by API. Could not get interface filename."
         LOG.error(msg)
예제 #3
0
 def config_filename(self):
     current_distro = distro_detect()
     if current_distro.name in ['rhel', 'fedora']:
         path = "/etc/sysconfig/network-scripts"
     elif current_distro.name == 'SuSE':
         path = "/etc/sysconfig/network"
     else:
         msg = 'Distro not supported by API. Could not get interface filename.'
         raise NWException(msg)
     return "{}/ifcfg-{}".format(path, self.name)
예제 #4
0
 def config_filename(self):
     current_distro = distro_detect()
     if current_distro.name in ["rhel", "fedora"]:
         path = "/etc/sysconfig/network-scripts"
     elif current_distro.name == "SuSE":
         path = "/etc/sysconfig/network"
     else:
         msg = "Distro not supported by API. Could not get interface filename."
         raise NWException(msg)
     return f"{path}/ifcfg-{self.name}"
예제 #5
0
    def save(self, ipaddr, netmask):
        """Save current interface IP Address to the system configuration file.

        If the ipaddr is valid (currently being used by the interface)
        this will try to save the current settings into /etc/. This
        check is necessary to avoid inconsistency. Before save, you
        should add_ipaddr, first.

        Currently, only RHEL, Fedora and SuSE are supported. And this
        will create a backup file of your current configuration if
        found.

        :param ipaddr : IP Address which need to configure for interface
        :param netmask: Network mask which is associated to the provided IP
        """
        if ipaddr not in self.get_ipaddrs():
            msg = ('ipaddr not configured on interface. To avoid '
                   'inconsistency, please add the ipaddr first.')
            raise NWException(msg)

        current_distro = distro_detect()

        filename = "ifcfg-{}".format(self.name)
        if current_distro.name in ['rhel', 'fedora']:
            path = "/etc/sysconfig/network-scripts"
        elif current_distro.name == 'SuSE':
            path = "/etc/sysconfig/network"
        else:
            msg = 'Distro not supported by API. Could not save ipaddr.'
            raise NWException(msg)

        ifcfg_dict = {
            'TYPE': self.if_type,
            'BOOTPROTO': 'static',
            'NAME': self.name,
            'DEVICE': self.name,
            'ONBOOT': 'yes',
            'IPADDR': ipaddr,
            'NETMASK': netmask,
            'IPV6INIT': 'yes',
            'IPV6_AUTOCONF': 'yes',
            'IPV6_DEFROUTE': 'yes'
        }
        if current_distro.name == 'SuSE':
            ifcfg_dict.pop('BOOTPROTO')

        if self.if_type == 'Bond':
            ifcfg_dict['BONDING_MASTER'] = 'yes'
            bond_dict = self._get_bondinterface_details()
            ifcfg_slave_dict = {
                'SLAVE': 'yes',
                'ONBOOT': 'yes',
                'MASTER': self.name
            }
            if current_distro.name == 'SuSE':
                ifcfg_dict['BONDING_MODULE_OPTS'] = 'mode=' \
                           + bond_dict['mode'][0]
                for index, slave in enumerate(bond_dict['slaves']):
                    bonding_slave = 'BONDING_SLAVE{}'.format(index)
                    ifcfg_dict[bonding_slave] = slave
                    ifcfg_slave_dict.update({'NAME': slave, 'DEVICE': slave})
                    self._write_to_file("{}/ifcfg-{}".format(path, slave),
                                        ifcfg_slave_dict)
            elif current_distro.name in ['rhel', 'fedora']:
                ifcfg_dict['BONDING_OPTS'] = 'mode=' + bond_dict['mode'][0]
                for index, slave in enumerate(bond_dict['slaves']):
                    ifcfg_slave_dict.update({
                        'NAME': slave,
                        'DEVICE': slave,
                        'TYPE': 'Ethernet'
                    })
                    self._write_to_file("{}/ifcfg-{}".format(path, slave),
                                        ifcfg_slave_dict)
            else:
                msg = 'Distro not supported by API. Could not save ipaddr.'
                raise NWException(msg)

        self._write_to_file("{}/{}".format(path, filename), ifcfg_dict)
예제 #6
0
    def save(self, ipaddr, netmask):
        """Save current interface IP Address to the system configuration file.

        If the ipaddr is valid (currently being used by the interface)
        this will try to save the current settings into /etc/. This
        check is necessary to avoid inconsistency. Before save, you
        should add_ipaddr, first.

        Currently, only RHEL, Fedora and SuSE are supported. And this
        will create a backup file of your current configuration if
        found.

        :param ipaddr : IP Address which need to configure for interface
        :param netmask: Network mask which is associated to the provided IP
        """
        if ipaddr not in self.get_ipaddrs():
            msg = ("ipaddr not configured on interface. To avoid "
                   "inconsistency, please add the ipaddr first.")
            raise NWException(msg)

        current_distro = distro_detect()

        filename = f"ifcfg-{self.name}"
        if current_distro.name in ["rhel", "fedora"]:
            path = "/etc/sysconfig/network-scripts"
        elif current_distro.name == "SuSE":
            path = "/etc/sysconfig/network"
        else:
            msg = "Distro not supported by API. Could not save ipaddr."
            raise NWException(msg)

        ifcfg_dict = {
            "TYPE": self.if_type,
            "BOOTPROTO": "static",
            "NAME": self.name,
            "DEVICE": self.name,
            "ONBOOT": "yes",
            "IPADDR": ipaddr,
            "NETMASK": netmask,
            "IPV6INIT": "yes",
            "IPV6_AUTOCONF": "yes",
            "IPV6_DEFROUTE": "yes",
        }
        if current_distro.name == "SuSE":
            ifcfg_dict.pop("BOOTPROTO")

        if self.if_type == "Bond":
            ifcfg_dict["BONDING_MASTER"] = "yes"
            bond_dict = self._get_bondinterface_details()
            ifcfg_slave_dict = {
                "SLAVE": "yes",
                "ONBOOT": "yes",
                "MASTER": self.name
            }
            if current_distro.name == "SuSE":
                ifcfg_dict[
                    "BONDING_MODULE_OPTS"] = "mode=" + bond_dict["mode"][0]
                for index, slave in enumerate(bond_dict["slaves"]):
                    bonding_slave = f"BONDING_SLAVE{index}"
                    ifcfg_dict[bonding_slave] = slave
                    ifcfg_slave_dict.update({"NAME": slave, "DEVICE": slave})
                    self._write_to_file(f"{path}/ifcfg-{slave}",
                                        ifcfg_slave_dict)
            elif current_distro.name in ["rhel", "fedora"]:
                ifcfg_dict["BONDING_OPTS"] = "mode=" + bond_dict["mode"][0]
                for index, slave in enumerate(bond_dict["slaves"]):
                    ifcfg_slave_dict.update({
                        "NAME": slave,
                        "DEVICE": slave,
                        "TYPE": "Ethernet"
                    })
                    self._write_to_file(f"{path}/ifcfg-{slave}",
                                        ifcfg_slave_dict)
            else:
                msg = "Distro not supported by API. Could not save ipaddr."
                raise NWException(msg)

        self._write_to_file(f"{path}/{filename}", ifcfg_dict)