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 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 16 | MIGRATE = "migrate" |
| 17 | WIPE = "wipe" |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 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(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 23 | """Creates the argparse parser.""" |
| 24 | parser = commandline.ArgumentParser(description=__doc__) |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 25 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 26 | # Put options that control the mode of script into mutually exclusive group. |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 27 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 28 | parser.add_argument( |
| 29 | "command", |
| 30 | action="store", |
| 31 | choices=COMMANDS, |
| 32 | help="The action to execute.", |
| 33 | ) |
| 34 | parser.add_argument( |
| 35 | "cred_dir", |
| 36 | action="store", |
| 37 | metavar="CIDB_CREDENTIALS_DIR", |
| 38 | help="Database credentials directory with certificates " |
| 39 | "and other connection information.", |
| 40 | ) |
| 41 | parser.add_argument( |
| 42 | "--migrate-version", |
| 43 | action="store", |
| 44 | default=None, |
| 45 | help="Maximum schema version to migrate to.", |
| 46 | ) |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 47 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 48 | return parser |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 49 | |
| 50 | |
| 51 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 52 | parser = GetParser() |
| 53 | options = parser.parse_args(argv) |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 54 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 55 | logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO) |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 56 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 57 | # Check that we have no uncommitted files, and that our checkout's HEAD is |
| 58 | # contained in a remote branch. This is to ensure that we don't accidentally |
| 59 | # run uncommitted migrations. |
| 60 | uncommitted_files = git.RunGit(os.getcwd(), ["status", "-s"]).stdout |
| 61 | if uncommitted_files: |
| 62 | cros_build_lib.Die("You appear to have uncommitted files. Aborting!") |
Aviv Keshet | 1892952 | 2015-12-07 13:20:00 -0800 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 64 | remote_branches = git.RunGit( |
| 65 | os.getcwd(), ["branch", "-r", "--contains"] |
| 66 | ).stdout |
| 67 | if not remote_branches: |
| 68 | cros_build_lib.Die( |
| 69 | "You appear to be on a local branch of chromite. Aborting!" |
| 70 | ) |
Aviv Keshet | 1892952 | 2015-12-07 13:20:00 -0800 | [diff] [blame] | 71 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 72 | if options.command == MIGRATE: |
| 73 | positive_confirmation = "please modify my database" |
| 74 | warn = ( |
| 75 | "This option will apply schema changes to your existing database. " |
| 76 | "You should not run this against the production database unless " |
| 77 | "your changes are thoroughly tested, and those tests included " |
| 78 | "in cidb_integration_test.py (including tests that old data is " |
| 79 | "sanely migrated forward). Database corruption could otherwise " |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 80 | 'result. Are you sure you want to proceed? If so, type "%s" ' |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 81 | "now.\n" |
| 82 | ) % positive_confirmation |
| 83 | elif options.command == WIPE: |
| 84 | positive_confirmation = "please delete my data" |
| 85 | warn = ( |
| 86 | "This operation will wipe (i.e. DELETE!) the entire contents of " |
| 87 | "the database pointed at by %s. Are you sure you want to proceed? " |
| 88 | 'If so, type "%s" now.\n' |
| 89 | ) % (os.path.join(options.cred_dir, "host.txt"), positive_confirmation) |
| 90 | else: |
| 91 | cros_build_lib.Die("No command or unsupported command. Exiting.") |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 92 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 93 | print(warn) |
| 94 | conf_string = input("(%s)?: " % positive_confirmation) |
| 95 | if conf_string != positive_confirmation: |
| 96 | cros_build_lib.Die("You changed your mind. Aborting.") |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 97 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 98 | if options.command == MIGRATE: |
| 99 | print("OK, applying migrations...") |
| 100 | db = cidb.CIDBConnection(options.cred_dir) |
| 101 | db.ApplySchemaMigrations(maxVersion=options.migrate_version) |
| 102 | elif options.command == WIPE: |
| 103 | print("OK, wiping database...") |
| 104 | db = cidb.CIDBConnection(options.cred_dir) |
| 105 | db.DropDatabase() |
| 106 | print("Done.") |