Пример #1
0
    def resetnetwork(self, name, value):
        ifaces = {}
        xen_macs = utils.list_xenstore_macaddrs()
        for iface in utils.list_hw_interfaces():
            mac = utils.get_hw_addr(iface)
            if not mac or mac not in xen_macs:
                continue
            ifaces[iface] = utils.get_interface(mac)

        # set hostname
        utils.backup_file('/etc/hostname')
        hostname = utils.get_hostname()
        with open('/etc/hostname', 'w') as hostnamefile:
            print(hostname, file=hostnamefile)
        p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return (str(p.returncode), 'Error setting hostname: hostname')

        # setup interface files
        self._setup_loopback()
        for ifname, iface in ifaces.items():
            self._setup_interface(ifname, iface)
            p = Popen(['ifdown', ifname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
            out, err = p.communicate()
            p = Popen(['ifup', ifname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
            out, err = p.communicate()
        if p.returncode != 0:
            return (str(p.returncode), 'Error restarting network')

        return ('0', '')
Пример #2
0
    def resetnetwork(self, name, value):
        ifaces = {}
        xen_macs = utils.list_xenstore_macaddrs()
        for iface in utils.list_hw_interfaces():
            mac = utils.get_hw_addr(iface)
            if not mac or mac not in xen_macs:
                continue
            ifaces[iface] = utils.get_interface(mac)

        # set hostname
        utils.backup_file('/etc/hostname')
        hostname = utils.get_hostname()
        p = Popen(['hostnamectl', 'set-hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return (str(p.returncode), 'Error setting hostname')

        # setup interface files
        for ifname, iface in ifaces.items():
            self._setup_interface(ifname, iface)
            p = Popen(['netctl', 'restart', ifname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return (str(p.returncode), 'Error restarting network')

        return ('0', '')
Пример #3
0
    def resetnetwork(self, name, value):
        ifaces = {}
        xen_macs = utils.list_xenstore_macaddrs()
        for iface in utils.list_hw_interfaces():
            mac = utils.get_hw_addr(iface)
            if not mac or mac not in xen_macs:
                continue
            ifaces[iface] = utils.get_interface(mac)

        # set hostname
        utils.backup_file('/etc/hostname')
        utils.backup_file('/etc/conf.d/hostname')
        hostname = utils.get_hostname()
        with open('/etc/hostname', 'w') as hostnamefile:
            print(hostname, file=hostnamefile)
        with open('/etc/conf.d/hostname', 'w') as hostnamefile:
            print('HOSTNAME={0}'.format(hostname), file=hostnamefile)
        p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return (str(p.returncode), 'Error setting hostname: hostname')

        # setup interface files
        utils.backup_file('/etc/conf.d/net')
        with open('/etc/conf.d/net', 'w') as iffile:
            print('modules="iproute2"', file=iffile)
        for ifname, iface in ifaces.items():
            self._setup_interface(ifname, iface)
            p = Popen(['/etc/init.d/net.{0}'.format(ifname), 'restart'], stdout=PIPE, stderr=PIPE, stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return (str(p.returncode), 'Error restarting network')

        return ('0', '')
Пример #4
0
    def resetnetwork(self, name, value):
        ifaces = {}
        xen_macs = utils.list_xenstore_macaddrs()
        for iface in utils.list_hw_interfaces():
            mac = utils.get_hw_addr(iface)
            if not mac or mac not in xen_macs:
                continue
            ifaces[iface] = utils.get_interface(mac)

        # set hostname
        hostname = utils.get_hostname()
        p = Popen(["hostname", hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return str(p.returncode)
        self._setup_hostname(hostname)

        # setup interface files
        for ifname, iface in ifaces.items():
            self._setup_interface(ifname, iface)
        p = Popen(["service", "resolv", "restart"], stdout=PIPE, stderr=PIPE, stdin=PIPE)
        out, err = p.communicate()
        p = Popen(["service", "netif", "restart"], stdout=PIPE, stderr=PIPE, stdin=PIPE)
        out, err = p.communicate()
        p = Popen(["service", "routing", "start"], stdout=PIPE, stderr=PIPE, stdin=PIPE)
        out, err = p.communicate()

        return ("0", "")
Пример #5
0
    def resetnetwork(self, name, value, client):
        ifaces = {}
        hostname_return_code, _ = self._setup_hostname(client)
        if hostname_return_code != 0:
            log.error('Error setting hostname on system')

        xen_macs = utils.list_xenstore_macaddrs(client)
        for iface in utils.list_hw_interfaces():
            mac = utils.get_hw_addr(iface)
            if not mac or mac not in xen_macs:
                continue

            ifaces[iface] = utils.get_interface(mac, client)

        # Backup original configuration file
        utils.backup_file(self.netconfig_file)

        # Setup interfaces file
        self._setup_loopback()
        for ifname, iface in ifaces.items():
            self._setup_interfaces(ifname, iface)

        # Loop through the interfaces and restart networking
        for ifname in ifaces.keys():
            p = Popen(['ifdown', ifname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return (str(p.returncode),
                        'Error stopping network: {0}'.format(ifname))
            """
            In some cases interfaces may retain old IP addresses especially
            when building from a custom image. To prevent that we will do a
            flush of the interface before bringing it back up.

            Refer to bug:
            https://bugs.launchpad.net/ubuntu/+source/ifupdown/+bug/1584682
            """
            p = Popen(['ip', 'addr', 'flush', 'dev', ifname],
                      stdout=PIPE,
                      stderr=PIPE,
                      stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return (str(p.returncode),
                        'Error flushing network: {0}'.format(ifname))

            # Sleep for one second
            time.sleep(1)
            p = Popen(['ifup', ifname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                log.error('Error received on network restart: {0}'.format(err))
                return (str(p.returncode),
                        'Error starting network: {0}'.format(ifname))

        return ('0', '')
Пример #6
0
    def test_get_os_interfaces_netifaces(self):
        interfaces = ['lo', 'eth1', 'eth0']
        with mock.patch('novaagent.utils.os.path.exists') as os_path:
            os_path.return_value = False
            with mock.patch('novaagent.utils.netifaces.interfaces') as netif:
                netif.return_value = interfaces

                list_interfaces = utils.list_hw_interfaces()

        self.assertEqual(interfaces, list_interfaces,
                         'Interfaces returned do not match expected return')
Пример #7
0
    def resetnetwork(self, name, value):
        ifaces = {}
        xen_macs = utils.list_xenstore_macaddrs()
        for iface in utils.list_hw_interfaces():
            mac = utils.get_hw_addr(iface)
            if not mac or mac not in xen_macs:
                continue
            ifaces[iface] = utils.get_interface(mac)

        # set hostname
        utils.backup_file('/etc/sysconfig/network')
        hostname = utils.get_hostname()
        with open('/etc/sysconfig/network', 'w') as netfile:
            print('NETWORKING=yes', file=netfile)
            print('NOZEROCONF=yes', file=netfile)
            print('NETWORKING_IPV6=yes', file=netfile)
            print('HOSTNAME={0}'.format(hostname), file=netfile)
        if os.path.exists('/usr/bin/hostnamectl'):
            utils.backup_file('/etc/hostname')
            p = Popen(['hostnamectl', 'set-hostname', hostname],
                      stdout=PIPE,
                      stderr=PIPE,
                      stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return (str(p.returncode), 'Error setting hostname')
        else:
            p = Popen(['hostname', hostname],
                      stdout=PIPE,
                      stderr=PIPE,
                      stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return (str(p.returncode), 'Error setting hostname')

        # setup interface files
        for ifname, iface in ifaces.items():
            self._setup_interface(ifname, iface)
            if 'routes' in iface:
                self._setup_routes(ifname, iface)
        p = Popen(['service', 'network', 'stop'],
                  stdout=PIPE,
                  stderr=PIPE,
                  stdin=PIPE)
        p = Popen(['service', 'network', 'start'],
                  stdout=PIPE,
                  stderr=PIPE,
                  stdin=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return (str(p.returncode), 'Error restarting network')

        return ('0', '')
Пример #8
0
    def resetnetwork(self, name, value, client):
        ifaces = {}
        hostname_return_code, hostname = self._setup_hostname(client)
        if hostname_return_code != 0:
            return (str(hostname_return_code), 'Error setting hostname')

        xen_macs = utils.list_xenstore_macaddrs(client)
        for iface in utils.list_hw_interfaces():
            mac = utils.get_hw_addr(iface)

            if not mac or mac not in xen_macs:
                continue

            ifaces[iface] = utils.get_interface(mac, client)

        utils.backup_file(self.network_file)
        with open(self.network_file, 'w') as netfile:
            netfile.write('NETWORKING=yes\n')
            netfile.write('NOZEROCONF=yes\n')
            netfile.write('NETWORKING_IPV6=yes\n')
            netfile.write('HOSTNAME={0}\n'.format(hostname))

        # move unused interface files out of the way but back them up
        move_files = utils.get_ifcfg_files_to_remove(
            self.netconfig_dir, '{0}-'.format(self.interface_file_prefix))
        for interface_config in move_files:
            utils.move_file(interface_config)

        # setup interface files
        for ifname, iface in ifaces.items():
            self._setup_interface(ifname, iface)
            if 'routes' in iface:
                self._setup_routes(ifname, iface)

        if os.path.exists('/usr/bin/systemctl'):
            p = Popen(['systemctl', 'restart', 'network.service'],
                      stdout=PIPE,
                      stderr=PIPE,
                      stdin=PIPE)
        else:
            p = Popen(['service', 'network', 'restart'],
                      stdout=PIPE,
                      stderr=PIPE,
                      stdin=PIPE)

        out, err = p.communicate()
        if p.returncode != 0:
            return (str(p.returncode), 'Error restarting network')

        return ('0', '')
Пример #9
0
    def resetnetwork(self, name, value):
        ifaces = {}
        xen_macs = utils.list_xenstore_macaddrs()
        for iface in utils.list_hw_interfaces():
            mac = utils.get_hw_addr(iface)
            if not mac or mac not in xen_macs:
                continue
            ifaces[iface] = utils.get_interface(mac)

        # set hostname
        utils.backup_file('/etc/sysconfig/network')
        hostname = utils.get_hostname()
        with open('/etc/sysconfig/network', 'w') as netfile:
            print('NETWORKING=yes', file=netfile)
            print('NOZEROCONF=yes', file=netfile)
            print('NETWORKING_IPV6=yes', file=netfile)
            print('HOSTNAME={0}'.format(hostname), file=netfile)
        if os.path.exists('/usr/bin/hostnamectl'):
            utils.backup_file('/etc/hostname')
            p = Popen(['hostnamectl', 'set-hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return (str(p.returncode), 'Error setting hostname')
        else:
            p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return (str(p.returncode), 'Error setting hostname')

        # setup interface files
        for ifname, iface in ifaces.items():
            self._setup_interface(ifname, iface)
            if 'routes' in iface:
                self._setup_routes(ifname, iface)
        p = Popen(['service', 'network', 'stop'], stdout=PIPE, stderr=PIPE, stdin=PIPE)
        p = Popen(['service', 'network', 'start'], stdout=PIPE, stderr=PIPE, stdin=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return (str(p.returncode), 'Error restarting network')

        return ('0', '')
Пример #10
0
    def resetnetwork(self, name, value):
        ifaces = {}
        xen_macs = utils.list_xenstore_macaddrs()
        for iface in utils.list_hw_interfaces():
            mac = utils.get_hw_addr(iface)
            if not mac or mac not in xen_macs:
                continue
            ifaces[iface] = utils.get_interface(mac)

        # set hostname
        utils.backup_file('/etc/hostname')
        utils.backup_file('/etc/conf.d/hostname')
        hostname = utils.get_hostname()
        with open('/etc/hostname', 'w') as hostnamefile:
            print(hostname, file=hostnamefile)
        with open('/etc/conf.d/hostname', 'w') as hostnamefile:
            print('HOSTNAME={0}'.format(hostname), file=hostnamefile)
        p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return (str(p.returncode), 'Error setting hostname: hostname')

        # setup interface files
        utils.backup_file('/etc/conf.d/net')
        with open('/etc/conf.d/net', 'w') as iffile:
            print('modules="iproute2"', file=iffile)
        for ifname, iface in ifaces.items():
            self._setup_interface(ifname, iface)
            p = Popen(['/etc/init.d/net.{0}'.format(ifname), 'restart'],
                      stdout=PIPE,
                      stderr=PIPE,
                      stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return (str(p.returncode), 'Error restarting network')

        return ('0', '')
Пример #11
0
    def resetnetwork(self, name, value):
        ifaces = {}
        xen_macs = utils.list_xenstore_macaddrs()
        for iface in utils.list_hw_interfaces():
            mac = utils.get_hw_addr(iface)
            if not mac or mac not in xen_macs:
                continue
            ifaces[iface] = utils.get_interface(mac)

        # set hostname
        hostname = utils.get_hostname()
        p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return str(p.returncode)
        self._setup_hostname(hostname)

        # setup interface files
        for ifname, iface in ifaces.items():
            self._setup_interface(ifname, iface)
        p = Popen(['service', 'resolv', 'restart'],
                  stdout=PIPE,
                  stderr=PIPE,
                  stdin=PIPE)
        out, err = p.communicate()
        p = Popen(['service', 'netif', 'restart'],
                  stdout=PIPE,
                  stderr=PIPE,
                  stdin=PIPE)
        out, err = p.communicate()
        p = Popen(['service', 'routing', 'start'],
                  stdout=PIPE,
                  stderr=PIPE,
                  stdin=PIPE)
        out, err = p.communicate()

        return ('0', '')
Пример #12
0
    def resetnetwork(self, name, value):
        ifaces = {}
        xen_macs = utils.list_xenstore_macaddrs()
        for iface in utils.list_hw_interfaces():
            mac = utils.get_hw_addr(iface)
            if not mac or mac not in xen_macs:
                continue
            ifaces[iface] = utils.get_interface(mac)

        # set hostname
        hostname = utils.get_hostname()
        utils.backup_file('/etc/HOSTNAME')
        with open('/etc/HOSTNAME', 'w') as hostfile:
            print(hostname, file=hostfile)
        if os.path.exists('/usr/bin/hostnamectl'):
            utils.backup_file('/etc/hostname')
            p = Popen(['hostnamectl', 'set-hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return (str(p.returncode), 'Error setting hostname')
        else:
            p = Popen(['hostname', hostname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return (str(p.returncode), 'Error setting hostname')

        # setup interface files
        for ifname, iface in ifaces.items():
            self._setup_interface(ifname, iface)
            self._setup_routes(ifname, iface)
            self._setup_dns(ifname, iface)
        p = Popen(['systemctl', 'restart', 'network.service'], stdout=PIPE, stderr=PIPE, stdin=PIPE)
        out, err = p.communicate()
        if p.returncode != 0:
            return (str(p.returncode), 'Error restarting network')

        return ('0', '')
Пример #13
0
    def resetnetwork(self, name, value, client):
        ifaces = {}
        hostname_return_code, hostname = self._setup_hostname(client)
        if hostname_return_code != 0:
            return (str(hostname_return_code), 'Error setting hostname')

        xen_macs = utils.list_xenstore_macaddrs(client)
        for iface in utils.list_hw_interfaces():
            mac = utils.get_hw_addr(iface)
            if not mac or mac not in xen_macs:
                continue

            ifaces[iface] = utils.get_interface(mac, client)

        # Setup interface file for all interfaces
        self._setup_loopback()
        for ifname, iface in ifaces.items():
            self._setup_interface(ifname, iface)

        # Loop through the interfaces and restart networking
        for ifname in ifaces.keys():
            p = Popen(['ifdown', ifname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return (str(p.returncode),
                        'Error stopping network: {0}'.format(ifname))

            # Sleep for one second
            time.sleep(1)
            p = Popen(['ifup', ifname], stdout=PIPE, stderr=PIPE, stdin=PIPE)
            out, err = p.communicate()
            if p.returncode != 0:
                return (str(p.returncode),
                        'Error starting network: {0}'.format(ifname))

        return ('0', '')
Пример #14
0
    def resetnetwork(self, name, value, client):
        ifaces = {}
        hostname_return_code, hostname = self._setup_hostname(client)
        if hostname_return_code != 0:
            log.error(
                'Error setting hostname: {0}'.format(hostname_return_code)
            )

        xen_macs = utils.list_xenstore_macaddrs(client)
        for iface in utils.list_hw_interfaces():
            mac = utils.get_hw_addr(iface)

            if not mac or mac not in xen_macs:
                continue

            ifaces[iface] = utils.get_interface(mac, client)

        utils.backup_file(self.network_file)
        with open(self.network_file, 'w') as netfile:
            netfile.write('NETWORKING=yes\n')
            netfile.write('NOZEROCONF=yes\n')
            netfile.write('NETWORKING_IPV6=yes\n')
            netfile.write('HOSTNAME={0}\n'.format(hostname))

        # move unused interface files out of the way but back them up
        move_files = utils.get_ifcfg_files_to_remove(
            self.netconfig_dir,
            '{0}-'.format(self.interface_file_prefix)
        )
        for interface_config in move_files:
            utils.backup_file(interface_config)

        # setup interface files
        for ifname, iface in ifaces.items():
            self._setup_interface(ifname, iface)
            if 'routes' in iface:
                self._setup_routes(ifname, iface)

            """
                Creating servers from custom images may leave IP information
                from the image source. Flush the interface before restart of
                the network to clear out source image IP information
            """
            p = Popen(
                ['ip', 'addr', 'flush', 'dev', ifname],
                stdout=PIPE,
                stderr=PIPE,
                stdin=PIPE
            )
            out, err = p.communicate()
            if p.returncode != 0:
                # Log error and continue to restart network
                log.error('Error flushing interface: {0}'.format(ifname))

        if os.path.exists('/usr/bin/systemctl'):
            p = Popen(
                ['systemctl', 'restart', 'network.service'],
                stdout=PIPE,
                stderr=PIPE,
                stdin=PIPE
            )
        else:
            p = Popen(
                ['service', 'network', 'restart'],
                stdout=PIPE,
                stderr=PIPE,
                stdin=PIPE
            )

        out, err = p.communicate()
        if p.returncode != 0:
            log.error('Error received on network restart: {0}'.format(err))
            return (str(p.returncode), 'Error restarting network')

        return ('0', '')