Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / rlib2.py @ a0dcf7c2

History | View | Annotate | Download (9.4 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
    client = luxi.Client()
132
    nodesdata = client.QueryNodes([], ["name"])
133
    nodeslist = [row[0] for row in nodesdata]
134

    
135
    if 'bulk' in self.queryargs:
136
      bulkdata = client.QueryNodes(nodeslist, N_FIELDS)
137
      return baserlib.MapBulkFields(bulkdata, N_FIELDS)
138

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

    
142

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

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

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

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

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

165
    If the optional 'bulk' argument is provided and set to 'true'
166
    value (i.e '?bulk=1'), the output contains detailed
167
    information about instances as a list.
168

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

191
    """
192
    client = luxi.Client()
193
    instancesdata = client.QueryInstances([], ["name"])
194
    instanceslist = [row[0] for row in instancesdata]
195

    
196

    
197
    if 'bulk' in self.queryargs:
198
      bulkdata = client.QueryInstances(instanceslist, I_FIELDS)
199
      return baserlib.MapBulkFields(bulkdata, I_FIELDS)
200

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

    
205
  def POST(self):
206
    """Create an instance.
207

208
    Returns:
209
      A job id.
210

211
    """
212
    opts = self.req.request_post_data
213

    
214
    beparams = baserlib.MakeParamsDict(opts, constants.BES_PARAMETERS)
215
    hvparams = baserlib.MakeParamsDict(opts, constants.HVS_PARAMETERS)
216

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

    
240
    job_id = ganeti.cli.SendJob([op])
241
    return job_id
242

    
243

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

247
  Implements an instance reboot.
248

249
  """
250

    
251
  DOC_URI = "/2/instances/[instance_name]/reboot"
252

    
253
  def POST(self):
254
    """Reboot an instance.
255

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

259
    """
260
    instance_name = self.items[0]
261
    reboot_type = self.queryargs.get('type',
262
                                     [constants.INSTANCE_REBOOT_HARD])[0]
263
    ignore_secondaries = bool(self.queryargs.get('ignore_secondaries',
264
                                                 [False])[0])
265
    op = ganeti.opcodes.OpRebootInstance(
266
        instance_name=instance_name,
267
        reboot_type=reboot_type,
268
        ignore_secondaries=ignore_secondaries)
269

    
270
    job_id = ganeti.cli.SendJob([op])
271

    
272
    return job_id
273

    
274

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

278
  Implements an instance startup.
279

280
  """
281

    
282
  DOC_URI = "/2/instances/[instance_name]/startup"
283

    
284
  def PUT(self):
285
    """Startup an instance.
286

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

290
    """
291
    instance_name = self.items[0]
292
    force_startup = bool(self.queryargs.get('force', [False])[0])
293
    op = ganeti.opcodes.OpStartupInstance(instance_name=instance_name,
294
                                          force=force_startup)
295

    
296
    job_id = ganeti.cli.SendJob([op])
297

    
298
    return job_id
299

    
300

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

304
  Implements an instance shutdown.
305

306
  """
307

    
308
  DOC_URI = "/2/instances/[instance_name]/shutdown"
309

    
310
  def PUT(self):
311
    """Shutdown an instance.
312

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

    
317
    job_id = ganeti.cli.SendJob([op])
318

    
319
    return job_id
320

    
321

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

325
  Manages per-instance tags.
326

327
  """
328
  DOC_URI = "/2/instances/[instance_name]/tags"
329

    
330
  def GET(self):
331
    """Returns a list of instance tags.
332

333
    Example: ["tag1", "tag2", "tag3"]
334

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

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

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

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

    
348
  def DELETE(self):
349
    """Delete a tag.
350

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

354
    """
355
    if 'tag' not in self.queryargs:
356
      # no we not gonna delete all tags from an instance
357
      raise http.HTTPNotImplemented()
358
    return baserlib._Tags_DELETE(constants.TAG_INSTANCE,
359
                                 self.queryargs['tag'],
360
                                 name=self.items[0])