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