一、安装基础环境
1.1 安装python 和 pip
yum install -y epel-release
yum install -y https://centos7.iuscommunity.org/ius-release.rpm
yum install -y python36u python36u-devel python36u-pip
# 更新pip
pip3.6 install --upgrade pip
pip -V
知识兔1.2 安装和新建python3虚拟环境
# 安装
pip install -U virtualenv
rm -f /usr/bin/python3 && ln -s /usr/bin/python3.6 /usr/bin/python3
# 建立virtualenv目录
mkdir -p /data/virtualenv
# 新建python3虚拟环境(纯净安装,名称为loonflow)
cd /data/virtualenv
virtualenv --no-site-packages -p /usr/bin/python3.6 loonflow
# 激活进入虚拟环境
source /data/virtualenv/loonflow/bin/activate
(loonflow) [root@localhost virtualenv]#
知识兔1.3 安装mysql
#下载yum源
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
#安装yum源
yum -y install mysql57-community-release-el7-10.noarch.rpm
#yum安装MySQL服务
yum -y install mysql-community-server MySQL-python mysql-devel
#首先启动MySQL
systemctl start mysqld.service
#查看密码
此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:
grep "password" /var/log/mysqld.log
如下命令进入数据库:
mysql -uroot -p
输入初始密码,此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin@123';
知识兔1.4 创建数据库
#创建库指定utf8编码
#后端数据库
mysql> CREATE DATABASE loonflow DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
#前端数据库
mysql> CREATE DATABASE shutongflow DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
知识兔1.5 安装redis
启动redis(用于生成唯一的工单流水号+celery异步任务[执行脚本、通知脚本])
#安装
yum install -y redis
# 启动
systemctl start redis
systemctl enable redis
知识兔二、安装后端
2.1 下载loonflow代码
cd /opt
git clone https://github.com/blackholll/loonflow.git
# 切换到 r0.3.19 分支
cd loonflow/
git checkout r0.3.19
知识兔2.2 安装loonflow依赖
# 进入虚拟环境
source /data/virtualenv/loonflow/bin/activate
# 安装requirements依赖
cd /opt/loonflow/requirements
# 添加requests模块
echo "requests" >> common.txt
# 安装
(loonflow) # pip install -r dev.txt
知识兔2.3 配置loonflow
1)配置DB
# loonflow/settings/dev.py
……
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'loonflow', # 刚刚新建的数据库名称
'USER': 'root', # 数据库用户
'PASSWORD': 'Admin@123', # 数据库密码
'HOST': '127.0.0.1', # 数据库机器IP
'PORT': '3306', # 端口
}
}
……
知识兔2)配置监听IP
# loonflow/settings/common.py
# ALLOWED_HOSTS 改成如下
ALLOWED_HOSTS = ['*']
知识兔3)初始化db
python manage.py makemigrations
python manage.py migrate
知识兔4)创建超级用户
python manage.py createsuperuser
账号:root,密码:Admin123
知识兔2.4 启动后端
python manage.py runserver 0.0.0.0:6060
知识兔启动之后,就可以访问admin后台,然后随便测试一个接口
# 后端url
http://172.16.34.250:6060
# 后端管理url
http://172.16.34.250:6060/admin
# api
http://172.16.34.250:6060/api/v1.0/workflows
知识兔三、安装前端
使用第三方代码:https://github.com/youshutong2080/shutongFlow
本系统使用Vue.js + Django开发,前端展示由loonflow配置决定,可以说前端是全动态,只需要配置好loonflow即可。
3.1 下载shutongFlow
cd /opt
git clone https://github.com/youshutong2080/shutongFlow.git
知识兔3.2 安装依赖
# 激活进入虚拟环境
source /data/virtualenv/loonflow/bin/activate
cd /opt/shutongFlow/apps
pip install -r requirements.txt
知识兔3.3 配置shutongFlow
# 初始化db
python manage.py makemigrations
python manage.py migrate
# 导入第三方数据(这里主要是用户数据)
cd /opt/shutongFlow/
mysql -uroot shutongflow < shutongflow.sql
# 导入loonflow数据(配置数据及用户数据)
mysql -uroot loonflownew < loonflownew.sql
知识兔3.4 启动前端
# 启动shutongFlow
cd /opt/shutongFlow/apps
python manage.py runserver 0.0.0.0:6062
知识兔# 启动vue,会监听6061端口,前端的访问端口。
npm install .
npm run dev
知识兔3.5 启动通知服务
# 启动celery任务:
celery -A tasks worker -l info -Q loonflow
知识兔详细命令如下:celery multi start -A tasks worker -l info -c 8 -Q loonflow --logfile=xxx.log --pidfile=xxx.pid
参数说明:-c为启动的celery进程数, logfile为日志文件路径, pidfile为pid文件路径,可自行视情况调整。
四、前后端用户同步
前端数据库:shutongflow.user(username,alias,email)
后端数据库:loonflow.account_loonuser(username,alias,email)
当前端shutongflow库中的user表有插入动作,则把(username,alias,email)数据同步到后端loonflow库的account_loonuser表的(username,alias,email)数据。
4.1 插入的触发器
use shutongflow
DELIMITER //
CREATE TRIGGER user_trigger AFTER INSERT ON user FOR EACH ROW
BEGIN
INSERT INTO loonflow.account_loonuser(username,alias,email) VALUES(new.username,new.alias,new.email);
END ;
//
DELIMITER ;
知识兔注:在命令提示符下输入delimiter // 这样是用//替换换行符,这样可避免点击换行键时执行程序。
4.2 查询触发器
select * from information_schema.triggers\G
知识兔4.3 查看数据
select * from shutongflow.user;
select * from loonflow.account_loonuser;
知识兔五、配置LDAP认证
当前端ldap登入后,会在user表中插入用户信息,然后触发器会同步数据到后端loonflow.account_loonuser用户表。
5.1 安装模块
# 激活进入虚拟环境
source /data/virtualenv/loonflow/bin/activate
pip install python-ldap django_auth_ldap
知识兔5.2 配置文件
修改前端同步ldap文件:shutongFlow/apps/apps/settings.py
import ldap
from django_auth_ldap.config import LDAPSearch, LDAPSearchUnion, GroupOfNamesType
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend', #配置为先使用LDAP认证,如通过认证则不再使用后面的认证方式
'django.contrib.auth.backends.ModelBackend',
)
AUTH_LDAP_SERVER_URI = "ldap://ldap.wmq.com:389" # ldap服务器地址
AUTH_LDAP_BIND_DN = 'cn=manager,dc=wmq,dc=com' # 管理员账号
AUTH_LDAP_BIND_PASSWORD = 'xxxxx'
OUg = 'DC=wmq,DC=com'
AUTH_LDAP_USER_SEARCH = LDAPSearch(OUg, ldap.SCOPE_SUBTREE, "(&(objectClass=inetOrgPerson)(uid=%(user)s))")
AUTH_LDAP_USER_ATTR_MAP = {"first_name": "givenName", "last_name": "sn", "alias": "sn", "email": "mail", "username":"name"}
AUTH_LDAP_ALWAYS_UPDATE_USER = True # 是否同步LDAP修改
AUTH_USER_MODEL = 'account.Users'
知识兔六、附:启动命令
进入虚拟环境
source /data/virtualenv/loonflow/bin/activate+ 启动后端
cd loonflow/
python manage.py runserver 0.0.0.0:6060
+ 启动前端
cd shutongFlow
python apps/manage.py runserver 0.0.0.0:6062
+ 启动vue
cd shutongFlow/fronted
npm run dev
+ 启动通知功能
cd loonflow/
celery -A tasks worker -l info -Q loonflow
访问前端:http://172.31.57.1:6061
默认账号:admin,密码:yxuqtr
前端数据库:http://172.31.57.1:6062/admin/
访问后端(支持ldap):http://172.31.57.1:6060
新建账号:root,密码:Admin123
后端数据库:http://172.31.57.1:6060/admin/
去除“所有工单”功能按钮,找到//注释即可。
shutongFlow/fronted/src/router/routers.js
知识兔七、附:企业微信通知脚本
send_wechat.py
内容如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
通知脚本loonflow会将标题、内容、参与人等信息作为全局变量传给通知脚本,脚本中直接使用这些参数,通过各自的发送消息逻辑将信息发送出去。
详情参数参考:loonflow/media/notice_script/demo_notice_script.py 里面有变量名称。
本脚本引用三个变量:participant:接收人,content_result:内容,title_result:标题。
另外通知脚本内容是exec来执行的,不是直接执行脚本。所以 __name__ !== "__main__",不能用if __name__ == "__main__"。
"""
import json
import requests
class WeChat:
def __init__(self):
self.CORPID = 'wxc08*****7d5b2cb'
self.CORPSECRET = 'pmv*******************QBRv5w'
self.AGENTID = '1000020'
def _get_access_token(self):
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
values = {'corpid': self.CORPID,
'corpsecret': self.CORPSECRET,
}
req = requests.post(url, params=values)
return req
def get_access_token(self):
get_req = self._get_access_token()
if get_req.status_code != 200:
print('连接服务器失败')
else:
get_req_json = json.loads(get_req.text)
if get_req_json['errcode'] != 0:
print('响应结果不正确')
else:
access_token = get_req_json['access_token']
return access_token
def send_data(self):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.get_access_token()
send_values = {
"touser": participant,
"msgtype": "text",
"agentid": self.AGENTID,
"text": {
"content": '%s <a href="https://zhishitu.com">' % (content_result,title_result)
},
"safe": "0"
}
send_msges = (bytes(json.dumps(send_values), 'utf-8'))
respone = requests.post(send_url, send_msges)
respone = respone.json()
return respone["errmsg"]
wx = WeChat()
wx.send_data()
知识兔参考:
后端:https://github.com/blackholll/loonflow
前端:https://github.com/youshutong2080/shutongFlow
通知脚本:https://github.com/blackholll/loonflow-helper/blob/master/notice_script_demo/notice_script_demo.py
django接入ldap:https://igolang.cn/Python/django%20%E6%8E%A5%E5%85%A5%20ldap/