Add ssh mux options by default if available.

This commit is contained in:
Darko Poljak 2016-03-18 23:34:02 +01:00
parent b4ac23b4f8
commit d0d0c258d6
3 changed files with 44 additions and 27 deletions

View File

@ -249,6 +249,12 @@ CDIST_OVERRIDE::
CDIST_ORDER_DEPENDENCY:: CDIST_ORDER_DEPENDENCY::
Create dependencies based on the execution order (see cdist-manifest(7)) Create dependencies based on the execution order (see cdist-manifest(7))
CDIST_REMOTE_EXEC::
Use this command for remote execution (should behave like ssh)
CDIST_REMOTE_COPY::
Use this command for remote copy (should behave like scp)
SEE ALSO SEE ALSO
-------- --------
- cdist(1) - cdist(1)

View File

@ -138,6 +138,11 @@ CDIST_LOCAL_SHELL::
CDIST_REMOTE_SHELL:: CDIST_REMOTE_SHELL::
Selects shell for remote scirpt execution, defaults to /bin/sh Selects shell for remote scirpt execution, defaults to /bin/sh
CDIST_REMOTE_EXEC::
Use this command for remote execution (should behave like ssh)
CDIST_REMOTE_COPY::
Use this command for remote copy (should behave like scp)
EXIT STATUS EXIT STATUS
----------- -----------

View File

@ -21,33 +21,27 @@
# #
# #
def inspect_ssh_mux_opts(): def inspect_ssh_mux_opts(control_path_dir="~/.ssh/"):
import subprocess
"""Inspect whether or not ssh supports multiplexing options""" """Inspect whether or not ssh supports multiplexing options"""
import subprocess
import os
control_path = os.path.join(control_path_dir, "cdist.master-%l-%r@%h:%p")
wanted_mux_opts = { wanted_mux_opts = {
"ControlPath":"~/.ssh/master-%l-%r@%h:%p", "ControlPath": control_path,
"ControlMaster":"auto", "ControlMaster": "auto",
"ControlPersist":"125", "ControlPersist": "125",
} }
# if checked key option is present then this assumes mux_opts = " ".join([" -o {}={}".format(x,
# all options in value are present wanted_mux_opts[x]) for x in wanted_mux_opts])
check = { try:
"ControlMaster": ("ControlMaster", "ControlPath"), subprocess.check_output("ssh {}".format(mux_opts),
"ControlPersist": ("ControlPersist",), stderr=subprocess.STDOUT, shell=True)
} except subprocess.CalledProcessError as e:
mux_opts = {} subproc_output = e.output.decode().lower()
for x in check: if "bad configuration option" in subproc_output:
try: return ""
subprocess.check_output("ssh -o {}".format(x), return mux_opts
stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError as e:
foo = e.output.decode().lower()
if not "bad configuration option" in foo:
for o in check[x]:
mux_opts[o] = wanted_mux_opts[o]
foo = [" -o {}={}".format(x, mux_opts[x]) for x in mux_opts]
return " ".join(foo)
def commandline(): def commandline():
"""Parse command line""" """Parse command line"""
@ -56,6 +50,9 @@ def commandline():
import cdist.banner import cdist.banner
import cdist.config import cdist.config
import cdist.shell import cdist.shell
import tempfile
import shutil
import os
# Construct parser others can reuse # Construct parser others can reuse
parser = {} parser = {}
@ -83,9 +80,18 @@ def commandline():
# Config # Config
# inspect multiplexing options for default remote copy/exec scp/ssh # inspect multiplexing options for default remote copy/exec scp/ssh
MUX_OPTS = inspect_ssh_mux_opts() # but not if env vars are present
cdist.REMOTE_EXEC += MUX_OPTS has_env_remote_exec = "CDIST_REMOTE_EXEC" in os.environ
cdist.REMOTE_COPY += MUX_OPTS has_env_remote_copy = "CDIST_REMOTE_COPY" in os.environ
if not has_env_remote_exec or not has_env_remote_copy:
control_path_dir = tempfile.mkdtemp(prefix="cdist.control.path")
import atexit
atexit.register(lambda: shutil.rmtree(control_path_dir))
MUX_OPTS = inspect_ssh_mux_opts(control_path_dir)
if not has_env_remote_exec:
cdist.REMOTE_EXEC += MUX_OPTS
if not has_env_remote_copy:
cdist.REMOTE_COPY += MUX_OPTS
parser['config'] = parser['sub'].add_parser('config', parser['config'] = parser['sub'].add_parser('config',
parents=[parser['loglevel']]) parents=[parser['loglevel']])