begin to add local file support

Signed-off-by: Nico Schottelius <nico@wurzel.schottelius.org>
This commit is contained in:
Nico Schottelius 2016-11-27 15:59:48 +01:00
parent ab5d941802
commit ff31dcada0
2 changed files with 33 additions and 9 deletions

View file

@ -36,13 +36,20 @@ log = logging.getLogger(__name__)
class Trigger(): class Trigger():
"""cdist trigger handling""" """cdist trigger handling"""
# Arguments that are only trigger specific
triggers_args = [ "http_port", "ipv6", "directory", "content" ]
def __init__(self, http_port=None, dry_run=False, ipv6=False, def __init__(self, http_port=None, dry_run=False, ipv6=False,
cdistargs=None): directory=None, content=None, cdistargs=None):
self.log = logging.getLogger("trigger") self.log = logging.getLogger("trigger")
self.dry_run = dry_run self.dry_run = dry_run
self.http_port = int(http_port) self.http_port = int(http_port)
self.ipv6 = ipv6 self.ipv6 = ipv6
self.args = cdistargs self.args = cdistargs
self.directory = directory
self.content = content
log.debug("IPv6: %s", self.ipv6) log.debug("IPv6: %s", self.ipv6)
def run_httpd(self): def run_httpd(self):
@ -63,14 +70,19 @@ class Trigger():
if self.http_port: if self.http_port:
self.run_httpd() self.run_httpd()
@staticmethod @classmethod
def commandline(args): def commandline(cls, args):
http_port = args.http_port http_port = args.http_port
ipv6 = args.ipv6 ipv6 = args.ipv6
del args.http_port
del args.ipv6 ownargs = {}
t = Trigger(http_port=http_port, dry_run=args.dry_run, ipv6=ipv6, for targ in cls.triggers_args:
cdistargs=args) arg = getattr(args, targ)
ownargs[targ] = arg
del arg
t = cls(**ownargs, dry_run=args.dry_run, cdistargs=args)
t.run() t.run()
class TriggerHttp(http.server.BaseHTTPRequestHandler): class TriggerHttp(http.server.BaseHTTPRequestHandler):
@ -81,11 +93,14 @@ class TriggerHttp(http.server.BaseHTTPRequestHandler):
self.cdistargs = self.server.cdistargs self.cdistargs = self.server.cdistargs
m = re.match("^/(?P<mode>config|install)/.*", self.path) m = re.match("^/(?P<subsystem>cdist|file)/(?P<action>create|delete|config|install)/", "/cdist/install/").group('subsystem')
if m: if m:
mode = m.group('mode') subsystem = m.group('subsystem')
action = m.group('action')
else: else:
code = 404 code = 404
if mode: if mode:
log.debug("Running cdist for %s in mode %s", host, mode) log.debug("Running cdist for %s in mode %s", host, mode)
if self.server.dry_run: if self.server.dry_run:

View file

@ -191,6 +191,15 @@ def commandline():
parser['trigger'].add_argument( parser['trigger'].add_argument(
'-H', '--http-port', action='store', default=3000, required=False, '-H', '--http-port', action='store', default=3000, required=False,
help=('Create trigger listener via http on specified port')) help=('Create trigger listener via http on specified port'))
parser['trigger'].add_argument(
'-D', '--directory', action='store', required=False,
help=('Where to create local files'))
parser['trigger'].add_argument(
'-C', '--content', action='store', required=False,
help=('What to store in created files'))
parser['trigger'].set_defaults(func=cdist.trigger.Trigger.commandline) parser['trigger'].set_defaults(func=cdist.trigger.Trigger.commandline)
# Install # Install