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