ia64/xen-unstable

changeset 12125:0a8b854697ad

[XEND] XendVDI saves configuration on change.

Added a base class called AutoSaveObject that will attempt to call
save_config() if any attribute in the object changes. It isn't
particularly efficient, but we do not expect VDI to change much.

Signed-off-by: Alastair Tse <atse@xensource.com>
author Alastair Tse <atse@xensource.com>
date Fri Oct 13 15:34:01 2006 +0100 (2006-10-13)
parents 91c7ee18c978
children a02f6d28eded
files tools/python/scripts/xapi.py tools/python/xen/xend/XendVDI.py
line diff
     1.1 --- a/tools/python/scripts/xapi.py	Fri Oct 13 15:13:21 2006 +0100
     1.2 +++ b/tools/python/scripts/xapi.py	Fri Oct 13 15:34:01 2006 +0100
     1.3 @@ -37,6 +37,7 @@ COMMANDS = {
     1.4      'sr-list':   ('', 'List all SRs'),
     1.5      'vbd-create': ('<domname> <pycfg>', 'Create VBD attached to domname'),
     1.6      'vdi-list'  : ('', 'List all VDI'),
     1.7 +    'vdi-rename': ('<vdi_uuid> <new_name>', 'Rename VDI'),
     1.8      'vdi-delete': ('<vdi_uuid>', 'Delete VDI'),
     1.9      'vif-create': ('<domname> <pycfg>', 'Create VIF attached to domname'),
    1.10  
    1.11 @@ -303,6 +304,17 @@ def xapi_vdi_delete(*args):
    1.12      print 'Deleting VDI %s' % vdi_uuid
    1.13      result = execute(server.VDI.destroy, session, vdi_uuid)
    1.14      print 'Done.'
    1.15 +
    1.16 +def xapi_vdi_rename(*args):
    1.17 +    server, session = _connect()
    1.18 +    if len(args) < 2:
    1.19 +        raise OptionError('Not enough arguments')
    1.20 +
    1.21 +    vdi_uuid = args[0]
    1.22 +    vdi_name = args[1]
    1.23 +    print 'Renaming VDI %s to %s' % (vdi_uuid, vdi_name)
    1.24 +    result = execute(server.VDI.set_name_label, session, vdi_uuid, vdi_name)
    1.25 +    print 'Done.'
    1.26      
    1.27          
    1.28  #
     2.1 --- a/tools/python/xen/xend/XendVDI.py	Fri Oct 13 15:13:21 2006 +0100
     2.2 +++ b/tools/python/xen/xend/XendVDI.py	Fri Oct 13 15:34:01 2006 +0100
     2.3 @@ -27,7 +27,25 @@ from xmlrpclib import dumps, loads
     2.4  KB = 1024
     2.5  MB = 1024 * 1024
     2.6  
     2.7 -class XendVDI:
     2.8 +class AutoSaveObject(object):
     2.9 +    
    2.10 +    def __init__(self):
    2.11 +        self.cfg_path = None
    2.12 +        self.auto_save = True
    2.13 +        object
    2.14 +
    2.15 +    def save_config(self, cfg_file = None):
    2.16 +        raise NotImplementedError()
    2.17 +    
    2.18 +    def __setattr__(self, name, value):
    2.19 +        """A very simple way of making sure all attribute changes are
    2.20 +        flushed to disk.
    2.21 +        """
    2.22 +        object.__setattr__(self, name, value)
    2.23 +        if name != 'auto_save' and getattr(self, 'auto_save', False):
    2.24 +            self.save_config()
    2.25 +
    2.26 +class XendVDI(AutoSaveObject):
    2.27      """Generic Xen API compatible VDI representation.
    2.28  
    2.29      @cvar SAVED_CFG: list of configuration attributes to save.
    2.30 @@ -60,20 +78,20 @@ class XendVDI:
    2.31          self.read_only = False
    2.32          self.type = "system"
    2.33  
    2.34 -        self.cfg_path = None
    2.35 -
    2.36      def load_config_dict(self, cfg):
    2.37          """Loads configuration into the object from a dict.
    2.38  
    2.39          @param cfg: configuration dict
    2.40          @type  cfg: dict
    2.41          """
    2.42 +        self.auto_save = False
    2.43          for key in self.SAVED_CFG:
    2.44              if key in cfg:
    2.45                  if key in self.SAVED_CFG_INT:
    2.46                      setattr(self, key, int(cfg[key]))
    2.47                  else:
    2.48                      setattr(self, key, cfg[key])
    2.49 +        self.auto_save = True
    2.50  
    2.51      def load_config(self, cfg_path):
    2.52          """Loads configuration from an XMLRPC parameter format.
    2.53 @@ -128,10 +146,12 @@ class XendQCOWVDI(XendVDI):
    2.54      def __init__(self, uuid, sr_uuid, qcow_path, image_path, cfg_path,
    2.55                   vsize, psize):
    2.56          XendVDI.__init__(self, uuid, sr_uuid)
    2.57 +        self.auto_save = False
    2.58          self.qcow_path = qcow_path
    2.59          self.image_path = image_path
    2.60          self.cfg_path = cfg_path
    2.61          self.physical_utilisation = psize
    2.62          self.virtual_size = vsize
    2.63          self.sector_size = 1
    2.64 +        self.auto_save = True
    2.65