summaryrefslogtreecommitdiff
path: root/cloudinit/net/network_state.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2016-03-24 15:07:41 -0400
committerScott Moser <smoser@ubuntu.com>2016-03-24 15:07:41 -0400
commita7053201e892f6401752e17998700d027d23ea89 (patch)
tree574da3509fb196872de979e01db33aab0ca27ce4 /cloudinit/net/network_state.py
parent5eedd9e6f15a49029e00aca83f863c89fdb6d198 (diff)
parent9c0b3fc96fc33107dde8e89b02a63dbfb04e207c (diff)
downloadvyos-cloud-init-a7053201e892f6401752e17998700d027d23ea89.tar.gz
vyos-cloud-init-a7053201e892f6401752e17998700d027d23ea89.zip
ConfigDrive: convert OpenStack network_data.json to network_config.yaml
OpenStack clouds may provide network_data.json information via the MetadataService in ConfigDrive. Teach ConfigDrive to read, store and convert the data into network_config yaml format. Making this available allows cloud-init to read network config from OpenStack and use the distro.apply_network_config() method to render the network_config from OpenStack into a distro network configuration file.
Diffstat (limited to 'cloudinit/net/network_state.py')
-rw-r--r--cloudinit/net/network_state.py45
1 files changed, 44 insertions, 1 deletions
diff --git a/cloudinit/net/network_state.py b/cloudinit/net/network_state.py
index df04c526..e32d2cdf 100644
--- a/cloudinit/net/network_state.py
+++ b/cloudinit/net/network_state.py
@@ -124,6 +124,17 @@ class NetworkState:
iface = interfaces.get(command['name'], {})
for param, val in command.get('params', {}).items():
iface.update({param: val})
+
+ # convert subnet ipv6 netmask to cidr as needed
+ subnets = command.get('subnets')
+ if subnets:
+ for subnet in subnets:
+ if subnet['type'] == 'static':
+ if 'netmask' in subnet and ':' in subnet['address']:
+ subnet['netmask'] = mask2cidr(subnet['netmask'])
+ for route in subnet.get('routes', []):
+ if 'netmask' in route:
+ route['netmask'] = mask2cidr(route['netmask'])
iface.update({
'name': command.get('name'),
'type': command.get('type'),
@@ -133,7 +144,7 @@ class NetworkState:
'mtu': command.get('mtu'),
'address': None,
'gateway': None,
- 'subnets': command.get('subnets'),
+ 'subnets': subnets,
})
self.network_state['interfaces'].update({command.get('name'): iface})
self.dump_network_state()
@@ -144,6 +155,7 @@ class NetworkState:
iface eth0.222 inet static
address 10.10.10.1
netmask 255.255.255.0
+ hwaddress ether BC:76:4E:06:96:B3
vlan-raw-device eth0
'''
required_keys = [
@@ -335,6 +347,37 @@ def cidr2mask(cidr):
return ".".join([str(x) for x in mask])
+def ipv4mask2cidr(mask):
+ if '.' not in mask:
+ return mask
+ return sum([bin(int(x)).count('1') for x in mask.split('.')])
+
+
+def ipv6mask2cidr(mask):
+ if ':' not in mask:
+ return mask
+
+ bitCount = [0, 0x8000, 0xc000, 0xe000, 0xf000, 0xf800, 0xfc00, 0xfe00,
+ 0xff00, 0xff80, 0xffc0, 0xffe0, 0xfff0, 0xfff8, 0xfffc,
+ 0xfffe, 0xffff]
+ cidr = 0
+ for word in mask.split(':'):
+ if not word or int(word, 16) == 0:
+ break
+ cidr += bitCount.index(int(word, 16))
+
+ return cidr
+
+
+def mask2cidr(mask):
+ if ':' in mask:
+ return ipv6mask2cidr(mask)
+ elif '.' in mask:
+ return ipv4mask2cidr(mask)
+ else:
+ return mask
+
+
if __name__ == '__main__':
import sys
import random