From 673d4d2a9bb12034f2a905a2b9d99b15bb3350a9 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Mon, 13 Jan 2020 09:22:37 +0100 Subject: [PATCH] Cleaner output --- .dockerignore | 1 + main.py | 40 +++++++++++++++++++++------------------- requirements.txt | 10 +--------- 3 files changed, 23 insertions(+), 28 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..8fa5b33 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +env \ No newline at end of file diff --git a/main.py b/main.py index bd4cfa6..152aa8a 100644 --- a/main.py +++ b/main.py @@ -2,20 +2,22 @@ import io import os import random import string -import warnings import subprocess -from typing import List +import warnings from os.path import abspath, join, dirname +from typing import List import paramiko from paramiko import SSHClient +VERSION = '1.0.3' + 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, env:dict = 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, environment=env) @@ -23,6 +25,8 @@ def execute(c: SSHClient, cmd: str, path: str = None, env:dict = None) -> str: def main(): + print(f'> Version: {VERSION}') + print('> Deployment started 🚀') host = os.environ.get('PLUGIN_HOST') port = os.environ.get('PLUGIN_PORT', 22) user = os.environ.get('PLUGIN_USER') @@ -45,32 +49,33 @@ def main(): 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]))) == 0: raise Exception('No authentication method provided') # Check if target is set - if len(sources) is not 0 and target is None: + if len(sources) != 0 and target is None: raise Exception('Target not set') # Remote Envs - envsraw = os.environ.get('PLUGIN_ENVS') + envs_raw = os.environ.get('PLUGIN_ENVS') envs = None - if envsraw is not None: + if envs_raw 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 os.environ.items() if k.startswith(prefix)} + # Take only the envs that start with PLUGIN_ and remove the prefix + envs = {k[len(prefix):]: v for k, v in os.environ.items() if k.startswith(prefix)} - if 'all' != envsraw or ',' in envsraw: + if 'all' != envs_raw or ',' in envs_raw: # Make them uppercase - selected = [x.upper() for x in clean_array(envsraw)] + selected = [x.upper() for x in clean_array(envs_raw)] # Select only the envs that where specified inside of envs - envs = {k:v for k,v in envs.items() if k in selected} + 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)) ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - ssh.connect(hostname=host, username=user, pkey=k, port=port, password=password) + print(host, user, port) + ssh.connect(hostname=host, username=user, pkey=k, port=port, password=password, timeout=3) # If a target is set, make sure the directory is created and writable if target is not None: @@ -88,18 +93,15 @@ def main(): sftp.remove(join(target, delete)) # COPY - if len(sources) is not 0: - archive = get_random_string(100) + '.tar.gz' # Keep the max file name length under 128 chars + if len(sources) != 0: + archive = get_random_string(64) + '.tar.gz' # Keep the max file name length under 128 chars archive_local = abspath(archive) archive_remote = join(target, archive) - # sources = list(map(lambda x: join(dirname(archive_local), x), sources)) - # print(sources) - # Compress cmd = ['tar', '-czf', archive, '-C', dirname(archive_local), *sources] run = subprocess.run(cmd, capture_output=True) - if run.returncode is not 0: + if run.returncode != 0: raise Exception('Error while compressing locally. {}'.format(run.stderr.decode('utf-8').strip())) # Upload diff --git a/requirements.txt b/requirements.txt index 2d2a643..ada4a36 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1 @@ -asn1crypto==0.24.0 -bcrypt==3.1.6 -cffi==1.12.2 -cryptography==2.6.1 -paramiko==2.4.2 -pyasn1==0.4.5 -pycparser==2.19 -PyNaCl==1.3.0 -six==1.12.0 +paramiko==2.7.1