python实现自动化上线脚本的示例
作者:Ian_安安 时间:2021-02-01 05:14:49
程序说明:
本程序实现将开发程序服务器中的打包文件通过该脚本上传到正式生产环境(注:生产环境和开发环境不互通)
程序基本思路:
将开发环境中的程序包拷贝到本地堡垒机
将程序包进行解压
获得解压后的文件通同步到生产服务器上
主要知识点:python库os.system()的基本使用 利用python调用xshell命令
程序使用方法:
python addline.py 开发主机ip 程序包 目标主机ip 上传目录 上传编号
如:python addline.py 240 /home/shaojinlong/2018-7-17/activityIqiyi_766bb10bd811e40732cf79dffde9a904_20180717.tar.gz 165 /home/zhouja01 190122
具体程序:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/7/17 20:56
# @Author : Zhou Jiaan
# @File : addline.py
import os
import sys
import re
import time
##使用方法:
# python addline.py 240 filename 165 directory runnum
## 如:python addline.py 240 /home/shaojinlong/2018-7-17/activityIqiyi_766bb10bd811e40732cf79dffde9a904_20180717.tar.gz 165 /home/zhouja01 190122
##拷贝文件
def cpfile(source_host,source_dir, runnum):
os.system("mkdir -p /sx/%s" % (runnum))
os.system("scp %s:%s /sx/%s " % (source_host,source_dir, runnum))
##解压文件
def tarfile(source_dir,runnum):
target_test = re.split(r'/', source_dir)
print(target_test[-1])
os.system(
"tar zxvf /sx/%s/%s -C /sx/%s/" % (runnum,target_test[-1], runnum))
time.sleep(1) #休眠一秒 因为同步脚本获取日志需要一秒钟
# 同步文件
def syncfile(filename, runnum,target_host):
os.system("ssh %s 'mkdir -p /home/zhouja01/sx/%s'" % (target_host,runnum))
os.system("scp -r /sx/%s/%s %s:/home/zhouja01/sx/%s/" %
(runnum, filename,target_host,runnum))
# os.system("ssh 165 'sudo -u apps sh /home/zhouja01/bcp_web.sh /home/zhouja01/sx/%s/%s /home/apps/ananetest/%s'" %
# (runnum, filename, filename))
def delfile(runnum):
os.system("mv /sx/%s /sx/wc"%(runnum))
def main():
source_host=sys.argv[1] #源主机ip
source_dir=sys.argv[2] #源主机文件
target_host=sys.argv[3] #目标主机ip
target_dir=sys.argv[4] #目标主机文件
runnum=sys.argv[5] #oa流水号
cpfile(source_host,source_dir,runnum) #将文件从源主机拷贝到堡垒机
tarfile(source_dir,runnum) #解压文件
#获得解压文件名
with open('/var/log/sx.log', 'r') as f:
lines = f.readlines()
last_line = lines[-1]
print(last_line)
filename = re.split(r'/', last_line)
print(filename[3])
syncfile(filename[3], runnum,target_host) #将文件同步到目标主机
delfile(runnum) #将堡垒机文件移动到完成目录
if __name__ == '__main__':
main()
来源:https://blog.csdn.net/sinat_34789167/article/details/81100406
标签:python,自动化,脚本
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
微信小程序地图(map)组件点击(tap)获取经纬度的方法
2023-09-03 21:33:37
![](https://img.aspxhome.com/file/2023/6/56196_0s.gif)
交互设计杂七杂八
2010-09-25 18:41:00
![](https://img.aspxhome.com/file/UploadPic/20109/25/02-24s.jpg)
系统默认的MySQL用户名消失的解决方法
2008-12-02 14:26:00
Python中的文件和目录操作实现代码
2022-08-30 15:12:11
Dreamweaver 网页编辑常用表现的实现方法
2010-10-20 20:05:00
ASP常见错误详解及解决方案小结 推荐第1/2页
2011-02-24 11:19:00
python框架flask知识总结
2023-07-27 20:24:53
![](https://img.aspxhome.com/file/2023/5/61445_0s.png)
asp上传文件自动重命名方法
2007-08-24 09:46:00
Python实现Word文档转换Markdown的示例
2022-06-24 08:23:29
![](https://img.aspxhome.com/file/2023/6/67296_0s.png)
Python实现操纵控制windows注册表的方法分析
2021-10-30 10:16:33
asp如何制作一个搜索引擎链接程序?
2010-07-07 12:26:00
asp fso删除非空目录的例子
2008-11-26 20:03:00
滑动门代码 兼容IE7 IE6 FireFox Opera
2008-06-07 13:56:00
20年来我得到的20条编程经验
2010-05-26 13:44:00
asp.net aspnetpager分页统计时与实际不符的解决办法
2023-06-30 04:02:32
python pands实现execl转csv 并修改csv指定列的方法
2022-11-20 01:45:27
tf.concat中axis的含义与使用详解
2021-05-21 12:38:00
ASP 写的判断 Money 各个位值的函数
2008-04-13 06:36:00
编码问题引起的折腾
2009-07-03 12:43:00
JS+CSS实现的日本门户网站经典选项卡导航效果
2023-09-04 03:40:24
![](https://img.aspxhome.com/file/2023/7/56267_0s.png)