Merge pull request #627 from darko-poljak/force_ipv4_ipv6_options

Add -4 and -6 params to force IPv4, IPv6 addresses respectively.
This commit is contained in:
Darko Poljak 2018-02-15 21:16:45 +01:00 committed by GitHub
commit b5f38abd39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 11 deletions

View file

@ -224,6 +224,16 @@ def get_parsers():
# Config # Config
parser['config_args'] = argparse.ArgumentParser(add_help=False) parser['config_args'] = argparse.ArgumentParser(add_help=False)
parser['config_args'].add_argument(
'-4', '--force-ipv4',
help=('Force to use IPv4 addresses only. No influence for custom'
' remote commands.'),
action='store_const', dest='force_ipv', const=4)
parser['config_args'].add_argument(
'-6', '--force-ipv6',
help=('Force to use IPv6 addresses only. No influence for custom'
' remote commands.'),
action='store_const', dest='force_ipv', const=6)
parser['config_args'].add_argument( parser['config_args'].add_argument(
'-A', '--all-tagged', '-A', '--all-tagged',
help=('Use all hosts present in tags db. Currently in beta.'), help=('Use all hosts present in tags db. Currently in beta.'),

View file

@ -32,6 +32,7 @@ import multiprocessing
from cdist.mputil import mp_pool_run, mp_sig_handler from cdist.mputil import mp_pool_run, mp_sig_handler
import atexit import atexit
import shutil import shutil
import socket
import cdist import cdist
import cdist.hostsource import cdist.hostsource
import cdist.exec.local import cdist.exec.local
@ -110,6 +111,13 @@ class Config(object):
args.remote_exec_pattern = None args.remote_exec_pattern = None
args.remote_copy_pattern = None args.remote_copy_pattern = None
# Determine forcing IPv4/IPv6 options if any, only for
# default remote commands.
if args.force_ipv:
force_addr_opt = " -{}".format(args.force_ipv)
else:
force_addr_opt = ""
args_dict = vars(args) args_dict = vars(args)
# if remote-exec and/or remote-copy args are None then user # if remote-exec and/or remote-copy args are None then user
# didn't specify command line options nor env vars: # didn't specify command line options nor env vars:
@ -118,9 +126,11 @@ class Config(object):
args_dict['remote_exec'] is None): args_dict['remote_exec'] is None):
mux_opts = inspect_ssh_mux_opts() mux_opts = inspect_ssh_mux_opts()
if args_dict['remote_exec'] is None: if args_dict['remote_exec'] is None:
args.remote_exec_pattern = cdist.REMOTE_EXEC + mux_opts args.remote_exec_pattern = (cdist.REMOTE_EXEC +
force_addr_opt + mux_opts)
if args_dict['remote_copy'] is None: if args_dict['remote_copy'] is None:
args.remote_copy_pattern = cdist.REMOTE_COPY + mux_opts args.remote_copy_pattern = (cdist.REMOTE_COPY +
force_addr_opt + mux_opts)
if mux_opts: if mux_opts:
cleanup_pattern = cdist.REMOTE_CMDS_CLEANUP_PATTERN cleanup_pattern = cdist.REMOTE_CMDS_CLEANUP_PATTERN
else: else:
@ -313,6 +323,16 @@ class Config(object):
remote_cmds_cleanup = "" remote_cmds_cleanup = ""
return (remote_exec, remote_copy, remote_cmds_cleanup, ) return (remote_exec, remote_copy, remote_cmds_cleanup, )
@staticmethod
def _address_family(args):
if args.force_ipv == 4:
family = socket.AF_INET
elif args.force_ipv == 6:
family = socket.AF_INET6
else:
family = 0
return family
@classmethod @classmethod
def onehost(cls, host, host_tags, host_base_path, host_dir_name, args, def onehost(cls, host, host_tags, host_base_path, host_dir_name, args,
parallel, configuration, remove_remote_files_dirs=False): parallel, configuration, remove_remote_files_dirs=False):
@ -331,7 +351,9 @@ class Config(object):
log.debug("remote_copy for host \"{}\": {}".format( log.debug("remote_copy for host \"{}\": {}".format(
host, remote_copy)) host, remote_copy))
target_host = ipaddr.resolve_target_addresses(host) family = cls._address_family(args)
log.debug("address family: {}".format(family))
target_host = ipaddr.resolve_target_addresses(host, family)
log.debug("target_host for host \"{}\": {}".format( log.debug("target_host for host \"{}\": {}".format(
host, target_host)) host, target_host))

View file

@ -23,13 +23,13 @@ import socket
import logging import logging
def resolve_target_addresses(host): def resolve_target_addresses(host, family=0):
host_name = resolve_target_host_name(host) host_name = resolve_target_host_name(host, family)
host_fqdn = resolve_target_fqdn(host) host_fqdn = resolve_target_fqdn(host)
return (host, host_name, host_fqdn) return (host, host_name, host_fqdn)
def resolve_target_host_name(host): def resolve_target_host_name(host, family=0):
log = logging.getLogger(host) log = logging.getLogger(host)
try: try:
# getaddrinfo returns a list of 5-tuples: # getaddrinfo returns a list of 5-tuples:
@ -38,7 +38,7 @@ def resolve_target_host_name(host):
# (address, port) for AF_INET, # (address, port) for AF_INET,
# (address, port, flow_info, scopeid) for AF_INET6 # (address, port, flow_info, scopeid) for AF_INET6
ip_addr = socket.getaddrinfo( ip_addr = socket.getaddrinfo(
host, None, type=socket.SOCK_STREAM)[0][4][0] host, None, family=family, type=socket.SOCK_STREAM)[0][4][0]
# gethostbyaddr returns triple # gethostbyaddr returns triple
# (hostname, aliaslist, ipaddrlist) # (hostname, aliaslist, ipaddrlist)
host_name = socket.gethostbyaddr(ip_addr)[0] host_name = socket.gethostbyaddr(ip_addr)[0]

View file

@ -20,16 +20,16 @@ SYNOPSIS
[-C CACHE_PATH_PATTERN] [-c CONF_DIR] [-i MANIFEST] [-C CACHE_PATH_PATTERN] [-c CONF_DIR] [-i MANIFEST]
[-j [JOBS]] [-n] [-o OUT_PATH] [-R [{tar,tgz,tbz2,txz}]] [-j [JOBS]] [-n] [-o OUT_PATH] [-R [{tar,tgz,tbz2,txz}]]
[-r REMOTE_OUT_DIR] [--remote-copy REMOTE_COPY] [-r REMOTE_OUT_DIR] [--remote-copy REMOTE_COPY]
[--remote-exec REMOTE_EXEC] [-I INVENTORY_DIR] [-A] [-a] [--remote-exec REMOTE_EXEC] [-I INVENTORY_DIR] [-4] [-6]
[-f HOSTFILE] [-p [HOST_MAX]] [-S] [-s] [-t] [-A] [-a] [-f HOSTFILE] [-p [HOST_MAX]] [-S] [-s] [-t]
[host [host ...]] [host [host ...]]
cdist install [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-g CONFIG_FILE] cdist install [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-g CONFIG_FILE]
[-C CACHE_PATH_PATTERN] [-c CONF_DIR] [-i MANIFEST] [-C CACHE_PATH_PATTERN] [-c CONF_DIR] [-i MANIFEST]
[-j [JOBS]] [-n] [-o OUT_PATH] [-R [{tar,tgz,tbz2,txz}]] [-j [JOBS]] [-n] [-o OUT_PATH] [-R [{tar,tgz,tbz2,txz}]]
[-r REMOTE_OUT_DIR] [--remote-copy REMOTE_COPY] [-r REMOTE_OUT_DIR] [--remote-copy REMOTE_COPY]
[--remote-exec REMOTE_EXEC] [-I INVENTORY_DIR] [-A] [-a] [--remote-exec REMOTE_EXEC] [-I INVENTORY_DIR] [-4] [-6]
[-f HOSTFILE] [-p [HOST_MAX]] [-S] [-s] [-t] [-A] [-a] [-f HOSTFILE] [-p [HOST_MAX]] [-S] [-s] [-t]
[host [host ...]] [host [host ...]]
cdist inventory [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-g CONFIG_FILE] cdist inventory [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-g CONFIG_FILE]
@ -118,6 +118,16 @@ CONFIG/INSTALL
Configure/install one or more hosts. Configure/install one or more hosts.
Install command is currently in beta. Install command is currently in beta.
.. option:: -4, --force-ipv4
Force to use IPv4 addresses only. No influence for
custom remote commands.
.. option:: -6, --force-ipv6
Force to use IPv6 addresses only. No influence for
custom remote commands.
.. option:: -A, --all-tagged .. option:: -A, --all-tagged
Use all hosts present in tags db. Currently in beta. Use all hosts present in tags db. Currently in beta.