82
82
def _parse_network_details(machine_id):
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.
90
logging.debug(_("Received machine_id from vmtools : %s") % machine_id[0])
88
91
network_details = []
89
92
if machine_id[1].strip() == "1":
92
network_info_list = machine_id[0].split(';')
93
assert len(network_info_list) % 6 == 0
94
no_grps = len(network_info_list) / 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(',')))
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:
99
no_grps = len(network_info_list) / 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(',')))
111
logging.debug(_("NIC information from vmtools : %s") % network_details)
106
112
return network_details
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'
335
os.remove(interface_file_name)
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)
333
if adapter_name and not ip_address == current_ip_address:
334
interface_file_name = \
335
'/etc/network/interfaces'
337
os.remove(interface_file_name)
339
_execute(['touch', interface_file_name])
340
interface_file = open(interface_file_name, 'w')
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()
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)
358
print "\nRestarting networking....\n"
368
logging.debug(_("Restarting networking....\n"))
359
369
_execute(['/etc/init.d/networking', 'restart'])