From fa5175fee5e0cd1ed2aa7d8b3d63b39cc3977eec Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Sun, 22 May 2016 09:45:08 +0200 Subject: [PATCH] Allow both hosts sources: command line args and file. --- cdist/config.py | 26 +++++++++++++++----------- docs/man/man1/cdist.text | 7 ++++--- scripts/cdist | 7 ++++--- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/cdist/config.py b/cdist/config.py index fd08d460..f5e62ce1 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -26,6 +26,7 @@ import shutil import sys import time import pprint +import itertools import cdist @@ -61,11 +62,17 @@ class Config(object): """ if isinstance(source, str): import fileinput - for host in fileinput.input(files=(source)): - yield host.strip() # remove leading and trailing whitespace + try: + for host in fileinput.input(files=(source)): + # remove leading and trailing whitespace + yield host.strip() + except (IOError, OSError) as e: + raise cdist.Error("Error reading hosts from \'{}\'".format( + source)) else: - for host in source: - yield host + if source: + for host in source: + yield host @classmethod @@ -77,7 +84,8 @@ class Config(object): log = logging.getLogger("cdist") if args.manifest == '-' and args.hostfile == '-': - raise cdist.Error("Cannot read both, manifest and host file, from stdin") + raise cdist.Error(("Cannot read both, manifest and host file, " + "from stdin")) # if no host source is specified then read hosts from stdin if not (args.hostfile or args.host): args.hostfile = '-' @@ -102,12 +110,8 @@ class Config(object): time_start = time.time() hostcnt = 0 - if args.host: - host_source = args.host - else: - host_source = args.hostfile - - for host in cls.hosts(host_source): + for host in itertools.chain(cls.hosts(args.host), + cls.hosts(args.hostfile)): hostcnt += 1 if args.parallel: log.debug("Creating child process for %s", host) diff --git a/docs/man/man1/cdist.text b/docs/man/man1/cdist.text index ed2ce805..4e45ae81 100644 --- a/docs/man/man1/cdist.text +++ b/docs/man/man1/cdist.text @@ -64,9 +64,10 @@ Configure one or more hosts environment variable. -f HOSTFILE, --file HOSTFILE:: - Read hosts to operate on from specified file or from stdin if '-' - (each host on separate line). If no host or host file is - specified then, by default, read hosts from stdin. + Read additional hosts to operate on from specified file + or from stdin if '-' (each host on separate line). + If no host or host file is specified then, by default, + read hosts from stdin. -i MANIFEST, --initial-manifest MANIFEST:: Path to a cdist manifest or - to read from stdin diff --git a/scripts/cdist b/scripts/cdist index 0cfd01d6..ccdb5232 100755 --- a/scripts/cdist +++ b/scripts/cdist @@ -90,9 +90,10 @@ def commandline(): help=('Add configuration directory (can be repeated, ' 'last one wins)'), action='append') parser['config'].add_argument('-f', '--file', - help=('Read hosts to operate on from specified file or from stdin ' - 'if \'-\' (each host on separate line). If no host or host ' - 'file is specified then, by default, read hosts from stdin.'), + help=('Read additional hosts to operate on from specified file ' + 'or from stdin if \'-\' (each host on separate line). ' + 'If no host or host file is specified then, by default, ' + 'read hosts from stdin.'), dest='hostfile', required=False) parser['config'].add_argument('-i', '--initial-manifest', help='Path to a cdist manifest or \'-\' to read from stdin.',