def write(self): """ Write all of the configuration sections to files. """ for f in UCIBuilder.FILES: setConfig(constants.RESERVED_CHUTE_NAME, self.contents[f], uci.getSystemPath(f))
def setOSFirewallRules(update): """ Takes a list of tuples (config, opts) and saves it to the firewall config file. """ uciutils.setConfig(update, cacheKeys=['osFirewallRules', 'developerFirewallRules'], filepath=uci.getSystemPath("firewall"))
def setOSWirelessConfig(update): """ Write settings from osWirelessConfig out to UCI files. """ uciutils.setConfig(update, cacheKeys=['osWirelessConfig'], filepath=uci.getSystemPath("wireless"))
def setL3BridgeConfig(update): """ Apply configuration for layer 3 bridging. """ changed = uciutils.setConfig(update.new, update.old, cacheKeys=['parproutedConfig'], filepath=uci.getSystemPath("parprouted"))
def setVirtDHCPSettings(update): """ Takes a list of tuples (config, opts) and saves it to the dhcp config file. """ uciutils.setConfig(update, cacheKeys=['virtDHCPSettings'], filepath=uci.getSystemPath("dhcp"))
def setL3BridgeConfig(update): """ Apply configuration for layer 3 bridging. """ uciutils.setConfig( update, cacheKeys=['parproutedConfig'], filepath=uci.getSystemPath("parprouted"))
def restoreConfigFile(chute, configname): """ Restore a system config file from backup. This can only be used during a chute update operation to revert changes that were made during that update operation. configname: name of configuration file ("network", "wireless", etc.) """ filepath = uci.getSystemPath(configname) cfgFile = uci.UCIConfig(filepath) cfgFile.restore(backupToken="paradrop", saveBackup=False)
def setOSNetworkConfig(update): """ Takes a list of tuples (config, opts) and saves it to the network config file. """ # Notes: # # Takes the config generated (cache:osNetworkConfig) and uses the UCI # module to actually push it to the disk # # old code under lib.internal.chs.chutelxc same function name changed = uciutils.setConfig(update.new, update.old, cacheKeys=['osNetworkConfig'], filepath=uci.getSystemPath("network"))
def revert_dhcp_settings(update): uciutils.setConfig(update, cacheKeys=['virtDHCPSettings'], filepath=uci.getSystemPath("dhcp"))
def revert_os_wireless_config(update): uciutils.setConfig(update, cacheKeys=['osWirelessConfig'], filepath=uci.getSystemPath("wireless"))
def test_uci(): """ Test UCI file utility module """ from paradrop.lib.utils import uci from paradrop.base import settings # Test functions for finding path to UCI files settings.loadSettings(mode="unittest") assert uci.getSystemConfigDir() == "/tmp/.paradrop-test/uci/config.d/" assert uci.getSystemPath( "network") == "/tmp/.paradrop-test/uci/config.d/network" # Test stringify function assert uci.stringify("a") == "a" blob = {"a": "b"} assert uci.stringify(blob) == blob blob = {"a": {"b": "c"}} assert uci.stringify(blob) == blob blob = {"a": ["b", "c"]} assert uci.stringify(blob) == blob blob = {"a": 5} strblob = {"a": "5"} assert uci.stringify(blob) == strblob assert uci.isMatch(blob, strblob) # Write a realistic configuration and load with uci module path = writeTempFile(NETWORK_WAN_CONFIG) config = uci.UCIConfig(path) # Test if it found the config section that we know should be there empty = {} assert config.getConfig(empty) == [] match = {"type": "interface", "name": "wan", "comment": "__PARADROP__"} assert len(config.getConfig(match)) == 1 match = {"type": "interface", "name": "wan", "comment": "chute"} assert config.getConfig(match) == [] assert config.getConfigIgnoreComments(empty) == [] assert len(config.getConfigIgnoreComments(match)) == 1 # More existence tests assert not config.existsConfig(empty, empty) match_config = { "type": "interface", "name": "wan", "comment": "__PARADROP__" } match_options = {"ifname": "eth0", "proto": "dhcp"} assert config.existsConfig(match_config, match_options) # Test adding and removing config.delConfigs([(match_config, match_options)]) assert not config.existsConfig(match_config, match_options) config.addConfigs([(match_config, match_options)]) assert config.existsConfig(match_config, match_options) config.delConfig(match_config, match_options) assert not config.existsConfig(match_config, match_options) config.addConfig(match_config, match_options) assert config.existsConfig(match_config, match_options) # Get configuration by chute name assert config.getChuteConfigs("none") == [] assert len(config.getChuteConfigs("__PARADROP__")) == 1 # Test saving and reloading config.save(backupToken="backup") config2 = uci.UCIConfig(path) # Simple test for the equality operators assert config == config2 assert not (config != config2) # Test chuteConfigsMatch function assert not uci.chuteConfigsMatch(config.getChuteConfigs("__PARADROP__"), config2.getChuteConfigs("none")) assert uci.chuteConfigsMatch(config.getChuteConfigs("__PARADROP__"), config2.getChuteConfigs("__PARADROP__")) # Further test the equality operators config2.filepath = "NOMATCH" assert not (config == config2) assert config != config2 config2.filepath = config.filepath config2.myname = "NOMATCH" assert not (config == config2) assert config != config2 config2.myname = config.myname config2.config = [] assert not (config == config2) assert config != config2
def setOSWirelessConfig(update): # Basically the same as the networking version of this changed = uciutils.setConfig(update.new, update.old, cacheKeys=['osWirelessConfig'], filepath=uci.getSystemPath("wireless"))
def revert_os_network_config(update): uciutils.setConfig( update, cacheKeys=['osNetworkConfig'], filepath=uci.getSystemPath("network"))
def revert_l3_bridge_config(update): uciutils.setConfig(update.old, update.new, cacheKeys=['parproutedConfig'], filepath=uci.getSystemPath("parprouted"))
def revert_os_network_config(update): uciutils.setConfig(update.old, update.new, cacheKeys=['osNetworkConfig'], filepath=uci.getSystemPath("network"))
def revert_os_firewall_rules(update): uciutils.setConfig(update, cacheKeys=['osFirewallRules', 'developerFirewallRules'], filepath=uci.getSystemPath("firewall"))
def revert_l3_bridge_config(update): uciutils.setConfig( update, cacheKeys=['parproutedConfig'], filepath=uci.getSystemPath("parprouted"))
def test_uci(): """ Test UCI file utility module """ from paradrop.lib.utils import uci from paradrop.lib import settings # Test functions for finding path to UCI files settings.UCI_CONFIG_DIR = "/tmp/config.d" assert uci.getSystemConfigDir() == "/tmp/config.d" assert uci.getSystemPath("network") == "/tmp/config.d/network" # Test stringify function assert uci.stringify("a") == "a" blob = {"a": "b"} assert uci.stringify(blob) == blob blob = {"a": {"b": "c"}} assert uci.stringify(blob) == blob blob = {"a": ["b", "c"]} assert uci.stringify(blob) == blob blob = {"a": 5} strblob = {"a": "5"} assert uci.stringify(blob) == strblob assert uci.isMatch(blob, strblob) # Write a realistic configuration and load with uci module path = writeTempFile(NETWORK_WAN_CONFIG) config = uci.UCIConfig(path) # Test if it found the config section that we know should be there empty = {} assert config.getConfig(empty) == [] match = {"type": "interface", "name": "wan", "comment": "__PARADROP__"} assert len(config.getConfig(match)) == 1 match = {"type": "interface", "name": "wan", "comment": "chute"} assert config.getConfig(match) == [] assert config.getConfigIgnoreComments(empty) == [] assert len(config.getConfigIgnoreComments(match)) == 1 # More existence tests assert not config.existsConfig(empty, empty) match_config = { "type": "interface", "name": "wan", "comment": "__PARADROP__" } match_options = { "ifname": "eth0", "proto": "dhcp" } assert config.existsConfig(match_config, match_options) # Test adding and removing config.delConfigs([(match_config, match_options)]) assert not config.existsConfig(match_config, match_options) config.addConfigs([(match_config, match_options)]) assert config.existsConfig(match_config, match_options) config.delConfig(match_config, match_options) assert not config.existsConfig(match_config, match_options) config.addConfig(match_config, match_options) assert config.existsConfig(match_config, match_options) # Get configuration by chute name assert config.getChuteConfigs("none") == [] assert len(config.getChuteConfigs("__PARADROP__")) == 1 # Test saving and reloading config.save(backupToken="backup") config2 = uci.UCIConfig(path) # Simple test for the equality operators assert config == config2 assert not (config != config2) # Test chuteConfigsMatch function assert not uci.chuteConfigsMatch(config.getChuteConfigs("__PARADROP__"), config2.getChuteConfigs("none")) assert uci.chuteConfigsMatch(config.getChuteConfigs("__PARADROP__"), config2.getChuteConfigs("__PARADROP__")) # Further test the equality operators config2.filepath = "NOMATCH" assert not (config == config2) assert config != config2 config2.filepath = config.filepath config2.myname = "NOMATCH" assert not (config == config2) assert config != config2 config2.myname = config.myname config2.config = [] assert not (config == config2) assert config != config2
def setSystemDevices(update): """ Initialize system configuration files. Creates basic sections that all chutes require such as the "wan" interface. """ hostConfig = update.new.getCache('hostConfig') dhcpSections = list() networkSections = list() firewallSections = list() wirelessSections = list() if 'wan' in hostConfig: config = {"type": "interface", "name": "wan"} options = dict() options['ifname'] = hostConfig['wan']['interface'] options['proto'] = "dhcp" networkSections.append((config, options)) config = {"type": "zone"} options = { "name": "wan", "masq": "1", "input": "ACCEPT", "forward": "REJECT", "output": "ACCEPT", "network": "wan" } firewallSections.append((config, options)) if 'lan' in hostConfig: config = {"type": "interface", "name": "lan"} options = dict() options['type'] = "bridge" options['proto'] = 'static' options['ipaddr'] = hostConfig['lan']['ipaddr'] options['netmask'] = hostConfig['lan']['netmask'] uciutils.setList(options, 'ifname', hostConfig['lan']['interfaces']) networkSections.append((config, options)) if 'dhcp' in hostConfig['lan']: dhcp = hostConfig['lan']['dhcp'] config = {'type': 'dnsmasq'} options = {'interface': 'lan'} dhcpSections.append((config, options)) config = {'type': 'dhcp', 'name': 'lan'} options = { 'interface': 'lan', 'start': dhcp['start'], 'limit': dhcp['limit'], 'leasetime': dhcp['leasetime'] } dhcpSections.append((config, options)) if 'wifi' in hostConfig: for dev in hostConfig['wifi']: config = {"type": "wifi-device", "name": dev['interface']} options = { 'type': 'auto', 'channel': dev['channel'] } wirelessSections.append((config, options)) if 'wifi-interfaces' in hostConfig: for iface in hostConfig['wifi-interfaces']: config = {"type": "wifi-iface"} wirelessSections.append((config, iface)) setConfig(settings.RESERVED_CHUTE, dhcpSections, uci.getSystemPath("dhcp")) setConfig(settings.RESERVED_CHUTE, networkSections, uci.getSystemPath("network")) setConfig(settings.RESERVED_CHUTE, firewallSections, uci.getSystemPath("firewall")) setConfig(settings.RESERVED_CHUTE, wirelessSections, uci.getSystemPath("wireless"))