Added
Link Here
|
1 |
This patch depends on the follow PR from the upsteram project |
2 |
https://github.com/GoogleCloudPlatform/compute-image-packages/pull/442 |
3 |
|
4 |
--- google_compute_engine/ip_forwarding/ip_forwarding_utils.py.orig 2017-07-18 16:43:14 UTC |
5 |
+++ google_compute_engine/ip_forwarding/ip_forwarding_utils.py |
6 |
@@ -17,6 +17,8 @@ |
7 |
|
8 |
import re |
9 |
import subprocess |
10 |
+import netifaces |
11 |
+import netaddr |
12 |
|
13 |
IP_REGEX = re.compile(r'\A(\d{1,3}\.){3}\d{1,3}\Z') |
14 |
IP_ALIAS_REGEX = re.compile(r'\A(\d{1,3}\.){3}\d{1,3}/\d{1,2}\Z') |
15 |
@@ -51,8 +53,8 @@ class IpForwardingUtils(object): |
16 |
options.update(kwargs) |
17 |
return options |
18 |
|
19 |
- def _RunIpRoute(self, args=None, options=None): |
20 |
- """Run a command with ip route and return the response. |
21 |
+ def _RunIfconfig(self, args=None, options=None): |
22 |
+ """Run a command with ifconfig and return the response. |
23 |
|
24 |
Args: |
25 |
args: list, the string ip route command args to execute. |
26 |
@@ -63,7 +65,7 @@ class IpForwardingUtils(object): |
27 |
""" |
28 |
args = args or [] |
29 |
options = options or {} |
30 |
- command = ['ip', 'route'] |
31 |
+ command = ['ifconfig'] |
32 |
command.extend(args) |
33 |
for item in options.items(): |
34 |
command.extend(item) |
35 |
@@ -108,10 +110,15 @@ class IpForwardingUtils(object): |
36 |
Returns: |
37 |
list, the IP address strings. |
38 |
""" |
39 |
- args = ['ls', 'table', 'local', 'type', 'local'] |
40 |
- options = self._CreateRouteOptions(dev=interface) |
41 |
- result = self._RunIpRoute(args=args, options=options) |
42 |
- return self.ParseForwardedIps(result.split()) |
43 |
+ try: |
44 |
+ ips = netifaces.ifaddresses('lo' + self.proto_id) |
45 |
+ ips = ips[netifaces.AF_INET] |
46 |
+ except (ValueError, KeyError) as e: |
47 |
+ return [] |
48 |
+ forwarded_ips = [] |
49 |
+ for ip in ips: |
50 |
+ forwarded_ips.append(ip['addr'] + '/' + str(netaddr.IPAddress(ip['netmask']).netmask_bits())) |
51 |
+ return self.ParseForwardedIps(forwarded_ips) |
52 |
|
53 |
def AddForwardedIp(self, address, interface): |
54 |
"""Configure a new IP address on the network interface. |
55 |
@@ -121,9 +128,12 @@ class IpForwardingUtils(object): |
56 |
interface: string, the output device to use. |
57 |
""" |
58 |
address = address if IP_ALIAS_REGEX.match(address) else '%s/32' % address |
59 |
- args = ['add', 'to', 'local', address] |
60 |
- options = self._CreateRouteOptions(dev=interface) |
61 |
- self._RunIpRoute(args=args, options=options) |
62 |
+ cmd = 'alias' |
63 |
+ try: |
64 |
+ forwarded_ips = netifaces.ifaddresses(interface) |
65 |
+ except (ValueError, KeyError) as e: |
66 |
+ cmd = 'create' |
67 |
+ self._RunIfconfig(args=[interface, cmd, address]) |
68 |
|
69 |
def RemoveForwardedIp(self, address, interface): |
70 |
"""Delete an IP address on the network interface. |
71 |
@@ -132,7 +142,5 @@ class IpForwardingUtils(object): |
72 |
address: string, the IP address to configure. |
73 |
interface: string, the output device to use. |
74 |
""" |
75 |
- address = address if IP_ALIAS_REGEX.match(address) else '%s/32' % address |
76 |
- args = ['delete', 'to', 'local', address] |
77 |
- options = self._CreateRouteOptions(dev=interface) |
78 |
- self._RunIpRoute(args=args, options=options) |
79 |
+ address = address if IP_REGEX.match(address) else address[:-3] |
80 |
+ self._RunIfconfig(args=[interface, '-alias', address]) |