-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RHELC-1153] Port the restorable file to work with backup controller #935
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -348,6 +348,125 @@ def restore(self): | |
super(RestorableRpmKey, self).restore() | ||
|
||
|
||
class NewRestorableFile(RestorableChange): | ||
def __init__(self, filepath): | ||
super(NewRestorableFile, self).__init__() | ||
self.filepath = filepath | ||
|
||
def enable(self): | ||
"""Save current version of a file""" | ||
# Prevent multiple backup | ||
if self.enabled: | ||
return | ||
|
||
loggerinst.info("Backing up %s." % self.filepath) | ||
if os.path.isfile(self.filepath): | ||
try: | ||
shutil.copy2(self.filepath, BACKUP_DIR) | ||
loggerinst.debug("Copied %s to %s." % (self.filepath, BACKUP_DIR)) | ||
|
||
except (OSError, IOError) as err: | ||
# IOError for py2 and OSError for py3 | ||
loggerinst.critical("Error(%s): %s" % (err.errno, err.strerror)) | ||
else: | ||
loggerinst.info("Can't find %s.", self.filepath) | ||
|
||
# Set the enabled value | ||
super(NewRestorableFile, self).enable() | ||
|
||
def restore(self, rollback=True): | ||
"""Restore a previously backed up file""" | ||
if rollback: | ||
loggerinst.task("Rollback: Restore %s from backup" % self.filepath) | ||
else: | ||
loggerinst.info("Restoring %s from backup" % self.filepath) | ||
|
||
# We do not have backup or not backed up by this | ||
if not self.enabled: | ||
loggerinst.info("%s hasn't been backed up." % self.filepath) | ||
return | ||
|
||
backup_filepath = os.path.join(BACKUP_DIR, os.path.basename(self.filepath)) | ||
|
||
if not os.path.isfile(backup_filepath): | ||
loggerinst.info("%s hasn't been backed up." % self.filepath) | ||
return | ||
|
||
try: | ||
shutil.copy2(backup_filepath, self.filepath) | ||
except (OSError, IOError) as err: | ||
# Do not call 'critical' which would halt the program. We are in | ||
# a rollback phase now and we want to rollback as much as possible. | ||
# IOError for py2 and OSError for py3 | ||
loggerinst.warning("Error(%s): %s" % (err.errno, err.strerror)) | ||
return | ||
|
||
if rollback: | ||
loggerinst.info("File %s restored." % self.filepath) | ||
super(NewRestorableFile, self).restore() | ||
else: | ||
loggerinst.debug("File %s restored." % self.filepath) | ||
# not setting enabled to false since this is not being rollback | ||
# restoring the backed up file for conversion purposes | ||
|
||
# Probably will be deprecated and unusable since using the BackupController | ||
# Depends on specific usage of this | ||
def remove(self): | ||
"""Remove restored file from original place, backup isn't removed""" | ||
try: | ||
os.remove(self.filepath) | ||
loggerinst.debug("File %s removed." % self.filepath) | ||
except (OSError, IOError): | ||
loggerinst.debug("Couldn't remove restored file %s" % self.filepath) | ||
|
||
|
||
class MissingFile(RestorableChange): | ||
""" | ||
File not present before conversion. Could be created during | ||
conversion so should be removed in rollback. | ||
""" | ||
|
||
def __init__(self, filepath): | ||
super(MissingFile, self).__init__() | ||
self.filepath = filepath | ||
|
||
def enable(self): | ||
if self.enabled: | ||
return | ||
Comment on lines
+434
to
+435
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're testing the |
||
|
||
if os.path.isfile(self.filepath): | ||
loggerinst.debug( | ||
"Shouldn't be called, file {filepath} is present before conversion".format(filepath=self.filepath) | ||
) | ||
return | ||
|
||
loggerinst.info("Marking file {filepath} as missing on system.".format(filepath=self.filepath)) | ||
super(MissingFile, self).enable() | ||
|
||
def restore(self): | ||
if not self.enabled: | ||
return | ||
|
||
loggerinst.task("Rollback: remove file created during conversion {filepath}".format(filepath=self.filepath)) | ||
|
||
if not os.path.isfile(self.filepath): | ||
loggerinst.info("File {filepath} wasn't created during conversion".format(filepath=self.filepath)) | ||
else: | ||
try: | ||
os.remove(self.filepath) | ||
loggerinst.info("File {filepath} removed".format(filepath=self.filepath)) | ||
except OSError as err: | ||
# Do not call 'critical' which would halt the program. We are in | ||
# a rollback phase now and we want to rollback as much as possible. | ||
loggerinst.warning("Error(%s): %s" % (err.errno, err.strerror)) | ||
return | ||
|
||
super(MissingFile, self).restore() | ||
|
||
|
||
# Legacy class for creating the restorable file | ||
# Can be removed after porting to new BackupController is finished | ||
# https://issues.redhat.com/browse/RHELC-1153 | ||
class RestorableFile: | ||
def __init__(self, filepath): | ||
self.filepath = filepath | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to have unit tests covering it. Unless I'm mistaken the Error we currently catch is for
restore()
function