Exemplo n.º 1
0
def persist_config_fedora_like(config):
    eth = config["eth"]
    # checkout config file
    config_file = "/etc/sysconfig/network-scripts/ifcfg-%s" % (eth)
    config_file_back = "%s.back" % (config_file)
    config_obj = {}
    config_obj["DEVICE"] = config["eth"]
    if config["type"] == "dynamic":
        config_obj["BOOTPROTO"] = "dhcp"
    else:
        config_obj["BOOTPROTO"] = "none"
        config_obj["NETMASK"] = config["mask"]
        config_obj["IPADDR"] = config["ip"]
        config_obj["PEERDNS"] = "yes"
        config_obj["DNS"] = config["dns"]
        config_obj["USERCTL"] = "no"
        config_obj["NETWORK"] = get_network_for_config(config)
        config_obj["GATEWAY"] = route.get_default_gw()

    config_obj["ONBOOT"] = "yes"

    try:
        if os.path.exists(config_file_back):
            os.remove(config_file_back)
        shutil.move(config_file, config_file_back)
        os.remove(config_file)
        f_hdl = open(config_file, "w")
        for k in iter(config_obj):
            f_hdl.write("%s=%s")
        f_hdl.close()
        logger.debug("New configuration successfully persisted")
        return 1
    except Exception, e:
        logger.error("Error occurred during persisting new configuration")
        return 0
Exemplo n.º 2
0
def test_route():
    cmd = 'ip route show'
    output = subprocess.check_output(cmd, stderr=subprocess.STDOUT,shell=True)
    match = re.search(r'default via ([^ ]+) dev ([^ ]+)', output.decode('ascii'))
    assert match, 'this test requires a default route to be present'
    assert match.group(1) == route.get_default_gw()
    assert match.group(2) == route.get_default_if()
Exemplo n.º 3
0
def test_route():
    cmd = 'ip route show'
    output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
    match = re.search(r'default via ([^ ]+) dev ([^ ]+)',
                      output.decode('ascii'))
    assert match, 'this test requires a default route to be present'
    assert match.group(1) == route.get_default_gw()
    assert match.group(2) == route.get_default_if()
Exemplo n.º 4
0
def refresh_if_config(ip, mask, gw, dns, type):
    logging.basicConfig()
    logger = logging.getLogger(get_server_logging_name())
    logger.setLevel(logging.DEBUG)
    logger.debug("Attempting to refresh if configuration of the host")
    do_persist = False  # flag indicate that the configuration should be persisted

    try:
        logger.debug("Retrieving current ip configuration")
        default_if = route.get_default_if()
        eth = Interface(default_if)
        current_ip = eth.get_ip()
        current_gw = route.get_default_gw()

        # handling ip modification
        if current_ip != ip:
            logger.debug("Need to update the ip address of the default network interface [%s]" % (default_if))
            # eth.set_ip(ip)
            do_persist = True
        else:
            logger.debug("No need to update configuration of the interface")

        # handling mask modification
        tmp_mask = IPAddress(mask)
        requested_mask = int(tmp_mask)
        if requested_mask != eth.get_netmask():
            logger.debug("Need to update the netmask")
            # eth.set_netmask(requested_mask)
            do_persist = True
        else:
            logger.debug("No need to update netmask configuration")

        # persist if necessary
        if do_persist:
            logger.debug("Persisting configuration ...")
            new_config = {}
            new_config["eth"] = default_if
            new_config["ip"] = ip
            new_config["mask"] = mask
            new_config["dns"] = dns
            new_config["gw"] = current_gw
            new_config["type"] = type  # type of configuration (static or dynamic)

            # TODO : need to check the distrib to choose the right method to persist data
            persist_config_debian_like(new_config)
            logger.debug("Network interface configuration successfully updated")
            restart_network(default_if)
        else:
            logger.debug("No need to persist the configuration")

    except Exception, ex:
        logger.error("Unable to refresh if configuration [%s]" % (ex))
Exemplo n.º 5
0
def persist_config_debian_like(config):
    logging.basicConfig()
    logger = logging.getLogger(get_server_logging_name())
    logger.setLevel(logging.DEBUG)
    logger.debug("Attempt to persist interface configuration")

    eth = config["eth"]
    config_file = "/etc/network/interfaces"
    tmp_config_file = "/tmp/interfaces_configuration.tmp"
    config_file_back = "%s.back" % (config_file)
    config_obj = {}

    # TODO : gateway should be validate ...
    if not "gw" in config:
        config["gw"] = route.get_default_gw()

    # define the top of the configuration file. These information should always be present
    top_config = "auto lo\n" "iface lo inet loopback\n\n" "auto eth0\n" "allow-hotplug eth0\n"
    if config["type"] == "dynamic":
        top_config = "%s\niface %s inet dhcp" % (top_config, eth)
    else:
        top_config = "%s\niface %s inet static" % (top_config, eth)
        # creating configuration obect ...
        config_obj["address"] = config["ip"]
        config_obj["netmask"] = config["mask"]
        config_obj["gateway"] = config["gw"]

    logger.debug("Configuration successfuly created ...")
    logger.debug("Writing interface configuration ...")

    try:
        if os.path.exists(config_file_back):
            logger.debug("Backing up old interface configuration")
            os.remove(config_file_back)

        logger.debug("replace old configuration backup")
        # shutil.move(config_file,  config_file_back)
        shutil.copy(config_file, "/tmp/interfaces_config.back")
        # os.remove(config_file)
        logger.debug("Writing new configuration")
        f_hdl = open(tmp_config_file, "w")
        f_hdl.write(top_config)
        for k in iter(config_obj):
            f_hdl.write("\n%s %s" % (k, config_obj[k]))
        f_hdl.write("\n")
        f_hdl.close()
        os.system("sudo mv %s %s" % (tmp_config_file, config_file))
        logger.debug("New configuration successfully persisted")
        return 1
    except Exception, e:
        logger.error("Error occurred during persisting new configuration [%s]" % (e))
        return 0
Exemplo n.º 6
0
	return old_url

def format_netmask(ip_str, netmask_int):
    try :
        plain_ip = "%s/%s" % (ip_str,  netmask_int)
        ip = IPNetwork(plain_ip)
        return str(ip.netmask)
    except Exception :
        return '0.0.0.0'

try :
    default_if = route.get_default_if();
    eth0 = Interface(default_if)
    ip = eth0.get_ip()
    mask = format_netmask(ip, eth0.get_netmask())
    gw = route.get_default_gw()
except Exception :
    ip = '0.0.0.0'
    mask = '0.0.0.0'
    gw = '0.0.0.0'

resolver = Resolver()
# load only the first dns but we may have up to 3 entries
dns = resolver.nameservers[0]
url = get_old_url()
response = { 'statusCode' : 0, 'statusMessage' : 'SUCCESS', 'body' : { 'ip' : ip, 'mask' : mask, 'gw' : gw, 'dns' : dns, 'url' : url.rstrip('\n') ,  'message' :  'Configuration successfully loaded' } }

# after processing sending back response as json ...
print "Content-type: application/json\n\n"
print json.dumps(response)