飞天使-发布代码结合jenkins

发布前先打包以及jenkins配置

ssh  -i  /var/lib/www/.ssh/www  [email protected] "sudo sh -x /data/xxxxsou/buildv1.sh $module $args"

jenkins选择参数化构建过程
module 中填写模块名
args 中填写hhh


打包

if [ $2 == 'hhh' ]
then
    cd /data/xxxxsou/ &&  rm -rf $1  &&  git clone [email protected]/$1.git 
else
    cd /data/xxxxsou/$1 &&  git pull  
fi

cd /data/xxxxsou/$1
git_commit_id=`git rev-parse HEAD`   
filename=$1-`date +%Y-%m-%d-%H-%M-%S`.tar.gz   

echo $git_commit_id
echo $filename


build_nodejs(){
    cd /data/xxxxsou && chown www:www $1 -R  
    if [[ $1 == 'xxx_1' || $1 == 'xxx_2' ]]  
    then
        echo "这是应用1"    
    else
        echo "这是应用2"
    fi

    echo $filename >> /data/xxxxsou/$1/dist/banben.txt
    echo $git_commit_id >> /data/xxxxsou/$1/dist/banben.txt
    cd /data/xxxxsou/$1 && rm -rf $1  && mkdir $1 && cp -rp dist/* $1 
    tar -czpf  $filename $1 --exclude=$1/.git  && mv $filename /opt/package-num1
    /usr/bin/cp /opt/package-num1/$filename /opt/package-num1/$1_last.tar.gz
    runuser -l www-c "scp  /opt/package-num1/$filename [email protected]:/opt/package-num1/"
}


build_php() {
    cd /data/xxxxsou  &&  find . -type f -exec chmod 600   {} +   
    cd /data/xxxxsou/ && chown www:www $1 -R
    cp -rp /data/xxxxsou/extension_lib/$1/storage /data/xxxxsou/$1/       
}



case $1 in
    xxx_php)
        build_php $1
        ;;
    xxx_nodejs)
        build_nodejs $1
        ;;
    *)
        echo "error"
esac

发布jenkins的配置
jenkins发布任务中配置
ssh [email protected]"sudo python3 build.py  $module $package_version  ${BUILD_USER_ID} "
if [ $module == "xxx_web" ]
then
    echo "testpip"
    ssh [email protected] "ssh 10.0.0.1 'sudo rm -rf /www/uploads && sudo ln -s /xxx/uploads  /www/uploads' "
fi

build.py脚本内容

服务端脚本

import requests
from requests.auth import HTTPBasicAuth
import json
import sys
import os
import time
import redis
import hashlib
import subprocess

r_redis = redis.Redis(host="11.0.0.10",port=6379,password="password")

hosts=list()

def deploy_msg(module,version,server,user):
    import requests
    token = "5435436541:Afesfesfesfesf"  
    chat_id = -123234324  
    try:
        r = requests.post(f'https://api.telegram.org/bot{token}/sendMessage', json={"chat_id": chat_id,
        "text": "代码发布通知:\n发布项目名: {module} \n发布包版本: {version} \n发布服务器: {server}  \n发布人: {user}".format(module=module,version=version,server=server,user=user)})
    except:
        pass

def CheckHttp(host): #发布代码后检查
    headers = {'Host': 'www.baidu.com', }
    try:
        r= requests.get('http://'+host+'/', headers=headers)
        return r.status_code
    except:
        return 500


def HostOnlineCheck(): 
    for i in r_redis.sinter('allhostredis'):  
        print(i.decode('utf-8'))
        if i.decode('utf-8')!='':
            try:
                r=requests.get("http://"+i.decode('utf-8')+":9888",timeout=15)  
                hosts.append(i.decode('utf-8'))
            except:
                pass
    return hosts


def Deploy(host): #发布函数
    url = 'http://'+host+':9888/postbuild'
    data={'module':sys.argv[1],'version':sys.argv[2],'env':'prod'}  
    try:
        r_http = requests.post(url,json.dumps(data),auth=HTTPBasicAuth('admin','httppasswd'),timeout=35)
        http_code=CheckHttp(host)  
        print("检查发布后http状态",i,"状态码",http_code)
        if r_http.json().get('code')!='0' or http_code!=200: 
            print('发布失败',i)
            return 1
        else:
            print(r_http.json())
            return 0
    except Exception as e:
        print(host,e)

#发布前检查
hosts=HostOnlineCheck()
print("在线主机",hosts)

#代码发布

if r_redis.get('host_start_lock')==None:  
    print("进行代码发布")
    time.sleep(5)
    r_redis.set('deploy_code_lock',1)    

    flag=0 #判断是否全部发布成功 

    for i in hosts:
        try:
            ret=Deploy(i)
            if ret==0:
                print(i,"发布成功")
                deploy_msg(sys.argv[1],sys.argv[2],i,sys.argv[3])
            else:
                print(i,"发布失败")
                flag=flag+1 #
        except Exception as e:
           print(i,"发布异常",e,i)

    #判断是否全部发布成功
    if flag==0:
        subprocess.call(['/usr/bin/cp','/opt/packageall/'+sys.argv[2],'/dataall/www/'+sys.argv[1]+'.tar.gz'])  
        r_redis.delete('deploy_code_lock')   
    else:
        r_redis.delete('deploy_code_lock')  
        subprocess.call(['/usr/bin/cp','/opt/packageall/'+sys.argv[2],'/dataall/www/'+sys.argv[1]+'.tar.gz'])  
        print("所有机器发布失败")

else:
    print("机器启动中,请10分钟后再试试")

客户端脚本

from flask import Flask
from flask_httpauth import HTTPBasicAuth
from flask import request
from flask import jsonify
import subprocess
import json
import requests
import os
import time

app = Flask(__name__)
auth = HTTPBasicAuth()

users = {
    "admin": "httppasswd",
}

api_module=['m1','m2','m3','m4']

@auth.get_password
def get_pw(username):
    if username in users:
        return users.get(username)
    return None



@app.route('/postbuild',methods=["POST"])  
@auth.login_required
def reload_code():
    print(request.data.decode('utf-8'))  
    post_data=json.loads(request.data.decode('utf-8'))
   
    p_remove_file = subprocess.call(["rm","-rf","/dataall/www/"+post_data.get('module')])  

    res = subprocess.call(["tar","-xvf","/opt/packageall/"+post_data.get('version'),"-C","/dataall/www/"])   


    data=("模块名称:{name}, 代码信息 {test}".format(name=request.data.decode('utf-8'), test=file_info))

    if res==0:
        data={'code':'0','result':data}
        return json.dumps(data),200
    else:
        subprocess.call(["/usr/local/openresty/nginx/sbin/nginx","-s","stop"])  
        data={'code':'1','result':file_info}
        return json.dumps(data),200


@app.route('/php',methods=["GET","POST"])
@auth.login_required
def php_fpm():

    www_conf = os.stat('/etc/php-fpm.d/www.conf')
    php_ini = os.stat('/etc/php.ini')
    file_info={'www.conf':www_conf,'php.ini':php_ini}
    res = subprocess.call(["systemctl","restart","php-fpm"])
    if res==0:
        data={'code':'0','result':file_info}
        return json.dumps(data),200
    else:
        subprocess.call(["/usr/local/openresty/nginx/sbin/nginx","-s","stop"]) 
        data={'code':'1','result':file_info}
        return json.dumps(data),200

@app.route('/nginx',methods=["GET","POST","PUT","DELETE"])
@auth.login_required
def nginx():
    try:
        print(request.data.decode('utf-8')) #打印POST数据
        subprocess.call(["cp","-rp","/nfs/conf","/usr/local/openresty/nginx/"])
        subprocess.call(["cp","-rp","/nfs/vhost","/server/"])

        nginx_conf = os.stat('/usr/local/openresty/nginx/conf/nginx.conf')
        vhost_conf = os.stat('/xxxx/www.conf')
        file_info={'nginx.conf':nginx_conf,'vhost':vhost_conf}
        
        args=json.loads(request.data.decode('utf-8'))
        out_bytes = subprocess.check_output(['/usr/local/openresty/nginx/sbin/nginx','-t'],stderr=subprocess.STDOUT)  

        print("重启nginx")
        if args.get('action')=='restart':
            subprocess.call(["/usr/local/openresty/nginx/sbin/nginx","-s","stop"])  
            subprocess.call(["/usr/local/openresty/nginx/sbin/nginx"])              
            data={'code':'0','result':file_info}
        elif args.get('action')=='reload':
            subprocess.call(["/usr/local/openresty/nginx/sbin/nginx","-s","reload"])
            data={'code':'0','result':file_info}
        else:
            pass
            data={'code':'0','result':file_info}
        return json.dumps(data),200
    except subprocess.CalledProcessError as e:
        out_bytes = e.output       # Output generated before error
        code      = e.returncode   # Return code
        data={'code':code,'result':out_bytes.decode('utf-8')}
        return json.dumps(data),200

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=9888)

发布的解决的需求是将git中文件如何合理的输送到代码服务器中,方式千变万化

猜你喜欢

转载自blog.csdn.net/startfefesfe/article/details/132168175
今日推荐