~0x44/nova/extdoc

« back to all changes in this revision

Viewing changes to tools/esx/guest_tool.py

  • Committer: Dan Prince
  • Date: 2011-09-14 15:34:08 UTC
  • mfrom: (1568 nova)
  • mto: This revision was merged to the branch mainline in revision 1570.
  • Revision ID: dan.prince@rackspace.com-20110914153408-y8iqlnz2uphraexp
Merge w/ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
81
81
 
82
82
def _parse_network_details(machine_id):
83
83
    """
84
 
    Parse the machine.id field to get MAC, IP, Netmask and Gateway fields
85
 
    machine.id is of the form MAC;IP;Netmask;Gateway;Broadcast;DNS1,DNS2
86
 
    where ';' is the separator.
 
84
    Parse the machine_id to get MAC, IP, Netmask and Gateway fields per NIC.
 
85
    machine_id is of the form ('NIC_record#NIC_record#', '')
 
86
    Each of the NIC will have record NIC_record in the form
 
87
    'MAC;IP;Netmask;Gateway;Broadcast;DNS' where ';' is field separator.
 
88
    Each record is separated by '#' from next record.
87
89
    """
 
90
    logging.debug(_("Received machine_id from vmtools : %s") % machine_id[0])
88
91
    network_details = []
89
92
    if machine_id[1].strip() == "1":
90
93
        pass
91
94
    else:
92
 
        network_info_list = machine_id[0].split(';')
93
 
        assert len(network_info_list) % 6 == 0
94
 
        no_grps = len(network_info_list) / 6
95
 
        i = 0
96
 
        while i < no_grps:
97
 
            k = i * 6
98
 
            network_details.append((
99
 
                            network_info_list[k].strip().lower(),
100
 
                            network_info_list[k + 1].strip(),
101
 
                            network_info_list[k + 2].strip(),
102
 
                            network_info_list[k + 3].strip(),
103
 
                            network_info_list[k + 4].strip(),
104
 
                            network_info_list[k + 5].strip().split(',')))
105
 
            i += 1
 
95
        for machine_id_str in machine_id[0].split('#'):
 
96
            network_info_list = machine_id_str.split(';')
 
97
            if len(network_info_list) % 6 != 0:
 
98
                break
 
99
            no_grps = len(network_info_list) / 6
 
100
            i = 0
 
101
            while i < no_grps:
 
102
                k = i * 6
 
103
                network_details.append((
 
104
                                network_info_list[k].strip().lower(),
 
105
                                network_info_list[k + 1].strip(),
 
106
                                network_info_list[k + 2].strip(),
 
107
                                network_info_list[k + 3].strip(),
 
108
                                network_info_list[k + 4].strip(),
 
109
                                network_info_list[k + 5].strip().split(',')))
 
110
                i += 1
 
111
    logging.debug(_("NIC information from vmtools : %s") % network_details)
106
112
    return network_details
107
113
 
108
114
 
279
285
 
280
286
 
281
287
def _set_rhel_networking(network_details=None):
 
288
    """Set IPv4 network settings for RHEL distros."""
282
289
    network_details = network_details or []
283
290
    all_dns_servers = []
284
291
    for network_detail in network_details:
320
327
 
321
328
 
322
329
def _set_ubuntu_networking(network_details=None):
 
330
    """Set IPv4 network settings for Ubuntu."""
323
331
    network_details = network_details or []
324
 
    """ Set IPv4 network settings for Ubuntu """
325
332
    all_dns_servers = []
326
 
    for network_detail in network_details:
 
333
    interface_file_name = '/etc/network/interfaces'
 
334
    # Remove file
 
335
    os.remove(interface_file_name)
 
336
    # Touch file
 
337
    _execute(['touch', interface_file_name])
 
338
    interface_file = open(interface_file_name, 'w')
 
339
    for device, network_detail in enumerate(network_details):
327
340
        mac_address, ip_address, subnet_mask, gateway, broadcast,\
328
341
            dns_servers = network_detail
329
342
        all_dns_servers.extend(dns_servers)
330
343
        adapter_name, current_ip_address = \
331
344
                _get_linux_adapter_name_and_ip_address(mac_address)
332
345
 
333
 
        if adapter_name and not ip_address == current_ip_address:
334
 
            interface_file_name = \
335
 
                '/etc/network/interfaces'
336
 
            # Remove file
337
 
            os.remove(interface_file_name)
338
 
            # Touch file
339
 
            _execute(['touch', interface_file_name])
340
 
            interface_file = open(interface_file_name, 'w')
 
346
        if adapter_name:
341
347
            interface_file.write('\nauto %s' % adapter_name)
342
348
            interface_file.write('\niface %s inet static' % adapter_name)
343
349
            interface_file.write('\nbroadcast %s' % broadcast)
344
350
            interface_file.write('\ngateway %s' % gateway)
345
351
            interface_file.write('\nnetmask %s' % subnet_mask)
346
 
            interface_file.write('\naddress %s' % ip_address)
347
 
            interface_file.close()
 
352
            interface_file.write('\naddress %s\n' % ip_address)
 
353
        logging.debug(_("Successfully configured NIC %d with "
 
354
                        "NIC info %s") % (device, network_detail))
 
355
    interface_file.close()
 
356
 
348
357
    if all_dns_servers:
349
358
        dns_file_name = "/etc/resolv.conf"
350
359
        os.remove(dns_file_name)
355
364
        for dns_server in unique_entries:
356
365
            dns_file.write("\nnameserver %s" % dns_server)
357
366
        dns_file.close()
358
 
    print "\nRestarting networking....\n"
 
367
 
 
368
    logging.debug(_("Restarting networking....\n"))
359
369
    _execute(['/etc/init.d/networking', 'restart'])
360
370
 
361
371