抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

[toc]

lnmp环境的建立

#!/usr/bin/env python
from fabric.colors import *
from fabric.api import *

env.user='root'
env.roledefs = {  #定义业务角色分组
    'webservers': [ 'xx.xxx.xx.xxx' ],
   'dbservers': ['xx.xxx.xx.xxx']
}

env.passwords = {
    'root@xxx.xx.xx.xxx:22' : 'passwd',
    'root@xxx.xx.xx.xxx:22' : 'passwd'
}

@roles('webservers') #webtask任务函数引用'webservers'角色修饰符
def webtask():
    print yellow("Install webtask packages class ! ")
    with settings(warn_only=True):
        run("yum -y install nginx")
        run("yum -y install php-fpm php-mysql php-mbstring php-xml php-mcrypt php-gd")
        run("chkconfig --levels 235 php-fpm on")
        run("chkconfig --levels 235 nginx on")
        run("systemctl start php-fpm")
        run("systemctl start nginx")

@roles('dbservers') # dbtask任务函引用'dbservers'角色修饰符
def dbtask(): #部署mysql环境
    print green("Install mariadb server !")
    with settings(warn_only=True):
        run("yum -y install mariadb mariadb-server")
        run("chkconfig --levels 235 mariadb on")
        run("systemctl start mariadb")

@roles('webservers','dbservers') # publictask任务函数同时引用两个角色修饰符
def publictask():  # 部署公共环境,如epel、ntp等
    print blue("Install epel ntp !")
    with settings(warn_only=True):
        run("rpm -Uvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm")
        run("rpm -y install ntp ntpdate")

def deploy():
    execute(publictask)
    execute(webtask)
    execute(dbtask)
#vim 底行模式的执行
: ! fab -f NGINX_DPL.py deploy

自动部署

注:目前代码还有些问题,小心参考

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from fabric.api import *
from fabric.colors import *
from fabric.context_managers import *
from fabric.contrib.console import confirm
import time 

env.user = 'root'
env.hosts = ['xx.xxx.xx.xxx','xx.xxx.xx.xxx']
env.password = 'test.......'
env.project_dev_source = '/var/www/html'  #开发机项目主目录
env.project_tar_source = '/var/www/releases' #开发机项目压缩包存储目录
env.project_pack_name = 'release'  # 项目压缩包前缀,文件名为 release.tar.gz

env.deploy_project_root = '/var/www/data' #项目生产环境主目录
env.deploy_release_dir = 'release' #项目发布目录,位于主目录下面
env.deploy_current_dir = 'current' #对外服务的当前版本软链接
env.deploy_version = time.strftime ("%Y%m%d") +"V2" #版本号

@runs_once #相当于一个模块
def input_versionid():
    return prompt ("please input project rollbak version ID:",default="")
@task
@runs_once
def tar_source():
    print yellow("Creating source package...")
    with lcd(env.project_dev_source):
        local("tar -czf %s.tar.gz." % (env.project_tar_source + env.project_pack_name))
        print green("Creating source package success!")

@task
def put_packages(): #上传任务函数
    print yellow("Start put package...")
    with settings(warn_only=True):
        with cd(env.deploy_project_root+env.deploy_release_dir):
            run("mkdir %s" % (env.deploy_version))
    env.deploy_full_path = env.deploy_project_root + env.deploy_release_dir + "/" + env.deploy_version
    with setting(warn_only=True):
        result = put(env.project_tar_source + env.project_pack_name + ".tar.gz", env.deploy_full_path)
    if result.failed and not("put file failed, Continue[Y/N]?"):
        abort("Aborting file put task!")
    with cd(env.deploy_full_path):
        run("tar -zxvf %s.tar.gz" % (env.project_pack_name))
        run("rm -rf %s.tar.gz" % (env.project_pack_name))

    print green("Put & untar packages success!")

@task
def make_symlink():   #为当前版本目录做软链接
    print yellow("update current sylink")
    env.deploy_full_path = env.deploy_project_root + env.deploy_release_dir + "/" +env.deploy_version
    with settings(warn_only=True): #删除软链接,重新创建并指定软链接源目录,新版本生效
        run("rm -rf %s" % (env.deploy_project_root + env.deploy_current_dir))
        run("ls -s %s %s" % (env.deploy_full_path, env.deploy_project_root + env.deploy_current_dir))
    print green("make symlink success!")
@task 
def rollback():  #版本回滚任务函数
    print yellow("rollback project version")
    versionid= input_versionid()
    if versionod=='':
        abort("Project version ID error,abort!")
    env.deploy_full_path=env.deploy_project_root + env.deploy_release_dir + "/" +versionid
    run("rm -f %s" % env.deploy_project_root +env.deploy_current_dir)
    run("ln -s %s %s" % (env.deploy_full_path, env.deploy_project_root + env.deploy_current_dir))
#删除软链接,重新创建并指定软链接源目录,新版本生效
    print green("rollback success!")

@task
def go():
    tar_source()
    put_packages()
    make_symlink()

评论