标签搜索

目 录CONTENT

文章目录

自动化我的网站部署流程!?

陈铭
2022-04-10 / 0 评论 / 0 点赞 / 196 阅读 / 435 字 / 正在检测是否收录...

自动化上传

python进行sfpt操作需要用到paramiko模块,pip install paramiko 就行

import paramiko

def upload_action(IP, User, Password, local_file, server_file):
    try:
        terminal = paramiko.Transport(IP, PORT)
        terminal.connect(username=User, password=Password)
        sftp = paramiko.SFTPClient.from_transport(terminal)
        sftp.put(local_file, server_file)
        terminal.close()
    except Exception as e:
        print (e)
        raise
    return

def download_action(IP, User, Password, local_file, server_file):
    try:
        terminal = paramiko.Transport(IP, PORT)
        terminal.connect(username=User, password=Password)
        sftp = paramiko.SFTPClient.from_transport(terminal)
        sftp.get(server_file, local_file)
        terminal.close()
    except Exception as e:
        print (e)
        raise
    return

upload_action("ip", "root", "password", "xxx.jar", "xxx.jar")

自动化重启

重启也是用paramiko执行shell

import time

import paramiko
import os

#创建一个ssh的客户端
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
#创建一个ssh的白名单
class AllowAllKeys(paramiko.MissingHostKeyPolicy):
    def missing_host_key(self, client, hostname, key):
        return
ssh.set_missing_host_key_policy(AllowAllKeys())
ssh.connect(
    hostname = "ip",
    port = xxx,
    username = "user",
    password = "password"
)

shell = ssh.invoke_shell()
stdin = shell.makefile('wb')
stdout = shell.makefile('rb')

stdin.write(''' 
cd /xxx/xxxx/xxxxx
ps -aux | grep xxxx.jar | awk '{print $2}' | xargs kill -9
nohup java -jar xxxx.jar > /xxx/xxx/xxx/log.txt 2>&1 &
''')
# 如果不睡眠一下的话,nohup命令可能会还没执行完,shell就退出了
# 所以选择5s,相对时间比较久,阻塞一下
time.sleep(5)

stdout.close()
stdin.close()
ssh.close()

自动化脚本

编写一个bat,执行maven打包和上传重启一系列工作

  • call 命令:避免当前执行命令返回exit 0,而导致整个bat提交结束
  • UploadandDowload.py:上述自动化上传脚本
  • RestartMyWeb.py:上述自动化重启脚本
@echo off
echo ===============maven clean===============
cd ..
call mvn clean
echo ===============maven package===============
call mvn package
echo ===============upload jar===============
cd Auto/
call python UploadandDowload.py
echo ===============restart web===============
call python RestartMyWeb.py
echo ===============auto success===============
0

评论区