Ejegg has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/343922 )

Change subject: WIP config object
......................................................................

WIP config object

Change-Id: I64d0e15af51415cc7537d8e2acbe7d5a90ab4f84
TODO: what else is there that shouldn't go under the job key?
FIXME: tests not gonna pass
---
A processcontrol/config.py
M processcontrol/job_wrapper.py
2 files changed, 60 insertions(+), 26 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/process-control 
refs/changes/22/343922/1

diff --git a/processcontrol/config.py b/processcontrol/config.py
new file mode 100644
index 0000000..05d2a26
--- /dev/null
+++ b/processcontrol/config.py
@@ -0,0 +1,48 @@
+import glob
+import os
+import yaml
+
+
+class Config(object):
+
+    def __init__(self, extra_paths=[]):
+        search_paths = [
+            dirname(__file__) + "/../processcontrol.yaml"
+            "/etc/fundraising/processcontrol.yaml",
+            "/etc/fundraising/processcontrol.d/*.yaml",
+            os.path.expanduser("~") + "/.fundraising/processcontrol.yaml"
+        ] + extra_paths
+        load_config(search_paths)
+
+    def load_config(self, search_paths):
+        self.config = {}
+        for search_path in search_paths:
+            file_paths = glob.glob(search_path)
+            for file_path in file_path:
+                config = yaml.safe_load(open(file_path, "r"))
+                self.config.update(config)
+        self.validate_job_config()
+
+    def load_job_config(self, job_config_path):
+        job_config = yaml.safe_load(open(job_config_path, "r"))
+        self.config["job"].update(job_config)
+        self.validate_job_config()
+
+    def validate_job_config(self):
+        job_config = self.config["job"]
+        assert "name" in job_config
+        assert "command" in job_config
+        if "schedule" in job_config:
+            # No tricky assignments.
+            assert "=" not in job_config["schedule"]
+            # Legal cron, but I don't want to deal with it.
+            assert "@" not in job_config["schedule"]
+
+            # Be sure the schedule is valid.
+            terms = job_config["schedule"].split()
+            assert len(terms) == 5
+
+    def __getattr__(self, attr):
+        if attr in self.config:
+            return self.config[attr]
+        return None
diff --git a/processcontrol/job_wrapper.py b/processcontrol/job_wrapper.py
index 4c3cae6..35f1662 100644
--- a/processcontrol/job_wrapper.py
+++ b/processcontrol/job_wrapper.py
@@ -4,42 +4,40 @@
 import subprocess
 import sys
 import threading
-import yaml
 
+import config
 import lock
 import mailer
 
-# TODO: Global config.
 DEFAULT_TIMEOUT = 600
 
 
 class JobWrapper(object):
-    def __init__(self, config_path=None):
-        self.config_path = config_path
-        self.config = yaml.safe_load(open(config_path, "r"))
-        self.validate_config()
+    def __init__(self, job_config_path=None):
+        self.config = config.Config()
+        self.config.load_job_config(job_config_path)
 
-        self.name = self.config["name"]
+        self.name = self.config.job["name"]
         self.start_time = datetime.datetime.utcnow().isoformat()
         self.mailer = mailer.Mailer(self.config)
 
-        if "timeout" in self.config:
-            self.timeout = self.config["timeout"]
+        if "timeout" in self.config.job:
+            self.timeout = self.config.job["timeout"]
         else:
             self.timeout = DEFAULT_TIMEOUT
 
-        if "disabled" in self.config and self.config["disabled"] is True:
+        if "disabled" in self.config.job and self.config.job["disabled"] is 
True:
             self.enabled = False
         else:
             self.enabled = True
 
-        if "schedule" not in self.config:
+        if "schedule" not in self.config.job:
             self.enabled = False
 
     def run(self):
         lock.begin(job_tag=self.name)
 
-        command = shlex.split(self.config["command"])
+        command = shlex.split(self.config.job["command"])
 
         self.process = subprocess.Popen(command, stdout=subprocess.PIPE, 
stderr=subprocess.PIPE)
         timer = threading.Timer(self.timeout, self.fail_timeout)
@@ -82,10 +80,10 @@
         # FIXME: Job will return SIGKILL now, fail_exitcode should ignore that 
signal now?
 
     def store_job_output(self, stdout_data):
-        if "stdout_destination" not in self.config:
+        if "stdout_destination" not in self.config.job:
             return
 
-        destination = self.config["stdout_destination"]
+        destination = self.config.job["stdout_destination"]
         out = open(destination, "a")
 
         header = (
@@ -97,15 +95,3 @@
 
         out.write(stdout_data.decode("utf-8"))
 
-    def validate_config(self):
-        assert "name" in self.config
-        assert "command" in self.config
-        if "schedule" in self.config:
-            # No tricky assignments.
-            assert "=" not in self.config["schedule"]
-            # Legal cron, but I don't want to deal with it.
-            assert "@" not in self.config["schedule"]
-
-            # Be sure the schedule is valid.
-            terms = self.config["schedule"].split()
-            assert len(terms) == 5

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I64d0e15af51415cc7537d8e2acbe7d5a90ab4f84
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/process-control
Gerrit-Branch: master
Gerrit-Owner: Ejegg <eeggles...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to