Skip to content

Commit fa3314d

Browse files
committed
Support libvirt network name and make it default
Previously only bridge name is supported for network name.
1 parent 9db17c7 commit fa3314d

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

easy_deploy.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,35 @@ def randomMAC():
235235
return ':'.join(map(lambda x: "%02x" % x, mac))
236236

237237

238-
def getMacAddress(network, name):
238+
def getNetwork(network):
239+
if network.lower() == 'NAT':
240+
return 'NAT', 'default'
241+
elif network.lower().startswith('pub'):
242+
return 'PUBLIC', PUBLIC_BRIDGE
243+
244+
if ':' in network:
245+
net_type, net_name = network.split(':', 1)
246+
net_type = net_type.lower()
247+
else:
248+
net_type = 'net'
249+
net_name = network
250+
if net_type == 'br':
251+
return 'BRIDGE', net_name
252+
elif net_type == 'net':
253+
return 'NETWORK', net_name
254+
else:
255+
print 'Unknow network_type.'
256+
sys.exit(4)
257+
258+
259+
def getMacAddress(net_type, net_name, name):
239260
global mac_dict
240-
if (network == PUBLIC_BRIDGE and name in mac_dict):
261+
if (net_type == 'PUBLIC' and name in mac_dict):
241262
mac = mac_dict[name]
242-
print 'Use %s for nic connected to %s' % (mac, network)
263+
print 'Use %s for nic connected to %s' % (mac, net_name)
243264
else:
244265
mac = randomMAC()
245-
print 'Generate random MAC address %s for network %s' % (mac, network)
266+
print 'Generate random MAC address %s for network %s' % (mac, net_name)
246267
return mac
247268

248269

@@ -268,10 +289,12 @@ def generateLibvirtXML(args, libvirt_xml):
268289
if len(args.nic) == 0:
269290
args.nic.append('NAT')
270291
for i, network in enumerate(args.nic):
271-
mac = getMacAddress(network, args.NAME)
292+
net_type, net_name = getNetwork(network)
293+
mac = getMacAddress(net_type, net_name, args.NAME)
272294
targetdev = getDeviceName(args.NAME, i)
273295
slot = '0x%02x' % (BASE_SLOT + i)
274-
param = {'network': network, 'mac': mac, 'slot': slot}
296+
param = {'net_type': net_type, 'net_name': net_name,
297+
'mac': mac, 'slot': slot}
275298
if targetdev:
276299
param['targetdev'] = targetdev
277300
params['nics'].append(param)
@@ -283,7 +306,7 @@ def generateLibvirtXML(args, libvirt_xml):
283306
f.write(tmpl.render(params))
284307

285308
for m in params['nics']:
286-
msg = "%s: %s" % (m['network'], m['mac'])
309+
msg = "%s(%s): %s" % (m['net_type'], m['net_name'], m['mac'])
287310
if m.get('targetdev'):
288311
msg += ' (%s)' % m['targetdev']
289312
print msg

templates/libvirt.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
3434
</controller>
3535
{% for nic in nics %}
36-
{% if nic['network'] == 'NAT' %}
36+
{% if nic['net_type'] == 'NAT' or nic['net_type'] == 'NETWORK' %}
3737
<interface type='network'>
38-
<source network='default'/>
38+
<source network='{{nic['net_name']}}'/>
3939
{% else %}
4040
<interface type='bridge'>
41-
<source bridge='{{nic['network']}}'/>
41+
<source bridge='{{nic['net_name']}}'/>
4242
{% endif %}
4343
<mac address='{{nic['mac']}}'/>
4444
<model type='virtio'/>

0 commit comments

Comments
 (0)