Fabric 上传代码至测试服务器、回滚等操作

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
   
import os, sys
import time
from fabric.api import *
from fabric.contrib import *
   
#写入文件内容
def file_put_contents(file,content):
    fp=open(file,'a')
    fp.write(content)
    fp.close()
#获取当前日期时间
def get_date_time(offset_day=0):
    timestamp=time.time()
    if offset_day!=0:
        timestamp+=offset_day*86400
    DATE_TIME=time.localtime(timestamp)
    return time.strftime('%Y%m%d%H%M%S',DATE_TIME)
def strtotime(date):
    return time.mktime(time.strptime(date, "%Y-%m-%d"))
   
def date(fmt, date = None):
    if date is None:
        return time.strftime(fmt, time.localtime(time.time()))
    else:
        return time.strftime(fmt, time.localtime(date))
   
class project_lib:
    class xuejiehome:
        source_dir      = "xuejiehome"
        # local dir
        local_project_dir   = "www.xuejiehome.com"
        local_target_dir    = "/var/www/gongsi/www.xuejiehome.com"
        # test dir  
        test_project_dir    = "www.xuejiehome.com"
        test_target_dir     = "/var/www/www.xuejiehome.com"
        # online test dir   
        online_test_project_dir = "www.xuejiehome.com"
                online_test_target_dir  = "/data/web/www.xuejiehome.com"
        # web dir
        online_project_dir  = "www.xuejiehome.com"
        online_target_dir   = "/web/www.xuejiehome.com"
        class xuejieblog:
                source_dir      = "xuejieblog"
        # local dir
        local_project_dir   = "www.xuejieblog.com"
        local_target_dir    = "/var/www/gongsi/www.xuejieblog.com"
        # test dir  
        test_project_dir    = "www.xuejieblog.com"
        test_target_dir     = "/var/www/www.xuejieblog.com"
        # online test dir   
        online_test_project_dir = "www.xuejieblog.com"
                online_test_target_dir  = "/data/web/www.xuejieblog.com"
        # web dir
        online_project_dir  = "www.xuejieblog.com"
        online_target_dir   = "/web/www.xuejieblog.com"
   
   
env.user = "root"
env.hosts = ["root@192.168.1.231:22", "192.168.1.230", "root@100.100.100.100:22", "root@100.100.100.100:22"]
env.passwords = {"root@192.168.1.231:22" : "123456", "192.168.1.230" : "tianji", "root@100.100.100.100:22": "123456","root@100.100.100.100:22": "123456"}
   
def back(files):
    for f in files:
        print "*" * 100
        print f
   
def base_to_pack(filename = "", source_dir = "", project_dir = "", target_dir = ""):
    path = "%s/%s" % (source_dir, filename)
   
    if os.path.isdir(path) or not os.path.exists(path):
        print "not found filename [%s]" % path
        return
    f = open(path, "r")
    files = map(lambda x: x.replace("n", "").replace("r", ""), f.readlines())
    backup(files, source_dir, target_dir)
    sqlfiles = [x for x in files if x.endswith(".sql")]
    #local("svn update --username pleamon --password tianji ../%s/" % project_dir)
    local("tar -zcvf %s_tar/%s.tar -C ../../%s -T %s/%s" % (source_dir, filename, project_dir, source_dir, filename))
    local("cp %s_tar/%s.tar %s_tar/last.tar" % (source_dir, filename, source_dir))
   
def base_to_server(source_dir, target_dir):
    print source_dir, target_dir
    put("%s_tar/last.tar" % source_dir, target_dir)
    with cd(target_dir):
        run("tar -xvf last.tar")
   
def backup(files, source_dir = "", target_dir = ""):
    for f in files:
        print f
   
def base_to_bak(filename = "", source_dir = "", project_dir = "", target_dir = ""):
    path = "%s/%s" % (source_dir, filename)
    if os.path.isdir(path) or not os.path.exists(path):
        print "not found filename [%s]" % path
        return
    f = open(path, "r")
    file_list = map(lambda x: x.replace("n", "").replace("r", ""), f.readlines())
    put(path, target_dir)
    with cd(target_dir):
        old='temp.old'
        for f in file_list:
            if files.exists(f):
                files.append(old,f)
        if files.exists(old):
            tar='%s.%s.tar' % (filename,get_date_time())
            run("tar -zcvf %s -C %s -T %s" % (tar,target_dir,old))
            run('rm -f %s' % old)
            get(tar,'%s_backup' % source_dir)
            run('rm -f %s' % tar)
            git_file='%s_backup/%s' % (source_dir,tar)
            local('git pull')
            local('git add %s' % git_file)
            local("git commit -m 'bak' %s" % git_file)
            local('git push origin master')
        else:
            print '你提交的都是新文件,无需备份'
        run('rm -f %s' % filename)
   
@hosts("root@100.100.100.100:22")
def reback(project_name,filename):
    project = getattr(project_lib, project_name)
    path = "%s_backup/%s" % (project.source_dir, filename)
    if os.path.isdir(path) or not os.path.exists(path):
        print "not found filename [%s]" % filename
        return
    else:
        put(path,project.online_target_dir)
        with cd(project.online_target_dir):
            if files.exists(filename):
                run("tar -xvf %s" % filename)
                run('rm -f %s' % filename)
   
@hosts("127.0.0.1")
def to_pack(project_name = "", filename = ""):
    project = getattr(project_lib, project_name)
    base_to_pack(filename, project.source_dir, project.local_project_dir, project.local_target_dir)
   
@hosts("root@192.168.1.231:22")
def to_test(project_name = "", filename = ""):
    project = getattr(project_lib, project_name)
    base_to_server(project.source_dir, project.test_target_dir)
   
@hosts("root@100.100.100.100:22")
def to_online_test(project_name = "", filename = ""):
    project = getattr(project_lib, project_name)
    base_to_server(project.source_dir, project.online_test_target_dir)
   
@hosts("root@100.100.100.100:22")
def to_product(project_name = "", filename = ""):
    project = getattr(project_lib, project_name)
    base_to_bak(filename,project.source_dir,project.online_project_dir,project.online_target_dir)
    base_to_server(project.source_dir, project.online_target_dir)

注:创建文件夹xuejiehome和xuejiehome_tar、xuejieblog和xuejieblog_tar

使用方法:

fab $OPERATE:$PROJECT

如:fab to_test:xuejiehome

by 雪洁 2015-02-02 07:49:22 2182 views
我来说几句

相关文章