4 # Copyright (C) 2006, 2007, 2008 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Remote API version 2 baserlib.library.
27 from ganeti import http
28 from ganeti import luxi
29 from ganeti import constants
30 from ganeti.rapi import baserlib
33 I_FIELDS = ["name", "os", "pnode", "snodes", "admin_state", "disk_template",
34 "ip", "mac", "bridge", "sda_size", "sdb_size", "beparams",
35 "oper_state", "status", "tags"]
37 N_FIELDS = ["name", "dtotal", "dfree",
38 "mtotal", "mnode", "mfree",
39 "pinst_cnt", "sinst_cnt", "tags"]
42 class R_version(baserlib.R_Generic):
45 This resource should be used to determine the remote API version and
46 to adapt clients accordingly.
52 """Returns the remote API version.
55 return constants.RAPI_VERSION
58 class R_2_info(baserlib.R_Generic):
65 """Returns cluster information.
71 "name": "cluster1.example.com",
72 "software_version": "1.2.4",
75 "master": "node1.example.com",
80 "hypervisor_type": "xen-pvm",
81 "protocol_version": 12
85 op = ganeti.opcodes.OpQueryClusterInfo()
86 return ganeti.cli.SubmitOpCode(op)
89 class R_2_os(baserlib.R_Generic):
96 """Return a list of all OSes.
98 Can return error 500 in case of a problem.
100 Example: ["debian-etch"]
103 op = ganeti.opcodes.OpDiagnoseOS(output_fields=["name", "valid"],
105 diagnose_data = ganeti.cli.SubmitOpCode(op)
107 if not isinstance(diagnose_data, list):
108 raise http.HttpInternalServerError(message="Can't get OS list")
110 return [row[0] for row in diagnose_data if row[1]]
113 class R_2_jobs(baserlib.R_Generic):
120 """Returns a dictionary of jobs.
122 @return: a dictionary with jobs id and uri.
126 # Convert the list of lists to the list of ids
127 result = [job_id for [job_id] in luxi.Client().QueryJobs(None, fields)]
128 return baserlib.BuildUriList(result, "/2/jobs/%s", uri_fields=("id", "uri"))
131 class R_2_jobs_id(baserlib.R_Generic):
132 """/2/jobs/[job_id] resource.
135 DOC_URI = "/2/jobs/[job_id]"
138 """Returns a job status.
140 @return: a dictionary with job parameters.
142 - id: job ID as a number
143 - status: current job status as a string
144 - ops: involved OpCodes as a list of dictionaries for each
146 - opstatus: OpCodes status as a list
147 - opresult: OpCodes results as a list of lists
150 fields = ["id", "ops", "status", "opstatus", "opresult"]
151 job_id = self.items[0]
152 result = luxi.Client().QueryJobs([job_id, ], fields)[0]
153 return baserlib.MapFields(fields, result)
156 """Cancel not-yet-started job.
159 job_id = self.items[0]
160 result = luxi.Client().CancelJob(job_id)
164 class R_2_nodes(baserlib.R_Generic):
165 """/2/nodes resource.
171 """Returns a list of all nodes.
177 "id": "node1.example.com",
178 "uri": "\/instances\/node1.example.com"
181 "id": "node2.example.com",
182 "uri": "\/instances\/node2.example.com"
186 If the optional 'bulk' argument is provided and set to 'true'
187 value (i.e '?bulk=1'), the output contains detailed
188 information about nodes as a list.
197 "name": "www.example.com",
207 @return: a dictionary with 'name' and 'uri' keys for each of them
210 client = luxi.Client()
211 nodesdata = client.QueryNodes([], ["name"])
212 nodeslist = [row[0] for row in nodesdata]
214 if 'bulk' in self.queryargs:
215 bulkdata = client.QueryNodes(nodeslist, N_FIELDS)
216 return baserlib.MapBulkFields(bulkdata, N_FIELDS)
218 return baserlib.BuildUriList(nodeslist, "/2/nodes/%s",
219 uri_fields=("id", "uri"))
222 class R_2_nodes_name(baserlib.R_Generic):
223 """/2/nodes/[node_name] resources.
226 DOC_URI = "/nodes/[node_name]"
229 """Send information about a node.
232 node_name = self.items[0]
233 op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
235 result = ganeti.cli.SubmitOpCode(op)
237 return baserlib.MapFields(N_FIELDS, result[0])
240 class R_2_instances(baserlib.R_Generic):
241 """/2/instances resource.
244 DOC_URI = "/2/instances"
247 """Returns a list of all available instances.
254 "name": "web.example.com",
255 "uri": "\/instances\/web.example.com"
258 "name": "mail.example.com",
259 "uri": "\/instances\/mail.example.com"
263 If the optional 'bulk' argument is provided and set to 'true'
264 value (i.e '?bulk=1'), the output contains detailed
265 information about instances as a list.
273 "name": "web.example.com",
274 "tags": ["tag1", "tag2"],
277 "pnode": "node1.example.com",
278 "mac": "01:23:45:67:89:01",
280 "snodes": ["node2.example.com"],
281 "disk_template": "drbd",
291 @returns: a dictionary with 'name' and 'uri' keys for each of them.
294 client = luxi.Client()
295 instancesdata = client.QueryInstances([], ["name"])
296 instanceslist = [row[0] for row in instancesdata]
299 if 'bulk' in self.queryargs:
300 bulkdata = client.QueryInstances(instanceslist, I_FIELDS)
301 return baserlib.MapBulkFields(bulkdata, I_FIELDS)
304 return baserlib.BuildUriList(instanceslist, "/2/instances/%s",
305 uri_fields=("id", "uri"))
308 """Create an instance.
313 opts = self.req.request_post_data
315 beparams = baserlib.MakeParamsDict(opts, constants.BES_PARAMETERS)
316 hvparams = baserlib.MakeParamsDict(opts, constants.HVS_PARAMETERS)
318 op = ganeti.opcodes.OpCreateInstance(
319 instance_name=opts.get('name'),
320 disk_size=opts.get('size', 20 * 1024),
321 swap_size=opts.get('swap', 4 * 1024),
322 disk_template=opts.get('disk_template', None),
323 mode=constants.INSTANCE_CREATE,
324 os_type=opts.get('os'),
325 pnode=opts.get('pnode'),
326 snode=opts.get('snode'),
327 ip=opts.get('ip', 'none'),
328 bridge=opts.get('bridge', None),
329 start=opts.get('start', True),
330 ip_check=opts.get('ip_check', True),
331 wait_for_sync=opts.get('wait_for_sync', True),
332 mac=opts.get('mac', 'auto'),
333 hypervisor=opts.get('hypervisor', None),
336 iallocator=opts.get('iallocator', None),
337 file_storage_dir=opts.get('file_storage_dir', None),
338 file_driver=opts.get('file_driver', 'loop'),
341 job_id = ganeti.cli.SendJob([op])
345 class R_2_instances_name(baserlib.R_Generic):
346 """/2/instances/[instance_name] resources.
349 DOC_URI = "/2/instances/[instance_name]"
352 """Send information about an instance.
355 instance_name = self.items[0]
356 op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
357 names=[instance_name])
358 result = ganeti.cli.SubmitOpCode(op)
360 return baserlib.MapFields(I_FIELDS, result[0])
363 class R_2_instances_name_reboot(baserlib.R_Generic):
364 """/2/instances/[instance_name]/reboot resource.
366 Implements an instance reboot.
370 DOC_URI = "/2/instances/[instance_name]/reboot"
373 """Reboot an instance.
375 The URI takes type=[hard|soft|full] and
376 ignore_secondaries=[False|True] parameters.
379 instance_name = self.items[0]
380 reboot_type = self.queryargs.get('type',
381 [constants.INSTANCE_REBOOT_HARD])[0]
382 ignore_secondaries = bool(self.queryargs.get('ignore_secondaries',
384 op = ganeti.opcodes.OpRebootInstance(
385 instance_name=instance_name,
386 reboot_type=reboot_type,
387 ignore_secondaries=ignore_secondaries)
389 job_id = ganeti.cli.SendJob([op])
394 class R_2_instances_name_startup(baserlib.R_Generic):
395 """/2/instances/[instance_name]/startup resource.
397 Implements an instance startup.
401 DOC_URI = "/2/instances/[instance_name]/startup"
404 """Startup an instance.
406 The URI takes force=[False|True] parameter to start the instance
407 if even if secondary disks are failing.
410 instance_name = self.items[0]
411 force_startup = bool(self.queryargs.get('force', [False])[0])
412 op = ganeti.opcodes.OpStartupInstance(instance_name=instance_name,
415 job_id = ganeti.cli.SendJob([op])
420 class R_2_instances_name_shutdown(baserlib.R_Generic):
421 """/2/instances/[instance_name]/shutdown resource.
423 Implements an instance shutdown.
427 DOC_URI = "/2/instances/[instance_name]/shutdown"
430 """Shutdown an instance.
433 instance_name = self.items[0]
434 op = ganeti.opcodes.OpShutdownInstance(instance_name=instance_name)
436 job_id = ganeti.cli.SendJob([op])
441 class _R_Tags(baserlib.R_Generic):
442 """ Quasiclass for tagging resources
444 Manages tags. Inheriting this class you suppose to define DOC_URI and
449 def __init__(self, items, queryargs, req):
450 """A tag resource constructor.
452 We have to override the default to sort out cluster naming case.
455 baserlib.R_Generic.__init__(self, items, queryargs, req)
457 if self.TAG_LEVEL != constants.TAG_CLUSTER:
463 """Returns a list of tags.
465 Example: ["tag1", "tag2", "tag3"]
468 return baserlib._Tags_GET(self.TAG_LEVEL, name=self.name)
471 """Add a set of tags.
473 The request as a list of strings should be PUT to this URI. And
474 you'll have back a job id.
477 return baserlib._Tags_PUT(self.TAG_LEVEL,
478 self.req.request_post_data, name=self.name)
483 In order to delete a set of tags, the DELETE
484 request should be addressed to URI like:
485 /tags?tag=[tag]&tag=[tag]
488 if 'tag' not in self.queryargs:
489 # no we not gonna delete all tags
490 raise http.HttpNotImplemented()
491 return baserlib._Tags_DELETE(self.TAG_LEVEL,
492 self.queryargs['tag'],
496 class R_2_instances_name_tags(_R_Tags):
497 """ /2/instances/[instance_name]/tags resource.
499 Manages per-instance tags.
502 DOC_URI = "/2/instances/[instance_name]/tags"
503 TAG_LEVEL = constants.TAG_INSTANCE
506 class R_2_nodes_name_tags(_R_Tags):
507 """ /2/nodes/[node_name]/tags resource.
509 Manages per-node tags.
512 DOC_URI = "/2/nodes/[node_name]/tags"
513 TAG_LEVEL = constants.TAG_NODE
516 class R_2_tags(_R_Tags):
517 """ /2/instances/tags resource.
519 Manages cluster tags.
523 TAG_LEVEL = constants.TAG_CLUSTER