naccyde / rpms / systemd

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