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
32 from ganeti.rapi.rlib1 import I_FIELDS, N_FIELDS
35 class R_2_jobs(baserlib.R_Generic):
42 """Returns a dictionary of jobs.
44 @return: a dictionary with jobs id and uri.
48 # Convert the list of lists to the list of ids
49 result = [job_id for [job_id] in luxi.Client().QueryJobs(None, fields)]
50 return baserlib.BuildUriList(result, "/2/jobs/%s", uri_fields=("id", "uri"))
53 class R_2_jobs_id(baserlib.R_Generic):
54 """/2/jobs/[job_id] resource.
57 DOC_URI = "/2/jobs/[job_id]"
60 """Returns a job status.
62 @return: a dictionary with job parameters.
64 - id: job ID as a number
65 - status: current job status as a string
66 - ops: involved OpCodes as a list of dictionaries for each
68 - opstatus: OpCodes status as a list
69 - opresult: OpCodes results as a list of lists
72 fields = ["id", "ops", "status", "opstatus", "opresult"]
73 job_id = self.items[0]
74 result = luxi.Client().QueryJobs([job_id, ], fields)[0]
75 return baserlib.MapFields(fields, result)
78 """Cancel not-yet-started job.
81 job_id = self.items[0]
82 result = luxi.Client().CancelJob(job_id)
86 class R_2_nodes(baserlib.R_Generic):
93 """Returns a list of all nodes.
99 "id": "node1.example.com",
100 "uri": "\/instances\/node1.example.com"
103 "id": "node2.example.com",
104 "uri": "\/instances\/node2.example.com"
108 If the optional 'bulk' argument is provided and set to 'true'
109 value (i.e '?bulk=1'), the output contains detailed
110 information about nodes as a list.
119 "name": "www.example.com",
129 @return: a dictionary with 'name' and 'uri' keys for each of them
132 client = luxi.Client()
133 nodesdata = client.QueryNodes([], ["name"])
134 nodeslist = [row[0] for row in nodesdata]
136 if 'bulk' in self.queryargs:
137 bulkdata = client.QueryNodes(nodeslist, N_FIELDS)
138 return baserlib.MapBulkFields(bulkdata, N_FIELDS)
140 return baserlib.BuildUriList(nodeslist, "/2/nodes/%s",
141 uri_fields=("id", "uri"))
144 class R_nodes(R_2_nodes):
148 # TODO: Temporary resource will be deprecated
152 class R_2_instances(baserlib.R_Generic):
153 """/2/instances resource.
156 DOC_URI = "/2/instances"
159 """Returns a list of all available instances.
166 "name": "web.example.com",
167 "uri": "\/instances\/web.example.com"
170 "name": "mail.example.com",
171 "uri": "\/instances\/mail.example.com"
175 If the optional 'bulk' argument is provided and set to 'true'
176 value (i.e '?bulk=1'), the output contains detailed
177 information about instances as a list.
185 "name": "web.example.com",
186 "tags": ["tag1", "tag2"],
189 "pnode": "node1.example.com",
190 "mac": "01:23:45:67:89:01",
192 "snodes": ["node2.example.com"],
193 "disk_template": "drbd",
203 @returns: a dictionary with 'name' and 'uri' keys for each of them.
206 client = luxi.Client()
207 instancesdata = client.QueryInstances([], ["name"])
208 instanceslist = [row[0] for row in instancesdata]
211 if 'bulk' in self.queryargs:
212 bulkdata = client.QueryInstances(instanceslist, I_FIELDS)
213 return baserlib.MapBulkFields(bulkdata, I_FIELDS)
216 return baserlib.BuildUriList(instanceslist, "/2/instances/%s",
217 uri_fields=("id", "uri"))
220 """Create an instance.
225 opts = self.req.request_post_data
227 beparams = baserlib.MakeParamsDict(opts, constants.BES_PARAMETERS)
228 hvparams = baserlib.MakeParamsDict(opts, constants.HVS_PARAMETERS)
230 op = ganeti.opcodes.OpCreateInstance(
231 instance_name=opts.get('name'),
232 disk_size=opts.get('size', 20 * 1024),
233 swap_size=opts.get('swap', 4 * 1024),
234 disk_template=opts.get('disk_template', None),
235 mode=constants.INSTANCE_CREATE,
236 os_type=opts.get('os'),
237 pnode=opts.get('pnode'),
238 snode=opts.get('snode'),
239 ip=opts.get('ip', 'none'),
240 bridge=opts.get('bridge', None),
241 start=opts.get('start', True),
242 ip_check=opts.get('ip_check', True),
243 wait_for_sync=opts.get('wait_for_sync', True),
244 mac=opts.get('mac', 'auto'),
245 hypervisor=opts.get('hypervisor', None),
248 iallocator=opts.get('iallocator', None),
249 file_storage_dir=opts.get('file_storage_dir', None),
250 file_driver=opts.get('file_driver', 'loop'),
253 job_id = ganeti.cli.SendJob([op])
257 class R_instances(R_2_instances):
258 """/instances resource.
261 # TODO: Temporary resource will be deprecated
262 DOC_URI = "/instances"
265 class R_2_instances_name_reboot(baserlib.R_Generic):
266 """/2/instances/[instance_name]/reboot resource.
268 Implements an instance reboot.
272 DOC_URI = "/2/instances/[instance_name]/reboot"
275 """Reboot an instance.
277 The URI takes type=[hard|soft|full] and
278 ignore_secondaries=[False|True] parameters.
281 instance_name = self.items[0]
282 reboot_type = self.queryargs.get('type',
283 [constants.INSTANCE_REBOOT_HARD])[0]
284 ignore_secondaries = bool(self.queryargs.get('ignore_secondaries',
286 op = ganeti.opcodes.OpRebootInstance(
287 instance_name=instance_name,
288 reboot_type=reboot_type,
289 ignore_secondaries=ignore_secondaries)
291 job_id = ganeti.cli.SendJob([op])
296 class R_2_instances_name_startup(baserlib.R_Generic):
297 """/2/instances/[instance_name]/startup resource.
299 Implements an instance startup.
303 DOC_URI = "/2/instances/[instance_name]/startup"
306 """Startup an instance.
308 The URI takes force=[False|True] parameter to start the instance
309 if even if secondary disks are failing.
312 instance_name = self.items[0]
313 force_startup = bool(self.queryargs.get('force', [False])[0])
314 op = ganeti.opcodes.OpStartupInstance(instance_name=instance_name,
317 job_id = ganeti.cli.SendJob([op])
322 class R_2_instances_name_shutdown(baserlib.R_Generic):
323 """/2/instances/[instance_name]/shutdown resource.
325 Implements an instance shutdown.
329 DOC_URI = "/2/instances/[instance_name]/shutdown"
332 """Shutdown an instance.
335 instance_name = self.items[0]
336 op = ganeti.opcodes.OpShutdownInstance(instance_name=instance_name)
338 job_id = ganeti.cli.SendJob([op])
343 class R_2_instances_name_tags(baserlib.R_Generic):
344 """/2/instances/[instance_name]/tags resource.
346 Manages per-instance tags.
349 DOC_URI = "/2/instances/[instance_name]/tags"
352 """Returns a list of instance tags.
354 Example: ["tag1", "tag2", "tag3"]
357 return baserlib._Tags_GET(constants.TAG_INSTANCE, name=self.items[0])
360 """Add a set of tags to the instance.
362 The request as a list of strings should be PUT to this URI. And
363 you'll have back a job id.
366 return baserlib._Tags_PUT(constants.TAG_INSTANCE,
367 self.post_data, name=self.items[0])
372 In order to delete a set of tags from a instance, the DELETE
373 request should be addressed to URI like:
374 /2/instances/[instance_name]/tags?tag=[tag]&tag=[tag]
377 if 'tag' not in self.queryargs:
378 # no we not gonna delete all tags from an instance
379 raise http.HttpNotImplemented()
380 return baserlib._Tags_DELETE(constants.TAG_INSTANCE,
381 self.queryargs['tag'],