Esempio n. 1
0
def main():
    if os.getenv('CHROME_REMOTE_DESKTOP_SESSION') == '1':
        # Disable itself when run under Google Chrome Remote Desktop, as it's
        # normally started at the console and starting up via Remote Desktop would
        # cause multiple bots to run concurrently on the host.
        print >> sys.stderr, (
            'Inhibiting Swarming bot under Google Chrome Remote Desktop.')
        return 0

    # Always make the current working directory the directory containing this
    # file. It simplifies assumptions.
    os.chdir(os.path.dirname(THIS_FILE))
    # Always create the logs dir first thing, before printing anything out.
    if not os.path.isdir('logs'):
        os.mkdir('logs')

    # This is necessary so os.path.join() works with unicode path. No kidding.
    # This must be done here as each of the command take wildly different code
    # path and this must be run in every case, as it causes really unexpected
    # issues otherwise, especially in module os.path.
    fix_encoding.fix_encoding()

    # This is extremely useful to debug hangs.
    signal_trace.register()

    if os.path.basename(THIS_FILE) == 'swarming_bot.zip':
        # Self-replicate itself right away as swarming_bot.1.zip and restart the bot
        # process as this copy. This enables LKGBC logic.
        print >> sys.stderr, 'Self replicating pid:%d.' % os.getpid()
        if os.path.isfile('swarming_bot.1.zip'):
            os.remove('swarming_bot.1.zip')
        shutil.copyfile('swarming_bot.zip', 'swarming_bot.1.zip')
        cmd = ['swarming_bot.1.zip'] + sys.argv[1:]
        print >> sys.stderr, 'cmd: %s' % cmd
        return common.exec_python(cmd)

    # sys.argv[0] is the zip file itself.
    cmd = 'start_slave'
    args = []
    if len(sys.argv) > 1:
        cmd = sys.argv[1]
        args = sys.argv[2:]

    fn = getattr(sys.modules[__name__], 'CMD%s' % cmd, None)
    if fn:
        try:
            return fn(args)
        except ImportError:
            logging.exception('Failed to run %s', cmd)
            with zipfile.ZipFile(THIS_FILE, 'r') as f:
                logging.error('Files in %s:\n%s', THIS_FILE, f.namelist())
            return 1

    print >> sys.stderr, 'Unknown command %s' % cmd
    return 1
Esempio n. 2
0
def main():
  if os.getenv('CHROME_REMOTE_DESKTOP_SESSION') == '1':
    # Disable itself when run under Google Chrome Remote Desktop, as it's
    # normally started at the console and starting up via Remote Desktop would
    # cause multiple bots to run concurrently on the host.
    print >> sys.stderr, (
        'Inhibiting Swarming bot under Google Chrome Remote Desktop.')
    return 0

  # Always make the current working directory the directory containing this
  # file. It simplifies assumptions.
  os.chdir(os.path.dirname(THIS_FILE))
  # Always create the logs dir first thing, before printing anything out.
  if not os.path.isdir('logs'):
    os.mkdir('logs')

  # This is necessary so os.path.join() works with unicode path. No kidding.
  # This must be done here as each of the command take wildly different code
  # path and this must be run in every case, as it causes really unexpected
  # issues otherwise, especially in module os.path.
  fix_encoding.fix_encoding()

  if os.path.basename(THIS_FILE) == 'swarming_bot.zip':
    # Self-replicate itself right away as swarming_bot.1.zip and restart as it.
    print >> sys.stderr, 'Self replicating pid:%d.' % os.getpid()
    if os.path.isfile('swarming_bot.1.zip'):
      os.remove('swarming_bot.1.zip')
    shutil.copyfile('swarming_bot.zip', 'swarming_bot.1.zip')
    cmd = ['swarming_bot.1.zip'] + sys.argv[1:]
    print >> sys.stderr, 'cmd: %s' % cmd
    return common.exec_python(cmd)

  # sys.argv[0] is the zip file itself.
  cmd = 'start_slave'
  args = []
  if len(sys.argv) > 1:
    cmd = sys.argv[1]
    args = sys.argv[2:]

  fn = getattr(sys.modules[__name__], 'CMD%s' % cmd, None)
  if fn:
    try:
      return fn(args)
    except ImportError:
      logging.exception('Failed to run %s', cmd)
      with zipfile.ZipFile(THIS_FILE, 'r') as f:
        logging.error('Files in %s:\n%s', THIS_FILE, f.namelist())
      return 1

  print >> sys.stderr, 'Unknown command %s' % cmd
  return 1
Esempio n. 3
0
def main():
  # Always make the current working directory the directory containing this
  # file. It simplifies assumptions.
  base_dir = os.path.dirname(THIS_FILE)
  os.chdir(base_dir)
  # Always create the logs dir first thing, before printing anything out.
  if not os.path.isdir('logs'):
    os.mkdir('logs')

  net.set_user_agent('swarming_bot/' + __version__)

  # This is necessary so os.path.join() works with unicode path. No kidding.
  # This must be done here as each of the command take wildly different code
  # path and this must be run in every case, as it causes really unexpected
  # issues otherwise, especially in module os.path.
  fix_encoding.fix_encoding()

  # This is extremely useful to debug hangs.
  signal_trace.register()

  if os.path.basename(THIS_FILE) == 'swarming_bot.zip':
    # Self-replicate itself right away as swarming_bot.1.zip and restart the bot
    # process as this copy. This enables LKGBC logic.
    print('Self replicating pid:%d.' % os.getpid(), file=sys.stderr)
    new_zip = os.path.join(base_dir, 'swarming_bot.1.zip')
    if os.path.isfile(new_zip):
      os.remove(new_zip)
    shutil.copyfile(THIS_FILE, new_zip)
    cmd = [new_zip] + sys.argv[1:]
    print('cmd: %s' % cmd, file=sys.stderr)
    return common.exec_python(cmd)

  # sys.argv[0] is the zip file itself.
  cmd = 'start_slave'
  args = []
  if len(sys.argv) > 1:
    cmd = sys.argv[1]
    args = sys.argv[2:]

  fn = getattr(sys.modules[__name__], 'CMD%s' % cmd, None)
  if fn:
    try:
      return fn(args)
    except ImportError:
      logging.exception('Failed to run %s', cmd)
      with zipfile.ZipFile(THIS_FILE, 'r') as f:
        logging.error('Files in %s:\n%s', THIS_FILE, f.namelist())
      return 1

  print('Unknown command %s' % cmd, file=sys.stderr)
  return 1
Esempio n. 4
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(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'])