~ubuntu-branches/ubuntu/trusty/freeipa/trusty

« back to all changes in this revision

Viewing changes to ipapython/platform/debian.py

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2013-03-07 14:10:03 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130307141003-kz4lq9vj4x692mqq
Tags: 3.1.2-0ubuntu1
* Merge from unreleased debian git.
  - new upstream release
  - doesn't use chkconfig anymore (LP: #1025018, #1124093)
  - drop -U from the ntpdate options (LP: #1149468)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Authors: Simo Sorce <ssorce redhat com>
2
 
#          Alexander Bokovoy <abokovoy redhat com>
3
 
#          Marko Myllynen <myllynen redhat com>
4
 
#
5
 
# Copyright (C) 2007-2011   Red Hat
6
 
# see file 'COPYING' for use and warranty information
7
 
#
8
 
# This program is free software; you can redistribute it and/or modify
9
 
# it under the terms of the GNU General Public License as published by
10
 
# the Free Software Foundation, either version 3 of the License, or
11
 
# (at your option) any later version.
12
 
#
13
 
# This program is distributed in the hope that it will be useful,
14
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
16
 
# GNU General Public License for more details.
17
 
#
18
 
# You should have received a copy of the GNU General Public License
19
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
#
21
 
 
22
 
import os
23
 
import sys
24
 
from ipapython import ipautil
25
 
from ipapython.platform import base
26
 
 
27
 
# All what we allow exporting directly from this module
28
 
# Everything else is made available through these symbols when they directly imported into ipapython.services:
29
 
# authconfig -- class reference for platform-specific implementation of authconfig(8)
30
 
# service    -- class reference for platform-specific implementation of a PlatformService class
31
 
# knownservices -- factory instance to access named services IPA cares about, names are ipapython.services.wellknownservices
32
 
# backup_and_replace_hostname -- platform-specific way to set hostname and make it persistent over reboots
33
 
# restore_context -- platform-sepcific way to restore security context, if applicable
34
 
__all__ = ['authconfig', 'service', 'knownservices', 'backup_and_replace_hostname', 'restore_context']
35
 
 
36
 
class DebianService(base.PlatformService):
37
 
    def stop(self, instance_name="", capture_output=True):
38
 
        ipautil.run(["/usr/sbin/service", self.service_name, "stop", instance_name], capture_output=capture_output)
39
 
 
40
 
    def start(self, instance_name="", capture_output=True):
41
 
        ipautil.run(["/usr/sbin/service", self.service_name, "start", instance_name], capture_output=capture_output)
42
 
 
43
 
    def restart(self, instance_name="", capture_output=True):
44
 
        ipautil.run(["/usr/sbin/service", self.service_name, "restart", instance_name], capture_output=capture_output)
45
 
 
46
 
    def is_running(self, instance_name=""):
47
 
        ret = True
48
 
        try:
49
 
            (sout,serr,rcode) = ipautil.run(["/usr/sbin/service", self.service_name, "status", instance_name])
50
 
            if sout.find("NOT running") >= 0:
51
 
                ret = False
52
 
            if sout.find("stop") >= 0:
53
 
                ret = False
54
 
        except ipautil.CalledProcessError:
55
 
                ret = False
56
 
        return ret
57
 
 
58
 
    def is_installed(self):
59
 
        installed = True
60
 
        try:
61
 
            ipautil.run(["/usr/sbin/service", self.service_name, "status"])
62
 
        except ipautil.CalledProcessError, e:
63
 
            if e.returncode == 1:
64
 
                # service is not installed or there is other serious issue
65
 
                installed = False
66
 
        return installed
67
 
 
68
 
    def is_enabled(self):
69
 
        ret = True
70
 
        try:
71
 
            (sout,serr,rcode) = ipautil.run(["/sbin/chkconfig", self.service_name])
72
 
            if sout.find("off") >= 0:
73
 
                ret = False
74
 
            if sout.find("unknown service") >= 0:
75
 
                ret = False
76
 
        except ipautil.CalledProcessError:
77
 
                ret = False
78
 
        return ret
79
 
 
80
 
    def enable(self):
81
 
        ipautil.run(["/sbin/chkconfig", self.service_name, "on"])
82
 
 
83
 
    def disable(self):
84
 
        ipautil.run(["/sbin/chkconfig", self.service_name, "off"])
85
 
 
86
 
    def install(self):
87
 
        ipautil.run(["/sbin/chkconfig", "--add", self.service_name])
88
 
 
89
 
    def remove(self):
90
 
        ipautil.run(["/sbin/chkconfig", "--del", self.service_name])
91
 
 
92
 
class DebianAuthConfig(base.AuthConfig):
93
 
    """
94
 
    AuthConfig class implements system-independent interface to configure
95
 
    system authentication resources. In Red Hat-produced systems this is done with
96
 
    authconfig(8) utility.
97
 
    """
98
 
    def __build_args(self):
99
 
        args = []
100
 
        for (option, value) in self.parameters.items():
101
 
            if type(value) is bool:
102
 
                if value:
103
 
                    args.append("--enable%s" % (option))
104
 
                else:
105
 
                    args.append("--disable%s" % (option))
106
 
            elif type(value) in (tuple, list):
107
 
                args.append("--%s" % (option))
108
 
                args.append("%s" % (value[0]))
109
 
            elif value is None:
110
 
                args.append("--%s" % (option))
111
 
            else:
112
 
                args.append("--%s%s" % (option,value))
113
 
        return args
114
 
 
115
 
    def execute(self):
116
 
        args = self.__build_args()
117
 
        print "Would run on a Red Hat platform: /usr/sbin/authconfig " + " ".join(args)
118
 
        ipautil.user_input("Please do the corresponding changes manually and press Enter")
119
 
        #ipautil.run(["/usr/sbin/authconfig"]+args)
120
 
 
121
 
class DebianServices(base.KnownServices):
122
 
    def __init__(self):
123
 
        services = dict()
124
 
        for s in base.wellknownservices:
125
 
            if s == "certmonger":
126
 
                services[s] = DebianService("certmonger")
127
 
            elif s == "messagebus":
128
 
                services[s] = DebianService("dbus")
129
 
            elif s == "ntpd":
130
 
                services[s] = DebianService("ntp")
131
 
            else:
132
 
                services[s] = DebianService(s)
133
 
        # Call base class constructor. This will lock services to read-only
134
 
        super(DebianServices, self).__init__(services)
135
 
 
136
 
authconfig = DebianAuthConfig
137
 
service = DebianService
138
 
knownservices = DebianServices()
139
 
 
140
 
def restore_context(filepath):
141
 
    """
142
 
    restore security context on the file path
143
 
    SELinux equivalent is /sbin/restorecon <filepath>
144
 
 
145
 
    restorecon's return values are not reliable so we have to
146
 
    ignore them (BZ #739604).
147
 
 
148
 
    ipautil.run() will do the logging.
149
 
    """
150
 
    if os.path.exists("/sbin/restorecon"):
151
 
        ipautil.run(["/sbin/restorecon", filepath], raiseonerr=False)
152
 
 
153
 
def backup_and_replace_hostname(fstore, statestore, hostname):
154
 
    network_filename = "/etc/hostname"
155
 
    # Backup original /etc/hostname
156
 
    fstore.backup_file(network_filename)
157
 
 
158
 
    # Write new configuration
159
 
    f = open(network_filename, 'w')
160
 
    f.write(hostname + "\n")
161
 
    f.close()
162
 
 
163
 
    try:
164
 
        ipautil.run(['/bin/hostname', hostname])
165
 
    except ipautil.CalledProcessError, e:
166
 
        print >>sys.stderr, "Failed to set this machine hostname to %s (%s)." % (hostname, str(e))
167
 
 
168
 
    # For SE Linux environments it is important to reset SE labels to the expected ones
169
 
    try:
170
 
        restore_context(network_filename)
171
 
    except ipautil.CalledProcessError, e:
172
 
        print >>sys.stderr, "Failed to set permissions for %s (%s)." % (network_filename, str(e))
173
 
 
174