Replace Popen calls with util.run_command
[time-slider.git] / usr / share / time-slider / lib / time_slider / smf.py
1 #!/usr/bin/python2
2 #
3 # CDDL HEADER START
4 #
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
8 #
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
13 #
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
19 #
20 # CDDL HEADER END
21 #
22
23 import subprocess
24 import threading
25 import util
26
27 #SMF EXIT CODES
28 SMF_EXIT_OK          = 0
29 SMF_EXIT_ERR_FATAL   = 95
30 SMF_EXIT_ERR_CONFIG  = 96
31 SMF_EXIT_MON_DEGRADE = 97
32 SMF_EXIT_MON_OFFLINE = 98
33 SMF_EXIT_ERR_NOSMF   = 99
34 SMF_EXIT_ERR_PERM    = 100
35 #SMF_EXIT_ERR_OTHER = non-zero
36
37
38 # Commonly used command paths
39 PFCMD = "/usr/bin/pfexec"
40 SVCSCMD = "/usr/bin/svcs"
41 SVCADMCMD = "/usr/sbin/svcadm"
42 SVCCFGCMD = "/usr/sbin/svccfg"
43 SVCPROPCMD = "/usr/bin/svcprop"
44
45
46 class SMFInstance(Exception):
47
48     def __init__(self, instanceName):
49         self.instanceName = instanceName
50         self.svcstate = self.get_service_state()
51         self.svcdeps = self.get_service_dependencies()
52
53
54     def get_service_dependencies(self):
55         cmd = [SVCSCMD, "-H", "-o", "fmri", "-d", self.instanceName]
56         outdata,errdata = util.run_command(cmd)
57         result = outdata.rstrip().split("\n")
58         return result
59
60     def get_verbose(self):
61         cmd = [SVCPROPCMD, "-c", "-p", \
62                DAEMONPROPGROUP + '/' + "verbose", \
63                self.instanceName]
64         outdata,errdata = util.run_command(cmd)
65         result = outdata.rstrip()
66         if result == "true":
67             return True
68         else:
69             return False
70
71     def find_dependency_errors(self):
72         errors = []
73         #FIXME - do this in one pass.
74         for dep in self.svcdeps:
75             cmd = [SVCSCMD, "-H", "-o", "state", dep]
76             outdata,errdata = util.run_command(cmd)
77             result = outdata.rstrip()
78             if result != "online":
79                 errors.append("%s\t%s" % (result, dep))
80         return errors
81
82     def get_service_state(self):
83         cmd = [SVCSCMD, "-H", "-o", "state", self.instanceName]
84         outdata,errdata = util.run_command(cmd)
85         result = outdata.rstrip()
86         return result
87
88     def get_prop(self, propgroup, propname):
89         cmd = [SVCPROPCMD, "-c", "-p", \
90                propgroup + '/' + propname,\
91                self.instanceName]
92         outdata,errdata = util.run_command(cmd)
93         result = outdata.rstrip()
94
95         return result
96
97     def set_prop(self, propgroup, propname, proptype, value):
98         cmd = [SVCCFGCMD, "-s", self.instanceName, "setprop", \
99                propgroup + '/' + propname, "=", proptype + ":", \
100                value]
101         util.run_command(cmd)
102         self.refresh_service()
103
104     def set_string_prop(self, propgroup, propname, value):
105         cmd = [SVCCFGCMD, "-s", self.instanceName, "setprop", \
106                propgroup + '/' + propname, "=", "astring:",
107                "\"%s\"" % (value)]
108         util.run_command(cmd)
109         self.refresh_service()
110
111     def set_boolean_prop(self, propgroup, propname, value):
112         if value == True:
113             strval = "true"
114         else:
115             strval = "false"
116         self.set_prop(propgroup, propname, "boolean", strval)
117
118     def set_integer_prop(self, propgroup, propname, value):
119         self.set_prop(propgroup, propname, "integer", str(value))
120
121     def refresh_service(self):
122         cmd = [SVCADMCMD, "refresh", self.instanceName]
123         util.run_command(cmd)
124
125     def disable_service (self):
126         if self.svcstate == "disabled":
127             return
128         cmd = [SVCADMCMD, "disable", self.instanceName]
129         util.run_command(cmd)
130         self.svcstate = self.get_service_state()
131
132     def enable_service (self):
133         if (self.svcstate == "online" or self.svcstate == "degraded"):
134             return
135         cmd = [SVCADMCMD, "enable", self.instanceName]
136         util.run_command(cmd)
137         self.svcstate = self.get_service_state()
138
139     def mark_maintenance (self):
140         cmd = [SVCADMCMD, "mark", "maintenance", self.instanceName]
141         util.run_command(cmd)
142
143     def __str__(self):
144         ret = "SMF Instance:\n" +\
145               "\tName:\t\t\t%s\n" % (self.instanceName) +\
146               "\tState:\t\t\t%s\n" % (self.svcstate)
147         return ret
148
149
150 if __name__ == "__main__":
151   S = SMFInstance('svc:/application/time-slider')
152   print S
153