Home | History | Annotate | Download | only in cros
      1 # Copyright (c) 2011 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 """Provides utility methods for interacting with upstart"""
      6 
      7 import os
      8 
      9 from autotest_lib.client.common_lib import utils
     10 
     11 def ensure_running(service_name):
     12     """Fails if |service_name| is not running.
     13 
     14     @param service_name: name of the service.
     15     """
     16     cmd = 'initctl status %s | grep start/running' % service_name
     17     utils.system(cmd)
     18 
     19 
     20 def has_service(service_name):
     21     """Returns true if |service_name| is installed on the system.
     22 
     23     @param service_name: name of the service.
     24     """
     25     return os.path.exists('/etc/init/' + service_name + '.conf')
     26 
     27 
     28 def is_running(service_name):
     29     """
     30     Returns true if |service_name| is running.
     31 
     32     @param service_name: name of service
     33     """
     34     return utils.system_output('status %s' % service_name).find('start/running') != -1
     35 
     36 def restart_job(service_name):
     37     """
     38     Restarts an upstart job if it's running.
     39     If it's not running, start it.
     40 
     41     @param service_name: name of service
     42     """
     43 
     44     if is_running(service_name):
     45         utils.system_output('restart %s' % service_name)
     46     else:
     47         utils.system_output('start %s' % service_name)
     48 
     49 def stop_job(service_name):
     50    """
     51    Stops an upstart job.
     52    Fails if the stop command fails.
     53 
     54    @param service_name: name of service
     55    """
     56 
     57    utils.system('stop %s' % service_name)
     58