ryantimwilson / rpms / systemd

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