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 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 9 | import os |
| 10 | import logging |
| 11 | |
| 12 | from chromite.lib import cidb |
| 13 | from chromite.lib import commandline |
| 14 | from chromite.lib import cros_build_lib |
| 15 | |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame^] | 16 | |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 17 | MIGRATE = 'migrate' |
| 18 | WIPE = 'wipe' |
| 19 | |
| 20 | COMMANDS = [MIGRATE, WIPE] |
| 21 | |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame^] | 22 | |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 23 | def 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 | |
| 41 | def 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 Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 64 | print('No command or unsupported command. Exiting.') |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 65 | exit() |
| 66 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 67 | print(warn) |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 68 | conf_string = cros_build_lib.GetInput('(%s)?: ' % positive_confirmation) |
| 69 | if conf_string != positive_confirmation: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 70 | print('You changed your mind. Aborting.') |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 71 | exit() |
| 72 | |
| 73 | if options.command == MIGRATE: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 74 | print('OK, applying migrations...') |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 75 | db = cidb.CIDBConnection(options.cred_dir) |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame^] | 76 | db.ApplySchemaMigrations(maxVersion=options.migrate_version) |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 77 | elif options.command == WIPE: |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 78 | print('OK, wiping database...') |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 79 | db = cidb.CIDBConnection(options.cred_dir) |
| 80 | db.DropDatabase() |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 81 | print('Done.') |
Aviv Keshet | 6059fd9 | 2014-07-23 13:50:23 -0700 | [diff] [blame] | 82 | |
| 83 | |