mirror of
https://github.com/cupcakearmy/drone-deploy.git
synced 2024-12-22 00:06:32 +00:00
Cleaner output
This commit is contained in:
parent
f922370c6c
commit
673d4d2a9b
1
.dockerignore
Normal file
1
.dockerignore
Normal file
@ -0,0 +1 @@
|
|||||||
|
env
|
40
main.py
40
main.py
@ -2,20 +2,22 @@ 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))
|
||||||
|
|
||||||
|
|
||||||
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:
|
if path is not None:
|
||||||
cmd = 'cd {}; {}'.format(path, cmd)
|
cmd = 'cd {}; {}'.format(path, cmd)
|
||||||
stdin, stdout, stderr = c.exec_command(cmd, environment=env)
|
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():
|
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,32 +49,33 @@ 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}
|
||||||
|
|
||||||
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))
|
||||||
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
|
||||||
|
@ -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
|
|
||||||
|
Loading…
Reference in New Issue
Block a user