paradrop.lib.utils package

Submodules

paradrop.lib.utils.addresses module

areWanPortsAvailable(portRange, takenPorts, chuteStor, name)[source]

Make sure that if we are forwarding a wan port, we have not already forwarded it.

checkPhyExists(radioid)[source]

Check if this chute exists at all, a directory /sys/class/ieee80211/phyX must exist.

getChuteIntf(name, netIntfs)[source]

This function takes a network interface name, and parses through the netIntfs object provided looking for a match, it returns name if none is found.

Example:
if ‘lan’ is the name, ‘lan’ will be returned. if ‘wifilxc’ is the name, ‘c####wifilxc’ will be returned.
This function also deals with the usage of macro expansions:
Example:

If the developer defines a ‘lan’ interface for their chute, but they also have a rule that needs to point to the HOST lan interface, a conflict will occur.

To solve this, the developer would specify the HOST lan with '@net.host.lan‘ and the chute lan with ‘lan’.

getGatewayIntf(ch)[source]

Looks at the key:networkInterfaces for the chute and determines what the gateway should be including the IP address and the internal interface name.

Returns:
A tuple (gatewayIP, gatewayInterface) None if networkInterfaces doesn’t exist or there is an error
getInternalIntfList(ch)[source]

Takes a chute object and uses the key:networkInterfaces to return a list of the internal network interfaces that will exist in the chute (e.g., eth0, eth1, ...)

Returns:
A list of interface names None if networkInterfaces doesn’t exist or there is an error
getSubnet(ip1, sn1)[source]
getWANIntf(ch)[source]

Looks at the key:networkInterfaces for the chute and finds the WAN interface.

Returns:
The dict from networkInterfaces None
incIpaddr(ipaddr, inc=1)[source]

Takes a quad dot format IP address string and adds the @inc value to it by converting it to a number.

Returns:
Incremented quad dot IP string or None if error
isIpAvailable(ipaddr, chuteStor, name)[source]

Make sure this IP address is available. Checks the IP addresses of all zones on all other chutes,

makes sure subnets are not the same.
isIpValid(ipaddr)[source]

Return True if Valid, otherwise False.

isRadioPassedthrough(radioid, chuteStor, name)[source]

Check if any chute has already passed through to a chute.

isSameSubnet(ip1, ip2)[source]
isStaMeshAvailable(chuteStor, name)[source]

Make sure this sta/mesh is available. Only one per device because we attach to wan directly.

isStaMeshOnRadio(radioid, chuteStor, name)[source]

If we are a wifi chute, we cannot have an sta on this channel.

isStaticIpAvailable(ipaddr, chuteStor, name)[source]

Make sure this static IP address is available. Checks the IP addresses of all zones on all other chutes,

makes sure not equal.
isWifiIntAvailable(radioid, numWifi, chuteStor, name)[source]

Make sure that fewer than 7 wifi interfaces have been configured. Otherwise, the wireless card will fail.

isWifiOnRadio(radioid, chuteStor, name)[source]

Make sure this sta is available. Only one per device because we attach to wan directly.

isWifiSSIDAvailable(ssid, chuteStor, name)[source]

Make sure this SSID is available.

maxIpaddr(ipaddr)[source]

Takes a quad dot format IP address string and makes it the largest valid value still in the same subnet.

Returns:
Incremented quad dot IP string or None if error

paradrop.lib.utils.dockerapi module

Functions associated with deploying and cleaning up docker containers.

build_host_config(update)[source]

Build the host_config dict for a docker container based on the passed in update.

Parameters:update (obj) – The update object containing information about the chute.
Returns:(dict) The host_config dict which docker needs in order to create the container.
failAndCleanUpDocker(validImages, validContainers)[source]

Clean up any intermediate containers that may have resulted from a failure and throw an Exception so that the abort process is called.

Parameters:
  • validImages (list) – A list of dicts containing the Id’s of all the images that should exist on the system.
  • validContainers (list) – A list of the Id’s of all the containers that should exist on the system.
Returns:

None

removeChute(update)[source]

Remove a docker container and the image it was built on based on the passed in update.

Parameters:update (obj) – The update object containing information about the chute.
Returns:None
restartChute(update)[source]

Start a docker container based on the passed in update.

Parameters:update (obj) – The update object containing information about the chute.
Returns:None
setup_net_interfaces(update)[source]

Link interfaces in the host to the internal interface in the docker container using pipework.

Parameters:update (obj) – The update object containing information about the chute.
Returns:None
startChute(update)[source]

Build and deploy a docker container based on the passed in update.

Parameters:update (obj) – The update object containing information about the chute.
Returns:None
stopChute(update)[source]

Stop a docker container based on the passed in update.

Parameters:update (obj) – The update object containing information about the chute.
Returns:None
writeDockerConfig()[source]

Write options to Docker configuration.

Mainly, we want to tell Docker not to start containers automatically on system boot.

paradrop.lib.utils.pdos module

basename(x)
copy(a, b)[source]
copytree(a, b)[source]

shutil’s copytree is dumb so use distutils.

doMount(part, mnt)[source]

This function mounts @part to @mnt.

doUnmount(mnt)[source]

This function unmounts @mnt.

exists(p)[source]
fixpath(p)[source]

This function is required because if we need to pass a path to something like tarfile, we cannot overwrite the function to fix the path, so we need to expose it somehow.

getFileType(f)[source]
getMountCmd()[source]
isMount(mnt)[source]

This function checks if @mnt is actually mounted.

isdir(a)[source]
isfile(a)[source]
ismount(p)[source]
makeExecutable(*args)[source]

The function that takes the list of files provided and sets the X bit on them.

mkdir(p)[source]
move(a, b)[source]
open(p, mode)[source]
oscall(cmd, get=False)[source]

This function performs a OS subprocess call. All output is thrown away unless an error has occured or if @get is True Arguments:

@cmd: the string command to run [get] : True means return (stdout, stderr)
Returns:
None if not @get and no error (stdout, retcode, stderr) if @get or yes error
readFile(filename, array=True, delimiter='\n')[source]

Reads in a file, the contents is NOT expected to be binary. Arguments:

@filename: absolute path to file @array : optional: return as array if true, return as string if False @delimiter: optional: if returning as a string, this str specifies what to use to join the lines
Returns:
A list of strings, separated by newlines None: if the file doesn’t exist
remove(a)[source]
syncFS()[source]
write(filename, data, mode='w')[source]

Writes out a config file to the specified location.

writeFile(filename, line, mode='a')[source]

Adds the following cfg (either str or list(str)) to this Chute’s current config file (just stored locally, not written to file.

paradrop.lib.utils.pdosq module

Quiet pdos module. Implements utility OS operations without relying on the output module. Therefore, this module can be used by output without circular dependency.

makedirs(p)[source]

Recursive directory creation (like mkdir -p). Returns True if the path is successfully created, False if it existed already, and raises an OSError on other error conditions.

paradrop.lib.utils.restart module

lib.utils.restart Contains the functions required to restart chutes properly on power cycle of device. Checks with pdconfd to make sure it was able to properly bring up all interfaces before starting chutes.

reloadChutes()[source]

This function is called to restart any chutes that were running prior to the system being restarted. It waits for pdconfd to come up and report whether or not it failed to bring up any of the interfaces that existed before the power cycle. If pdconfd indicates something failed we then force a stop update in order to bring down all interfaces associated with that chute and mark it with a warning. If the stop fails we mark the chute with a warning manually and change its state to stopped and save to storage this isn’t great as it could mean our system still has interfaces up associated with that chute. If pdconfd doesn’t report failure we attempt to start the chute and if this fails we trust the abort process to restore the system to a consistent state and we manually mark the chute as stopped and add a warning to it. :param None :returns: (list) A list of update dicts to be used to create updateObjects that should be run before accepting new updates.

updateStatus(update)[source]

This function is a callback for the updates we do upon restarting the system. It checks whether or not the update completed successfully and if not it changes the state of the chute to stopped and adds a warning. :param update: The update object containing information about the chute that was created and whether it was successful or not. :type update: obj :returns: None

paradrop.lib.utils.storage module

class PDStorage(filename, reactor, saveTimer)[source]

ParaDropStorage class.

This class is designed to be implemented by other classes. Its purpose is to make whatever data is considered important persistant to disk.

This is done by providing a reactor so a “LoopingCall” can be utilized to save to disk.

The implementer can override functions in order to implement this class:
getAttr() : Get the attr we need to save to disk setAttr() : Set the attr we got from disk importAttr(): Takes a payload and returns the properly formatted data exportAttr(): Takes the data and returns a payload attrSaveable(): Returns True if we should save this attr
attrSaveable()[source]

THIS SHOULD BE OVERRIDEN BY THE IMPLEMENTER.

exportAttr(data)[source]

By default do nothing, but expect that this function could be overwritten

importAttr(pyld)[source]

By default do nothing, but expect that this function could be overwritten

loadFromDisk()[source]

Attempts to load the data from disk. Returns True if success, False otherwise.

saveToDisk()[source]

Saves the data to disk.

paradrop.lib.utils.uci module

class UCIConfig(filepath)[source]
Wrapper around the UCI configuration files.

These files are found under /etc/config/, and are used by OpenWrt to keep track of configuration for modules typically found in /etc/init.d/

The modules of interest and with current support are:
  • firewall
  • network
  • wireless
  • qos
  • This class should work with any UCI module but ALL others are UNTESTED!

New configuration settings can be added to the UCI file via addConfig().

Each UCI config file is expected to contain the following syntax:

config keyA [valueA]
option key1 value1 ... list key2 value1 list key2 value2 ... list key3 value1 list key3 value2
Based on the UCI file above, the config syntax would look like the following:

config is a list of tuples, containing 2 dict objects in each tuple:

  • tuple[0] describes the first line (config keyA [valueA])

    {‘type’: keyA, ‘name’: valueA} The value parameter is optional and if missing, then the ‘name’ key is also missing (rather than set to None).

  • tuple[1] describes the options associated with the settings (both ‘option’ and ‘list’ lines)

    {‘key1’: ‘value1’, ...}

    If a list is present, it looks like the following:
    {

    ..., ‘list’: {

    ‘key2’: [value1, value2, ...], ‘key3’: [value1, value2, ...] }

    }

So for the example above, the full config definition would look like:
C = {‘type’: ‘keyA’, ‘name’: ‘valueA’} O = {‘key1’: ‘value1’, ‘list’: {‘key2’: [‘value1’, ‘value2’], ‘key3’: [‘value1’, ‘value2’]}} config = [(C, O)]
addConfig(config, options)[source]

Adds the tuple to our config.

addConfigs(configs)[source]

Adds a list of tuples to our config

backup(backupToken)[source]

Puts a backup of this config to the location specified in @backupPath.

checkWanConfig(internalid)[source]
delConfig(config, options)[source]

Finds a match to the config input and removes it from the internal config data structure.

delConfigs(configs)[source]

Adds a list of tuples to our config

existsConfig(config, options)[source]

Tests if the (config, options) is in the current config file.

getChuteConfigs(internalid)[source]
getConfig(config)[source]

Returns a list of call configs with the given title

getConfigIgnoreComments(config)[source]

Returns a list of call configs with the given title. Comments are ignored.

readConfig()[source]

Reads in the config file.

restore(backupToken, saveBackup=True)[source]

Replaces real file (at /etc/config/) with backup copy from /tmp/-@backupToken location.

Arguments:
backupToken: The backup token appended at the end of the backup path saveBackup : A flag to keep a backup copy or delete it (default is keep backup)
save(backupToken=None, internalid=None)[source]

Saves out the file in the proper format.

Arguments:
[backupPath] : Save a backup copy of the UCI file to the path provided.
Should be a token name like ‘backup’, it gets appended with a hyphen.
chuteConfigsMatch(chutePre, chutePost)[source]

Takes two lists of objects, and returns whether or not they are identical.

configsMatch(a, b)[source]

Takes 2 config objects, returns True if they match, False otherwise.

getSystemConfigDir()[source]
getSystemPath(filename)[source]

Get the path to the system configuration file.

This function also attempts to create the configuration directory if it does not exist.

Typical filenames: network, wireless, qos, firewall, dhcp, etc.

isMatch(a, b)[source]
isMatchIgnoreComments(a, b)[source]
setupArgParse()[source]
singleConfigMatches(a, b)[source]
stringify(a)[source]

Module contents