Compare commits

..

11 Commits

Author SHA1 Message Date
thuongnht c9b539abea
Merge pull request #6 from thebaptiste/metwork
fix: in ssh_process, we exit(1) only if the exit code of the ssh command is not 0
2021-03-05 12:28:50 +01:00
Jean-Baptiste VESLIN 5aa3c5e8d1
Update app.py 2021-03-05 10:36:08 +01:00
Jean-Baptiste VESLIN 1e8a5783bc
feat: use ssh command exit code 2021-03-05 10:18:23 +01:00
Jean-Baptiste VESLIN fed5e4953c
fix: don't exit(1) if both err and out are not empty 2021-03-05 10:05:00 +01:00
thuongnht 35fb71b02d
Merge pull request #5 from thebaptiste/metwork
feat: the action should fail in case of authentification, ssh or scp errors
2021-03-04 20:06:19 +01:00
Jean-Baptiste VESLIN d3b9734a32
Update entrypoint.sh 2021-03-04 17:47:51 +01:00
Jean-Baptiste VESLIN 3d97e5b8eb
feat: exit 1 if app.py exited 1 2021-03-04 17:44:13 +01:00
Jean-Baptiste VESLIN a6548c1a21
feat: exit(1), don't raise 2021-03-04 17:18:32 +01:00
Jean-Baptiste VESLIN de52e872d4
feat: exit(1) on errors 2021-03-04 16:50:39 +01:00
Jean-Baptiste VESLIN d5d6f7b3cb
feat: add exceptions handling 2021-03-04 16:17:44 +01:00
Nguyen Huu Thuong e1c2bf0c8d fix '.' and connection per key 2020-08-15 17:12:01 +02:00
2 changed files with 44 additions and 12 deletions

49
app.py
View File

@ -6,6 +6,8 @@ import scp
import sys import sys
import math import math
import re import re
import tempfile
import os
envs = environ envs = environ
@ -42,19 +44,34 @@ def strip_and_parse_envs(p):
return None return None
for c in strips: for c in strips:
p = p.strip(c) p = p.strip(c)
return path.expandvars(p) return path.expandvars(p) if p != "." else f"{path.realpath(p)}/*"
def connect(callback=None): def connect(callback=None):
with paramiko.SSHClient() as ssh: tmp = tempfile.NamedTemporaryFile(delete=False)
p_key = paramiko.RSAKey.from_private_key(INPUT_KEY) if INPUT_KEY else None try:
ssh = paramiko.SSHClient()
p_key = None
if INPUT_KEY:
tmp.write(INPUT_KEY.encode())
tmp.close()
p_key = paramiko.RSAKey.from_private_key_file(filename=tmp.name)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(INPUT_HOST, port=INPUT_PORT, username=INPUT_USER, ssh.connect(INPUT_HOST, port=INPUT_PORT, username=INPUT_USER,
pkey=p_key, password=INPUT_PASS, pkey=p_key, password=INPUT_PASS,
timeout=convert_to_seconds(INPUT_CONNECT_TIMEOUT)) timeout=convert_to_seconds(INPUT_CONNECT_TIMEOUT))
except Exception as err:
print(f"Connect error\n{err}")
sys.exit(1)
else:
if callback: if callback:
callback(ssh) callback(ssh)
finally:
os.unlink(tmp.name)
tmp.close()
# Define progress callback that prints the current percentage completed for the file # Define progress callback that prints the current percentage completed for the file
def progress(filename, size, sent): def progress(filename, size, sent):
@ -79,6 +96,8 @@ def ssh_process(ssh, input_ssh):
stdin, stdout, stderr = ssh.exec_command(command_str) stdin, stdout, stderr = ssh.exec_command(command_str)
ssh_exit_status = stdout.channel.recv_exit_status()
out = "".join(stdout.readlines()) out = "".join(stdout.readlines())
out = out.strip() if out is not None else None out = out.strip() if out is not None else None
if out: if out:
@ -87,10 +106,11 @@ def ssh_process(ssh, input_ssh):
err = "".join(stderr.readlines()) err = "".join(stderr.readlines())
err = err.strip() if err is not None else None err = err.strip() if err is not None else None
if err: if err:
if out is None: print(f"Error: \n{err}")
raise Exception(err)
else: if ssh_exit_status != 0:
print(f"Error: \n{err}") print(f"ssh exit status: {ssh_exit_status}")
sys.exit(1)
pass pass
@ -117,10 +137,19 @@ def scp_process(ssh, input_scp):
with scp.SCPClient(ssh.get_transport(), progress=progress, sanitize=lambda x: x) as conn: with scp.SCPClient(ssh.get_transport(), progress=progress, sanitize=lambda x: x) as conn:
for l2r in copy_list: for l2r in copy_list:
remote = l2r.get('r') remote = l2r.get('r')
ssh.exec_command(f"mkdir -p {remote} || true") try:
ssh.exec_command(f"mkdir -p {remote}")
except Exception as err:
print(f"Remote mkdir error. Can't create {remote}\n{err}")
sys.exit(1)
for f in [f for f in glob(l2r.get('l'))]: for f in [f for f in glob(l2r.get('l'))]:
conn.put(f, remote_path=remote, recursive=True) try:
print(f"{f} -> {remote}") conn.put(f, remote_path=remote, recursive=True)
print(f"{f} -> {remote}")
except Exception as err:
print(f"Scp error. Can't copy {f} on {remote}\n{err}")
sys.exit(1)
pass pass

View File

@ -3,5 +3,8 @@
echo "+++++++++++++++++++STARTING PIPELINES+++++++++++++++++++" echo "+++++++++++++++++++STARTING PIPELINES+++++++++++++++++++"
python3 /opt/tools/app.py python3 /opt/tools/app.py
RET=$?
echo "+++++++++++++++++++END PIPELINES+++++++++++++++++++" echo "+++++++++++++++++++END PIPELINES+++++++++++++++++++"
exit $RET