Completed
Pull Request — master (#2596)
by Edward
06:01
created

st2common.cmd.main()   B

Complexity

Conditions 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 8.8571
cc 3
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
17
"""
18
A utility script that purges st2 executions older than certain
19
timestamp.
20
21
*** RISK RISK RISK. You will lose data. Run at your own risk. ***
22
"""
23
24
from datetime import datetime
25
import pytz
26
27
from oslo_config import cfg
28
29
from st2common import config
30
from st2common import log as logging
31
from st2common.script_setup import setup as common_setup
32
from st2common.script_setup import teardown as common_teardown
33
from st2common.constants.exit_codes import SUCCESS_EXIT_CODE
34
from st2common.constants.exit_codes import FAILURE_EXIT_CODE
35
from st2common.garbage_collection.executions import purge_executions
36
37
LOG = logging.getLogger(__name__)
38
39
40
def _do_register_cli_opts(opts, ignore_errors=False):
41
    for opt in opts:
42
        try:
43
            cfg.CONF.register_cli_opt(opt)
44
        except:
45
            if not ignore_errors:
46
                raise
47
48
49
def _register_cli_opts():
50
    cli_opts = [
51
        cfg.StrOpt('timestamp', default=None,
52
                   help='Will delete execution and liveaction models older than ' +
53
                   'this UTC timestamp. ' +
54
                   'Example value: 2015-03-13T19:01:27.255542Z.'),
55
        cfg.StrOpt('action-ref', default='',
56
                   help='action-ref to delete executions for.'),
57
        cfg.BoolOpt('purge-incomplete', default=False,
58
                    help='Purge all models irrespective of their ``status``.' +
59
                    'By default, only executions in completed states such as "succeeeded" ' +
60
                    ', "failed", "canceled" and "timed_out" are deleted.'),
61
    ]
62
    _do_register_cli_opts(cli_opts)
63
64
65
def main():
66
    _register_cli_opts()
67
    common_setup(config=config, setup_db=True, register_mq_exchanges=False)
68
69
    # Get config values
70
    timestamp = cfg.CONF.timestamp
71
    action_ref = cfg.CONF.action_ref
72
    purge_incomplete = cfg.CONF.purge_incomplete
73
74
    if not timestamp:
75
        LOG.error('Please supply a timestamp for purging models. Aborting.')
76
        return 1
77
    else:
78
        timestamp = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')
79
        timestamp = timestamp.replace(tzinfo=pytz.UTC)
80
81
    try:
82
        purge_executions(logger=LOG, timestamp=timestamp, action_ref=action_ref,
83
                         purge_incomplete=purge_incomplete)
84
    except Exception as e:
85
        LOG.exception(str(e))
86
        return FAILURE_EXIT_CODE
87
    finally:
88
        common_teardown()
89
90
    return SUCCESS_EXIT_CODE
91