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()

posted on   net2817  阅读(222)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示