xixuan | bea010f | 2017-03-27 10:10:19 -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 | |
| 5 | """Module containing the constants to be reused throughout suite_scheduler.""" |
| 6 | |
Xixuan Wu | d55ac6e | 2019-03-14 10:56:39 -0700 | [diff] [blame] | 7 | import collections |
xixuan | bea010f | 2017-03-27 10:10:19 -0700 | [diff] [blame] | 8 | import logging |
| 9 | import os |
| 10 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 11 | from google.appengine.api import app_identity |
| 12 | |
xixuan | bea010f | 2017-03-27 10:10:19 -0700 | [diff] [blame] | 13 | # Namedtuple for storing rest client info. |
Xixuan Wu | d55ac6e | 2019-03-14 10:56:39 -0700 | [diff] [blame] | 14 | RestClientInfo = collections.namedtuple( |
xixuan | bea010f | 2017-03-27 10:10:19 -0700 | [diff] [blame] | 15 | 'RestClientInfo', 'scopes, service_name, service_version') |
| 16 | |
xixuan | bea010f | 2017-03-27 10:10:19 -0700 | [diff] [blame] | 17 | # 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 Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 21 | |
| 22 | |
Xixuan Wu | d55ac6e | 2019-03-14 10:56:39 -0700 | [diff] [blame] | 23 | class StorageBucket(object): |
| 24 | """Constants refer to Google storage bucket.""" |
| 25 | |
| 26 | PROD_SUITE_SCHEDULER = 'suite-scheduler.google.com.a.appspot.com' |
| 27 | |
| 28 | |
Xixuan Wu | 27a61f8 | 2017-09-14 11:42:37 -0700 | [diff] [blame] | 29 | class AppID(object): |
| 30 | """Constants refer to app id.""" |
| 31 | |
| 32 | PROD_APP = 'suite-scheduler' |
| 33 | STAGING_APP = 'suite-scheduler-staging' |
| 34 | |
| 35 | |
Xinan Lin | cd6d40b | 2019-08-09 15:53:43 -0700 | [diff] [blame^] | 36 | class 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 Wu | 27a61f8 | 2017-09-14 11:42:37 -0700 | [diff] [blame] | 46 | class 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 Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 63 | class 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 Wu | 6f117e9 | 2017-10-27 10:51:58 -0700 | [diff] [blame] | 89 | STACKDRIVER_CLIENT = RestClientInfo._make( |
| 90 | ['https://www.googleapis.com/auth/logging.read', |
| 91 | 'logging', |
| 92 | 'v2']) |
| 93 | |
Xixuan Wu | 7d142a9 | 2019-04-26 12:03:02 -0700 | [diff] [blame] | 94 | BIGQUERY_CLIENT = RestClientInfo._make( |
| 95 | ['https://www.googleapis.com/auth/bigquery', |
| 96 | 'bigquery', |
| 97 | 'v2']) |
| 98 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 99 | |
| 100 | class 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 | |
| 113 | class 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 | |
| 129 | def 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 Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 139 | def application_id(parse=True): |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 140 | """Get current running application id. |
| 141 | |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 142 | Args: |
| 143 | parse: whether to parse prefix of application id. |
| 144 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 145 | Returns: |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 146 | 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 Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 150 | """ |
| 151 | app_id = app_identity.get_application_id() |
| 152 | logging.info('app_id: %s', app_id) |
Xixuan Wu | 8c173c3 | 2017-11-06 11:23:06 -0800 | [diff] [blame] | 153 | if app_id == 'None': |
| 154 | return None |
| 155 | |
| 156 | if not parse: |
| 157 | return app_id |
| 158 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 159 | 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 Wu | 69a5e78 | 2017-11-06 14:32:05 -0800 | [diff] [blame] | 163 | |
| 164 | |
| 165 | def 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() |