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")
59 self.add_attribute(defaults, name, value)
61 def add_sliver_attribute(self, hostname, name, value):
62 node = self.get_node_element(hostname)
63 sliver = node.find("sliver")
64 self.add_attribute(sliver, name, value)
66 def remove_attribute(self, elem, name, value):
68 opts = elem.iterfind(name)
74 def remove_default_sliver_attribute(self, name, value):
75 defaults = self.rspec.find(".//sliver_defaults")
76 self.remove_attribute(defaults, name, value)
78 def remove_sliver_attribute(self, hostname, name, value):
79 node = self.get_node_element(hostname)
80 sliver = node.find("sliver")
81 self.remove_attribute(sliver, name, value)
83 def get_site_nodes(self, siteid):
84 query = './/site[@id="%s"]/node/hostname/text()' % siteid
85 result = self.rspec.xpath(query)
88 def get_link_list(self):
90 links = self.rspec.iterfind(".//link")
92 (end1, end2) = link.get("endpoints").split()
93 name = link.find("description")
94 linklist.append((name.text,
95 self.get_site_nodes(end1),
96 self.get_site_nodes(end2)))
99 def get_vlink_list(self):
101 vlinks = self.rspec.iterfind(".//vlink")
103 endpoints = vlink.get("endpoints")
104 (end1, end2) = endpoints.split()
105 query = './/node[@id="%s"]/hostname/text()'
106 node1 = self.rspec.xpath(query % end1)[0]
107 node2 = self.rspec.xpath(query % end2)[0]
108 desc = "%s <--> %s" % (node1, node2)
109 kbps = vlink.find("kbps")
110 vlinklist.append((endpoints, desc, kbps.text))
113 def query_links(self, fromnode, tonode):
114 fromsite = fromnode.getparent()
115 tosite = tonode.getparent()
116 fromid = fromsite.get("id")
117 toid = tosite.get("id")
119 query = ".//link[@endpoints = '%s %s']" % (fromid, toid)
120 results = self.rspec.xpath(query)
122 query = ".//link[@endpoints = '%s %s']" % (toid, fromid)
123 results = self.rspec.xpath(query)
126 def query_vlinks(self, endpoints):
127 query = ".//vlink[@endpoints = '%s']" % endpoints
128 results = self.rspec.xpath(query)
132 def add_vlink(self, fromhost, tohost, kbps):
133 fromnode = self.get_node_element(fromhost)
134 tonode = self.get_node_element(tohost)
135 links = self.query_links(fromnode, tonode)
138 vlink = etree.SubElement(link, "vlink")
139 fromid = fromnode.get("id")
140 toid = tonode.get("id")
141 vlink.set("endpoints", "%s %s" % (fromid, toid))
142 self.add_attribute(vlink, "kbps", kbps)
145 def remove_vlink(self, endpoints):
146 vlinks = self.query_vlinks(endpoints)
148 vlink.getparent().remove(vlink)
151 return etree.tostring(self.rspec, pretty_print=True,
152 xml_declaration=True)
157 def save(self, filename):
158 f = open(filename, "w")
159 f.write(self.toxml())
164 def __init__(self, usage, description, epilog=None):
165 self.parser = OptionParser(usage=usage, description=description,
167 self.parser.add_option("-i", "", dest="infile", metavar="FILE",
168 help="read RSpec from FILE (default is stdin)")
169 self.parser.add_option("-o", "", dest="outfile", metavar="FILE",
170 help="write output to FILE (default is stdout)")
171 self.nodefile = False
174 def add_nodefile_option(self):
176 self.parser.add_option("-n", "", dest="nodefile",
178 help="read node list from FILE"),
180 def add_show_attributes_option(self):
181 self.parser.add_option("-s", "--show-attributes", action="store_true",
182 dest="showatt", default=False,
183 help="show sliver attributes")
185 def add_attribute_options(self):
186 self.parser.add_option("", "--capabilities", action="append",
187 metavar="<cap1,cap2,...>",
188 help="Vserver bcapabilities")
189 self.parser.add_option("", "--codemux", action="append",
190 metavar="<host,local-port>",
191 help="Demux HTTP between slices using " +
193 self.parser.add_option("", "--cpu-pct", action="append",
195 help="Reserved CPU percent (e.g., 25)")
196 self.parser.add_option("", "--cpu-share", action="append",
198 help="Number of CPU shares (e.g., 5)")
199 self.parser.add_option("", "--delegations",
200 metavar="<slice1,slice2,...>", action="append",
201 help="List of slices with delegation authority")
202 self.parser.add_option("", "--disk-max",
203 metavar="<num>", action="append",
204 help="Disk quota (1k disk blocks)")
205 self.parser.add_option("", "--initscript",
206 metavar="<name>", action="append",
207 help="Slice initialization script (e.g., stork)")
208 self.parser.add_option("", "--ip-addresses", action="append",
210 help="Add an IP address to a sliver")
211 self.parser.add_option("", "--net-i2-max-kbyte",
212 metavar="<KBytes>", action="append",
213 help="Maximum daily network Tx limit " +
215 self.parser.add_option("", "--net-i2-max-rate",
216 metavar="<Kbps>", action="append",
217 help="Maximum bandwidth over I2 routes")
218 self.parser.add_option("", "--net-i2-min-rate",
219 metavar="<Kbps>", action="append",
220 help="Minimum bandwidth over I2 routes")
221 self.parser.add_option("", "--net-i2-share",
222 metavar="<num>", action="append",
223 help="Number of bandwidth shares over I2 routes")
224 self.parser.add_option("", "--net-i2-thresh-kbyte",
225 metavar="<KBytes>", action="append",
226 help="Limit sent to I2 hosts before warning, " +
228 self.parser.add_option("", "--net-max-kbyte",
229 metavar="<KBytes>", action="append",
230 help="Maximum daily network Tx limit " +
232 self.parser.add_option("", "--net-max-rate",
233 metavar="<Kbps>", action="append",
234 help="Maximum bandwidth over non-I2 routes")
235 self.parser.add_option("", "--net-min-rate",
236 metavar="<Kbps>", action="append",
237 help="Minimum bandwidth over non-I2 routes")
238 self.parser.add_option("", "--net-share",
239 metavar="<num>", action="append",
240 help="Number of bandwidth shares over non-I2 " +
242 self.parser.add_option("", "--net-thresh-kbyte",
243 metavar="<KBytes>", action="append",
244 help="Limit sent to non-I2 hosts before " +
245 "warning, throttling")
246 self.parser.add_option("", "--vsys",
247 metavar="<name>", action="append",
248 help="Vsys script (e.g., fd_fusemount)")
249 self.parser.add_option("", "--vsys-vnet",
250 metavar="<IP network>", action="append",
251 help="Allocate a virtual private network")
253 def get_attribute_dict(self):
254 attrlist = ['capabilities','codemux','cpu_pct','cpu_share',
255 'delegations','disk_max','initscript','ip_addresses',
256 'net_i2_max_kbyte','net_i2_max_rate','net_i2_min_rate',
257 'net_i2_share','net_i2_thresh_kbyte',
258 'net_max_kbyte','net_max_rate','net_min_rate',
259 'net_share','net_thresh_kbyte',
262 for attr in attrlist:
263 value = getattr(self.opts, attr, None)
264 if value is not None:
265 attrdict[attr] = value
269 (self.opts, self.args) = self.parser.parse_args()
272 sys.stdin = open(self.opts.infile, "r")
273 xml = sys.stdin.read()
274 self.rspec = RSpec(xml)
277 if self.opts.nodefile:
278 f = open(self.opts.nodefile, "r")
279 self.nodes = f.read().split()
282 self.nodes = self.args
284 if self.opts.outfile:
285 sys.stdout = open(self.opts.outfile, "w")