Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 1 | # Copyright 2017 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 | |
Xixuan Wu | 8929a30 | 2017-12-07 18:09:31 -0800 | [diff] [blame] | 5 | """Module for setup environemtn for local testing & Google App Engine.""" |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 6 | |
| 7 | |
Xixuan Wu | 8929a30 | 2017-12-07 18:09:31 -0800 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 10 | import argparse |
| 11 | import os |
Xixuan Wu | 30244f9 | 2019-03-21 09:49:17 -0700 | [diff] [blame] | 12 | import shutil |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 13 | import subprocess |
| 14 | |
| 15 | import file_getter |
| 16 | |
| 17 | import local_import # pylint: disable=unused-import |
| 18 | try: |
Xinan Lin | cd6d40b | 2019-08-09 15:53:43 -0700 | [diff] [blame] | 19 | from chromite.lib import gs, git, osutils # pylint: disable=g-import-not-at-top |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 20 | except ImportError: |
Xixuan Wu | 8929a30 | 2017-12-07 18:09:31 -0800 | [diff] [blame] | 21 | print('Cannot import chromite') |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 22 | gs = None |
Xinan Lin | cd6d40b | 2019-08-09 15:53:43 -0700 | [diff] [blame] | 23 | git = None |
| 24 | osutils = None |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 25 | |
| 26 | |
| 27 | # The credentials folder for suite-scheduler service. |
| 28 | CREDS_GS_PATH = 'gs://suite-scheduler.google.com.a.appspot.com/credentials/' |
| 29 | |
Xinan Lin | cd6d40b | 2019-08-09 15:53:43 -0700 | [diff] [blame] | 30 | # Remote repositories of luci-py and Chromite. |
| 31 | LUCI_REPO = 'https://chromium.googlesource.com/infra/luci/luci-py' |
| 32 | CHROMITE_REPO = 'https://chromium.googlesource.com/chromiumos/chromite' |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 33 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 34 | |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 35 | def _install_third_party_lib(input_args): |
| 36 | """Install useful third-party lib. |
| 37 | |
| 38 | The libraries are specified in requirement.txt. They're used in both local |
| 39 | development environment and Google App Engine, which means they will be |
| 40 | uploaded to GAE. |
| 41 | |
| 42 | Args: |
| 43 | input_args: the arguments passed in by users. |
| 44 | """ |
| 45 | third_party_lib_path = file_getter.THIRD_PARTY_LIB_PATH |
| 46 | if input_args.update_lib and os.path.exists(third_party_lib_path): |
| 47 | os.rmdir(third_party_lib_path) |
| 48 | |
| 49 | if not os.path.exists(third_party_lib_path): |
Xixuan Wu | 8929a30 | 2017-12-07 18:09:31 -0800 | [diff] [blame] | 50 | print('Creating lib path: %s' % third_party_lib_path) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 51 | os.mkdir(third_party_lib_path) |
Xixuan Wu | 8929a30 | 2017-12-07 18:09:31 -0800 | [diff] [blame] | 52 | print('Installing packages') |
| 53 | cmd = ['pip', 'install', '-t', 'lib', '-r', 'requirements.txt'] |
| 54 | try: |
| 55 | subprocess.check_call(cmd) |
| 56 | except subprocess.CalledProcessError: |
| 57 | # TODO(crbug.com/792690): this is a workround of fixing installing |
| 58 | # third-party packages on different versions of Ubuntu/Debian. |
| 59 | print("Failed to run '%s'; trying a different command." % ' '.join(cmd)) |
| 60 | subprocess.check_call(['pip', 'install', '-t', 'lib', '--system', |
| 61 | '-r', 'requirements.txt']) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 62 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 63 | |
Xinan Lin | cd6d40b | 2019-08-09 15:53:43 -0700 | [diff] [blame] | 64 | def _install_infra_libs(skip_install_infra_libs): |
| 65 | """Install useful libs from Chromium infra and Chromite. |
| 66 | |
| 67 | infra_libs consists of prpc client from luci and necessary proto files from |
| 68 | Chromite. Suite scheduler relies on those libs to conduct prpc call to |
| 69 | Buildbucket. |
| 70 | |
| 71 | Args: |
| 72 | skip_install_infra_libs: A boolean variable to indicate whether to skip |
| 73 | downloading the infra libs. |
| 74 | """ |
| 75 | |
| 76 | if skip_install_infra_libs: |
| 77 | return |
| 78 | |
| 79 | print('Recreate infra_libs directory if already exists...') |
| 80 | if os.path.exists(file_getter.INFRA_LIBS_PATH): |
| 81 | osutils.RmDir(file_getter.INFRA_LIBS_PATH) |
| 82 | os.mkdir(file_getter.INFRA_LIBS_PATH) |
| 83 | os.mkdir(file_getter.LUCI_PATH) |
| 84 | os.mkdir(file_getter.CHROMITE_PATH) |
| 85 | |
| 86 | print('Fetching prpc client from luci-py lib...') |
| 87 | luci_checkout = [ |
| 88 | '/appengine/components/components/auth/**/*.py', |
| 89 | '/appengine/components/components/datastore_utils/**/*.py', |
| 90 | '/appengine/components/components/prpc/**/*.py', |
| 91 | '/appengine/components/components/*.py', |
| 92 | '!*test.py'] |
| 93 | git.ShallowFetch(file_getter.LUCI_PATH, LUCI_REPO, |
| 94 | sparse_checkout=luci_checkout) |
| 95 | |
| 96 | print('Fetching proto files from Chromite...') |
| 97 | chromite_checkout = [ |
| 98 | '/api/gen/**/*.py', |
| 99 | '/api/__init__.py', |
| 100 | '/third_party/infra_libs/*', |
| 101 | '/third_party/google/*', |
| 102 | '/third_party/__init__.py', |
| 103 | '/__init__.py', |
| 104 | '!*test.py'] |
| 105 | git.ShallowFetch(file_getter.CHROMITE_PATH, CHROMITE_REPO, |
| 106 | sparse_checkout=chromite_checkout) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 107 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 108 | |
Xixuan Wu | 30244f9 | 2019-03-21 09:49:17 -0700 | [diff] [blame] | 109 | def fetch_service_credentials(force_update_cred): |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 110 | """Fetch service credentials from GS bucket. |
| 111 | |
| 112 | Args: |
Xinan Lin | cd6d40b | 2019-08-09 15:53:43 -0700 | [diff] [blame] | 113 | force_update_cred: A boolean variable to indicate whether to force to |
Xixuan Wu | 30244f9 | 2019-03-21 09:49:17 -0700 | [diff] [blame] | 114 | re-download the creds. |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 115 | |
| 116 | Raises: |
| 117 | ValueError: if credentials cannot be fetched due to no valid gs package. |
| 118 | """ |
| 119 | credentials_path = file_getter.CREDENTIALS_PATH |
Xixuan Wu | 30244f9 | 2019-03-21 09:49:17 -0700 | [diff] [blame] | 120 | if force_update_cred and os.path.exists(credentials_path): |
| 121 | shutil.rmtree(credentials_path) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 122 | |
| 123 | if not os.path.exists(credentials_path): |
| 124 | if gs is None: |
| 125 | raise ValueError( |
Xixuan Wu | 30244f9 | 2019-03-21 09:49:17 -0700 | [diff] [blame] | 126 | 'Chromite cannot be imported.\nPlease download the credentials ' |
| 127 | 'manually:\n\tgsutil cp -r %s %s' % ( |
| 128 | CREDS_GS_PATH, os.path.dirname(credentials_path))) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 129 | |
| 130 | os.mkdir(credentials_path) |
| 131 | try: |
| 132 | ctx = gs.GSContext(init_boto=True) |
Xixuan Wu | 30244f9 | 2019-03-21 09:49:17 -0700 | [diff] [blame] | 133 | ctx.Copy(CREDS_GS_PATH + '*', credentials_path) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 134 | except gs.GSCommandError as e: |
Xixuan Wu | 8929a30 | 2017-12-07 18:09:31 -0800 | [diff] [blame] | 135 | print('The GS credentials is not configured properly. ' |
| 136 | 'Please contact ChromeOS Infrastructure' |
| 137 | 'team for more info about it.') |
| 138 | print('Failed to get suite_scheduler service credentials: %r.' |
| 139 | 'Deleting %s' % (str(e), credentials_path)) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 140 | os.rmdir(credentials_path) |
| 141 | |
| 142 | |
| 143 | def _verify(): |
| 144 | """Verify the credentials & third_party lib work.""" |
| 145 | subprocess.check_call(['python', 'runner.py']) |
| 146 | |
| 147 | |
| 148 | def _make_parser(): |
| 149 | """Return parser.""" |
| 150 | parser = argparse.ArgumentParser( |
| 151 | description=__doc__, |
| 152 | formatter_class=argparse.RawDescriptionHelpFormatter) |
| 153 | parser.add_argument( |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 154 | '--update_lib', |
| 155 | action='store_true', |
| 156 | help='Force to reinstall all required third-party libraries.') |
| 157 | parser.add_argument( |
| 158 | '--update_cred', |
| 159 | action='store_true', |
| 160 | help='Force to re-copy the credentials from Google Storage.') |
Xinan Lin | cd6d40b | 2019-08-09 15:53:43 -0700 | [diff] [blame] | 161 | parser.add_argument( |
| 162 | '--skip_infra_libs', |
| 163 | action='store_true', |
| 164 | help='Skip to install infra libs.') |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 165 | return parser |
| 166 | |
| 167 | |
| 168 | if __name__ == '__main__': |
| 169 | setup_parser = _make_parser() |
| 170 | setup_args = setup_parser.parse_args() |
| 171 | _install_third_party_lib(setup_args) |
Xixuan Wu | 30244f9 | 2019-03-21 09:49:17 -0700 | [diff] [blame] | 172 | fetch_service_credentials(setup_args.update_cred) |
Xinan Lin | cd6d40b | 2019-08-09 15:53:43 -0700 | [diff] [blame] | 173 | _install_infra_libs(setup_args.skip_infra_libs) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 174 | _verify() |