예제 #1
0
 def get_routes_ipformat(self, routecfg_path):
     """
     Constructs a list of route map with key,value information
     for the below format of routes.
     :param: route file which has information in ip format
     :return: list of dictionaries of the key,value information
              of the routes.
     Ex:
     10.10.10.0/24 via 192.168.0.10 dev eth0
     172.16.1.10/32 via 192.168.0.10 dev eth0
     """
     with open(routecfg_path, "r") as routecfg_file:
         line = routecfg_file.read()
         cfg_map = []
         try:
             each_route = [x for x in (y.split() for y in line.split('\n'))
                           if x]
             for options in each_route:
                 if len(options) >= 3:
                     route_info = {'ADDRESS': options[0].split('/')[0],
                                   'NETMASK': options[0].split('/')[1],
                                   'GATEWAY': options[2]}
                     if metric in options and options[3] == metric:
                         route_info['METRIC'] = options[4]
                     elif metric in options and options[5] == metric:
                         route_info['METRIC'] = options[6]
                     cfg_map.append(route_info)
                 else:
                     wok_log.warn("Skipping the invalid route information" +
                                  encode_value(options))
         except Exception, e:
             raise OperationFailed("GINNET0030E", {'err': e.message})
         return cfg_map
예제 #2
0
 def activate(self, ifacename):
     wok_log.info('Bring up an interface ' + ifacename)
     iface_type = netinfo.get_interface_type(ifacename)
     if iface_type == "Ethernet":
         cmd_ipup = ['ip', 'link', 'set', '%s' % ifacename, 'up']
         out, error, returncode = run_command(cmd_ipup)
         if returncode != 0:
             wok_log.error(
                 'Unable to bring up the interface on ' + ifacename +
                 ', ' + error)
             raise OperationFailed('GINNET0059E', {'name': ifacename,
                                                   'error': error})
         # Some times based on system load, it takes few seconds to
         # reflect the  /sys/class/net files upon execution of 'ip link
         # set' command.  Following snippet is to wait util files get
         # reflect.
         timeout = self.wait_time(ifacename)
         if timeout == 5:
             wok_log.warn("Time-out has happened upon execution of 'ip "
                          "link set <interface> up', hence behavior of "
                          "activating an interface may not as expected.")
         else:
             wok_log.info(
                 'Successfully brought up the interface ' + ifacename)
     wok_log.info('Activating an interface ' + ifacename)
     cmd_ifup = ['ifup', '%s' % ifacename]
     out, error, returncode = run_command(cmd_ifup)
     if returncode != 0:
         wok_log.error(
             'Unable to activate the interface on ' + ifacename +
             ', ' + error)
         raise OperationFailed('GINNET0016E',
                               {'name': ifacename, 'error': error})
     wok_log.info(
         'Connection successfully activated for the interface ' + ifacename)
예제 #3
0
 def get_list(self):
     nics = cfgInterfacesHelper.get_interface_list()
     nics_with_ifcfgfile = []
     for iface in nics:
         filename = ifcfg_filename_format % iface
         fileexist = os.path.isfile(os.sep + network_configpath + filename)
         if not fileexist:
             wok_log.warn('ifcfg file not exist for' ' interface :' + iface)
         else:
             nics_with_ifcfgfile.append(iface)
     return sorted(map(decode_value, nics_with_ifcfgfile))
예제 #4
0
 def activate_iface(self, ifacename):
     wok_log.info('Bring up an interface ' + ifacename)
     iface_type = netinfo.get_interface_type(ifacename)
     if iface_type == "nic":
         cmd_ipup = ['ip', 'link', 'set', '%s' % ifacename, 'up']
         out, error, returncode = run_command(cmd_ipup)
         if returncode != 0:
             # non-ascii encoded value and unicode value
             # cannot be concatenated, so convert both variable
             # to one format.
             raise OperationFailed('GINNET0059E', {
                 'name': encode_value(ifacename),
                 'error': encode_value(error)
             })
         # Some times based on system load, it takes few seconds to
         # reflect the  /sys/class/net files upon execution of 'ip link
         # set' command.  Following snippet is to wait util files get
         # reflect.
         timeout = self.wait_time(ifacename)
         if timeout == 5:
             wok_log.warn("Time-out has happened upon execution of 'ip "
                          "link set <interface> up', hence behavior of "
                          "activating an interface may not as expected.")
         else:
             wok_log.info('Successfully brought up the interface ' +
                          ifacename)
     wok_log.info('Activating an interface ' + ifacename)
     cmd_ifup = ['ifup', ifacename]
     out, error, returncode = run_command(cmd_ifup)
     if (returncode == 4):
         raise OperationFailed('GINNET0095E', {'name': ifacename})
     # Timeout is used for carrier file
     # since the carrier file needs few seconds approx 5 sec
     # to update the carrier value of an iface from 0 to 1.
     self.wait_time_carrier(ifacename)
     # Check for the carrier value after the device is activated
     if os.path.isfile(carrier_path % ifacename):
         with open(carrier_path % ifacename) as car_file:
             carrier_val = car_file.readline().strip()
         if (carrier_val == '0'):
             if iface_type != "nic":
                 raise OperationFailed('GINNET0094E', {'name': ifacename})
             else:
                 raise OperationFailed('GINNET0090E', {'name': ifacename})
     else:
         raise OperationFailed('GINNET0091E', {'name': ifacename})
     if returncode != 0:
         raise OperationFailed('GINNET0016E', {
             'name': encode_value(ifacename),
             'error': encode_value(error)
         })
     wok_log.info('Connection successfully activated for the interface ' +
                  ifacename)
예제 #5
0
 def deactivate_iface(self, ifacename):
     filename = ifcfg_filename_format % ifacename
     filepath = os.sep + network_configpath + filename
     wok_log.info('Deactivating an interface ' + ifacename)
     if os.path.exists(filepath):
         cmd_ifdown = ['ifdown', '%s' % ifacename]
         out, error, returncode = run_command(cmd_ifdown)
         if returncode != 0:
             raise OperationFailed('GINNET0017E', {
                 'name': encode_value(ifacename),
                 'error': encode_value(error)
             })
         wok_log.info(
             'Connection successfully deactivated for the interface ' +
             ifacename)
     wok_log.info('Bringing down an interface ' + ifacename)
     iface_type = netinfo.get_interface_type(ifacename)
     if iface_type == "nic":
         cmd_ipdown = ['ip', 'link', 'set', '%s' % ifacename, 'down']
         out, error, returncode = run_command(cmd_ipdown)
         if returncode != 0:
             raise OperationFailed('GINNET0060E', {
                 'name': encode_value(ifacename),
                 'error': encode_value(error)
             })
         # Some times based on system load, it takes few seconds to
         # reflect the  /sys/class/net files upon execution of 'ip link
         # set' command. Following snippet is to wait util files get
         # reflect.
         timeout = self.wait_time(ifacename)
         if timeout == 5:
             wok_log.warn("Time-out has happened upon execution of 'ip "
                          "link set <interface> down', hence behavior "
                          "of activating an interface may not as "
                          "expected.")
         else:
             wok_log.info('Successfully brought down the interface ' +
                          ifacename)
     vlan_or_bond = [
         nw_cfginterfaces_utils.IFACE_BOND,
         nw_cfginterfaces_utils.IFACE_VLAN
     ]
     if iface_type in vlan_or_bond:
         if encode_value(ifacename) in ethtool.get_devices():
             cmd_ip_link_del = ['ip', 'link', 'delete', '%s' % ifacename]
             out, error, returncode = run_command(cmd_ip_link_del)
             if (returncode != 0 and
                 (encode_value(ifacename) in ethtool.get_devices())):
                 raise OperationFailed(
                     'GINNET0017E', {
                         'name': encode_value(ifacename),
                         'error': encode_value(error)
                     })
예제 #6
0
 def get_list(self):
     nics = cfgInterfacesHelper.get_interface_list()
     nics_with_ifcfgfile = []
     for iface in nics:
         filename = ifcfg_filename_format % iface
         fileexist = os.path.isfile(os.sep + network_configpath + filename)
         if not fileexist:
             wok_log.warn('ifcfg file not exist for'
                          ' interface :' + iface)
         else:
             nics_with_ifcfgfile.append(iface)
     return sorted(map(decode_value, nics_with_ifcfgfile))
예제 #7
0
 def activate_iface(self, ifacename):
     wok_log.info('Bring up an interface ' + ifacename)
     iface_type = netinfo.get_interface_type(ifacename)
     if iface_type == "nic":
         cmd_ipup = ['ip', 'link', 'set', '%s' % ifacename, 'up']
         out, error, returncode = run_command(cmd_ipup)
         if returncode != 0:
             # non-ascii encoded value and unicode value
             # cannot be concatenated, so convert both variable
             # to one format.
             raise OperationFailed('GINNET0059E',
                                   {'name': encode_value(ifacename),
                                    'error': encode_value(error)})
         # Some times based on system load, it takes few seconds to
         # reflect the  /sys/class/net files upon execution of 'ip link
         # set' command.  Following snippet is to wait util files get
         # reflect.
         timeout = self.wait_time(ifacename)
         if timeout == 5:
             wok_log.warn("Time-out has happened upon execution of 'ip "
                          "link set <interface> up', hence behavior of "
                          "activating an interface may not as expected.")
         else:
             wok_log.info('Successfully brought up the interface ' +
                          ifacename)
     wok_log.info('Activating an interface ' + ifacename)
     cmd_ifup = ['ifup', ifacename]
     out, error, returncode = run_command(cmd_ifup)
     # Timeout is used for carrier file
     # since the carrier file needs few seconds approx 5 sec
     # to update the carrier value of an iface from 0 to 1.
     self.wait_time_carrier(ifacename)
     # Check for the carrier value after the device is activated
     if os.path.isfile(carrier_path % ifacename):
         with open(carrier_path % ifacename) as car_file:
             carrier_val = car_file.readline().strip()
         if (carrier_val == '0'):
             if iface_type != "nic":
                 raise OperationFailed('GINNET0094E', {'name': ifacename})
             else:
                 raise OperationFailed('GINNET0090E', {'name': ifacename})
     else:
         raise OperationFailed('GINNET0091E', {'name': ifacename})
     if returncode != 0:
         raise OperationFailed('GINNET0016E',
                               {'name': encode_value(ifacename),
                                'error': encode_value(error)})
     wok_log.info(
         'Connection successfully activated for the interface ' + ifacename)
예제 #8
0
 def get_list(self):
     nics = cfgInterfacesHelper.get_interface_list()
     # To handle issue https://github.com/kimchi-project/ginger/issues/99
     # cfginterface resource model deals with interface which has config
     # files.remove interface from interface list if ifcfg file not exist.
     nics_with_ifcfgfile = []
     for iface in nics:
         filename = ifcfg_filename_format % iface
         fileexist = os.path.isfile(os.sep + network_configpath + filename)
         if not fileexist:
             wok_log.warn('ifcfg file not exist for'
                          ' interface :' + iface)
         else:
             nics_with_ifcfgfile.append(iface)
     return sorted(nics_with_ifcfgfile)
예제 #9
0
 def get_list(self):
     nics = cfgInterfacesHelper.get_interface_list()
     # To handle issue https://github.com/kimchi-project/ginger/issues/99
     # cfginterface resource model deals with interface which has config
     # files.remove interface from interface list if ifcfg file not exist.
     nics_with_ifcfgfile = []
     for iface in nics:
         filename = ifcfg_filename_format % iface
         fileexist = os.path.isfile(os.sep + network_configpath + filename)
         if not fileexist:
             wok_log.warn('ifcfg file not exist for' ' interface :' + iface)
         else:
             nics_with_ifcfgfile.append(iface)
     # Ensure comparison are done in same type.
     return sorted(map(decode_value, nics_with_ifcfgfile))
예제 #10
0
    def deactivate_iface(self, ifacename):
        wok_log.info('Deactivating an interface ' + ifacename)
        cmd_ifdown = ['ifdown', '%s' % ifacename]
        out, error, returncode = run_command(cmd_ifdown)
        if returncode != 0:
            wok_log.error(
                'Unable to deactivate the interface on ' + ifacename +
                ', ' + error)
            raise OperationFailed('GINNET0017E',
                                  {'name': ifacename, 'error': error})
        wok_log.info(
            'Connection successfully deactivated for the interface ' +
            ifacename)

        wok_log.info('Bringing down an interface ' + ifacename)
        iface_type = netinfo.get_interface_type(ifacename)
        if iface_type == "Ethernet":
            cmd_ipdown = ['ip', 'link', 'set', '%s' % ifacename, 'down']
            out, error, returncode = run_command(cmd_ipdown)
            if returncode != 0:
                wok_log.error(
                    'Unable to bring down the interface on ' + ifacename +
                    ', ' + error)
                raise OperationFailed('GINNET0060E', {'name': ifacename,
                                                      'error': error})
            # Some times based on system load, it takes few seconds to
            # reflect the  /sys/class/net files upon execution of 'ip link
            # set' command. Following snippet is to wait util files get
            # reflect.
            timeout = self.wait_time(ifacename)
            if timeout == 5:
                wok_log.warn("Time-out has happened upon execution of 'ip "
                             "link set <interface> down', hence behavior of "
                             "activating an interface may not as expected.")
            else:
                wok_log.info('Successfully brought down the interface ' +
                             ifacename)
        vlan_or_bond = [nw_cfginterfaces_utils.IFACE_BOND,
                        nw_cfginterfaces_utils.IFACE_VLAN]
        if iface_type in vlan_or_bond:
            if ifacename in ethtool.get_devices():
                cmd_ip_link_del = ['ip', 'link', 'delete', '%s' % ifacename]
                out, error, returncode = run_command(cmd_ip_link_del)
                if returncode != 0 and ifacename in ethtool.get_devices():
                    wok_log.error('Unable to delete the interface ' +
                                  ifacename + ', ' + error)
                raise OperationFailed('GINNET0017E', {'name': ifacename,
                                                      'error': error})
예제 #11
0
 def deactivate_iface(self, ifacename):
     filename = ifcfg_filename_format % ifacename
     filepath = os.sep + network_configpath + filename
     wok_log.info('Deactivating an interface ' + ifacename)
     if os.path.exists(filepath):
         cmd_ifdown = ['ifdown', '%s' % ifacename]
         out, error, returncode = run_command(cmd_ifdown)
         if returncode != 0:
             raise OperationFailed('GINNET0017E',
                                   {'name': encode_value(ifacename),
                                    'error': encode_value(error)})
         wok_log.info(
             'Connection successfully deactivated for the interface ' +
             ifacename)
     wok_log.info('Bringing down an interface ' + ifacename)
     iface_type = netinfo.get_interface_type(ifacename)
     if iface_type == "nic":
         cmd_ipdown = ['ip', 'link', 'set', '%s' % ifacename, 'down']
         out, error, returncode = run_command(cmd_ipdown)
         if returncode != 0:
             raise OperationFailed('GINNET0060E',
                                   {'name': encode_value(ifacename),
                                    'error': encode_value(error)})
         # Some times based on system load, it takes few seconds to
         # reflect the  /sys/class/net files upon execution of 'ip link
         # set' command. Following snippet is to wait util files get
         # reflect.
         timeout = self.wait_time(ifacename)
         if timeout == 5:
             wok_log.warn("Time-out has happened upon execution of 'ip "
                          "link set <interface> down', hence behavior "
                          "of activating an interface may not as "
                          "expected.")
         else:
             wok_log.info('Successfully brought down the interface ' +
                          ifacename)
     vlan_or_bond = [nw_cfginterfaces_utils.IFACE_BOND,
                     nw_cfginterfaces_utils.IFACE_VLAN]
     if iface_type in vlan_or_bond:
         if encode_value(ifacename) in ethtool.get_devices():
             cmd_ip_link_del = ['ip', 'link', 'delete', '%s' % ifacename]
             out, error, returncode = run_command(cmd_ip_link_del)
             if (returncode != 0 and
                (encode_value(ifacename) in ethtool.get_devices())):
                 raise OperationFailed('GINNET0017E',
                                       {'name': encode_value(ifacename),
                                        'error': encode_value(error)})
예제 #12
0
 def get_routes_ipformat(self, routecfg_path):
     """
     Constructs a list of route map with key,value information
     for the below format of routes.
     :param: route file which has information in ip format
     :return: list of dictionaries of the key,value information
              of the routes.
     Ex:
     10.10.10.0/24 via 192.168.0.10 dev eth0
     172.16.1.10/32 via 192.168.0.10 dev eth0
     """
     with open(routecfg_path, "r") as routecfg_file:
         line = routecfg_file.read()
         cfg_map = []
         try:
             each_route = [
                 x for x in (y.split() for y in line.split('\n')) if x
             ]
             for options in each_route:
                 if len(options) >= 3:
                     route_info = {
                         'ADDRESS': options[0].split('/')[0],
                         'NETMASK': options[0].split('/')[1],
                         'GATEWAY': options[2]
                     }
                     if metric in options and options[3] == metric:
                         route_info['METRIC'] = options[4]
                     elif metric in options and options[5] == metric:
                         route_info['METRIC'] = options[6]
                     cfg_map.append(route_info)
                 else:
                     wok_log.warn("Skipping the invalid route information" +
                                  encode_value(options))
         except Exception, e:
             raise OperationFailed("GINNET0030E", {'err': e.message})
         return cfg_map