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 | bea010f | 2017-03-27 10:10:19 -0700 | [diff] [blame] | 7 | from collections import namedtuple |
| 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. |
| 14 | RestClientInfo = namedtuple( |
| 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 | 27a61f8 | 2017-09-14 11:42:37 -0700 | [diff] [blame] | 23 | class AppID(object): |
| 24 | """Constants refer to app id.""" |
| 25 | |
| 26 | PROD_APP = 'suite-scheduler' |
| 27 | STAGING_APP = 'suite-scheduler-staging' |
| 28 | |
| 29 | |
| 30 | class CalendarID(object): |
| 31 | """Constants refer to calendar id.""" |
| 32 | |
| 33 | PROD_NEW_BUILD = ( |
| 34 | 'google.com_8n9gndgp0o642d7ta7dakbiiso@group.calendar.google.com') |
| 35 | PROD_NIGHTLY = ( |
| 36 | 'google.com_e8p3i4pk80lt6snfb57fdgff3g@group.calendar.google.com') |
| 37 | PROD_WEEKLY = ( |
| 38 | 'google.com_9es5a7m1r9til9uf5qc8mpr6p4@group.calendar.google.com') |
| 39 | STAGING_NEW_BUILD = ( |
| 40 | 'google.com_pjlsd9371ic158vdgcacspu0m4@group.calendar.google.com') |
| 41 | STAGING_NIGHTLY = ( |
| 42 | 'google.com_lu3v13kr4if93l9embsvb5llj4@group.calendar.google.com') |
| 43 | STAGING_WEEKLY = ( |
| 44 | 'google.com_9l29i5ncfk24e5aktjr6gaerp8@group.calendar.google.com') |
| 45 | |
| 46 | |
Xixuan Wu | 5d6063e | 2017-09-05 16:15:07 -0700 | [diff] [blame] | 47 | class RestClient(object): |
| 48 | """Constants related to rest clients to google service.""" |
| 49 | |
| 50 | # client info for connecting to android build API. |
| 51 | ANDROID_BUILD_CLIENT = RestClientInfo._make( |
| 52 | ['https://www.googleapis.com/auth/androidbuild.internal', |
| 53 | 'androidbuildinternal', |
| 54 | 'v2beta1']) |
| 55 | |
| 56 | # client info for connecting to google storage API. |
| 57 | STORAGE_CLIENT = RestClientInfo._make( |
| 58 | ['https://www.googleapis.com/auth/devstorage.full_control', |
| 59 | 'storage', |
| 60 | 'v1']) |
| 61 | |
| 62 | # client info for connecting to google calendar API. |
| 63 | CALENDAR_CLIENT = RestClientInfo._make( |
| 64 | ['https://www.googleapis.com/auth/calendar', |
| 65 | 'calendar', |
| 66 | 'v3']) |
| 67 | |
| 68 | SWARMING_CLIENT = RestClientInfo._make( |
| 69 | ['https://www.googleapis.com/auth/userinfo.email', |
| 70 | 'swarming', |
| 71 | 'v1']) |
| 72 | |
| 73 | |
| 74 | class RunningEnv(object): |
| 75 | """Constants related to app engine running environment.""" |
| 76 | |
| 77 | # Running this GAE project locally with python. |
| 78 | ENV_STANDALONE = 0 |
| 79 | |
| 80 | # Running this GAE project with local development server. |
| 81 | ENV_DEVELOPMENT_SERVER = 1 |
| 82 | |
| 83 | # Running this GAE project in app-engine production. |
| 84 | ENV_PROD = 2 |
| 85 | |
| 86 | |
| 87 | class Priorities(object): |
| 88 | """Constants related to task priority.""" |
| 89 | |
| 90 | # weekly task's priority |
| 91 | WEEKLY = 10 |
| 92 | |
| 93 | # daily task's priority |
| 94 | DAILY = 20 |
| 95 | |
| 96 | # postbuild task's priority |
| 97 | POSTBUILD = 30 |
| 98 | |
| 99 | # the default task priority |
| 100 | DEFAULT = 40 |
| 101 | |
| 102 | |
| 103 | def environment(): |
| 104 | """Return proper environment settings.""" |
| 105 | if os.getenv(_RUNNING_ENV, '').startswith(_ENV_DEVELOPMENT_STR): |
| 106 | return RunningEnv.ENV_DEVELOPMENT_SERVER |
| 107 | elif os.getenv(_RUNNING_ENV, '').startswith(_ENV_APP_ENGINE_STR): |
| 108 | return RunningEnv.ENV_PROD |
| 109 | else: |
| 110 | return RunningEnv.ENV_STANDALONE |
| 111 | |
| 112 | |
| 113 | def application_id(): |
| 114 | """Get current running application id. |
| 115 | |
| 116 | Returns: |
| 117 | If it's a Google internal GAE whose id format is 'google.com:<name>', |
| 118 | return <name>; Otherwise, return the full id. |
| 119 | """ |
| 120 | app_id = app_identity.get_application_id() |
| 121 | logging.info('app_id: %s', app_id) |
| 122 | if app_id is not None and len(app_id.split(':')) > 1: |
| 123 | return app_id.split(':')[1] |
| 124 | else: |
| 125 | return app_id |