[Cluster-devel] conga/luci/utils luci_admin

rmccabe at sourceware.org rmccabe at sourceware.org
Fri Jun 16 05:35:23 UTC 2006


CVSROOT:	/cvs/cluster
Module name:	conga
Changes by:	rmccabe at sourceware.org	2006-06-16 05:35:22

Modified files:
	luci/utils     : luci_admin 

Log message:
	85% restore. i hate zope.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/luci/utils/luci_admin.diff?cvsroot=cluster&r1=1.7&r2=1.8

--- conga/luci/utils/luci_admin	2006/06/14 20:59:37	1.7
+++ conga/luci/utils/luci_admin	2006/06/16 05:35:22	1.8
@@ -7,9 +7,10 @@
 	'/usr/lib64/zope/lib/python/Products',
 	'/usr/lib/zope/lib/python',
 	'/usr/lib/zope/lib/python/Products'
-));
+))
 
 from sys import stderr, argv
+import ZODB
 from ZODB.FileStorage import FileStorage
 from ZODB.DB import DB
 import xml
@@ -17,6 +18,20 @@
 from xml.dom import minidom
 import types
 
+import Zope2
+import OFS
+import App
+import App.Extensions
+import OFS.Application
+from App.Extensions import *
+from OFS.Application import AppInitializer
+import OFS.Folder
+import AccessControl
+import AccessControl.User
+from AccessControl.SecurityManagement import newSecurityManager
+import Products.CMFCore
+import Products.CMFCore.MemberDataTool
+import transaction
 
 LUCI_USER='luci'
 LUCI_GROUP='luci'
@@ -30,10 +45,152 @@
 INITUSER_FILE_PATH = '/var/lib/luci/inituser'
 
 
+examine_classes = [
+	'OFS.Folder.Folder',
+	'AccessControl.User.User',
+	'Products.CMFCore.MemberDataTool.MemberData'
+]
 
 def luci_restore(argv):
-	print "TODO: implement me"
-	return 1
+	if len(argv) > 0:
+		dbfn = argv[0]
+	else:
+		dbfn = LUCI_DB_PATH
+
+	if len(argv) > 1:
+		backupfn = argv[1]
+	else:
+		backupfn = LUCI_BACKUP_PATH
+
+	try:
+		fs = FileStorage(dbfn)
+		db = DB(fs)
+		db.pack()
+		conn = db.open()
+	except:
+		stderr.write('Unable to open the luci database \"' + dbfn + '\"\n')
+		sys.exit(1)
+
+	try:
+		node = xml.dom.minidom.parse(backupfn)
+	except:
+		stderr.write('Unable to open the luci backup file \"' + backupfn + '\"\n')
+		sys.exit(1)
+
+	if node.firstChild.nodeName != 'luci':
+		'The backup file \"' + backupfn + '\" is not in the expected format (expected <luci>)\n'
+		sys.exit(1)
+
+	if not node.getElementsByTagName('backupData'):
+		'The backup file \"' + backupfn + '\" is not in the expected format (expected <luci>)\n'
+		sys.exit(1)
+
+	tempuser = AccessControl.User.UnrestrictedUser('admin', '',
+				('manage','Manager', 'Owner', 'View', 'Authenticated'), [])
+	newSecurityManager(None, tempuser)
+
+	app = conn.root()['Application']
+	AppInitializer(app).initialize()
+
+	portal_mem = app.unrestrictedTraverse('/luci/portal_membership')
+	userList = node.getElementsByTagName('user')
+	for u in userList:
+		id = u.getAttribute('id')
+		if not id:
+			sys.stderr.write('Missing ID for user')
+			continue
+		id = str(id)
+
+		passwd = u.getAttribute('passwd')
+		if not passwd:
+			sys.stderr.write('Missing password for user \"' + id + '\"')
+			continue
+		passwd = str(passwd)
+
+		email = u.getAttribute('email')
+		if not email:
+			email = id + '@luci.example.org'
+		else:
+			email = str(email)
+		if id != 'admin':
+			props = {
+				'username': id,
+				'roles': [ 'Member' ],
+				'password': 'changeme',
+				'confirm': 'changeme',
+				'domains': [],
+				'email': email
+			}
+
+			portal_reg.addMember(id, 'changeme', props)
+			member = portal_mem.getMemberById(id)
+			if not member:
+				transaction.abort()
+				sys.stderr.write('Error adding user \"' + id + '\"')
+			#transaction.commit()
+
+	x = app.unrestrictedTraverse('/luci/systems/storage')
+	systemList = node.getElementsByTagName('system')
+	for s in systemList:
+		id = s.getAttribute('id')
+		if not id:
+			continue
+		id = str(id)
+		title = str(s.getAttribute('title'))
+
+		x.manage_addFolder(id)
+		new_system = app.unrestrictedTraverse('/luci/systems/storage/' + id)
+
+		userPerms = s.getElementsByTagName('permList')[0].childNodes
+		for i in userPerms:
+			if i.nodeType != xml.dom.Node.ELEMENT_NODE:
+				continue
+			newuser = i.getAttribute('name')
+			if not newuser:
+				continue
+			new_system.setLocalRoles(newuser, ['View'])
+
+		#transaction.commit()
+
+	x = app.unrestrictedTraverse('/luci/systems/cluster')
+	clusterList = node.getElementsByTagName('cluster')
+	for c in clusterList:
+		id = c.getAttribute('id')
+		if not id:
+			continue
+		id = str(id)
+		title = str(c.getAttribute('title'))
+
+		new_cluster = app.unrestrictedTraverse('/luci/systems/cluster/' + id)
+		if not new_cluster:
+			print 'error adding cluster',id
+			tranaction.abort()
+			sys.exit(1)
+
+		userPerms = c.getElementsByTagName('permList')[0].childNodes
+		for i in userPerms:
+			if i.nodeType != xml.dom.Node.ELEMENT_NODE:
+				continue
+			newuser = i.getAttribute('name')
+			if not newuser:
+				continue
+			new_cluster.setLocalRoles(newuser, ['View'])
+
+		clusterSystems = c.getElementsByTagName('systemsList')[0].childNodes
+		for i in clusterSystems:
+			if i.nodeType != xml.dom.Node.ELEMENT_NODE:
+				continue
+			newsys = i.getAttribute('name')
+			if not newsys:
+				continue
+			newsys = str(newsys)
+			x.manage_addFolder(newsys)
+
+	transaction.abort()
+	conn.close()
+	db.pack()
+	db.close()
+	fs.close()
 
 # This function's ability to work is dependent
 # upon the structure of @dict
@@ -79,10 +236,6 @@
 	clusters = {}
 	users = {}
 
-	examine_classes = [ 'OFS.Folder.Folder',
-						'AccessControl.User.User',
-						'Products.CMFCore.MemberDataTool.MemberData' ]
-
 	if len(argv) > 1:
 		backupfn = argv[1]
 	else:
@@ -100,27 +253,32 @@
 	# kinds of objects.
 	temp = sys.stderr
 	null = file('/dev/null')
-	sys.stderr = null
 
 	next_oid = None
 	while True:
 		oid, tid, data, next_oid = fs.record_iternext(next_oid)
 
+		sys.stderr = null
 		try:
 			obj = conn.get(oid)
 			obj_class = str(type(obj)).split('\'')[1]
 		except:
 			continue
+		sys.stderr = temp
 
 		if not obj_class in examine_classes:
 			continue
 
+		# for some reason, when stderr is dumped to /dev/null
+		# something goes wrong here, which is why it's set and restored
+		# inside the loop.
 		try:
 			conn.setstate(obj)
-			dict = obj.__dict__
 		except:
 			continue
 
+		dict = obj.__dict__
+
 		if obj_class == 'OFS.Folder.Folder':
 			if not 'title' in dict or dict['title'][0:9] != '__luci__:':
 				continue
@@ -157,21 +315,34 @@
 				roles = dict['__ac_local_roles__']
 				for i in roles:
 					if not i in users:
-						users[i] = { 'id': i }
+						users[i] = { 'id': i, 'name': i }
 					if 'View' in roles[i]:
 						cur['permList'].append(i)
 		elif obj_class == 'AccessControl.User.User':
-			if not dict['name'] in users:
-				users[dict['name']] = {}
-			cur_user = users[dict['name']]
+			try:
+				cur_user = users[dict['name']]
+			except:
+				try:
+					cur_user = users[dict['id']]
+				except:
+					users[dict['name']] = {}
+					cur_user = users[dict['name']]
+					cur_user['id'] = dict['name']
 			cur_user['name'] = dict['name']
 			cur_user['passwd'] = dict['__']
 		elif obj_class == 'Products.CMFCore.MemberDataTool.MemberData':
-			if not 'id' in dict:
+			if not 'id' in dict and not 'name' in dict:
 				continue
-			if not dict['id'] in users:
-				users[dict['id']] = {}
-			cur_user = users[dict['id']]
+
+			try:
+				cur_user = users[dict['id']]
+			except:
+				try:
+					cur_user = users[dict['name']]
+				except:
+					users[dict['id']] = {}
+					cur_user = users[dict['id']]
+					cur_user['name'] = dict['id']
 			for i in dict:
 				cur_user[i] = dict[i]
 		if next_oid is None:
@@ -180,8 +351,20 @@
 	null.close()
 	conn.close()
 	db.close()
+	fs.close()
 
-	backup = {'systemList': systems, 'clusterList': clusters, 'userList': users }
+	junk = list()
+	for i in users:
+		if not 'passwd' in users[i]:
+			junk.append(i)
+	for i in junk:
+		del users[i]
+
+	backup = {
+		'systemList': systems,
+		'clusterList': clusters,
+		'userList': users
+	}
 
 	doc = xml.dom.minidom.Document()
 	luciData = doc.createElement('luci')
@@ -469,7 +652,7 @@
     return
 
 
-def help(argv):
+def luci_help(argv):
     print 'Usage:'
     print argv[0] + ' [init|backup|restore|password|help]'
     print
@@ -484,7 +667,7 @@
 
 def main(argv):
     if len(argv) != 2:
-        help(argv)
+        luci_help(argv)
         sys.exit(1)
         pass
     
@@ -506,11 +689,11 @@
     elif 'password' in argv:
         password(argv)
     elif 'help' in argv:
-        help(argv)
+        luci_help(argv)
     else:
         print 'Unknown command'
         print
-        help(argv)
+        luci_help(argv)
         sys.exit(1)
 
         




More information about the Cluster-devel mailing list