~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/virt/netutils.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
FLAGS = flags.FLAGS
30
30
 
 
31
flags.DECLARE('injected_network_template', 'nova.virt.disk.api')
 
32
 
 
33
Template = None
 
34
 
 
35
 
 
36
def _late_load_cheetah():
 
37
    global Template
 
38
    if Template is None:
 
39
        t = __import__('Cheetah.Template', globals(), locals(),
 
40
                       ['Template'], -1)
 
41
        Template = t.Template
 
42
 
31
43
 
32
44
def get_net_and_mask(cidr):
33
45
    net = netaddr.IPNetwork(cidr)
42
54
def get_ip_version(cidr):
43
55
    net = netaddr.IPNetwork(cidr)
44
56
    return int(net.version)
 
57
 
 
58
 
 
59
def get_injected_network_template(network_info, use_ipv6=FLAGS.use_ipv6,
 
60
                                  template=FLAGS.injected_network_template):
 
61
    """
 
62
    return a rendered network template for the given network_info
 
63
 
 
64
    :param network_info:
 
65
       :py:meth:`~nova.network.manager.NetworkManager.get_instance_nw_info`
 
66
 
 
67
    Note: this code actually depends on the legacy network_info, but will
 
68
    convert the type itself if necessary.
 
69
    """
 
70
 
 
71
    # the code below depends on the legacy 'network_info'
 
72
    if hasattr(network_info, 'legacy'):
 
73
        network_info = network_info.legacy()
 
74
 
 
75
    nets = []
 
76
    ifc_num = -1
 
77
    have_injected_networks = False
 
78
 
 
79
    for (network_ref, mapping) in network_info:
 
80
        ifc_num += 1
 
81
 
 
82
        if not network_ref['injected']:
 
83
            continue
 
84
 
 
85
        have_injected_networks = True
 
86
        address = mapping['ips'][0]['ip']
 
87
        netmask = mapping['ips'][0]['netmask']
 
88
        address_v6 = None
 
89
        gateway_v6 = None
 
90
        netmask_v6 = None
 
91
        if use_ipv6:
 
92
            address_v6 = mapping['ip6s'][0]['ip']
 
93
            netmask_v6 = mapping['ip6s'][0]['netmask']
 
94
            gateway_v6 = mapping['gateway_v6']
 
95
        net_info = {'name': 'eth%d' % ifc_num,
 
96
               'address': address,
 
97
               'netmask': netmask,
 
98
               'gateway': mapping['gateway'],
 
99
               'broadcast': mapping['broadcast'],
 
100
               'dns': ' '.join(mapping['dns']),
 
101
               'address_v6': address_v6,
 
102
               'gateway_v6': gateway_v6,
 
103
               'netmask_v6': netmask_v6}
 
104
        nets.append(net_info)
 
105
 
 
106
    if have_injected_networks is False:
 
107
        return None
 
108
 
 
109
    if not template:
 
110
        return None
 
111
 
 
112
    _late_load_cheetah()
 
113
 
 
114
    ifc_template = open(template).read()
 
115
    return str(Template(ifc_template,
 
116
                        searchList=[{'interfaces': nets,
 
117
                                     'use_ipv6': use_ipv6}]))