ArielGlenn has submitted this change and it was merged.

Change subject: file examiner: use salt module for remote cmd
......................................................................


file examiner: use salt module for remote cmd

next up: do the same for dir examiner

Change-Id: I04112a17e32c83b24cb9347f71b8a01c4b963db1
---
M dataretention/data_auditor.py
M dataretention/retention/cli.py
M dataretention/retention/examiner.py
M dataretention/retention/retentionaudit.py
4 files changed, 51 insertions(+), 54 deletions(-)

Approvals:
  ArielGlenn: Verified; Looks good to me, approved



diff --git a/dataretention/data_auditor.py b/dataretention/data_auditor.py
index 3410df3..a987360 100644
--- a/dataretention/data_auditor.py
+++ b/dataretention/data_auditor.py
@@ -7,7 +7,7 @@
 from retention.remotefileauditor import RemoteFilesAuditor
 from retention.remotelogauditor import RemoteLogsAuditor
 from retention.remotehomeauditor import RemoteHomesAuditor
-from retention.examiner import FileExaminer, DirExaminer
+from retention.examiner import RemoteFileExaminer, DirExaminer
 
 def usage(message=None):
     if message:
@@ -102,17 +102,6 @@
 
 
 def main():
-    if len(sys.argv) == 1:
-        # special case, no args, we expect a special
-        # executor function to have been defined and
-        # added to the existing code, shovelled into
-        # the interpreter... if not there, whine
-        for key, value_unused in sys.modules[__name__].__dict__.items():
-            if key == 'executor':
-                executor()
-                sys.exit(0)
-        usage()
-
     hosts_expr = None
     audit_type = None
     files_to_check = None
@@ -224,7 +213,7 @@
         direxam.run()
         sys.exit(0)
     elif file_info is not None:
-        fileexam = FileExaminer(file_info, hosts_expr, linecount, timeout)
+        fileexam = RemoteFileExaminer(file_info, hosts_expr, linecount, 
timeout)
         fileexam.run()
         sys.exit(0)
 
diff --git a/dataretention/retention/cli.py b/dataretention/retention/cli.py
index 3b09b1f..f3f8863 100644
--- a/dataretention/retention/cli.py
+++ b/dataretention/retention/cli.py
@@ -19,7 +19,7 @@
 import retention.utils
 from retention.utils import JsonHelper
 from retention.config import Config
-from retention.examiner import DirExaminer, FileExaminer
+from retention.examiner import DirExaminer, RemoteFileExaminer
 import retention.fileutils
 import retention.ruleutils
 
@@ -574,7 +574,7 @@
     def get_file_contents(self, path):
         # get 20 lines and hope that's enough for the user to evaluate
         # fixme the number of lines should be configurable
-        fileexamin = FileExaminer(path, self.host, 20, self.timeout, 
quiet=True)
+        fileexamin = RemoteFileExaminer(path, self.host, 20, self.timeout, 
quiet=True)
         contents = fileexamin.run()
         return contents
 
diff --git a/dataretention/retention/examiner.py 
b/dataretention/retention/examiner.py
index 1b31996..129fc92 100644
--- a/dataretention/retention/examiner.py
+++ b/dataretention/retention/examiner.py
@@ -11,9 +11,9 @@
 from retention.fileinfo import FileInfo, EntryInfo
 
 
-class FileExaminer(object):
+class RemoteFileExaminer(object):
     '''
-    retrieval and display of file contents on local or remote host
+    retrieval and display of file contents on remote host
     '''
     def __init__(self, path, host, num_lines, timeout=20, quiet=False):
         self.path = path
@@ -26,44 +26,44 @@
         '''
         do all the work
         '''
-        if retention.utils.running_locally(self.host):
-            finf = FileInfo(self.path, None)
-            if finf.get_is_binary(self.num_lines):
-                result = "BINARY CONTENT\n"
-            else:
-                result = finf.start_content
+        client = LocalClientPlus()
+        module_args = [self.path,
+                       self.num_lines,
+                       self.timeout]
+
+        result = client.cmd([self.host],
+                            "retentionaudit.examine_file",
+                            module_args, expr_form='list',
+                            timeout=self.timeout)
+
+        if self.host in result:
             if not self.quiet:
-                print result,
-            return result
+                print result[self.host]
+            return result[self.host]
+
+
+class LocalFileExaminer(object):
+    '''
+    retrieval and display of file contents on local host
+    '''
+    def __init__(self, path, num_lines, timeout=20, quiet=False):
+        self.path = path
+        self.timeout = timeout
+        self.num_lines = num_lines
+        self.quiet = quiet
+
+    def run(self):
+        '''
+        do all the work
+        '''
+        finf = FileInfo(self.path, None)
+        if finf.get_is_binary(self.num_lines):
+            result = "BINARY CONTENT\n"
         else:
-            client = LocalClientPlus()
-            code = "# -*- coding: utf-8 -*-\n"
-            code += self.generate_executor()
-            with open(__file__, 'r') as fp_:
-                code += fp_.read()
-            result = client.cmd([self.host], "cmd.exec_code",
-                                ["python2", code],
-                                expr_form='list',
-                                timeout=self.timeout)
-            if self.host in result:
-                if not self.quiet:
-                    print result[self.host]
-                return result[self.host]
-
-    def generate_executor(self):
-        '''
-        horrible hack: this code is fed to salt when we feed this
-        script to stdin to run it remotely, thus bypassing all
-        the command line argument parsing logic
-
-        in this case we set up for FileExaminer directly
-        '''
-        code = """
-def executor():
-    fe = FileExaminer('%s', 'localhost', %d, %d)
-    fe.run()
-""" % (self.path, self.num_lines, self.timeout)
-        return code
+            result = finf.start_content
+        if not self.quiet:
+            print result,
+        return result
 
 
 class DirContents(object):
@@ -204,7 +204,7 @@
 
         if retention.utils.running_locally(self.host):
             dcont = DirContents(self.path, self.batchno, self.batchsize,
-                             self.prettyprint)
+                                self.prettyprint)
             result = dcont.get_contents()
             if result != 'ok':
                 print ('WARNING: failed to get directory contents'
diff --git a/dataretention/retention/retentionaudit.py 
b/dataretention/retention/retentionaudit.py
index 8b85771..8c7aa9b 100644
--- a/dataretention/retention/retentionaudit.py
+++ b/dataretention/retention/retentionaudit.py
@@ -7,6 +7,7 @@
 from retention.localfileaudit import LocalFilesAuditor
 from retention.locallogaudit import LocalLogsAuditor
 from retention.localhomeaudit import LocalHomesAuditor
+from retention.examiner import LocalFileExaminer
 
 log = logging.getLogger(__name__)
 
@@ -42,3 +43,10 @@
                                  maxfiles)
     result = hauditor.do_local_audit()
     return result
+
+def examine_file(path, num_lines,
+                 timeout, quiet=False):
+    fexaminer = LocalFileExaminer(path, num_lines,
+                                  timeout, quiet)
+    result = fexaminer.run()
+    return result

-- 
To view, visit https://gerrit.wikimedia.org/r/233459
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I04112a17e32c83b24cb9347f71b8a01c4b963db1
Gerrit-PatchSet: 2
Gerrit-Project: operations/software
Gerrit-Branch: master
Gerrit-Owner: ArielGlenn <[email protected]>
Gerrit-Reviewer: ArielGlenn <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to