def test_rotating_phase_2(): # Simulate rotating the log. logging_utils.prepare_logging('shared.log') logging.info('Child1') os.rename('shared.log', 'shared.1.log') logging.info('Child2') return 0
def CMDshell(args): """Starts a shell with api.* in..""" logging_utils.prepare_logging(None) logging_utils.set_console_level(logging.DEBUG) from bot_code import bot_main from api import os_utilities from api import platforms local_vars = { 'bot_main': bot_main, 'json': json, 'os_utilities': os_utilities, 'platforms': platforms, } # Can't use: from api.platforms import * local_vars.update( (k, v) for k, v in platforms.__dict__.iteritems() if not k.startswith('_')) if args: for arg in args: exec code.compile_command(arg) in local_vars else: code.interact( 'Locals:\n ' + '\n '.join( sorted(local_vars)), None, local_vars) return 0
def CMDconfig(_args): """Prints the config.json embedded in this zip.""" logging_utils.prepare_logging(None) from bot_code import bot_main json.dump(bot_main.get_config(), sys.stdout, indent=2, sort_keys=True) print('') return 0
def CMDshell(args): """Starts a shell with api.* in..""" logging_utils.prepare_logging(None) logging_utils.set_console_level(logging.DEBUG) from bot_code import bot_main from api import os_utilities from api import platforms local_vars = { 'bot_main': bot_main, 'json': json, 'os_utilities': os_utilities, 'platforms': platforms, } # Can't use: from api.platforms import * local_vars.update((k, v) for k, v in platforms.__dict__.iteritems() if not k.startswith('_')) if args: for arg in args: exec code.compile_command(arg) in local_vars else: code.interact('Locals:\n ' + '\n '.join(sorted(local_vars)), None, local_vars) return 0
def CMDstart_slave(args): """Ill named command that actually sets up the bot then start it.""" # TODO(maruel): Rename function. logging_utils.prepare_logging('bot_config.log') logging_utils.set_console_level(logging.DEBUG) parser = optparse.OptionParser() parser.add_option( '--survive', action='store_true', help='Do not reboot the host even if bot_config.setup_bot() asked to') options, args = parser.parse_args(args) # User provided bot_config.py logging.info( 'importing bot_config: %s, %s', THIS_FILE, zip_package.generate_version()) try: import bot_main bot_main.setup_bot(options.survive) except Exception: logging.exception('bot_config.py is invalid.') logging.info('Starting the bot: %s', THIS_FILE) cmd = [sys.executable, THIS_FILE, 'start_bot'] if sys.platform in ('cygwin', 'win32'): try: subprocess.Popen(cmd) return 0 except Exception as e: logging.exception('failed to start: %s', e) return 1 else: os.execv(cmd[0], cmd)
def CMDrestart(_args): """Utility subcommand that hides the difference between each OS to reboot the host.""" logging_utils.prepare_logging(None) import os_utilities # This function doesn't return. os_utilities.restart() # Should never reach here. return 1
def CMDstart_bot(args): """Starts the swarming bot.""" logging_utils.prepare_logging(os.path.join('logs', 'swarming_bot.log')) logging.info( 'importing bot_main: %s, %s', THIS_FILE, zip_package.generate_version()) from bot_code import bot_main result = bot_main.main(args) logging.info('bot_main exit code: %d', result) return result
def CMDreboot(_args): """Utility subcommand that hides the difference between each OS to reboot the host.""" logging_utils.prepare_logging(None) import os_utilities # This function doesn't return. os_utilities.host_reboot() # Should never reach here. return 1
def CMDstart_bot(args): """Starts the swarming bot.""" logging_utils.prepare_logging('swarming_bot.log') logging_utils.set_console_level(logging.DEBUG) logging.info( 'importing bot_main: %s, %s', THIS_FILE, zip_package.generate_version()) import bot_main result = bot_main.main(args) logging.info('bot_main exit code: %d', result) return result
def test_prepare_logging(self): root = logging.RootLogger(logging.DEBUG) filepath = os.path.join(self.tmp, 'test.log') logging_utils.prepare_logging(filepath, root) root.debug('foo') with open(filepath, 'rb') as f: result = f.read() # It'd be nice to figure out a way to ensure it's properly in UTC but it's # tricky to do reliably. expected = _LOG_HEADER + ' D: foo\n$' self.assertTrue(re.match(expected, result), (expected, result))
def test_prepare_logging(self): root = logging.RootLogger(logging.DEBUG) tmp_dir = tempfile.mkdtemp(prefix="logging_utils_test") try: filepath = os.path.join(tmp_dir, "test.log") logging_utils.prepare_logging(filepath, root) root.debug("foo") with open(filepath, "rb") as f: result = f.read() finally: shutil.rmtree(tmp_dir) # It'd be nice to figure out a way to ensure it's properly in UTC but it's # tricky to do reliably. self.assertTrue(re.match(_LOG_HEADER + "DEBUG foo\n$", result), result)
def CMDstart_bot(args): """Starts the swarming bot.""" logging_utils.prepare_logging(os.path.join('logs', 'swarming_bot.log')) from bot_code import bot_main logging.info('importing bot_main: %s, %s', THIS_FILE, bot_main.generate_version()) adb_logger = logging.getLogger('adb') logging_utils.prepare_logging(os.path.join('logs', 'adb.log'), adb_logger) adb_logger.setLevel(logging.DEBUG) for child in ('high', 'low', 'usb', 'cmd'): adb_logger.getChild(child).setLevel(logging.DEBUG) adb_logger.propagate = False result = bot_main.main(args) logging.info('bot_main exit code: %d', result) return result
def CMDstart_slave(args): """Ill named command that actually sets up the bot then start it.""" # TODO(maruel): Rename function. logging_utils.prepare_logging(os.path.join('logs', 'bot_config.log')) parser = optparse.OptionParser() parser.add_option( '--survive', action='store_true', help='Do not reboot the host even if bot_config.setup_bot() asked to') options, args = parser.parse_args(args) try: from bot_code import bot_main bot_main.setup_bot(options.survive) except Exception: logging.exception('bot_main.py failed.') logging.info('Starting the bot: %s', THIS_FILE) return common.exec_python([THIS_FILE, 'start_bot'])
def test_rotating_phase_1(): logging_utils.prepare_logging('shared.log') logging.info('Parent1') r = call('test_rotating_phase_2', None) logging.info('Parent2') return r
#!/usr/bin/env python # Copyright 2019 The LUCI Authors. All rights reserved. # Use of this source code is governed under the Apache License, Version 2.0 # that can be found in the LICENSE file. import logging import os import subprocess import sys THIS_DIR = os.path.dirname(os.path.abspath(__file__)) CLIENT_DIR = os.path.dirname(os.path.dirname(THIS_DIR)) sys.path.insert(0, CLIENT_DIR) sys.path.insert(0, os.path.join(CLIENT_DIR, 'third_party')) from utils import logging_utils dst = sys.argv[1] logging_utils.prepare_logging(os.path.join(dst, 'shared.log')) logging.info('Parent1') r = subprocess.call([sys.executable, '-u', 'phase2.py', dst], cwd=THIS_DIR) logging.info('Parent2') sys.exit(r)
def CMDversion(_args): """Prints the version of this file and the hash of the code.""" logging_utils.prepare_logging(None) print zip_package.generate_version() return 0
def CMDtask_runner(args): """Internal command to run a swarming task.""" logging_utils.prepare_logging(os.path.join('logs', 'task_runner.log')) from bot_code import task_runner return task_runner.main(args)
def CMDserver(_args): """Prints the server url. It's like 'config' but easier to parse.""" logging_utils.prepare_logging(None) from bot_code import bot_main print bot_main.get_config()['server'] return 0
def CMDsetup(_args): """Setup the bot to auto-start but doesn't start the bot.""" logging_utils.prepare_logging(os.path.join('logs', 'bot_config.log')) from bot_code import bot_main bot_main.setup_bot(True) return 0
def CMDtask_runner(args): """Internal command to run a swarming task.""" logging_utils.prepare_logging('task_runner.log') import task_runner return task_runner.main(args)
'logs', # TODO(maruel): Move inside work. 'task_runner_in.json', 'task_runner_out.json', } self.assertEqual(expected, set(os.listdir(self.root_dir))) expected = { u'exit_code': 1 if sys.platform == 'win32' else -signal.SIGTERM, u'hard_timeout': False, u'io_timeout': False, u'must_signal_internal_failure': u'task_runner received signal %d' % task_runner.SIG_BREAK_OR_TERM, u'version': 3, } with open(task_result_file, 'rb') as f: self.assertEqual(expected, json.load(f)) self.assertEqual(0, proc.returncode) if __name__ == '__main__': fix_encoding.fix_encoding() if '-v' in sys.argv: unittest.TestCase.maxDiff = None logging_utils.prepare_logging(None) logging_utils.set_console_level( logging.DEBUG if '-v' in sys.argv else logging.CRITICAL+1) # Fix litteral text expectation. os.environ['LANG'] = 'en_US.UTF-8' os.environ['LANGUAGE'] = 'en_US.UTF-8' unittest.main()
} self.assertEqual(expected, set(os.listdir(self.root_dir))) expected = { u'exit_code': 1 if sys.platform == 'win32' else -signal.SIGTERM, u'hard_timeout': False, u'io_timeout': False, u'must_signal_internal_failure': u'task_runner received signal %d' % task_runner.SIG_BREAK_OR_TERM, u'version': 3, } with open(task_result_file, 'rb') as f: self.assertEqual(expected, json.load(f)) self.assertEqual(0, proc.returncode) if __name__ == '__main__': fix_encoding.fix_encoding() if '-v' in sys.argv: unittest.TestCase.maxDiff = None logging_utils.prepare_logging(None) logging_utils.set_console_level(logging.DEBUG if '-v' in sys.argv else logging.CRITICAL + 1) # Fix litteral text expectation. os.environ['LANG'] = 'en_US.UTF-8' os.environ['LANGUAGE'] = 'en_US.UTF-8' unittest.main()
def CMDrun_isolated(args): """Internal command to run an isolated command.""" logging_utils.prepare_logging('run_isolated_bot.log') sys.path.insert(0, os.path.join(THIS_FILE, 'client')) import run_isolated return run_isolated.main(args)