From 535181435f9bdae9c1589251442187f33419ee1c Mon Sep 17 00:00:00 2001
From: Nico Schottelius <nico@wurzel.schottelius.org>
Date: Sun, 30 Oct 2016 15:01:45 +0100
Subject: [PATCH] update for darko

Signed-off-by: Nico Schottelius <nico@wurzel.schottelius.org>
---
 cdist/config.py  | 14 +++++++++++++-
 cdist/trigger.py | 38 +++++++++++++++++++++++++++-----------
 scripts/cdist    |  1 +
 3 files changed, 41 insertions(+), 12 deletions(-)

diff --git a/cdist/config.py b/cdist/config.py
index b0131601..14c99fe8 100644
--- a/cdist/config.py
+++ b/cdist/config.py
@@ -136,7 +136,7 @@ class Config(object):
         import multiprocessing
 
         # FIXME: Refactor relict - remove later
-        log = logging.getLogger("cdist")
+        log = logging.getLogger(__name__)
 
         if args.manifest == '-' and args.hostfile == '-':
             raise cdist.Error(("Cannot read both, manifest and host file, "
@@ -227,6 +227,8 @@ class Config(object):
             raise cdist.Error("Failed to configure the following hosts: " +
                               " ".join(failed_hosts))
 
+
+
     @classmethod
     def onehost(cls, host, host_base_path, host_dir_name, args, parallel):
         """Configure ONE system"""
@@ -317,6 +319,16 @@ class Config(object):
             else:
                 raise
 
+
+    # FIXME begin to cleanup with this method
+    @staticmethod
+    def create_host_tmpdir(host):
+        base_dir = tempfile.mkdtemp()
+        hostdir = cdist.str_hash(host)
+
+        return (base_dir, hostdir)
+
+
     def run(self):
         """Do what is most often done: deploy & cleanup"""
         start_time = time.time()
diff --git a/cdist/trigger.py b/cdist/trigger.py
index b53f4ad1..b8c4611f 100644
--- a/cdist/trigger.py
+++ b/cdist/trigger.py
@@ -29,7 +29,8 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
 
 import multiprocessing
 
-import cdist
+import cdist.config
+import cdist.install
 
 log = logging.getLogger(__name__)
 
@@ -42,16 +43,22 @@ class Trigger():
         self.http_port = int(http_port)
         self.ipv4only = ipv4only
 
+        self.args = "fun"
+
         # can only be set once
         multiprocessing.set_start_method('forkserver')
 
+    # Create pool suitable for passing objects
+    def __init_pool(self):
+        pass
+
     def run_httpd(self):
         server_address = ('', self.http_port)
 
         if self.ipv4only:
-            httpd = HTTPServerV4(server_address, TriggerHttp)
+            httpd = HTTPServerV4(self.args, server_address, TriggerHttp)
         else:
-            httpd = HTTPServerV6(server_address, TriggerHttp)
+            httpd = HTTPServerV6(self.args, server_address, TriggerHttp)
 
         httpd.serve_forever()
 
@@ -65,15 +72,14 @@ class Trigger():
         t.run()
 
 class TriggerHttp(BaseHTTPRequestHandler):
-    def __init__(self, *args, **kwargs):
-        http.server.BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
-
     def do_GET(self):
         # FIXME: dispatch to pool instead of single process
         host = self.client_address[0]
         code = 200
         mode = None
 
+        print(self.server.cdistargs)
+
         m = re.match("^/(?P<mode>config|install)/.*", self.path)
         if m:
             mode = m.group('mode')
@@ -95,12 +101,12 @@ class TriggerHttp(BaseHTTPRequestHandler):
     def run_cdist(self, mode, host):
         log.debug("Running cdist {} {}".format(mode, host))
 
+        cname = mode.title()
+        module = getattr(cdist, mode)
+        theclass = getattr(module, cname)
 
-class HTTPServerV4(http.server.HTTPServer):
-    """
-    Server that listens to IPv4 and IPv6 requests
-    """
-    address_family = socket.AF_INET
+        host_base_path, hostdir = theclass.create_host_tmpdir(host)
+        theclass.onehost(host, host_base_path, hostdir, args, parallel=False)
 
 
 class HTTPServerV6(http.server.HTTPServer):
@@ -108,3 +114,13 @@ class HTTPServerV6(http.server.HTTPServer):
     Server that listens both to IPv4 and IPv6 requests
     """
     address_family = socket.AF_INET6
+
+    def __init__(self, cdistargs, *args, **kwargs):
+        self.cdistargs = cdistargs
+        http.server.HTTPServer.__init__(self, *args, **kwargs)
+
+class HTTPServerV4(HTTPServerV6):
+    """
+    Server that listens to IPv4 and IPv6 requests
+    """
+    address_family = socket.AF_INET
diff --git a/scripts/cdist b/scripts/cdist
index 8c6b62db..8077c43a 100755
--- a/scripts/cdist
+++ b/scripts/cdist
@@ -150,6 +150,7 @@ def commandline():
            '-s', '--sequential',
            help='Operate on multiple hosts sequentially (default)',
            action='store_false', dest='parallel')
+
     # remote-copy and remote-exec defaults are environment variables
     # if set; if not then None - these will be futher handled after
     # parsing to determine implementation default