From c0a56be0dda3ebbc51fd3be4fb15d53aef66c1a2 Mon Sep 17 00:00:00 2001 From: Tony Mack Date: Mon, 14 Jun 2010 16:08:21 +0000 Subject: [PATCH] adding new client cli tools --- setup.py | 6 + sfa/client/sfiAddAttribute.py | 36 ++++ sfa/client/sfiAddSliver.py | 22 +++ sfa/client/sfiDeleteAttribute.py | 37 ++++ sfa/client/sfiDeleteSliver.py | 22 +++ sfa/client/sfiListNodes.py | 17 ++ sfa/client/sfiListSlivers.py | 28 +++ sfa/util/rspecHelper.py | 292 +++++++++++++++++++++++++++++++ 8 files changed, 460 insertions(+) create mode 100755 sfa/client/sfiAddAttribute.py create mode 100755 sfa/client/sfiAddSliver.py create mode 100755 sfa/client/sfiDeleteAttribute.py create mode 100755 sfa/client/sfiDeleteSliver.py create mode 100755 sfa/client/sfiListNodes.py create mode 100755 sfa/client/sfiListSlivers.py create mode 100755 sfa/util/rspecHelper.py diff --git a/setup.py b/setup.py index 932ef0a7..f6936d60 100755 --- a/setup.py +++ b/setup.py @@ -23,6 +23,12 @@ bins = [ 'sfa/client/getRecord.py', 'sfa/client/setRecord.py', 'sfa/client/sfadump.py', + 'sfa/client/sfiAddAttribute.py', + 'sfa/client/sfiAddSliver.py', + 'sfa/client/sfiDeleteAttribute.py', + 'sfa/client/sfiDeleteSliver.py', + 'sfa/client/sfiListNodes.py', + 'sfa/client/sfiListSlivers.py', 'sfatables/sfatables', ] diff --git a/sfa/client/sfiAddAttribute.py b/sfa/client/sfiAddAttribute.py new file mode 100755 index 00000000..33b94a3a --- /dev/null +++ b/sfa/client/sfiAddAttribute.py @@ -0,0 +1,36 @@ +#! /usr/bin/env python + +import sys +from sfa.util.rspecHelper import RSpec, Commands + +command = Commands(usage="%prog [options] [node1 node2...]", + description="Add sliver attributes to the RSpec. " + + "This command reads in an RSpec and outputs a modified " + + "RSpec. Use this to add attributes to individual nodes " + + "in your slice. If no nodes are specified, the " + + "attributes will be added to ALL nodes.", + epilog="NOTE: Only admins can actually set these " + + "attributes, with the exception of --delegations") +command.add_nodefile_option() +command.add_attribute_options() +command.prep() + +attrs = command.get_attribute_dict() +for name in attrs: + for value in attrs[name]: + if not command.nodes: + try: + command.rspec.add_default_sliver_attribute(name, value) + except: + print >> sys.stderr, "FAILED: on all nodes: %s=%s" % (name, value) + else: + for node in command.nodes: + try: + command.rspec.add_sliver_attribute(node, name, value) + except: + print >> sys.stderr, "FAILED: on node %s: %s=%s" % (node, name, value) + +print command.rspec + + + diff --git a/sfa/client/sfiAddSliver.py b/sfa/client/sfiAddSliver.py new file mode 100755 index 00000000..be1c51c2 --- /dev/null +++ b/sfa/client/sfiAddSliver.py @@ -0,0 +1,22 @@ +#! /usr/bin/env python + +import sys +from sfa.util.rspecHelper import RSpec, Commands + +command = Commands(usage="%prog [options] node1 node2...", + description="Add slivers to the RSpec. " + + "This command reads in an RSpec and outputs a modified " + + "RSpec. Use this to add nodes to your slice.") +command.add_nodefile_option() +command.prep() + +for node in command.nodes: + try: + command.rspec.add_sliver(node) + except: + print >> sys.stderr, "FAILED: %s" % node + +print command.rspec + + + diff --git a/sfa/client/sfiDeleteAttribute.py b/sfa/client/sfiDeleteAttribute.py new file mode 100755 index 00000000..8e8f0a28 --- /dev/null +++ b/sfa/client/sfiDeleteAttribute.py @@ -0,0 +1,37 @@ +#! /usr/bin/env python + +import sys +from sfa.util.rspecHelper import RSpec, Commands + +command = Commands(usage="%prog [options] [node1 node2...]", + description="Delete sliver attributes from the RSpec. " + + "This command reads in an RSpec and outputs a modified " + + "RSpec. Use this to remove attributes from nodes " + + "in your slice. If no nodes are specified, the " + + "attributes will be removed from ALL nodes.", + epilog="NOTE: Only admins can actually set these " + + "attributes, with the exception of --delegations") +command.add_nodefile_option() +command.add_attribute_options() +command.prep() + +attrs = command.get_attribute_dict() +for name in attrs: + print >> sys.stderr, name, attrs[name] + for value in attrs[name]: + if not command.nodes: + try: + command.rspec.remove_default_sliver_attribute(name, value) + except: + print >> sys.stderr, "FAILED: on all nodes: %s=%s" % (name, value) + else: + for node in command.nodes: + try: + command.rspec.remove_sliver_attribute(node, name, value) + except: + print >> sys.stderr, "FAILED: on node %s: %s=%s" % (node, name, value) + +print command.rspec + + + diff --git a/sfa/client/sfiDeleteSliver.py b/sfa/client/sfiDeleteSliver.py new file mode 100755 index 00000000..c8e769aa --- /dev/null +++ b/sfa/client/sfiDeleteSliver.py @@ -0,0 +1,22 @@ +#! /usr/bin/env python + +import sys +from sfa.util.rspecHelper import RSpec, Commands + +command = Commands(usage="%prog [options] node1 node2...", + description="Delete slivers from the RSpec. " + + "This command reads in an RSpec and outputs a modified " + + "RSpec. Use this to remove nodes from your slice.") +command.add_nodefile_option() +command.prep() + +for node in command.nodes: + try: + command.rspec.remove_sliver(node) + except: + print >> sys.stderr, "FAILED: %s" % node + +print command.rspec + + + diff --git a/sfa/client/sfiListNodes.py b/sfa/client/sfiListNodes.py new file mode 100755 index 00000000..df14fe38 --- /dev/null +++ b/sfa/client/sfiListNodes.py @@ -0,0 +1,17 @@ +#! /usr/bin/env python + +import sys +from sfa.util.rspecHelper import RSpec, Commands + +command = Commands(usage="%prog [options]", + description="List all nodes in the RSpec. " + + "Use this to display the list of nodes on which it is " + + "possible to create a slice.") +command.prep() + +nodes = command.rspec.get_node_list() +for node in nodes: + print node + + + diff --git a/sfa/client/sfiListSlivers.py b/sfa/client/sfiListSlivers.py new file mode 100755 index 00000000..685521ca --- /dev/null +++ b/sfa/client/sfiListSlivers.py @@ -0,0 +1,28 @@ +#! /usr/bin/env python + +import sys +from sfa.util.rspecHelper import RSpec, Commands + +command = Commands(usage="%prog [options]", + description="List all slivers in the RSpec. " + + "Use this to display the list of nodes belonging to " + + "the slice.") +command.add_show_attributes_option() +command.prep() + +nodes = command.rspec.get_sliver_list() +if command.opts.showatt: + defaults = command.rspec.get_default_sliver_attributes() + if defaults: + print "ALL NODES" + for (name, value) in defaults: + print " %s: %s" % (name, value) + +for node in nodes: + print node + if command.opts.showatt: + atts = command.rspec.get_sliver_attributes(node) + for (name, value) in atts: + print " %s: %s" % (name, value) + + diff --git a/sfa/util/rspecHelper.py b/sfa/util/rspecHelper.py new file mode 100755 index 00000000..e629a86a --- /dev/null +++ b/sfa/util/rspecHelper.py @@ -0,0 +1,292 @@ +#! /usr/bin/env python + +import sys +from lxml import etree +from StringIO import StringIO +from optparse import OptionParser + +class RSpec: + def __init__(self, xml): + parser = etree.XMLParser(remove_blank_text=True) + tree = etree.parse(StringIO(xml), parser) + self.rspec = tree.getroot() + + def get_node_element(self, hostname): + names = self.rspec.iterfind("./network/site/node/hostname") + for name in names: + if name.text == hostname: + return name.getparent() + return None + + def get_node_list(self): + result = self.rspec.xpath("./network/site/node/hostname/text()") + return result + + def get_sliver_list(self): + result = self.rspec.xpath("./network/site/node[sliver]/hostname/text()") + return result + + def add_sliver(self, hostname): + node = self.get_node_element(hostname) + etree.SubElement(node, "sliver") + + def remove_sliver(self, hostname): + node = self.get_node_element(hostname) + node.remove(node.find("sliver")) + + def attributes_list(self, elem): + opts = [] + if elem is not None: + for e in elem: + opts.append((e.tag, e.text)) + return opts + + def get_default_sliver_attributes(self): + defaults = self.rspec.find(".//sliver_defaults") + return self.attributes_list(defaults) + + def get_sliver_attributes(self, hostname): + node = self.get_node_element(hostname) + sliver = node.find("sliver") + return self.attributes_list(sliver) + + def add_attribute(self, elem, name, value): + opt = etree.SubElement(elem, name) + opt.text = value + + def add_default_sliver_attribute(self, name, value): + defaults = self.rspec.find(".//sliver_defaults") + self.add_attribute(defaults, name, value) + + def add_sliver_attribute(self, hostname, name, value): + node = self.get_node_element(hostname) + sliver = node.find("sliver") + self.add_attribute(sliver, name, value) + + def remove_attribute(self, elem, name, value): + if elem is not None: + opts = elem.iterfind(name) + if opts is not None: + for opt in opts: + if opt.text == value: + elem.remove(opt) + + def remove_default_sliver_attribute(self, name, value): + defaults = self.rspec.find(".//sliver_defaults") + self.remove_attribute(defaults, name, value) + + def remove_sliver_attribute(self, hostname, name, value): + node = self.get_node_element(hostname) + sliver = node.find("sliver") + self.remove_attribute(sliver, name, value) + + def get_site_nodes(self, siteid): + query = './/site[@id="%s"]/node/hostname/text()' % siteid + result = self.rspec.xpath(query) + return result + + def get_link_list(self): + linklist = [] + links = self.rspec.iterfind(".//link") + for link in links: + (end1, end2) = link.get("endpoints").split() + name = link.find("description") + linklist.append((name.text, + self.get_site_nodes(end1), + self.get_site_nodes(end2))) + return linklist + + def get_vlink_list(self): + vlinklist = [] + vlinks = self.rspec.iterfind(".//vlink") + for vlink in vlinks: + endpoints = vlink.get("endpoints") + (end1, end2) = endpoints.split() + query = './/node[@id="%s"]/hostname/text()' + node1 = self.rspec.xpath(query % end1)[0] + node2 = self.rspec.xpath(query % end2)[0] + desc = "%s <--> %s" % (node1, node2) + kbps = vlink.find("kbps") + vlinklist.append((endpoints, desc, kbps.text)) + return vlinklist + + def query_links(self, fromnode, tonode): + fromsite = fromnode.getparent() + tosite = tonode.getparent() + fromid = fromsite.get("id") + toid = tosite.get("id") + + query = ".//link[@endpoints = '%s %s']" % (fromid, toid) + results = self.rspec.xpath(query) + if results == None: + query = ".//link[@endpoints = '%s %s']" % (toid, fromid) + results = self.rspec.xpath(query) + return results + + def query_vlinks(self, endpoints): + query = ".//vlink[@endpoints = '%s']" % endpoints + results = self.rspec.xpath(query) + return results + + + def add_vlink(self, fromhost, tohost, kbps): + fromnode = self.get_node_element(fromhost) + tonode = self.get_node_element(tohost) + links = self.query_links(fromnode, tonode) + + for link in links: + vlink = etree.SubElement(link, "vlink") + fromid = fromnode.get("id") + toid = tonode.get("id") + vlink.set("endpoints", "%s %s" % (fromid, toid)) + self.add_attribute(vlink, "kbps", kbps) + + + def remove_vlink(self, endpoints): + vlinks = self.query_vlinks(endpoints) + for vlink in vlinks: + vlink.getparent().remove(vlink) + + def toxml(self): + return etree.tostring(self.rspec, pretty_print=True, + xml_declaration=True) + + def __str__(self): + return self.toxml() + + def save(self, filename): + f = open(filename, "w") + f.write(self.toxml()) + f.close() + + +class Commands: + def __init__(self, usage, description, epilog=None): + self.parser = OptionParser(usage=usage, description=description, + epilog=epilog) + self.parser.add_option("-i", "", dest="infile", metavar="FILE", + help="read RSpec from FILE (default is stdin)") + self.parser.add_option("-o", "", dest="outfile", metavar="FILE", + help="write output to FILE (default is stdout)") + self.nodefile = False + self.attributes = {} + + def add_nodefile_option(self): + self.nodefile = True + self.parser.add_option("-n", "", dest="nodefile", + metavar="FILE", + help="read node list from FILE"), + + def add_show_attributes_option(self): + self.parser.add_option("-s", "--show-attributes", action="store_true", + dest="showatt", default=False, + help="show sliver attributes") + + def add_attribute_options(self): + self.parser.add_option("", "--capabilities", action="append", + metavar="", + help="Vserver bcapabilities") + self.parser.add_option("", "--codemux", action="append", + metavar="", + help="Demux HTTP between slices using " + + "localhost ports") + self.parser.add_option("", "--cpu-pct", action="append", + metavar="", + help="Reserved CPU percent (e.g., 25)") + self.parser.add_option("", "--cpu-share", action="append", + metavar="", + help="Number of CPU shares (e.g., 5)") + self.parser.add_option("", "--delegations", + metavar="", action="append", + help="List of slices with delegation authority") + self.parser.add_option("", "--disk-max", + metavar="", action="append", + help="Disk quota (1k disk blocks)") + self.parser.add_option("", "--initscript", + metavar="", action="append", + help="Slice initialization script (e.g., stork)") + self.parser.add_option("", "--ip-addresses", action="append", + metavar="", + help="Add an IP address to a sliver") + self.parser.add_option("", "--net-i2-max-kbyte", + metavar="", action="append", + help="Maximum daily network Tx limit " + + "to I2 hosts.") + self.parser.add_option("", "--net-i2-max-rate", + metavar="", action="append", + help="Maximum bandwidth over I2 routes") + self.parser.add_option("", "--net-i2-min-rate", + metavar="", action="append", + help="Minimum bandwidth over I2 routes") + self.parser.add_option("", "--net-i2-share", + metavar="", action="append", + help="Number of bandwidth shares over I2 routes") + self.parser.add_option("", "--net-i2-thresh-kbyte", + metavar="", action="append", + help="Limit sent to I2 hosts before warning, " + + "throttling") + self.parser.add_option("", "--net-max-kbyte", + metavar="", action="append", + help="Maximum daily network Tx limit " + + "to non-I2 hosts.") + self.parser.add_option("", "--net-max-rate", + metavar="", action="append", + help="Maximum bandwidth over non-I2 routes") + self.parser.add_option("", "--net-min-rate", + metavar="", action="append", + help="Minimum bandwidth over non-I2 routes") + self.parser.add_option("", "--net-share", + metavar="", action="append", + help="Number of bandwidth shares over non-I2 " + + "routes") + self.parser.add_option("", "--net-thresh-kbyte", + metavar="", action="append", + help="Limit sent to non-I2 hosts before " + + "warning, throttling") + self.parser.add_option("", "--vsys", + metavar="", action="append", + help="Vsys script (e.g., fd_fusemount)") + self.parser.add_option("", "--vsys-vnet", + metavar="", action="append", + help="Allocate a virtual private network") + + def get_attribute_dict(self): + attrlist = ['capabilities','codemux','cpu_pct','cpu_share', + 'delegations','disk_max','initscript','ip_addresses', + 'net_i2_max_kbyte','net_i2_max_rate','net_i2_min_rate', + 'net_i2_share','net_i2_thresh_kbyte', + 'net_max_kbyte','net_max_rate','net_min_rate', + 'net_share','net_thresh_kbyte', + 'vsys','vsys_vnet'] + attrdict = {} + for attr in attrlist: + value = getattr(self.opts, attr, None) + if value is not None: + attrdict[attr] = value + return attrdict + + def prep(self): + (self.opts, self.args) = self.parser.parse_args() + + if self.opts.infile: + sys.stdin = open(self.opts.infile, "r") + xml = sys.stdin.read() + self.rspec = RSpec(xml) + + if self.nodefile: + if self.opts.nodefile: + f = open(self.opts.nodefile, "r") + self.nodes = f.read().split() + f.close() + else: + self.nodes = self.args + + if self.opts.outfile: + sys.stdout = open(self.opts.outfile, "w") + + + + + + + -- 2.43.0