blob: 4457d5198ac9c6ac8f57da6726b5677ea2e2570b [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
Chris McDonald59650c32021-07-20 15:29:28 -06007import logging
Aviv Keshet6059fd92014-07-23 13:50:23 -07008import os
Aviv Keshet6059fd92014-07-23 13:50:23 -07009
10from chromite.lib import cidb
11from chromite.lib import commandline
12from chromite.lib import cros_build_lib
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
Alex Klein1699fab2022-09-08 08:46:06 -060016MIGRATE = "migrate"
17WIPE = "wipe"
Aviv Keshet6059fd92014-07-23 13:50:23 -070018
19COMMANDS = [MIGRATE, WIPE]
20
Mike Frysingerd6e2df02014-11-26 02:55:04 -050021
Aviv Keshet6059fd92014-07-23 13:50:23 -070022def GetParser():
Alex Klein1699fab2022-09-08 08:46:06 -060023 """Creates the argparse parser."""
24 parser = commandline.ArgumentParser(description=__doc__)
Aviv Keshet6059fd92014-07-23 13:50:23 -070025
Alex Klein1699fab2022-09-08 08:46:06 -060026 # Put options that control the mode of script into mutually exclusive group.
Aviv Keshet6059fd92014-07-23 13:50:23 -070027
Alex Klein1699fab2022-09-08 08:46:06 -060028 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 Keshet6059fd92014-07-23 13:50:23 -070047
Alex Klein1699fab2022-09-08 08:46:06 -060048 return parser
Aviv Keshet6059fd92014-07-23 13:50:23 -070049
50
51def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060052 parser = GetParser()
53 options = parser.parse_args(argv)
Aviv Keshet6059fd92014-07-23 13:50:23 -070054
Alex Klein1699fab2022-09-08 08:46:06 -060055 logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
Aviv Keshet6059fd92014-07-23 13:50:23 -070056
Alex Klein1699fab2022-09-08 08:46:06 -060057 # 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 Keshet18929522015-12-07 13:20:00 -080063
Alex Klein1699fab2022-09-08 08:46:06 -060064 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 Keshet18929522015-12-07 13:20:00 -080071
Alex Klein1699fab2022-09-08 08:46:06 -060072 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 Keshet6059fd92014-07-23 13:50:23 -070080 'result. Are you sure you want to proceed? If so, type "%s" '
Alex Klein1699fab2022-09-08 08:46:06 -060081 "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 Keshet6059fd92014-07-23 13:50:23 -070092
Alex Klein1699fab2022-09-08 08:46:06 -060093 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 Keshet6059fd92014-07-23 13:50:23 -070097
Alex Klein1699fab2022-09-08 08:46:06 -060098 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.")