Home | History | Annotate | Download | only in tendo
      1 # Copyright 2015 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 from autotest_lib.client.bin import utils as bin_utils
      6 from autotest_lib.client.common_lib import utils
      7 
      8 def run_and_check_result(host, command):
      9     """Run a command on |host| and return whether it succeeded.
     10 
     11     @param host: Host object  if we're interested in a remote host.
     12     @param cmd: string command to run on |host|.
     13     @return True if the command succeeds. otherwise False.
     14 
     15     """
     16     run = utils.run
     17     if host is not None:
     18         run = host.run
     19     result = run(command, ignore_status=True)
     20     return result.exit_status == 0
     21 
     22 def webservd_is_installed(host=None):
     23     """Check if the webservd binary is installed.
     24 
     25     @param host: Host object if we're interested in a remote host.
     26     @return True iff webservd is installed in this system.
     27 
     28     """
     29     return run_and_check_result(
     30             host, 'if [ -f /usr/bin/webservd ]; then exit 0; fi; exit 1')
     31 
     32 def webservd_is_running(host=None, startup_timeout_seconds=5):
     33     """Check if the webservd binary is installed and running.
     34 
     35     @param host: Host object if we're interested in a remote host.
     36     @param startup_timeout_seconds: int time to wait for the server to start.
     37     @return True iff webservd is installed and running in this system.
     38 
     39     """
     40     if not webservd_is_installed(host):
     41         return False
     42 
     43     try:
     44         check_running = lambda: run_and_check_result(
     45                 host, 'initctl status webservd | grep start/running')
     46         bin_utils.poll_for_condition(check_running,
     47                                      timeout=startup_timeout_seconds,
     48                                      desc='webservd startup')
     49     except bin_utils.TimeoutError:
     50         return False
     51 
     52     return True
     53