Mistral部署与使用

What is Mistral?

Mistral is a workflow service. Most business processes consist of multiple distinct interconnected steps that need to be executed in a particular order in a distributed environment. One can describe such process as a set of tasks and task relations and upload such description to Mistral so that it takes care of state management, correct execution order, parallelism, synchronization and high availability. Mistral also provides flexible task scheduling so that we can run a process according to a specified schedule (i.e. every Sunday at 4.00pm) instead of running it immediately. We call such set of tasks and relations between them a workflow.

Mistral的部署

环境说明:
· Centos7.2.1511
· Openstack Liberty
· Mistral stable/mitaka

安装代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@controller ~]# git clone -b stable/mitaka https://github.com/openstack/mistral.git
[root@controller ~]# cd mistral/
[root@controller mistral]# pip install -r requirements.txt
[root@controller mistral]# python setup.py install

# 上述步骤完成后,检查下oslo依赖包的版本。oslo.config的版本最好与下面所示一致。(或不低于3.19.0)
[root@controller ~]# pip list|grep -i oslo.config
oslo.config (3.21.0)
# 检查keystonemiddleware的版本,最好与下面所示一致。
[root@controller ~]# pip list|grep -i keystonemiddleware
keystonemiddleware (4.10.0)

[root@controller mistral]# mkdir -p /etc/mistral /var/log/mistral
[root@controller mistral]# cp etc/* /etc/mistral/

生成配置文件

参考:http://docs.openstack.org/developer/mistral/guides/installation_guide.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@controller mistral]# oslo-config-generator --config-file tools/config/config-generator.mistral.conf --output-file etc/mistral.conf
[root@controller mistral]# cp etc/mistral.conf /etc/mistral/

# 配置mistral.conf,要在keystone_authtoken session中添加以下参数项:
[keystone_authtoken]

#
# From keystonemiddleware.auth_token
#

auth_uri = http://{keystone_ip}:5000
auth_version = 3
identity_uri = http://{keystone_ip}:35357/
admin_user = admin
admin_password = {admin_pass}
admin_tenant_name = admin

准备数据库

1
2
3
4
5
6
7
8
9
10
11
MariaDB [(none)]> create database mistral;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> grant all on mistral.* to mistral@'localhost' identified by 'mistral';
Query OK, 0 rows affected (0.04 sec)

MariaDB [(none)]> grant all on mistral.* to mistral@'%' identified by 'mistral';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

创建service & endpoint

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@controller ~]# keystone service-create --type workflow --name mistral --description "OpenStack Workflow service"
+-------------+----------------------------------+
| Property | Value |
+-------------+----------------------------------+
| description | OpenStack Workflow service |
| enabled | True |
| id | 2deb471789bc46d786510a57b41c4348 |
| name | mistral |
| type | workflow |
+-------------+----------------------------------+
[root@controller ~]# keystone endpoint-create --region "RegionOne" --service 2deb471789bc46d786510a57b41c4348 --publicurl http://{mistral_ip}:8989/v2 --adminurl http://{mistral_ip}:8989/v2 --internalurl http://{mistral_ip}:8989/v2
+-------------+----------------------------------+
| Property | Value |
+-------------+----------------------------------+
| adminurl | http://{mistral_ip}:8989/v2 |
| id | 43a075777d9040fa8db3061146a232c4 |
| internalurl | http://{mistral_ip}:8989/v2 |
| publicurl | http://{mistral_ip}:8989/v2 |
| region | RegionOne |
| service_id | 2deb471789bc46d786510a57b41c4348 |
+-------------+----------------------------------+

初始化数据库

1
2
3
4
# 初始化mistral的数据库
[root@controller ~]# mistral-db-manage --config-file /etc/mistral/mistral.conf upgrade head
# 添加缺省的mistral actions
[root@controller ~]# mistral-db-manage --config-file /etc/mistral/mistral.conf populate

启动服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@controller ~]# cat > /usr/lib/systemd/system/openstack-mistral-server.service << EOF
> [Unit]
> #Description=OpenStack Nova API Server
> #After=syslog.target network.target
>
> [Service]
> Type=notify
> NotifyAccess=all
> TimeoutStartSec=0
> Restart=always
> User=root
> ExecStart=/usr/bin/mistral-server --config-file /etc/mistral/mistral.conf --log-file /var/log/mistral/mistral.log
>
> [Install]
> WantedBy=multi-user.target
> EOF

[root@controller ~]# systemctl start openstack-mistral-server

Mistral的使用

常用的使用方法有:

  1. 通过模板文件(yaml格式)创建一个workflow,再以该workflow为基础,传入指定的参数(json格式),创建execution

    1
    2
    mistral workflow-create ./×××××.yaml
    mistral execution-create {workflow_name or workflow_uuid} ./×××××.json
  2. 创建定时任务,同样先创建workflow,再将workflow加入一个定时触发器中,同时也要传入指定参数(json格式)与定时触发器周期

    1
    2
    mistral workflow-create ./×××××.yaml
    mistral mistral cron-trigger-create --pattern "* * * * *" {name} {workflow_name or workflow_uuid} ./×××××.json

举栗说明

Mistral的workflow其实是actions的集合,在workflow的yaml模板中,规定了该workflow的版本、名称、类型、输入输出参数、任务及任务流程。

Workflow的yaml模板

假定下面这个文件名字为check.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
---
# 模板的版本
version: '2.0'

# 模板的名字
workflow1:
# 模板的类型,direct表示顺序型,另一个是reverse,表示反序的任务流
type: direct

# workflow的传入参数,通过json文件,如instance_body就是json文件的一个键
input:
- instance_body
- check_body
# workflow的输出参数,以下为yaql语法
# <% $.instance_uuid %>表示取变量instance_uuid的值
output:
instance: <% $.instance_uuid %>

# 任务部分
tasks:
# 任务名称,比如check任务用来检查虚拟机的资源情况
check:
# 任务描述
description: Check.
# 任务的具体action,std表示标准任务,以下的std.check即为一个自定义action,修改的文件路径为
# /usr/lib/python2.7/site-packages/mistral/actions/std_actions.py
action: std.check
# action的传入参数可以通过mistral action-get {action_name}来查看
input:
body: <% $.check_body %>
# action的输出参数,以下为yaql的写法
# <% task(check).result.check_result %>表示取任务check的结果中check_result的值
publish:
check_result: <% task(check).result.check_result %>
# 当该任务执行后需要做的操作,on-success表示任务成功后执行下列所示的别的任务
# 其他还有on-error及on-complete等
# 参考:http://docs.openstack.org/developer/mistral/dsl/dsl_v2.html#common-workflow-attributes
on-success:
- create_instance

create_instance:
description: Request to create a VNFD.
# 以下是缺省的action,可以使用mistral action-list查看
action: nova.servers_create
# action的传入参数可以通过mistral action-get {action_name}来查看
input:
name: <% $.instance_body.name %>
flavor: <% $.instance_body.flavor %>
image: <% $.instance_body.image %>
nics: <% $.instance_body.nics %>
publish:
instance_uuid: <% task(create_instance).result.id %>

传入参数的json文件

1
{"check_body": {*********}, "instance_body": {"name": "****", "image": "*****", flavor: "****", nics: {******}}}

自定义的action函数

找到下述文件,在文件末尾空白地方添加一个新的类。

1
/usr/lib/python2.7/site-packages/mistral/actions/std_actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Check(base.Action)

def __init__(self, **kwargs):
...

def run(self):
...

# 注意返回值的名字要和yaml文件中的output一致
return check_result


def test(self):
return 'Check'

自定义函数写完之后,找到下述文件,增加新的entry_point。

1
/usr/lib/python2.7/site-packages/mistral-4.0.0.0b2.dev6-py2.7.egg-info/entry_points.txt
1
2
3
4
5
6
...

[mistral.actions]
...
std.check = mistral.actions.std_actions:Check
...

之后再同步数据库

1
[root@controller ~]# mistral-db-manage --config-file /etc/mistral/mistral.conf populate

现在这个自定义功能就可以被mistral使用了。

参考