File: manage.py

package info (click to toggle)
freezer-api 13.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,880 kB
  • sloc: python: 15,656; sh: 389; makefile: 59
file content (98 lines) | stat: -rw-r--r-- 2,851 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
(c) Copyright 2015-2016 Hewlett-Packard Enterprise Development Company, L.P.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

"""
import sys

from oslo_config import cfg
from oslo_log import log
from oslo_serialization import jsonutils as json

from freezer_api import __version__ as FREEZER_API_VERSION
from freezer_api.common import config
from freezer_api.db import manager


CONF = cfg.CONF
LOG = log.getLogger(__name__)


DEFAULT_INDEX = 'freezer'
DEFAULT_REPLICAS = 0


def add_db_opts(subparser):
    parser = subparser.add_parser('db')
    parser.add_argument(
        'options',
        choices=['sync', 'update', 'remove', 'show', 'update-settings'],
        help='Create/update/delete freezer-api mappings in DB backend.'
    )


def parse_config():
    DB_INIT = [
        cfg.SubCommandOpt('db',
                          dest='db',
                          title='DB Options',
                          handler=add_db_opts
                          )
    ]
    # register database backend drivers
    config.register_db_drivers_opt()
    # register database cli options
    CONF.register_cli_opts(DB_INIT)
    # register logging opts
    log.register_options(CONF)
    default_config_files = cfg.find_config_files('freezer', 'freezer-api')
    CONF(args=sys.argv[1:],
         project='freezer-api',
         default_config_files=default_config_files,
         version=FREEZER_API_VERSION
         )


def main():
    parse_config()
    config.setup_logging()

    if not CONF.db:
        CONF.print_help()
        sys.exit(0)

    try:
        db_driver = manager.get_db_driver(CONF.storage.driver,
                                          backend=CONF.storage.backend)
        if CONF.db.options.lower() == 'sync':
            db_driver.db_sync()
        elif CONF.db.options.lower() == 'update':
            db_driver.db_sync()
        elif CONF.db.options.lower() == 'remove':
            db_driver.db_remove()
        elif CONF.db.options.lower() == 'show':
            db_tables = db_driver.db_show()
            if db_tables:
                print(json.dumps(db_tables))
            else:
                print("No Tables/Mappings found!")
        else:
            raise Exception('Option {0} not found !'.format(CONF.db.options))
    except Exception as e:
        LOG.error(e)
        print(e)


if __name__ == '__main__':
    sys.exit(main())