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