Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / scripts / cidb_admin.py
1 # Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """Script for administering the Continuous Integration Database."""
6
7 from __future__ import print_function
8
9 import os
10 import logging
11
12 from chromite.lib import cidb
13 from chromite.lib import commandline
14 from chromite.lib import cros_build_lib
15
16 MIGRATE = 'migrate'
17 WIPE = 'wipe'
18
19 COMMANDS = [MIGRATE, WIPE]
20
21 def GetParser():
22   """Creates the argparse parser."""
23   parser = commandline.ArgumentParser(description=__doc__)
24
25   # Put options that control the mode of script into mutually exclusive group.
26
27   parser.add_argument('command', action='store', choices=COMMANDS,
28                       help='The action to execute.')
29   parser.add_argument('cred_dir', action='store',
30                       metavar='CIDB_CREDENTIALS_DIR',
31                       help='Database credentials directory with certificates '
32                            'and other connection information.')
33   parser.add_argument('--migrate-version', action='store', default=None,
34                       help='Maximum schema version to migrate to.')
35
36   return parser
37
38
39 def main(argv):
40   parser = GetParser()
41   options = parser.parse_args(argv)
42
43   logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
44
45   if options.command == MIGRATE:
46     positive_confirmation = 'please modify my database'
47     warn = ('This option will apply schema changes to your existing database. '
48             'You should not run this against the production database unless '
49             'your changes are thoroughly tested, and those tests included '
50             'in cidb_integration_test.py (including tests that old data is '
51             'sanely migrated forward). Database corruption could otherwise '
52             'result. Are you sure you want to proceed? If so, type "%s" '
53             'now.\n') % positive_confirmation
54   elif options.command == WIPE:
55     positive_confirmation = 'please delete my data'
56     warn = ('This operation will wipe (i.e. DELETE!) the entire contents of '
57             'the database pointed at by %s. Are you sure you want to proceed? '
58             'If so, type "%s" now.\n') % (
59                 os.path.join(options.cred_dir, 'host.txt'),
60                 positive_confirmation)
61   else:
62     print('No command or unsupported command. Exiting.')
63     exit()
64
65   print(warn)
66   conf_string = cros_build_lib.GetInput('(%s)?: ' % positive_confirmation)
67   if conf_string != positive_confirmation:
68     print('You changed your mind. Aborting.')
69     exit()
70
71   if options.command == MIGRATE:
72     print('OK, applying migrations...')
73     db = cidb.CIDBConnection(options.cred_dir)
74     db.ApplySchemaMigrations(maxVersion = options.migrate_version)
75   elif options.command == WIPE:
76     print('OK, wiping database...')
77     db = cidb.CIDBConnection(options.cred_dir)
78     db.DropDatabase()
79     print('Done.')
80
81