[toc]
lnmp环境的建立
| |
| 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') |
| 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') |
| def dbtask(): |
| 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') |
| def publictask(): |
| 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) |
| |
| : ! fab -f NGINX_DPL.py deploy |
自动部署
注:目前代码还有些问题,小心参考
| |
| |
| 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' |
| |
| 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() |