rlib2.py 9.63 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#
#

# Copyright (C) 2006, 2007, 2008 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""Remote API version 2 baserlib.library.

"""

import ganeti.opcodes
27
from ganeti import http
28
from ganeti import luxi
29
from ganeti import constants
Iustin Pop's avatar
Iustin Pop committed
30
from ganeti.rapi import baserlib
31

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

34
35
36
37
38
39
40
41
42
43
44
45

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

  """
  DOC_URI = "/2/jobs"

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

    Returns:
      A dictionary with jobs id and uri.
Iustin Pop's avatar
Iustin Pop committed
46

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    """
    fields = ["id"]
    # Convert the list of lists to the list of ids
    result = [job_id for [job_id] in luxi.Client().QueryJobs(None, fields)]
    return baserlib.BuildUriList(result, "/2/jobs/%s", uri_fields=("id", "uri"))


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

  """
  DOC_URI = "/2/jobs/[job_id]"

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

Iustin Pop's avatar
Iustin Pop committed
63
    Returns:
64
65
66
67
68
      A dictionary with job parameters.

    The result includes:
      id - job ID as a number
      status - current job status as a string
Iustin Pop's avatar
Iustin Pop committed
69
      ops - involved OpCodes as a list of dictionaries for each opcodes in
70
71
72
        the job
      opstatus - OpCodes status as a list
      opresult - OpCodes results as a list of lists
Iustin Pop's avatar
Iustin Pop committed
73

74
75
76
    """
    fields = ["id", "ops", "status", "opstatus", "opresult"]
    job_id = self.items[0]
Iustin Pop's avatar
Iustin Pop committed
77
    result = luxi.Client().QueryJobs([job_id, ], fields)[0]
78
79
    return baserlib.MapFields(fields, result)

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
80
81
82
83
84
85
86
87
  def DELETE(self):
    """Cancel not-yet-started job.

    """
    job_id = self.items[0]
    result = luxi.Client().CancelJob(job_id)
    return result

88
89
90
91
92
93

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

  """
  DOC_URI = "/2/nodes"
Iustin Pop's avatar
Iustin Pop committed
94

95
96
  def GET(self):
    """Returns a list of all nodes.
Iustin Pop's avatar
Iustin Pop committed
97

98
99
100
101
102
103
104
105
106
107
108
109
110
    Returns:
      A dictionary with 'name' and 'uri' keys for each of them.

    Example: [
        {
          "id": "node1.example.com",
          "uri": "\/instances\/node1.example.com"
        },
        {
          "id": "node2.example.com",
          "uri": "\/instances\/node2.example.com"
        }]

Iustin Pop's avatar
Iustin Pop committed
111
    If the optional 'bulk' argument is provided and set to 'true'
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    value (i.e '?bulk=1'), the output contains detailed
    information about nodes as a list.

    Example: [
        {
          "pinst_cnt": 1,
          "mfree": 31280,
          "mtotal": 32763,
          "name": "www.example.com",
          "tags": [],
          "mnode": 512,
          "dtotal": 5246208,
          "sinst_cnt": 2,
          "dfree": 5171712
        },
        ...
    ]

    """
131
132
133
    client = luxi.Client()
    nodesdata = client.QueryNodes([], ["name"])
    nodeslist = [row[0] for row in nodesdata]
Iustin Pop's avatar
Iustin Pop committed
134

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

139
    return baserlib.BuildUriList(nodeslist, "/2/nodes/%s",
Iustin Pop's avatar
Iustin Pop committed
140
                                 uri_fields=("id", "uri"))
141
142


143
144
145
146
147
148
149
150
class R_nodes(R_2_nodes):
  """/nodes resource

  """
  # TODO: Temporary resource will be deprecated
  DOC_URI = "/nodes"


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
class R_2_instances(baserlib.R_Generic):
  """/2/instances resource.

  """
  DOC_URI = "/2/instances"

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

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

    Example: [
        {
          "name": "web.example.com",
          "uri": "\/instances\/web.example.com"
        },
        {
          "name": "mail.example.com",
          "uri": "\/instances\/mail.example.com"
        }]

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

    Example: [
        {
           "status": "running",
           "bridge": "xen-br0",
           "name": "web.example.com",
           "tags": ["tag1", "tag2"],
           "admin_ram": 512,
           "sda_size": 20480,
           "pnode": "node1.example.com",
           "mac": "01:23:45:67:89:01",
           "sdb_size": 4096,
           "snodes": ["node2.example.com"],
           "disk_template": "drbd",
           "ip": null,
           "admin_state": true,
           "os": "debian-etch",
           "vcpus": 2,
           "oper_state": true
        },
        ...
    ]

    """
200
201
202
    client = luxi.Client()
    instancesdata = client.QueryInstances([], ["name"])
    instanceslist = [row[0] for row in instancesdata]
203
204


205
206
207
    if 'bulk' in self.queryargs:
      bulkdata = client.QueryInstances(instanceslist, I_FIELDS)
      return baserlib.MapBulkFields(bulkdata, I_FIELDS)
208
209
210
211
212

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

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
213
  def POST(self):
214
215
216
217
218
219
    """Create an instance.

    Returns:
      A job id.

    """
220
    opts = self.req.request_post_data
221

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
222
223
    beparams = baserlib.MakeParamsDict(opts, constants.BES_PARAMETERS)
    hvparams = baserlib.MakeParamsDict(opts, constants.HVS_PARAMETERS)
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

    op = ganeti.opcodes.OpCreateInstance(
        instance_name=opts.get('name'),
        disk_size=opts.get('size', 20 * 1024),
        swap_size=opts.get('swap', 4 * 1024),
        disk_template=opts.get('disk_template', None),
        mode=constants.INSTANCE_CREATE,
        os_type=opts.get('os'),
        pnode=opts.get('pnode'),
        snode=opts.get('snode'),
        ip=opts.get('ip', 'none'),
        bridge=opts.get('bridge', None),
        start=opts.get('start', True),
        ip_check=opts.get('ip_check', True),
        wait_for_sync=opts.get('wait_for_sync', True),
        mac=opts.get('mac', 'auto'),
        hypervisor=opts.get('hypervisor', None),
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
241
        hvparams=hvparams,
242
243
244
245
246
247
248
249
250
        beparams=beparams,
        iallocator=opts.get('iallocator', None),
        file_storage_dir=opts.get('file_storage_dir', None),
        file_driver=opts.get('file_driver', 'loop'),
        )

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

251

252
253
254
255
256
257
258
259
class R_instances(R_2_instances):
  """/instances resource.

  """
  # TODO: Temporary resource will be deprecated
  DOC_URI = "/instances"


260
261
262
263
264
265
266
267
268
class R_2_instances_name_reboot(baserlib.R_Generic):
  """/2/instances/[instance_name]/reboot resource.

  Implements an instance reboot.

  """

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

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
269
  def POST(self):
270
271
    """Reboot an instance.

272
273
274
    The URI takes type=[hard|soft|full] and
    ignore_secondaries=[False|True] parameters.

275
276
    """
    instance_name = self.items[0]
277
278
279
280
    reboot_type = self.queryargs.get('type',
                                     [constants.INSTANCE_REBOOT_HARD])[0]
    ignore_secondaries = bool(self.queryargs.get('ignore_secondaries',
                                                 [False])[0])
281
282
283
284
285
286
287
288
289
290
    op = ganeti.opcodes.OpRebootInstance(
        instance_name=instance_name,
        reboot_type=reboot_type,
        ignore_secondaries=ignore_secondaries)

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

    return job_id


291
292
293
294
295
296
297
298
299
class R_2_instances_name_startup(baserlib.R_Generic):
  """/2/instances/[instance_name]/startup resource.

  Implements an instance startup.

  """

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

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
300
  def PUT(self):
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
    """Startup an instance.

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

    """
    instance_name = self.items[0]
    force_startup = bool(self.queryargs.get('force', [False])[0])
    op = ganeti.opcodes.OpStartupInstance(instance_name=instance_name,
                                          force=force_startup)

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

    return job_id


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

  Implements an instance shutdown.

  """

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

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
326
  def PUT(self):
327
328
329
330
331
332
333
334
335
336
337
    """Shutdown an instance.

    """
    instance_name = self.items[0]
    op = ganeti.opcodes.OpShutdownInstance(instance_name=instance_name)

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

    return job_id


338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
class R_2_instances_name_tags(baserlib.R_Generic):
  """/2/instances/[instance_name]/tags resource.

  Manages per-instance tags.

  """
  DOC_URI = "/2/instances/[instance_name]/tags"

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

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

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

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
354
  def PUT(self):
355
356
    """Add a set of tags to the instance.

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
357
    The request as a list of strings should be PUT to this URI. And you'll have
358
359
360
    back a job id.

    """
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
361
    return baserlib._Tags_PUT(constants.TAG_INSTANCE,
362
                               self.post_data, name=self.items[0])
363
364
365
366
367
368
369
370
371
372

  def DELETE(self):
    """Delete a tag.

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

    """
    if 'tag' not in self.queryargs:
      # no we not gonna delete all tags from an instance
373
      raise http.HTTPNotImplemented()
374
375
376
    return baserlib._Tags_DELETE(constants.TAG_INSTANCE,
                                 self.queryargs['tag'],
                                 name=self.items[0])