Revision 59b4eeef lib/rapi/baserlib.py

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

  

Also available in: Unified diff