Beispiel #1
0
def read_xenstore():
    recon()
    xs_handle = pyxenstore.Handle()

    try:
        hostname = xs_handle.read(XENSTORE_HOSTNAME_PATH)
        print('hostname: %r (from xenstore)\n' % hostname)
    except pyxenstore.NotFoundError:
        hostname = DEFAULT_HOSTNAME
        print('hostname: %r (DEFAULT)\n' % hostname)

    interfaces = []

    try:
        entries = xs_handle.entries(XENSTORE_INTERFACE_PATH)
    except pyxenstore.NotFoundError:
        entries = []

    for entry in entries:
        data = xs_handle.read(XENSTORE_INTERFACE_PATH + '/' + entry)
        data = anyjson.deserialize(data)
        interfaces.append(data)
        print('interface %s: %r\n' % (entry, data))

    del xs_handle
Beispiel #2
0
    def __init__(self, *args, **kwargs):

        self.request_path = kwargs.get("request_path", XENSTORE_REQUEST_PATH)
        self.response_path = kwargs.get("response_path",
                                        XENSTORE_RESPONSE_PATH)

        self.xs_handle = pyxenstore.Handle()
        try:
            self.xs_handle.mkdir(self.request_path)
        except:
            pass

        self.requests = []
Beispiel #3
0
    def resetnetwork_cmd(self, data):

        os_mod = self.detect_os()
        if not os_mod:
            raise SystemError("Couldn't figure out my OS")

        xs_handle = pyxenstore.Handle()

        try:
            hostname = xs_handle.read(XENSTORE_HOSTNAME_PATH)
            logging.info('hostname: %r (from xenstore)' % hostname)
        except pyxenstore.NotFoundError:
            hostname = DEFAULT_HOSTNAME
            logging.info('hostname: %r (default)' % hostname)

        interfaces = []

        try:
            entries = xs_handle.entries(XENSTORE_INTERFACE_PATH)
        except pyxenstore.NotFoundError:
            entries = []

        for entry in entries:
            data = xs_handle.read(XENSTORE_INTERFACE_PATH + '/' + entry)
            data = anyjson.deserialize(data)
            interfaces.append(data)
            logging.info('interface %s: %r' % (entry, data))

        del xs_handle

        # Normalize interfaces data. It can come in a couple of different
        # (similar) formats, none of which are convenient.
        by_macaddr = dict([(mac, (up, name))
                           for name, up, mac in agentlib.get_interfaces()])

        config = {}

        for interface in interfaces:
            ifconfig = {}

            mac = interface.get('mac')
            if not mac:
                raise RuntimeError('No MAC found in config')

            # by_macaddr is keyed using lower case hexadecimal
            mac = mac.lower()

            ifconfig['mac'] = mac

            # 'label' used to be the method to determine which interface
            # this configuration applies to, but 'mac' is safer to use.
            # 'label' is now only used for printing a comment in the
            # generated configuration to easier differentiate interfaces.
            if mac not in by_macaddr:
                raise RuntimeError('Unknown interface MAC %s' % mac)

            ifconfig['label'] = interface.get('label')

            up, ifname = by_macaddr[mac]

            # Record if the interface is up already
            ifconfig['up'] = up

            # List of IPv4 and IPv6 addresses
            ip4s = interface.get('ips', [])
            ip6s = interface.get('ip6s', [])
            if not ip4s and not ip6s:
                raise RuntimeError('No IPs found for interface')

            # Gateway (especially IPv6) can be tied to an interface
            gateway4 = interface.get('gateway')
            gateway6 = interface.get('gateway6')

            # Filter out any IPs that aren't enabled
            for ip in ip4s + ip6s:
                try:
                    ip['enabled'] = int(ip.get('enabled', 0))
                except ValueError:
                    raise RuntimeError("Invalid value %r for 'enabled' key" %
                                       ip.get('enabled'))

            ip4s = filter(lambda i: i['enabled'], ip4s)
            ip6s = filter(lambda i: i['enabled'], ip6s)

            # Validate and normalize IPv4 and IPv6 addresses
            for ip in ip4s:
                if 'ip' not in ip:
                    raise RuntimeError("Missing 'ip' key for IPv4 address")
                if 'netmask' not in ip:
                    raise RuntimeError(
                        "Missing 'netmask' key for IPv4 address")

                # Rename 'ip' to 'address' to be more specific
                ip['address'] = ip.pop('ip')
                ip['prefixlen'] = NETMASK_TO_PREFIXLEN[ip['netmask']]

            for ip in ip6s:
                if 'ip' not in ip and 'address' not in ip:
                    raise RuntimeError(
                        "Missing 'ip' or 'address' key for IPv6 address")
                if 'netmask' not in ip:
                    raise RuntimeError(
                        "Missing 'netmask' key for IPv6 address")

                if 'gateway' in ip:
                    # FIXME: Should we fail if gateway6 is already set?
                    gateway6 = ip.pop('gateway')

                # FIXME: Should we fail if both 'ip' and 'address' are
                # specified but differ?

                # Rename 'ip' to 'address' to be more specific
                if 'address' not in ip:
                    ip['address'] = ip.pop('ip')

                # Rename 'netmask' to 'prefixlen' to be more accurate
                ip['prefixlen'] = ip.pop('netmask')

            ifconfig['ip4s'] = ip4s
            ifconfig['ip6s'] = ip6s

            ifconfig['gateway4'] = gateway4
            ifconfig['gateway6'] = gateway6

            # Routes are optional
            routes = interface.get('routes', [])

            # Validate and normalize routes
            for route in routes:
                if 'route' not in route:
                    raise RuntimeError("Missing 'route' key for route")
                if 'netmask' not in route:
                    raise RuntimeError("Missing 'netmask' key for route")
                if 'gateway' not in route:
                    raise RuntimeError("Missing 'gateway' key for route")

                # Rename 'route' to 'network' to be more specific
                route['network'] = route.pop('route')
                route['prefixlen'] = NETMASK_TO_PREFIXLEN[route['netmask']]

            ifconfig['routes'] = routes

            ifconfig['dns'] = interface.get('dns', [])

            config[ifname] = ifconfig

        # TODO: Should we fail if there isn't at least one gateway specified?
        #if not gateway4 and not gateway6:
        #    raise RuntimeError('No gateway found for public interface')

        return os_mod.network.configure_network(hostname, config)
Beispiel #4
0
 def _check_handle(self):
     if not self.xs_handle:
         self.xs_handle = pyxenstore.Handle()