Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / rlib2.py @ 21f04e5e

History | View | Annotate | Download (9.6 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008 Google Inc.
5
#
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.
10
#
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.
15
#
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
19
# 02110-1301, USA.
20

    
21

    
22
"""Remote API version 2 baserlib.library.
23

24
"""
25

    
26
import ganeti.opcodes
27
from ganeti import http
28
from ganeti import luxi
29
from ganeti import constants
30
from ganeti.rapi import baserlib
31

    
32
from ganeti.rapi.rlib1 import I_FIELDS, N_FIELDS
33

    
34

    
35
class R_2_jobs(baserlib.R_Generic):
36
  """/2/jobs resource.
37

38
  """
39
  DOC_URI = "/2/jobs"
40

    
41
  def GET(self):
42
    """Returns a dictionary of jobs.
43

44
    Returns:
45
      A dictionary with jobs id and uri.
46

47
    """
48
    fields = ["id"]
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"))
52

    
53

    
54
class R_2_jobs_id(baserlib.R_Generic):
55
  """/2/jobs/[job_id] resource.
56

57
  """
58
  DOC_URI = "/2/jobs/[job_id]"
59

    
60
  def GET(self):
61
    """Returns a job status.
62

63
    Returns:
64
      A dictionary with job parameters.
65

66
    The result includes:
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
70
        the job
71
      opstatus - OpCodes status as a list
72
      opresult - OpCodes results as a list of lists
73

74
    """
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)
79

    
80
  def DELETE(self):
81
    """Cancel not-yet-started job.
82

83
    """
84
    job_id = self.items[0]
85
    result = luxi.Client().CancelJob(job_id)
86
    return result
87

    
88

    
89
class R_2_nodes(baserlib.R_Generic):
90
  """/2/nodes resource.
91

92
  """
93
  DOC_URI = "/2/nodes"
94

    
95
  def GET(self):
96
    """Returns a list of all nodes.
97

98
    Returns:
99
      A dictionary with 'name' and 'uri' keys for each of them.
100

101
    Example: [
102
        {
103
          "id": "node1.example.com",
104
          "uri": "\/instances\/node1.example.com"
105
        },
106
        {
107
          "id": "node2.example.com",
108
          "uri": "\/instances\/node2.example.com"
109
        }]
110

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.
114

115
    Example: [
116
        {
117
          "pinst_cnt": 1,
118
          "mfree": 31280,
119
          "mtotal": 32763,
120
          "name": "www.example.com",
121
          "tags": [],
122
          "mnode": 512,
123
          "dtotal": 5246208,
124
          "sinst_cnt": 2,
125
          "dfree": 5171712
126
        },
127
        ...
128
    ]
129

130
    """
131
    op = ganeti.opcodes.OpQueryNodes(output_fields=["name"], names=[])
132
    nodeslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
133

    
134
    if 'bulk' in self.queryargs:
135
      op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
136
                                       names=nodeslist)
137
      result = ganeti.cli.SubmitOpCode(op)
138
      return baserlib.MapBulkFields(result, N_FIELDS)
139

    
140
    return baserlib.BuildUriList(nodeslist, "/2/nodes/%s",
141
                                 uri_fields=("id", "uri"))
142

    
143

    
144
class R_2_instances(baserlib.R_Generic):
145
  """/2/instances resource.
146

147
  """
148
  DOC_URI = "/2/instances"
149

    
150
  def GET(self):
151
    """Returns a list of all available instances.
152

153
    Returns:
154
       A dictionary with 'name' and 'uri' keys for each of them.
155

156
    Example: [
157
        {
158
          "name": "web.example.com",
159
          "uri": "\/instances\/web.example.com"
160
        },
161
        {
162
          "name": "mail.example.com",
163
          "uri": "\/instances\/mail.example.com"
164
        }]
165

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.
169

170
    Example: [
171
        {
172
           "status": "running",
173
           "bridge": "xen-br0",
174
           "name": "web.example.com",
175
           "tags": ["tag1", "tag2"],
176
           "admin_ram": 512,
177
           "sda_size": 20480,
178
           "pnode": "node1.example.com",
179
           "mac": "01:23:45:67:89:01",
180
           "sdb_size": 4096,
181
           "snodes": ["node2.example.com"],
182
           "disk_template": "drbd",
183
           "ip": null,
184
           "admin_state": true,
185
           "os": "debian-etch",
186
           "vcpus": 2,
187
           "oper_state": true
188
        },
189
        ...
190
    ]
191

192
    """
193
    op = ganeti.opcodes.OpQueryInstances(output_fields=["name"], names=[])
194
    instanceslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
195

    
196
    if 'bulk' in self.queryargs:
197
      op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
198
                                           names=instanceslist)
199
      result = ganeti.cli.SubmitOpCode(op)
200
      return baserlib.MapBulkFields(result, I_FIELDS)
201

    
202

    
203
    else:
204
      return baserlib.BuildUriList(instanceslist, "/2/instances/%s",
205
                                   uri_fields=("id", "uri"))
206

    
207
  def POST(self):
208
    """Create an instance.
209

210
    Returns:
211
      A job id.
212

213
    """
214
    opts = self.req.request_post_data
215

    
216
    beparams = baserlib.MakeParamsDict(opts, constants.BES_PARAMETERS)
217
    hvparams = baserlib.MakeParamsDict(opts, constants.HVS_PARAMETERS)
218

    
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),
235
        hvparams=hvparams,
236
        beparams=beparams,
237
        iallocator=opts.get('iallocator', None),
238
        file_storage_dir=opts.get('file_storage_dir', None),
239
        file_driver=opts.get('file_driver', 'loop'),
240
        )
241

    
242
    job_id = ganeti.cli.SendJob([op])
243
    return job_id
244

    
245

    
246
class R_2_instances_name_reboot(baserlib.R_Generic):
247
  """/2/instances/[instance_name]/reboot resource.
248

249
  Implements an instance reboot.
250

251
  """
252

    
253
  DOC_URI = "/2/instances/[instance_name]/reboot"
254

    
255
  def POST(self):
256
    """Reboot an instance.
257

258
    The URI takes type=[hard|soft|full] and
259
    ignore_secondaries=[False|True] parameters.
260

261
    """
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',
266
                                                 [False])[0])
267
    op = ganeti.opcodes.OpRebootInstance(
268
        instance_name=instance_name,
269
        reboot_type=reboot_type,
270
        ignore_secondaries=ignore_secondaries)
271

    
272
    job_id = ganeti.cli.SendJob([op])
273

    
274
    return job_id
275

    
276

    
277
class R_2_instances_name_startup(baserlib.R_Generic):
278
  """/2/instances/[instance_name]/startup resource.
279

280
  Implements an instance startup.
281

282
  """
283

    
284
  DOC_URI = "/2/instances/[instance_name]/startup"
285

    
286
  def PUT(self):
287
    """Startup an instance.
288

289
    The URI takes force=[False|True] parameter to start the instance if even if
290
    secondary disks are failing.
291

292
    """
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,
296
                                          force=force_startup)
297

    
298
    job_id = ganeti.cli.SendJob([op])
299

    
300
    return job_id
301

    
302

    
303
class R_2_instances_name_shutdown(baserlib.R_Generic):
304
  """/2/instances/[instance_name]/shutdown resource.
305

306
  Implements an instance shutdown.
307

308
  """
309

    
310
  DOC_URI = "/2/instances/[instance_name]/shutdown"
311

    
312
  def PUT(self):
313
    """Shutdown an instance.
314

315
    """
316
    instance_name = self.items[0]
317
    op = ganeti.opcodes.OpShutdownInstance(instance_name=instance_name)
318

    
319
    job_id = ganeti.cli.SendJob([op])
320

    
321
    return job_id
322

    
323

    
324
class R_2_instances_name_tags(baserlib.R_Generic):
325
  """/2/instances/[instance_name]/tags resource.
326

327
  Manages per-instance tags.
328

329
  """
330
  DOC_URI = "/2/instances/[instance_name]/tags"
331

    
332
  def GET(self):
333
    """Returns a list of instance tags.
334

335
    Example: ["tag1", "tag2", "tag3"]
336

337
    """
338
    return baserlib._Tags_GET(constants.TAG_INSTANCE, name=self.items[0])
339

    
340
  def PUT(self):
341
    """Add a set of tags to the instance.
342

343
    The request as a list of strings should be PUT to this URI. And you'll have
344
    back a job id.
345

346
    """
347
    return baserlib._Tags_PUT(constants.TAG_INSTANCE,
348
                               self.post_data, name=self.items[0])
349

    
350
  def DELETE(self):
351
    """Delete a tag.
352

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]
355

356
    """
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'],
362
                                 name=self.items[0])