blob: 780f6ac0c4885d3d57de6e79108134e2e4095a72 [file] [log] [blame]
Jason Glasgow5395ed22011-08-19 13:16:47 -04001# 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
Ryan Harrisonb74d2242013-02-04 11:36:49 -05005"""Provides utility methods for interacting with upstart"""
Jason Glasgow5395ed22011-08-19 13:16:47 -04006
Paul Stewartf748e3b2011-09-12 15:04:11 -07007import os
Jason Glasgow5395ed22011-08-19 13:16:47 -04008
Andrey Ulanovec24bc22015-05-05 21:15:08 -07009from autotest_lib.client.common_lib import utils
10
Allen Webbff11d952018-08-22 17:22:10 -070011
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
Jason Glasgow5395ed22011-08-19 13:16:47 -040020def ensure_running(service_name):
Bertrand SIMONNETbd6cdb62014-07-01 12:25:27 -070021 """Fails if |service_name| is not running.
22
23 @param service_name: name of the service.
24 """
Jason Glasgow5395ed22011-08-19 13:16:47 -040025 cmd = 'initctl status %s | grep start/running' % service_name
Andrey Ulanovec24bc22015-05-05 21:15:08 -070026 utils.system(cmd)
Bertrand SIMONNETbd6cdb62014-07-01 12:25:27 -070027
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')
Brian Norris8a0bd702016-06-03 18:06:55 -070035
36
Brian Norris1f5870a2016-06-03 18:18:10 -070037def 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
Brian Norris8a0bd702016-06-03 18:06:55 -070045def 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
Brian Norris1f5870a2016-06-03 18:18:10 -070053 if is_running(service_name):
Brian Norris8a0bd702016-06-03 18:06:55 -070054 utils.system_output('restart %s' % service_name)
55 else:
56 utils.system_output('start %s' % service_name)
Brian Norris686b7242016-06-03 18:30:46 -070057
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)