]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-deploy.git/commitdiff
create grep functions for remote hosts
authorAlfredo Deza <alfredo.deza@inktank.com>
Tue, 23 Sep 2014 15:25:52 +0000 (11:25 -0400)
committerAlfredo Deza <alfredo.deza@inktank.com>
Tue, 23 Sep 2014 16:56:23 +0000 (12:56 -0400)
Signed-off-by: Alfredo Deza <alfredo.deza@inktank.com>
ceph_deploy/hosts/remotes.py

index d0df06d3665933cef734d8b2650123a877663aa1..26c41004364c8774b4f36c62475ac9490968dfe0 100644 (file)
@@ -212,6 +212,33 @@ def get_file(path):
         pass
 
 
+def object_grep(term, file_object):
+    for line in file_object.readlines():
+        if term in line:
+            return True
+    return False
+
+
+def grep(term, file_path):
+    # A small grep-like function that will search for a word in a file and
+    # return True if it does and False if it does not.
+
+    # Implemented initially to have a similar behavior as the init system
+    # detection in Ceph's init scripts::
+
+    #     # detect systemd
+    #     # SYSTEMD=0
+    #     grep -qs systemd /proc/1/comm && SYSTEMD=1
+
+    # .. note:: Because we intent to be operating in silent mode, we explicitly
+    # return ``False`` if the file does not exist.
+    if not os.path.isfile(file_path):
+        return False
+
+    with open(file_path) as _file:
+        return object_grep(term, _file)
+
+
 def shortname():
     """get remote short hostname"""
     return socket.gethostname().split('.', 1)[0]