From d5d6f7b3cbc401b7f512a2195fd8c03a134bc029 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste VESLIN Date: Thu, 4 Mar 2021 16:17:44 +0100 Subject: [PATCH 1/5] feat: add exceptions handling --- app.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 34d8c55..772ebd2 100644 --- a/app.py +++ b/app.py @@ -60,8 +60,14 @@ def connect(callback=None): ssh.connect(INPUT_HOST, port=INPUT_PORT, username=INPUT_USER, pkey=p_key, password=INPUT_PASS, timeout=convert_to_seconds(INPUT_CONNECT_TIMEOUT)) + except: + print("Connect error") + raise + + else: if callback: callback(ssh) + finally: os.unlink(tmp.name) tmp.close() @@ -89,7 +95,7 @@ def ssh_process(ssh, input_ssh): print(command_str) stdin, stdout, stderr = ssh.exec_command(command_str) - + out = "".join(stdout.readlines()) out = out.strip() if out is not None else None if out: @@ -98,11 +104,9 @@ def ssh_process(ssh, input_ssh): err = "".join(stderr.readlines()) err = err.strip() if err is not None else None if err: - if out is None: - raise Exception(err) - else: - print(f"Error: \n{err}") - + print(f"Error: \n{err}") + raise Exception(err) + pass @@ -128,10 +132,19 @@ def scp_process(ssh, input_scp): with scp.SCPClient(ssh.get_transport(), progress=progress, sanitize=lambda x: x) as conn: for l2r in copy_list: remote = l2r.get('r') - ssh.exec_command(f"mkdir -p {remote} || true") + try: + ssh.exec_command(f"mkdir -p {remote}") + except: + print(f"Remote mkdir error. Can't create {remote}") + raise + for f in [f for f in glob(l2r.get('l'))]: - conn.put(f, remote_path=remote, recursive=True) - print(f"{f} -> {remote}") + try: + conn.put(f, remote_path=remote, recursive=True) + print(f"{f} -> {remote}") + except: + print(f"scp error. Can't copy {f} on {remote}") + raise pass From de52e872d4970acd88db09135a0d04f65fb358e5 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste VESLIN Date: Thu, 4 Mar 2021 16:50:39 +0100 Subject: [PATCH 2/5] feat: exit(1) on errors --- app.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app.py b/app.py index 772ebd2..d6d2aea 100644 --- a/app.py +++ b/app.py @@ -63,6 +63,7 @@ def connect(callback=None): except: print("Connect error") raise + sys.exit(1) else: if callback: @@ -106,6 +107,7 @@ def ssh_process(ssh, input_ssh): if err: print(f"Error: \n{err}") raise Exception(err) + sys.exit(1) pass @@ -137,6 +139,7 @@ def scp_process(ssh, input_scp): except: print(f"Remote mkdir error. Can't create {remote}") raise + sys.exit(1) for f in [f for f in glob(l2r.get('l'))]: try: @@ -145,6 +148,7 @@ def scp_process(ssh, input_scp): except: print(f"scp error. Can't copy {f} on {remote}") raise + sys.exit(1) pass From a6548c1a21e9a40484d1911dcced974ffa23df85 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste VESLIN Date: Thu, 4 Mar 2021 17:18:32 +0100 Subject: [PATCH 3/5] feat: exit(1), don't raise --- app.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/app.py b/app.py index d6d2aea..1ad11f7 100644 --- a/app.py +++ b/app.py @@ -60,9 +60,8 @@ def connect(callback=None): ssh.connect(INPUT_HOST, port=INPUT_PORT, username=INPUT_USER, pkey=p_key, password=INPUT_PASS, timeout=convert_to_seconds(INPUT_CONNECT_TIMEOUT)) - except: - print("Connect error") - raise + except Exception as err: + print(f"Connect error\n{err}") sys.exit(1) else: @@ -106,7 +105,6 @@ def ssh_process(ssh, input_ssh): err = err.strip() if err is not None else None if err: print(f"Error: \n{err}") - raise Exception(err) sys.exit(1) pass @@ -136,18 +134,16 @@ def scp_process(ssh, input_scp): remote = l2r.get('r') try: ssh.exec_command(f"mkdir -p {remote}") - except: - print(f"Remote mkdir error. Can't create {remote}") - raise + 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'))]: try: conn.put(f, remote_path=remote, recursive=True) print(f"{f} -> {remote}") - except: - print(f"scp error. Can't copy {f} on {remote}") - raise + except Exception as err: + print(f"Scp error. Can't copy {f} on {remote}\n{err}") sys.exit(1) pass From 3d97e5b8eb2f916b13d31b715a6906d6bfc8b79b Mon Sep 17 00:00:00 2001 From: Jean-Baptiste VESLIN Date: Thu, 4 Mar 2021 17:44:13 +0100 Subject: [PATCH 4/5] feat: exit 1 if app.py exited 1 --- entrypoint.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/entrypoint.sh b/entrypoint.sh index 90a86f4..b5023a9 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,5 +3,8 @@ echo "+++++++++++++++++++STARTING PIPELINES+++++++++++++++++++" python3 /opt/tools/app.py +RET = $? echo "+++++++++++++++++++END PIPELINES+++++++++++++++++++" + +exit $RET From d3b9734a3262069504187dc81f65c18ce440c363 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste VESLIN Date: Thu, 4 Mar 2021 17:47:51 +0100 Subject: [PATCH 5/5] Update entrypoint.sh --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index b5023a9..02605d5 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,7 +3,7 @@ echo "+++++++++++++++++++STARTING PIPELINES+++++++++++++++++++" python3 /opt/tools/app.py -RET = $? +RET=$? echo "+++++++++++++++++++END PIPELINES+++++++++++++++++++"