1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
3
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
4
|
|
|
# this work for additional information regarding copyright ownership. |
5
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
6
|
|
|
# (the "License"); you may not use this file except in compliance with |
7
|
|
|
# the License. You may obtain a copy of the License at |
8
|
|
|
# |
9
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
# |
11
|
|
|
# Unless required by applicable law or agreed to in writing, software |
12
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
# See the License for the specific language governing permissions and |
15
|
|
|
# limitations under the License. |
16
|
|
|
|
17
|
|
|
import mongoengine as me |
18
|
|
|
|
19
|
|
|
from st2common import config |
20
|
|
|
from st2common.constants.pack import DEFAULT_PACK_NAME |
21
|
|
|
from st2common.service_setup import db_setup |
22
|
|
|
from st2common.service_setup import db_teardown |
23
|
|
|
from st2common.models.db import MongoDBAccess |
24
|
|
|
from st2common.models.db import stormbase |
25
|
|
|
from st2common.models.db.rule import ActionExecutionSpecDB |
26
|
|
|
from st2common.models.system.common import ResourceReference |
27
|
|
|
from st2common.persistence.base import Access, ContentPackResource |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class Migration(object): |
31
|
|
|
class RuleDB(stormbase.StormFoundationDB, stormbase.TagsMixin, |
32
|
|
|
stormbase.ContentPackResourceMixin): |
33
|
|
|
"""Specifies the action to invoke on the occurrence of a Trigger. It |
34
|
|
|
also includes the transformation to perform to match the impedance |
35
|
|
|
between the payload of a TriggerInstance and input of a action. |
36
|
|
|
Attribute: |
37
|
|
|
trigger: Trigger that trips this rule. |
38
|
|
|
criteria: |
39
|
|
|
action: Action to execute when the rule is tripped. |
40
|
|
|
status: enabled or disabled. If disabled occurrence of the trigger |
41
|
|
|
does not lead to execution of a action and vice-versa. |
42
|
|
|
""" |
43
|
|
|
name = me.StringField(required=True) |
44
|
|
|
ref = me.StringField(required=True) |
45
|
|
|
description = me.StringField() |
46
|
|
|
pack = me.StringField( |
47
|
|
|
required=False, |
48
|
|
|
help_text='Name of the content pack.', |
49
|
|
|
unique_with='name') |
50
|
|
|
trigger = me.StringField() |
51
|
|
|
criteria = stormbase.EscapedDictField() |
52
|
|
|
action = me.EmbeddedDocumentField(ActionExecutionSpecDB) |
53
|
|
|
enabled = me.BooleanField(required=True, default=True, |
54
|
|
|
help_text=u'Flag indicating whether the rule is enabled.') |
55
|
|
|
|
56
|
|
|
meta = { |
57
|
|
|
'indexes': stormbase.TagsMixin.get_indices() |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
# specialized access objects |
62
|
|
|
rule_access_with_pack = MongoDBAccess(Migration.RuleDB) |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
class RuleDB(stormbase.StormBaseDB, stormbase.TagsMixin): |
66
|
|
|
"""Specifies the action to invoke on the occurrence of a Trigger. It |
67
|
|
|
also includes the transformation to perform to match the impedance |
68
|
|
|
between the payload of a TriggerInstance and input of a action. |
69
|
|
|
Attribute: |
70
|
|
|
trigger: Trigger that trips this rule. |
71
|
|
|
criteria: |
72
|
|
|
action: Action to execute when the rule is tripped. |
73
|
|
|
status: enabled or disabled. If disabled occurrence of the trigger |
74
|
|
|
does not lead to execution of a action and vice-versa. |
75
|
|
|
""" |
76
|
|
|
trigger = me.StringField() |
77
|
|
|
criteria = stormbase.EscapedDictField() |
78
|
|
|
action = me.EmbeddedDocumentField(ActionExecutionSpecDB) |
79
|
|
|
enabled = me.BooleanField(required=True, default=True, |
80
|
|
|
help_text=u'Flag indicating whether the rule is enabled.') |
81
|
|
|
|
82
|
|
|
meta = { |
83
|
|
|
'indexes': stormbase.TagsMixin.get_indices() |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
rule_access_without_pack = MongoDBAccess(RuleDB) |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
class RuleWithoutPack(Access): |
91
|
|
|
impl = rule_access_without_pack |
92
|
|
|
|
93
|
|
|
@classmethod |
94
|
|
|
def _get_impl(cls): |
95
|
|
|
return cls.impl |
96
|
|
|
|
97
|
|
|
@classmethod |
98
|
|
|
def _get_by_object(cls, object): |
|
|
|
|
99
|
|
|
# For Rule name is unique. |
100
|
|
|
name = getattr(object, 'name', '') |
101
|
|
|
return cls.get_by_name(name) |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
class RuleWithPack(ContentPackResource): |
105
|
|
|
impl = rule_access_with_pack |
106
|
|
|
|
107
|
|
|
@classmethod |
108
|
|
|
def _get_impl(cls): |
109
|
|
|
return cls.impl |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
def migrate_rules(): |
113
|
|
|
try: |
114
|
|
|
existing_rules = RuleWithoutPack.get_all() |
115
|
|
|
|
116
|
|
|
for rule in existing_rules: |
117
|
|
|
rule_with_pack = Migration.RuleDB( |
118
|
|
|
id=rule.id, |
119
|
|
|
name=rule.name, |
120
|
|
|
description=rule.description, |
121
|
|
|
trigger=rule.trigger, |
122
|
|
|
criteria=rule.criteria, |
123
|
|
|
action=rule.action, |
124
|
|
|
enabled=rule.enabled, |
125
|
|
|
pack=DEFAULT_PACK_NAME, |
126
|
|
|
ref=ResourceReference.to_string_reference(pack=DEFAULT_PACK_NAME, |
127
|
|
|
name=rule.name) |
128
|
|
|
) |
129
|
|
|
print('Migrating rule: %s to rule: %s' % (rule.name, rule_with_pack.ref)) |
130
|
|
|
RuleWithPack.add_or_update(rule_with_pack) |
131
|
|
|
except Exception as e: |
132
|
|
|
print('Migration failed. %s' % str(e)) |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
def main(): |
136
|
|
|
config.parse_args() |
137
|
|
|
|
138
|
|
|
# Connect to db. |
139
|
|
|
db_setup() |
140
|
|
|
|
141
|
|
|
# Migrate rules. |
142
|
|
|
migrate_rules() |
143
|
|
|
|
144
|
|
|
# Disconnect from db. |
145
|
|
|
db_teardown() |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
if __name__ == '__main__': |
149
|
|
|
main() |
150
|
|
|
|
It is generally discouraged to redefine built-ins as this makes code very hard to read.