|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2022 The StackStorm Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# 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 | +Migration which deletes the marker collections now that st2exporter has been removed. |
| 18 | +Only st2exporter used the marker collections. |
| 19 | +
|
| 20 | +NB: Most people will not have these collections because st2exporter was optional, |
| 21 | +and the collections were not configured to be created automatically. |
| 22 | +""" |
| 23 | + |
| 24 | +import sys |
| 25 | +import traceback |
| 26 | + |
| 27 | +import mongoengine as me |
| 28 | + |
| 29 | +from mongoengine.connection import get_db |
| 30 | +from oslo_config import cfg |
| 31 | + |
| 32 | +from st2common import config |
| 33 | +from st2common.service_setup import db_setup |
| 34 | +from st2common.service_setup import db_teardown |
| 35 | + |
| 36 | + |
| 37 | +MARKER_COLLECTION = "marker_d_b" |
| 38 | +DUMPER_MARKER_COLLECTION = "dumper_marker_d_b" |
| 39 | + |
| 40 | + |
| 41 | +def delete_marker_collections(): |
| 42 | + db = get_db() |
| 43 | + collections = db.collection_names() |
| 44 | + |
| 45 | + if MARKER_COLLECTION in collections: |
| 46 | + print(f"Dropping {MARKER_COLLECTION} collection...") |
| 47 | + db[MARKER_COLLECTION].drop() |
| 48 | + |
| 49 | + if DUMPER_MARKER_COLLECTION in collections: |
| 50 | + print(f"Dropping {DUMPER_MARKER_COLLECTION} collection...") |
| 51 | + db[DUMPER_MARKER_COLLECTION].drop() |
| 52 | + |
| 53 | + |
| 54 | +def main(): |
| 55 | + config.parse_args() |
| 56 | + db_setup() |
| 57 | + |
| 58 | + try: |
| 59 | + delete_marker_collections(display_prompt=not cfg.CONF.yes) |
| 60 | + exit_code = 0 |
| 61 | + except Exception as e: |
| 62 | + print("ABORTED: Collection deletion aborted on first failure: %s" % (str(e))) |
| 63 | + traceback.print_exc() |
| 64 | + exit_code = 1 |
| 65 | + |
| 66 | + db_teardown() |
| 67 | + sys.exit(exit_code) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + main() |
0 commit comments