root / lib / rapi / client.py @ 6ce90543
History | View | Annotate | Download (54.5 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2010, 2011, 2012 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 |
"""Ganeti RAPI client.
|
23 |
|
24 |
@attention: To use the RAPI client, the application B{must} call
|
25 |
C{pycurl.global_init} during initialization and
|
26 |
C{pycurl.global_cleanup} before exiting the process. This is very
|
27 |
important in multi-threaded programs. See curl_global_init(3) and
|
28 |
curl_global_cleanup(3) for details. The decorator L{UsesRapiClient}
|
29 |
can be used.
|
30 |
|
31 |
"""
|
32 |
|
33 |
# No Ganeti-specific modules should be imported. The RAPI client is supposed to
|
34 |
# be standalone.
|
35 |
|
36 |
import logging |
37 |
import simplejson |
38 |
import socket |
39 |
import urllib |
40 |
import threading |
41 |
import pycurl |
42 |
import time |
43 |
|
44 |
try:
|
45 |
from cStringIO import StringIO |
46 |
except ImportError: |
47 |
from StringIO import StringIO |
48 |
|
49 |
|
50 |
GANETI_RAPI_PORT = 5080
|
51 |
GANETI_RAPI_VERSION = 2
|
52 |
|
53 |
HTTP_DELETE = "DELETE"
|
54 |
HTTP_GET = "GET"
|
55 |
HTTP_PUT = "PUT"
|
56 |
HTTP_POST = "POST"
|
57 |
HTTP_OK = 200
|
58 |
HTTP_NOT_FOUND = 404
|
59 |
HTTP_APP_JSON = "application/json"
|
60 |
|
61 |
REPLACE_DISK_PRI = "replace_on_primary"
|
62 |
REPLACE_DISK_SECONDARY = "replace_on_secondary"
|
63 |
REPLACE_DISK_CHG = "replace_new_secondary"
|
64 |
REPLACE_DISK_AUTO = "replace_auto"
|
65 |
|
66 |
NODE_EVAC_PRI = "primary-only"
|
67 |
NODE_EVAC_SEC = "secondary-only"
|
68 |
NODE_EVAC_ALL = "all"
|
69 |
|
70 |
NODE_ROLE_DRAINED = "drained"
|
71 |
NODE_ROLE_MASTER_CANDIATE = "master-candidate"
|
72 |
NODE_ROLE_MASTER = "master"
|
73 |
NODE_ROLE_OFFLINE = "offline"
|
74 |
NODE_ROLE_REGULAR = "regular"
|
75 |
|
76 |
JOB_STATUS_QUEUED = "queued"
|
77 |
JOB_STATUS_WAITING = "waiting"
|
78 |
JOB_STATUS_CANCELING = "canceling"
|
79 |
JOB_STATUS_RUNNING = "running"
|
80 |
JOB_STATUS_CANCELED = "canceled"
|
81 |
JOB_STATUS_SUCCESS = "success"
|
82 |
JOB_STATUS_ERROR = "error"
|
83 |
JOB_STATUS_FINALIZED = frozenset([
|
84 |
JOB_STATUS_CANCELED, |
85 |
JOB_STATUS_SUCCESS, |
86 |
JOB_STATUS_ERROR, |
87 |
]) |
88 |
JOB_STATUS_ALL = frozenset([
|
89 |
JOB_STATUS_QUEUED, |
90 |
JOB_STATUS_WAITING, |
91 |
JOB_STATUS_CANCELING, |
92 |
JOB_STATUS_RUNNING, |
93 |
]) | JOB_STATUS_FINALIZED |
94 |
|
95 |
# Legacy name
|
96 |
JOB_STATUS_WAITLOCK = JOB_STATUS_WAITING |
97 |
|
98 |
# Internal constants
|
99 |
_REQ_DATA_VERSION_FIELD = "__version__"
|
100 |
_QPARAM_DRY_RUN = "dry-run"
|
101 |
_QPARAM_FORCE = "force"
|
102 |
|
103 |
# Feature strings
|
104 |
INST_CREATE_REQV1 = "instance-create-reqv1"
|
105 |
INST_REINSTALL_REQV1 = "instance-reinstall-reqv1"
|
106 |
NODE_MIGRATE_REQV1 = "node-migrate-reqv1"
|
107 |
NODE_EVAC_RES1 = "node-evac-res1"
|
108 |
|
109 |
# Old feature constant names in case they're references by users of this module
|
110 |
_INST_CREATE_REQV1 = INST_CREATE_REQV1 |
111 |
_INST_REINSTALL_REQV1 = INST_REINSTALL_REQV1 |
112 |
_NODE_MIGRATE_REQV1 = NODE_MIGRATE_REQV1 |
113 |
_NODE_EVAC_RES1 = NODE_EVAC_RES1 |
114 |
|
115 |
# Older pycURL versions don't have all error constants
|
116 |
try:
|
117 |
_CURLE_SSL_CACERT = pycurl.E_SSL_CACERT |
118 |
_CURLE_SSL_CACERT_BADFILE = pycurl.E_SSL_CACERT_BADFILE |
119 |
except AttributeError: |
120 |
_CURLE_SSL_CACERT = 60
|
121 |
_CURLE_SSL_CACERT_BADFILE = 77
|
122 |
|
123 |
_CURL_SSL_CERT_ERRORS = frozenset([
|
124 |
_CURLE_SSL_CACERT, |
125 |
_CURLE_SSL_CACERT_BADFILE, |
126 |
]) |
127 |
|
128 |
|
129 |
class Error(Exception): |
130 |
"""Base error class for this module.
|
131 |
|
132 |
"""
|
133 |
pass
|
134 |
|
135 |
|
136 |
class GanetiApiError(Error): |
137 |
"""Generic error raised from Ganeti API.
|
138 |
|
139 |
"""
|
140 |
def __init__(self, msg, code=None): |
141 |
Error.__init__(self, msg)
|
142 |
self.code = code
|
143 |
|
144 |
|
145 |
class CertificateError(GanetiApiError): |
146 |
"""Raised when a problem is found with the SSL certificate.
|
147 |
|
148 |
"""
|
149 |
pass
|
150 |
|
151 |
|
152 |
def _AppendIf(container, condition, value): |
153 |
"""Appends to a list if a condition evaluates to truth.
|
154 |
|
155 |
"""
|
156 |
if condition:
|
157 |
container.append(value) |
158 |
|
159 |
return condition
|
160 |
|
161 |
|
162 |
def _AppendDryRunIf(container, condition): |
163 |
"""Appends a "dry-run" parameter if a condition evaluates to truth.
|
164 |
|
165 |
"""
|
166 |
return _AppendIf(container, condition, (_QPARAM_DRY_RUN, 1)) |
167 |
|
168 |
|
169 |
def _AppendForceIf(container, condition): |
170 |
"""Appends a "force" parameter if a condition evaluates to truth.
|
171 |
|
172 |
"""
|
173 |
return _AppendIf(container, condition, (_QPARAM_FORCE, 1)) |
174 |
|
175 |
|
176 |
def _SetItemIf(container, condition, item, value): |
177 |
"""Sets an item if a condition evaluates to truth.
|
178 |
|
179 |
"""
|
180 |
if condition:
|
181 |
container[item] = value |
182 |
|
183 |
return condition
|
184 |
|
185 |
|
186 |
def UsesRapiClient(fn): |
187 |
"""Decorator for code using RAPI client to initialize pycURL.
|
188 |
|
189 |
"""
|
190 |
def wrapper(*args, **kwargs): |
191 |
# curl_global_init(3) and curl_global_cleanup(3) must be called with only
|
192 |
# one thread running. This check is just a safety measure -- it doesn't
|
193 |
# cover all cases.
|
194 |
assert threading.activeCount() == 1, \ |
195 |
"Found active threads when initializing pycURL"
|
196 |
|
197 |
pycurl.global_init(pycurl.GLOBAL_ALL) |
198 |
try:
|
199 |
return fn(*args, **kwargs)
|
200 |
finally:
|
201 |
pycurl.global_cleanup() |
202 |
|
203 |
return wrapper
|
204 |
|
205 |
|
206 |
def GenericCurlConfig(verbose=False, use_signal=False, |
207 |
use_curl_cabundle=False, cafile=None, capath=None, |
208 |
proxy=None, verify_hostname=False, |
209 |
connect_timeout=None, timeout=None, |
210 |
_pycurl_version_fn=pycurl.version_info): |
211 |
"""Curl configuration function generator.
|
212 |
|
213 |
@type verbose: bool
|
214 |
@param verbose: Whether to set cURL to verbose mode
|
215 |
@type use_signal: bool
|
216 |
@param use_signal: Whether to allow cURL to use signals
|
217 |
@type use_curl_cabundle: bool
|
218 |
@param use_curl_cabundle: Whether to use cURL's default CA bundle
|
219 |
@type cafile: string
|
220 |
@param cafile: In which file we can find the certificates
|
221 |
@type capath: string
|
222 |
@param capath: In which directory we can find the certificates
|
223 |
@type proxy: string
|
224 |
@param proxy: Proxy to use, None for default behaviour and empty string for
|
225 |
disabling proxies (see curl_easy_setopt(3))
|
226 |
@type verify_hostname: bool
|
227 |
@param verify_hostname: Whether to verify the remote peer certificate's
|
228 |
commonName
|
229 |
@type connect_timeout: number
|
230 |
@param connect_timeout: Timeout for establishing connection in seconds
|
231 |
@type timeout: number
|
232 |
@param timeout: Timeout for complete transfer in seconds (see
|
233 |
curl_easy_setopt(3)).
|
234 |
|
235 |
"""
|
236 |
if use_curl_cabundle and (cafile or capath): |
237 |
raise Error("Can not use default CA bundle when CA file or path is set") |
238 |
|
239 |
def _ConfigCurl(curl, logger): |
240 |
"""Configures a cURL object
|
241 |
|
242 |
@type curl: pycurl.Curl
|
243 |
@param curl: cURL object
|
244 |
|
245 |
"""
|
246 |
logger.debug("Using cURL version %s", pycurl.version)
|
247 |
|
248 |
# pycurl.version_info returns a tuple with information about the used
|
249 |
# version of libcurl. Item 5 is the SSL library linked to it.
|
250 |
# e.g.: (3, '7.18.0', 463360, 'x86_64-pc-linux-gnu', 1581, 'GnuTLS/2.0.4',
|
251 |
# 0, '1.2.3.3', ...)
|
252 |
sslver = _pycurl_version_fn()[5]
|
253 |
if not sslver: |
254 |
raise Error("No SSL support in cURL") |
255 |
|
256 |
lcsslver = sslver.lower() |
257 |
if lcsslver.startswith("openssl/"): |
258 |
pass
|
259 |
elif lcsslver.startswith("nss/"): |
260 |
# TODO: investigate compatibility beyond a simple test
|
261 |
pass
|
262 |
elif lcsslver.startswith("gnutls/"): |
263 |
if capath:
|
264 |
raise Error("cURL linked against GnuTLS has no support for a" |
265 |
" CA path (%s)" % (pycurl.version, ))
|
266 |
else:
|
267 |
raise NotImplementedError("cURL uses unsupported SSL version '%s'" % |
268 |
sslver) |
269 |
|
270 |
curl.setopt(pycurl.VERBOSE, verbose) |
271 |
curl.setopt(pycurl.NOSIGNAL, not use_signal)
|
272 |
|
273 |
# Whether to verify remote peer's CN
|
274 |
if verify_hostname:
|
275 |
# curl_easy_setopt(3): "When CURLOPT_SSL_VERIFYHOST is 2, that
|
276 |
# certificate must indicate that the server is the server to which you
|
277 |
# meant to connect, or the connection fails. [...] When the value is 1,
|
278 |
# the certificate must contain a Common Name field, but it doesn't matter
|
279 |
# what name it says. [...]"
|
280 |
curl.setopt(pycurl.SSL_VERIFYHOST, 2)
|
281 |
else:
|
282 |
curl.setopt(pycurl.SSL_VERIFYHOST, 0)
|
283 |
|
284 |
if cafile or capath or use_curl_cabundle: |
285 |
# Require certificates to be checked
|
286 |
curl.setopt(pycurl.SSL_VERIFYPEER, True)
|
287 |
if cafile:
|
288 |
curl.setopt(pycurl.CAINFO, str(cafile))
|
289 |
if capath:
|
290 |
curl.setopt(pycurl.CAPATH, str(capath))
|
291 |
# Not changing anything for using default CA bundle
|
292 |
else:
|
293 |
# Disable SSL certificate verification
|
294 |
curl.setopt(pycurl.SSL_VERIFYPEER, False)
|
295 |
|
296 |
if proxy is not None: |
297 |
curl.setopt(pycurl.PROXY, str(proxy))
|
298 |
|
299 |
# Timeouts
|
300 |
if connect_timeout is not None: |
301 |
curl.setopt(pycurl.CONNECTTIMEOUT, connect_timeout) |
302 |
if timeout is not None: |
303 |
curl.setopt(pycurl.TIMEOUT, timeout) |
304 |
|
305 |
return _ConfigCurl
|
306 |
|
307 |
|
308 |
class GanetiRapiClient(object): # pylint: disable=R0904 |
309 |
"""Ganeti RAPI client.
|
310 |
|
311 |
"""
|
312 |
USER_AGENT = "Ganeti RAPI Client"
|
313 |
_json_encoder = simplejson.JSONEncoder(sort_keys=True)
|
314 |
|
315 |
def __init__(self, host, port=GANETI_RAPI_PORT, |
316 |
username=None, password=None, logger=logging, |
317 |
curl_config_fn=None, curl_factory=None): |
318 |
"""Initializes this class.
|
319 |
|
320 |
@type host: string
|
321 |
@param host: the ganeti cluster master to interact with
|
322 |
@type port: int
|
323 |
@param port: the port on which the RAPI is running (default is 5080)
|
324 |
@type username: string
|
325 |
@param username: the username to connect with
|
326 |
@type password: string
|
327 |
@param password: the password to connect with
|
328 |
@type curl_config_fn: callable
|
329 |
@param curl_config_fn: Function to configure C{pycurl.Curl} object
|
330 |
@param logger: Logging object
|
331 |
|
332 |
"""
|
333 |
self._username = username
|
334 |
self._password = password
|
335 |
self._logger = logger
|
336 |
self._curl_config_fn = curl_config_fn
|
337 |
self._curl_factory = curl_factory
|
338 |
|
339 |
try:
|
340 |
socket.inet_pton(socket.AF_INET6, host) |
341 |
address = "[%s]:%s" % (host, port)
|
342 |
except socket.error:
|
343 |
address = "%s:%s" % (host, port)
|
344 |
|
345 |
self._base_url = "https://%s" % address |
346 |
|
347 |
if username is not None: |
348 |
if password is None: |
349 |
raise Error("Password not specified") |
350 |
elif password:
|
351 |
raise Error("Specified password without username") |
352 |
|
353 |
def _CreateCurl(self): |
354 |
"""Creates a cURL object.
|
355 |
|
356 |
"""
|
357 |
# Create pycURL object if no factory is provided
|
358 |
if self._curl_factory: |
359 |
curl = self._curl_factory()
|
360 |
else:
|
361 |
curl = pycurl.Curl() |
362 |
|
363 |
# Default cURL settings
|
364 |
curl.setopt(pycurl.VERBOSE, False)
|
365 |
curl.setopt(pycurl.FOLLOWLOCATION, False)
|
366 |
curl.setopt(pycurl.MAXREDIRS, 5)
|
367 |
curl.setopt(pycurl.NOSIGNAL, True)
|
368 |
curl.setopt(pycurl.USERAGENT, self.USER_AGENT)
|
369 |
curl.setopt(pycurl.SSL_VERIFYHOST, 0)
|
370 |
curl.setopt(pycurl.SSL_VERIFYPEER, False)
|
371 |
curl.setopt(pycurl.HTTPHEADER, [ |
372 |
"Accept: %s" % HTTP_APP_JSON,
|
373 |
"Content-type: %s" % HTTP_APP_JSON,
|
374 |
]) |
375 |
|
376 |
assert ((self._username is None and self._password is None) ^ |
377 |
(self._username is not None and self._password is not None)) |
378 |
|
379 |
if self._username: |
380 |
# Setup authentication
|
381 |
curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC) |
382 |
curl.setopt(pycurl.USERPWD, |
383 |
str("%s:%s" % (self._username, self._password))) |
384 |
|
385 |
# Call external configuration function
|
386 |
if self._curl_config_fn: |
387 |
self._curl_config_fn(curl, self._logger) |
388 |
|
389 |
return curl
|
390 |
|
391 |
@staticmethod
|
392 |
def _EncodeQuery(query): |
393 |
"""Encode query values for RAPI URL.
|
394 |
|
395 |
@type query: list of two-tuples
|
396 |
@param query: Query arguments
|
397 |
@rtype: list
|
398 |
@return: Query list with encoded values
|
399 |
|
400 |
"""
|
401 |
result = [] |
402 |
|
403 |
for name, value in query: |
404 |
if value is None: |
405 |
result.append((name, ""))
|
406 |
|
407 |
elif isinstance(value, bool): |
408 |
# Boolean values must be encoded as 0 or 1
|
409 |
result.append((name, int(value)))
|
410 |
|
411 |
elif isinstance(value, (list, tuple, dict)): |
412 |
raise ValueError("Invalid query data type %r" % type(value).__name__) |
413 |
|
414 |
else:
|
415 |
result.append((name, value)) |
416 |
|
417 |
return result
|
418 |
|
419 |
def _SendRequest(self, method, path, query, content): |
420 |
"""Sends an HTTP request.
|
421 |
|
422 |
This constructs a full URL, encodes and decodes HTTP bodies, and
|
423 |
handles invalid responses in a pythonic way.
|
424 |
|
425 |
@type method: string
|
426 |
@param method: HTTP method to use
|
427 |
@type path: string
|
428 |
@param path: HTTP URL path
|
429 |
@type query: list of two-tuples
|
430 |
@param query: query arguments to pass to urllib.urlencode
|
431 |
@type content: str or None
|
432 |
@param content: HTTP body content
|
433 |
|
434 |
@rtype: str
|
435 |
@return: JSON-Decoded response
|
436 |
|
437 |
@raises CertificateError: If an invalid SSL certificate is found
|
438 |
@raises GanetiApiError: If an invalid response is returned
|
439 |
|
440 |
"""
|
441 |
assert path.startswith("/") |
442 |
|
443 |
curl = self._CreateCurl()
|
444 |
|
445 |
if content is not None: |
446 |
encoded_content = self._json_encoder.encode(content)
|
447 |
else:
|
448 |
encoded_content = ""
|
449 |
|
450 |
# Build URL
|
451 |
urlparts = [self._base_url, path]
|
452 |
if query:
|
453 |
urlparts.append("?")
|
454 |
urlparts.append(urllib.urlencode(self._EncodeQuery(query)))
|
455 |
|
456 |
url = "".join(urlparts)
|
457 |
|
458 |
self._logger.debug("Sending request %s %s (content=%r)", |
459 |
method, url, encoded_content) |
460 |
|
461 |
# Buffer for response
|
462 |
encoded_resp_body = StringIO() |
463 |
|
464 |
# Configure cURL
|
465 |
curl.setopt(pycurl.CUSTOMREQUEST, str(method))
|
466 |
curl.setopt(pycurl.URL, str(url))
|
467 |
curl.setopt(pycurl.POSTFIELDS, str(encoded_content))
|
468 |
curl.setopt(pycurl.WRITEFUNCTION, encoded_resp_body.write) |
469 |
|
470 |
try:
|
471 |
# Send request and wait for response
|
472 |
try:
|
473 |
curl.perform() |
474 |
except pycurl.error, err:
|
475 |
if err.args[0] in _CURL_SSL_CERT_ERRORS: |
476 |
raise CertificateError("SSL certificate error %s" % err, |
477 |
code=err.args[0])
|
478 |
|
479 |
raise GanetiApiError(str(err), code=err.args[0]) |
480 |
finally:
|
481 |
# Reset settings to not keep references to large objects in memory
|
482 |
# between requests
|
483 |
curl.setopt(pycurl.POSTFIELDS, "")
|
484 |
curl.setopt(pycurl.WRITEFUNCTION, lambda _: None) |
485 |
|
486 |
# Get HTTP response code
|
487 |
http_code = curl.getinfo(pycurl.RESPONSE_CODE) |
488 |
|
489 |
# Was anything written to the response buffer?
|
490 |
if encoded_resp_body.tell():
|
491 |
response_content = simplejson.loads(encoded_resp_body.getvalue()) |
492 |
else:
|
493 |
response_content = None
|
494 |
|
495 |
if http_code != HTTP_OK:
|
496 |
if isinstance(response_content, dict): |
497 |
msg = ("%s %s: %s" %
|
498 |
(response_content["code"],
|
499 |
response_content["message"],
|
500 |
response_content["explain"]))
|
501 |
else:
|
502 |
msg = str(response_content)
|
503 |
|
504 |
raise GanetiApiError(msg, code=http_code)
|
505 |
|
506 |
return response_content
|
507 |
|
508 |
def GetVersion(self): |
509 |
"""Gets the Remote API version running on the cluster.
|
510 |
|
511 |
@rtype: int
|
512 |
@return: Ganeti Remote API version
|
513 |
|
514 |
"""
|
515 |
return self._SendRequest(HTTP_GET, "/version", None, None) |
516 |
|
517 |
def GetFeatures(self): |
518 |
"""Gets the list of optional features supported by RAPI server.
|
519 |
|
520 |
@rtype: list
|
521 |
@return: List of optional features
|
522 |
|
523 |
"""
|
524 |
try:
|
525 |
return self._SendRequest(HTTP_GET, "/%s/features" % GANETI_RAPI_VERSION, |
526 |
None, None) |
527 |
except GanetiApiError, err:
|
528 |
# Older RAPI servers don't support this resource
|
529 |
if err.code == HTTP_NOT_FOUND:
|
530 |
return []
|
531 |
|
532 |
raise
|
533 |
|
534 |
def GetOperatingSystems(self): |
535 |
"""Gets the Operating Systems running in the Ganeti cluster.
|
536 |
|
537 |
@rtype: list of str
|
538 |
@return: operating systems
|
539 |
|
540 |
"""
|
541 |
return self._SendRequest(HTTP_GET, "/%s/os" % GANETI_RAPI_VERSION, |
542 |
None, None) |
543 |
|
544 |
def GetInfo(self): |
545 |
"""Gets info about the cluster.
|
546 |
|
547 |
@rtype: dict
|
548 |
@return: information about the cluster
|
549 |
|
550 |
"""
|
551 |
return self._SendRequest(HTTP_GET, "/%s/info" % GANETI_RAPI_VERSION, |
552 |
None, None) |
553 |
|
554 |
def RedistributeConfig(self): |
555 |
"""Tells the cluster to redistribute its configuration files.
|
556 |
|
557 |
@rtype: string
|
558 |
@return: job id
|
559 |
|
560 |
"""
|
561 |
return self._SendRequest(HTTP_PUT, |
562 |
"/%s/redistribute-config" % GANETI_RAPI_VERSION,
|
563 |
None, None) |
564 |
|
565 |
def ModifyCluster(self, **kwargs): |
566 |
"""Modifies cluster parameters.
|
567 |
|
568 |
More details for parameters can be found in the RAPI documentation.
|
569 |
|
570 |
@rtype: string
|
571 |
@return: job id
|
572 |
|
573 |
"""
|
574 |
body = kwargs |
575 |
|
576 |
return self._SendRequest(HTTP_PUT, |
577 |
"/%s/modify" % GANETI_RAPI_VERSION, None, body) |
578 |
|
579 |
def GetClusterTags(self): |
580 |
"""Gets the cluster tags.
|
581 |
|
582 |
@rtype: list of str
|
583 |
@return: cluster tags
|
584 |
|
585 |
"""
|
586 |
return self._SendRequest(HTTP_GET, "/%s/tags" % GANETI_RAPI_VERSION, |
587 |
None, None) |
588 |
|
589 |
def AddClusterTags(self, tags, dry_run=False): |
590 |
"""Adds tags to the cluster.
|
591 |
|
592 |
@type tags: list of str
|
593 |
@param tags: tags to add to the cluster
|
594 |
@type dry_run: bool
|
595 |
@param dry_run: whether to perform a dry run
|
596 |
|
597 |
@rtype: string
|
598 |
@return: job id
|
599 |
|
600 |
"""
|
601 |
query = [("tag", t) for t in tags] |
602 |
_AppendDryRunIf(query, dry_run) |
603 |
|
604 |
return self._SendRequest(HTTP_PUT, "/%s/tags" % GANETI_RAPI_VERSION, |
605 |
query, None)
|
606 |
|
607 |
def DeleteClusterTags(self, tags, dry_run=False): |
608 |
"""Deletes tags from the cluster.
|
609 |
|
610 |
@type tags: list of str
|
611 |
@param tags: tags to delete
|
612 |
@type dry_run: bool
|
613 |
@param dry_run: whether to perform a dry run
|
614 |
@rtype: string
|
615 |
@return: job id
|
616 |
|
617 |
"""
|
618 |
query = [("tag", t) for t in tags] |
619 |
_AppendDryRunIf(query, dry_run) |
620 |
|
621 |
return self._SendRequest(HTTP_DELETE, "/%s/tags" % GANETI_RAPI_VERSION, |
622 |
query, None)
|
623 |
|
624 |
def GetInstances(self, bulk=False): |
625 |
"""Gets information about instances on the cluster.
|
626 |
|
627 |
@type bulk: bool
|
628 |
@param bulk: whether to return all information about all instances
|
629 |
|
630 |
@rtype: list of dict or list of str
|
631 |
@return: if bulk is True, info about the instances, else a list of instances
|
632 |
|
633 |
"""
|
634 |
query = [] |
635 |
_AppendIf(query, bulk, ("bulk", 1)) |
636 |
|
637 |
instances = self._SendRequest(HTTP_GET,
|
638 |
"/%s/instances" % GANETI_RAPI_VERSION,
|
639 |
query, None)
|
640 |
if bulk:
|
641 |
return instances
|
642 |
else:
|
643 |
return [i["id"] for i in instances] |
644 |
|
645 |
def GetInstance(self, instance): |
646 |
"""Gets information about an instance.
|
647 |
|
648 |
@type instance: str
|
649 |
@param instance: instance whose info to return
|
650 |
|
651 |
@rtype: dict
|
652 |
@return: info about the instance
|
653 |
|
654 |
"""
|
655 |
return self._SendRequest(HTTP_GET, |
656 |
("/%s/instances/%s" %
|
657 |
(GANETI_RAPI_VERSION, instance)), None, None) |
658 |
|
659 |
def GetInstanceInfo(self, instance, static=None): |
660 |
"""Gets information about an instance.
|
661 |
|
662 |
@type instance: string
|
663 |
@param instance: Instance name
|
664 |
@rtype: string
|
665 |
@return: Job ID
|
666 |
|
667 |
"""
|
668 |
if static is not None: |
669 |
query = [("static", static)]
|
670 |
else:
|
671 |
query = None
|
672 |
|
673 |
return self._SendRequest(HTTP_GET, |
674 |
("/%s/instances/%s/info" %
|
675 |
(GANETI_RAPI_VERSION, instance)), query, None)
|
676 |
|
677 |
def CreateInstance(self, mode, name, disk_template, disks, nics, |
678 |
**kwargs): |
679 |
"""Creates a new instance.
|
680 |
|
681 |
More details for parameters can be found in the RAPI documentation.
|
682 |
|
683 |
@type mode: string
|
684 |
@param mode: Instance creation mode
|
685 |
@type name: string
|
686 |
@param name: Hostname of the instance to create
|
687 |
@type disk_template: string
|
688 |
@param disk_template: Disk template for instance (e.g. plain, diskless,
|
689 |
file, or drbd)
|
690 |
@type disks: list of dicts
|
691 |
@param disks: List of disk definitions
|
692 |
@type nics: list of dicts
|
693 |
@param nics: List of NIC definitions
|
694 |
@type dry_run: bool
|
695 |
@keyword dry_run: whether to perform a dry run
|
696 |
|
697 |
@rtype: string
|
698 |
@return: job id
|
699 |
|
700 |
"""
|
701 |
query = [] |
702 |
|
703 |
_AppendDryRunIf(query, kwargs.get("dry_run"))
|
704 |
|
705 |
if _INST_CREATE_REQV1 in self.GetFeatures(): |
706 |
# All required fields for request data version 1
|
707 |
body = { |
708 |
_REQ_DATA_VERSION_FIELD: 1,
|
709 |
"mode": mode,
|
710 |
"name": name,
|
711 |
"disk_template": disk_template,
|
712 |
"disks": disks,
|
713 |
"nics": nics,
|
714 |
} |
715 |
|
716 |
conflicts = set(kwargs.iterkeys()) & set(body.iterkeys()) |
717 |
if conflicts:
|
718 |
raise GanetiApiError("Required fields can not be specified as" |
719 |
" keywords: %s" % ", ".join(conflicts)) |
720 |
|
721 |
body.update((key, value) for key, value in kwargs.iteritems() |
722 |
if key != "dry_run") |
723 |
else:
|
724 |
raise GanetiApiError("Server does not support new-style (version 1)" |
725 |
" instance creation requests")
|
726 |
|
727 |
return self._SendRequest(HTTP_POST, "/%s/instances" % GANETI_RAPI_VERSION, |
728 |
query, body) |
729 |
|
730 |
def DeleteInstance(self, instance, dry_run=False): |
731 |
"""Deletes an instance.
|
732 |
|
733 |
@type instance: str
|
734 |
@param instance: the instance to delete
|
735 |
|
736 |
@rtype: string
|
737 |
@return: job id
|
738 |
|
739 |
"""
|
740 |
query = [] |
741 |
_AppendDryRunIf(query, dry_run) |
742 |
|
743 |
return self._SendRequest(HTTP_DELETE, |
744 |
("/%s/instances/%s" %
|
745 |
(GANETI_RAPI_VERSION, instance)), query, None)
|
746 |
|
747 |
def ModifyInstance(self, instance, **kwargs): |
748 |
"""Modifies an instance.
|
749 |
|
750 |
More details for parameters can be found in the RAPI documentation.
|
751 |
|
752 |
@type instance: string
|
753 |
@param instance: Instance name
|
754 |
@rtype: string
|
755 |
@return: job id
|
756 |
|
757 |
"""
|
758 |
body = kwargs |
759 |
|
760 |
return self._SendRequest(HTTP_PUT, |
761 |
("/%s/instances/%s/modify" %
|
762 |
(GANETI_RAPI_VERSION, instance)), None, body)
|
763 |
|
764 |
def ActivateInstanceDisks(self, instance, ignore_size=None): |
765 |
"""Activates an instance's disks.
|
766 |
|
767 |
@type instance: string
|
768 |
@param instance: Instance name
|
769 |
@type ignore_size: bool
|
770 |
@param ignore_size: Whether to ignore recorded size
|
771 |
@rtype: string
|
772 |
@return: job id
|
773 |
|
774 |
"""
|
775 |
query = [] |
776 |
_AppendIf(query, ignore_size, ("ignore_size", 1)) |
777 |
|
778 |
return self._SendRequest(HTTP_PUT, |
779 |
("/%s/instances/%s/activate-disks" %
|
780 |
(GANETI_RAPI_VERSION, instance)), query, None)
|
781 |
|
782 |
def DeactivateInstanceDisks(self, instance): |
783 |
"""Deactivates an instance's disks.
|
784 |
|
785 |
@type instance: string
|
786 |
@param instance: Instance name
|
787 |
@rtype: string
|
788 |
@return: job id
|
789 |
|
790 |
"""
|
791 |
return self._SendRequest(HTTP_PUT, |
792 |
("/%s/instances/%s/deactivate-disks" %
|
793 |
(GANETI_RAPI_VERSION, instance)), None, None) |
794 |
|
795 |
def RecreateInstanceDisks(self, instance, disks=None, nodes=None): |
796 |
"""Recreate an instance's disks.
|
797 |
|
798 |
@type instance: string
|
799 |
@param instance: Instance name
|
800 |
@type disks: list of int
|
801 |
@param disks: List of disk indexes
|
802 |
@type nodes: list of string
|
803 |
@param nodes: New instance nodes, if relocation is desired
|
804 |
@rtype: string
|
805 |
@return: job id
|
806 |
|
807 |
"""
|
808 |
body = {} |
809 |
_SetItemIf(body, disks is not None, "disks", disks) |
810 |
_SetItemIf(body, nodes is not None, "nodes", nodes) |
811 |
|
812 |
return self._SendRequest(HTTP_POST, |
813 |
("/%s/instances/%s/recreate-disks" %
|
814 |
(GANETI_RAPI_VERSION, instance)), None, body)
|
815 |
|
816 |
def GrowInstanceDisk(self, instance, disk, amount, wait_for_sync=None): |
817 |
"""Grows a disk of an instance.
|
818 |
|
819 |
More details for parameters can be found in the RAPI documentation.
|
820 |
|
821 |
@type instance: string
|
822 |
@param instance: Instance name
|
823 |
@type disk: integer
|
824 |
@param disk: Disk index
|
825 |
@type amount: integer
|
826 |
@param amount: Grow disk by this amount (MiB)
|
827 |
@type wait_for_sync: bool
|
828 |
@param wait_for_sync: Wait for disk to synchronize
|
829 |
@rtype: string
|
830 |
@return: job id
|
831 |
|
832 |
"""
|
833 |
body = { |
834 |
"amount": amount,
|
835 |
} |
836 |
|
837 |
_SetItemIf(body, wait_for_sync is not None, "wait_for_sync", wait_for_sync) |
838 |
|
839 |
return self._SendRequest(HTTP_POST, |
840 |
("/%s/instances/%s/disk/%s/grow" %
|
841 |
(GANETI_RAPI_VERSION, instance, disk)), |
842 |
None, body)
|
843 |
|
844 |
def GetInstanceTags(self, instance): |
845 |
"""Gets tags for an instance.
|
846 |
|
847 |
@type instance: str
|
848 |
@param instance: instance whose tags to return
|
849 |
|
850 |
@rtype: list of str
|
851 |
@return: tags for the instance
|
852 |
|
853 |
"""
|
854 |
return self._SendRequest(HTTP_GET, |
855 |
("/%s/instances/%s/tags" %
|
856 |
(GANETI_RAPI_VERSION, instance)), None, None) |
857 |
|
858 |
def AddInstanceTags(self, instance, tags, dry_run=False): |
859 |
"""Adds tags to an instance.
|
860 |
|
861 |
@type instance: str
|
862 |
@param instance: instance to add tags to
|
863 |
@type tags: list of str
|
864 |
@param tags: tags to add to the instance
|
865 |
@type dry_run: bool
|
866 |
@param dry_run: whether to perform a dry run
|
867 |
|
868 |
@rtype: string
|
869 |
@return: job id
|
870 |
|
871 |
"""
|
872 |
query = [("tag", t) for t in tags] |
873 |
_AppendDryRunIf(query, dry_run) |
874 |
|
875 |
return self._SendRequest(HTTP_PUT, |
876 |
("/%s/instances/%s/tags" %
|
877 |
(GANETI_RAPI_VERSION, instance)), query, None)
|
878 |
|
879 |
def DeleteInstanceTags(self, instance, tags, dry_run=False): |
880 |
"""Deletes tags from an instance.
|
881 |
|
882 |
@type instance: str
|
883 |
@param instance: instance to delete tags from
|
884 |
@type tags: list of str
|
885 |
@param tags: tags to delete
|
886 |
@type dry_run: bool
|
887 |
@param dry_run: whether to perform a dry run
|
888 |
@rtype: string
|
889 |
@return: job id
|
890 |
|
891 |
"""
|
892 |
query = [("tag", t) for t in tags] |
893 |
_AppendDryRunIf(query, dry_run) |
894 |
|
895 |
return self._SendRequest(HTTP_DELETE, |
896 |
("/%s/instances/%s/tags" %
|
897 |
(GANETI_RAPI_VERSION, instance)), query, None)
|
898 |
|
899 |
def RebootInstance(self, instance, reboot_type=None, ignore_secondaries=None, |
900 |
dry_run=False):
|
901 |
"""Reboots an instance.
|
902 |
|
903 |
@type instance: str
|
904 |
@param instance: instance to rebot
|
905 |
@type reboot_type: str
|
906 |
@param reboot_type: one of: hard, soft, full
|
907 |
@type ignore_secondaries: bool
|
908 |
@param ignore_secondaries: if True, ignores errors for the secondary node
|
909 |
while re-assembling disks (in hard-reboot mode only)
|
910 |
@type dry_run: bool
|
911 |
@param dry_run: whether to perform a dry run
|
912 |
@rtype: string
|
913 |
@return: job id
|
914 |
|
915 |
"""
|
916 |
query = [] |
917 |
_AppendDryRunIf(query, dry_run) |
918 |
_AppendIf(query, reboot_type, ("type", reboot_type))
|
919 |
_AppendIf(query, ignore_secondaries is not None, |
920 |
("ignore_secondaries", ignore_secondaries))
|
921 |
|
922 |
return self._SendRequest(HTTP_POST, |
923 |
("/%s/instances/%s/reboot" %
|
924 |
(GANETI_RAPI_VERSION, instance)), query, None)
|
925 |
|
926 |
def ShutdownInstance(self, instance, dry_run=False, no_remember=False, |
927 |
**kwargs): |
928 |
"""Shuts down an instance.
|
929 |
|
930 |
@type instance: str
|
931 |
@param instance: the instance to shut down
|
932 |
@type dry_run: bool
|
933 |
@param dry_run: whether to perform a dry run
|
934 |
@type no_remember: bool
|
935 |
@param no_remember: if true, will not record the state change
|
936 |
@rtype: string
|
937 |
@return: job id
|
938 |
|
939 |
"""
|
940 |
query = [] |
941 |
body = kwargs |
942 |
|
943 |
_AppendDryRunIf(query, dry_run) |
944 |
_AppendIf(query, no_remember, ("no-remember", 1)) |
945 |
|
946 |
return self._SendRequest(HTTP_PUT, |
947 |
("/%s/instances/%s/shutdown" %
|
948 |
(GANETI_RAPI_VERSION, instance)), query, body) |
949 |
|
950 |
def StartupInstance(self, instance, dry_run=False, no_remember=False): |
951 |
"""Starts up an instance.
|
952 |
|
953 |
@type instance: str
|
954 |
@param instance: the instance to start up
|
955 |
@type dry_run: bool
|
956 |
@param dry_run: whether to perform a dry run
|
957 |
@type no_remember: bool
|
958 |
@param no_remember: if true, will not record the state change
|
959 |
@rtype: string
|
960 |
@return: job id
|
961 |
|
962 |
"""
|
963 |
query = [] |
964 |
_AppendDryRunIf(query, dry_run) |
965 |
_AppendIf(query, no_remember, ("no-remember", 1)) |
966 |
|
967 |
return self._SendRequest(HTTP_PUT, |
968 |
("/%s/instances/%s/startup" %
|
969 |
(GANETI_RAPI_VERSION, instance)), query, None)
|
970 |
|
971 |
def ReinstallInstance(self, instance, os=None, no_startup=False, |
972 |
osparams=None):
|
973 |
"""Reinstalls an instance.
|
974 |
|
975 |
@type instance: str
|
976 |
@param instance: The instance to reinstall
|
977 |
@type os: str or None
|
978 |
@param os: The operating system to reinstall. If None, the instance's
|
979 |
current operating system will be installed again
|
980 |
@type no_startup: bool
|
981 |
@param no_startup: Whether to start the instance automatically
|
982 |
@rtype: string
|
983 |
@return: job id
|
984 |
|
985 |
"""
|
986 |
if _INST_REINSTALL_REQV1 in self.GetFeatures(): |
987 |
body = { |
988 |
"start": not no_startup, |
989 |
} |
990 |
_SetItemIf(body, os is not None, "os", os) |
991 |
_SetItemIf(body, osparams is not None, "osparams", osparams) |
992 |
return self._SendRequest(HTTP_POST, |
993 |
("/%s/instances/%s/reinstall" %
|
994 |
(GANETI_RAPI_VERSION, instance)), None, body)
|
995 |
|
996 |
# Use old request format
|
997 |
if osparams:
|
998 |
raise GanetiApiError("Server does not support specifying OS parameters" |
999 |
" for instance reinstallation")
|
1000 |
|
1001 |
query = [] |
1002 |
_AppendIf(query, os, ("os", os))
|
1003 |
_AppendIf(query, no_startup, ("nostartup", 1)) |
1004 |
|
1005 |
return self._SendRequest(HTTP_POST, |
1006 |
("/%s/instances/%s/reinstall" %
|
1007 |
(GANETI_RAPI_VERSION, instance)), query, None)
|
1008 |
|
1009 |
def ReplaceInstanceDisks(self, instance, disks=None, mode=REPLACE_DISK_AUTO, |
1010 |
remote_node=None, iallocator=None): |
1011 |
"""Replaces disks on an instance.
|
1012 |
|
1013 |
@type instance: str
|
1014 |
@param instance: instance whose disks to replace
|
1015 |
@type disks: list of ints
|
1016 |
@param disks: Indexes of disks to replace
|
1017 |
@type mode: str
|
1018 |
@param mode: replacement mode to use (defaults to replace_auto)
|
1019 |
@type remote_node: str or None
|
1020 |
@param remote_node: new secondary node to use (for use with
|
1021 |
replace_new_secondary mode)
|
1022 |
@type iallocator: str or None
|
1023 |
@param iallocator: instance allocator plugin to use (for use with
|
1024 |
replace_auto mode)
|
1025 |
|
1026 |
@rtype: string
|
1027 |
@return: job id
|
1028 |
|
1029 |
"""
|
1030 |
query = [ |
1031 |
("mode", mode),
|
1032 |
] |
1033 |
|
1034 |
# TODO: Convert to body parameters
|
1035 |
|
1036 |
if disks is not None: |
1037 |
_AppendIf(query, True,
|
1038 |
("disks", ",".join(str(idx) for idx in disks))) |
1039 |
|
1040 |
_AppendIf(query, remote_node is not None, ("remote_node", remote_node)) |
1041 |
_AppendIf(query, iallocator is not None, ("iallocator", iallocator)) |
1042 |
|
1043 |
return self._SendRequest(HTTP_POST, |
1044 |
("/%s/instances/%s/replace-disks" %
|
1045 |
(GANETI_RAPI_VERSION, instance)), query, None)
|
1046 |
|
1047 |
def PrepareExport(self, instance, mode): |
1048 |
"""Prepares an instance for an export.
|
1049 |
|
1050 |
@type instance: string
|
1051 |
@param instance: Instance name
|
1052 |
@type mode: string
|
1053 |
@param mode: Export mode
|
1054 |
@rtype: string
|
1055 |
@return: Job ID
|
1056 |
|
1057 |
"""
|
1058 |
query = [("mode", mode)]
|
1059 |
return self._SendRequest(HTTP_PUT, |
1060 |
("/%s/instances/%s/prepare-export" %
|
1061 |
(GANETI_RAPI_VERSION, instance)), query, None)
|
1062 |
|
1063 |
def ExportInstance(self, instance, mode, destination, shutdown=None, |
1064 |
remove_instance=None,
|
1065 |
x509_key_name=None, destination_x509_ca=None): |
1066 |
"""Exports an instance.
|
1067 |
|
1068 |
@type instance: string
|
1069 |
@param instance: Instance name
|
1070 |
@type mode: string
|
1071 |
@param mode: Export mode
|
1072 |
@rtype: string
|
1073 |
@return: Job ID
|
1074 |
|
1075 |
"""
|
1076 |
body = { |
1077 |
"destination": destination,
|
1078 |
"mode": mode,
|
1079 |
} |
1080 |
|
1081 |
_SetItemIf(body, shutdown is not None, "shutdown", shutdown) |
1082 |
_SetItemIf(body, remove_instance is not None, |
1083 |
"remove_instance", remove_instance)
|
1084 |
_SetItemIf(body, x509_key_name is not None, "x509_key_name", x509_key_name) |
1085 |
_SetItemIf(body, destination_x509_ca is not None, |
1086 |
"destination_x509_ca", destination_x509_ca)
|
1087 |
|
1088 |
return self._SendRequest(HTTP_PUT, |
1089 |
("/%s/instances/%s/export" %
|
1090 |
(GANETI_RAPI_VERSION, instance)), None, body)
|
1091 |
|
1092 |
def MigrateInstance(self, instance, mode=None, cleanup=None): |
1093 |
"""Migrates an instance.
|
1094 |
|
1095 |
@type instance: string
|
1096 |
@param instance: Instance name
|
1097 |
@type mode: string
|
1098 |
@param mode: Migration mode
|
1099 |
@type cleanup: bool
|
1100 |
@param cleanup: Whether to clean up a previously failed migration
|
1101 |
@rtype: string
|
1102 |
@return: job id
|
1103 |
|
1104 |
"""
|
1105 |
body = {} |
1106 |
_SetItemIf(body, mode is not None, "mode", mode) |
1107 |
_SetItemIf(body, cleanup is not None, "cleanup", cleanup) |
1108 |
|
1109 |
return self._SendRequest(HTTP_PUT, |
1110 |
("/%s/instances/%s/migrate" %
|
1111 |
(GANETI_RAPI_VERSION, instance)), None, body)
|
1112 |
|
1113 |
def FailoverInstance(self, instance, iallocator=None, |
1114 |
ignore_consistency=None, target_node=None): |
1115 |
"""Does a failover of an instance.
|
1116 |
|
1117 |
@type instance: string
|
1118 |
@param instance: Instance name
|
1119 |
@type iallocator: string
|
1120 |
@param iallocator: Iallocator for deciding the target node for
|
1121 |
shared-storage instances
|
1122 |
@type ignore_consistency: bool
|
1123 |
@param ignore_consistency: Whether to ignore disk consistency
|
1124 |
@type target_node: string
|
1125 |
@param target_node: Target node for shared-storage instances
|
1126 |
@rtype: string
|
1127 |
@return: job id
|
1128 |
|
1129 |
"""
|
1130 |
body = {} |
1131 |
_SetItemIf(body, iallocator is not None, "iallocator", iallocator) |
1132 |
_SetItemIf(body, ignore_consistency is not None, |
1133 |
"ignore_consistency", ignore_consistency)
|
1134 |
_SetItemIf(body, target_node is not None, "target_node", target_node) |
1135 |
|
1136 |
return self._SendRequest(HTTP_PUT, |
1137 |
("/%s/instances/%s/failover" %
|
1138 |
(GANETI_RAPI_VERSION, instance)), None, body)
|
1139 |
|
1140 |
def RenameInstance(self, instance, new_name, ip_check=None, name_check=None): |
1141 |
"""Changes the name of an instance.
|
1142 |
|
1143 |
@type instance: string
|
1144 |
@param instance: Instance name
|
1145 |
@type new_name: string
|
1146 |
@param new_name: New instance name
|
1147 |
@type ip_check: bool
|
1148 |
@param ip_check: Whether to ensure instance's IP address is inactive
|
1149 |
@type name_check: bool
|
1150 |
@param name_check: Whether to ensure instance's name is resolvable
|
1151 |
@rtype: string
|
1152 |
@return: job id
|
1153 |
|
1154 |
"""
|
1155 |
body = { |
1156 |
"new_name": new_name,
|
1157 |
} |
1158 |
|
1159 |
_SetItemIf(body, ip_check is not None, "ip_check", ip_check) |
1160 |
_SetItemIf(body, name_check is not None, "name_check", name_check) |
1161 |
|
1162 |
return self._SendRequest(HTTP_PUT, |
1163 |
("/%s/instances/%s/rename" %
|
1164 |
(GANETI_RAPI_VERSION, instance)), None, body)
|
1165 |
|
1166 |
def GetInstanceConsole(self, instance): |
1167 |
"""Request information for connecting to instance's console.
|
1168 |
|
1169 |
@type instance: string
|
1170 |
@param instance: Instance name
|
1171 |
@rtype: dict
|
1172 |
@return: dictionary containing information about instance's console
|
1173 |
|
1174 |
"""
|
1175 |
return self._SendRequest(HTTP_GET, |
1176 |
("/%s/instances/%s/console" %
|
1177 |
(GANETI_RAPI_VERSION, instance)), None, None) |
1178 |
|
1179 |
def GetJobs(self): |
1180 |
"""Gets all jobs for the cluster.
|
1181 |
|
1182 |
@rtype: list of int
|
1183 |
@return: job ids for the cluster
|
1184 |
|
1185 |
"""
|
1186 |
return [int(j["id"]) |
1187 |
for j in self._SendRequest(HTTP_GET, |
1188 |
"/%s/jobs" % GANETI_RAPI_VERSION,
|
1189 |
None, None)] |
1190 |
|
1191 |
def GetJobStatus(self, job_id): |
1192 |
"""Gets the status of a job.
|
1193 |
|
1194 |
@type job_id: string
|
1195 |
@param job_id: job id whose status to query
|
1196 |
|
1197 |
@rtype: dict
|
1198 |
@return: job status
|
1199 |
|
1200 |
"""
|
1201 |
return self._SendRequest(HTTP_GET, |
1202 |
"/%s/jobs/%s" % (GANETI_RAPI_VERSION, job_id),
|
1203 |
None, None) |
1204 |
|
1205 |
def WaitForJobCompletion(self, job_id, period=5, retries=-1): |
1206 |
"""Polls cluster for job status until completion.
|
1207 |
|
1208 |
Completion is defined as any of the following states listed in
|
1209 |
L{JOB_STATUS_FINALIZED}.
|
1210 |
|
1211 |
@type job_id: string
|
1212 |
@param job_id: job id to watch
|
1213 |
@type period: int
|
1214 |
@param period: how often to poll for status (optional, default 5s)
|
1215 |
@type retries: int
|
1216 |
@param retries: how many time to poll before giving up
|
1217 |
(optional, default -1 means unlimited)
|
1218 |
|
1219 |
@rtype: bool
|
1220 |
@return: C{True} if job succeeded or C{False} if failed/status timeout
|
1221 |
@deprecated: It is recommended to use L{WaitForJobChange} wherever
|
1222 |
possible; L{WaitForJobChange} returns immediately after a job changed and
|
1223 |
does not use polling
|
1224 |
|
1225 |
"""
|
1226 |
while retries != 0: |
1227 |
job_result = self.GetJobStatus(job_id)
|
1228 |
|
1229 |
if job_result and job_result["status"] == JOB_STATUS_SUCCESS: |
1230 |
return True |
1231 |
elif not job_result or job_result["status"] in JOB_STATUS_FINALIZED: |
1232 |
return False |
1233 |
|
1234 |
if period:
|
1235 |
time.sleep(period) |
1236 |
|
1237 |
if retries > 0: |
1238 |
retries -= 1
|
1239 |
|
1240 |
return False |
1241 |
|
1242 |
def WaitForJobChange(self, job_id, fields, prev_job_info, prev_log_serial): |
1243 |
"""Waits for job changes.
|
1244 |
|
1245 |
@type job_id: string
|
1246 |
@param job_id: Job ID for which to wait
|
1247 |
@return: C{None} if no changes have been detected and a dict with two keys,
|
1248 |
C{job_info} and C{log_entries} otherwise.
|
1249 |
@rtype: dict
|
1250 |
|
1251 |
"""
|
1252 |
body = { |
1253 |
"fields": fields,
|
1254 |
"previous_job_info": prev_job_info,
|
1255 |
"previous_log_serial": prev_log_serial,
|
1256 |
} |
1257 |
|
1258 |
return self._SendRequest(HTTP_GET, |
1259 |
"/%s/jobs/%s/wait" % (GANETI_RAPI_VERSION, job_id),
|
1260 |
None, body)
|
1261 |
|
1262 |
def CancelJob(self, job_id, dry_run=False): |
1263 |
"""Cancels a job.
|
1264 |
|
1265 |
@type job_id: string
|
1266 |
@param job_id: id of the job to delete
|
1267 |
@type dry_run: bool
|
1268 |
@param dry_run: whether to perform a dry run
|
1269 |
@rtype: tuple
|
1270 |
@return: tuple containing the result, and a message (bool, string)
|
1271 |
|
1272 |
"""
|
1273 |
query = [] |
1274 |
_AppendDryRunIf(query, dry_run) |
1275 |
|
1276 |
return self._SendRequest(HTTP_DELETE, |
1277 |
"/%s/jobs/%s" % (GANETI_RAPI_VERSION, job_id),
|
1278 |
query, None)
|
1279 |
|
1280 |
def GetNodes(self, bulk=False): |
1281 |
"""Gets all nodes in the cluster.
|
1282 |
|
1283 |
@type bulk: bool
|
1284 |
@param bulk: whether to return all information about all instances
|
1285 |
|
1286 |
@rtype: list of dict or str
|
1287 |
@return: if bulk is true, info about nodes in the cluster,
|
1288 |
else list of nodes in the cluster
|
1289 |
|
1290 |
"""
|
1291 |
query = [] |
1292 |
_AppendIf(query, bulk, ("bulk", 1)) |
1293 |
|
1294 |
nodes = self._SendRequest(HTTP_GET, "/%s/nodes" % GANETI_RAPI_VERSION, |
1295 |
query, None)
|
1296 |
if bulk:
|
1297 |
return nodes
|
1298 |
else:
|
1299 |
return [n["id"] for n in nodes] |
1300 |
|
1301 |
def GetNode(self, node): |
1302 |
"""Gets information about a node.
|
1303 |
|
1304 |
@type node: str
|
1305 |
@param node: node whose info to return
|
1306 |
|
1307 |
@rtype: dict
|
1308 |
@return: info about the node
|
1309 |
|
1310 |
"""
|
1311 |
return self._SendRequest(HTTP_GET, |
1312 |
"/%s/nodes/%s" % (GANETI_RAPI_VERSION, node),
|
1313 |
None, None) |
1314 |
|
1315 |
def EvacuateNode(self, node, iallocator=None, remote_node=None, |
1316 |
dry_run=False, early_release=None, |
1317 |
mode=None, accept_old=False): |
1318 |
"""Evacuates instances from a Ganeti node.
|
1319 |
|
1320 |
@type node: str
|
1321 |
@param node: node to evacuate
|
1322 |
@type iallocator: str or None
|
1323 |
@param iallocator: instance allocator to use
|
1324 |
@type remote_node: str
|
1325 |
@param remote_node: node to evaucate to
|
1326 |
@type dry_run: bool
|
1327 |
@param dry_run: whether to perform a dry run
|
1328 |
@type early_release: bool
|
1329 |
@param early_release: whether to enable parallelization
|
1330 |
@type mode: string
|
1331 |
@param mode: Node evacuation mode
|
1332 |
@type accept_old: bool
|
1333 |
@param accept_old: Whether caller is ready to accept old-style (pre-2.5)
|
1334 |
results
|
1335 |
|
1336 |
@rtype: string, or a list for pre-2.5 results
|
1337 |
@return: Job ID or, if C{accept_old} is set and server is pre-2.5,
|
1338 |
list of (job ID, instance name, new secondary node); if dry_run was
|
1339 |
specified, then the actual move jobs were not submitted and the job IDs
|
1340 |
will be C{None}
|
1341 |
|
1342 |
@raises GanetiApiError: if an iallocator and remote_node are both
|
1343 |
specified
|
1344 |
|
1345 |
"""
|
1346 |
if iallocator and remote_node: |
1347 |
raise GanetiApiError("Only one of iallocator or remote_node can be used") |
1348 |
|
1349 |
query = [] |
1350 |
_AppendDryRunIf(query, dry_run) |
1351 |
|
1352 |
if _NODE_EVAC_RES1 in self.GetFeatures(): |
1353 |
# Server supports body parameters
|
1354 |
body = {} |
1355 |
|
1356 |
_SetItemIf(body, iallocator is not None, "iallocator", iallocator) |
1357 |
_SetItemIf(body, remote_node is not None, "remote_node", remote_node) |
1358 |
_SetItemIf(body, early_release is not None, |
1359 |
"early_release", early_release)
|
1360 |
_SetItemIf(body, mode is not None, "mode", mode) |
1361 |
else:
|
1362 |
# Pre-2.5 request format
|
1363 |
body = None
|
1364 |
|
1365 |
if not accept_old: |
1366 |
raise GanetiApiError("Server is version 2.4 or earlier and caller does" |
1367 |
" not accept old-style results (parameter"
|
1368 |
" accept_old)")
|
1369 |
|
1370 |
# Pre-2.5 servers can only evacuate secondaries
|
1371 |
if mode is not None and mode != NODE_EVAC_SEC: |
1372 |
raise GanetiApiError("Server can only evacuate secondary instances") |
1373 |
|
1374 |
_AppendIf(query, iallocator, ("iallocator", iallocator))
|
1375 |
_AppendIf(query, remote_node, ("remote_node", remote_node))
|
1376 |
_AppendIf(query, early_release, ("early_release", 1)) |
1377 |
|
1378 |
return self._SendRequest(HTTP_POST, |
1379 |
("/%s/nodes/%s/evacuate" %
|
1380 |
(GANETI_RAPI_VERSION, node)), query, body) |
1381 |
|
1382 |
def MigrateNode(self, node, mode=None, dry_run=False, iallocator=None, |
1383 |
target_node=None):
|
1384 |
"""Migrates all primary instances from a node.
|
1385 |
|
1386 |
@type node: str
|
1387 |
@param node: node to migrate
|
1388 |
@type mode: string
|
1389 |
@param mode: if passed, it will overwrite the live migration type,
|
1390 |
otherwise the hypervisor default will be used
|
1391 |
@type dry_run: bool
|
1392 |
@param dry_run: whether to perform a dry run
|
1393 |
@type iallocator: string
|
1394 |
@param iallocator: instance allocator to use
|
1395 |
@type target_node: string
|
1396 |
@param target_node: Target node for shared-storage instances
|
1397 |
|
1398 |
@rtype: string
|
1399 |
@return: job id
|
1400 |
|
1401 |
"""
|
1402 |
query = [] |
1403 |
_AppendDryRunIf(query, dry_run) |
1404 |
|
1405 |
if _NODE_MIGRATE_REQV1 in self.GetFeatures(): |
1406 |
body = {} |
1407 |
|
1408 |
_SetItemIf(body, mode is not None, "mode", mode) |
1409 |
_SetItemIf(body, iallocator is not None, "iallocator", iallocator) |
1410 |
_SetItemIf(body, target_node is not None, "target_node", target_node) |
1411 |
|
1412 |
assert len(query) <= 1 |
1413 |
|
1414 |
return self._SendRequest(HTTP_POST, |
1415 |
("/%s/nodes/%s/migrate" %
|
1416 |
(GANETI_RAPI_VERSION, node)), query, body) |
1417 |
else:
|
1418 |
# Use old request format
|
1419 |
if target_node is not None: |
1420 |
raise GanetiApiError("Server does not support specifying target node" |
1421 |
" for node migration")
|
1422 |
|
1423 |
_AppendIf(query, mode is not None, ("mode", mode)) |
1424 |
|
1425 |
return self._SendRequest(HTTP_POST, |
1426 |
("/%s/nodes/%s/migrate" %
|
1427 |
(GANETI_RAPI_VERSION, node)), query, None)
|
1428 |
|
1429 |
def GetNodeRole(self, node): |
1430 |
"""Gets the current role for a node.
|
1431 |
|
1432 |
@type node: str
|
1433 |
@param node: node whose role to return
|
1434 |
|
1435 |
@rtype: str
|
1436 |
@return: the current role for a node
|
1437 |
|
1438 |
"""
|
1439 |
return self._SendRequest(HTTP_GET, |
1440 |
("/%s/nodes/%s/role" %
|
1441 |
(GANETI_RAPI_VERSION, node)), None, None) |
1442 |
|
1443 |
def SetNodeRole(self, node, role, force=False, auto_promote=None): |
1444 |
"""Sets the role for a node.
|
1445 |
|
1446 |
@type node: str
|
1447 |
@param node: the node whose role to set
|
1448 |
@type role: str
|
1449 |
@param role: the role to set for the node
|
1450 |
@type force: bool
|
1451 |
@param force: whether to force the role change
|
1452 |
@type auto_promote: bool
|
1453 |
@param auto_promote: Whether node(s) should be promoted to master candidate
|
1454 |
if necessary
|
1455 |
|
1456 |
@rtype: string
|
1457 |
@return: job id
|
1458 |
|
1459 |
"""
|
1460 |
query = [] |
1461 |
_AppendForceIf(query, force) |
1462 |
_AppendIf(query, auto_promote is not None, ("auto-promote", auto_promote)) |
1463 |
|
1464 |
return self._SendRequest(HTTP_PUT, |
1465 |
("/%s/nodes/%s/role" %
|
1466 |
(GANETI_RAPI_VERSION, node)), query, role) |
1467 |
|
1468 |
def PowercycleNode(self, node, force=False): |
1469 |
"""Powercycles a node.
|
1470 |
|
1471 |
@type node: string
|
1472 |
@param node: Node name
|
1473 |
@type force: bool
|
1474 |
@param force: Whether to force the operation
|
1475 |
@rtype: string
|
1476 |
@return: job id
|
1477 |
|
1478 |
"""
|
1479 |
query = [] |
1480 |
_AppendForceIf(query, force) |
1481 |
|
1482 |
return self._SendRequest(HTTP_POST, |
1483 |
("/%s/nodes/%s/powercycle" %
|
1484 |
(GANETI_RAPI_VERSION, node)), query, None)
|
1485 |
|
1486 |
def ModifyNode(self, node, **kwargs): |
1487 |
"""Modifies a node.
|
1488 |
|
1489 |
More details for parameters can be found in the RAPI documentation.
|
1490 |
|
1491 |
@type node: string
|
1492 |
@param node: Node name
|
1493 |
@rtype: string
|
1494 |
@return: job id
|
1495 |
|
1496 |
"""
|
1497 |
return self._SendRequest(HTTP_POST, |
1498 |
("/%s/nodes/%s/modify" %
|
1499 |
(GANETI_RAPI_VERSION, node)), None, kwargs)
|
1500 |
|
1501 |
def GetNodeStorageUnits(self, node, storage_type, output_fields): |
1502 |
"""Gets the storage units for a node.
|
1503 |
|
1504 |
@type node: str
|
1505 |
@param node: the node whose storage units to return
|
1506 |
@type storage_type: str
|
1507 |
@param storage_type: storage type whose units to return
|
1508 |
@type output_fields: str
|
1509 |
@param output_fields: storage type fields to return
|
1510 |
|
1511 |
@rtype: string
|
1512 |
@return: job id where results can be retrieved
|
1513 |
|
1514 |
"""
|
1515 |
query = [ |
1516 |
("storage_type", storage_type),
|
1517 |
("output_fields", output_fields),
|
1518 |
] |
1519 |
|
1520 |
return self._SendRequest(HTTP_GET, |
1521 |
("/%s/nodes/%s/storage" %
|
1522 |
(GANETI_RAPI_VERSION, node)), query, None)
|
1523 |
|
1524 |
def ModifyNodeStorageUnits(self, node, storage_type, name, allocatable=None): |
1525 |
"""Modifies parameters of storage units on the node.
|
1526 |
|
1527 |
@type node: str
|
1528 |
@param node: node whose storage units to modify
|
1529 |
@type storage_type: str
|
1530 |
@param storage_type: storage type whose units to modify
|
1531 |
@type name: str
|
1532 |
@param name: name of the storage unit
|
1533 |
@type allocatable: bool or None
|
1534 |
@param allocatable: Whether to set the "allocatable" flag on the storage
|
1535 |
unit (None=no modification, True=set, False=unset)
|
1536 |
|
1537 |
@rtype: string
|
1538 |
@return: job id
|
1539 |
|
1540 |
"""
|
1541 |
query = [ |
1542 |
("storage_type", storage_type),
|
1543 |
("name", name),
|
1544 |
] |
1545 |
|
1546 |
_AppendIf(query, allocatable is not None, ("allocatable", allocatable)) |
1547 |
|
1548 |
return self._SendRequest(HTTP_PUT, |
1549 |
("/%s/nodes/%s/storage/modify" %
|
1550 |
(GANETI_RAPI_VERSION, node)), query, None)
|
1551 |
|
1552 |
def RepairNodeStorageUnits(self, node, storage_type, name): |
1553 |
"""Repairs a storage unit on the node.
|
1554 |
|
1555 |
@type node: str
|
1556 |
@param node: node whose storage units to repair
|
1557 |
@type storage_type: str
|
1558 |
@param storage_type: storage type to repair
|
1559 |
@type name: str
|
1560 |
@param name: name of the storage unit to repair
|
1561 |
|
1562 |
@rtype: string
|
1563 |
@return: job id
|
1564 |
|
1565 |
"""
|
1566 |
query = [ |
1567 |
("storage_type", storage_type),
|
1568 |
("name", name),
|
1569 |
] |
1570 |
|
1571 |
return self._SendRequest(HTTP_PUT, |
1572 |
("/%s/nodes/%s/storage/repair" %
|
1573 |
(GANETI_RAPI_VERSION, node)), query, None)
|
1574 |
|
1575 |
def GetNodeTags(self, node): |
1576 |
"""Gets the tags for a node.
|
1577 |
|
1578 |
@type node: str
|
1579 |
@param node: node whose tags to return
|
1580 |
|
1581 |
@rtype: list of str
|
1582 |
@return: tags for the node
|
1583 |
|
1584 |
"""
|
1585 |
return self._SendRequest(HTTP_GET, |
1586 |
("/%s/nodes/%s/tags" %
|
1587 |
(GANETI_RAPI_VERSION, node)), None, None) |
1588 |
|
1589 |
def AddNodeTags(self, node, tags, dry_run=False): |
1590 |
"""Adds tags to a node.
|
1591 |
|
1592 |
@type node: str
|
1593 |
@param node: node to add tags to
|
1594 |
@type tags: list of str
|
1595 |
@param tags: tags to add to the node
|
1596 |
@type dry_run: bool
|
1597 |
@param dry_run: whether to perform a dry run
|
1598 |
|
1599 |
@rtype: string
|
1600 |
@return: job id
|
1601 |
|
1602 |
"""
|
1603 |
query = [("tag", t) for t in tags] |
1604 |
_AppendDryRunIf(query, dry_run) |
1605 |
|
1606 |
return self._SendRequest(HTTP_PUT, |
1607 |
("/%s/nodes/%s/tags" %
|
1608 |
(GANETI_RAPI_VERSION, node)), query, tags) |
1609 |
|
1610 |
def DeleteNodeTags(self, node, tags, dry_run=False): |
1611 |
"""Delete tags from a node.
|
1612 |
|
1613 |
@type node: str
|
1614 |
@param node: node to remove tags from
|
1615 |
@type tags: list of str
|
1616 |
@param tags: tags to remove from the node
|
1617 |
@type dry_run: bool
|
1618 |
@param dry_run: whether to perform a dry run
|
1619 |
|
1620 |
@rtype: string
|
1621 |
@return: job id
|
1622 |
|
1623 |
"""
|
1624 |
query = [("tag", t) for t in tags] |
1625 |
_AppendDryRunIf(query, dry_run) |
1626 |
|
1627 |
return self._SendRequest(HTTP_DELETE, |
1628 |
("/%s/nodes/%s/tags" %
|
1629 |
(GANETI_RAPI_VERSION, node)), query, None)
|
1630 |
|
1631 |
def GetGroups(self, bulk=False): |
1632 |
"""Gets all node groups in the cluster.
|
1633 |
|
1634 |
@type bulk: bool
|
1635 |
@param bulk: whether to return all information about the groups
|
1636 |
|
1637 |
@rtype: list of dict or str
|
1638 |
@return: if bulk is true, a list of dictionaries with info about all node
|
1639 |
groups in the cluster, else a list of names of those node groups
|
1640 |
|
1641 |
"""
|
1642 |
query = [] |
1643 |
_AppendIf(query, bulk, ("bulk", 1)) |
1644 |
|
1645 |
groups = self._SendRequest(HTTP_GET, "/%s/groups" % GANETI_RAPI_VERSION, |
1646 |
query, None)
|
1647 |
if bulk:
|
1648 |
return groups
|
1649 |
else:
|
1650 |
return [g["name"] for g in groups] |
1651 |
|
1652 |
def GetGroup(self, group): |
1653 |
"""Gets information about a node group.
|
1654 |
|
1655 |
@type group: str
|
1656 |
@param group: name of the node group whose info to return
|
1657 |
|
1658 |
@rtype: dict
|
1659 |
@return: info about the node group
|
1660 |
|
1661 |
"""
|
1662 |
return self._SendRequest(HTTP_GET, |
1663 |
"/%s/groups/%s" % (GANETI_RAPI_VERSION, group),
|
1664 |
None, None) |
1665 |
|
1666 |
def CreateGroup(self, name, alloc_policy=None, dry_run=False): |
1667 |
"""Creates a new node group.
|
1668 |
|
1669 |
@type name: str
|
1670 |
@param name: the name of node group to create
|
1671 |
@type alloc_policy: str
|
1672 |
@param alloc_policy: the desired allocation policy for the group, if any
|
1673 |
@type dry_run: bool
|
1674 |
@param dry_run: whether to peform a dry run
|
1675 |
|
1676 |
@rtype: string
|
1677 |
@return: job id
|
1678 |
|
1679 |
"""
|
1680 |
query = [] |
1681 |
_AppendDryRunIf(query, dry_run) |
1682 |
|
1683 |
body = { |
1684 |
"name": name,
|
1685 |
"alloc_policy": alloc_policy
|
1686 |
} |
1687 |
|
1688 |
return self._SendRequest(HTTP_POST, "/%s/groups" % GANETI_RAPI_VERSION, |
1689 |
query, body) |
1690 |
|
1691 |
def ModifyGroup(self, group, **kwargs): |
1692 |
"""Modifies a node group.
|
1693 |
|
1694 |
More details for parameters can be found in the RAPI documentation.
|
1695 |
|
1696 |
@type group: string
|
1697 |
@param group: Node group name
|
1698 |
@rtype: string
|
1699 |
@return: job id
|
1700 |
|
1701 |
"""
|
1702 |
return self._SendRequest(HTTP_PUT, |
1703 |
("/%s/groups/%s/modify" %
|
1704 |
(GANETI_RAPI_VERSION, group)), None, kwargs)
|
1705 |
|
1706 |
def DeleteGroup(self, group, dry_run=False): |
1707 |
"""Deletes a node group.
|
1708 |
|
1709 |
@type group: str
|
1710 |
@param group: the node group to delete
|
1711 |
@type dry_run: bool
|
1712 |
@param dry_run: whether to peform a dry run
|
1713 |
|
1714 |
@rtype: string
|
1715 |
@return: job id
|
1716 |
|
1717 |
"""
|
1718 |
query = [] |
1719 |
_AppendDryRunIf(query, dry_run) |
1720 |
|
1721 |
return self._SendRequest(HTTP_DELETE, |
1722 |
("/%s/groups/%s" %
|
1723 |
(GANETI_RAPI_VERSION, group)), query, None)
|
1724 |
|
1725 |
def RenameGroup(self, group, new_name): |
1726 |
"""Changes the name of a node group.
|
1727 |
|
1728 |
@type group: string
|
1729 |
@param group: Node group name
|
1730 |
@type new_name: string
|
1731 |
@param new_name: New node group name
|
1732 |
|
1733 |
@rtype: string
|
1734 |
@return: job id
|
1735 |
|
1736 |
"""
|
1737 |
body = { |
1738 |
"new_name": new_name,
|
1739 |
} |
1740 |
|
1741 |
return self._SendRequest(HTTP_PUT, |
1742 |
("/%s/groups/%s/rename" %
|
1743 |
(GANETI_RAPI_VERSION, group)), None, body)
|
1744 |
|
1745 |
def AssignGroupNodes(self, group, nodes, force=False, dry_run=False): |
1746 |
"""Assigns nodes to a group.
|
1747 |
|
1748 |
@type group: string
|
1749 |
@param group: Node group name
|
1750 |
@type nodes: list of strings
|
1751 |
@param nodes: List of nodes to assign to the group
|
1752 |
|
1753 |
@rtype: string
|
1754 |
@return: job id
|
1755 |
|
1756 |
"""
|
1757 |
query = [] |
1758 |
_AppendForceIf(query, force) |
1759 |
_AppendDryRunIf(query, dry_run) |
1760 |
|
1761 |
body = { |
1762 |
"nodes": nodes,
|
1763 |
} |
1764 |
|
1765 |
return self._SendRequest(HTTP_PUT, |
1766 |
("/%s/groups/%s/assign-nodes" %
|
1767 |
(GANETI_RAPI_VERSION, group)), query, body) |
1768 |
|
1769 |
def GetGroupTags(self, group): |
1770 |
"""Gets tags for a node group.
|
1771 |
|
1772 |
@type group: string
|
1773 |
@param group: Node group whose tags to return
|
1774 |
|
1775 |
@rtype: list of strings
|
1776 |
@return: tags for the group
|
1777 |
|
1778 |
"""
|
1779 |
return self._SendRequest(HTTP_GET, |
1780 |
("/%s/groups/%s/tags" %
|
1781 |
(GANETI_RAPI_VERSION, group)), None, None) |
1782 |
|
1783 |
def AddGroupTags(self, group, tags, dry_run=False): |
1784 |
"""Adds tags to a node group.
|
1785 |
|
1786 |
@type group: str
|
1787 |
@param group: group to add tags to
|
1788 |
@type tags: list of string
|
1789 |
@param tags: tags to add to the group
|
1790 |
@type dry_run: bool
|
1791 |
@param dry_run: whether to perform a dry run
|
1792 |
|
1793 |
@rtype: string
|
1794 |
@return: job id
|
1795 |
|
1796 |
"""
|
1797 |
query = [("tag", t) for t in tags] |
1798 |
_AppendDryRunIf(query, dry_run) |
1799 |
|
1800 |
return self._SendRequest(HTTP_PUT, |
1801 |
("/%s/groups/%s/tags" %
|
1802 |
(GANETI_RAPI_VERSION, group)), query, None)
|
1803 |
|
1804 |
def DeleteGroupTags(self, group, tags, dry_run=False): |
1805 |
"""Deletes tags from a node group.
|
1806 |
|
1807 |
@type group: str
|
1808 |
@param group: group to delete tags from
|
1809 |
@type tags: list of string
|
1810 |
@param tags: tags to delete
|
1811 |
@type dry_run: bool
|
1812 |
@param dry_run: whether to perform a dry run
|
1813 |
@rtype: string
|
1814 |
@return: job id
|
1815 |
|
1816 |
"""
|
1817 |
query = [("tag", t) for t in tags] |
1818 |
_AppendDryRunIf(query, dry_run) |
1819 |
|
1820 |
return self._SendRequest(HTTP_DELETE, |
1821 |
("/%s/groups/%s/tags" %
|
1822 |
(GANETI_RAPI_VERSION, group)), query, None)
|
1823 |
|
1824 |
def Query(self, what, fields, qfilter=None): |
1825 |
"""Retrieves information about resources.
|
1826 |
|
1827 |
@type what: string
|
1828 |
@param what: Resource name, one of L{constants.QR_VIA_RAPI}
|
1829 |
@type fields: list of string
|
1830 |
@param fields: Requested fields
|
1831 |
@type qfilter: None or list
|
1832 |
@param qfilter: Query filter
|
1833 |
|
1834 |
@rtype: string
|
1835 |
@return: job id
|
1836 |
|
1837 |
"""
|
1838 |
body = { |
1839 |
"fields": fields,
|
1840 |
} |
1841 |
|
1842 |
_SetItemIf(body, qfilter is not None, "qfilter", qfilter) |
1843 |
# TODO: remove "filter" after 2.7
|
1844 |
_SetItemIf(body, qfilter is not None, "filter", qfilter) |
1845 |
|
1846 |
return self._SendRequest(HTTP_PUT, |
1847 |
("/%s/query/%s" %
|
1848 |
(GANETI_RAPI_VERSION, what)), None, body)
|
1849 |
|
1850 |
def QueryFields(self, what, fields=None): |
1851 |
"""Retrieves available fields for a resource.
|
1852 |
|
1853 |
@type what: string
|
1854 |
@param what: Resource name, one of L{constants.QR_VIA_RAPI}
|
1855 |
@type fields: list of string
|
1856 |
@param fields: Requested fields
|
1857 |
|
1858 |
@rtype: string
|
1859 |
@return: job id
|
1860 |
|
1861 |
"""
|
1862 |
query = [] |
1863 |
|
1864 |
if fields is not None: |
1865 |
_AppendIf(query, True, ("fields", ",".join(fields))) |
1866 |
|
1867 |
return self._SendRequest(HTTP_GET, |
1868 |
("/%s/query/%s/fields" %
|
1869 |
(GANETI_RAPI_VERSION, what)), query, None)
|