rlib2.py 9.7 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
80
81
82
83
84
85
    return baserlib.MapFields(fields, result)


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

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

87
88
  def GET(self):
    """Returns a list of all nodes.
Iustin Pop's avatar
Iustin Pop committed
89

90
91
92
93
94
95
96
97
98
99
100
101
102
    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
103
    If the optional 'bulk' argument is provided and set to 'true'
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    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
        },
        ...
    ]

    """
    op = ganeti.opcodes.OpQueryNodes(output_fields=["name"], names=[])
    nodeslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
Iustin Pop's avatar
Iustin Pop committed
125

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

132
    return baserlib.BuildUriList(nodeslist, "/2/nodes/%s",
Iustin Pop's avatar
Iustin Pop committed
133
                                 uri_fields=("id", "uri"))
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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


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
        },
        ...
    ]

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

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


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

199
200
201
202
203
204
205
  def PUT(self):
    """Create an instance.

    Returns:
      A job id.

    """
206
    opts = self.req.request_post_data
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

    # beparams
    mem = opts.get('mem', None)
    vcpus = opts.get('vcpus', None)
    auto_balance = opts.get('auto_balance', None)

    beparams = {}

    for key, const in [(mem, constants.BE_MEMORY),
                       (vcpus, constants.BE_VCPUS),
                       (auto_balance, constants.BE_AUTO_BALANCE)]:
      if key is not None:
        beparams[const] = key

    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),
        hvparams=opts.get('hvparams', {}),
        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

247

248
249
250
251
252
253
254
255
256
257
258
259
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"

  def GET(self):
    """Reboot an instance.

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

263
264
    """
    instance_name = self.items[0]
265
266
267
268
    reboot_type = self.queryargs.get('type',
                                     [constants.INSTANCE_REBOOT_HARD])[0]
    ignore_secondaries = bool(self.queryargs.get('ignore_secondaries',
                                                 [False])[0])
269
270
271
272
273
274
275
276
277
278
    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


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
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"

  def GET(self):
    """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"

  def GET(self):
    """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


326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
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])

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

345
    The request as a list of strings should be POST to this URI. And you'll have
346
347
348
349
350
    back a job id.

    """
    return baserlib._Tags_POST(constants.TAG_INSTANCE,
                               self.post_data, name=self.items[0])
351
352
353
354
355
356
357
358
359
360

  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
361
      raise http.HTTPNotImplemented()
362
363
364
    return baserlib._Tags_DELETE(constants.TAG_INSTANCE,
                                 self.queryargs['tag'],
                                 name=self.items[0])