richardphibel / rpms / systemd

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