5 from StringIO import StringIO
6 from optparse import OptionParser
9 def __init__(self, xml):
10 parser = etree.XMLParser(remove_blank_text=True)
11 tree = etree.parse(StringIO(xml), parser)
12 self.rspec = tree.getroot()
14 def get_node_element(self, hostname):
15 names = self.rspec.iterfind("./network/site/node/hostname")
17 if name.text == hostname:
18 return name.getparent()
21 def get_node_list(self):
22 result = self.rspec.xpath("./network/site/node/hostname/text()")
25 def get_sliver_list(self):
26 result = self.rspec.xpath("./network/site/node[sliver]/hostname/text()")
29 def add_sliver(self, hostname):
30 node = self.get_node_element(hostname)
31 etree.SubElement(node, "sliver")
33 def remove_sliver(self, hostname):
34 node = self.get_node_element(hostname)
35 node.remove(node.find("sliver"))
37 def attributes_list(self, elem):
41 opts.append((e.tag, e.text))
44 def get_default_sliver_attributes(self):
45 defaults = self.rspec.find(".//sliver_defaults")
46 return self.attributes_list(defaults)
48 def get_sliver_attributes(self, hostname):
49 node = self.get_node_element(hostname)
50 sliver = node.find("sliver")
51 return self.attributes_list(sliver)
53 def add_attribute(self, elem, name, value):
54 opt = etree.SubElement(elem, name)
57 def add_default_sliver_attribute(self, name, value):
58 defaults = self.rspec.find(".//sliver_defaults")
60 defaults = etree.Element("sliver_defaults")
61 network = self.rspec.find(".//network")
62 network.insert(0, defaults)
63 self.add_attribute(defaults, name, value)
65 def add_sliver_attribute(self, hostname, name, value):
66 node = self.get_node_element(hostname)
67 sliver = node.find("sliver")
68 self.add_attribute(sliver, name, value)
70 def remove_attribute(self, elem, name, value):
72 opts = elem.iterfind(name)
78 def remove_default_sliver_attribute(self, name, value):
79 defaults = self.rspec.find(".//sliver_defaults")
80 self.remove_attribute(defaults, name, value)
82 def remove_sliver_attribute(self, hostname, name, value):
83 node = self.get_node_element(hostname)
84 sliver = node.find("sliver")
85 self.remove_attribute(sliver, name, value)
87 def get_site_nodes(self, siteid):
88 query = './/site[@id="%s"]/node/hostname/text()' % siteid
89 result = self.rspec.xpath(query)
92 def get_link_list(self):
94 links = self.rspec.iterfind(".//link")
96 (end1, end2) = link.get("endpoints").split()
97 name = link.find("description")
98 linklist.append((name.text,
99 self.get_site_nodes(end1),
100 self.get_site_nodes(end2)))
103 def get_vlink_list(self):
105 vlinks = self.rspec.iterfind(".//vlink")
107 endpoints = vlink.get("endpoints")
108 (end1, end2) = endpoints.split()
109 query = './/node[@id="%s"]/hostname/text()'
110 node1 = self.rspec.xpath(query % end1)[0]
111 node2 = self.rspec.xpath(query % end2)[0]
112 desc = "%s <--> %s" % (node1, node2)
113 kbps = vlink.find("kbps")
114 vlinklist.append((endpoints, desc, kbps.text))
117 def query_links(self, fromnode, tonode):
118 fromsite = fromnode.getparent()
119 tosite = tonode.getparent()
120 fromid = fromsite.get("id")
121 toid = tosite.get("id")
123 query = ".//link[@endpoints = '%s %s']" % (fromid, toid)
124 results = self.rspec.xpath(query)
126 query = ".//link[@endpoints = '%s %s']" % (toid, fromid)
127 results = self.rspec.xpath(query)
130 def query_vlinks(self, endpoints):
131 query = ".//vlink[@endpoints = '%s']" % endpoints
132 results = self.rspec.xpath(query)
136 def add_vlink(self, fromhost, tohost, kbps):
137 fromnode = self.get_node_element(fromhost)
138 tonode = self.get_node_element(tohost)
139 links = self.query_links(fromnode, tonode)
142 vlink = etree.SubElement(link, "vlink")
143 fromid = fromnode.get("id")
144 toid = tonode.get("id")
145 vlink.set("endpoints", "%s %s" % (fromid, toid))
146 self.add_attribute(vlink, "kbps", kbps)
149 def remove_vlink(self, endpoints):
150 vlinks = self.query_vlinks(endpoints)
152 vlink.getparent().remove(vlink)
155 return etree.tostring(self.rspec, pretty_print=True,
156 xml_declaration=True)
161 def save(self, filename):
162 f = open(filename, "w")
163 f.write(self.toxml())
168 def __init__(self, usage, description, epilog=None):
169 self.parser = OptionParser(usage=usage, description=description,
171 self.parser.add_option("-i", "", dest="infile", metavar="FILE",
172 help="read RSpec from FILE (default is stdin)")
173 self.parser.add_option("-o", "", dest="outfile", metavar="FILE",
174 help="write output to FILE (default is stdout)")
175 self.nodefile = False
178 def add_nodefile_option(self):
180 self.parser.add_option("-n", "", dest="nodefile",
182 help="read node list from FILE"),
184 def add_show_attributes_option(self):
185 self.parser.add_option("-s", "--show-attributes", action="store_true",
186 dest="showatt", default=False,
187 help="show sliver attributes")
189 def add_attribute_options(self):
190 self.parser.add_option("", "--capabilities", action="append",
191 metavar="<cap1,cap2,...>",
192 help="Vserver bcapabilities")
193 self.parser.add_option("", "--codemux", action="append",
194 metavar="<host,local-port>",
195 help="Demux HTTP between slices using " +
197 self.parser.add_option("", "--cpu-pct", action="append",
199 help="Reserved CPU percent (e.g., 25)")
200 self.parser.add_option("", "--cpu-share", action="append",
202 help="Number of CPU shares (e.g., 5)")
203 self.parser.add_option("", "--delegations",
204 metavar="<slice1,slice2,...>", action="append",
205 help="List of slices with delegation authority")
206 self.parser.add_option("", "--disk-max",
207 metavar="<num>", action="append",
208 help="Disk quota (1k disk blocks)")
209 self.parser.add_option("", "--initscript",
210 metavar="<name>", action="append",
211 help="Slice initialization script (e.g., stork)")
212 self.parser.add_option("", "--ip-addresses", action="append",
214 help="Add an IP address to a sliver")
215 self.parser.add_option("", "--net-i2-max-kbyte",
216 metavar="<KBytes>", action="append",
217 help="Maximum daily network Tx limit " +
219 self.parser.add_option("", "--net-i2-max-rate",
220 metavar="<Kbps>", action="append",
221 help="Maximum bandwidth over I2 routes")
222 self.parser.add_option("", "--net-i2-min-rate",
223 metavar="<Kbps>", action="append",
224 help="Minimum bandwidth over I2 routes")
225 self.parser.add_option("", "--net-i2-share",
226 metavar="<num>", action="append",
227 help="Number of bandwidth shares over I2 routes")
228 self.parser.add_option("", "--net-i2-thresh-kbyte",
229 metavar="<KBytes>", action="append",
230 help="Limit sent to I2 hosts before warning, " +
232 self.parser.add_option("", "--net-max-kbyte",
233 metavar="<KBytes>", action="append",
234 help="Maximum daily network Tx limit " +
236 self.parser.add_option("", "--net-max-rate",
237 metavar="<Kbps>", action="append",
238 help="Maximum bandwidth over non-I2 routes")
239 self.parser.add_option("", "--net-min-rate",
240 metavar="<Kbps>", action="append",
241 help="Minimum bandwidth over non-I2 routes")
242 self.parser.add_option("", "--net-share",
243 metavar="<num>", action="append",
244 help="Number of bandwidth shares over non-I2 " +
246 self.parser.add_option("", "--net-thresh-kbyte",
247 metavar="<KBytes>", action="append",
248 help="Limit sent to non-I2 hosts before " +
249 "warning, throttling")
250 self.parser.add_option("", "--vsys",
251 metavar="<name>", action="append",
252 help="Vsys script (e.g., fd_fusemount)")
253 self.parser.add_option("", "--vsys-vnet",
254 metavar="<IP network>", action="append",
255 help="Allocate a virtual private network")
257 def get_attribute_dict(self):
258 attrlist = ['capabilities','codemux','cpu_pct','cpu_share',
259 'delegations','disk_max','initscript','ip_addresses',
260 'net_i2_max_kbyte','net_i2_max_rate','net_i2_min_rate',
261 'net_i2_share','net_i2_thresh_kbyte',
262 'net_max_kbyte','net_max_rate','net_min_rate',
263 'net_share','net_thresh_kbyte',
266 for attr in attrlist:
267 value = getattr(self.opts, attr, None)
268 if value is not None:
269 attrdict[attr] = value
273 (self.opts, self.args) = self.parser.parse_args()
276 sys.stdin = open(self.opts.infile, "r")
277 xml = sys.stdin.read()
278 self.rspec = RSpec(xml)
281 if self.opts.nodefile:
282 f = open(self.opts.nodefile, "r")
283 self.nodes = f.read().split()
286 self.nodes = self.args
288 if self.opts.outfile:
289 sys.stdout = open(self.opts.outfile, "w")