dryang / rpms / systemd

Forked from rpms/systemd a year ago
Clone
923a60
#!/usr/bin/python
923a60
# -*- Mode: Python; python-indent: 8; indent-tabs-mode: t -*-
923a60
923a60
import sys, os, argparse, errno
923a60
923a60
def find_service(service, runlevel):
923a60
	priority = -1
923a60
923a60
	for l in os.listdir("/etc/rc%i.d" % runlevel):
923a60
		if len(l) < 4:
923a60
			continue
923a60
923a60
		if l[0] != 'S' or l[3:] != service:
923a60
			continue
923a60
923a60
		p = int(l[1:3])
923a60
923a60
		if p >= 0 and p <= 99 and p >= priority:
923a60
			priority = p;
923a60
923a60
	return priority
923a60
923a60
def lookup_database(services):
923a60
	try:
923a60
		database = open("/var/lib/systemd/sysv-convert/database", "r")
923a60
	except IOError, e:
923a60
		if e.errno != errno.ENOENT:
923a60
			raise e
923a60
923a60
		return {}
923a60
923a60
	found = {}
923a60
	k = 0
923a60
923a60
	for line in database:
923a60
		service, r, p = line.strip().split("\t", 3)
923a60
		k += 1
923a60
923a60
		try:
923a60
			runlevel = int(r)
923a60
			priority = int(p)
923a60
		except ValueError, e:
923a60
			sys.stderr.write("Failed to parse database line %i. Ignoring." % k)
923a60
			continue
923a60
923a60
		if runlevel not in (2, 3, 4, 5):
923a60
			sys.stderr.write("Runlevel out of bounds in database line %i. Ignoring." % k)
923a60
			continue
923a60
923a60
		if priority < 0 or priority > 99:
923a60
			sys.stderr.write("Priority out of bounds in database line %i. Ignoring." % k)
923a60
			continue
923a60
923a60
		if service not in services:
923a60
			continue
923a60
923a60
		if service not in found:
923a60
			found[service] = {}
923a60
923a60
		if runlevel not in found[service] or found[service][runlevel] < priority:
923a60
			found[service][runlevel] = priority
923a60
923a60
	return found
923a60
923a60
def mkdir_p(path):
923a60
	try:
923a60
		os.makedirs(path, 0755)
923a60
	except OSError, e:
923a60
		if e.errno != errno.EEXIST:
923a60
			raise e
923a60
923a60
if os.geteuid() != 0:
923a60
	sys.stderr.write("Need to be root.\n")
923a60
	sys.exit(1)
923a60
923a60
parser = argparse.ArgumentParser(description='Save and Restore SysV Service Runlevel Information')
923a60
923a60
parser.add_argument('services', metavar='SERVICE', type=str, nargs='+',
923a60
		    help='Service names')
923a60
923a60
parser.add_argument('--save', dest='save', action='store_const',
923a60
		    const=True, default=False,
923a60
		    help='Save SysV runlevel information for one or more services')
923a60
923a60
parser.add_argument('--show', dest='show', action='store_const',
923a60
		    const=True, default=False,
923a60
		    help='Show saved SysV runlevel information for one or more services')
923a60
923a60
parser.add_argument('--apply', dest='apply', action='store_const',
923a60
		    const=True, default=False,
923a60
		    help='Apply saved SysV runlevel information for one or more services to systemd counterparts')
923a60
923a60
a = parser.parse_args()
923a60
923a60
if a.save:
923a60
	for service in a.services:
923a60
		if not os.access("/etc/rc.d/init.d/%s" % service, os.F_OK):
923a60
			sys.stderr.write("SysV service %s does not exist.\n" % service)
923a60
			sys.exit(1)
923a60
923a60
	mkdir_p("/var/lib/systemd/sysv-convert")
923a60
	database = open("/var/lib/systemd/sysv-convert/database", "a")
923a60
923a60
	for runlevel in (2, 3, 4, 5):
923a60
		priority = find_service(service, runlevel)
923a60
923a60
		if priority >= 0:
923a60
			database.write("%s\t%s\t%s\n" % (service, runlevel, priority))
923a60
923a60
elif a.show:
923a60
	found = lookup_database(a.services)
923a60
923a60
	if len(found) <= 0:
923a60
		sys.stderr.write("No information about passed services found.\n")
923a60
		sys.exit(1)
923a60
923a60
	for service, data in found.iteritems():
923a60
		for runlevel, priority in data.iteritems():
923a60
			sys.stdout.write("SysV service %s enabled in runlevel %s at priority %s\n" % (service, runlevel, priority))
923a60
923a60
elif a.apply:
923a60
	for service in a.services:
923a60
		if not os.access("/lib/systemd/system/%s.service" % service, os.F_OK):
923a60
			sys.stderr.write("systemd service %s.service does not exist.\n" % service)
923a60
			sys.exit(1)
923a60
923a60
	found = lookup_database(a.services)
923a60
923a60
	if len(found) <= 0:
923a60
		sys.stderr.write("No information about passed services found.\n")
923a60
		sys.exit(1)
923a60
923a60
	for service, data in found.iteritems():
923a60
		for runlevel in data.iterkeys():
923a60
923a60
			sys.stderr.write("ln -sf /lib/systemd/system/%s.service /etc/systemd/system/runlevel%i.target.wants/%s.service\n" % (service, runlevel, service))
923a60
923a60
			mkdir_p("/etc/systemd/system/runlevel%i.target.wants" % runlevel)
923a60
923a60
			try:
923a60
				os.symlink("/lib/systemd/system/%s.service" % service,
923a60
					   "/etc/systemd/system/runlevel%i.target.wants/%s.service" % (runlevel, service))
923a60
			except OSError, e:
923a60
				if e.errno != errno.EEXIST:
923a60
					raise e
923a60
923a60
else:
923a60
	parser.print_help()