소스 검색

Started script for crontab update through named pipe commands

Lukas Angerer 5 년 전
부모
커밋
22dbfc8700
7개의 변경된 파일176개의 추가작업 그리고 1개의 파일을 삭제
  1. 4 0
      data/cron-fragment.txt
  2. 50 0
      data/settings.json
  3. 1 1
      docker-compose.yml
  4. 7 0
      scripts/commands/CmdExit.py
  5. 26 0
      scripts/commands/CmdUpdate.py
  6. 0 0
      scripts/commands/__init__.py
  7. 88 0
      scripts/cron-section-replace.py

+ 4 - 0
data/cron-fragment.txt

@@ -0,0 +1,4 @@
+# Weekdays / 08:30 - Holidays
+30 08 * * MON-FRI        /home/pi/mpc-alarm-cron.sh
+# Sunday / 09:00 - Lazy
+00 09 * * SUN            /home/pi/mpc-alarm-cron.sh

+ 50 - 0
data/settings.json

@@ -0,0 +1,50 @@
+{
+  "Alerts": {
+    "Command": "/home/pi/mpc-alarm-cron.sh",
+    "Groups": [
+      {
+        "Name": "Weekdays",
+        "Options": [
+          {
+            "Label": "06:20 - Work",
+            "Pattern": "20 06 * * MON-FRI"
+          },
+          {
+            "Label": "08:30 - Holidays",
+            "Pattern": "30 08 * * MON-FRI"
+          },
+          {
+            "Label": "09:00 - Sleeping In",
+            "Pattern": "00 09 * * MON-FRI"
+          }
+        ]
+      },
+      {
+        "Name": "Saturday",
+        "Options": [
+          {
+            "Label": "08:20 - Long-Jog",
+            "Pattern": "20 08 * * SAT"
+          },
+          {
+            "Label": "08:00 - Long-Jog (early)",
+            "Pattern": "00 08 * * SAT"
+          },
+          {
+            "Label": "07:40 - Long-Jog (very early)",
+            "Pattern": "40 07 * * SAT"
+          }
+        ]
+      },
+      {
+        "Name": "Sunday",
+        "Options": [
+          {
+            "Label": "09:00 - Lazy",
+            "Pattern": "00 09 * * SUN"
+          }
+        ]
+      }
+    ]
+  }
+}

+ 1 - 1
docker-compose.yml

@@ -1,4 +1,4 @@
-version: "3.9"  # optional since v1.27.0
+version: "2.0"
 services:
   web:
     image: executry/cronalarm:latest

+ 7 - 0
scripts/commands/CmdExit.py

@@ -0,0 +1,7 @@
+
+class CmdExit:
+    def __init__(self, config):
+        self.config = config
+    
+    def execute(self):
+        self.config["Running"] = False

+ 26 - 0
scripts/commands/CmdUpdate.py

@@ -0,0 +1,26 @@
+import os
+import re
+
+class CmdUpdate:
+    def __init__(self, config):
+        self.config = config
+    
+    def execute(self):
+        print("Updating...")
+        stream = os.popen("crontab -l")
+        crontab = stream.read()
+        result = crontab
+        begin = re.search("\s*#\s*BEGIN\(ALERTS\).*", crontab)
+        if begin:
+            fragment = ""
+            with open(self.config["CronFragment"]) as file:
+                fragment = file.read()
+            
+            result = crontab[:begin.span()[1]] + "\n"
+            result += fragment
+
+            end = re.search("\s*#\s*END\(ALERTS\).*", crontab)
+            if end:
+                result += crontab[end.span()[0]:]
+        
+        print(result)

+ 0 - 0
scripts/commands/__init__.py


+ 88 - 0
scripts/cron-section-replace.py

@@ -0,0 +1,88 @@
+#!/usr/bin/python3
+
+import os
+import errno
+import re
+from commands.CmdExit import CmdExit
+from commands.CmdUpdate import CmdUpdate
+
+DIR = os.path.dirname(os.path.realpath(__file__))
+
+CONFIG = {
+    "PipePath": os.path.join(DIR, "../data/cmd-pipe"),
+    "CronFragment": os.path.join(DIR, "../data/cron-fragment.txt"),
+    "Running": True,
+}
+
+# try:
+#     os.mkfifo(PIPE)
+# except OSError as oe:
+#     if oe.errno != errno.EEXIST:
+#         raise
+
+def execute(command):
+    if command == "EXIT":
+        global running
+        running = False
+    elif command == "UPDATE":
+        print("Updating...")
+        stream = os.popen('crontab -l')
+        crontab = stream.read()
+        result = crontab
+        begin = re.search("\s*#\s*BEGIN\(ALERTS\).*", crontab)
+        if begin:
+            result = crontab[:begin.span()[1]] + "\n"
+
+            end = re.search("\s*#\s*END\(ALERTS\).*", crontab)
+            if end:
+                result += "muahahah\ntest"
+                result += crontab[end.span()[0]:]
+        
+        print(result)
+    else:
+        print("UNKNOWN COMMAND: {0}".format(command))
+
+
+def main(config):
+    # commandPipe = ''
+    # cronFragment = ''
+    # try:
+    #     opts, args = getopt.getopt(argv,"h",["help","pipe=","cron="])
+    # except getopt.GetoptError:
+    #     print 'cron-section-replace --pipe <command-pipe> --cron <cron-fragment>'
+    #     sys.exit(2)
+    # for opt, arg in opts:
+    #     if opt in ("-h", "--help"):
+    #         print 'cron-section-replace --pipe <command-pipe> --cron <cron-fragment>'
+    #         sys.exit()
+    #     elif opt in ("--pipe"):
+    #         commandPipe = arg
+    #     elif opt in ("--cron):
+    #         cronFragment = arg
+    
+    # print(commandPipe)
+    # print(cronFragment)
+    # sys.exit()
+
+    commands = {
+        "EXIT": CmdExit(config),
+        "UPDATE": CmdUpdate(config)
+    }
+
+    
+    while config["Running"]:
+        print("Waiting for connection...")
+        with open(config["PipePath"]) as pipe:
+            print("Connection established")
+            while True:
+                cmd = pipe.read().strip()
+                if len(cmd) == 0:
+                    break
+                if cmd in commands:
+                    commands[cmd].execute()
+                else:
+                    print("UNKNOWN COMMAND: {0}".format(cmd))
+                #execute(data.strip())
+
+if __name__ == "__main__":
+   main(CONFIG)