• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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            try:
32                self._temp_dir.__exit__(exc_type, exc_value, traceback)
33            except OSError:
34                # Ignore the errors when cleaning up the temporary folder.
35                pass
36            return True
37
38        def name(self):
39            """Returns the location of the isolate dir."""
40            return self._temp_dir.name
41
42    def __init__(self, extra_inits: List[AbstractContextManager] = None):
43        # Keep the alphabetical order.
44        self._extra_inits = [
45            self.IsolateDir(),
46            ScopedFfxConfig('ffx.isolated', 'true'),
47            ScopedFfxConfig('daemon.autostart', 'false'),
48            # fxb/126212: The timeout rate determines the timeout for each file
49            # transfer based on the size of the file / this rate (in MB).
50            # Decreasing the rate to 1 (from 5) increases the timeout in
51            # swarming, where large files can take longer to transfer.
52            ScopedFfxConfig('fastboot.flash.timeout_rate', '1'),
53            ScopedFfxConfig('fastboot.reboot.reconnect_timeout', '120'),
54            ScopedFfxConfig('fastboot.usb.disabled', 'true'),
55            ScopedFfxConfig('log.level', 'debug'),
56            ScopedFfxConfig('repository.server.listen', '"[::]:0"'),
57        ] + (extra_inits or [])
58
59    # Updating configurations to meet the requirement of isolate.
60    def __enter__(self):
61        # This environment variable needs to be set before stopping ffx daemon
62        # to avoid sending unnecessary analytics.
63        os.environ['FUCHSIA_ANALYTICS_DISABLED'] = '1'
64        stop_ffx_daemon()
65        for extra_init in self._extra_inits:
66            extra_init.__enter__()
67        start_ffx_daemon()
68        return self
69
70    def __exit__(self, exc_type, exc_value, traceback):
71        for extra_init in self._extra_inits:
72            extra_init.__exit__(exc_type, exc_value, traceback)
73        stop_ffx_daemon()
74
75    def isolate_dir(self):
76        """Returns the location of the isolate dir."""
77        return self._extra_inits[0].name()
78
79
80def main():
81    """Executes the IsolateDaemon and waits for the sigterm."""
82    catch_sigterm()
83    with IsolateDaemon() as daemon:
84        # Clients can assume the daemon is up and running when the output is
85        # captured. Note, the client may rely on the printed isolate_dir.
86        print(daemon.isolate_dir(), flush=True)
87        wait_for_sigterm('shutting down the daemon.')
88
89
90if __name__ == '__main__':
91    sys.exit(main())
92