Allow both hosts sources: command line args and file.

This commit is contained in:
Darko Poljak 2016-05-22 09:45:08 +02:00
parent 4fce4a631c
commit fa5175fee5
3 changed files with 23 additions and 17 deletions

View file

@ -26,6 +26,7 @@ import shutil
import sys import sys
import time import time
import pprint import pprint
import itertools
import cdist import cdist
@ -61,9 +62,15 @@ class Config(object):
""" """
if isinstance(source, str): if isinstance(source, str):
import fileinput import fileinput
try:
for host in fileinput.input(files=(source)): for host in fileinput.input(files=(source)):
yield host.strip() # remove leading and trailing whitespace # remove leading and trailing whitespace
yield host.strip()
except (IOError, OSError) as e:
raise cdist.Error("Error reading hosts from \'{}\'".format(
source))
else: else:
if source:
for host in source: for host in source:
yield host yield host
@ -77,7 +84,8 @@ class Config(object):
log = logging.getLogger("cdist") log = logging.getLogger("cdist")
if args.manifest == '-' and args.hostfile == '-': 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 no host source is specified then read hosts from stdin
if not (args.hostfile or args.host): if not (args.hostfile or args.host):
args.hostfile = '-' args.hostfile = '-'
@ -102,12 +110,8 @@ class Config(object):
time_start = time.time() time_start = time.time()
hostcnt = 0 hostcnt = 0
if args.host: for host in itertools.chain(cls.hosts(args.host),
host_source = args.host cls.hosts(args.hostfile)):
else:
host_source = args.hostfile
for host in cls.hosts(host_source):
hostcnt += 1 hostcnt += 1
if args.parallel: if args.parallel:
log.debug("Creating child process for %s", host) log.debug("Creating child process for %s", host)

View file

@ -64,9 +64,10 @@ Configure one or more hosts
environment variable. environment variable.
-f HOSTFILE, --file HOSTFILE:: -f HOSTFILE, --file HOSTFILE::
Read hosts to operate on from specified file or from stdin if '-' Read additional hosts to operate on from specified file
(each host on separate line). If no host or host file is or from stdin if '-' (each host on separate line).
specified then, by default, read hosts from stdin. If no host or host file is specified then, by default,
read hosts from stdin.
-i MANIFEST, --initial-manifest MANIFEST:: -i MANIFEST, --initial-manifest MANIFEST::
Path to a cdist manifest or - to read from stdin Path to a cdist manifest or - to read from stdin

View file

@ -90,9 +90,10 @@ def commandline():
help=('Add configuration directory (can be repeated, ' help=('Add configuration directory (can be repeated, '
'last one wins)'), action='append') 'last one wins)'), action='append')
parser['config'].add_argument('-f', '--file', parser['config'].add_argument('-f', '--file',
help=('Read hosts to operate on from specified file or from stdin ' help=('Read additional hosts to operate on from specified file '
'if \'-\' (each host on separate line). If no host or host ' 'or from stdin if \'-\' (each host on separate line). '
'file is specified then, by default, read hosts from stdin.'), 'If no host or host file is specified then, by default, '
'read hosts from stdin.'),
dest='hostfile', required=False) dest='hostfile', required=False)
parser['config'].add_argument('-i', '--initial-manifest', parser['config'].add_argument('-i', '--initial-manifest',
help='Path to a cdist manifest or \'-\' to read from stdin.', help='Path to a cdist manifest or \'-\' to read from stdin.',