Revision 59b4eeef

b/lib/rapi/baserlib.py
23 23

  
24 24
"""
25 25

  
26
import logging
27

  
26 28
import ganeti.cli
27
import ganeti.opcodes
28 29

  
29 30
from ganeti import luxi
30 31
from ganeti import rapi
31 32
from ganeti import http
32 33
from ganeti import ssconf
33 34
from ganeti import constants
35
from ganeti import opcodes
36
from ganeti import errors
34 37

  
35 38

  
36 39
def BuildUriList(ids, uri_format, uri_fields=("name", "uri")):
......
85 88
  """
86 89
  if kind == constants.TAG_INSTANCE or kind == constants.TAG_NODE:
87 90
    if not name:
88
      raise HttpBadRequest("Missing name on tag request")
89
    cl = luxi.Client()
91
      raise http.HttpBadRequest("Missing name on tag request")
92
    cl = GetClient()
90 93
    if kind == constants.TAG_INSTANCE:
91 94
      fn = cl.QueryInstances
92 95
    else:
......
106 109
  """Helper function to set tags.
107 110

  
108 111
  """
109
  cl = luxi.Client()
110
  return cl.SubmitJob([ganeti.opcodes.OpAddTags(kind=kind, name=name,
111
                                                tags=tags)])
112
  return SubmitJob([opcodes.OpAddTags(kind=kind, name=name, tags=tags)])
112 113

  
113 114

  
114 115
def _Tags_DELETE(kind, tags, name=""):
115 116
  """Helper function to delete tags.
116 117

  
117 118
  """
118
  cl = luxi.Client()
119
  return cl.SubmitJob([ganeti.opcodes.OpDelTags(kind=kind, name=name,
120
                                                tags=tags)])
119
  return SubmitJob([opcodes.OpDelTags(kind=kind, name=name, tags=tags)])
121 120

  
122 121

  
123 122
def MapBulkFields(itemslist, fields):
......
163 162
  return result
164 163

  
165 164

  
165
def SubmitJob(op, cl=None):
166
  """Generic wrapper for submit job, for better http compatibility.
167

  
168
  @type op: list
169
  @param op: the list of opcodes for the job
170
  @type cl: None or luxi.Client
171
  @param cl: optional luxi client to use
172
  @rtype: string
173
  @return: the job ID
174

  
175
  """
176
  try:
177
    if cl is None:
178
      cl = GetClient()
179
    return cl.SubmitJob(op)
180
  except errors.JobQueueFull:
181
    raise http.HttpServiceUnavailable("Job queue is full, needs archiving")
182
  except errors.JobQueueDrainError:
183
    raise http.HttpServiceUnavailable("Job queue is drained, cannot submit")
184
  except luxi.NoMasterError, err:
185
    raise http.HttpBadGateway("Master seems to unreachable: %s" % str(err))
186
  except luxi.TimeoutError, err:
187
    raise http.HttpGatewayTimeout("Timeout while talking to the master"
188
                                  " daemon. Error: %s" % str(err))
189

  
190
def GetClient():
191
  """Geric wrapper for luxi.Client(), for better http compatiblity.
192

  
193
  """
194
  try:
195
    return luxi.Client()
196
  except luxi.NoMasterError, err:
197
    raise http.HttpBadGateway("Master seems to unreachable: %s" % str(err))
198

  
199

  
200
def FeedbackFn(ts, log_type, log_msg):
201
  """Feedback logging function for http case.
202

  
203
  We don't have a stdout for printing log messages, so log them to the
204
  http log at least.
205

  
206
  """
207
  logging.info("%s: %s", log_type, log_msg)
208

  
209

  
166 210
class R_Generic(object):
167 211
  """Generic class for resources.
168 212

  
b/lib/rapi/rlib2.py
23 23

  
24 24
"""
25 25

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

  
32 32

  
33

  
33 34
I_FIELDS = ["name", "admin_state", "os",
34 35
            "pnode", "snodes",
35 36
            "disk_template",
......
105 106
      }
106 107

  
107 108
    """
108
    client = luxi.Client()
109
    client = baserlib.GetClient()
109 110
    return client.QueryClusterInfo()
110 111

  
111 112

  
......
123 124
    Example: ["debian-etch"]
124 125

  
125 126
    """
126
    op = ganeti.opcodes.OpDiagnoseOS(output_fields=["name", "valid"],
127
                                     names=[])
128
    diagnose_data = ganeti.cli.SubmitOpCode(op)
127
    cl = baserlib.GetClient()
128
    op = opcodes.OpDiagnoseOS(output_fields=["name", "valid"], names=[])
129
    job_id = baserlib.SubmitJob([op], cl)
130
    # we use custom feedback function, instead of print we log the status
131
    result = cli.PollJob(job_id, cl, feedback_fn=baserlib.FeedbackFn)
132
    diagnose_data = result[0]
129 133

  
130 134
    if not isinstance(diagnose_data, list):
131
      raise http.HttpInternalServerError(message="Can't get OS list")
135
      raise http.HttpBadGateway(message="Can't get OS list")
132 136

  
133 137
    return [row[0] for row in diagnose_data if row[1]]
134 138

  
......
146 150

  
147 151
    """
148 152
    fields = ["id"]
153
    cl = baserlib.GetClient()
149 154
    # Convert the list of lists to the list of ids
150
    result = [job_id for [job_id] in luxi.Client().QueryJobs(None, fields)]
155
    result = [job_id for [job_id] in cl.QueryJobs(None, fields)]
151 156
    return baserlib.BuildUriList(result, "/2/jobs/%s",
152 157
                                 uri_fields=("id", "uri"))
153 158

  
......
176 181
              "received_ts", "start_ts", "end_ts",
177 182
              ]
178 183
    job_id = self.items[0]
179
    result = luxi.Client().QueryJobs([job_id, ], fields)[0]
184
    result = baserlib.GetClient().QueryJobs([job_id, ], fields)[0]
180 185
    if result is None:
181 186
      raise http.HttpNotFound()
182 187
    return baserlib.MapFields(fields, result)
......
186 191

  
187 192
    """
188 193
    job_id = self.items[0]
189
    result = luxi.Client().CancelJob(job_id)
194
    result = baserlib.GetClient().CancelJob(job_id)
190 195
    return result
191 196

  
192 197

  
......
237 242
    @return: a dictionary with 'name' and 'uri' keys for each of them
238 243

  
239 244
    """
240
    client = luxi.Client()
245
    client = baserlib.GetClient()
241 246

  
242 247
    if self.useBulk():
243 248
      bulkdata = client.QueryNodes([], N_FIELDS, False)
......
260 265

  
261 266
    """
262 267
    node_name = self.items[0]
263
    client = luxi.Client()
268
    client = baserlib.GetClient()
264 269
    result = client.QueryNodes(names=[node_name], fields=N_FIELDS,
265 270
                               use_locking=self.useLocking())
266 271

  
......
326 331
    @return: a dictionary with 'name' and 'uri' keys for each of them.
327 332

  
328 333
    """
329
    client = luxi.Client()
334
    client = baserlib.GetClient()
330 335

  
331 336
    use_locking = self.useLocking()
332 337
    if self.useBulk():
......
368 373
             "ip": fn("ip", None),
369 374
             "bridge": fn("bridge", None)}]
370 375

  
371
    op = ganeti.opcodes.OpCreateInstance(
372
        mode=constants.INSTANCE_CREATE,
373
        instance_name=fn('name'),
374
        disks=disks,
375
        disk_template=fn('disk_template'),
376
        os_type=fn('os'),
377
        pnode=fn('pnode', None),
378
        snode=fn('snode', None),
379
        iallocator=fn('iallocator', None),
380
        nics=nics,
381
        start=fn('start', True),
382
        ip_check=fn('ip_check', True),
383
        wait_for_sync=True,
384
        hypervisor=fn('hypervisor', None),
385
        hvparams=hvparams,
386
        beparams=beparams,
387
        file_storage_dir=fn('file_storage_dir', None),
388
        file_driver=fn('file_driver', 'loop'),
389
        )
390

  
391
    job_id = ganeti.cli.SendJob([op])
392
    return job_id
376
    op = opcodes.OpCreateInstance(
377
      mode=constants.INSTANCE_CREATE,
378
      instance_name=fn('name'),
379
      disks=disks,
380
      disk_template=fn('disk_template'),
381
      os_type=fn('os'),
382
      pnode=fn('pnode', None),
383
      snode=fn('snode', None),
384
      iallocator=fn('iallocator', None),
385
      nics=nics,
386
      start=fn('start', True),
387
      ip_check=fn('ip_check', True),
388
      wait_for_sync=True,
389
      hypervisor=fn('hypervisor', None),
390
      hvparams=hvparams,
391
      beparams=beparams,
392
      file_storage_dir=fn('file_storage_dir', None),
393
      file_driver=fn('file_driver', 'loop'),
394
      )
395

  
396
    return baserlib.SubmitJob([op])
393 397

  
394 398

  
395 399
class R_2_instances_name(baserlib.R_Generic):
......
402 406
    """Send information about an instance.
403 407

  
404 408
    """
405
    client = luxi.Client()
409
    client = baserlib.GetClient()
406 410
    instance_name = self.items[0]
407 411
    result = client.QueryInstances(names=[instance_name], fields=I_FIELDS,
408 412
                                   use_locking=self.useLocking())
......
413 417
    """Delete an instance.
414 418

  
415 419
    """
416
    op = ganeti.opcodes.OpRemoveInstance(instance_name=self.items[0],
417
                                         ignore_failures=False)
418
    job_id = ganeti.cli.SendJob([op])
419
    return job_id
420
    op = opcodes.OpRemoveInstance(instance_name=self.items[0],
421
                                  ignore_failures=False)
422
    return baserlib.SubmitJob([op])
420 423

  
421 424

  
422 425
class R_2_instances_name_reboot(baserlib.R_Generic):
......
440 443
                                     [constants.INSTANCE_REBOOT_HARD])[0]
441 444
    ignore_secondaries = bool(self.queryargs.get('ignore_secondaries',
442 445
                                                 [False])[0])
443
    op = ganeti.opcodes.OpRebootInstance(
444
        instance_name=instance_name,
445
        reboot_type=reboot_type,
446
        ignore_secondaries=ignore_secondaries)
447

  
448
    job_id = ganeti.cli.SendJob([op])
446
    op = opcodes.OpRebootInstance(instance_name=instance_name,
447
                                  reboot_type=reboot_type,
448
                                  ignore_secondaries=ignore_secondaries)
449 449

  
450
    return job_id
450
    return baserlib.SubmitJob([op])
451 451

  
452 452

  
453 453
class R_2_instances_name_startup(baserlib.R_Generic):
......
468 468
    """
469 469
    instance_name = self.items[0]
470 470
    force_startup = bool(self.queryargs.get('force', [False])[0])
471
    op = ganeti.opcodes.OpStartupInstance(instance_name=instance_name,
472
                                          force=force_startup)
471
    op = opcodes.OpStartupInstance(instance_name=instance_name,
472
                                   force=force_startup)
473 473

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

  
476
    return job_id
474
    return baserlib.SubmitJob([op])
477 475

  
478 476

  
479 477
class R_2_instances_name_shutdown(baserlib.R_Generic):
......
490 488

  
491 489
    """
492 490
    instance_name = self.items[0]
493
    op = ganeti.opcodes.OpShutdownInstance(instance_name=instance_name)
494

  
495
    job_id = ganeti.cli.SendJob([op])
491
    op = opcodes.OpShutdownInstance(instance_name=instance_name)
496 492

  
497
    return job_id
493
    return baserlib.SubmitJob([op])
498 494

  
499 495

  
500 496
class _R_Tags(baserlib.R_Generic):

Also available in: Unified diff