项目部署,环境搭建(pip/ makefile)

1 python包管理--pip


  • pip search 你想要的包 # 查询相关包
  • pip install 包名 [==版本号]; # 指定版本可选
  • pip freeze >requires.txt # 导出当前环境下的配置列表
  • pip install -r requires.txt # 根据配置列表文件安装指定版本包

2 makefile 脚本环境搭建

unix系上脚本能够作到自动化运维;执行make,会自动寻找当前目录下的makefile/Makefile文件; html

makefile语法: python

目标 : 条件 git

    脚本 api

举例: 运维

clean: ssh

    find . -name '*.pyc' -delete        
测试

命令行执行 make clean便可 ui

3 Fabric 自动化运维利器

官网 http://www.fabfile.org/ this

入门文档 http://docs.fabfile.org/en/1.10/tutorial.html spa


  • 命令行fab 
  1. -f 指定fabfile模块,默认当前目录下fabfile.py;
  2. -H, --hosts 指定远程主机,多个用' , '分隔,格式同 ssh的host_string,即 username@host;
  3. -l 显示当前fabfile中可用命令
  4. --colorize-errors  错误信息做色


举个栗子~

#!/usr/bin/env python
# encoding: utf-8

'''
makefile的fabfile移植版
'''
from fabric.api import run, cd, settings, abort, env
from fabric.contrib.console import confirm


class DeployClass():
    '''
    部署脚本
    '''
    def init_constant(self, *arg):
        '''
        初始化常量
        '''
        self.PREFIX_PATH = '/data/www/'
        self.ALL_PATH = [self.PREFIX_PATH + i.strip() for i in arg if i]
        self.CTL = 'supervisorctl -s unix:///tmp/supervisor.sock '

    def update(self, *arg):
        '''
        更新当前分支下代码
        '''
        # 初始化
        self.init_constant(arg)

        # 校验并去除无效路径
        self.pre_test()

        for project_path in self.ALL_PATH:
            with cd(project_path):
                branch = self._get_current_branch()
                if not branch: continue
                run('git pull origin %s' % branch)

    def oper_supervisor(self, action, no_start, no_end, prefix):
        '''
        多进程服务操做(start/stop/restart)
        :params action 操做
        :params no_start 进程编号起
        :params no_end  进程编号止
        :params prefix  进程名称前缀
        '''
        assert action in ('start', 'stop', 'restart')
        self.init_constant()
        for no in range(int(no_start), int(no_end)+1):
            cmd = '%s %s%s' % (action, prefix, no)
            run('%s %s' % (self.CTL, cmd))


    def oper_supervisor_multi(self, action, *process):
        '''
        多进程服务操做(start/stop/restart)
        :params action 操做
        :params process 进程名称,...
        '''
        assert action in ('start', 'stop', 'restart')
        self.init_constant()
        for pro in process:
            cmd = '%s %s' % (action, pro)
            run('%s %s' % (self.CTL, cmd))

    @staticmethod
    def _get_current_branch():
        '''
        获取当前路径下的分支名称
        '''
        # res = run("git branch|grep '^\*'|awk -F'*' 'print {$2}'")
        with settings(warn_only=True):
            print '------------update[%s]start-----------' % run('pwd')
            res = run('git branch')
            if res.failed:
                if confirm('[%s] is not a git rep, want exit?' % run('pwd')):
                    abort('ok, you stop it !')
                else:
                    return None


            branch = run("git branch|grep '^\*'|awk -F'*' '{print $2}'")
            return branch

    def pre_test(self):
        '''
        先行测试
        '''
        print '------------>all_path-----------%s'% self.ALL_PATH
        with settings(warn_only=True):
            # 测试路径是否合法
            for _path in self.ALL_PATH:
                res = run('test -d %s' % _path)
                if res.failed:
                    if confirm('[%s] not exist! end this?' % _path):
                        abort('ok, you stop it !')
                    else:
                        print '============rm path [%s]================' % _path
                        self.ALL_PATH.remove(_path)  # 继续执行时,剔除无效路径


DC = DeployClass()


def update(*arg):
    '''
    更新
    '''
    DC.update(*arg)


def oper_supervisor(action, no_start, no_end, prefix):
    '''
    操做项目的多个进程
    '''
    DC.oper_supervisor(action, no_start, no_end, prefix)


def oper_supervisor_multi(action, *process):
    '''
    操做多个进程的项目
    '''
    DC.oper_supervisor_multi(action, *process)
相关文章
相关标签/搜索