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