ia64/xen-unstable

view tools/python/xen/xend/XendVDI.py @ 12725:36fe7ca48e54

Tidy up the creation of directories that Xend needs. This avoids potential
races in this creation.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Fri Dec 01 11:32:32 2006 +0000 (2006-12-01)
parents 1f790f5fcdbb
children 4fbefd9cb85e
line source
1 #!/usr/bin/python
2 #============================================================================
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of version 2.1 of the GNU Lesser General Public
5 # License as published by the Free Software Foundation.
6 #
7 # This library is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
11 #
12 # You should have received a copy of the GNU Lesser General Public
13 # License along with this library; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 #============================================================================
16 # Copyright (C) 2006 XenSource Ltd.
17 #============================================================================
18 #
19 # Representation of a Xen API VDI
20 #
22 import os
24 from xen.util.xmlrpclib2 import stringify
25 from xmlrpclib import dumps, loads
27 KB = 1024
28 MB = 1024 * 1024
30 class AutoSaveObject(object):
32 def __init__(self):
33 self.cfg_path = None
34 self.auto_save = True
35 object
37 def save_config(self, cfg_file = None):
38 raise NotImplementedError()
40 def __setattr__(self, name, value):
41 """A very simple way of making sure all attribute changes are
42 flushed to disk.
43 """
44 object.__setattr__(self, name, value)
45 if name != 'auto_save' and getattr(self, 'auto_save', False):
46 self.save_config()
48 class XendVDI(AutoSaveObject):
49 """Generic Xen API compatible VDI representation.
51 @cvar SAVED_CFG: list of configuration attributes to save.
52 @cvar SAVED_CFG_INT: list of configurations that should be ints.
53 """
55 SAVED_CFG = ['name_label',
56 'name_description',
57 'sector_size',
58 'virtual_size',
59 'physical_utilisation',
60 'parent',
61 'children',
62 'sharable',
63 'read_only']
65 SAVED_CFG_INT = ['sector_size', 'virtual_size', 'physical_utilisation']
67 def __init__(self, uuid, sr_uuid):
68 self.uuid = uuid
69 self.sr_uuid = sr_uuid
70 self.name_label = ""
71 self.name_description = ""
72 self.sector_size = 1024
73 self.virtual_size = 0
74 self.physical_utilisation = 0
75 self.parent = None
76 self.children = []
77 self.sharable = False
78 self.read_only = False
79 self.type = "system"
81 def load_config_dict(self, cfg):
82 """Loads configuration into the object from a dict.
84 @param cfg: configuration dict
85 @type cfg: dict
86 """
87 self.auto_save = False
88 for key in self.SAVED_CFG:
89 if key in cfg:
90 if key in self.SAVED_CFG_INT:
91 setattr(self, key, int(cfg[key]))
92 else:
93 setattr(self, key, cfg[key])
94 self.auto_save = True
96 def load_config(self, cfg_path):
97 """Loads configuration from an XMLRPC parameter format.
99 @param cfg_path: configuration file path
100 @type cfg_path: type
101 @rtype: bool
102 @return: Successful or not.
103 """
104 try:
105 cfg, _ = loads(open(cfg_path).read())
106 cfg = cfg[0]
107 self.load_config_dict(cfg)
108 self.cfg_path = cfg_path
109 except IOError, e:
110 return False
112 return True
114 def save_config(self, cfg_path = None):
115 """Saves configuration at give path in XMLRPC parameter format.
117 If cfg_path is not give, it defaults to the where the VDI
118 configuration as loaded if it load_config was called.
120 @keyword cfg_path: optional configuration file path
121 @rtype: bool
122 @return: Successful or not.
123 """
124 try:
125 if not cfg_path and not self.cfg_path:
126 return False
128 if not cfg_path:
129 cfg_path = self.cfg_path
131 cfg = {}
132 for key in self.SAVED_CFG:
133 try:
134 cfg[key] = getattr(self, key)
135 except AttributeError:
136 pass
137 open(cfg_path, 'w').write(dumps((stringify(cfg),),
138 allow_none = True))
139 except IOError, e:
140 return False
142 return True
144 class XendQCOWVDI(XendVDI):
146 def __init__(self, uuid, sr_uuid, qcow_path, cfg_path, vsize, psize):
147 XendVDI.__init__(self, uuid, sr_uuid)
148 self.auto_save = False
149 self.qcow_path = qcow_path
150 self.cfg_path = cfg_path
151 self.physical_utilisation = psize
152 self.virtual_size = vsize
153 self.sector_size = 512
154 self.auto_save = True