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. |
xixuan | bea010f | 2017-03-27 10:10:19 -0700 | [diff] [blame] | 4 | """Module for fetching files used by suite scheduler.""" |
| 5 | |
Brigit Rossbach | eb611ac | 2020-09-24 14:55:24 -0600 | [diff] [blame] | 6 | import base64 |
Brigit Rossbach | 50d086f | 2020-09-17 13:29:49 -0600 | [diff] [blame] | 7 | import logging |
xixuan | bea010f | 2017-03-27 10:10:19 -0700 | [diff] [blame] | 8 | import os |
Dhanya Ganesh | 2b29ad1 | 2020-09-22 22:18:58 +0000 | [diff] [blame] | 9 | import subprocess |
Brigit Rossbach | eb611ac | 2020-09-24 14:55:24 -0600 | [diff] [blame] | 10 | import urllib2 |
xixuan | bea010f | 2017-03-27 10:10:19 -0700 | [diff] [blame] | 11 | |
xixuan | bea010f | 2017-03-27 10:10:19 -0700 | [diff] [blame] | 12 | # The path to save credentials. |
| 13 | CREDENTIALS_PATH = os.path.join(os.path.dirname(__file__), 'credentials') |
| 14 | |
xixuan | bea010f | 2017-03-27 10:10:19 -0700 | [diff] [blame] | 15 | # The path to save configs. |
| 16 | CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'configs') |
| 17 | |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 18 | # The suite scheduler config file for testing. |
Brigit Rossbach | 50d086f | 2020-09-17 13:29:49 -0600 | [diff] [blame] | 19 | TEST_SUITE_SCHEDULER_CONFIG_FILE = os.path.join(CONFIG_PATH, |
| 20 | 'fake_suite_scheduler.ini') |
xixuan | bea010f | 2017-03-27 10:10:19 -0700 | [diff] [blame] | 21 | |
Brigit Rossbach | 50d086f | 2020-09-17 13:29:49 -0600 | [diff] [blame] | 22 | # Test lab config file |
| 23 | TEST_LAB_CONFIG_FILE = os.path.join(CONFIG_PATH, 'fake_lab_config.ini') |
| 24 | |
Jack Neus | 8f0edb4 | 2022-03-17 20:21:39 +0000 | [diff] [blame] | 25 | # Test rubik boards config file |
| 26 | TEST_RUBIK_CONFIG_FILE = os.path.join(CONFIG_PATH, 'fake_rubik_config.ini') |
| 27 | |
Brigit Rossbach | 50d086f | 2020-09-17 13:29:49 -0600 | [diff] [blame] | 28 | # Suite scheduler config repo |
| 29 | NEW_SUITE_SCHEDULER_CONFIG_FILE = None |
| 30 | NEW_LAB_CONFIG_FILE = None |
Jack Neus | 8f0edb4 | 2022-03-17 20:21:39 +0000 | [diff] [blame] | 31 | NEW_RUBIK_CONFIG_FILE = None |
Brigit Rossbach | 50d086f | 2020-09-17 13:29:49 -0600 | [diff] [blame] | 32 | |
Dhanya Ganesh | 643a49e | 2020-09-25 18:14:22 +0000 | [diff] [blame] | 33 | def _write_config(config_name): |
| 34 | tot_response = urllib2.urlopen( |
Brigit Rossbach | 3049724 | 2021-02-17 10:57:44 -0700 | [diff] [blame] | 35 | 'https://chromium.googlesource.com/chromiumos/infra/suite_scheduler/+/refs/heads/main/generated_configs/{}?format=text'.format(config_name) |
Dhanya Ganesh | 643a49e | 2020-09-25 18:14:22 +0000 | [diff] [blame] | 36 | ).read() |
Dhanya Ganesh | 626da11 | 2020-09-25 18:42:20 +0000 | [diff] [blame] | 37 | config_text = base64.b64decode(tot_response) |
Dhanya Ganesh | 643a49e | 2020-09-25 18:14:22 +0000 | [diff] [blame] | 38 | logging.info('Writing %s as: %s', config_name, config_text) |
Dhanya Ganesh | 643a49e | 2020-09-25 18:14:22 +0000 | [diff] [blame] | 39 | filename = '/tmp/{}'.format(config_name) |
| 40 | with open(filename, 'w') as f: |
| 41 | f.write(config_text) |
| 42 | return filename |
Brigit Rossbach | 50d086f | 2020-09-17 13:29:49 -0600 | [diff] [blame] | 43 | |
| 44 | def clone_config(): |
Brigit Rossbach | eb611ac | 2020-09-24 14:55:24 -0600 | [diff] [blame] | 45 | """Function to clone the config for suite scheduler from the current head.""" |
Brigit Rossbach | f951d8d | 2020-09-22 11:22:01 -0600 | [diff] [blame] | 46 | global NEW_SUITE_SCHEDULER_CONFIG_FILE |
| 47 | global NEW_LAB_CONFIG_FILE |
Jack Neus | 8f0edb4 | 2022-03-17 20:21:39 +0000 | [diff] [blame] | 48 | global NEW_RUBIK_CONFIG_FILE |
Brigit Rossbach | eb611ac | 2020-09-24 14:55:24 -0600 | [diff] [blame] | 49 | logging.info('Fetching config from suite scheduler repo.') |
Brigit Rossbach | bdf2532 | 2020-10-21 11:54:25 -0600 | [diff] [blame] | 50 | try: |
| 51 | NEW_SUITE_SCHEDULER_CONFIG_FILE = _write_config('suite_scheduler.ini') |
| 52 | NEW_LAB_CONFIG_FILE = _write_config('lab_config.ini') |
Jack Neus | 8f0edb4 | 2022-03-17 20:21:39 +0000 | [diff] [blame] | 53 | NEW_RUBIK_CONFIG_FILE = _write_config('rubik_config.ini') |
Brigit Rossbach | bdf2532 | 2020-10-21 11:54:25 -0600 | [diff] [blame] | 54 | except Exception as e: |
| 55 | logging.error(str(e)) |
Brigit Rossbach | 50d086f | 2020-09-17 13:29:49 -0600 | [diff] [blame] | 56 | |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 57 | # Service account secret json file used for staging project. |
| 58 | STAGING_CLIENT_SECRETS_FILE = os.path.join( |
Xixuan Wu | 865fa28 | 2017-09-05 15:23:19 -0700 | [diff] [blame] | 59 | CREDENTIALS_PATH, |
| 60 | 'suite-scheduler-staging_client_secret_service_account.json') |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 61 | |
| 62 | # Service account secret json file used for prod project. |
| 63 | PROD_CLIENT_SECRETS_FILE = os.path.join( |
Prathmesh Prabhu | 8dff3e2 | 2020-06-23 13:49:45 -0700 | [diff] [blame] | 64 | CREDENTIALS_PATH, 'suite-scheduler_client_secret_service_account.json') |