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.
45 A dictionary with jobs id and uri.
49 # Convert the list of lists to the list of ids
50 result = [job_id for [job_id] in luxi.Client().QueryJobs(None, fields)]
51 return baserlib.BuildUriList(result, "/2/jobs/%s", uri_fields=("id", "uri"))
54 class R_2_jobs_id(baserlib.R_Generic):
55 """/2/jobs/[job_id] resource.
58 DOC_URI = "/2/jobs/[job_id]"
61 """Returns a job status.
64 A dictionary with job parameters.
67 id - job ID as a number
68 status - current job status as a string
69 ops - involved OpCodes as a list of dictionaries for each opcodes in
71 opstatus - OpCodes status as a list
72 opresult - OpCodes results as a list of lists
75 fields = ["id", "ops", "status", "opstatus", "opresult"]
76 job_id = self.items[0]
77 result = luxi.Client().QueryJobs([job_id, ], fields)[0]
78 return baserlib.MapFields(fields, result)
81 """Cancel not-yet-started job.
84 job_id = self.items[0]
85 result = luxi.Client().CancelJob(job_id)
89 class R_2_nodes(baserlib.R_Generic):
96 """Returns a list of all nodes.
99 A dictionary with 'name' and 'uri' keys for each of them.
103 "id": "node1.example.com",
104 "uri": "\/instances\/node1.example.com"
107 "id": "node2.example.com",
108 "uri": "\/instances\/node2.example.com"
111 If the optional 'bulk' argument is provided and set to 'true'
112 value (i.e '?bulk=1'), the output contains detailed
113 information about nodes as a list.
120 "name": "www.example.com",
131 op = ganeti.opcodes.OpQueryNodes(output_fields=["name"], names=[])
132 nodeslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
134 if 'bulk' in self.queryargs:
135 op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
137 result = ganeti.cli.SubmitOpCode(op)
138 return baserlib.MapBulkFields(result, N_FIELDS)
140 return baserlib.BuildUriList(nodeslist, "/2/nodes/%s",
141 uri_fields=("id", "uri"))
144 class R_2_instances(baserlib.R_Generic):
145 """/2/instances resource.
148 DOC_URI = "/2/instances"
151 """Returns a list of all available instances.
154 A dictionary with 'name' and 'uri' keys for each of them.
158 "name": "web.example.com",
159 "uri": "\/instances\/web.example.com"
162 "name": "mail.example.com",
163 "uri": "\/instances\/mail.example.com"
166 If the optional 'bulk' argument is provided and set to 'true'
167 value (i.e '?bulk=1'), the output contains detailed
168 information about instances as a list.
174 "name": "web.example.com",
175 "tags": ["tag1", "tag2"],
178 "pnode": "node1.example.com",
179 "mac": "01:23:45:67:89:01",
181 "snodes": ["node2.example.com"],
182 "disk_template": "drbd",
193 op = ganeti.opcodes.OpQueryInstances(output_fields=["name"], names=[])
194 instanceslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
196 if 'bulk' in self.queryargs:
197 op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
199 result = ganeti.cli.SubmitOpCode(op)
200 return baserlib.MapBulkFields(result, I_FIELDS)
204 return baserlib.BuildUriList(instanceslist, "/2/instances/%s",
205 uri_fields=("id", "uri"))
208 """Create an instance.
214 opts = self.req.request_post_data
216 beparams = baserlib.MakeParamsDict(opts, constants.BES_PARAMETERS)
217 hvparams = baserlib.MakeParamsDict(opts, constants.HVS_PARAMETERS)
219 op = ganeti.opcodes.OpCreateInstance(
220 instance_name=opts.get('name'),
221 disk_size=opts.get('size', 20 * 1024),
222 swap_size=opts.get('swap', 4 * 1024),
223 disk_template=opts.get('disk_template', None),
224 mode=constants.INSTANCE_CREATE,
225 os_type=opts.get('os'),
226 pnode=opts.get('pnode'),
227 snode=opts.get('snode'),
228 ip=opts.get('ip', 'none'),
229 bridge=opts.get('bridge', None),
230 start=opts.get('start', True),
231 ip_check=opts.get('ip_check', True),
232 wait_for_sync=opts.get('wait_for_sync', True),
233 mac=opts.get('mac', 'auto'),
234 hypervisor=opts.get('hypervisor', None),
237 iallocator=opts.get('iallocator', None),
238 file_storage_dir=opts.get('file_storage_dir', None),
239 file_driver=opts.get('file_driver', 'loop'),
242 job_id = ganeti.cli.SendJob([op])
246 class R_2_instances_name_reboot(baserlib.R_Generic):
247 """/2/instances/[instance_name]/reboot resource.
249 Implements an instance reboot.
253 DOC_URI = "/2/instances/[instance_name]/reboot"
256 """Reboot an instance.
258 The URI takes type=[hard|soft|full] and
259 ignore_secondaries=[False|True] parameters.
262 instance_name = self.items[0]
263 reboot_type = self.queryargs.get('type',
264 [constants.INSTANCE_REBOOT_HARD])[0]
265 ignore_secondaries = bool(self.queryargs.get('ignore_secondaries',
267 op = ganeti.opcodes.OpRebootInstance(
268 instance_name=instance_name,
269 reboot_type=reboot_type,
270 ignore_secondaries=ignore_secondaries)
272 job_id = ganeti.cli.SendJob([op])
277 class R_2_instances_name_startup(baserlib.R_Generic):
278 """/2/instances/[instance_name]/startup resource.
280 Implements an instance startup.
284 DOC_URI = "/2/instances/[instance_name]/startup"
287 """Startup an instance.
289 The URI takes force=[False|True] parameter to start the instance if even if
290 secondary disks are failing.
293 instance_name = self.items[0]
294 force_startup = bool(self.queryargs.get('force', [False])[0])
295 op = ganeti.opcodes.OpStartupInstance(instance_name=instance_name,
298 job_id = ganeti.cli.SendJob([op])
303 class R_2_instances_name_shutdown(baserlib.R_Generic):
304 """/2/instances/[instance_name]/shutdown resource.
306 Implements an instance shutdown.
310 DOC_URI = "/2/instances/[instance_name]/shutdown"
313 """Shutdown an instance.
316 instance_name = self.items[0]
317 op = ganeti.opcodes.OpShutdownInstance(instance_name=instance_name)
319 job_id = ganeti.cli.SendJob([op])
324 class R_2_instances_name_tags(baserlib.R_Generic):
325 """/2/instances/[instance_name]/tags resource.
327 Manages per-instance tags.
330 DOC_URI = "/2/instances/[instance_name]/tags"
333 """Returns a list of instance tags.
335 Example: ["tag1", "tag2", "tag3"]
338 return baserlib._Tags_GET(constants.TAG_INSTANCE, name=self.items[0])
341 """Add a set of tags to the instance.
343 The request as a list of strings should be POST to this URI. And you'll have
347 return baserlib._Tags_POST(constants.TAG_INSTANCE,
348 self.post_data, name=self.items[0])
353 In order to delete a set of tags from a instance, DELETE request should be
354 addressed to URI like: /2/instances/[instance_name]/tags?tag=[tag]&tag=[tag]
357 if 'tag' not in self.queryargs:
358 # no we not gonna delete all tags from an instance
359 raise http.HTTPNotImplemented()
360 return baserlib._Tags_DELETE(constants.TAG_INSTANCE,
361 self.queryargs['tag'],