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 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)

View File

@ -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

View File

@ -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.',