X-Git-Url: http://git.onelab.eu/?p=sface.git;a=blobdiff_plain;f=sface%2Fscreens%2Fmainscreen.py;h=985e3154e05b7d6122fefc89c307099dbd4388a8;hp=b20ae516c414e0e70d5fe25d226e503c4662f617;hb=03b467d0bf99663f894553af50265816bd28027e;hpb=9be93c1e7a8a2a4e0cd1c890e9ebe2acf6030787 diff --git a/sface/screens/mainscreen.py b/sface/screens/mainscreen.py index b20ae51..985e315 100644 --- a/sface/screens/mainscreen.py +++ b/sface/screens/mainscreen.py @@ -1,15 +1,16 @@ import datetime import os +import urlparse from PyQt4.QtCore import * from PyQt4.QtGui import * #from sfa.util.rspecHelper import RSpec -from sfa.rspecs.rspec_parser import parse_rspec from sface.config import config -from sface.sfirenew import SfiRenewer +from sface.sfirenew import RenewWindow from sface.sfiprocess import SfiProcess from sface.screens.sfascreen import SfaScreen +from sface.sfidata import SfiData already_in_nodes = [] @@ -23,6 +24,10 @@ tag_status = { "in": "Already Set", "add": "To be Added", "remove": "To be Removed"} +color_status = { "in": QColor.fromRgb(0, 250, 250), + "add": QColor.fromRgb(0, 250, 0), + "remove": QColor.fromRgb(250, 0, 0) } + default_tags = "Default tags" settable_tags = ['delegations', 'initscript'] @@ -31,6 +36,9 @@ NODE_STATUS_COLUMN = 1 MEMBERSHIP_STATUS_COLUMN = 2 KIND_COLUMN = 3 +# maximum length of a name to display before clipping +NAME_MAX_LEN = 48 + def itemType(index): if index.parent().parent().isValid(): return "tag" @@ -51,7 +59,16 @@ class NodeView(QTreeView): self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.setToolTip("Double click on a row to change its status. Right click on a host to add a tag.") + def keyPressEvent(self, event): + if (event.key() == Qt.Key_Space): + self.toggleSelection() + else: + QTreeView.keyPressEvent(self, event) + def mouseDoubleClickEvent(self, event): + self.toggleSelection() + + def toggleSelection(self): index = self.currentIndex() model = index.model() status_index = model.index(index.row(), MEMBERSHIP_STATUS_COLUMN, index.parent()) @@ -124,7 +141,7 @@ class NodeView(QTreeView): tagstring = QString("%s: %s" % (tagname, value)) tagItem = QStandardItem(tagstring) status = QStandardItem(QString(tag_status['add'])) - nodeItem.appendRow([tagItem, status]) + nodeItem.appendRow([tagItem, QStandardItem(QString("")), status]) elif status_data in (node_status['out'], node_status['remove']): QMessageBox.warning(self, "Not selected", "Can only add tags to selected nodes") @@ -144,15 +161,21 @@ class NodeNameDelegate(QStyledItemDelegate): def __init__(self, parent): QStyledItemDelegate.__init__(self, parent) + def displayText(self, value, locale): + data = str(QStyledItemDelegate.displayText(self, value, locale)) + if (len(data)>NAME_MAX_LEN): + data = data[:(NAME_MAX_LEN-3)] + "..." + return QString(data) + def paint(self, painter, option, index): model = index.model() + data = str(self.displayText(index.data(), QLocale())) status_index = model.index(index.row(), MEMBERSHIP_STATUS_COLUMN, index.parent()) status_data = status_index.data().toString() fm = QFontMetrics(option.font) rect = QRect(option.rect) - data = index.data().toString() rect.setHeight(rect.height() - 2) rect.setWidth(fm.width(QString(data)) + 6) rect.setX(rect.x() + 5) @@ -170,44 +193,20 @@ class NodeNameDelegate(QStyledItemDelegate): painter.fillRect(option.rect, option.palette.color(QPalette.Active, QPalette.Highlight)) if itemType(index) == "node": - if status_data == node_status['in']: # already in the slice - painter.fillPath(path, QColor.fromRgb(0, 250, 250)) - painter.setPen(QColor.fromRgb(0, 0, 0)) - painter.drawText(rect, 0, QString(data)) + for x in node_status.keys(): + if (node_status[x] == status_data) and (x in color_status): + painter.fillPath(path, color_status[x]) - elif status_data == node_status['add']: # newly added to the slice - painter.fillPath(path, QColor.fromRgb(0, 250, 0)) - painter.setPen(QColor.fromRgb(0, 0, 0)) - painter.drawText(rect, 0, QString(data)) - - elif status_data == node_status['remove']: # removed from the slice - painter.fillPath(path, QColor.fromRgb(250, 0, 0)) - painter.setPen(QColor.fromRgb(0, 0, 0)) - painter.drawText(rect, 0, QString(data)) - - else: - painter.setPen(QColor.fromRgb(0, 0, 0)) - painter.drawText(rect, 0, QString(data)) + painter.setPen(QColor.fromRgb(0, 0, 0)) + painter.drawText(rect, 0, QString(data)) else: - if status_data == tag_status['in']: # already in the slice - painter.fillPath(path, QColor.fromRgb(0, 250, 250)) - painter.setPen(QColor.fromRgb(0, 0, 0)) - painter.drawText(rect, 0, QString(data)) + for x in tag_status.keys(): + if (tag_status[x] == status_data) and (x in color_status): + painter.fillPath(path, color_status[x]) - elif status_data == tag_status['add']: # newly added to the slice - painter.fillPath(path, QColor.fromRgb(0, 250, 0)) - painter.setPen(QColor.fromRgb(0, 0, 0)) - painter.drawText(rect, 0, QString(data)) - - elif status_data == tag_status['remove']: # removed from the slice - painter.fillPath(path, QColor.fromRgb(250, 0, 0)) - painter.setPen(QColor.fromRgb(0, 0, 0)) - painter.drawText(rect, 0, QString(data)) - - else: - painter.setPen(QColor.fromRgb(0, 0, 0)) - painter.drawText(rect, 0, QString(data)) + painter.setPen(QColor.fromRgb(0, 0, 0)) + painter.drawText(rect, 0, QString(data)) painter.restore() @@ -339,7 +338,7 @@ class SliceWidget(QWidget): self.connect(refresh, SIGNAL('clicked()'), self.refresh) self.connect(renew, SIGNAL('clicked()'), self.renew) - self.connect(submit, SIGNAL('clicked()'), self.submit) + self.connect(submit, SIGNAL('clicked()'), self.submit_pg_compat) self.connect(searchbox, SIGNAL('textChanged(QString)'), self.search) self.connect(filterbox, SIGNAL('currentIndexChanged(QString)'), self.filter) self.connect(self.nodeView, SIGNAL('hostnameClicked(QString)'), @@ -348,20 +347,39 @@ class SliceWidget(QWidget): self.updateView() def submitFinished(self): - self.setStatus("Slice data submitted.") - QTimer.singleShot(1000, self.refresh) + self.disconnect(self.process, SIGNAL('finished()'), self.submitFinished) + + faultString = self.process.getFaultString() + if not faultString: + self.setStatus("Slice data submitted.") + else: + self.setStatus("Slice submit failed: %s" % (faultString)) - def refreshFinished(self): - self.setStatus("Slice data refreshed.", timeout=5000) self.updateView() self.parent().signalAll("rspecUpdated") - def readSliceRSpec(self): - rspec_file = config.getSliceRSpecFile() - if os.path.exists(rspec_file): - xml = open(rspec_file).read() - return parse_rspec(xml) - return None + def refreshResourcesFinished(self): + self.disconnect(self.process, SIGNAL('finished()'), self.refreshResourcesFinished) + + faultString = self.process.getFaultString() + if not faultString: + self.setStatus("Refreshing slice RSpec.") + self.connect(self.process, SIGNAL('finished()'), self.refreshRSpecFinished) + self.process.retrieveRspec() + else: + self.setStatus("Resources refresh failed: %s" % (faultString)) + + def refreshRSpecFinished(self): + self.disconnect(self.process, SIGNAL('finished()'), self.refreshRSpecFinished) + + faultString = self.process.getFaultString() + if not faultString: + self.setStatus("Slice data refreshed.", timeout=5000) + else: + self.setStatus("Slice refresh failed: %s" % (faultString)) + + self.updateView() + self.parent().signalAll("rspecUpdated") def setStatus(self, msg, timeout=None): self.parent().setStatus(msg, timeout) @@ -380,13 +398,13 @@ class SliceWidget(QWidget): def itemStatus(self, item): statusItem = item.parent().child(item.row(), MEMBERSHIP_STATUS_COLUMN) - return statusItem.data(Qt.DisplayRole).toString() + return str(statusItem.data(Qt.DisplayRole).toString()) def itemText(self, item): - return item.data(Qt.DisplayRole).toString() + return str(item.data(Qt.DisplayRole).toString()) # Recursively walk the tree, making changes to the RSpec - def process_subtree(self, rspec, item, depth = 0): + def process_subtree(self, rspec, resources, item, depth = 0): change = False model = self.nodeModel @@ -398,11 +416,18 @@ class SliceWidget(QWidget): status = self.itemStatus(item) if status == node_status['add']: print "Add hostname: %s" % hostname - rspec.add_slivers(str(hostname), testbed) - change = True + + resource_node = resources.get_node_element(hostname) + + if resource_node==None: + print "Error: Failed to find %s in resources rspec" % hostname + else: + rspec.merge_node(resource_node, testbed) + rspec.add_slivers([{"hostname": str(hostname)}], testbed) + change = True elif status == node_status['remove']: print "Remove hostname: %s" % hostname - rspec.remove_slivers(str(hostname), testbed) + rspec.remove_slivers([{"hostname": str(hostname)}], testbed) change = True elif depth == 3: # Tag tag, value = self.itemText(item).split(": ") @@ -413,14 +438,14 @@ class SliceWidget(QWidget): testbed = self.itemText(item.parent().parent()) if status == tag_status['add']: print "Add tag to (%s, %s): %s/%s " % (testbed, node, tag, value) - if node.startsWith(default_tags): + if node.startswith(default_tags): rspec.add_default_sliver_attribute(tag, value, testbed) else: rspec.add_sliver_attribute(node, tag, value, testbed) change = True elif status == tag_status['remove']: print "Remove tag from (%s, %s): %s/%s " % (testbed, node, tag, value) - if node.startsWith(default_tags): + if node.startswith(default_tags): rspec.remove_default_sliver_attribute(tag, value, testbed) else: rspec.remove_sliver_attribute(node, tag, value, testbed) @@ -428,7 +453,7 @@ class SliceWidget(QWidget): children = item.rowCount() for row in range(0, children): - status = self.process_subtree(rspec, item.child(row), depth + 1) + status = self.process_subtree(rspec, resources, item.child(row), depth + 1) change = change or status return change @@ -437,34 +462,102 @@ class SliceWidget(QWidget): if self.checkRunningProcess(): return - rspec = self.readSliceRSpec() - change = self.process_subtree(rspec, self.nodeModel.invisibleRootItem()) + rspec = SfiData().getSliceRSpec() + resources = SfiData().getResourcesRSpec() + change = self.process_subtree(rspec, resources, self.nodeModel.invisibleRootItem()) if not change: self.setStatus("No change in slice data. Not submitting!", timeout=3000) return - self.disconnect(self.process, SIGNAL('finished()'), self.refreshFinished) - self.connect(self.process, SIGNAL('finished()'), self.submitFinished) + # Several aggregates have issues with the section in the + # rspec, so make sure it's not there. + stats_elems = rspec.xml.xpath("//statistics") + if len(stats_elems)>0: + stats_elem = stats_elems[0] + parent = stats_elem.xpath("..")[0] + parent.remove(stats_elem) + self.connect(self.process, SIGNAL('finished()'), self.submitFinished) self.process.applyRSpec(rspec) self.setStatus("Sending slice data (RSpec). This will take some time...") - def renew(self): - dlg = RenewWindow(parent=self) - if (dlg.exec_() == QDialog.Accepted): - self.setStatus("Renewing Slice.") + # ProtoGENI-compatible submit. Contact each aggregate individually rather + # than using the slice manager. + # This code will be removed when ProtoGENI slicemanager is patched. + def submit_pg_compat(self): + if self.checkRunningProcess(): + return + + rspec = SfiData().getSliceRSpec() + resources = SfiData().getResourcesRSpec() + change = self.process_subtree(rspec, resources, self.nodeModel.invisibleRootItem()) - self.renewProcess = SfiRenewer(config.getSlice(), dlg.get_new_expiration(), self) - self.connect(self.renewProcess, SIGNAL('finished()'), self.renewFinished) + if not change: + self.setStatus("No change in slice data. Not submitting!", timeout=3000) + return - def renewFinished(self): - if self.renewProcess.statusMsg: - self.setStatus("Renew " + self.renewProcess.status + ": " + self.renewProcess.statusMsg) + # Several aggregates have issues with the section in the + # rspec, so make sure it's not there. + stats_elems = rspec.xml.xpath("//statistics") + if len(stats_elems)>0: + stats_elem = stats_elems[0] + parent = stats_elem.xpath("..")[0] + parent.remove(stats_elem) + + self.submit_aggSuccessCount = 0 + self.submit_aggFailCount = 0 + self.submit_rspec = rspec + self.connect(self.process, SIGNAL('finished()'), self.getVersionFinished) + self.process.getSliceMgrVersion() + self.setStatus("Getting aggregate directory...") + + def getVersionFinished(self): + self.disconnect(self.process, SIGNAL('finished()'), self.getVersionFinished) + + faultString = self.process.getFaultString() + if not faultString: + peers = SfiData().getSliceMgrVersion()["peers"] + self.submit_aggs = [(key, peers[key]) for key in peers.keys()] + str = "Successfully retrieved agg list. " + self.submitNextAgg(str) else: - self.setStatus("Renew " + self.renewProcess.status) - self.disconnect(self.renewProcess, SIGNAL('finished()'), self.renewFinished) - self.renewProcess = None + self.setStatus("getSliceMgrVersion failed: %s" % (faultString)) + + def submitNextAgg(self, statusStr=""): + if (self.submit_aggs == []): + self.setStatus(statusStr + "Finished submitting. %d/%d aggs succeeded." % + (self.submit_aggSuccessCount,self.submit_aggSuccessCount+self.submit_aggFailCount)) + QTimer.singleShot(2500, self.refresh) + return + + self.submit_agg = self.submit_aggs.pop() + + urlParts = urlparse.urlsplit(self.submit_agg[1]) + amPort = urlParts.port + amAddr = urlParts.hostname+urlParts.path + + self.setStatus(statusStr + "Submitting to %s..." % (self.submit_agg[0])) + + self.connect(self.process, SIGNAL('finished()'), self.submitNextAggFinished) + self.process.applyRSpec(self.submit_rspec, aggAddr = amAddr, aggPort = amPort, saveObtained=False) + + def submitNextAggFinished(self): + self.disconnect(self.process, SIGNAL('finished()'), self.submitNextAggFinished) + + faultString = self.process.getFaultString() + if not faultString: + self.submit_aggSuccessCount+=1 + str = "Succeeded on %s. " % (self.submit_agg[0]) + else: + self.submit_aggFailCount+=1 + str = "Failed on %s. " % (self.submit_agg[0]) # , faultString) + + self.submitNextAgg(str) + + def renew(self): + dlg = RenewWindow(parent=self) + dlg.exec_() def refresh(self): if not config.getSlice(): @@ -475,11 +568,10 @@ class SliceWidget(QWidget): self.setStatus("There is already a process running. Please wait.") return - self.disconnect(self.process, SIGNAL('finished()'), self.submitFinished) - self.connect(self.process, SIGNAL('finished()'), self.refreshFinished) + self.connect(self.process, SIGNAL('finished()'), self.refreshResourcesFinished) - self.process.getRSpecFromSM() - self.setStatus("Refreshing slice data. This will take some time...") + self.process.retrieveResources() + self.setStatus("Refreshing resources. This will take some time...") def updateView(self): global already_in_nodes @@ -487,20 +579,27 @@ class SliceWidget(QWidget): self.network_names = [] self.nodeModel.clear() - rspec = self.readSliceRSpec() + rspec = SfiData().getSliceRSpec() if not rspec: return None + resources = SfiData().getResourcesRSpec() + if not resources: + return None + rootItem = self.nodeModel.invisibleRootItem() - #networks = sorted(rspec.get_network_list()) networks = rspec.get_networks() + + for network in resources.get_networks(): + if not network in networks: + networks.append(network) + for network in networks: self.network_names.append(network) - #all_nodes = rspec.get_node_list(network) - #sliver_nodes = rspec.get_sliver_list(network) - all_nodes = rspec.get_nodes(network) + all_nodes = resources.get_nodes(network) sliver_nodes = rspec.get_nodes_with_slivers(network) + available_nodes = [ node for node in all_nodes if node not in sliver_nodes ] networkItem = QStandardItem(QString(network)) @@ -539,7 +638,7 @@ class SliceWidget(QWidget): for node in available_nodes: nodeItem = QStandardItem(QString(node)) statusItem = QStandardItem(QString(node_status['out'])) - nodeStatus = QStandardItem(QString(rspec.get_node_element(node, network).attrib.get("boot_state",""))) + nodeStatus = QStandardItem(QString(resources.get_node_element(node, network).attrib.get("boot_state",""))) networkItem.appendRow([nodeItem, nodeStatus, statusItem, QStandardItem(QString("node"))]) self.filterModel.setSourceModel(self.nodeModel) @@ -562,44 +661,6 @@ class SliceWidget(QWidget): def nodeSelectionChanged(self, hostname): self.parent().nodeSelectionChanged(hostname) -class RenewWindow(QDialog): - def __init__(self, parent=None): - super(RenewWindow, self).__init__(parent) - self.setWindowTitle("Renew Slivers") - - self.duration = QComboBox() - - self.expirations = [] - - durations = ( (1, "One Week"), (2, "Two Weeks"), (3, "Three Weeks"), (4, "One Month") ) - - now = datetime.datetime.utcnow() - for (weeks, desc) in durations: - exp = now + datetime.timedelta(days = weeks * 7) - desc = desc + " " + exp.strftime("%Y-%m-%d %H:%M:%S") - self.expirations.append(exp) - self.duration.addItem(desc) - - self.duration.setCurrentIndex(0) - - buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) - buttonBox.button(QDialogButtonBox.Ok).setDefault(True) - - layout = QVBoxLayout() - layout.addWidget(self.duration) - layout.addWidget(buttonBox) - self.setLayout(layout) - - self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()")) - self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) - - def accept(self): - QDialog.accept(self) - - def get_new_expiration(self): - index = self.duration.currentIndex() - return self.expirations[index] - class MainScreen(SfaScreen): def __init__(self, parent): SfaScreen.__init__(self, parent)