def configure_network(hostname, interfaces):
    update_files = {}

    # Generate new conf.d/net file
    if os.path.isfile('/sbin/rc'):
        data, ifaces = _confd_net_file(interfaces)
    else:
        data, ifaces = _confd_net_file_legacy(interfaces)

    update_files[NETWORK_FILE] = data

    # Generate new resolv.conf file
    filepath, data = commands.network.get_resolv_conf(interfaces)
    if data:
        update_files[filepath] = data

    # Generate new hostname file
    data = get_hostname_file(hostname)
    update_files[HOSTNAME_FILE] = data

    # Generate new /etc/hosts file
    filepath, data = commands.network.get_etc_hosts(interfaces, hostname)
    update_files[filepath] = data

    # Write out new files
    commands.network.update_files(update_files)

    pipe = subprocess.PIPE

    # Set hostname
    try:
        commands.network.sethostname(hostname)
    except Exception as e:
        logging.error("Couldn't sethostname(): %s" % str(e))
        return (500, "Couldn't set hostname: %s" % str(e))

    # Restart network
    for ifname in ifaces:
        if commands.utils.is_system_command("ip"):
            if not _clean_assigned_ip(ifname):
                return (500, "Couldn't flush network %s: %d" %
                        (ifname, status))
        else:
            logging.warning("Couldn't flush old network configuration as" +
                          " safeguard. Required 'ip' command not present.")

        scriptpath = '/etc/init.d/net.%s' % ifname
        if not os.path.exists(scriptpath):
            # Gentoo won't create these symlinks automatically
            os.symlink('net.lo', scriptpath)

        logging.debug('executing %s restart' % scriptpath)
        script_proc = subprocess.Popen([scriptpath, 'restart'],
                            stdin=pipe, stdout=pipe, stderr=pipe, env={})
        logging.debug('waiting on pid %d' % script_proc.pid)
        status = os.waitpid(script_proc.pid, 0)[1]
        logging.debug('status = %d' % status)


        if status != 0:
            return (500, "Couldn't restart network %s: %d" % (ifname, status))

    return (0, "")
dataset/ETHPy150Open rackerlabs/openstack-guest-agents-unix/commands/gentoo/network.py/configure_network/original