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: |
| 19 | from chromite.lib import gs # pylint: disable=g-import-not-at-top |
| 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 |
| 23 | |
| 24 | |
| 25 | # The credentials folder for suite-scheduler service. |
| 26 | CREDS_GS_PATH = 'gs://suite-scheduler.google.com.a.appspot.com/credentials/' |
| 27 | |
| 28 | |
| 29 | def _install_third_party_lib(input_args): |
| 30 | """Install useful third-party lib. |
| 31 | |
| 32 | The libraries are specified in requirement.txt. They're used in both local |
| 33 | development environment and Google App Engine, which means they will be |
| 34 | uploaded to GAE. |
| 35 | |
| 36 | Args: |
| 37 | input_args: the arguments passed in by users. |
| 38 | """ |
| 39 | third_party_lib_path = file_getter.THIRD_PARTY_LIB_PATH |
| 40 | if input_args.update_lib and os.path.exists(third_party_lib_path): |
| 41 | os.rmdir(third_party_lib_path) |
| 42 | |
| 43 | if not os.path.exists(third_party_lib_path): |
Xixuan Wu | 8929a30 | 2017-12-07 18:09:31 -0800 | [diff] [blame] | 44 | print('Creating lib path: %s' % third_party_lib_path) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 45 | os.mkdir(third_party_lib_path) |
Xixuan Wu | 8929a30 | 2017-12-07 18:09:31 -0800 | [diff] [blame] | 46 | print('Installing packages') |
| 47 | cmd = ['pip', 'install', '-t', 'lib', '-r', 'requirements.txt'] |
| 48 | try: |
| 49 | subprocess.check_call(cmd) |
| 50 | except subprocess.CalledProcessError: |
| 51 | # TODO(crbug.com/792690): this is a workround of fixing installing |
| 52 | # third-party packages on different versions of Ubuntu/Debian. |
| 53 | print("Failed to run '%s'; trying a different command." % ' '.join(cmd)) |
| 54 | subprocess.check_call(['pip', 'install', '-t', 'lib', '--system', |
| 55 | '-r', 'requirements.txt']) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 56 | |
| 57 | |
Xixuan Wu | 30244f9 | 2019-03-21 09:49:17 -0700 | [diff] [blame] | 58 | def fetch_service_credentials(force_update_cred): |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 59 | """Fetch service credentials from GS bucket. |
| 60 | |
| 61 | Args: |
Xixuan Wu | 30244f9 | 2019-03-21 09:49:17 -0700 | [diff] [blame] | 62 | force_update_cred: A boolean virable to indicate whether to force to |
| 63 | re-download the creds. |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 64 | |
| 65 | Raises: |
| 66 | ValueError: if credentials cannot be fetched due to no valid gs package. |
| 67 | """ |
| 68 | credentials_path = file_getter.CREDENTIALS_PATH |
Xixuan Wu | 30244f9 | 2019-03-21 09:49:17 -0700 | [diff] [blame] | 69 | if force_update_cred and os.path.exists(credentials_path): |
| 70 | shutil.rmtree(credentials_path) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 71 | |
| 72 | if not os.path.exists(credentials_path): |
| 73 | if gs is None: |
| 74 | raise ValueError( |
Xixuan Wu | 30244f9 | 2019-03-21 09:49:17 -0700 | [diff] [blame] | 75 | 'Chromite cannot be imported.\nPlease download the credentials ' |
| 76 | 'manually:\n\tgsutil cp -r %s %s' % ( |
| 77 | CREDS_GS_PATH, os.path.dirname(credentials_path))) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 78 | |
| 79 | os.mkdir(credentials_path) |
| 80 | try: |
| 81 | ctx = gs.GSContext(init_boto=True) |
Xixuan Wu | 30244f9 | 2019-03-21 09:49:17 -0700 | [diff] [blame] | 82 | ctx.Copy(CREDS_GS_PATH + '*', credentials_path) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 83 | except gs.GSCommandError as e: |
Xixuan Wu | 8929a30 | 2017-12-07 18:09:31 -0800 | [diff] [blame] | 84 | print('The GS credentials is not configured properly. ' |
| 85 | 'Please contact ChromeOS Infrastructure' |
| 86 | 'team for more info about it.') |
| 87 | print('Failed to get suite_scheduler service credentials: %r.' |
| 88 | 'Deleting %s' % (str(e), credentials_path)) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 89 | os.rmdir(credentials_path) |
| 90 | |
| 91 | |
| 92 | def _verify(): |
| 93 | """Verify the credentials & third_party lib work.""" |
| 94 | subprocess.check_call(['python', 'runner.py']) |
| 95 | |
| 96 | |
| 97 | def _make_parser(): |
| 98 | """Return parser.""" |
| 99 | parser = argparse.ArgumentParser( |
| 100 | description=__doc__, |
| 101 | formatter_class=argparse.RawDescriptionHelpFormatter) |
| 102 | parser.add_argument( |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 103 | '--update_lib', |
| 104 | action='store_true', |
| 105 | help='Force to reinstall all required third-party libraries.') |
| 106 | parser.add_argument( |
| 107 | '--update_cred', |
| 108 | action='store_true', |
| 109 | help='Force to re-copy the credentials from Google Storage.') |
| 110 | return parser |
| 111 | |
| 112 | |
| 113 | if __name__ == '__main__': |
| 114 | setup_parser = _make_parser() |
| 115 | setup_args = setup_parser.parse_args() |
| 116 | _install_third_party_lib(setup_args) |
Xixuan Wu | 30244f9 | 2019-03-21 09:49:17 -0700 | [diff] [blame] | 117 | fetch_service_credentials(setup_args.update_cred) |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 118 | _verify() |