xref: /aosp_15_r20/external/cronet/build/fuchsia/test/isolate_daemon.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#!/usr/bin/env vpython3
2# Copyright 2023 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Sets up the isolate daemon environment to run test on the bots."""
6
7import os
8import sys
9import tempfile
10
11from contextlib import AbstractContextManager
12from typing import List
13
14from common import catch_sigterm, set_ffx_isolate_dir, start_ffx_daemon, \
15                   stop_ffx_daemon, wait_for_sigterm
16from ffx_integration import ScopedFfxConfig
17
18
19class IsolateDaemon(AbstractContextManager):
20    """Sets up the environment of an isolate ffx daemon."""
21    class IsolateDir(AbstractContextManager):
22        """Sets up the ffx isolate dir to a temporary folder."""
23        def __init__(self):
24            self._temp_dir = tempfile.TemporaryDirectory()
25
26        def __enter__(self):
27            set_ffx_isolate_dir(self._temp_dir.__enter__())
28            return self
29
30        def __exit__(self, exc_type, exc_value, traceback):
31            return self._temp_dir.__exit__(exc_type, exc_value, traceback)
32
33        def name(self):
34            """Returns the location of the isolate dir."""
35            return self._temp_dir.name
36
37    def __init__(self, extra_inits: List[AbstractContextManager] = None):
38        # Keep the alphabetical order.
39        self._extra_inits = [
40            self.IsolateDir(),
41            ScopedFfxConfig('ffx.isolated', 'true'),
42            ScopedFfxConfig('daemon.autostart', 'false'),
43            # fxb/126212: The timeout rate determines the timeout for each file
44            # transfer based on the size of the file / this rate (in MB).
45            # Decreasing the rate to 1 (from 5) increases the timeout in
46            # swarming, where large files can take longer to transfer.
47            ScopedFfxConfig('fastboot.flash.timeout_rate', '1'),
48            ScopedFfxConfig('fastboot.reboot.reconnect_timeout', '120'),
49            ScopedFfxConfig('fastboot.usb.disabled', 'true'),
50            ScopedFfxConfig('log.level', 'debug'),
51            ScopedFfxConfig('repository.server.listen', '"[::]:0"'),
52        ] + (extra_inits or [])
53
54    # Updating configurations to meet the requirement of isolate.
55    def __enter__(self):
56        # This environment variable needs to be set before stopping ffx daemon
57        # to avoid sending unnecessary analytics.
58        os.environ['FUCHSIA_ANALYTICS_DISABLED'] = '1'
59        stop_ffx_daemon()
60        for extra_init in self._extra_inits:
61            extra_init.__enter__()
62        start_ffx_daemon()
63        return self
64
65    def __exit__(self, exc_type, exc_value, traceback):
66        for extra_init in self._extra_inits:
67            extra_init.__exit__(exc_type, exc_value, traceback)
68        stop_ffx_daemon()
69
70    def isolate_dir(self):
71        """Returns the location of the isolate dir."""
72        return self._extra_inits[0].name()
73
74
75def main():
76    """Executes the IsolateDaemon and waits for the sigterm."""
77    catch_sigterm()
78    with IsolateDaemon() as daemon:
79        # Clients can assume the daemon is up and running when the output is
80        # captured. Note, the client may rely on the printed isolate_dir.
81        print(daemon.isolate_dir(), flush=True)
82        wait_for_sigterm('shutting down the daemon.')
83
84
85if __name__ == '__main__':
86    sys.exit(main())
87