blob: 82d469b6731d30aa4fa541cf214182f2f4cb2ad5 [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
Mike Frysinger383367e2014-09-16 15:06:17 -04007from __future__ import print_function
8
Aviv Keshet6059fd92014-07-23 13:50:23 -07009import os
10import logging
11
12from chromite.lib import cidb
13from chromite.lib import commandline
14from chromite.lib import cros_build_lib
15
16MIGRATE = 'migrate'
17WIPE = 'wipe'
18
19COMMANDS = [MIGRATE, WIPE]
20
21def 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
39def 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:
Mike Frysinger383367e2014-09-16 15:06:17 -040062 print('No command or unsupported command. Exiting.')
Aviv Keshet6059fd92014-07-23 13:50:23 -070063 exit()
64
Mike Frysinger383367e2014-09-16 15:06:17 -040065 print(warn)
Aviv Keshet6059fd92014-07-23 13:50:23 -070066 conf_string = cros_build_lib.GetInput('(%s)?: ' % positive_confirmation)
67 if conf_string != positive_confirmation:
Mike Frysinger383367e2014-09-16 15:06:17 -040068 print('You changed your mind. Aborting.')
Aviv Keshet6059fd92014-07-23 13:50:23 -070069 exit()
70
71 if options.command == MIGRATE:
Mike Frysinger383367e2014-09-16 15:06:17 -040072 print('OK, applying migrations...')
Aviv Keshet6059fd92014-07-23 13:50:23 -070073 db = cidb.CIDBConnection(options.cred_dir)
74 db.ApplySchemaMigrations(maxVersion = options.migrate_version)
75 elif options.command == WIPE:
Mike Frysinger383367e2014-09-16 15:06:17 -040076 print('OK, wiping database...')
Aviv Keshet6059fd92014-07-23 13:50:23 -070077 db = cidb.CIDBConnection(options.cred_dir)
78 db.DropDatabase()
Mike Frysinger383367e2014-09-16 15:06:17 -040079 print('Done.')
Aviv Keshet6059fd92014-07-23 13:50:23 -070080
81