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