From 11b21739cbeee401572c8621f40ac25603b2ac69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sat, 5 Jun 2021 21:45:19 +0200 Subject: [PATCH] add support for (re)defining environment variables --- README | 14 ++++++++++++++ eoptasks.py | 19 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README b/README index f848622..777319c 100644 --- a/README +++ b/README @@ -50,6 +50,20 @@ or to prevent common errors or misconceptions:: saas/test = saas/test,-hds,-re,-docbow +Custom server options can be defined in [server:SERVER_NAME] sections, ex:: + + [server:server1.local] + env = TERM=xterm + + +Any option starting with env will be used to define or replace environment +variables, ex:: + + [server:server1.local] + env = TERM=xterm + env1 = PS1=LANG=C.UTF-8 + + Examples -------- diff --git a/eoptasks.py b/eoptasks.py index d7bbb52..fe865ad 100755 --- a/eoptasks.py +++ b/eoptasks.py @@ -18,6 +18,7 @@ import argparse import configparser +import copy import curses import json import os @@ -272,9 +273,25 @@ def command_window(args): if args.args: cmd += ' ' + ' '.join(['"%s"' % x for x in args.args]) orig_cmd = cmd + + config = get_config() + environ = {} + if config.has_section('server:%s' % args.command_server_name): + new_environ = {} + for key, value in config.items('server:%s' % args.command_server_name): + if key.startswith('env'): + new_environ[value.split('=', 1)[0]] = value.split('=', 1)[1] + if new_environ: + environ = copy.copy(os.environ) + environ.update(new_environ) + + call_kwargs = {} + if environ: + call_kwargs['env'] = environ + while True: # -t: force a tty for interactive commands. - rc = subprocess.call(['ssh', '-t', args.command_server_name] + [cmd]) + rc = subprocess.call(['ssh', '-t', args.command_server_name] + [cmd], **call_kwargs) if rc == 0: send_status_message( tmux_session_name, {'@type': 'server-result', 'info': {args.command_server_name: 'success'}}