Cleaner output

This commit is contained in:
cupcakearmy 2020-01-13 09:22:37 +01:00
parent f922370c6c
commit 673d4d2a9b
3 changed files with 23 additions and 28 deletions

1
.dockerignore Normal file
View File

@ -0,0 +1 @@
env

34
main.py
View File

@ -2,14 +2,16 @@ import io
import os import os
import random import random
import string import string
import warnings
import subprocess import subprocess
from typing import List import warnings
from os.path import abspath, join, dirname from os.path import abspath, join, dirname
from typing import List
import paramiko import paramiko
from paramiko import SSHClient from paramiko import SSHClient
VERSION = '1.0.3'
def get_random_string(length: int = 128) -> str: 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))
@ -23,6 +25,8 @@ def execute(c: SSHClient, cmd: str, path: str = None, env:dict = None) -> str:
def main(): def main():
print(f'> Version: {VERSION}')
print('> Deployment started 🚀')
host = os.environ.get('PLUGIN_HOST') host = os.environ.get('PLUGIN_HOST')
port = os.environ.get('PLUGIN_PORT', 22) port = os.environ.get('PLUGIN_PORT', 22)
user = os.environ.get('PLUGIN_USER') user = os.environ.get('PLUGIN_USER')
@ -45,24 +49,24 @@ def main():
raise Exception('Missing host, port or user env variable') raise Exception('Missing host, port or user env variable')
# Check if there is a possible authentication method # 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') raise Exception('No authentication method provided')
# Check if target is set # 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') raise Exception('Target not set')
# Remote Envs # Remote Envs
envsraw = os.environ.get('PLUGIN_ENVS') envs_raw = os.environ.get('PLUGIN_ENVS')
envs = None envs = None
if envsraw is not None: if envs_raw is not None:
prefix = 'PLUGIN_' prefix = 'PLUGIN_'
# Take only the envs that start with PLUGIN_ and remore the 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)} 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 # 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 # 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}
@ -70,7 +74,8 @@ def main():
try: try:
k = paramiko.RSAKey.from_private_key(io.StringIO(key)) k = paramiko.RSAKey.from_private_key(io.StringIO(key))
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 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 a target is set, make sure the directory is created and writable
if target is not None: if target is not None:
@ -88,18 +93,15 @@ def main():
sftp.remove(join(target, delete)) sftp.remove(join(target, delete))
# COPY # COPY
if len(sources) is not 0: if len(sources) != 0:
archive = get_random_string(100) + '.tar.gz' # Keep the max file name length under 128 chars archive = get_random_string(64) + '.tar.gz' # Keep the max file name length under 128 chars
archive_local = abspath(archive) archive_local = abspath(archive)
archive_remote = join(target, archive) archive_remote = join(target, archive)
# sources = list(map(lambda x: join(dirname(archive_local), x), sources))
# print(sources)
# Compress # Compress
cmd = ['tar', '-czf', archive, '-C', dirname(archive_local), *sources] cmd = ['tar', '-czf', archive, '-C', dirname(archive_local), *sources]
run = subprocess.run(cmd, capture_output=True) 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())) raise Exception('Error while compressing locally. {}'.format(run.stderr.decode('utf-8').strip()))
# Upload # Upload

View File

@ -1,9 +1 @@
asn1crypto==0.24.0 paramiko==2.7.1
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