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", "admin_state", "os",
36 "nic.ips", "nic.macs", "nic.bridges",
37 "disk.sizes", "disk_usage",
38 "beparams", "hvparams",
39 "oper_state", "oper_ram", "status",
42 N_FIELDS = ["name", "offline", "master_candidate", "drained",
44 "mtotal", "mnode", "mfree",
45 "pinst_cnt", "sinst_cnt", "tags",
46 "ctotal", "cnodes", "csockets",
50 class R_version(baserlib.R_Generic):
53 This resource should be used to determine the remote API version and
54 to adapt clients accordingly.
60 """Returns the remote API version.
63 return constants.RAPI_VERSION
66 class R_2_info(baserlib.R_Generic):
73 """Returns cluster information.
78 "config_version": 2000000,
80 "software_version": "2.0.0~beta1",
83 "candidate_pool_size": 10,
84 "enabled_hypervisors": [
90 "default_hypervisor": "fake",
91 "master": "node1.example.com",
96 "protocol_version": 20,
107 client = luxi.Client()
108 return client.QueryClusterInfo()
111 class R_2_os(baserlib.R_Generic):
118 """Return a list of all OSes.
120 Can return error 500 in case of a problem.
122 Example: ["debian-etch"]
125 op = ganeti.opcodes.OpDiagnoseOS(output_fields=["name", "valid"],
127 diagnose_data = ganeti.cli.SubmitOpCode(op)
129 if not isinstance(diagnose_data, list):
130 raise http.HttpInternalServerError(message="Can't get OS list")
132 return [row[0] for row in diagnose_data if row[1]]
135 class R_2_jobs(baserlib.R_Generic):
142 """Returns a dictionary of jobs.
144 @return: a dictionary with jobs id and uri.
148 # Convert the list of lists to the list of ids
149 result = [job_id for [job_id] in luxi.Client().QueryJobs(None, fields)]
150 return baserlib.BuildUriList(result, "/2/jobs/%s",
151 uri_fields=("id", "uri"))
154 class R_2_jobs_id(baserlib.R_Generic):
155 """/2/jobs/[job_id] resource.
158 DOC_URI = "/2/jobs/[job_id]"
161 """Returns a job status.
163 @return: a dictionary with job parameters.
165 - id: job ID as a number
166 - status: current job status as a string
167 - ops: involved OpCodes as a list of dictionaries for each
169 - opstatus: OpCodes status as a list
170 - opresult: OpCodes results as a list of lists
173 fields = ["id", "ops", "status", "summary",
174 "opstatus", "opresult", "oplog",
175 "received_ts", "start_ts", "end_ts",
177 job_id = self.items[0]
178 result = luxi.Client().QueryJobs([job_id, ], fields)[0]
180 raise http.HttpNotFound()
181 return baserlib.MapFields(fields, result)
184 """Cancel not-yet-started job.
187 job_id = self.items[0]
188 result = luxi.Client().CancelJob(job_id)
192 class R_2_nodes(baserlib.R_Generic):
193 """/2/nodes resource.
199 """Returns a list of all nodes.
205 "id": "node1.example.com",
206 "uri": "\/instances\/node1.example.com"
209 "id": "node2.example.com",
210 "uri": "\/instances\/node2.example.com"
214 If the optional 'bulk' argument is provided and set to 'true'
215 value (i.e '?bulk=1'), the output contains detailed
216 information about nodes as a list.
225 "name": "www.example.com",
236 @return: a dictionary with 'name' and 'uri' keys for each of them
239 client = luxi.Client()
242 bulkdata = client.QueryNodes([], N_FIELDS, False)
243 return baserlib.MapBulkFields(bulkdata, N_FIELDS)
245 nodesdata = client.QueryNodes([], ["name"], False)
246 nodeslist = [row[0] for row in nodesdata]
247 return baserlib.BuildUriList(nodeslist, "/2/nodes/%s",
248 uri_fields=("id", "uri"))
251 class R_2_nodes_name(baserlib.R_Generic):
252 """/2/nodes/[node_name] resources.
255 DOC_URI = "/nodes/[node_name]"
258 """Send information about a node.
261 node_name = self.items[0]
262 client = luxi.Client()
263 result = client.QueryNodes(names=[node_name], fields=N_FIELDS,
264 use_locking=self.useLocking())
266 return baserlib.MapFields(N_FIELDS, result[0])
269 class R_2_instances(baserlib.R_Generic):
270 """/2/instances resource.
273 DOC_URI = "/2/instances"
276 """Returns a list of all available instances.
283 "name": "web.example.com",
284 "uri": "\/instances\/web.example.com"
287 "name": "mail.example.com",
288 "uri": "\/instances\/mail.example.com"
292 If the optional 'bulk' argument is provided and set to 'true'
293 value (i.e '?bulk=1'), the output contains detailed
294 information about instances as a list.
305 "name": "web.example.com",
306 "tags": ["tag1", "tag2"],
314 "pnode": "node1.example.com",
315 "nic.macs": ["01:23:45:67:89:01"],
316 "snodes": ["node2.example.com"],
317 "disk_template": "drbd",
325 @returns: a dictionary with 'name' and 'uri' keys for each of them.
328 client = luxi.Client()
330 use_locking = self.useLocking()
332 bulkdata = client.QueryInstances([], I_FIELDS, use_locking)
333 return baserlib.MapBulkFields(bulkdata, I_FIELDS)
335 instancesdata = client.QueryInstances([], ["name"], use_locking)
336 instanceslist = [row[0] for row in instancesdata]
337 return baserlib.BuildUriList(instanceslist, "/2/instances/%s",
338 uri_fields=("id", "uri"))
341 """Create an instance.
346 if not isinstance(self.req.request_body, dict):
347 raise http.HttpBadRequest("Invalid body contents, not a dictionary")
349 beparams = baserlib.MakeParamsDict(self.req.request_body,
350 constants.BES_PARAMETERS)
351 hvparams = baserlib.MakeParamsDict(self.req.request_body,
352 constants.HVS_PARAMETERS)
353 fn = self.getBodyParameter
356 disk_data = fn('disks')
357 if not isinstance(disk_data, list):
358 raise http.HttpBadRequest("The 'disks' parameter should be a list")
360 for idx, d in enumerate(disk_data):
361 if not isinstance(d, int):
362 raise http.HttpBadRequest("Disk %d specification wrong: should"
364 disks.append({"size": d})
365 # nic processing (one nic only)
366 nics = [{"mac": fn("mac", constants.VALUE_AUTO),
367 "ip": fn("ip", None),
368 "bridge": fn("bridge", None)}]
370 op = ganeti.opcodes.OpCreateInstance(
371 mode=constants.INSTANCE_CREATE,
372 instance_name=fn('name'),
374 disk_template=fn('disk_template'),
376 pnode=fn('pnode', None),
377 snode=fn('snode', None),
378 iallocator=fn('iallocator', None),
380 start=fn('start', True),
381 ip_check=fn('ip_check', True),
383 hypervisor=fn('hypervisor', None),
386 file_storage_dir=fn('file_storage_dir', None),
387 file_driver=fn('file_driver', 'loop'),
390 job_id = ganeti.cli.SendJob([op])
394 class R_2_instances_name(baserlib.R_Generic):
395 """/2/instances/[instance_name] resources.
398 DOC_URI = "/2/instances/[instance_name]"
401 """Send information about an instance.
404 client = luxi.Client()
405 instance_name = self.items[0]
406 result = client.QueryInstances(names=[instance_name], fields=I_FIELDS,
407 use_locking=self.useLocking())
409 return baserlib.MapFields(I_FIELDS, result[0])
412 """Delete an instance.
415 op = ganeti.opcodes.OpRemoveInstance(instance_name=self.items[0],
416 ignore_failures=False)
417 job_id = ganeti.cli.SendJob([op])
421 class R_2_instances_name_reboot(baserlib.R_Generic):
422 """/2/instances/[instance_name]/reboot resource.
424 Implements an instance reboot.
428 DOC_URI = "/2/instances/[instance_name]/reboot"
431 """Reboot an instance.
433 The URI takes type=[hard|soft|full] and
434 ignore_secondaries=[False|True] parameters.
437 instance_name = self.items[0]
438 reboot_type = self.queryargs.get('type',
439 [constants.INSTANCE_REBOOT_HARD])[0]
440 ignore_secondaries = bool(self.queryargs.get('ignore_secondaries',
442 op = ganeti.opcodes.OpRebootInstance(
443 instance_name=instance_name,
444 reboot_type=reboot_type,
445 ignore_secondaries=ignore_secondaries)
447 job_id = ganeti.cli.SendJob([op])
452 class R_2_instances_name_startup(baserlib.R_Generic):
453 """/2/instances/[instance_name]/startup resource.
455 Implements an instance startup.
459 DOC_URI = "/2/instances/[instance_name]/startup"
462 """Startup an instance.
464 The URI takes force=[False|True] parameter to start the instance
465 if even if secondary disks are failing.
468 instance_name = self.items[0]
469 force_startup = bool(self.queryargs.get('force', [False])[0])
470 op = ganeti.opcodes.OpStartupInstance(instance_name=instance_name,
473 job_id = ganeti.cli.SendJob([op])
478 class R_2_instances_name_shutdown(baserlib.R_Generic):
479 """/2/instances/[instance_name]/shutdown resource.
481 Implements an instance shutdown.
485 DOC_URI = "/2/instances/[instance_name]/shutdown"
488 """Shutdown an instance.
491 instance_name = self.items[0]
492 op = ganeti.opcodes.OpShutdownInstance(instance_name=instance_name)
494 job_id = ganeti.cli.SendJob([op])
499 class _R_Tags(baserlib.R_Generic):
500 """ Quasiclass for tagging resources
502 Manages tags. Inheriting this class you suppose to define DOC_URI and
508 def __init__(self, items, queryargs, req):
509 """A tag resource constructor.
511 We have to override the default to sort out cluster naming case.
514 baserlib.R_Generic.__init__(self, items, queryargs, req)
516 if self.TAG_LEVEL != constants.TAG_CLUSTER:
522 """Returns a list of tags.
524 Example: ["tag1", "tag2", "tag3"]
527 return baserlib._Tags_GET(self.TAG_LEVEL, name=self.name)
530 """Add a set of tags.
532 The request as a list of strings should be PUT to this URI. And
533 you'll have back a job id.
536 if 'tag' not in self.queryargs:
537 raise http.HttpBadRequest("Please specify tag(s) to add using the"
538 " the 'tag' parameter")
539 return baserlib._Tags_PUT(self.TAG_LEVEL,
540 self.queryargs['tag'], name=self.name)
545 In order to delete a set of tags, the DELETE
546 request should be addressed to URI like:
547 /tags?tag=[tag]&tag=[tag]
550 if 'tag' not in self.queryargs:
551 # no we not gonna delete all tags
552 raise http.HttpBadRequest("Cannot delete all tags - please specify"
553 " tag(s) using the 'tag' parameter")
554 return baserlib._Tags_DELETE(self.TAG_LEVEL,
555 self.queryargs['tag'],
559 class R_2_instances_name_tags(_R_Tags):
560 """ /2/instances/[instance_name]/tags resource.
562 Manages per-instance tags.
565 DOC_URI = "/2/instances/[instance_name]/tags"
566 TAG_LEVEL = constants.TAG_INSTANCE
569 class R_2_nodes_name_tags(_R_Tags):
570 """ /2/nodes/[node_name]/tags resource.
572 Manages per-node tags.
575 DOC_URI = "/2/nodes/[node_name]/tags"
576 TAG_LEVEL = constants.TAG_NODE
579 class R_2_tags(_R_Tags):
580 """ /2/instances/tags resource.
582 Manages cluster tags.
586 TAG_LEVEL = constants.TAG_CLUSTER