Merge pull request #476 from darko-poljak/improve_hostfile

Support comments in hostfile, skip empty lines.
This commit is contained in:
Darko Poljak 2016-08-21 21:48:35 +02:00 committed by GitHub
commit a2a0a2a543
3 changed files with 44 additions and 6 deletions

View File

@ -89,6 +89,25 @@ class Config(object):
self.local.create_files_dirs()
self.remote.create_files_dirs()
@staticmethod
def hostfile_process_line(line):
"""Return host from read line or None if no host present."""
if not line:
return None
# remove comment if present
comment_index = line.find('#')
if comment_index >= 0:
host = line[:comment_index]
else:
host = line
# remove leading and trailing whitespaces
host = host.strip()
# skip empty lines
if host:
return host
else:
return None
@staticmethod
def hosts(source):
"""Yield hosts from source.
@ -99,11 +118,13 @@ class Config(object):
import fileinput
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))
host = Config.hostfile_process_line(host)
if host:
yield host
except (IOError, OSError, UnicodeError) as e:
raise cdist.Error(
"Error reading hosts from file \'{}\': {}".format(
source, e))
else:
if source:
for host in source:

View File

@ -1,6 +1,9 @@
Changelog
---------
next:
* Core: Improve hostfile: support comments, skip empty lines (Darko Poljak)
4.3.0: 2016-08-19
* Documentation: Add Parallelization chapter (Darko Poljak)
* Core: Add -b, --enable-beta option for enabling beta functionalities (Darko Poljak)

View File

@ -82,7 +82,7 @@ Configure one or more hosts.
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.
read hosts from stdin. For the file format see below.
.. option:: -i MANIFEST, --initial-manifest MANIFEST
@ -117,6 +117,20 @@ Configure one or more hosts.
Command to use for remote execution (should behave like ssh)
HOSTFILE FORMAT
~~~~~~~~~~~~~~~
HOSTFILE contains hosts per line.
All characters after and including '#' until the end of line is a comment.
In a line, all leading and trailing whitespace characters are ignored.
Empty lines are ignored/skipped.
Hostfile line is processed like the following. First, all comments are
removed. Then all leading and trailing whitespace characters are stripped.
If such a line results in empty line it is ignored/skipped. Otherwise,
host string is used.
SHELL
-----
This command allows you to spawn a shell that enables access