Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / rlib2.py @ d50b3059

History | View | Annotate | Download (9.5 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

    
81
class R_2_nodes(baserlib.R_Generic):
82
  """/2/nodes resource.
83

84
  """
85
  DOC_URI = "/2/nodes"
86

    
87
  def GET(self):
88
    """Returns a list of all nodes.
89

90
    Returns:
91
      A dictionary with 'name' and 'uri' keys for each of them.
92

93
    Example: [
94
        {
95
          "id": "node1.example.com",
96
          "uri": "\/instances\/node1.example.com"
97
        },
98
        {
99
          "id": "node2.example.com",
100
          "uri": "\/instances\/node2.example.com"
101
        }]
102

103
    If the optional 'bulk' argument is provided and set to 'true'
104
    value (i.e '?bulk=1'), the output contains detailed
105
    information about nodes as a list.
106

107
    Example: [
108
        {
109
          "pinst_cnt": 1,
110
          "mfree": 31280,
111
          "mtotal": 32763,
112
          "name": "www.example.com",
113
          "tags": [],
114
          "mnode": 512,
115
          "dtotal": 5246208,
116
          "sinst_cnt": 2,
117
          "dfree": 5171712
118
        },
119
        ...
120
    ]
121

122
    """
123
    op = ganeti.opcodes.OpQueryNodes(output_fields=["name"], names=[])
124
    nodeslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
125

    
126
    if 'bulk' in self.queryargs:
127
      op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
128
                                       names=nodeslist)
129
      result = ganeti.cli.SubmitOpCode(op)
130
      return baserlib.MapBulkFields(result, N_FIELDS)
131

    
132
    return baserlib.BuildUriList(nodeslist, "/2/nodes/%s",
133
                                 uri_fields=("id", "uri"))
134

    
135

    
136
class R_2_instances(baserlib.R_Generic):
137
  """/2/instances resource.
138

139
  """
140
  DOC_URI = "/2/instances"
141

    
142
  def GET(self):
143
    """Returns a list of all available instances.
144

145
    Returns:
146
       A dictionary with 'name' and 'uri' keys for each of them.
147

148
    Example: [
149
        {
150
          "name": "web.example.com",
151
          "uri": "\/instances\/web.example.com"
152
        },
153
        {
154
          "name": "mail.example.com",
155
          "uri": "\/instances\/mail.example.com"
156
        }]
157

158
    If the optional 'bulk' argument is provided and set to 'true'
159
    value (i.e '?bulk=1'), the output contains detailed
160
    information about instances as a list.
161

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

184
    """
185
    op = ganeti.opcodes.OpQueryInstances(output_fields=["name"], names=[])
186
    instanceslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
187

    
188
    if 'bulk' in self.queryargs:
189
      op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
190
                                           names=instanceslist)
191
      result = ganeti.cli.SubmitOpCode(op)
192
      return baserlib.MapBulkFields(result, I_FIELDS)
193

    
194

    
195
    else:
196
      return baserlib.BuildUriList(instanceslist, "/2/instances/%s",
197
                                   uri_fields=("id", "uri"))
198

    
199
  def PUT(self):
200
    """Create an instance.
201

202
    Returns:
203
      A job id.
204

205
    """
206
    opts = self.req.request_post_data
207

    
208
    beparams = baserlib.MakeParamsDict(opts, constants.BES_PARAMETERS)
209
    hvparams = baserlib.MakeParamsDict(opts, constants.HVS_PARAMETERS)
210

    
211
    op = ganeti.opcodes.OpCreateInstance(
212
        instance_name=opts.get('name'),
213
        disk_size=opts.get('size', 20 * 1024),
214
        swap_size=opts.get('swap', 4 * 1024),
215
        disk_template=opts.get('disk_template', None),
216
        mode=constants.INSTANCE_CREATE,
217
        os_type=opts.get('os'),
218
        pnode=opts.get('pnode'),
219
        snode=opts.get('snode'),
220
        ip=opts.get('ip', 'none'),
221
        bridge=opts.get('bridge', None),
222
        start=opts.get('start', True),
223
        ip_check=opts.get('ip_check', True),
224
        wait_for_sync=opts.get('wait_for_sync', True),
225
        mac=opts.get('mac', 'auto'),
226
        hypervisor=opts.get('hypervisor', None),
227
        hvparams=hvparams,
228
        beparams=beparams,
229
        iallocator=opts.get('iallocator', None),
230
        file_storage_dir=opts.get('file_storage_dir', None),
231
        file_driver=opts.get('file_driver', 'loop'),
232
        )
233

    
234
    job_id = ganeti.cli.SendJob([op])
235
    return job_id
236

    
237

    
238
class R_2_instances_name_reboot(baserlib.R_Generic):
239
  """/2/instances/[instance_name]/reboot resource.
240

241
  Implements an instance reboot.
242

243
  """
244

    
245
  DOC_URI = "/2/instances/[instance_name]/reboot"
246

    
247
  def GET(self):
248
    """Reboot an instance.
249

250
    The URI takes type=[hard|soft|full] and
251
    ignore_secondaries=[False|True] parameters.
252

253
    """
254
    instance_name = self.items[0]
255
    reboot_type = self.queryargs.get('type',
256
                                     [constants.INSTANCE_REBOOT_HARD])[0]
257
    ignore_secondaries = bool(self.queryargs.get('ignore_secondaries',
258
                                                 [False])[0])
259
    op = ganeti.opcodes.OpRebootInstance(
260
        instance_name=instance_name,
261
        reboot_type=reboot_type,
262
        ignore_secondaries=ignore_secondaries)
263

    
264
    job_id = ganeti.cli.SendJob([op])
265

    
266
    return job_id
267

    
268

    
269
class R_2_instances_name_startup(baserlib.R_Generic):
270
  """/2/instances/[instance_name]/startup resource.
271

272
  Implements an instance startup.
273

274
  """
275

    
276
  DOC_URI = "/2/instances/[instance_name]/startup"
277

    
278
  def GET(self):
279
    """Startup an instance.
280

281
    The URI takes force=[False|True] parameter to start the instance if even if
282
    secondary disks are failing.
283

284
    """
285
    instance_name = self.items[0]
286
    force_startup = bool(self.queryargs.get('force', [False])[0])
287
    op = ganeti.opcodes.OpStartupInstance(instance_name=instance_name,
288
                                          force=force_startup)
289

    
290
    job_id = ganeti.cli.SendJob([op])
291

    
292
    return job_id
293

    
294

    
295
class R_2_instances_name_shutdown(baserlib.R_Generic):
296
  """/2/instances/[instance_name]/shutdown resource.
297

298
  Implements an instance shutdown.
299

300
  """
301

    
302
  DOC_URI = "/2/instances/[instance_name]/shutdown"
303

    
304
  def GET(self):
305
    """Shutdown an instance.
306

307
    """
308
    instance_name = self.items[0]
309
    op = ganeti.opcodes.OpShutdownInstance(instance_name=instance_name)
310

    
311
    job_id = ganeti.cli.SendJob([op])
312

    
313
    return job_id
314

    
315

    
316
class R_2_instances_name_tags(baserlib.R_Generic):
317
  """/2/instances/[instance_name]/tags resource.
318

319
  Manages per-instance tags.
320

321
  """
322
  DOC_URI = "/2/instances/[instance_name]/tags"
323

    
324
  def GET(self):
325
    """Returns a list of instance tags.
326

327
    Example: ["tag1", "tag2", "tag3"]
328

329
    """
330
    return baserlib._Tags_GET(constants.TAG_INSTANCE, name=self.items[0])
331

    
332
  def POST(self):
333
    """Add a set of tags to the instance.
334

335
    The request as a list of strings should be POST to this URI. And you'll have
336
    back a job id.
337

338
    """
339
    return baserlib._Tags_POST(constants.TAG_INSTANCE,
340
                               self.post_data, name=self.items[0])
341

    
342
  def DELETE(self):
343
    """Delete a tag.
344

345
    In order to delete a set of tags from a instance, DELETE request should be
346
    addressed to URI like: /2/instances/[instance_name]/tags?tag=[tag]&tag=[tag]
347

348
    """
349
    if 'tag' not in self.queryargs:
350
      # no we not gonna delete all tags from an instance
351
      raise http.HTTPNotImplemented()
352
    return baserlib._Tags_DELETE(constants.TAG_INSTANCE,
353
                                 self.queryargs['tag'],
354
                                 name=self.items[0])