dcavalca / rpms / systemd

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