Test Failed
Push — master ( e380d0...f5671d )
by W
02:58
created

st2common/st2common/services/policies.py (10 issues)

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
from __future__ import absolute_import
17
18
from st2common.constants import action as ac_const
19
from st2common import log as logging
20
from st2common.persistence import policy as pc_db_access
21
from st2common import policies as engine
22
23
24
LOG = logging.getLogger(__name__)
25
26
27
def apply_pre_run_policies(lv_ac_db):
28
    LOG.debug('Applying pre-run policies for liveaction "%s".' % str(lv_ac_db.id))
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
29
30
    policy_dbs = pc_db_access.Policy.query(resource_ref=lv_ac_db.action, enabled=True)
31
    LOG.debug('Identified %s policies for the action "%s".' % (len(policy_dbs), lv_ac_db.action))
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
32
33
    for policy_db in policy_dbs:
34
        LOG.debug('Getting driver for policy "%s" (%s).' % (policy_db.ref, policy_db.policy_type))
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
35
        driver = engine.get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters)
36
37
        try:
38
            message = 'Applying policy "%s" (%s) for liveaction "%s".'
39
            LOG.info(message % (policy_db.ref, policy_db.policy_type, str(lv_ac_db.id)))
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
40
            lv_ac_db = driver.apply_before(lv_ac_db)
41
        except:
42
            message = 'An exception occurred while applying policy "%s" (%s) for liveaction "%s".'
43
            LOG.exception(message % (policy_db.ref, policy_db.policy_type, str(lv_ac_db.id)))
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
44
45
        if lv_ac_db.status == ac_const.LIVEACTION_STATUS_DELAYED:
46
            break
47
48
    return lv_ac_db
49
50
51
def apply_post_run_policies(lv_ac_db):
52
    LOG.debug('Applying post run policies for liveaction "%s".' % str(lv_ac_db.id))
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
53
54
    policy_dbs = pc_db_access.Policy.query(resource_ref=lv_ac_db.action, enabled=True)
55
    LOG.debug('Identified %s policies for the action "%s".' % (len(policy_dbs), lv_ac_db.action))
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
56
57
    for policy_db in policy_dbs:
58
        LOG.debug('Getting driver for policy "%s" (%s).' % (policy_db.ref, policy_db.policy_type))
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
59
        driver = engine.get_driver(policy_db.ref, policy_db.policy_type, **policy_db.parameters)
60
61
        try:
62
            message = 'Applying policy "%s" (%s) for liveaction "%s".'
63
            LOG.info(message % (policy_db.ref, policy_db.policy_type, str(lv_ac_db.id)))
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
64
            lv_ac_db = driver.apply_after(lv_ac_db)
65
        except:
66
            message = 'An exception occurred while applying policy "%s" (%s) for liveaction "%s".'
67
            LOG.exception(message % (policy_db.ref, policy_db.policy_type, str(lv_ac_db.id)))
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
68
69
    return lv_ac_db
70