Browse Source

Testing write to named pipe as file

Lukas Angerer 5 years ago
parent
commit
591024c040

+ 2 - 0
CronAlarm/Config/CronFragment.cs

@@ -3,5 +3,7 @@
     public class CronFragment
     {
         public string FilePath { get; set; }
+
+        public string CommandPipe { get; set; }
     }
 }

+ 13 - 0
CronAlarm/CronFragmentHandler.cs

@@ -33,6 +33,19 @@ namespace CronAlarm
                     writer.WriteLine(entry.Command);
                 }
             }
+
+            NotifyUpdate();
+        }
+
+        private void NotifyUpdate()
+        {
+            if (File.Exists(_cronFragment.CommandPipe))
+            {
+                using (var writer = File.AppendText(_cronFragment.CommandPipe))
+                {
+                    writer.WriteLine("UPDATE");
+                }
+            }
         }
     }
 }

+ 2 - 1
CronAlarm/appsettings.json

@@ -8,6 +8,7 @@
   },
   "AllowedHosts": "*",
   "CronFragment": {
-    "FilePath": "./wwwroot/data/cron-fragment.txt"
+    "FilePath": "./wwwroot/data/cron-fragment.txt",
+    "CommandPipe": "./wwwroot/data/cmd-pipe"
   }
 }

+ 3 - 0
CronAlarm/wwwroot/data/cmd-pipe

@@ -0,0 +1,3 @@
+UPDATE
+UPDATE
+UPDATE

+ 26 - 0
scripts/cron-api.py

@@ -0,0 +1,26 @@
+#!/usr/bin/python3
+
+import os
+from flask import Flask, Response, json, request
+from cron.Parser import Parser
+
+api = Flask(__name__)
+
+@api.route('/version', methods=['GET'])
+def get_version():
+    return Response(json.dumps({ "version": "1.0" }), mimetype="application/json")
+
+@api.route('/section/<string:name>', methods=['GET'])
+def get_section(name):
+    cronFile = Parser().parse()
+    return Response(cronFile.get_section(name), mimetype="text/plain")
+
+@api.route('/section/<string:name>', methods=['POST'])
+def set_section(name):
+    parser = Parser()
+    cronFile = parser.parse()
+    cronFile.set_section(name, request.data.decode())
+    return Response(parser.render(cronFile), mimetype="text/plain")
+
+if __name__ == "__main__":
+    api.run(host = "0.0.0.0", port = 5000)

+ 21 - 0
scripts/cron/CronFile.py

@@ -0,0 +1,21 @@
+import os
+import re
+
+class CronFile:
+    def __init__(self):
+        self.fragments = []
+        self.sections = {}
+
+    def add_fragment(self, name, content):
+        self.fragments.append((name, content))
+        if name is not None:
+            self.sections[name] = len(self.fragments) - 1
+
+    def get_section(self, name):
+        if name in self.sections:
+            return self.fragments[self.sections[name]][1]
+
+    def set_section(self, name, content):
+        if name not in self.sections:
+            self.add_fragment(name, "")
+        self.fragments[self.sections[name]] = (name, "\n" + content)

+ 38 - 0
scripts/cron/Parser.py

@@ -0,0 +1,38 @@
+import os
+import re
+from .CronFile import CronFile
+
+class Parser:
+    def parse(self):
+        cronFile = CronFile()
+        crontab = ""
+        with os.popen("crontab -l") as stream:
+            crontab = stream.read()
+        result = crontab
+        begin = re.search("^\s*#\s*BEGIN\((\w+)\).*$", crontab, flags=re.MULTILINE)
+        while begin:
+            section = begin.group(1)
+            cronFile.add_fragment(None, crontab[:begin.span()[0]])
+        
+            crontab = crontab[begin.span()[1]:]
+            end = re.search("^\s*#\s*END\({0}\).*$".format(section), crontab, flags=re.MULTILINE)
+            if end:
+                cronFile.add_fragment(section, crontab[:end.span()[0]])
+                crontab = crontab[end.span()[1]:]
+            
+            begin = re.search("^\s*#\s*BEGIN\((\w+)\).*$", crontab, flags=re.MULTILINE)
+
+        cronFile.add_fragment(None, crontab)
+
+        return cronFile
+    
+    def render(self, cronFile):
+        result = ""
+        for fragment in cronFile.fragments:
+            if fragment[0] is None:
+                result += fragment[1]
+            else:
+                result += "\n# BEGIN({0})".format(fragment[0])
+                result += fragment[1]
+                result += "\n# END({0})\n".format(fragment[0])
+        return result

+ 0 - 0
scripts/cron/__init__.py