Python -- production environment code package release management


The release of program production environment is the last environment for business online, which requires the functions of source code packaging, release, switching, rollback, version management, etc. this example implements a whole set of processes, in which version switching and rollback are realized by using the soft link of Linux. The specific code is as follows:

# -*- coding:UTF-8 -*-
'''
Created on 2018 January 12, 2010

@author: liuyazhuang
'''

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 = ['192.168.209.121', '192.168.209.122']
env.password = 'Password'

#Developer project main directory
env.project_dev_source = '/data/dev/source/'
#Compressed package storage directory of development machine project
env.project_tar_source = "/data/dev/releases/"
#Project compression package prefix, filename is release.tar.gz
env.project_pack_name='release'

#Main directory of project production environment
env.deploy_project_root = "/data/application/"
#Project publishing directory, under the home directory
env.deploy_release_dir = 'releases'
#Current version soft link of external service
env.deploy_current_dir = 'current'
#Version No
env.deploy_version=time.strftime("%Y%m%d") + "v2"

@runs_once
def input_versionid():  #Obtain the version number entered by the user for version rollback
    return prompt("please input project rollback version ID:", default="")

@task
@runs_once
def tar_source():  #Package the local project home directory and store the compressed package in the local compressed package directory
    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_package():  #Upload task function
    #Start printing
    print yellow("Start put package...")
    with settings(warn_only = True):
        with cd(env.deploy_project_root + env.deploy_release_dir):
            #Create version directory
            run("mkdir %s" % (env.deploy_version))
        env.deplog_full_path = env.deploy_project_root + env.deploy_release_dir + "/" + env.deploy_version
        
        #Upload project package to this directory
    with settings(warn_only = True):
        result = put(env.project_tar_source + env.project_pack_name + ".tar.gz", env.deplog_full_path)
        if result.failed and not("put file failed, Continue[Y/N]?"):
            abort("Aborting file put task!")
            
    #Delete the compressed package after successful decompression
    with cd(env.deplog_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 package success!")
        

@task
def make_symlink():   #Link to the current version directory
    print yellow("update current symlink")
    env.deploy_full_path = env.deploy_project_root + env.deploy_release_dir + "/" + env.deploy_version
    #Delete the soft link, re create and develop the soft link source directory, and the new version will take effect
    with settings(warn_only = True):
        run("rm -rf %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("make symlink success!")
    
@task
def rollback():    #Version rollback task function
    print yellow("rollback project version")
    #Get the rollback version number entered by the user
    versionid = input_versionid()
    if versionid == '':
        abort("Please version ID error, abort!")
    
    env.deploy_full_path = env.deploy_project_root + env.deploy_release_dir + "/" + versionid
    #Delete the soft link, re create and develop the soft link directory, and the new version will take effect
    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():    #Automatic program version release entry function
    tar_source()
    put_package()
    make_symlink()
This instance implements a universal code release management function, which supports rapid deployment and rollback. Whether the release or rollback is implemented by switching the current soft link, it is very flexible. Due to the use of Linux soft link for switching, users of our version release and rollback operations have no awareness, and at the same time, our business online process is standardized.

Tags: Linux

Posted on Mon, 04 May 2020 08:57:30 -0700 by Yeti