[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Cluster-devel] conga/luci/utils luci_admin



CVSROOT:	/cvs/cluster
Module name:	conga
Changes by:	rmccabe sourceware org	2006-06-16 23:19:59

Modified files:
	luci/utils     : luci_admin 

Log message:
	

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

--- conga/luci/utils/luci_admin	2006/06/16 19:35:33	1.11
+++ conga/luci/utils/luci_admin	2006/06/16 23:19:58	1.12
@@ -118,7 +118,7 @@
 		if not portal_mem or not portal_reg:
 			raise
 	except:
-		sys.stderr.write('Your Luci installation appears to be corrupt. Please reinstall, then try to restore again.')
+		sys.stderr.write('Your Luci installation appears to be corrupt.')
 		return -1
 
 	userList = node.getElementsByTagName('userList')
@@ -127,6 +127,10 @@
 		return -1
 
 	userList = userList[0].getElementsByTagName('user')
+	if not userList or len(userList) < 1:
+		sys.stderr.write('Your backup file contains no users. At the very least, the admin user must exist. Please check that your backup file is not corrupt.\n')
+		return -1
+	
 	for u in userList:
 		id = u.getAttribute('id')
 		if not id:
@@ -164,7 +168,8 @@
 				return -1
 
 			verbose.write('Added user \"' + id + '\"\n')
-		#transaction.commit()
+		transaction.commit()
+
 
 	try:
 		x = app.unrestrictedTraverse('/luci/systems/storage')
@@ -180,14 +185,20 @@
 		sys.write.stderr('No storage systems to add')
 	else:
 		systemList = systemList[0].getElementsByTagName('system')
+		if len(systemList) < 1:
+			sys.write.stderr('No storage systems to add')
 
 	for s in systemList:
 		id = s.getAttribute('id')
 		if not id:
-			sys.stderr.write('Missing ID for storage system')
-			continue
+			sys.stderr.write('Missing ID for storage system. Your backup may be corrupt.\n')
+			transaction.abort()
+			return -1
 		id = str(id)
-		title = str(s.getAttribute('title'))
+		try:
+			title = str(s.getAttribute('title'))
+		except:
+			title = '__luci__:system'
 
 		x.manage_addFolder(id, title)
 		try:
@@ -201,18 +212,21 @@
 
 		userPerms = s.getElementsByTagName('permList')
 		if not userPerms or len(userPerms) < 1:
+			verbose.write('Added storage system \"' + id + '\"\n')
 			continue
 		userPerms = userPerms[0].getElementsByTagName('ref')
 		for i in userPerms:
-			if i.nodeType != xml.dom.Node.ELEMENT_NODE:
-				continue
 			newuser = i.getAttribute('name')
 			if not newuser:
 				continue
-			new_system.manage_setLocalRoles(newuser, ['View'])
+			try:
+				new_system.manage_setLocalRoles(newuser, ['View'])
+				verbose.write('Added view permission to storage system \"' + id + '\" for \"' + newuser + '\"\n')
+			except:
+				sys.stderr.write('An error occurred while restoring permission for storage system \"' + id + '\" for user \"' + newuser + '\"\n')
 
 		verbose.write('Added storage system \"' + id + '\"\n')
-		#transaction.commit()
+		transaction.commit()
 
 	try:
 		x = app.unrestrictedTraverse('/luci/systems/cluster')
@@ -222,14 +236,28 @@
 		sys.stderr.write('Cannot find the Luci cluster directory. Your Luci installation may be corrupt.')
 		transaction.abort()
 		return -1
-	clusterList = node.getElementsByTagName('cluster')
+
+	clusterList = node.getElementsByTagName('clusterList')
+	if not clusterList or len(clusterList) < 1:
+		verbose('No clusters to add')
+	else:
+		clusterList = clusterList[0].getElementsByTagName('cluster')
+		if len(clusterList) < 1:
+			verbose('No clusters to add')
+
 	for c in clusterList:
 		id = c.getAttribute('id')
 		if not id:
 			sys.stderr.write('Cluster element is missing id\n')
-			continue
+			transaction.abort()
+			return -1
 		id = str(id)
-		title = str(c.getAttribute('title'))
+
+		title = c.getAttribute('title')
+		if not title:
+			title = '__luci__:cluster'
+		else:
+			title = str(title)
 
 		try:
 			x.manage_addFolder(id, title)
@@ -241,49 +269,65 @@
 			transaction.abort()
 			return -1
 
+		viewperm = list()
+
 		userPerms = c.getElementsByTagName('permList')
-		if not userPerms or len(userPerms) < 1:
-			continue
-		userPerms = userPerms[0].getElementsByTagName('ref')
-		for i in userPerms:
-			if i.nodeType != xml.dom.Node.ELEMENT_NODE:
-				continue
-			newuser = i.getAttribute('name')
-			if not newuser:
-				continue
-			new_cluster.manage_setLocalRoles(newuser, ['View'])
+		if userPerms and len(userPerms) > 0:
+			userPerms = userPerms[0].getElementsByTagName('ref')
+			for i in userPerms:
+				newuser = i.getAttribute('name')
+				if not newuser:
+					continue
+				newuser = str(newuser)
 
-		clusterSystems = c.getElementsByTagName('systemsList')
+				try:
+					new_cluster.manage_setLocalRoles(newuser, ['View'])
+					verbose.write('Added view permission to cluster \"' + id + '\" for \"' + newuser + '\"\n')
+				except:
+					sys.stderr.write('An error occurred while restoring permission for cluster \"' + id + '\" for user \"' + newuser + '\"\n')
+				viewperm.append(newuser)
+
+		clusterSystems = c.getElementsByTagName('csystemList')
 		if not clusterSystems or len(clusterSystems) < 1:
 			verbose.write('Cluster \"' + id + '\" has no storage systems\n')
-			continue
-		clusterSystems = clusterSystems[0].getElementsByTagName('ref')
-		for i in clusterSystems:
-			newsys = i.getAttribute('name')
-			if not newsys:
-				sys.stderr.write('Storage system missing name for cluster \"' + id + '\"\n')
-				continue
-			newsys = str(newsys)
-			stitle = i.getAttribute('title')
-			if stitle:
-				stitle = str(stitle)
-			else:
-				stitle = None
-			try:
-				x.manage_addFolder(newsys, stitle)
-				newcs = app.unrestrictedTraverse('/luci/systems/cluster/' + id + '/' + newsys)
-				if not newcs:
-					raise
-			except:
-				sys.stderr.write('An error occurred while restoring the storage system \"' + newsys + '\" for cluster \"' + id + '\"')
-				transaction.abort()
-				return -1
-			verbose.write('Added storage system \"' + newsys + '\" for cluster \"' + id)
-		verbose.write('Added cluster\"' + id + '\"')
-		#transaction.commit()
+		else:
+			clusterSystems = clusterSystems[0].getElementsByTagName('csystem')
+			for i in clusterSystems:
+				newsys = i.getAttribute('id')
+				if not newsys:
+					sys.stderr.write('Storage system missing name for cluster \"' + id + '\"\n')
+					continue
+				newsys = str(newsys)
+				stitle = i.getAttribute('title')
+				if not stitle:
+					stitle = '__luci__:csystem:' + id
+				else:
+					stitle = str(stitle)
+
+				try:
+					x.manage_addFolder(newsys, stitle)
+					newcs = app.unrestrictedTraverse('/luci/systems/cluster/' + id + '/' + newsys)
+					if not newcs:
+						raise
+				except:
+					sys.stderr.write('An error occurred while restoring the storage system \"' + newsys + '\" for cluster \"' + id + '\"\n')
+					transaction.abort()
+					return -1
+				transaction.commit()
+
+				try:
+					for i in viewperm:
+						newcs.manage_setLocalRoles(i, ['View'])
+						verbose.write('Added view permission to cluster system \"' + newsys + '\" for \"' + i + '\"\n')
+				except:
+					sys.stderr.write('An error occurred while restoring permissions for cluster system \"' + newsys + '\" in cluster \"' + id + '\" for user \"' + i + '\"\n')
+					transaction.abort()
+					return -1
+				verbose.write('Added storage system \"' + newsys + '\" for cluster \"' + id + '\"\n')
+		verbose.write('Added cluster \"' + id + '\"\n')
+		transaction.commit()
 
-	#transaction.commit()
-	transaction.abort()
+	transaction.commit()
 	conn.close()
 	db.pack()
 	db.close()
@@ -328,7 +372,7 @@
 		conn = db.open()
 	except:
 		stderr.write('Unable to open the luci database \"' + dbfn + '\"\n')
-		sys.exit(1)
+		return -1
 
 	systems = {}
 	clusters = {}
@@ -339,14 +383,6 @@
 	else:
 		backupfn = LUCI_BACKUP_PATH
 
-	# TODO: this needs to backup the backup, if any, and should
-	# write to a tempfile, then rename.
-	try:
-		f = file(backupfn, 'w')
-	except:
-		sys.stderr.write('Unable to open \"' + backupfn + '\" to write backup.\n')
-		sys.exit(1)
-
 	# To suppress some useless zope error messages.
 	sys.stderr = null
 
@@ -374,27 +410,38 @@
 		dict = obj.__dict__
 
 		if obj_class == 'OFS.Folder.Folder':
-			if not 'title' in dict or dict['title'][0:9] != '__luci__:':
+			if not 'title' in dict or not dict['title'] or dict['title'][0:9] != '__luci__:':
 				if next_oid is None: break
 				else: continue
 			title = dict['title'].split(':')
 			cur = None
 
 			if title[1] == 'cluster':
-				clusters[dict['id']] = {
-					'id': dict['id'],
-					'title': dict['title'],
-					'permList': [],
-					'systemsList:': []
-				}
-				cur = clusters[dict['id']]
+				if not dict['id'] in clusters:
+					clusters[dict['id']] = {
+						'id': dict['id'],
+						'title': dict['title'],
+						'permList': [],
+						'csystemList': {}
+					}
+					cur = clusters[dict['id']]
+				else:
+					cur = clusters[dict['id']]
+					cur['id'] = dict['id']
+					cur['title'] = dict['title']
 			elif title[1] == 'csystem':
 				if len(title) > 2:
 					clusterName = title[2]
-				else:
-					parent = obj.parentNode()
-					clusterName = parent.__dict__['id']
-				clusters[clusterName]['systemsList'].append(dict['id'])
+				if not clusterName in clusters:
+					clusters[clusterName] = {
+						'id': clusterName,
+						'csystemList': {},
+						'permList': []
+					}
+				clusters[clusterName]['csystemList'][dict['id']] = {
+					'id': dict['id'],
+					'title': '__luci__:csystem:' + clusterName,
+				}
 			elif title[1] == 'system':
 				systems[dict['id']] = {
 					'id': dict['id'],
@@ -407,7 +454,7 @@
 				if next_oid is None: break
 				else: continue
 
-			if cur:
+			if cur and '__ac_local_roles__' in dict:
 				roles = dict['__ac_local_roles__']
 				for i in roles:
 					if not i in users:
@@ -446,7 +493,6 @@
 			break
 
 	sys.stderr = orig_stderr
-	null.close()
 	conn.close()
 	db.close()
 	fs.close()
@@ -459,9 +505,9 @@
 		del users[i]
 
 	backup = {
+		'userList': users,
 		'systemList': systems,
-		'clusterList': clusters,
-		'userList': users
+		'clusterList': clusters
 	}
 
 	doc = xml.dom.minidom.Document()
@@ -485,7 +531,7 @@
 		certfile.close()
 	except False:
 		sys.stderr.write('Unable to read ' + SSL_PRIVKEY_PATH + '\n')
-		sys.exit(1)
+		return -1
 
 	try:
 		certfile = file(SSL_PUBKEY_PATH, 'rb')
@@ -503,7 +549,7 @@
 		certfile.close()
 	except:
 		sys.stderr.write('Unable to read ' + SSL_PUBKEY_PATH + '\n')
-		sys.exit(1)
+		return -1
 
 	try:
 		certfile = file(SSL_KEYCONFIG_PATH, 'rb')
@@ -523,6 +569,15 @@
 		sys.stderr.write('Unable to read ' + SSL_KEYCONFIG_PATH + '\n')
 
 	luciData.appendChild(dataNode)
+
+	# TODO: this needs to backup the backup, if any, and should
+	# write to a tempfile, then rename.
+	try:
+		f = file(backupfn, 'w')
+	except:
+		sys.stderr.write('Unable to open \"' + backupfn + '\" to write backup.\n')
+		return -1
+
 	f.write(doc.toprettyxml())
 	f.flush()
 	f.close()
@@ -735,16 +790,22 @@
         sys.exit(1)
         pass
     
-    print 'Backing up Luci server'
-    luci_backup(argv[2:])
-    
+    print 'Backing up the Luci server...'
+
+    if luci_backup(argv[2:]):
+        sys.write.stderr('The Luci backup failed.\n')
+        sys.exit(1)
+    print 'Backup was successful. The backup file is',LUCI_BACKUP_PATH
     return
 
 
 def restore(argv):
-    print 'Restoring Luci server'
+    print 'Restoring the Luci server...'
     
-    luci_restore(argv[2:])
+    if luci_restore(argv[2:]):
+        print 'The Luci restore failed. Try reinstalling Luci, then restoring again.'
+        sys.exit(1)
+
     restart_message()
     
     return


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]