From cc33b5f874a6e91a2b5735a532bb7370cf7694bd Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Sat, 25 May 2019 16:42:19 +0200 Subject: [PATCH] support for remote env vars --- main.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index bef78cc..a409971 100644 --- a/main.py +++ b/main.py @@ -15,10 +15,10 @@ def get_random_string(length: int = 128) -> str: return ''.join(random.choices(string.ascii_lowercase + string.digits, k=length)) -def execute(c: SSHClient, cmd: str, path: str = None) -> str: +def execute(c: SSHClient, cmd: str, path: str = None, env:dict = None) -> str: if path is not None: cmd = 'cd {}; {}'.format(path, cmd) - stdin, stdout, stderr = c.exec_command(cmd) + stdin, stdout, stderr = c.exec_command(cmd, environment=env) return stdout.read().decode('utf-8').strip() @@ -30,6 +30,7 @@ def main(): password = os.environ.get('PLUGIN_PASSWORD') key = os.environ.get('PLUGIN_KEY') + # Takes a string, splits it at the comma and removes empty elements def clean_array(s: str) -> List[str]: return list(filter(None, s.split(','))) @@ -38,16 +39,32 @@ def main(): deletes = clean_array(os.environ.get('PLUGIN_DELETE', '')) target = os.environ.get('PLUGIN_TARGET') + # Check for host, port and user for env in [host, port, user]: if env is None: - raise Exception('Missing ENV variable') + raise Exception('Missing host, port or user env variable') + # Check if there is a possible authentication method if len(list(filter(lambda x: x is not None, [password, key]))) is 0: raise Exception('No authentication method provided') + # Check if target is set if len(sources) is not 0 and target is None: raise Exception('Target not set') + # Remote Envs + envsraw = os.environ.get('PLUGIN_ENVS') + if envsraw is not None: + prefix = 'PLUGIN_' + # Take only the envs that start with PLUGIN_ and remore the prefix + envs = {k[len(prefix):]:v for k, v in a.items() if k.startswith(prefix)} + + if 'all' != envsraw or ',' in envsraw: + # Make them uppercase + selected = [x.upper() for x in clean_array(envsraw)] + # Select only the envs that where specified inside of envs + envs = {k:v for k,v in envs.items() if k in selected} + ssh: SSHClient = paramiko.SSHClient() try: k = paramiko.RSAKey.from_private_key(io.StringIO(key))