Blame SOURCES/0003-config-defend-on-etc-target-backup-directory.patch

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