ryantimwilson / rpms / systemd

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