Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 1 | # 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 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 7 | import logging |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 8 | import os |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 9 | |
| 10 | from chromite.lib import cidb |
| 11 | from chromite.lib import commandline |
| 12 | from chromite.lib import cros_build_lib |
Aviv Keshet | 1892952 | 2015-12-07 13:20:00 -0800 | [diff] [blame] | 13 | from chromite.lib import git |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 14 | |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 15 | |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 16 | MIGRATE = 'migrate' |
| 17 | WIPE = 'wipe' |
| 18 | |
| 19 | COMMANDS = [MIGRATE, WIPE] |
| 20 | |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 21 | |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 22 | def 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 | |
| 40 | def main(argv): |
| 41 | parser = GetParser() |
| 42 | options = parser.parse_args(argv) |
| 43 | |
| 44 | logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) |
| 45 | |
Aviv Keshet | 1892952 | 2015-12-07 13:20:00 -0800 | [diff] [blame] | 46 | # 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. |
Mike Frysinger | 7a580c1 | 2022-08-01 21:45:41 -0400 | [diff] [blame] | 49 | uncommitted_files = git.RunGit(os.getcwd(), ['status', '-s']).stdout |
Aviv Keshet | 1892952 | 2015-12-07 13:20:00 -0800 | [diff] [blame] | 50 | if uncommitted_files: |
| 51 | cros_build_lib.Die('You appear to have uncommitted files. Aborting!') |
| 52 | |
| 53 | remote_branches = git.RunGit( |
Mike Frysinger | 7a580c1 | 2022-08-01 21:45:41 -0400 | [diff] [blame] | 54 | os.getcwd(), ['branch', '-r', '--contains']).stdout |
Aviv Keshet | 1892952 | 2015-12-07 13:20:00 -0800 | [diff] [blame] | 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 Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 60 | 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 Keshet | 1892952 | 2015-12-07 13:20:00 -0800 | [diff] [blame] | 77 | cros_build_lib.Die('No command or unsupported command. Exiting.') |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 78 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 79 | print(warn) |
Chris McDonald | 00e31c3 | 2021-06-15 12:33:51 -0600 | [diff] [blame] | 80 | conf_string = input('(%s)?: ' % positive_confirmation) |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 81 | if conf_string != positive_confirmation: |
Aviv Keshet | 1892952 | 2015-12-07 13:20:00 -0800 | [diff] [blame] | 82 | cros_build_lib.Die('You changed your mind. Aborting.') |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 83 | |
| 84 | if options.command == MIGRATE: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 85 | print('OK, applying migrations...') |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 86 | db = cidb.CIDBConnection(options.cred_dir) |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 87 | db.ApplySchemaMigrations(maxVersion=options.migrate_version) |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 88 | elif options.command == WIPE: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 89 | print('OK, wiping database...') |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 90 | db = cidb.CIDBConnection(options.cred_dir) |
| 91 | db.DropDatabase() |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 92 | print('Done.') |