2011-01-13 11:26:30 -06:00
|
|
|
#!/usr/bin/python
|
2010-12-04 14:42:14 -06:00
|
|
|
# Authors: Simo Sorce <ssorce@redhat.com>
|
2008-02-28 10:37:06 -06:00
|
|
|
#
|
2010-12-04 14:42:14 -06:00
|
|
|
# Copyright (C) 2008-2010 Red Hat
|
2008-02-28 10:37:06 -06:00
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2008-02-28 10:37:06 -06:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-02-28 10:37:06 -06:00
|
|
|
#
|
2010-12-04 14:42:14 -06:00
|
|
|
|
|
|
|
import sys
|
|
|
|
try:
|
|
|
|
from ipaserver.install import service
|
2010-12-10 16:40:37 -06:00
|
|
|
from ipaserver.install import dsinstance
|
2010-12-04 14:42:14 -06:00
|
|
|
from ipapython import config
|
|
|
|
from ipalib import api, errors
|
|
|
|
import logging
|
|
|
|
import ldap
|
|
|
|
import socket
|
|
|
|
except ImportError:
|
|
|
|
print >> sys.stderr, """\
|
|
|
|
There was a problem importing one of the required Python modules. The
|
|
|
|
error was:
|
|
|
|
|
|
|
|
%s
|
|
|
|
""" % sys.exc_value
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def parse_options():
|
|
|
|
usage = "%prog start|stop|restart|status\n"
|
|
|
|
parser = config.IPAOptionParser(usage=usage,
|
|
|
|
formatter=config.IPAFormatter())
|
|
|
|
|
|
|
|
parser.add_option("-d", "--debug", action="store_true", dest="debug",
|
|
|
|
help="Display debugging information")
|
|
|
|
|
|
|
|
options, args = parser.parse_args()
|
|
|
|
safe_options = parser.get_safe_opts(options)
|
|
|
|
|
|
|
|
return safe_options, options, args
|
|
|
|
|
|
|
|
def emit_err(err):
|
2011-01-17 08:20:35 -06:00
|
|
|
sys.stderr.write(err)
|
2010-12-04 14:42:14 -06:00
|
|
|
|
|
|
|
def get_config():
|
|
|
|
base = "cn=%s,cn=masters,cn=ipa,cn=etc,%s" % (socket.gethostname(),
|
|
|
|
api.env.basedn)
|
|
|
|
srcfilter = '(ipaConfigString=enabledService)'
|
|
|
|
attrs = ['cn', 'ipaConfigString']
|
|
|
|
|
|
|
|
try:
|
|
|
|
con = ldap.initialize(api.env.ldap_uri)
|
|
|
|
con.simple_bind()
|
|
|
|
res = con.search_st(base,
|
|
|
|
ldap.SCOPE_SUBTREE,
|
|
|
|
filterstr=srcfilter,
|
|
|
|
attrlist=attrs,
|
|
|
|
timeout=10)
|
2010-12-10 16:40:37 -06:00
|
|
|
except Exception, e:
|
2010-12-04 14:42:14 -06:00
|
|
|
print "Error retrieving list of services %s" % e
|
|
|
|
print "Is IPA installed ?"
|
2010-12-10 16:40:37 -06:00
|
|
|
raise
|
2010-12-04 14:42:14 -06:00
|
|
|
|
|
|
|
svc_list = []
|
|
|
|
|
|
|
|
for entry in res:
|
|
|
|
name = entry[1]['cn'][0]
|
|
|
|
for p in entry[1]['ipaConfigString']:
|
|
|
|
if p.startswith('startOrder '):
|
|
|
|
order = p.split()[1]
|
|
|
|
svc_list.append((order, name))
|
|
|
|
|
|
|
|
return svc_list
|
|
|
|
|
2010-12-10 16:40:37 -06:00
|
|
|
def ipa_start(serverid):
|
2010-12-04 14:42:14 -06:00
|
|
|
|
|
|
|
try:
|
|
|
|
print "Starting Directory Service"
|
2011-01-17 08:17:08 -06:00
|
|
|
service.start('dirsrv', instance_name=serverid, capture_output=False)
|
2010-12-04 14:42:14 -06:00
|
|
|
except:
|
|
|
|
emit_err("Failed to start Directory Service")
|
|
|
|
return
|
|
|
|
|
2010-12-10 16:40:37 -06:00
|
|
|
svc_list = []
|
|
|
|
try:
|
|
|
|
svc_list = get_config()
|
|
|
|
except:
|
|
|
|
emit_err("Failed to read data from Directory Service")
|
|
|
|
emit_err("Shutting down")
|
2011-01-17 08:17:08 -06:00
|
|
|
service.stop('dirsrv', instance_name=serverid, capture_output=False)
|
2010-12-10 16:40:37 -06:00
|
|
|
|
|
|
|
if len(svc_list) == 0:
|
|
|
|
return
|
2010-12-04 14:42:14 -06:00
|
|
|
|
|
|
|
for (order, svc) in sorted(svc_list):
|
|
|
|
svc_name = service.SERVICE_LIST[svc][0]
|
|
|
|
try:
|
|
|
|
print "Starting %s Service" % svc
|
2011-01-17 08:17:08 -06:00
|
|
|
service.start(svc_name, capture_output=False)
|
2010-12-04 14:42:14 -06:00
|
|
|
except:
|
|
|
|
emit_err("Failed to start %s Service" % svc)
|
|
|
|
emit_err("Shutting down")
|
|
|
|
for (order, svc) in sorted(svc_list):
|
|
|
|
svc_name = service.SERVICE_LIST[svc][0]
|
|
|
|
try:
|
2011-01-17 08:17:08 -06:00
|
|
|
service.stop(svc_name, capture_output=False)
|
2010-12-04 14:42:14 -06:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
2011-01-17 08:17:08 -06:00
|
|
|
service.stop('dirsrv', instance_name=serverid, capture_output=False)
|
2010-12-04 14:42:14 -06:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return
|
|
|
|
|
2010-12-10 16:40:37 -06:00
|
|
|
def ipa_stop(serverid):
|
2010-12-04 14:42:14 -06:00
|
|
|
|
2010-12-10 16:40:37 -06:00
|
|
|
svc_list = []
|
|
|
|
try:
|
|
|
|
svc_list = get_config()
|
|
|
|
except:
|
|
|
|
# ok if dirsrv died this may fail, so let's try to quickly restart it
|
|
|
|
# and see if we can get anything. If not throw our hands up and just
|
|
|
|
# exit
|
|
|
|
try:
|
2011-01-17 08:17:08 -06:00
|
|
|
service.start('dirsrv', instance_name=serverid, capture_output=False)
|
2010-12-10 16:40:37 -06:00
|
|
|
svc_list = get_config()
|
|
|
|
except:
|
|
|
|
emit_err("Failed to read data from Directory Service")
|
|
|
|
emit_err("Shutting down")
|
2011-01-17 08:17:08 -06:00
|
|
|
service.stop('dirsrv', instance_name=serverid, capture_output=False)
|
2010-12-10 16:40:37 -06:00
|
|
|
|
|
|
|
if len(svc_list) == 0:
|
|
|
|
return
|
2010-12-04 14:42:14 -06:00
|
|
|
|
|
|
|
for (order, svc) in sorted(svc_list, reverse=True):
|
|
|
|
svc_name = service.SERVICE_LIST[svc][0]
|
|
|
|
try:
|
|
|
|
print "Stopping %s Service" % svc
|
2011-01-17 08:17:08 -06:00
|
|
|
service.stop(svc_name, capture_output=False)
|
2010-12-04 14:42:14 -06:00
|
|
|
except:
|
|
|
|
emit_err("Failed to stop %s Service" % svc)
|
|
|
|
|
|
|
|
try:
|
|
|
|
print "Stopping Directory Service"
|
2011-01-17 08:17:08 -06:00
|
|
|
service.stop('dirsrv', instance_name=serverid, capture_output=False)
|
2010-12-04 14:42:14 -06:00
|
|
|
except:
|
|
|
|
emit_err("Failed to stop Directory Service")
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2010-12-10 16:40:37 -06:00
|
|
|
def ipa_restart(serverid):
|
2010-12-04 14:42:14 -06:00
|
|
|
try:
|
|
|
|
print "Restarting Directory Service"
|
2011-01-17 08:17:08 -06:00
|
|
|
service.restart('dirsrv', instance_name=serverid, capture_output=False)
|
2010-12-04 14:42:14 -06:00
|
|
|
except:
|
|
|
|
emit_err("Failed to restart Directory Service")
|
|
|
|
return
|
|
|
|
|
2010-12-10 16:40:37 -06:00
|
|
|
svc_list = []
|
|
|
|
try:
|
|
|
|
svc_list = get_config()
|
|
|
|
except:
|
|
|
|
emit_err("Failed to read data from Directory Service")
|
|
|
|
emit_err("Shutting down")
|
2011-01-17 08:17:08 -06:00
|
|
|
service.stop('dirsrv', instance_name=serverid, capture_output=False)
|
2010-12-10 16:40:37 -06:00
|
|
|
|
|
|
|
if len(svc_list) == 0:
|
|
|
|
return
|
2010-12-04 14:42:14 -06:00
|
|
|
|
|
|
|
for (order, svc) in sorted(svc_list):
|
|
|
|
svc_name = service.SERVICE_LIST[svc][0]
|
|
|
|
try:
|
|
|
|
print "Restarting %s Service" % svc
|
2011-01-17 08:17:08 -06:00
|
|
|
service.restart(svc_name, capture_output=False)
|
2010-12-04 14:42:14 -06:00
|
|
|
except:
|
|
|
|
emit_err("Failed to restart %s Service" % svc)
|
|
|
|
emit_err("Shutting down")
|
|
|
|
for (order, svc) in sorted(svc_list):
|
|
|
|
svc_name = service.SERVICE_LIST[svc][0]
|
|
|
|
try:
|
2011-01-17 08:17:08 -06:00
|
|
|
service.stop(svc_name, capture_output=False)
|
2010-12-04 14:42:14 -06:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
2011-01-17 08:17:08 -06:00
|
|
|
service.stop('dirsrv', instance_name=serverid, capture_output=False)
|
2010-12-04 14:42:14 -06:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return
|
|
|
|
|
2010-12-10 16:40:37 -06:00
|
|
|
def ipa_status(serverid):
|
2010-12-04 14:42:14 -06:00
|
|
|
try:
|
2010-12-10 16:40:37 -06:00
|
|
|
if service.is_running('dirsrv', instance_name=serverid):
|
2010-12-04 14:42:14 -06:00
|
|
|
print "Directory Service: RUNNING"
|
|
|
|
else:
|
|
|
|
print "Directory Service: STOPPED"
|
|
|
|
except:
|
|
|
|
print "Failed to get Directory Service status"
|
|
|
|
return
|
|
|
|
|
2010-12-10 16:40:37 -06:00
|
|
|
svc_list = []
|
|
|
|
try:
|
|
|
|
svc_list = get_config()
|
|
|
|
except:
|
|
|
|
print "Failed to get list of services to probe status"
|
|
|
|
|
|
|
|
if len(svc_list) == 0:
|
|
|
|
return
|
2010-12-04 14:42:14 -06:00
|
|
|
|
|
|
|
for (order, svc) in sorted(svc_list):
|
|
|
|
svc_name = service.SERVICE_LIST[svc][0]
|
|
|
|
try:
|
|
|
|
if service.is_running(svc_name):
|
|
|
|
print "%s Service: RUNNING" % svc
|
|
|
|
else:
|
|
|
|
print "%s Service: STOPPED" % svc
|
|
|
|
except:
|
|
|
|
print "Failed to get %s Service status" % svc
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
safe_options, options, args = parse_options()
|
|
|
|
|
|
|
|
if len(args) != 1:
|
|
|
|
sys.exit("You must specify one action")
|
|
|
|
elif args[0] != "start" and args[0] != "stop" and args[0] != "restart" and args[0] != "status":
|
|
|
|
sys.exit("Unrecognized action [" + args[0] + "]")
|
|
|
|
|
|
|
|
api.bootstrap(context='cli', debug=options.debug)
|
|
|
|
api.finalize()
|
|
|
|
|
2010-12-10 16:40:37 -06:00
|
|
|
serverid = dsinstance.realm_to_serverid(api.env.realm)
|
|
|
|
|
2010-12-04 14:42:14 -06:00
|
|
|
if args[0].lower() == "start":
|
2010-12-10 16:40:37 -06:00
|
|
|
ipa_start(serverid)
|
2010-12-04 14:42:14 -06:00
|
|
|
elif args[0].lower() == "stop":
|
2010-12-10 16:40:37 -06:00
|
|
|
ipa_stop(serverid)
|
2010-12-04 14:42:14 -06:00
|
|
|
elif args[0].lower() == "restart":
|
2010-12-10 16:40:37 -06:00
|
|
|
ipa_restart(serverid)
|
2010-12-04 14:42:14 -06:00
|
|
|
elif args[0].lower() == "status":
|
2010-12-10 16:40:37 -06:00
|
|
|
ipa_status(serverid)
|
2010-12-04 14:42:14 -06:00
|
|
|
|
|
|
|
try:
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|
|
|
|
except RuntimeError, e:
|
|
|
|
print "%s" % e
|
|
|
|
sys.exit(1)
|
|
|
|
except SystemExit, e:
|
|
|
|
sys.exit(e)
|
|
|
|
except KeyboardInterrupt, e:
|
|
|
|
sys.exit(1)
|