예제 #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()
        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', '')
예제 #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')
        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', '')
예제 #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
        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", "")
예제 #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
        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', '')
예제 #5
0
 def test_network_get_mac_addresses_failure(self):
     client = ClientTest([])
     mac_addrs = utils.list_xenstore_macaddrs(client)
     self.assertEqual(
         mac_addrs,
         [],
         'Mac addrs returned is not empty list after error'
     )
예제 #6
0
    def test_network_get_mac_addresses_exception_popen(self):
        with mock.patch('novaagent.xenstore.xenstore.Popen',
                        side_effect=ValueError):
            mac_addrs = utils.list_xenstore_macaddrs(None)

        self.assertEqual(
            mac_addrs, [],
            'Mac addrs returned is not empty list after popen exception')
예제 #7
0
 def test_network_get_mac_addresses_success(self):
     check_mac_addrs = ['BC764E206C5B', 'BC764E206C5A']
     client = ClientTest(xen_data.get_mac_addresses())
     mac_addrs = utils.list_xenstore_macaddrs(client)
     self.assertEqual(
         mac_addrs,
         check_mac_addrs,
         'Mac addrs returned do not match expected value'
     )
예제 #8
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', '')
예제 #9
0
    def test_network_get_mac_addresses_failure_popen(self):
        with mock.patch('novaagent.xenstore.xenstore.Popen') as popen:
            popen.return_value.communicate.return_value = (b'', '')
            popen.return_value.returncode = 1

            mac_addrs = utils.list_xenstore_macaddrs(None)

        self.assertEqual(
            mac_addrs, [],
            'Mac addrs returned is not empty list after popen error')
예제 #10
0
    def test_network_get_mac_addresses_success_popen(self):
        check_mac_addrs = ['BC764E206C5B', 'BC764E206C5A']
        with mock.patch('novaagent.xenstore.xenstore.Popen') as popen:
            popen.return_value.communicate.return_value = (
                utils_data.get_mac_addresses())
            popen.return_value.returncode = 0

            mac_addrs = utils.list_xenstore_macaddrs(None)

        self.assertEqual(mac_addrs, check_mac_addrs,
                         'Mac addrs returned do not match expected value')
예제 #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
        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', '')
예제 #12
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', '')
예제 #13
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', '')
예제 #14
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', '')
예제 #15
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', '')
예제 #16
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', '')
예제 #17
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', '')
예제 #18
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', '')