Python -- deploying LNMP business service environment

Please indicate the source of Reprint: http://blog.csdn.net/l1028386804/article/details/79042734

Before we go online, we need to deploy the corresponding environment, often a business involves multiple environments, such as Web, DB, Proxy, Cache, etc. in this example, we define different roles through env.roledefs, and then use the @ roles("webservers") modifier to bind to the corresponding task function to realize the host deployment differences of different roles. The specific code is as follows:

# -*- coding:UTF-8 -*-
'''
//Deploy LNMP business service environment
Created on 2018 January 12, 2010

@author: liuyazhuang
'''

from fabric.colors import *
from fabric.api import *

env.user = 'root'
#Define business role groups
env.roledefs = {
    'webservers':['192.168.209.121', '192.168.209.122'],
    'dbservers':['192.168.209.123']
}

env.passwords = {
    'root@192.168.209.121:22' : "Password 1",
    'root@192.168.209.122:22' : "Password 2",
    'root@192.168.209.123:22' : "Password 3",
}

@roles('webservers') #webtask function references' webservers' role modifier
def webtask():  #Deploying nginx PHP FPM environment
    print yellow("Install nginx php php-fpm...")
    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")
        
@roles('dbservers') #dbatsk task function references dbservers role modifier
def dbtask():   #Deploy mysql environment
    print yellow("Install MySQL...")
    with settings(warn_only = True):
        run("yum -y install mysql mysql-server")
        run("chkconfig --levels 235 mysqld on")
        
@roles('webservers', 'dbservers')   #The publictask task function references two role modifiers at the same time
def publictask():   #Deploy public class environment, such as epel, ntp, etc
    print yellow("Install epel ntp...")
    with settings(warn_only = True):
        run("rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm")
        run("yum -y install ntp")
        
def deploy():
    execute(publictask)
    execute(webtask)
    execute(dbtask)
This example uses roles to distinguish different business service environments and deploy different packages. We only need a Python script to complete the customization of different business environments.

Tags: PHP MySQL Nginx yum

Posted on Mon, 04 May 2020 08:48:51 -0700 by shibobo12