Python脚本实现项目远端部署

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于Python脚本实现项目远端部署的主要内容,如果未能解决你的问题,请参考以下文章

14-Jenkins-Pipeline实现自动部署

JenkinsJenkins配置从节点,实现远程主机调用功能

多节点部署一键启动脚本

dotnet项目执行shell脚本实现简单的自动化部署

喜欢的女孩想看樱花,安排!将静态网页部署在Coding远端!她说棒极了~

Jenkins部署Python项目实战