blob: 1e7dcaf0932382b19b2fb65c0f5163643325574f [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
12import subprocess
13
14import file_getter
15
16import local_import # pylint: disable=unused-import
17try:
18 from chromite.lib import gs # pylint: disable=g-import-not-at-top
19except ImportError:
Xixuan Wu8929a302017-12-07 18:09:31 -080020 print('Cannot import chromite')
Xixuan Wu26d06e02017-09-20 14:50:28 -070021 gs = None
22
23
24# The credentials folder for suite-scheduler service.
25CREDS_GS_PATH = 'gs://suite-scheduler.google.com.a.appspot.com/credentials/'
26
27
28def _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 Wu8929a302017-12-07 18:09:31 -080043 print('Creating lib path: %s' % third_party_lib_path)
Xixuan Wu26d06e02017-09-20 14:50:28 -070044 os.mkdir(third_party_lib_path)
Xixuan Wu8929a302017-12-07 18:09:31 -080045 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 Wu26d06e02017-09-20 14:50:28 -070055
56
57def _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 Wu8929a302017-12-07 18:09:31 -080081 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 Wu26d06e02017-09-20 14:50:28 -070086 os.rmdir(credentials_path)
87
88
89def _verify():
90 """Verify the credentials & third_party lib work."""
91 subprocess.check_call(['python', 'runner.py'])
92
93
94def _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
114if __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()