blob: 0a3161ec923a0f95459d43ccf7264a1e2babae85 [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
Aviv Keshet6059fd92014-07-23 13:50:23 -07008
9from chromite.lib import cidb
10from chromite.lib import commandline
11from chromite.lib import cros_build_lib
Ralph Nathan91874ca2015-03-19 13:29:41 -070012from chromite.lib import cros_logging as logging
Aviv Keshet18929522015-12-07 13:20:00 -080013from chromite.lib import git
Aviv Keshet6059fd92014-07-23 13:50:23 -070014
Mike Frysingerd6e2df02014-11-26 02:55:04 -050015
Aviv Keshet6059fd92014-07-23 13:50:23 -070016MIGRATE = 'migrate'
17WIPE = 'wipe'
18
19COMMANDS = [MIGRATE, WIPE]
20
Mike Frysingerd6e2df02014-11-26 02:55:04 -050021
Aviv Keshet6059fd92014-07-23 13:50:23 -070022def GetParser():
23 """Creates the argparse parser."""
24 parser = commandline.ArgumentParser(description=__doc__)
25
26 # Put options that control the mode of script into mutually exclusive group.
27
28 parser.add_argument('command', action='store', choices=COMMANDS,
29 help='The action to execute.')
30 parser.add_argument('cred_dir', action='store',
31 metavar='CIDB_CREDENTIALS_DIR',
32 help='Database credentials directory with certificates '
33 'and other connection information.')
34 parser.add_argument('--migrate-version', action='store', default=None,
35 help='Maximum schema version to migrate to.')
36
37 return parser
38
39
40def main(argv):
41 parser = GetParser()
42 options = parser.parse_args(argv)
43
44 logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
45
Aviv Keshet18929522015-12-07 13:20:00 -080046 # Check that we have no uncommitted files, and that our checkout's HEAD is
47 # contained in a remote branch. This is to ensure that we don't accidentally
48 # run uncommitted migrations.
49 uncommitted_files = git.RunGit(os.getcwd(), ['status', '-s']).output
50 if uncommitted_files:
51 cros_build_lib.Die('You appear to have uncommitted files. Aborting!')
52
53 remote_branches = git.RunGit(
54 os.getcwd(), ['branch', '-r', '--contains']).output
55 if not remote_branches:
56 cros_build_lib.Die(
57 'You appear to be on a local branch of chromite. Aborting!')
58
59
Aviv Keshet6059fd92014-07-23 13:50:23 -070060 if options.command == MIGRATE:
61 positive_confirmation = 'please modify my database'
62 warn = ('This option will apply schema changes to your existing database. '
63 'You should not run this against the production database unless '
64 'your changes are thoroughly tested, and those tests included '
65 'in cidb_integration_test.py (including tests that old data is '
66 'sanely migrated forward). Database corruption could otherwise '
67 'result. Are you sure you want to proceed? If so, type "%s" '
68 'now.\n') % positive_confirmation
69 elif options.command == WIPE:
70 positive_confirmation = 'please delete my data'
71 warn = ('This operation will wipe (i.e. DELETE!) the entire contents of '
72 'the database pointed at by %s. Are you sure you want to proceed? '
73 'If so, type "%s" now.\n') % (
74 os.path.join(options.cred_dir, 'host.txt'),
75 positive_confirmation)
76 else:
Aviv Keshet18929522015-12-07 13:20:00 -080077 cros_build_lib.Die('No command or unsupported command. Exiting.')
Aviv Keshet6059fd92014-07-23 13:50:23 -070078
Mike Frysinger383367e2014-09-16 15:06:17 -040079 print(warn)
Chris McDonald00e31c32021-06-15 12:33:51 -060080 conf_string = input('(%s)?: ' % positive_confirmation)
Aviv Keshet6059fd92014-07-23 13:50:23 -070081 if conf_string != positive_confirmation:
Aviv Keshet18929522015-12-07 13:20:00 -080082 cros_build_lib.Die('You changed your mind. Aborting.')
Aviv Keshet6059fd92014-07-23 13:50:23 -070083
84 if options.command == MIGRATE:
Mike Frysinger383367e2014-09-16 15:06:17 -040085 print('OK, applying migrations...')
Aviv Keshet6059fd92014-07-23 13:50:23 -070086 db = cidb.CIDBConnection(options.cred_dir)
Mike Frysingerd6e2df02014-11-26 02:55:04 -050087 db.ApplySchemaMigrations(maxVersion=options.migrate_version)
Aviv Keshet6059fd92014-07-23 13:50:23 -070088 elif options.command == WIPE:
Mike Frysinger383367e2014-09-16 15:06:17 -040089 print('OK, wiping database...')
Aviv Keshet6059fd92014-07-23 13:50:23 -070090 db = cidb.CIDBConnection(options.cred_dir)
91 db.DropDatabase()
Mike Frysinger383367e2014-09-16 15:06:17 -040092 print('Done.')