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 self._extra_inits = [ 39 self.IsolateDir(), 40 ScopedFfxConfig('repository.server.listen', '"[::]:0"'), 41 ScopedFfxConfig('daemon.autostart', 'false'), 42 ScopedFfxConfig('discovery.zedboot.enabled', 'false'), 43 ScopedFfxConfig('fastboot.reboot.reconnect_timeout', '120'), 44 ScopedFfxConfig('log.level', 'debug') 45 ] + (extra_inits or []) 46 47 def __enter__(self): 48 # Updating configurations to meet the requirement of isolate. 49 os.environ['FUCHSIA_ANALYTICS_DISABLED'] = '1' 50 stop_ffx_daemon() 51 for extra_init in self._extra_inits: 52 extra_init.__enter__() 53 start_ffx_daemon() 54 return self 55 56 def __exit__(self, exc_type, exc_value, traceback): 57 for extra_init in self._extra_inits: 58 extra_init.__exit__(exc_type, exc_value, traceback) 59 stop_ffx_daemon() 60 61 def isolate_dir(self): 62 """Returns the location of the isolate dir.""" 63 return self._extra_inits[0].name() 64 65 66def main(): 67 """Executes the IsolateDaemon and waits for the sigterm.""" 68 catch_sigterm() 69 with IsolateDaemon() as daemon: 70 # Clients can assume the daemon is up and running when the output is 71 # captured. Note, the client may rely on the printed isolate_dir. 72 print(daemon.isolate_dir(), flush=True) 73 wait_for_sigterm('shutting down the daemon.') 74 75 76if __name__ == '__main__': 77 sys.exit(main()) 78