Sysrestore fixes.

Latest patch used the wrong path and all files where actually going to /tmp
even if a different path was specified.
Makes also StateFile behave the same as FileStore, and be a public class, this
way a common path can be used too.
This commit is contained in:
Simo Sorce
2008-03-31 17:27:56 -04:00
parent e666bfbca7
commit 7b5088955a
2 changed files with 50 additions and 47 deletions

View File

@@ -48,7 +48,8 @@ class FileStore:
The file @path/sysrestore.index is used to store information The file @path/sysrestore.index is used to store information
about the original location of the saved files. about the original location of the saved files.
""" """
self._path = path+"/"+SYSRESTORE_INDEXFILE self._path = path
self._index = self._path + "/" + SYSRESTORE_INDEXFILE
self.random = random.Random() self.random = random.Random()
@@ -60,12 +61,12 @@ class FileStore:
be an empty dictionary if the file doesn't exist. be an empty dictionary if the file doesn't exist.
""" """
logging.debug("Loading Index file from '%s'", self._path) logging.debug("Loading Index file from '%s'", self._index)
self.files = {} self.files = {}
p = ConfigParser.SafeConfigParser() p = ConfigParser.SafeConfigParser()
p.read(self._path) p.read(self._index)
for section in p.sections(): for section in p.sections():
if section == "files": if section == "files":
@@ -74,15 +75,15 @@ class FileStore:
def save(self): def save(self):
"""Save the file list to @_path. If @files is an empty """Save the file list to @_index. If @files is an empty
dict, then @_path should be removed. dict, then @_index should be removed.
""" """
logging.debug("Saving Index File to '%s'", self._path) logging.debug("Saving Index File to '%s'", self._index)
if len(self.files) == 0: if len(self.files) == 0:
logging.debug(" -> no files, removing file") logging.debug(" -> no files, removing file")
if os.path.exists(self._path): if os.path.exists(self._index):
os.remove(self._path) os.remove(self._index)
return return
p = ConfigParser.SafeConfigParser() p = ConfigParser.SafeConfigParser()
@@ -91,7 +92,7 @@ class FileStore:
for (key, value) in self.files.items(): for (key, value) in self.files.items():
p.set('files', key, str(value)) p.set('files', key, str(value))
f = file(self._path, "w") f = file(self._index, "w")
p.write(f) p.write(f)
f.close() f.close()
@@ -117,7 +118,7 @@ class FileStore:
filename += h filename += h
filename += "-"+file filename += "-"+file
backup_path = os.path.join(SYSRESTORE_PATH, filename) backup_path = os.path.join(self._path, filename)
if os.path.exists(backup_path): if os.path.exists(backup_path):
logging.debug(" -> Not backing up - already have a copy of '%s'", path) logging.debug(" -> Not backing up - already have a copy of '%s'", path)
return return
@@ -156,7 +157,7 @@ class FileStore:
if not filename: if not filename:
raise ValueError("No such file name in the index") raise ValueError("No such file name in the index")
backup_path = os.path.join(SYSRESTORE_PATH, filename) backup_path = os.path.join(self._path, filename)
if not os.path.exists(backup_path): if not os.path.exists(backup_path):
logging.debug(" -> Not restoring - '%s' doesn't exist", backup_path) logging.debug(" -> Not restoring - '%s' doesn't exist", backup_path)
return False return False
@@ -187,7 +188,7 @@ class FileStore:
(mode,uid,gid,path) = string.split(value, ',', 3) (mode,uid,gid,path) = string.split(value, ',', 3)
backup_path = os.path.join(SYSRESTORE_PATH, filename) backup_path = os.path.join(self._path, filename)
if not os.path.exists(backup_path): if not os.path.exists(backup_path):
logging.debug(" -> Not restoring - '%s' doesn't exist", backup_path) logging.debug(" -> Not restoring - '%s' doesn't exist", backup_path)
@@ -203,7 +204,7 @@ class FileStore:
return True return True
class _StateFile: class StateFile:
"""A metadata file for recording system state which can """A metadata file for recording system state which can
be backed up and later restored. The format is something be backed up and later restored. The format is something
like: like:
@@ -214,7 +215,7 @@ class _StateFile:
""" """
def __init__(self, path = SYSRESTORE_PATH): def __init__(self, path = SYSRESTORE_PATH):
"""Create a _StateFile object, loading from @path. """Create a StateFile object, loading from @path.
The dictionary @modules, a member of the returned object, The dictionary @modules, a member of the returned object,
is where the state can be modified. @modules is indexed is where the state can be modified. @modules is indexed
@@ -277,43 +278,40 @@ class _StateFile:
p.write(f) p.write(f)
f.close() f.close()
def backup_state(module, key, value): def backup_state(self, module, key, value):
"""Backup an item of system state from @module, identified """Backup an item of system state from @module, identified
by the string @key and with the value @value. @value may be by the string @key and with the value @value. @value may be
a string or boolean. a string or boolean.
""" """
if not (isinstance(value, str) or isinstance(value, bool)): if not (isinstance(value, str) or isinstance(value, bool)):
raise ValueError("Only strings or booleans supported") raise ValueError("Only strings or booleans supported")
state = _StateFile() if not self.modules.has_key(module):
self.modules[module] = {}
if not state.modules.has_key(module): if not self.modules.has_key(key):
state.modules[module] = {} self.modules[module][key] = value
if not state.modules.has_key(key): self.save()
state.modules[module][key] = value
state.save() def restore_state(self, module, key):
"""Return the value of an item of system state from @module,
identified by the string @key, and remove it from the backed
up system state.
def restore_state(module, key): If the item doesn't exist, #None will be returned, otherwise
"""Return the value of an item of system state from @module, the original string or boolean value is returned.
identified by the string @key, and remove it from the backed """
up system state.
If the item doesn't exist, #None will be returned, otherwise if not self.modules.has_key(module):
the original string or boolean value is returned. return None
"""
state = _StateFile()
if not state.modules.has_key(module): if not self.modules[module].has_key(key):
return None return None
if not state.modules[module].has_key(key): value = self.modules[module][key]
return None del self.modules[module][key]
value = state.modules[module][key] self.save()
del state.modules[module][key]
state.save() return value
return value

View File

@@ -78,11 +78,16 @@ def print_msg(message, output_fd=sys.stdout):
class Service: class Service:
def __init__(self, service_name): def __init__(self, service_name, sstore=None):
self.service_name = service_name self.service_name = service_name
self.steps = [] self.steps = []
self.output_fd = sys.stdout self.output_fd = sys.stdout
if sstore:
self.sstore = sstore
else:
self.sstore = sysrestore.StateFile('/var/lib/ipa/sysrestore')
def set_output(self, fd): def set_output(self, fd):
self.output_fd = fd self.output_fd = fd
@@ -114,10 +119,10 @@ class Service:
return is_enabled(self.service_name) return is_enabled(self.service_name)
def backup_state(self, key, value): def backup_state(self, key, value):
sysrestore.backup_state(self.service_name, key, value) self.sstore.backup_state(self.service_name, key, value)
def restore_state(self, key): def restore_state(self, key):
return sysrestore.restore_state(self.service_name, key) return self.sstore.restore_state(self.service_name, key)
def print_msg(self, message): def print_msg(self, message):
print_msg(message, self.output_fd) print_msg(message, self.output_fd)