Python脚本实现项目远端部署

本文为博主原创文章,未经博主允许不得转载!

  • update-link.py

 1 #!/usr/bin/python
 2 import sys, os
 3 import argparse
 4 import subprocess
 5 from distutils.version import LooseVersion
 6 import re
 7 def main():
 8     kwargs = parse_argv(sys.argv[1:])
 9     module = kwargs.module[0]
10     version = None if kwargs.version ==None else kwargs.version[0]
11     product_dir = ‘/opt/repo/env/‘+module if kwargs.product_dir ==None 12                     else os.path.join(kwargs.product_dir[0], module)
13     if not os.path.isdir(product_dir):
14         raise Exception(‘Not a valid product directory: ‘ + product_dir)
15
16     target = get_path(product_dir, version)
17     if not os.path.isdir(target):
18         raise Exception(‘Can not find target directory: ‘ +target)
19
20     link_name = ‘/opt/‘+module
21     if os.path.isdir(os.path.dirname(link_name)) != True:
22         os.makedirs(os.path.dirname(link_name), 0755)
23     if os.path.exists(link_name):
24         if os.path.islink(link_name):
25             os.remove(link_name)
26         else:
27             raise Exception("Remove old link error, not a link: "+link_name)
28
29     cmd = [‘ln‘, ‘-s‘, target, link_name]
30     returncode = subprocess.call(cmd)
31     if returncode != 0:
32         raise Exception(‘Cmd false: ‘ + ‘ ‘.join(cmd))
33
34 def get_path(product_dir, version):
35     if version == None:
36         if len(os.listdir(product_dir)) == 0:
37             raise Exception(‘Empty directory: ‘ + product_dir)
38         version = LooseVersion(‘0.0.0.0‘)
39         dir_names = [f for f in os.listdir(product_dir) if
40                      re.match(‘^(\d+\.){3}\d+‘, f)]
41         for f in dir_names:
42             tmp = LooseVersion(f)
43             if version < tmp:
44                 version = tmp
45         version = version.__str__()
46     return os.path.join(product_dir, version)
47 def parse_argv(argv):
48     parser = argparse.ArgumentParser(
49                     description=‘‘‘update link for module.‘‘‘)
50     parser.add_argument(‘--module‘, nargs=1, required=True,
51                     help=‘‘‘module to unpack.‘‘‘)
52     parser.add_argument(‘--product-dir‘, nargs=1,
53                     help=‘‘‘directory that contain all versions of product.‘‘‘)
54     parser.add_argument(‘--version‘, nargs=1)
55
56
57     return parser.parse_args(argv)
58 if __name__ == ‘__main__‘:
59     main()
  • main.yml

 1 - name: check app install status
 2   stat: path=/opt/tomcat_hive/bin
 3   register: hive_install_status
 4 - name: install app if needed
 5   shell: echo " backend have not been installed!"
 6   when: hive_install_status.stat.exists == False
 7 - name: stop exist process
 8   shell: /opt/tomcat_hive/bin/shutdown.sh --force
 9   environment:
10     JAVA_HOME: /usr/java/jdk1.8.0_141
11   when: hive_install_status.stat.exists
12   ignore_errors: True
13 - name: install hive backend by pip
14   pip: name=sdn-portal-backend state=present extra_args="-i http://pypi.XXX.com/ci/dev/+simple --trusted-host pypi.XXX.com --no-cache -t /opt/repo/env/dev/portal_backend"
15 - name: remove exist link
16   file: path=/opt/dev/portal_backend state=absent
17 - name: make sure dir exists
18   file: path=/opt/script/setup state=directory mode="u=rwx,g=rx,o=rx"
19 - name: make sure py script exists
20   copy: src=update-link.py dest=/opt/script/setup/update-link.py mode="u=rwx,g=rx,o=rx"
21 - name: update oss backend
22   shell: python /opt/script/setup/update-link.py --module dev/portal_backend
23 - name: startup oss backend
24   shell: nohup /opt/tomcat_hive/bin/catalina.sh start &
25   environment:
26     JAVA_HOME: /usr/java/jdk1.8.0_141
时间: 2024-08-05 02:33:48

Python脚本实现项目远端部署的相关文章

cocos2d-x使用python脚本创建项目的简单方法

本文有CC原创.转载请注明地址:http://blog.csdn.net/oktears/article/details/13297003 在cocos2d-x2.1.4以上的版本号中.取消了使用vs模版创建项目的方法,開始使用python脚本创建项目,使用python创建项目须要在命令行敲非常多指令,比較麻烦. 我把这些命令组合在一起,放在了一个批处理文件里.直接双击打开批处理文件就能够通过几个简单的指令来创建项目. 第一步: 新建一个txt文件.将以下这段代码拷贝进去,保存文件.将文件名称改

Python脚本实现项目工程自动远程部署

说明: 该脚本实现将本地的项目工程包远程发送到远程服务器上.并将老版本的项目备份,上传新的项目,本脚本的中的项目自动项目自身的启动脚本. #!/usr/bin/env  python #coding:utf-8 #Author Summer #Create date:2015-07-29 import datetime import time from fabric.context_managers import * from fabric.contrib.console import conf

Python离线项目迁移部署

最近遇到了一个场景:需要将Python项目文件打包到无法联网的主机上部署执行,本篇文章记录针对于该场景的处理方案. 说明: 源主机(可联网):安装了Python3和pip3 目标主机(无法联网):需安装和源主机相同的Python版本和pip3,部署执行项目文件 主机系统为centos,Python版本为3.5.2,通过虚拟环境+pip进行迁移 目标主机离线安装Python及pip3 源主机中下载所需包 Python3 首先,下载Python3,可以在官网或者通过源主机(可联网的其它主机)wget

70个Python练手项目

前言: 不管学习那门语言都希望能做出实际的东西来,这个实际的东西当然就是项目啦,不用多说大家都知道学编程语言一定要做项目才行. 这里整理了70个Python实战项目列表,都有完整且详细的教程,你可以从中选择自己想做的项目进行参考学习练手,你也可以从中寻找灵感去做自己的项目. 70个Python项目列表: 1.[Python 图片转字符画]2.[200行Python代码实现2048]3.[Python3 实现火车票查询工具]4.[高德API+Python解决租房问题 ]5.[Python3 色情图

利用pyinstaller将python脚本打包发布

之前写了一个小工具,将excel配置表转换为json.xml.lua等配置文件.最近在学习egret,正好需要转换配置文件,刚好就用上了.然而当我想把工具拷到工作目录时,就发愁了.之前我为了方便扩展,把程序拆分得太细: [email protected]:~/Documents/code/github/py_exceltools$ ls -lh 总用量 80K drwxrwxr-x 2 xzc xzc 4.0K 7月 27 23:03 bin drwxrwxr-x 2 xzc xzc 4.0K

python脚本实现集群检测和管理

python脚本实现集群检测和管理 场景是这样的:一个生产机房,会有很多的测试机器和生产机器(也就是30台左右吧),由于管理较为混乱导致了哪台机器有人用.哪台机器没人用都不清楚,从而产生了一个想法--利用一台机器来管理所有的机器,记录设备责任人.设备使用状态等等信息....那么,为什么选择python,python足够简单并且拥有丰富的第三方库的支持. 最初的想法 由于刚参加工作不久,对这些东西也都没有接触过,轮岗到某个部门需要做出点东西来(项目是什么还没情况,就要做出东西来,没办法硬着头皮想点

某互联网后台自动化组合测试框架RF+Sikuli+Python脚本

某互联网后台自动化组合测试框架RF+Sikuli+Python脚本 http://www.jianshu.com/p/b3e204c8651a 字数949 阅读323 评论1 喜欢0 一.**RobotFramework 1.**工具介绍:Robotframework在测试中作为组织测试用例和BDD关键字的平台,主要使用RIDE进行管理,它不是一个工具,而仅仅是一个框架,使用Python进行开发,同时支持WEB测试(Selenium).Java GUI 测试,启动线程.终端.SSH等.支持BDD

SecureCRT中python脚本编写

SecureCRT中python脚本编写学习指南 SecureCRT python 引言 在测试网络设备中,通常使用脚本对设备端进行配置和测试以及维护:对于PE设备的测试维护人员来说使用较多是SecureCRT工具:SecureCRT支持VB.JavaScript.Python等多种脚本语言,为了实现脚本在CRT中更加丰富稳定地执行,掌握CRT的常用函数是非常有用的.接下来的时间我将对SecureCRT脚本编写的常用函数展开学习应用. 内容 (1)使用python语言实现SecureCRT中的D

扫描端口占用情况的python脚本

之前项目上线前,领导要求让写一个脚本用来判断端口的占用情况.由于现在python3使用也比较多,基于python2修改了一下,做了个python3版本的,现在做一下总结. 一.python脚本实现扫描端口: pthon2下代码如下(当时的环境): #!/usr/bin/env python # -*- coding:utf-8 -*- # used for pthon2.*    import socket, time, thread socket.setdefaulttimeout(3) #设