|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
import abc |
|
17
|
|
|
import importlib |
|
18
|
|
|
import inspect |
|
19
|
|
|
import six |
|
20
|
|
|
|
|
21
|
|
|
from st2common import log as logging |
|
22
|
|
|
from st2common.persistence import policy as policy_access |
|
23
|
|
|
from st2common.services import coordination |
|
24
|
|
|
|
|
25
|
|
|
LOG = logging.getLogger(__name__) |
|
26
|
|
|
|
|
27
|
|
|
__all__ = [ |
|
28
|
|
|
'ResourcePolicyApplicator', |
|
29
|
|
|
'get_driver' |
|
30
|
|
|
] |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
@six.add_metaclass(abc.ABCMeta) |
|
34
|
|
|
class ResourcePolicyApplicator(object): |
|
35
|
|
|
"""Abstract policy application class.""" |
|
36
|
|
|
|
|
37
|
|
|
def __init__(self, policy_ref, policy_type): |
|
38
|
|
|
self._policy_ref = policy_ref |
|
39
|
|
|
self._policy_type = policy_type |
|
40
|
|
|
|
|
41
|
|
|
def apply_before(self, target): |
|
42
|
|
|
""" |
|
43
|
|
|
Apply the policy before the target do work. |
|
44
|
|
|
|
|
45
|
|
|
:param target: The instance of the resource being affected by this policy. |
|
46
|
|
|
:type target: ``object`` |
|
47
|
|
|
|
|
48
|
|
|
:rtype: ``object`` |
|
49
|
|
|
""" |
|
50
|
|
|
# Warn users that the coordination service is not configured |
|
51
|
|
|
if not coordination.configured(): |
|
52
|
|
|
LOG.warn('Coordination service is not configured. Policy enforcement is best effort.') |
|
53
|
|
|
|
|
54
|
|
|
return target |
|
55
|
|
|
|
|
56
|
|
|
def apply_after(self, target): |
|
57
|
|
|
""" |
|
58
|
|
|
Apply the policy after the target does work. |
|
59
|
|
|
|
|
60
|
|
|
:param target: The instance of the resource being affected by this policy. |
|
61
|
|
|
:type target: ``object`` |
|
62
|
|
|
|
|
63
|
|
|
:rtype: ``object`` |
|
64
|
|
|
""" |
|
65
|
|
|
# Warn users that the coordination service is not configured |
|
66
|
|
|
if not coordination.configured(): |
|
67
|
|
|
LOG.warn('Coordination service is not configured. Policy enforcement is best effort.') |
|
68
|
|
|
|
|
69
|
|
|
return target |
|
70
|
|
|
|
|
71
|
|
|
def _get_lock_name(self, values): |
|
72
|
|
|
""" |
|
73
|
|
|
Return a safe string which can be used as a lock name. |
|
74
|
|
|
|
|
75
|
|
|
:param values: Dictionary with values to use in the lock name. |
|
76
|
|
|
:type values: ``dict`` |
|
77
|
|
|
|
|
78
|
|
|
:rtype: ``st`` |
|
79
|
|
|
""" |
|
80
|
|
|
lock_uid = [] |
|
81
|
|
|
|
|
82
|
|
|
for key, value in six.iteritems(values): |
|
83
|
|
|
lock_uid.append('%s=%s' % (key, value)) |
|
84
|
|
|
|
|
85
|
|
|
lock_uid = ','.join(lock_uid) |
|
86
|
|
|
return lock_uid |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
def get_driver(policy_ref, policy_type, **parameters): |
|
90
|
|
|
policy_type_db = policy_access.PolicyType.get_by_ref(policy_type) |
|
91
|
|
|
module = importlib.import_module(policy_type_db.module, package=None) |
|
92
|
|
|
|
|
93
|
|
|
for name, obj in inspect.getmembers(module, predicate=inspect.isclass): |
|
94
|
|
|
if obj.__module__ != module.__name__: |
|
95
|
|
|
# Ignore classes which are just imported, but not defined in the module we are |
|
96
|
|
|
# interested in |
|
97
|
|
|
continue |
|
98
|
|
|
|
|
99
|
|
|
if (issubclass(obj, ResourcePolicyApplicator) and not obj.__name__.startswith('Base')): |
|
|
|
|
|
|
100
|
|
|
return obj(policy_ref, policy_type, **parameters) |
|
101
|
|
|
|