jenkins OPS发布程序

[root@hadoop201 /opt/deploy]# cat deploy.conf
#!/usr/bin/python36

[content]
hosts=10.82.40.18,10.82.40.19,10.82.40.20,10.82.40.21,10.82.40.22,10.82.40.23,10.82.40.24,10.82.40.25
username=ops
jarPath=/opt/deploy/jar/news-content-1.0-SNAPSHOT.jar
remotePath=/dfs/jar/news-content-1.0-SNAPSHOT.jar
jar=news-content-1.0-SNAPSHOT.jar
javaport=8180

[user]
hosts=10.82.40.14,10.82.40.15,10.82.40.16,10.82.40.17,10.82.40.30,10.82.40.31,10.82.40.32,10.82.40.33
username=root
jarPath=/opt/deploy/jar/news-user-1.0-SNAPSHOT.jar
remotePath=/dfs/jar/news-user-1.0-SNAPSHOT.jar
jar=news-user-1.0-SNAPSHOT.jar
javaport=8480

[root@hadoop201 /opt/deploy]# cat ssh_deploy.py
#!/usr/bin/env python36
# -*- coding: utf-8 -*-
# @Time : 2018/12/6 17:52
# @Author : 
# @Site :
# @File : testdeploy.py
# @Software: PyCharm


import paramiko
import sys
import configparser
import os
import json
import requests
import time

def ssh_transport(pkey, port, host, username):
private_key = paramiko.RSAKey.from_private_key_file(pkey)
transport = paramiko.Transport((host, port))
transport.connect(username=username, pkey=private_key)
return transport

def ssh_connect(transport):
ssh = paramiko.SSHClient()
ssh._transport = transport
return ssh

def sftp_connect(transport):
sftp = paramiko.SFTP.from_transport(transport)
return sftp

def dingding(text):
headers = {'Content-Type': 'application/json'}
api1_url = 'https://oapi.dingtalk.com/robot/send?access_token=ae4232b53d04fce6dd9a5c727a4b6f4c1ea60179bea44dc78cc294f189f0a6fa'
api2_url = 'https://oapi.dingtalk.com/robot/send?access_token=4b8a190a68e14e4949dd8cfad70e35b7b2e75580d19bbf5e291a8d94047446f5'
json_text = {
"msgtype": "text",
"text": {
"content": text
},
"at": {
"atMobiles": [
'18623606325'
],
"isAtAll": "true"
}
}
requests.post(url=api1_url, data=json.dumps(json_text), headers=headers)
requests.post(url=api2_url, data=json.dumps(json_text), headers=headers)


def main():
env = sys.argv[1]
cf = configparser.ConfigParser()
cf.read("/opt/deploy/deploy.conf", encoding='utf-8')
pkey = "/root/.ssh/id_rsa"
port = 10036
username = cf.get(env, "username")
jarPath = cf.get(env, "jarPath")
remotePath = cf.get(env, "remotePath")
javaport = cf.get(env, "javaport")
cmd = "/dfs/start_java.sh %s %s 1>&2" % (cf.get(env, "jar"), javaport)
if not os.path.exists(jarPath):
print("=========================================================")
print("JAR 包不存在:" + cf.get(env, "jar"))
exit()
hosts = cf.get(env, 'hosts').split(',') or cf.get(env, 'hosts')
result = True
for host in hosts:
transport = ssh_transport(pkey, port, host, username)
ssh = ssh_connect(transport)
sftp = sftp_connect(transport)
sftp.put(jarPath, remotePath)
sftp.close()
stdin, stdout, stderr = ssh.exec_command(cmd)
err_list = stderr.readlines()
for item in err_list:
print(item)
if "失败" in item:
result = False
ssh.close()
print("=========================================================")
print("部署失败")
if not result:
ssh.close()
transport.close()
break
transport.close()
ssh.close()
if result:
status = "成功"
else:
status = "失败"
text = """
生产环境后端程序发布
发布模块:{0}
发布状态:{1}
发布时间:{2}
""".format(env, status, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

dingding(text)

if __name__ == "__main__":
main()

原文地址:https://www.cnblogs.com/net2817/p/11051238.html