Ben Segall | 530d86d | 2023-05-29 16:11:00 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2023 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | """This script manages the counter for how many times developers should |
| 6 | be notified before uploading reclient metrics.""" |
| 7 | |
| 8 | import json |
| 9 | import os |
| 10 | import subprocess |
| 11 | import sys |
| 12 | |
| 13 | THIS_DIR = os.path.dirname(__file__) |
| 14 | CONFIG = os.path.join(THIS_DIR, 'reclient_metrics.cfg') |
| 15 | VERSION = 1 |
| 16 | |
| 17 | |
| 18 | def default_config(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 19 | return { |
| 20 | 'is-googler': is_googler(), |
| 21 | 'countdown': 10, |
| 22 | 'version': VERSION, |
| 23 | } |
Ben Segall | 530d86d | 2023-05-29 16:11:00 +0000 | [diff] [blame] | 24 | |
| 25 | |
| 26 | def load_config(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 27 | config = None |
| 28 | try: |
| 29 | with open(CONFIG) as f: |
| 30 | raw_config = json.load(f) |
| 31 | if raw_config['version'] == VERSION: |
| 32 | raw_config['countdown'] = max(0, raw_config['countdown'] - 1) |
| 33 | config = raw_config |
| 34 | except Exception: |
| 35 | pass |
| 36 | if not config: |
| 37 | config = default_config() |
| 38 | save_config(config) |
| 39 | return config |
Ben Segall | 530d86d | 2023-05-29 16:11:00 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def save_config(config): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 43 | with open(CONFIG, 'w') as f: |
| 44 | json.dump(config, f) |
Ben Segall | 530d86d | 2023-05-29 16:11:00 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def show_message(config, ninja_out): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 48 | print(""" |
Ben Segall | 530d86d | 2023-05-29 16:11:00 +0000 | [diff] [blame] | 49 | Your reclient metrics will be uploaded to the chromium build metrics database. The uploaded metrics will be used to analyze user side build performance. |
| 50 | |
| 51 | We upload the contents of {ninja_out_abs}. |
| 52 | This contains |
| 53 | * Flags passed to reproxy |
| 54 | * Auth related flags are filtered out by reproxy |
| 55 | * Start and end time of build tasks |
| 56 | * Aggregated durations and counts of events during remote build actions |
| 57 | * OS (e.g. Win, Mac or Linux) |
| 58 | * Number of cpu cores and the amount of RAM of the building machine |
| 59 | |
| 60 | Uploading reclient metrics will be started after you run autoninja another {config_count} time(s). |
| 61 | |
| 62 | If you don't want to upload reclient metrics, please run following command. |
| 63 | $ python3 {file_path} opt-out |
| 64 | |
| 65 | If you want to allow upload reclient metrics from next autoninja run, please run the |
| 66 | following command. |
| 67 | $ python3 {file_path} opt-in |
| 68 | |
| 69 | If you have questions about this, please send an email to foundry-x@google.com |
| 70 | |
| 71 | You can find a more detailed explanation in |
| 72 | {metrics_readme_path} |
| 73 | or |
| 74 | https://chromium.googlesource.com/chromium/tools/depot_tools/+/main/reclient_metrics.README.md |
| 75 | """.format( |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 76 | ninja_out_abs=os.path.abspath( |
| 77 | os.path.join(ninja_out, ".reproxy_tmp", "logs", "rbe_metrics.txt")), |
| 78 | config_count=config.get("countdown", 0), |
| 79 | file_path=__file__, |
| 80 | metrics_readme_path=os.path.abspath( |
| 81 | os.path.join(THIS_DIR, "reclient_metrics.README.md")), |
| 82 | )) |
Ben Segall | 530d86d | 2023-05-29 16:11:00 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def is_googler(config=None): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 86 | """Check whether this user is Googler or not.""" |
| 87 | if config is not None and 'is-googler' in config: |
| 88 | return config['is-googler'] |
| 89 | # Use cipd auth-info to check for googler status as |
| 90 | # downloading rewrapper configs already requires cipd to be logged in |
| 91 | p = subprocess.run('cipd auth-info', |
| 92 | stdout=subprocess.PIPE, |
| 93 | stderr=subprocess.PIPE, |
| 94 | text=True, |
| 95 | shell=True) |
| 96 | if p.returncode != 0: |
| 97 | return False |
| 98 | lines = p.stdout.splitlines() |
| 99 | if len(lines) == 0: |
| 100 | return False |
| 101 | l = lines[0] |
| 102 | # |l| will be like 'Logged in as <user>@google.com.' for googlers. |
| 103 | return l.startswith('Logged in as ') and l.endswith('@google.com.') |
Ben Segall | 530d86d | 2023-05-29 16:11:00 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | def check_status(ninja_out): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 107 | """Checks metrics collections status and shows notice to user if needed. |
Ben Segall | 530d86d | 2023-05-29 16:11:00 +0000 | [diff] [blame] | 108 | |
| 109 | Returns True if metrics should be collected.""" |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 110 | config = load_config() |
| 111 | if not is_googler(config): |
| 112 | return False |
| 113 | if 'opt-in' in config: |
| 114 | return config['opt-in'] |
| 115 | if config.get("countdown", 0) > 0: |
| 116 | show_message(config, ninja_out) |
| 117 | return False |
| 118 | return True |
Ben Segall | 530d86d | 2023-05-29 16:11:00 +0000 | [diff] [blame] | 119 | |
| 120 | |
| 121 | def main(argv): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 122 | cfg = load_config() |
Ben Segall | 530d86d | 2023-05-29 16:11:00 +0000 | [diff] [blame] | 123 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 124 | if not is_googler(cfg): |
| 125 | save_config(cfg) |
| 126 | return 0 |
Ben Segall | 530d86d | 2023-05-29 16:11:00 +0000 | [diff] [blame] | 127 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 128 | if len(argv) == 2 and argv[1] == 'opt-in': |
| 129 | cfg['opt-in'] = True |
| 130 | cfg['countdown'] = 0 |
| 131 | save_config(cfg) |
| 132 | print('reclient metrics upload is opted in.') |
| 133 | return 0 |
Ben Segall | 530d86d | 2023-05-29 16:11:00 +0000 | [diff] [blame] | 134 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 135 | if len(argv) == 2 and argv[1] == 'opt-out': |
| 136 | cfg['opt-in'] = False |
| 137 | save_config(cfg) |
| 138 | print('reclient metrics upload is opted out.') |
| 139 | return 0 |
Ben Segall | 530d86d | 2023-05-29 16:11:00 +0000 | [diff] [blame] | 140 | |
| 141 | |
| 142 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 143 | sys.exit(main(sys.argv)) |