def download_artifact_safe(ssh, plan, branch, exception_handler=None, exception_handler_args=(), exception_log=""): """ Downloads artifact from repo. Locks file while it's being downloaded. If exception is thrown during download, exception_log is printed and exception_handler function is called. :param ssh: sshclient with opened connection :type ssh: paramiko.SSHClient :param plan: name of current bamboo plan :type plan: str :param branch: name of current git branch :type branch: str :param exception_handler: function called when exception is thrown while artifact is being downloaded :type exception_handler: function :param exception_handler_args: args for exception_handler :type exception_handler_args: tuple :param exception_log: log that is printed when exception is thrown while artifact is being downloaded :type exception_log: str :return None """ file_name = artifact_path(plan, branch) lock_file(ssh, file_name) try: download_artifact(ssh, plan, branch) except SCPException: print exception_log if exception_handler: exception_handler(*exception_handler_args) finally: unlock_file(ssh, file_name)
def download_artifact(ssh: SSHClient, plan: str, branch: str) -> None: """ Downloads artifact from repo via SCP protocol. :param ssh: sshclient with opened connection :param plan: name of current bamboo plan :param branch: name of current git branch """ with SCPClient(ssh.get_transport()) as scp: scp.get(artifact_path(plan, branch), local_path=plan.replace("-", '_') + ARTIFACTS_EXT)
def upload_artifact_safe(ssh, artifact, plan, branch): file_name = artifact_path(plan, branch) partial_file_name = file_name + PARTIAL_EXT lock_file(ssh, partial_file_name) try: upload_artifact(ssh, artifact, plan, branch) rename_uploaded_file(ssh, file_name) except: print "Uploading artifact of plan {0}, on branch {1} failed".format(plan, branch) ssh.exec_command("rm -rf {}".format(partial_file_name)) finally: unlock_file(ssh, partial_file_name)
def upload_artifact_safe(ssh, artifact, plan, branch): file_name = artifact_path(plan, branch) partial_file_name = file_name + PARTIAL_EXT lock_file(ssh, partial_file_name) try: upload_artifact(ssh, artifact, plan, branch) rename_uploaded_file(ssh, file_name) except: print "Uploading artifact of plan {0}, on branch {1} failed" \ .format(plan, branch) ssh.exec_command("rm -rf {}".format(partial_file_name)) finally: unlock_file(ssh, partial_file_name)
def download_artifact(ssh, plan, branch): """ Downloads artifact from repo via SCP protocol. :param ssh: sshclient with opened connection :type ssh: paramiko.SSHClient :param plan: name of current bamboo plan :type plan: str :param branch: name of current git branch :type branch: str :return None """ with SCPClient(ssh.get_transport()) as scp: scp.get(artifact_path(plan, branch), local_path=plan.replace("-", '_') + ARTIFACTS_EXT)
def upload_artifact(ssh, artifact, plan, branch): """ Uploads given artifact to repo. :param ssh: sshclient with opened connection :type ssh: paramiko.SSHClient :param artifact: name of artifact to be pushed :type artifact: str :param plan: name of current bamboo plan :type plan: str :param branch: name of current git branch :type branch: str :return None """ with SCPClient(ssh.get_transport()) as scp: scp.put(artifact, remote_path=artifact_path(plan, branch) + PARTIAL_EXT)
def download_artifact_safe(ssh, plan, branch, exception_handler=None, exception_handler_args=(), exception_log=""): """ Downloads artifact from repo. Locks file while it's being downloaded. If exception is thrown during download, exception_log is printed and exception_handler function is called. :param ssh: sshclient with opened connection :type ssh: paramiko.SSHClient :param plan: name of current bamboo plan :type plan: str :param branch: name of current git branch :type branch: str :param exception_handler: function called when exception is thrown while artifact is being downloaded :type exception_handler: function :param exception_handler_args: args for exception_handler :type exception_handler_args: tuple :param exception_log: log that is printed when exception is thrown while artifact is being downloaded :type exception_log: str :return None """ file_name = artifact_path(plan, branch) def signal_handler(_signum, _frame): ssh.connect(args.hostname, port=args.port, username=args.username) unlock_file(ssh, file_name) sys.exit(1) signal.signal(signal.SIGINT, signal_handler) lock_file(ssh, file_name) try: download_artifact(ssh, plan, branch) except: print exception_log if exception_handler: exception_handler(*exception_handler_args) finally: unlock_file(ssh, file_name)
def upload_artifact_safe(ssh: SSHClient, artifact: str, plan: str, branch: str, hostname: str, port: int, username: str) -> None: file_name = artifact_path(plan, branch) ext = partial_extension() partial_file_name = file_name + ext def signal_handler(_signum, _frame): ssh.connect(hostname, port=port, username=username) delete_file(ssh, partial_file_name) sys.exit(1) signal.signal(signal.SIGINT, signal_handler) try: upload_artifact(ssh, artifact, partial_file_name) rename_uploaded_file(ssh, partial_file_name, file_name) except: print("Uploading artifact of plan {0}, on branch {1} failed".format( plan, branch)) delete_file(ssh, partial_file_name)
def upload_artifact_safe(ssh, artifact, plan, branch): file_name = artifact_path(plan, branch) partial_file_name = file_name + PARTIAL_EXT def signal_handler(_signum, _frame): ssh.connect(args.hostname, port=args.port, username=args.username) unlock_file(ssh, partial_file_name) delete_file(ssh, partial_file_name) unlock_file(ssh, file_name) sys.exit(1) signal.signal(signal.SIGINT, signal_handler) lock_file(ssh, partial_file_name) try: upload_artifact(ssh, artifact, plan, branch) rename_uploaded_file(ssh, file_name) except: print "Uploading artifact of plan {0}, on branch {1} failed" \ .format(plan, branch) delete_file(ssh, partial_file_name) finally: unlock_file(ssh, partial_file_name)