예제 #1
0
 def test_netmask_to_prefix_24(self):
     cidr = utils.netmask_to_prefix('255.255.255.0')
     self.assertEqual(cidr, 24,
                      'Cidr returned does not match expected value')
     cidr = utils.netmask_to_prefix('255.255.0.0')
     self.assertEqual(cidr, 16,
                      'Cidr returned does not match expected value')
    def _setup_interface(self, ifname, iface):
        addrs = []
        addrs.extend(['{0}/{1}'.format(x['ip'], utils.netmask_to_prefix(x['netmask'])) for x in iface['ips']])
        if 'ip6s' in iface and iface['ip6s']:
            addrs.extend(['{ip}/{netmask}'.format(**x) for x in iface['ip6s']])
        routes = []
        gateways = []
        if 'gateway' in iface and iface['gateway']:
            gateways.append(iface['gateway'])
        if 'gateway_v6' in iface and iface['gateway_v6']:
            gateways.append(iface['gateway_v6'])
        if 'routes' in iface and iface['routes']:
            for route in iface['routes']:
                route['length'] = utils.netmask_to_prefix(route['netmask'])
                routes.append(route)

        utils.backup_file('/etc/systemd/network/{0}.network'.format(ifname))
        with open('/etc/systemd/network/{0}.network'.format(ifname), 'w') as iffile:
            print('# Label {0}'.format(iface['label']), file=iffile)
            print('[Match]\nName={0}\n'.format(ifname), file=iffile)
            print('[Network]', file=iffile)
            for x in addrs:
                print('Address={0}'.format(x), file=iffile)
            for x in gateways:
                print('Gateway={0}'.format(x), file=iffile)
            if 'dns' in iface and iface['dns']:
                print('DNS={0}'.format(' '.join(iface['dns'])), file=iffile)
            for x in routes:
                print('\n[Route]\nGateway={gateway}\nDestination={route}/{length}'.format(**route), file=iffile)
예제 #3
0
파일: debian.py 프로젝트: rmesta/nova-agent
    def _setup_netplan(self, ifaces):
        # Setup netplan file from xenstore network info
        temp_network = {
            'version': 2,
            'renderer': 'networkd',
            'ethernets': {}
        }
        for ifname, iface in ifaces.items():
            temp_net = {'addresses': [], 'dhcp4': False}
            for temp_ip in iface['ips']:
                temp_net['addresses'].append(
                    '{0}/{1}'.format(
                        temp_ip.get('ip'),
                        utils.netmask_to_prefix(temp_ip.get('netmask'))
                    )
                )

            if iface.get('gateway'):
                temp_net['gateway4'] = iface.get('gateway')

            if iface.get('ip6s'):
                temp_net['dhcp6'] = False
                temp_net['gateway6'] = iface.get('gateway_v6')
                for temp_ipv6 in iface['ip6s']:
                    temp_net['addresses'].append(
                        '{0}/{1}'.format(
                            temp_ipv6.get('ip'),
                            temp_ipv6.get('netmask')
                        )
                    )

            if iface.get('dns'):
                temp_net['nameservers'] = {
                    'addresses': iface.get('dns')
                }

            if iface.get('routes'):
                temp_net['routes'] = []
                for route in iface.get('routes'):
                    temp_route = {}
                    temp_route['to'] = '{0}/{1}'.format(
                        route.get('route'),
                        utils.netmask_to_prefix(route.get('netmask'))
                    )
                    temp_route['via'] = route.get('gateway')
                    temp_net['routes'].append(temp_route)

            temp_network['ethernets'][ifname] = temp_net

        # Setup full dictionary and write to yaml file
        netplan_data = {'network': temp_network}
        with open(self.netplan_file, 'w') as netplan_file:
            yaml.dump(netplan_data, netplan_file, default_flow_style=False)
예제 #4
0
    def _setup_interface(self, ifname, iface):
        addrs = []
        addrs.extend([
            '{0}/{1}'.format(x['ip'], utils.netmask_to_prefix(x['netmask']))
            for x in iface['ips']
        ])
        if 'ip6s' in iface and iface['ip6s']:
            addrs.extend(['{ip}/{netmask}'.format(**x) for x in iface['ip6s']])
        routes = []
        if 'gateway' in iface and iface['gateway']:
            routes.append('default via {0}'.format(iface['gateway']))
        if 'gateway_v6' in iface and iface['gateway_v6']:
            routes.append('default via {0}'.format(iface['gateway_v6']))
        if 'routes' in iface and iface['routes']:
            for route in iface['routes']:
                routes.append(
                    '{route}/{netmask} via {gateway}'.format(**route))

        with open('/etc/conf.d/net', 'a') as iffile:
            print('# Label {0}'.format(iface['label']), file=iffile)
            print('config_{0}="\n\t{1}\n"'.format(ifname, '\n\t'.join(addrs)),
                  file=iffile)
            print('routes_{0}="\n\t{1}\n"'.format(ifname, '\n\t'.join(routes)),
                  file=iffile)
            if 'dns' in iface and iface['dns']:
                print('dns_servers_{0}="\n\t{1}\n"'.format(
                    ifname, '\n\t'.join(iface['dns'])),
                      file=iffile)
    def _setup_interface(self, ifname, iface):
        addrs = []
        addrs.extend([
            '{0}/{1}'.format(x['ip'], utils.netmask_to_prefix(x['netmask']))
            for x in iface['ips']
        ])
        if 'ip6s' in iface and iface['ip6s']:
            addrs.extend(['{ip}/{netmask}'.format(**x) for x in iface['ip6s']])
        routes = []
        gateways = []
        if 'gateway' in iface and iface['gateway']:
            gateways.append(iface['gateway'])
        if 'gateway_v6' in iface and iface['gateway_v6']:
            gateways.append(iface['gateway_v6'])
        if 'routes' in iface and iface['routes']:
            for route in iface['routes']:
                route['length'] = utils.netmask_to_prefix(route['netmask'])
                routes.append(route)

        utils.backup_file('/etc/systemd/network/{0}.network'.format(ifname))
        with open('/etc/systemd/network/{0}.network'.format(ifname),
                  'w') as iffile:
            print('# Label {0}'.format(iface['label']), file=iffile)
            print('[Match]\nName={0}\n'.format(ifname), file=iffile)
            print('[Network]', file=iffile)
            for x in addrs:
                print('Address={0}'.format(x), file=iffile)
            for x in gateways:
                print('Gateway={0}'.format(x), file=iffile)
            if 'dns' in iface and iface['dns']:
                print('DNS={0}'.format(' '.join(iface['dns'])), file=iffile)
            for x in routes:
                print(
                    '\n[Route]\nGateway={gateway}\nDestination={route}/{length}'
                    .format(**route),
                    file=iffile)
예제 #6
0
    def _setup_interface(self, ifname, iface):
        addrs = []
        addrs.extend(['{0}/{1}'.format(x['ip'], utils.netmask_to_prefix(x['netmask'])) for x in iface['ips']])
        if 'ip6s' in iface and iface['ip6s']:
            addrs.extend(['{ip}/{netmask}'.format(**x) for x in iface['ip6s']])
        routes = []
        if 'gateway' in iface and iface['gateway']:
            routes.append('default via {0}'.format(iface['gateway']))
        if 'gateway_v6' in iface and iface['gateway_v6']:
            routes.append('default via {0}'.format(iface['gateway_v6']))
        if 'routes' in iface and iface['routes']:
            for route in iface['routes']:
                routes.append('{route}/{netmask} via {gateway}'.format(**route))

        with open('/etc/conf.d/net', 'a') as iffile:
            print('# Label {0}'.format(iface['label']), file=iffile)
            print('config_{0}="\n\t{1}\n"'.format(ifname, '\n\t'.join(addrs)), file=iffile)
            print('routes_{0}="\n\t{1}\n"'.format(ifname, '\n\t'.join(routes)), file=iffile)
            if 'dns' in iface and iface['dns']:
                print('dns_servers_{0}="\n\t{1}\n"'.format(ifname, '\n\t'.join(iface['dns'])), file=iffile)