def _load_config(section, options, default_value="", filename=INI_FILE): """ Get values for some options and a given section from a config file. :param section: Section Name :param options: List of options :param default_value: Default value if an option doesn't have a value. Default is empty string. :param filename: config file. Default is INI_FILE. :return: """ results = {} if not options: return results with salt.utils.files.fopen(filename, "r") as config_file: config_parser = configparser.RawConfigParser(dict_type=CaseInsensitiveDict) config_parser.readfp(config_file) for option in options: if six.PY2: results[option] = ( _remove_quotes(config_parser.get(section, option)) if config_parser.has_option(section, option) else default_value ) else: results[option] = _remove_quotes( config_parser.get(section, option, fallback=default_value) ) return results
def __check_ethercat(self): """ Check if ethercat is installed. :return: True if ethercat is installed, otherwise False. """ if self.os_grain["lsb_distrib_id"] != "nilrt": return False with salt.utils.files.fopen("/etc/natinst/share/ni-rt.ini", "r") as config_file: config_parser = configparser.RawConfigParser( dict_type=CaseInsensitiveDict) config_parser.readfp(config_file) if six.PY2: if (config_parser.has_option("lvrt", "AdditionalNetworkProtocols") and "ethercat" in config_parser.get( "lvrt", "AdditionalNetworkProtocols").lower()): return True return False else: return ("ethercat" in config_parser.get("lvrt", "AdditionalNetworkProtocols", fallback="").lower())
def get(setting): ''' .. note:: Get one of these settings: - NoFPGAApp - NoApp - ConsoleOut - EmbeddedUI - LabVIEWAccess :param setting: Name of setting. :return: Returns value of that setting or -1 if error. CLI Example: .. code-block:: bash salt '*' startup.get noapp ''' setting = setting.strip().lower() system_settings = { 'nofpgaapp': 'NoFPGAApp.enabled', 'noapp': 'NoApp.enabled', 'consoleout': 'ConsoleOut.enabled', 'embeddedui': 'ui.enabled' } lvrt_settings = {'labviewaccess': 'RTTarget.RTProtocolAllowed'} config = configparser.RawConfigParser(dict_type=CaseInsensitiveDict) config.read(NIRTINI_PATH) if setting in system_settings: return config.get('systemsettings', system_settings[setting]).strip('\"') elif setting in lvrt_settings: return config.get('lvrt', lvrt_settings[setting]).strip('\"') return -1
def _get_requestmode_info(interface): ''' return requestmode for given interface ''' with salt.utils.files.fopen(INI_FILE, 'r') as config_file: config_parser = configparser.RawConfigParser( dict_type=CaseInsensitiveDict) config_parser.read_file(config_file) link_local_enabled = '' if not config_parser.has_option(interface, 'linklocalenabled') else \ int(_remove_quotes(config_parser.get(interface, 'linklocalenabled'))) dhcp_enabled = '' if not config_parser.has_option(interface, 'dhcpenabled') else \ int(_remove_quotes(config_parser.get(interface, 'dhcpenabled'))) if dhcp_enabled == 1: return 'dhcp_linklocal' if link_local_enabled == 1 else 'dhcp_only' else: if link_local_enabled == 1: return 'linklocal_only' if link_local_enabled == 0: return 'static' # some versions of nirtcfg don't set the dhcpenabled/linklocalenabled variables # when selecting "DHCP or Link Local" from MAX, so return it by default to avoid # having the requestmode "None" because none of the conditions above matched. return 'dhcp_linklocal'
def _get_interface_info(interface): ''' return details about given interface ''' data = { 'label': interface.name, 'connectionid': interface.name, 'up': False, 'ipv4': { 'supportedrequestmodes': ['dhcp_linklocal', 'dhcp_only', 'linklocal_only', 'static'], 'requestmode': _get_requestmode_info(interface.name) }, 'hwaddr': interface.hwaddr[:-1] } if interface.flags & IFF_RUNNING != 0: data['up'] = True data['ipv4']['address'] = interface.sockaddrToStr(interface.addr) data['ipv4']['netmask'] = interface.sockaddrToStr(interface.netmask) data['ipv4']['gateway'] = '0.0.0.0' data['ipv4']['dns'] = _get_dns_info() elif data['ipv4']['requestmode'] == 'static': with salt.utils.files.fopen(INI_FILE, 'r') as config_file: config_parser = configparser.RawConfigParser( dict_type=CaseInsensitiveDict) config_parser.read_file(config_file) data['ipv4']['address'] = _remove_quotes( config_parser.get(interface.name, 'IP_Address')) data['ipv4']['netmask'] = _remove_quotes( config_parser.get(interface.name, 'Subnet_Mask')) data['ipv4']['gateway'] = _remove_quotes( config_parser.get(interface.name, 'Gateway')) data['ipv4']['dns'] = [ _remove_quotes(config_parser.get(interface.name, 'DNS_Address')) ] with salt.utils.files.fopen('/proc/net/route', 'r') as route_file: pattern = re.compile( r'^{interface}\t[0]{{8}}\t([0-9A-Z]{{8}})'.format( interface=interface.name), re.MULTILINE) match = pattern.search(route_file.read()) iface_gateway_hex = None if not match else match.group(1) if iface_gateway_hex is not None and len(iface_gateway_hex) == 8: data['ipv4']['gateway'] = '.'.join([ str(int(iface_gateway_hex[i:i + 2], 16)) for i in range(6, -1, -2) ]) return data
def __check_ethercat(self): ''' Check if ethercat is installed. :return: True if ethercat is installed, otherwise False. ''' if self.os_grain['lsb_distrib_id'] != 'nilrt': return False with salt.utils.files.fopen('/etc/natinst/share/ni-rt.ini', 'r') as config_file: config_parser = configparser.RawConfigParser(dict_type=CaseInsensitiveDict) config_parser.readfp(config_file) if six.PY2: if config_parser.has_option('lvrt', 'AdditionalNetworkProtocols') and 'ethercat' in config_parser.get( 'lvrt', 'AdditionalNetworkProtocols').lower(): return True return False else: return 'ethercat' in config_parser.get('lvrt', 'AdditionalNetworkProtocols', fallback='').lower()