import os
import tkinter
import zipfile
from datetime import datetime
from tkinter.messagebox import *

import paramiko

# ssh_host = "hadoop5"
# ssh_port = 22
# ssh_user = "root"
# ssh_pwd = "LrmkEqypH4ZV4S4jA3gq3tSRTNsp2gpjqupLDM5K"

# ssh_host = "47.90.241.149"
# ssh_port = 22
# ssh_user = "root"
# ssh_pwd = "ElGB62S1pybws9Le"


ssh_host = "104.244.91.203"
ssh_port = 26696
ssh_user = "root"
ssh_pwd = "PRVQ4CkwyxnD"


def crlf_2_lf(full_path):
    """
      sh windows脚本转换为 unix分隔符
    :param full_path:
    :return:
    """
    WINDOWS_LINE_ENDING = b'\r\n'
    UNIX_LINE_ENDING = b'\n'
    with open(full_path, 'rb') as open_file:
        content = open_file.read()
    return content.replace(WINDOWS_LINE_ENDING, UNIX_LINE_ENDING)


def handle_zip(dir_name, filename):
    zfName = filename
    result_path = os.path.join(os.getcwd(), zfName)
    try:
        os.remove(result_path)
    except:
        pass
    foo = zipfile.ZipFile(zfName, 'w')
    for root, dirs, files in os.walk(dir_name):
        for f in files:
            full_path = os.path.join(root, f)
            zip_path = full_path[len(dir_name) + 1:]
            if f.startswith("."):
                continue
            if full_path.endswith("sh"):
                foo.writestr(zinfo_or_arcname=zip_path, data=crlf_2_lf(full_path))
                pass
            else:
                foo.write(full_path, zip_path)

    foo.close()
    print("压缩文件成功!!")
    print(f"压缩文件目录为{result_path}")
    return result_path


def put_and_unzip(local_file, remote_dir):
    window = tkinter.Tk()
    window.withdraw()
    result = askquestion('确认', f'是否确认部署到目录{remote_dir}')
    if (result == 'no'):
        return

    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(hostname=ssh_host, port=ssh_port, username=ssh_user, password=ssh_pwd)
    print("连接远程服务器成功...")
    #  时间后缀
    suffix = datetime.now().strftime("%m_%d_%H_%M")
    cmd = f"mv {remote_dir} {remote_dir}_back_{suffix}"
    print("===================执行命令=======================")
    print(cmd)
    client.exec_command(cmd)
    cmd = f"mkdir {remote_dir}"
    print("===================执行命令=======================")
    print(cmd)
    client.exec_command(cmd)
    print(f"备份远程目录{remote_dir}下的文件中.....")

    sftp = client.open_sftp()
    file_name = local_file[local_file.rfind("\\") + 1:]
    remote_path = f"{remote_dir}/{file_name}"
    print(f"上传文件【{local_file}】到远程【{remote_path}】中...")
    sftp.put(local_file, remote_path)
    print("上传成功!!")

    cmd = f""" cd {remote_dir} && unzip -d {remote_dir} {file_name}"""
    print("===================执行命令=======================")
    print(cmd)
    client.exec_command(cmd)
    print(f"解压远程压缩文件{remote_path}中.....")

    cmd = f"rm -rf {remote_path}"
    print("===================执行命令=======================")
    print(cmd)
    client.exec_command(cmd)
    print(f"删除远程压缩文件{remote_path}中.....")
    client.close()
    pass


if __name__ == '__main__':
    local_dir = "E:\Amazon-Selection\\py_gpt"
    remote_dir = "/opt/py_gpt"
    result_path = handle_zip(local_dir, "result.zip")
    put_and_unzip(result_path, remote_dir)

#     python -m flask run
