[node-patches] Change in ovirt-node[master]: refactor ovirt_store_config to native python

jboggs at redhat.com jboggs at redhat.com
Tue Oct 30 19:50:25 UTC 2012


Joey Boggs has uploaded a new change for review.

Change subject: refactor ovirt_store_config to native python
......................................................................

refactor ovirt_store_config to native python

rhbz#871212

Signed-off-by: Joey Boggs <jboggs at redhat.com>
Change-Id: Ic37903fb70f006fda133533b93515a654b32de8a
---
M scripts/ovirtnode/ovirtfunctions.py
M scripts/ovirtnode/storage.py
2 files changed, 53 insertions(+), 25 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/34/8934/1

diff --git a/scripts/ovirtnode/ovirtfunctions.py b/scripts/ovirtnode/ovirtfunctions.py
index 0fd7167..fc556b9 100644
--- a/scripts/ovirtnode/ovirtfunctions.py
+++ b/scripts/ovirtnode/ovirtfunctions.py
@@ -664,7 +664,7 @@
 #   ovirt_store_config /etc/config /etc/config2 ...
 #   copy to /config and bind-mount back
 
-def ovirt_store_config(files):
+def ovirt_store_config(files, destination = None):
     rc = True
     if is_stateless():
         return True
@@ -673,13 +673,16 @@
         return False
     if isinstance(files,STRING_TYPE):
         files_list = []
-        files_list.append(files)
+        if os.path.isdir(files):
+            for f in os.listdir(files):
+                files_list.append(os.path.join(files, f))
+        else:
+            files_list.append(files)
     else:
         files_list=files
     for f in files_list:
         filename = os.path.abspath(f)
         persist_it=True
-
         if os.path.isdir(filename):
             # ensure that, if this is a directory
             # that it's not already persisted
@@ -697,34 +700,58 @@
                     logger.warn("File already persisted: " + filename)
                     persist_it=False
                 else:
-                    # persistent copy needs refresh
-                    if system("umount -n " + filename + " 2> /dev/null"):
-                        system("rm -f /config"+ filename)
+                    persist_it=True
         else:
             # skip if file does not exist
             logger.warn("Skipping, file '" + filename + "' does not exist")
             continue
-
         if persist_it:
-            # skip if already bind-mounted
-            if not check_bind_mount(filename):
-                dirname = os.path.dirname(filename)
-                system("mkdir -p /config/" + dirname)
-                if system("cp -a " + filename + " /config"+filename):
-                    if not system("mount -n --bind /config"+filename+ " " + \
-                                  filename):
-                        logger.error("Failed to persist: " + filename)
-                        rc = False
-                    else:
-                        logger.info("File: " + filename + " persisted")
-                        rc = rc and True
-            # register in /config/files used by rc.sysinit
-            ret = system_closefds("grep -q \"^$" + filename +"$\" " + \
-                                  " /config/files 2> /dev/null")
-            if ret > 0:
-                system_closefds("echo "+filename+" >> /config/files")
-                logger.info("Successfully persisted: " + filename)
+            # filename - file to be persisted
+            # destination - defaults to filename unless specified
+            # tmp_destination - temp file under /config before final placement
+            # final_destination - final resting place under /config
+
+            dirname = os.path.dirname(filename).lstrip("/")
+            if destination is None:
+                destination = filename
+                final_destination = os.path.join('/config', filename.lstrip("/"))
+            else:
+                final_destination = os.path.join('/config', destination.lstrip("/"))
+            tmp_destination = os.path.join(final_destination + ".tmp")
+
+            if not os.path.exists(os.path.join('/config',  dirname)):
+                os.makedirs(os.path.join('/config',  dirname))
+            try:
+                logger.debug("Copying: %s to %s" % (filename, tmp_destination))
+                shutil.copyfile(filename, tmp_destination)
+            except IOError, e:
+                logger.error("Unable to copy %s to %s: %s" \
+                    % (filename, tmp_destination, e))
+                return False
+            # temp file inplace, check for existing file
+            if check_bind_mount(destination):
+                logger.debug("%s is bind mounted, unmounting" % destination)
+                if system("umount -n %s" % destination):
+                    logger.debug("Unmounted: %s" % destination)
+            logger.debug("Moving %s to %s" % \
+                (tmp_destination, final_destination))
+            shutil.move(tmp_destination, final_destination)
+            if not system("mount -n --bind %s /%s " \
+                % (final_destination, final_destination.lstrip("/config"))):
+                logger.error("Failed to persist: " + filename)
+                rc = False
+            else:
+                logger.info("File: " + filename + " persisted")
                 rc = rc and True
+            # register in /config/files used by rc.sysinit
+            # reset destination in case of directories
+            destination = None
+            with open('/config/files', 'r') as f:
+                if not filename in f:
+                    with open('/config/files', 'a+') as f:
+                        f.write("%s\n" % filename)
+                    logger.info("Successfully persisted: " + filename)
+                    rc = rc and True
         else:
             logger.warn(filename + " Already persisted")
             rc = rc and True
diff --git a/scripts/ovirtnode/storage.py b/scripts/ovirtnode/storage.py
index b83514c..7a11c1b 100644
--- a/scripts/ovirtnode/storage.py
+++ b/scripts/ovirtnode/storage.py
@@ -502,6 +502,7 @@
 
         logger.info("Mounting config partition")
         _functions.mount_config()
+        open("/config/files", 'w').close()
         if os.path.ismount("/config"):
             _functions.ovirt_store_config("/etc/fstab")
         # remount /var/log from tmpfs to HostVG/Logging


--
To view, visit http://gerrit.ovirt.org/8934
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic37903fb70f006fda133533b93515a654b32de8a
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Joey Boggs <jboggs at redhat.com>



More information about the node-patches mailing list