blob: e205c7e97d4742220f206e7305c922880c8942e8 [file] [log] [blame]
Xixuan Wu26d06e02017-09-20 14:50:28 -07001# 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 Wu8929a302017-12-07 18:09:31 -08005"""Module for setup environemtn for local testing & Google App Engine."""
Xixuan Wu26d06e02017-09-20 14:50:28 -07006
7
Xixuan Wu8929a302017-12-07 18:09:31 -08008from __future__ import print_function
9
Xixuan Wu26d06e02017-09-20 14:50:28 -070010import argparse
11import os
Xixuan Wu30244f92019-03-21 09:49:17 -070012import shutil
Xixuan Wu26d06e02017-09-20 14:50:28 -070013import subprocess
14
15import file_getter
16
17import local_import # pylint: disable=unused-import
18try:
19 from chromite.lib import gs # pylint: disable=g-import-not-at-top
20except ImportError:
Xixuan Wu8929a302017-12-07 18:09:31 -080021 print('Cannot import chromite')
Xixuan Wu26d06e02017-09-20 14:50:28 -070022 gs = None
23
24
25# The credentials folder for suite-scheduler service.
26CREDS_GS_PATH = 'gs://suite-scheduler.google.com.a.appspot.com/credentials/'
27
28
29def _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 Wu8929a302017-12-07 18:09:31 -080044 print('Creating lib path: %s' % third_party_lib_path)
Xixuan Wu26d06e02017-09-20 14:50:28 -070045 os.mkdir(third_party_lib_path)
Xixuan Wu8929a302017-12-07 18:09:31 -080046 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 Wu26d06e02017-09-20 14:50:28 -070056
57
Xixuan Wu30244f92019-03-21 09:49:17 -070058def fetch_service_credentials(force_update_cred):
Xixuan Wu26d06e02017-09-20 14:50:28 -070059 """Fetch service credentials from GS bucket.
60
61 Args:
Xixuan Wu30244f92019-03-21 09:49:17 -070062 force_update_cred: A boolean virable to indicate whether to force to
63 re-download the creds.
Xixuan Wu26d06e02017-09-20 14:50:28 -070064
65 Raises:
66 ValueError: if credentials cannot be fetched due to no valid gs package.
67 """
68 credentials_path = file_getter.CREDENTIALS_PATH
Xixuan Wu30244f92019-03-21 09:49:17 -070069 if force_update_cred and os.path.exists(credentials_path):
70 shutil.rmtree(credentials_path)
Xixuan Wu26d06e02017-09-20 14:50:28 -070071
72 if not os.path.exists(credentials_path):
73 if gs is None:
74 raise ValueError(
Xixuan Wu30244f92019-03-21 09:49:17 -070075 '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 Wu26d06e02017-09-20 14:50:28 -070078
79 os.mkdir(credentials_path)
80 try:
81 ctx = gs.GSContext(init_boto=True)
Xixuan Wu30244f92019-03-21 09:49:17 -070082 ctx.Copy(CREDS_GS_PATH + '*', credentials_path)
Xixuan Wu26d06e02017-09-20 14:50:28 -070083 except gs.GSCommandError as e:
Xixuan Wu8929a302017-12-07 18:09:31 -080084 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 Wu26d06e02017-09-20 14:50:28 -070089 os.rmdir(credentials_path)
90
91
92def _verify():
93 """Verify the credentials & third_party lib work."""
94 subprocess.check_call(['python', 'runner.py'])
95
96
97def _make_parser():
98 """Return parser."""
99 parser = argparse.ArgumentParser(
100 description=__doc__,
101 formatter_class=argparse.RawDescriptionHelpFormatter)
102 parser.add_argument(
Xixuan Wu26d06e02017-09-20 14:50:28 -0700103 '--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
113if __name__ == '__main__':
114 setup_parser = _make_parser()
115 setup_args = setup_parser.parse_args()
116 _install_third_party_lib(setup_args)
Xixuan Wu30244f92019-03-21 09:49:17 -0700117 fetch_service_credentials(setup_args.update_cred)
Xixuan Wu26d06e02017-09-20 14:50:28 -0700118 _verify()