Skip to content

Commit

Permalink
Support libvirt network name and make it default
Browse files Browse the repository at this point in the history
Previously only bridge name is supported for network name.
  • Loading branch information
amotoki committed Feb 17, 2015
1 parent 9db17c7 commit fa3314d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
37 changes: 30 additions & 7 deletions easy_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,35 @@ def randomMAC():
return ':'.join(map(lambda x: "%02x" % x, mac))


def getMacAddress(network, name):
def getNetwork(network):
if network.lower() == 'NAT':
return 'NAT', 'default'
elif network.lower().startswith('pub'):
return 'PUBLIC', PUBLIC_BRIDGE

if ':' in network:
net_type, net_name = network.split(':', 1)
net_type = net_type.lower()
else:
net_type = 'net'
net_name = network
if net_type == 'br':
return 'BRIDGE', net_name
elif net_type == 'net':
return 'NETWORK', net_name
else:
print 'Unknow network_type.'
sys.exit(4)


def getMacAddress(net_type, net_name, name):
global mac_dict
if (network == PUBLIC_BRIDGE and name in mac_dict):
if (net_type == 'PUBLIC' and name in mac_dict):
mac = mac_dict[name]
print 'Use %s for nic connected to %s' % (mac, network)
print 'Use %s for nic connected to %s' % (mac, net_name)
else:
mac = randomMAC()
print 'Generate random MAC address %s for network %s' % (mac, network)
print 'Generate random MAC address %s for network %s' % (mac, net_name)
return mac


Expand All @@ -268,10 +289,12 @@ def generateLibvirtXML(args, libvirt_xml):
if len(args.nic) == 0:
args.nic.append('NAT')
for i, network in enumerate(args.nic):
mac = getMacAddress(network, args.NAME)
net_type, net_name = getNetwork(network)
mac = getMacAddress(net_type, net_name, args.NAME)
targetdev = getDeviceName(args.NAME, i)
slot = '0x%02x' % (BASE_SLOT + i)
param = {'network': network, 'mac': mac, 'slot': slot}
param = {'net_type': net_type, 'net_name': net_name,
'mac': mac, 'slot': slot}
if targetdev:
param['targetdev'] = targetdev
params['nics'].append(param)
Expand All @@ -283,7 +306,7 @@ def generateLibvirtXML(args, libvirt_xml):
f.write(tmpl.render(params))

for m in params['nics']:
msg = "%s: %s" % (m['network'], m['mac'])
msg = "%s(%s): %s" % (m['net_type'], m['net_name'], m['mac'])
if m.get('targetdev'):
msg += ' (%s)' % m['targetdev']
print msg
Expand Down
6 changes: 3 additions & 3 deletions templates/libvirt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
{% for nic in nics %}
{% if nic['network'] == 'NAT' %}
{% if nic['net_type'] == 'NAT' or nic['net_type'] == 'NETWORK' %}
<interface type='network'>
<source network='default'/>
<source network='{{nic['net_name']}}'/>
{% else %}
<interface type='bridge'>
<source bridge='{{nic['network']}}'/>
<source bridge='{{nic['net_name']}}'/>
{% endif %}
<mac address='{{nic['mac']}}'/>
<model type='virtio'/>
Expand Down

0 comments on commit fa3314d

Please sign in to comment.