summaryrefslogtreecommitdiffstats
path: root/func/minion/modules/service.py
blob: d0889073deb09a7b27b13fc7c664fc7b80c11265 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
## func
##
## Copyright 2007, Red Hat, Inc
## Michael DeHaan <mdehaan@redhat.com>
##
## This software may be freely redistributed under the terms of the GNU
## general public license.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##
##

import codes
from modules import func_module

import sub_process
import os

class Service(func_module.FuncModule):

    def __init__(self):
        self.methods = {
            "start"       : self.start,
            "stop"        : self.stop,
            "restart"     : self.restart,
            "reload"      : self.reload,
            "status"      : self.status,
            "get_enabled" : self.get_enabled,
            "get_running" : self.get_running,
            "inventory"   : self.inventory,
        }
        func_module.FuncModule.__init__(self)

    def __command(self, service_name, command):

        filename = os.path.join("/etc/rc.d/init.d/",service_name)
        if os.path.exists(filename):
            return sub_process.call(["/sbin/service", service_name, command])
        else:
            raise codes.FuncException("Service not installed: %s" % service_name)

    def start(self, service_name):
        return self.__command(service_name, "start")

    def stop(self, service_name):
        return self.__command(service_name, "stop")

    def restart(self, service_name):
        return self.__command(service_name, "restart")

    def reload(self, service_name):
        return self.__command(service_name, "reload")

    def status(self, service_name):
        return self.__command(service_name, "status")

    def inventory(self):
        return {
            "running" : self.get_running(),
            "enabled" : self.get_enabled()
        }

    def get_enabled(self):
        """
        Get the list of services that are enabled at the various runlevels.  Xinetd services
        only provide whether or not they are running, not specific runlevel info.
        """

        chkconfig = sub_process.Popen(["/sbin/chkconfig", "--list"], stdout=sub_process.PIPE)
        data = chkconfig.communicate()[0]
        results = []
        for line in data.split("\n"):
            if line.find("0:") != -1:
               # regular services
               tokens = line.split()
               results.append((tokens[0],tokens[1:]))
            elif line.find(":") != -1 and not line.endswith(":"):
               # xinetd.d based services
               tokens = line.split()
               tokens[0] = tokens[0].replace(":","")
               results.append((tokens[0],tokens[1]))
        return results

    def get_running(self):
        """
        Get a list of which services are running, stopped, or disabled.
        """
        chkconfig = sub_process.Popen(["/sbin/service", "--status-all"], stdout=sub_process.PIPE)
        data = chkconfig.communicate()[0]
        results = []
        for line in data.split("\n"):
            if line.find(" is ") != -1:
                tokens = line.split()
                results.append((tokens[0], tokens[-1].replace("...","")))
        return results

methods = Service()
register_rpc = methods.register_rpc