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.local.create_files_dirs()
self.remote.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 @staticmethod
def hosts(source): def hosts(source):
"""Yield hosts from source. """Yield hosts from source.
@ -99,11 +118,13 @@ class Config(object):
import fileinput import fileinput
try: try:
for host in fileinput.input(files=(source)): for host in fileinput.input(files=(source)):
# remove leading and trailing whitespace host = Config.hostfile_process_line(host)
yield host.strip() if host:
except (IOError, OSError) as e: yield host
raise cdist.Error("Error reading hosts from \'{}\'".format( except (IOError, OSError, UnicodeError) as e:
source)) raise cdist.Error(
"Error reading hosts from file \'{}\': {}".format(
source, e))
else: else:
if source: if source:
for host in source: for host in source:

View file

@ -1,6 +1,9 @@
Changelog Changelog
--------- ---------
next:
* Core: Improve hostfile: support comments, skip empty lines (Darko Poljak)
4.3.0: 2016-08-19 4.3.0: 2016-08-19
* Documentation: Add Parallelization chapter (Darko Poljak) * Documentation: Add Parallelization chapter (Darko Poljak)
* Core: Add -b, --enable-beta option for enabling beta functionalities (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 Read additional hosts to operate on from specified file
or from stdin if '-' (each host on separate line). or from stdin if '-' (each host on separate line).
If no host or host file is specified then, by default, 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 .. 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) 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 SHELL
----- -----
This command allows you to spawn a shell that enables access This command allows you to spawn a shell that enables access