基于Flask的WebHooks服务搭建

砖头
2021-12-21 / 0 评论 / 77 阅读 / 正在检测是否收录...

1、首先确保已正确安装nginx,supervisor,并正常启用nginx和supervisor

yum install nginx supervisor
systemctl start nginx
systemctl enable nginx
systemctl start supervisor
systemctl enable supervisor

2、新建Flask项目和相关虚拟环境包等

virtualenv venv #新建虚拟环境
. venv/bin/activate
pip install gunicorn

3、修改app.py文件

import os
from flask import Flask, request, jsonify

app = Flask(__name__)

# gitEE中webhooks的secret
gitee_secret = 'XXXX'


@app.route('/hooks', methods=['POST'])
def post_data():
    # 认证签名是否有效
    signature = request.headers.get('X-Gitee-Token', '')
    print("获取到访问token:%s", signature)

    if signature != gitee_secret:
        return "token认证无效", 401
    # 运行shell脚本,更新代码
    os.system('sh deploy.sh')
    return jsonify({"status": 200})


if __name__ == '__main__':
    app.run()

3、新建deploy.sh文件

echo '========Start DEPLY========'
echo '--------enter Dir ------------'
cd /home/opc/XXXX
echo '--------start git pull------------'
git pull
echo '--------init requirements------------'
. venv/bin/activate
pip install -r requirements.txt
deactivate
echo '-----ex root-----'
sudo -i
echo '-----restart supervision-----'
supervisorctl restart XXXX
echo '----- reload nginx-----'
nginx -s reload
echo '========END DEPLY========'

4、新建supervisor.ini文件(也可省略,手工启动即可)

;[include]
;files = relative/directory/*.ini
[program:XXXX]
directory=/home/opc/XXXX
command=/home/opc/XXXX/venv/bin/python3 /home/opc/XXXX/venv/bin/gunicorn -b 127.0.0.1:1111 app:app
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true

5、将supervisor.ini文件拷贝到/etc/supervisor.d/目录下,并重启supervisord

0

评论 (0)

取消