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