support for remote env vars

This commit is contained in:
cupcakearmy 2019-05-25 16:42:19 +02:00
parent 5fd4397f5f
commit cc33b5f874

23
main.py
View File

@ -15,10 +15,10 @@ def get_random_string(length: int = 128) -> str:
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=length)) 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: if path is not None:
cmd = 'cd {}; {}'.format(path, cmd) 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() return stdout.read().decode('utf-8').strip()
@ -30,6 +30,7 @@ def main():
password = os.environ.get('PLUGIN_PASSWORD') password = os.environ.get('PLUGIN_PASSWORD')
key = os.environ.get('PLUGIN_KEY') 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]: def clean_array(s: str) -> List[str]:
return list(filter(None, s.split(','))) return list(filter(None, s.split(',')))
@ -38,16 +39,32 @@ def main():
deletes = clean_array(os.environ.get('PLUGIN_DELETE', '')) deletes = clean_array(os.environ.get('PLUGIN_DELETE', ''))
target = os.environ.get('PLUGIN_TARGET') target = os.environ.get('PLUGIN_TARGET')
# Check for host, port and user
for env in [host, port, user]: for env in [host, port, user]:
if env is None: 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: if len(list(filter(lambda x: x is not None, [password, key]))) is 0:
raise Exception('No authentication method provided') raise Exception('No authentication method provided')
# Check if target is set
if len(sources) is not 0 and target is None: if len(sources) is not 0 and target is None:
raise Exception('Target not set') 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() ssh: SSHClient = paramiko.SSHClient()
try: try:
k = paramiko.RSAKey.from_private_key(io.StringIO(key)) k = paramiko.RSAKey.from_private_key(io.StringIO(key))