2016-10-24 13:58:39 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2016 Nico Schottelius (nico-cdist at schottelius.org)
|
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import logging
|
2016-10-25 10:38:13 +00:00
|
|
|
import re
|
|
|
|
import socket
|
|
|
|
import http.server
|
2016-10-30 19:25:32 +00:00
|
|
|
import socketserver
|
2016-10-25 10:38:13 +00:00
|
|
|
|
2016-10-24 13:58:39 +00:00
|
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
|
2016-10-25 10:38:13 +00:00
|
|
|
import multiprocessing
|
2016-10-24 13:58:39 +00:00
|
|
|
|
2016-10-30 14:01:45 +00:00
|
|
|
import cdist.config
|
|
|
|
import cdist.install
|
2016-10-25 10:38:13 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2016-10-24 13:58:39 +00:00
|
|
|
|
|
|
|
class Trigger():
|
|
|
|
"""cdist trigger handling"""
|
|
|
|
|
2016-10-30 15:41:59 +00:00
|
|
|
def __init__(self, http_port=None, dry_run=False, ipv4only=False,
|
|
|
|
cdistargs=None):
|
2016-10-24 13:58:39 +00:00
|
|
|
self.log = logging.getLogger("trigger")
|
|
|
|
self.dry_run = dry_run
|
2016-10-25 10:38:13 +00:00
|
|
|
self.http_port = int(http_port)
|
|
|
|
self.ipv4only = ipv4only
|
|
|
|
|
2016-10-30 15:41:59 +00:00
|
|
|
self.args = cdistargs
|
2016-10-30 14:01:45 +00:00
|
|
|
|
2016-10-25 10:38:13 +00:00
|
|
|
# can only be set once
|
|
|
|
multiprocessing.set_start_method('forkserver')
|
|
|
|
|
|
|
|
def run_httpd(self):
|
|
|
|
server_address = ('', self.http_port)
|
|
|
|
|
|
|
|
if self.ipv4only:
|
2016-10-30 14:01:45 +00:00
|
|
|
httpd = HTTPServerV4(self.args, server_address, TriggerHttp)
|
2016-10-25 10:38:13 +00:00
|
|
|
else:
|
2016-10-30 14:01:45 +00:00
|
|
|
httpd = HTTPServerV6(self.args, server_address, TriggerHttp)
|
2016-10-24 13:58:39 +00:00
|
|
|
|
|
|
|
httpd.serve_forever()
|
|
|
|
|
2016-10-25 10:38:13 +00:00
|
|
|
def run(self):
|
|
|
|
if self.http_port:
|
|
|
|
self.run_httpd()
|
|
|
|
|
2016-10-24 13:58:39 +00:00
|
|
|
@staticmethod
|
|
|
|
def commandline(args):
|
2016-10-30 15:41:59 +00:00
|
|
|
http_port = args.http_port
|
|
|
|
ipv4only = args.ipv4
|
|
|
|
del args.http_port
|
|
|
|
del args.ipv4
|
|
|
|
t = Trigger(http_port=http_port, ipv4only=ipv4only, cdistargs=args)
|
2016-10-25 10:38:13 +00:00
|
|
|
t.run()
|
2016-10-24 13:58:39 +00:00
|
|
|
|
|
|
|
class TriggerHttp(BaseHTTPRequestHandler):
|
|
|
|
def do_GET(self):
|
2016-10-25 10:38:13 +00:00
|
|
|
host = self.client_address[0]
|
|
|
|
code = 200
|
|
|
|
mode = None
|
|
|
|
|
2016-10-30 15:41:59 +00:00
|
|
|
self.cdistargs = self.server.cdistargs
|
|
|
|
print(self.cdistargs)
|
|
|
|
print('path: ' + str(self.path))
|
2016-10-30 14:01:45 +00:00
|
|
|
|
2016-10-25 10:38:13 +00:00
|
|
|
m = re.match("^/(?P<mode>config|install)/.*", self.path)
|
|
|
|
if m:
|
|
|
|
mode = m.group('mode')
|
|
|
|
else:
|
|
|
|
code = 404
|
2016-10-30 15:41:59 +00:00
|
|
|
print('mode: ' + str(mode))
|
2016-10-25 10:38:13 +00:00
|
|
|
|
|
|
|
if mode:
|
|
|
|
self.run_cdist(mode, host)
|
|
|
|
|
|
|
|
self.send_response(code)
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def do_HEAD(self):
|
|
|
|
self.do_GET()
|
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
self.do_GET()
|
|
|
|
|
|
|
|
def run_cdist(self, mode, host):
|
|
|
|
log.debug("Running cdist {} {}".format(mode, host))
|
|
|
|
|
2016-10-30 14:01:45 +00:00
|
|
|
cname = mode.title()
|
|
|
|
module = getattr(cdist, mode)
|
|
|
|
theclass = getattr(module, cname)
|
2016-10-25 10:38:13 +00:00
|
|
|
|
2016-10-30 15:41:59 +00:00
|
|
|
if hasattr(self.cdistargs, 'out_path'):
|
|
|
|
out_path = self.cdistargs.out_path
|
|
|
|
else:
|
|
|
|
out_path = None
|
|
|
|
host_base_path, hostdir = theclass.create_host_base_dirs(
|
|
|
|
host, theclass.create_base_root_path(out_path))
|
|
|
|
theclass.construct_remote_exec_copy_patterns(self.cdistargs)
|
|
|
|
theclass.onehost(host, host_base_path, hostdir, self.cdistargs,
|
|
|
|
parallel=False)
|
2016-10-25 10:38:13 +00:00
|
|
|
|
|
|
|
|
2016-10-30 19:25:32 +00:00
|
|
|
class HTTPServerV6(socketserver.ForkingMixIn, http.server.HTTPServer):
|
2016-10-25 10:38:13 +00:00
|
|
|
"""
|
2016-10-30 13:49:07 +00:00
|
|
|
Server that listens both to IPv4 and IPv6 requests.
|
2016-10-25 10:38:13 +00:00
|
|
|
"""
|
|
|
|
address_family = socket.AF_INET6
|
2016-10-30 14:01:45 +00:00
|
|
|
|
|
|
|
def __init__(self, cdistargs, *args, **kwargs):
|
|
|
|
self.cdistargs = cdistargs
|
|
|
|
http.server.HTTPServer.__init__(self, *args, **kwargs)
|
|
|
|
|
|
|
|
class HTTPServerV4(HTTPServerV6):
|
|
|
|
"""
|
2016-10-30 14:09:26 +00:00
|
|
|
Server that listens to IPv4 requests
|
2016-10-30 14:01:45 +00:00
|
|
|
"""
|
|
|
|
address_family = socket.AF_INET
|