ia64/xen-unstable

view tools/python/xen/xend/XendNetwork.py @ 13209:3f24490932cd

Cope if a VIF is not attached to a network.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Thu Dec 28 15:26:45 2006 +0000 (2006-12-28)
parents d759e9df902f
children 568433250d84
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (c) 2006 Xensource Inc.
16 #============================================================================
18 import os
19 import commands
20 import re
21 import struct
22 import socket
24 import XendDomain
25 import XendNode
26 from XendLogging import log
28 IP_ROUTE_RE = r'^default via ([\d\.]+) dev (\w+)'
30 def linux_get_default_network():
31 """Returns the network details of the host."""
33 ip_cmd = '/sbin/ip route'
34 rc, output = commands.getstatusoutput(ip_cmd)
35 default_route = None
36 default_dev = None
37 default_netmask = None
38 if rc == 0:
39 # find default route/device
40 for line in output.split('\n'):
41 is_default = re.search(IP_ROUTE_RE, line)
42 if is_default:
43 default_route = is_default.group(1)
44 default_dev = is_default.group(2)
46 # find network address and network mask
47 if default_dev:
48 dev_re = r'^([\d\.]+)/(\d+) dev %s' % default_dev
49 for line in output.split('\n'):
50 is_dev = re.search(dev_re, line)
51 if is_dev:
52 # convert integer netmask to string representation
53 netmask = 0xffffffff ^ (2**(32-int(is_dev.group(2))) - 1)
54 packed = struct.pack('!I', netmask)
55 default_netmask = socket.inet_ntoa(packed)
57 return (default_route, default_netmask)
60 class XendNetwork:
61 def __init__(self, uuid, name, description, gateway, netmask):
62 self.uuid = uuid
63 self.name_label = name
64 self.name_description = description
65 self.default_gateway = gateway
66 self.default_netmask = netmask
68 def set_name_label(self, new_name):
69 self.name_label = new_name
70 XendNode.instance().save_networks()
72 def set_name_description(self, new_desc):
73 self.name_description = new_desc
74 XendNode.instance().save_networks()
76 def set_default_gateway(self, new_gateway):
77 if re.search('^\d+\.\d+\.\d+\.\d+$', new_gateway):
78 self.default_gateway = new_gateway
79 XendNode.instance().save_networks()
81 def set_default_netmask(self, new_netmask):
82 if re.search('^\d+\.\d+\.\d+\.\d+$', new_netmask):
83 self.default_netmask = new_netmask
84 XendNode.instance().save_networks()
86 def get_VIF_UUIDs(self):
87 result = []
88 vms = XendDomain.instance().get_all_vms()
89 for vm in vms:
90 vifs = vm.get_vifs()
91 for vif in vifs:
92 vif_cfg = vm.get_dev_xenapi_config('vif', vif)
93 if vif_cfg.get('network') == self.uuid:
94 result.append(vif)
95 return result
97 def get_PIF_UUIDs(self):
98 return [x.uuid for x in XendNode.instance().pifs.values()
99 if x.network == self]
101 def get_record(self, transient = True):
102 result = {
103 'uuid': self.uuid,
104 'name_label': self.name_label,
105 'name_description': self.name_description,
106 'default_gateway': self.default_gateway,
107 'default_netmask': self.default_netmask,
108 }
109 if transient:
110 result['VIFs'] = self.get_VIF_UUIDs()
111 result['PIFs'] = self.get_PIF_UUIDs()
112 return result