Completed
Pull Request — master (#2380)
by W
06:39
created

st2common.cmd.main()   B

Complexity

Conditions 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 24
rs 8.9714
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 trigger instances 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.trigger_instances import purge_trigger_instances
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 trigger instances older than ' +
53
                   'this UTC timestamp. ' +
54
                   'Example value: 2015-03-13T19:01:27.255542Z')
55
    ]
56
    _do_register_cli_opts(cli_opts)
57
58
59
def main():
60
    _register_cli_opts()
61
    common_setup(config=config, setup_db=True, register_mq_exchanges=False)
62
63
    # Get config values
64
    timestamp = cfg.CONF.timestamp
65
66
    if not timestamp:
67
        LOG.error('Please supply a timestamp for purging models. Aborting.')
68
        return 1
69
    else:
70
        timestamp = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')
71
        timestamp = timestamp.replace(tzinfo=pytz.UTC)
72
73
    # Purge models.
74
    try:
75
        purge_trigger_instances(logger=LOG, timestamp=timestamp)
76
    except Exception as e:
77
        LOG.exception(str(e))
78
        return FAILURE_EXIT_CODE
79
    finally:
80
        common_teardown()
81
82
    return SUCCESS_EXIT_CODE
83