summaryrefslogtreecommitdiff
path: root/targetcli
diff options
context:
space:
mode:
authorPrasanna Kumar Kalever <prasanna.kalever@redhat.com>2017-12-15 15:16:28 +0530
committerPrasanna Kumar Kalever <prasanna.kalever@redhat.com>2017-12-15 15:39:35 +0530
commit6349a75bd71f2f15c3acd89588321524c94676e8 (patch)
tree0e850d314ea8e5c9a8c88934748065263fbf2d70 /targetcli
parent4f6008b7dbb22a1df19265f5cb4c7b6787475641 (diff)
downloadtargetcli-6349a75bd71f2f15c3acd89588321524c94676e8.tar.gz
config: defend on '/etc/target/backup' directory
Currently we do not create '/etc/target/backup/' while we expect the directory to be presented by package (rpm). If for some reason '/etc/target/backup' is not available, say may be a unintentional deletion or use of targetcli which is compiled from source, we see below errors on up on saveconfig command, Case 1: No '/etc/target/backup/' dir $ targetcli / saveconfig Could not create backup file /etc/target/backup/saveconfig-20171215-15:26:48.json: No such file or directory. Configuration saved to /etc/target/saveconfig.json Case 2: No '/etc/target/' dir $ targetcli / saveconfig Could not create backup file /etc/target/backup/saveconfig-20171215-15:27:42.json: No such file or directory. [Errno 2] No such file or directory: '/etc/target/saveconfig.json.temp' This patch tries to create '/etc/target/backup' directory tree in case if it is absent. Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
Diffstat (limited to 'targetcli')
-rw-r--r--targetcli/ui_root.py50
1 files changed, 29 insertions, 21 deletions
diff --git a/targetcli/ui_root.py b/targetcli/ui_root.py
index 8bc8521..33bb948 100644
--- a/targetcli/ui_root.py
+++ b/targetcli/ui_root.py
@@ -78,30 +78,38 @@ class UIRoot(UINode):
datetime.now().strftime("%Y%m%d-%H:%M:%S") + ".json"
backupfile = backup_dir + "/" + backup_name
backup_error = None
- try:
- shutil.copy(savefile, backupfile)
- except IOError as ioe:
- backup_error = ioe.strerror or "Unknown error"
- if backup_error == None:
- # Kill excess backups
+ if not os.path.exists(backup_dir):
+ try:
+ os.makedirs(backup_dir);
+ except OSError as exe:
+ raise ExecutionError("Cannot create backup directory [%s] %s." % (backup_dir, exc.strerror))
+
+ if os.path.exists(savefile):
try:
- with open(universal_prefs_file) as prefs:
- backups = [line for line in prefs.read().splitlines() if re.match('^kept_backups\s*=', line)]
- kept_backups = int(backups[0].split('=')[1].strip())
- except:
- kept_backups = default_kept_backups
-
- backups = sorted(glob(os.path.dirname(savefile) + "/backup/*.json"))
- files_to_unlink = list(reversed(backups))[kept_backups:]
- for f in files_to_unlink:
- with ignored(IOError):
- os.unlink(f)
-
- self.shell.log.info("Last %d configs saved in %s." % \
+ shutil.copy(savefile, backupfile)
+ except IOError as ioe:
+ backup_error = ioe.strerror or "Unknown error"
+
+ if backup_error == None:
+ # Kill excess backups
+ try:
+ with open(universal_prefs_file) as prefs:
+ backups = [line for line in prefs.read().splitlines() if re.match('^kept_backups\s*=', line)]
+ kept_backups = int(backups[0].split('=')[1].strip())
+ except:
+ kept_backups = default_kept_backups
+
+ backups = sorted(glob(os.path.dirname(savefile) + "/backup/*.json"))
+ files_to_unlink = list(reversed(backups))[kept_backups:]
+ for f in files_to_unlink:
+ with ignored(IOError):
+ os.unlink(f)
+
+ self.shell.log.info("Last %d configs saved in %s." % \
(kept_backups, backup_dir))
- else:
- self.shell.log.warning("Could not create backup file %s: %s." % \
+ else:
+ self.shell.log.warning("Could not create backup file %s: %s." % \
(backupfile, backup_error))
self.rtsroot.save_to_file(savefile)