blob: 1b66ad07d3ccff0bd9f1427132d428b1608de46c [file] [log] [blame]
xixuanbea010f2017-03-27 10:10:19 -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 containing the constants to be reused throughout suite_scheduler."""
6
Xixuan Wud55ac6e2019-03-14 10:56:39 -07007import collections
xixuanbea010f2017-03-27 10:10:19 -07008import logging
9import os
10
Xixuan Wu5d6063e2017-09-05 16:15:07 -070011from google.appengine.api import app_identity
12
xixuanbea010f2017-03-27 10:10:19 -070013# Namedtuple for storing rest client info.
Xixuan Wud55ac6e2019-03-14 10:56:39 -070014RestClientInfo = collections.namedtuple(
xixuanbea010f2017-03-27 10:10:19 -070015 'RestClientInfo', 'scopes, service_name, service_version')
16
xixuanbea010f2017-03-27 10:10:19 -070017# Constants for detecting the running environment.
18_RUNNING_ENV = 'SERVER_SOFTWARE'
19_ENV_DEVELOPMENT_STR = 'Development'
20_ENV_APP_ENGINE_STR = 'Google App Engine/'
Xixuan Wu5d6063e2017-09-05 16:15:07 -070021
22
Xixuan Wud55ac6e2019-03-14 10:56:39 -070023class StorageBucket(object):
24 """Constants refer to Google storage bucket."""
25
26 PROD_SUITE_SCHEDULER = 'suite-scheduler.google.com.a.appspot.com'
27
28
Xixuan Wu27a61f82017-09-14 11:42:37 -070029class AppID(object):
30 """Constants refer to app id."""
31
32 PROD_APP = 'suite-scheduler'
33 STAGING_APP = 'suite-scheduler-staging'
34
35
Xinan Lincd6d40b2019-08-09 15:53:43 -070036class Buildbucket(object):
37 """Constants used when making buildbucket calls."""
38
39 HOST = 'cr-buildbucket.appspot.com'
40 PROJECT = 'chromeos'
41 BUCKET = 'testplatform'
42 PROD_BUILDER = 'cros_test_platform'
43 STAGING_BUILDER = 'cros_test_platform-dev'
44
45
Xixuan Wu27a61f82017-09-14 11:42:37 -070046class CalendarID(object):
47 """Constants refer to calendar id."""
48
49 PROD_NEW_BUILD = (
50 'google.com_8n9gndgp0o642d7ta7dakbiiso@group.calendar.google.com')
51 PROD_NIGHTLY = (
52 'google.com_e8p3i4pk80lt6snfb57fdgff3g@group.calendar.google.com')
53 PROD_WEEKLY = (
54 'google.com_9es5a7m1r9til9uf5qc8mpr6p4@group.calendar.google.com')
55 STAGING_NEW_BUILD = (
56 'google.com_pjlsd9371ic158vdgcacspu0m4@group.calendar.google.com')
57 STAGING_NIGHTLY = (
58 'google.com_lu3v13kr4if93l9embsvb5llj4@group.calendar.google.com')
59 STAGING_WEEKLY = (
60 'google.com_9l29i5ncfk24e5aktjr6gaerp8@group.calendar.google.com')
61
62
Xixuan Wu5d6063e2017-09-05 16:15:07 -070063class RestClient(object):
64 """Constants related to rest clients to google service."""
65
66 # client info for connecting to android build API.
67 ANDROID_BUILD_CLIENT = RestClientInfo._make(
68 ['https://www.googleapis.com/auth/androidbuild.internal',
69 'androidbuildinternal',
70 'v2beta1'])
71
72 # client info for connecting to google storage API.
73 STORAGE_CLIENT = RestClientInfo._make(
74 ['https://www.googleapis.com/auth/devstorage.full_control',
75 'storage',
76 'v1'])
77
78 # client info for connecting to google calendar API.
79 CALENDAR_CLIENT = RestClientInfo._make(
80 ['https://www.googleapis.com/auth/calendar',
81 'calendar',
82 'v3'])
83
84 SWARMING_CLIENT = RestClientInfo._make(
85 ['https://www.googleapis.com/auth/userinfo.email',
86 'swarming',
87 'v1'])
88
Xixuan Wu6f117e92017-10-27 10:51:58 -070089 STACKDRIVER_CLIENT = RestClientInfo._make(
90 ['https://www.googleapis.com/auth/logging.read',
91 'logging',
92 'v2'])
93
Xixuan Wu7d142a92019-04-26 12:03:02 -070094 BIGQUERY_CLIENT = RestClientInfo._make(
95 ['https://www.googleapis.com/auth/bigquery',
96 'bigquery',
97 'v2'])
98
Xixuan Wu5d6063e2017-09-05 16:15:07 -070099
100class RunningEnv(object):
101 """Constants related to app engine running environment."""
102
103 # Running this GAE project locally with python.
104 ENV_STANDALONE = 0
105
106 # Running this GAE project with local development server.
107 ENV_DEVELOPMENT_SERVER = 1
108
109 # Running this GAE project in app-engine production.
110 ENV_PROD = 2
111
112
113class Priorities(object):
114 """Constants related to task priority."""
115
116 # weekly task's priority
117 WEEKLY = 10
118
119 # daily task's priority
120 DAILY = 20
121
122 # postbuild task's priority
123 POSTBUILD = 30
124
125 # the default task priority
126 DEFAULT = 40
127
128
129def environment():
130 """Return proper environment settings."""
131 if os.getenv(_RUNNING_ENV, '').startswith(_ENV_DEVELOPMENT_STR):
132 return RunningEnv.ENV_DEVELOPMENT_SERVER
133 elif os.getenv(_RUNNING_ENV, '').startswith(_ENV_APP_ENGINE_STR):
134 return RunningEnv.ENV_PROD
135 else:
136 return RunningEnv.ENV_STANDALONE
137
138
Xixuan Wu8c173c32017-11-06 11:23:06 -0800139def application_id(parse=True):
Xixuan Wu5d6063e2017-09-05 16:15:07 -0700140 """Get current running application id.
141
Xixuan Wu8c173c32017-11-06 11:23:06 -0800142 Args:
143 parse: whether to parse prefix of application id.
144
Xixuan Wu5d6063e2017-09-05 16:15:07 -0700145 Returns:
Xixuan Wu8c173c32017-11-06 11:23:06 -0800146 If the id == 'None', return None;
147 If it's a Google internal GAE whose id format is 'google.com:<name>' and
148 parse is specified as True, return <name>;
149 Otherwise, return the full id.
Xixuan Wu5d6063e2017-09-05 16:15:07 -0700150 """
151 app_id = app_identity.get_application_id()
152 logging.info('app_id: %s', app_id)
Xixuan Wu8c173c32017-11-06 11:23:06 -0800153 if app_id == 'None':
154 return None
155
156 if not parse:
157 return app_id
158
Xixuan Wu5d6063e2017-09-05 16:15:07 -0700159 if app_id is not None and len(app_id.split(':')) > 1:
160 return app_id.split(':')[1]
161 else:
162 return app_id
Xixuan Wu69a5e782017-11-06 14:32:05 -0800163
164
165def server_name():
166 """Return server name.
167
168 Returns:
169 If this instance is running locally, return 'localhost:xxxx'.
170 If this instance is running on GAE, return '***.googleplex.com' or
171 '***.appspot.com'.
172 """
173 return app_identity.get_default_version_hostname()