python实现自动化上线脚本的示例


Posted in Python onJuly 01, 2019

程序说明:

本程序实现将开发程序服务器中的打包文件通过该脚本上传到正式生产环境(注:生产环境和开发环境不互通)

程序基本思路:

将开发环境中的程序包拷贝到本地堡垒机

将程序包进行解压

获得解压后的文件通同步到生产服务器上

主要知识点: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()

以上这篇python实现自动化上线脚本的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python使用函数默认值实现函数静态变量的方法
Aug 18 Python
python求列表交集的方法汇总
Nov 10 Python
详解python 发送邮件实例代码
Dec 22 Python
Python基于递归算法实现的汉诺塔与Fibonacci数列示例
Apr 18 Python
python 自动批量打开网页的示例
Feb 21 Python
python 字典 setdefault()和get()方法比较详解
Aug 07 Python
python excel转换csv代码实例
Aug 26 Python
Python学习笔记之函数的参数和返回值的使用
Nov 20 Python
Python模块future用法原理详解
Jan 20 Python
基于Python3.7.1无法导入Numpy的解决方式
Mar 09 Python
django 连接数据库出现1045错误的解决方式
May 14 Python
Opencv求取连通区域重心实例
Jun 04 Python
在Python中构建增广矩阵的实现方法
Jul 01 #Python
django框架实现模板中获取request 的各种信息示例
Jul 01 #Python
Python整数对象实现原理详解
Jul 01 #Python
python实现两个dict合并与计算操作示例
Jul 01 #Python
Python字符串对象实现原理详解
Jul 01 #Python
Python转换时间的图文方法
Jul 01 #Python
Python列表对象实现原理详解
Jul 01 #Python
You might like
复杂检索数据并分页显示的处理方法
2006/10/09 PHP
php 数组的创建、调用和更新实现代码
2009/03/09 PHP
奉献出一个封装的curl函数 便于调用(抓数据专用)
2013/07/22 PHP
PHP使用ODBC连接数据库的方法
2015/07/18 PHP
PHP计算数组中值的和与乘积的方法(array_sum与array_product函数)
2016/04/01 PHP
ThinkPHP框架表单验证操作方法
2017/07/19 PHP
PHP 使用二进制保存用户状态的实例
2018/01/29 PHP
php实现微信发红包功能
2018/07/13 PHP
浅谈php使用curl模拟多线程发送请求
2019/03/08 PHP
Yii框架使用PHPExcel导出Excel文件的方法分析【改进版】
2019/07/24 PHP
jQuery EasyUI API 中文文档 - ProgressBar 进度条
2011/09/29 Javascript
javascript之typeof、instanceof操作符使用探讨
2013/05/19 Javascript
jQuery回调函数的定义及用法实例
2014/12/23 Javascript
jQuery实现带延迟效果的滑动菜单代码
2015/09/02 Javascript
引用jquery框架后出错的解决方法
2016/08/09 Javascript
jquery实现ajax提交表单信息的简单方法(推荐)
2016/08/24 Javascript
Web前端框架bootstrap实战【第一次接触使用】
2016/12/28 Javascript
javascript 判断用户有没有操作页面
2017/10/17 Javascript
vue中的mvvm模式讲解
2019/01/31 Javascript
微信小程序实现音乐播放器
2019/11/20 Javascript
小议Python中自定义函数的可变参数的使用及注意点
2016/06/21 Python
解决Python 遍历字典时删除元素报异常的问题
2016/09/11 Python
Python简单获取二维数组行列数的方法示例
2018/12/21 Python
python使用pip安装模块出现ReadTimeoutError: HTTPSConnectionPool的解决方法
2019/10/04 Python
Kears 使用:通过回调函数保存最佳准确率下的模型操作
2020/06/17 Python
keras和tensorflow使用fit_generator 批次训练操作
2020/07/03 Python
埃弗顿足球俱乐部官方网上商店:Everton Direct
2018/01/13 全球购物
全球性的众包图形设计市场:DesignCrowd
2021/02/02 全球购物
大学军训感言400字
2014/03/11 职场文书
作风建设年活动实施方案
2014/10/24 职场文书
家庭财产分割协议范文
2014/11/24 职场文书
工会经费申请报告
2015/05/15 职场文书
离婚上诉状范文
2015/05/23 职场文书
《世界多美呀》教学反思
2016/02/22 职场文书
vue实现简单数据双向绑定
2021/04/28 Vue.js
海贼王十大潜力果实,路飞仅排第十,第一可毁世界(震震果实)
2022/03/18 日漫