4 # Copyright (C) 2010, 2011 Google Inc.
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.
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.
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
22 """Ganeti RAPI client.
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}
33 # No Ganeti-specific modules should be imported. The RAPI client is supposed to
45 from cStringIO import StringIO
47 from StringIO import StringIO
50 GANETI_RAPI_PORT = 5080
51 GANETI_RAPI_VERSION = 2
53 HTTP_DELETE = "DELETE"
59 HTTP_APP_JSON = "application/json"
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"
66 NODE_EVAC_PRI = "primary-only"
67 NODE_EVAC_SEC = "secondary-only"
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"
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([
88 JOB_STATUS_ALL = frozenset([
93 ]) | JOB_STATUS_FINALIZED
96 JOB_STATUS_WAITLOCK = JOB_STATUS_WAITING
99 _REQ_DATA_VERSION_FIELD = "__version__"
100 _QPARAM_DRY_RUN = "dry-run"
101 _QPARAM_FORCE = "force"
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"
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
115 # Older pycURL versions don't have all error constants
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
123 _CURL_SSL_CERT_ERRORS = frozenset([
125 _CURLE_SSL_CACERT_BADFILE,
129 class Error(Exception):
130 """Base error class for this module.
136 class CertificateError(Error):
137 """Raised when a problem is found with the SSL certificate.
143 class GanetiApiError(Error):
144 """Generic error raised from Ganeti API.
147 def __init__(self, msg, code=None):
148 Error.__init__(self, msg)
152 def _AppendIf(container, condition, value):
153 """Appends to a list if a condition evaluates to truth.
157 container.append(value)
162 def _AppendDryRunIf(container, condition):
163 """Appends a "dry-run" parameter if a condition evaluates to truth.
166 return _AppendIf(container, condition, (_QPARAM_DRY_RUN, 1))
169 def _AppendForceIf(container, condition):
170 """Appends a "force" parameter if a condition evaluates to truth.
173 return _AppendIf(container, condition, (_QPARAM_FORCE, 1))
176 def _SetItemIf(container, condition, item, value):
177 """Sets an item if a condition evaluates to truth.
181 container[item] = value
186 def UsesRapiClient(fn):
187 """Decorator for code using RAPI client to initialize pycURL.
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
194 assert threading.activeCount() == 1, \
195 "Found active threads when initializing pycURL"
197 pycurl.global_init(pycurl.GLOBAL_ALL)
199 return fn(*args, **kwargs)
201 pycurl.global_cleanup()
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.
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
220 @param cafile: In which file we can find the certificates
222 @param capath: In which directory we can find the certificates
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
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)).
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")
239 def _ConfigCurl(curl, logger):
240 """Configures a cURL object
242 @type curl: pycurl.Curl
243 @param curl: cURL object
246 logger.debug("Using cURL version %s", pycurl.version)
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',
252 sslver = _pycurl_version_fn()[5]
254 raise Error("No SSL support in cURL")
256 lcsslver = sslver.lower()
257 if lcsslver.startswith("openssl/"):
259 elif lcsslver.startswith("gnutls/"):
261 raise Error("cURL linked against GnuTLS has no support for a"
262 " CA path (%s)" % (pycurl.version, ))
264 raise NotImplementedError("cURL uses unsupported SSL version '%s'" %
267 curl.setopt(pycurl.VERBOSE, verbose)
268 curl.setopt(pycurl.NOSIGNAL, not use_signal)
270 # Whether to verify remote peer's CN
272 # curl_easy_setopt(3): "When CURLOPT_SSL_VERIFYHOST is 2, that
273 # certificate must indicate that the server is the server to which you
274 # meant to connect, or the connection fails. [...] When the value is 1,
275 # the certificate must contain a Common Name field, but it doesn't matter
276 # what name it says. [...]"
277 curl.setopt(pycurl.SSL_VERIFYHOST, 2)
279 curl.setopt(pycurl.SSL_VERIFYHOST, 0)
281 if cafile or capath or use_curl_cabundle:
282 # Require certificates to be checked
283 curl.setopt(pycurl.SSL_VERIFYPEER, True)
285 curl.setopt(pycurl.CAINFO, str(cafile))
287 curl.setopt(pycurl.CAPATH, str(capath))
288 # Not changing anything for using default CA bundle
290 # Disable SSL certificate verification
291 curl.setopt(pycurl.SSL_VERIFYPEER, False)
293 if proxy is not None:
294 curl.setopt(pycurl.PROXY, str(proxy))
297 if connect_timeout is not None:
298 curl.setopt(pycurl.CONNECTTIMEOUT, connect_timeout)
299 if timeout is not None:
300 curl.setopt(pycurl.TIMEOUT, timeout)
305 class GanetiRapiClient(object): # pylint: disable=R0904
306 """Ganeti RAPI client.
309 USER_AGENT = "Ganeti RAPI Client"
310 _json_encoder = simplejson.JSONEncoder(sort_keys=True)
312 def __init__(self, host, port=GANETI_RAPI_PORT,
313 username=None, password=None, logger=logging,
314 curl_config_fn=None, curl_factory=None):
315 """Initializes this class.
318 @param host: the ganeti cluster master to interact with
320 @param port: the port on which the RAPI is running (default is 5080)
321 @type username: string
322 @param username: the username to connect with
323 @type password: string
324 @param password: the password to connect with
325 @type curl_config_fn: callable
326 @param curl_config_fn: Function to configure C{pycurl.Curl} object
327 @param logger: Logging object
330 self._username = username
331 self._password = password
332 self._logger = logger
333 self._curl_config_fn = curl_config_fn
334 self._curl_factory = curl_factory
337 socket.inet_pton(socket.AF_INET6, host)
338 address = "[%s]:%s" % (host, port)
340 address = "%s:%s" % (host, port)
342 self._base_url = "https://%s" % address
344 if username is not None:
346 raise Error("Password not specified")
348 raise Error("Specified password without username")
350 def _CreateCurl(self):
351 """Creates a cURL object.
354 # Create pycURL object if no factory is provided
355 if self._curl_factory:
356 curl = self._curl_factory()
360 # Default cURL settings
361 curl.setopt(pycurl.VERBOSE, False)
362 curl.setopt(pycurl.FOLLOWLOCATION, False)
363 curl.setopt(pycurl.MAXREDIRS, 5)
364 curl.setopt(pycurl.NOSIGNAL, True)
365 curl.setopt(pycurl.USERAGENT, self.USER_AGENT)
366 curl.setopt(pycurl.SSL_VERIFYHOST, 0)
367 curl.setopt(pycurl.SSL_VERIFYPEER, False)
368 curl.setopt(pycurl.HTTPHEADER, [
369 "Accept: %s" % HTTP_APP_JSON,
370 "Content-type: %s" % HTTP_APP_JSON,
373 assert ((self._username is None and self._password is None) ^
374 (self._username is not None and self._password is not None))
377 # Setup authentication
378 curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
379 curl.setopt(pycurl.USERPWD,
380 str("%s:%s" % (self._username, self._password)))
382 # Call external configuration function
383 if self._curl_config_fn:
384 self._curl_config_fn(curl, self._logger)
389 def _EncodeQuery(query):
390 """Encode query values for RAPI URL.
392 @type query: list of two-tuples
393 @param query: Query arguments
395 @return: Query list with encoded values
400 for name, value in query:
402 result.append((name, ""))
404 elif isinstance(value, bool):
405 # Boolean values must be encoded as 0 or 1
406 result.append((name, int(value)))
408 elif isinstance(value, (list, tuple, dict)):
409 raise ValueError("Invalid query data type %r" % type(value).__name__)
412 result.append((name, value))
416 def _SendRequest(self, method, path, query, content):
417 """Sends an HTTP request.
419 This constructs a full URL, encodes and decodes HTTP bodies, and
420 handles invalid responses in a pythonic way.
423 @param method: HTTP method to use
425 @param path: HTTP URL path
426 @type query: list of two-tuples
427 @param query: query arguments to pass to urllib.urlencode
428 @type content: str or None
429 @param content: HTTP body content
432 @return: JSON-Decoded response
434 @raises CertificateError: If an invalid SSL certificate is found
435 @raises GanetiApiError: If an invalid response is returned
438 assert path.startswith("/")
440 curl = self._CreateCurl()
442 if content is not None:
443 encoded_content = self._json_encoder.encode(content)
448 urlparts = [self._base_url, path]
451 urlparts.append(urllib.urlencode(self._EncodeQuery(query)))
453 url = "".join(urlparts)
455 self._logger.debug("Sending request %s %s (content=%r)",
456 method, url, encoded_content)
458 # Buffer for response
459 encoded_resp_body = StringIO()
462 curl.setopt(pycurl.CUSTOMREQUEST, str(method))
463 curl.setopt(pycurl.URL, str(url))
464 curl.setopt(pycurl.POSTFIELDS, str(encoded_content))
465 curl.setopt(pycurl.WRITEFUNCTION, encoded_resp_body.write)
468 # Send request and wait for response
471 except pycurl.error, err:
472 if err.args[0] in _CURL_SSL_CERT_ERRORS:
473 raise CertificateError("SSL certificate error %s" % err)
475 raise GanetiApiError(str(err))
477 # Reset settings to not keep references to large objects in memory
479 curl.setopt(pycurl.POSTFIELDS, "")
480 curl.setopt(pycurl.WRITEFUNCTION, lambda _: None)
482 # Get HTTP response code
483 http_code = curl.getinfo(pycurl.RESPONSE_CODE)
485 # Was anything written to the response buffer?
486 if encoded_resp_body.tell():
487 response_content = simplejson.loads(encoded_resp_body.getvalue())
489 response_content = None
491 if http_code != HTTP_OK:
492 if isinstance(response_content, dict):
494 (response_content["code"],
495 response_content["message"],
496 response_content["explain"]))
498 msg = str(response_content)
500 raise GanetiApiError(msg, code=http_code)
502 return response_content
504 def GetVersion(self):
505 """Gets the Remote API version running on the cluster.
508 @return: Ganeti Remote API version
511 return self._SendRequest(HTTP_GET, "/version", None, None)
513 def GetFeatures(self):
514 """Gets the list of optional features supported by RAPI server.
517 @return: List of optional features
521 return self._SendRequest(HTTP_GET, "/%s/features" % GANETI_RAPI_VERSION,
523 except GanetiApiError, err:
524 # Older RAPI servers don't support this resource
525 if err.code == HTTP_NOT_FOUND:
530 def GetOperatingSystems(self):
531 """Gets the Operating Systems running in the Ganeti cluster.
534 @return: operating systems
537 return self._SendRequest(HTTP_GET, "/%s/os" % GANETI_RAPI_VERSION,
541 """Gets info about the cluster.
544 @return: information about the cluster
547 return self._SendRequest(HTTP_GET, "/%s/info" % GANETI_RAPI_VERSION,
550 def RedistributeConfig(self):
551 """Tells the cluster to redistribute its configuration files.
557 return self._SendRequest(HTTP_PUT,
558 "/%s/redistribute-config" % GANETI_RAPI_VERSION,
561 def ModifyCluster(self, **kwargs):
562 """Modifies cluster parameters.
564 More details for parameters can be found in the RAPI documentation.
572 return self._SendRequest(HTTP_PUT,
573 "/%s/modify" % GANETI_RAPI_VERSION, None, body)
575 def GetClusterTags(self):
576 """Gets the cluster tags.
579 @return: cluster tags
582 return self._SendRequest(HTTP_GET, "/%s/tags" % GANETI_RAPI_VERSION,
585 def AddClusterTags(self, tags, dry_run=False):
586 """Adds tags to the cluster.
588 @type tags: list of str
589 @param tags: tags to add to the cluster
591 @param dry_run: whether to perform a dry run
597 query = [("tag", t) for t in tags]
598 _AppendDryRunIf(query, dry_run)
600 return self._SendRequest(HTTP_PUT, "/%s/tags" % GANETI_RAPI_VERSION,
603 def DeleteClusterTags(self, tags, dry_run=False):
604 """Deletes tags from the cluster.
606 @type tags: list of str
607 @param tags: tags to delete
609 @param dry_run: whether to perform a dry run
614 query = [("tag", t) for t in tags]
615 _AppendDryRunIf(query, dry_run)
617 return self._SendRequest(HTTP_DELETE, "/%s/tags" % GANETI_RAPI_VERSION,
620 def GetInstances(self, bulk=False):
621 """Gets information about instances on the cluster.
624 @param bulk: whether to return all information about all instances
626 @rtype: list of dict or list of str
627 @return: if bulk is True, info about the instances, else a list of instances
631 _AppendIf(query, bulk, ("bulk", 1))
633 instances = self._SendRequest(HTTP_GET,
634 "/%s/instances" % GANETI_RAPI_VERSION,
639 return [i["id"] for i in instances]
641 def GetInstance(self, instance):
642 """Gets information about an instance.
645 @param instance: instance whose info to return
648 @return: info about the instance
651 return self._SendRequest(HTTP_GET,
652 ("/%s/instances/%s" %
653 (GANETI_RAPI_VERSION, instance)), None, None)
655 def GetInstanceInfo(self, instance, static=None):
656 """Gets information about an instance.
658 @type instance: string
659 @param instance: Instance name
664 if static is not None:
665 query = [("static", static)]
669 return self._SendRequest(HTTP_GET,
670 ("/%s/instances/%s/info" %
671 (GANETI_RAPI_VERSION, instance)), query, None)
673 def CreateInstance(self, mode, name, disk_template, disks, nics,
675 """Creates a new instance.
677 More details for parameters can be found in the RAPI documentation.
680 @param mode: Instance creation mode
682 @param name: Hostname of the instance to create
683 @type disk_template: string
684 @param disk_template: Disk template for instance (e.g. plain, diskless,
686 @type disks: list of dicts
687 @param disks: List of disk definitions
688 @type nics: list of dicts
689 @param nics: List of NIC definitions
691 @keyword dry_run: whether to perform a dry run
699 _AppendDryRunIf(query, kwargs.get("dry_run"))
701 if _INST_CREATE_REQV1 in self.GetFeatures():
702 # All required fields for request data version 1
704 _REQ_DATA_VERSION_FIELD: 1,
707 "disk_template": disk_template,
712 conflicts = set(kwargs.iterkeys()) & set(body.iterkeys())
714 raise GanetiApiError("Required fields can not be specified as"
715 " keywords: %s" % ", ".join(conflicts))
717 body.update((key, value) for key, value in kwargs.iteritems()
720 raise GanetiApiError("Server does not support new-style (version 1)"
721 " instance creation requests")
723 return self._SendRequest(HTTP_POST, "/%s/instances" % GANETI_RAPI_VERSION,
726 def DeleteInstance(self, instance, dry_run=False):
727 """Deletes an instance.
730 @param instance: the instance to delete
737 _AppendDryRunIf(query, dry_run)
739 return self._SendRequest(HTTP_DELETE,
740 ("/%s/instances/%s" %
741 (GANETI_RAPI_VERSION, instance)), query, None)
743 def ModifyInstance(self, instance, **kwargs):
744 """Modifies an instance.
746 More details for parameters can be found in the RAPI documentation.
748 @type instance: string
749 @param instance: Instance name
756 return self._SendRequest(HTTP_PUT,
757 ("/%s/instances/%s/modify" %
758 (GANETI_RAPI_VERSION, instance)), None, body)
760 def ActivateInstanceDisks(self, instance, ignore_size=None):
761 """Activates an instance's disks.
763 @type instance: string
764 @param instance: Instance name
765 @type ignore_size: bool
766 @param ignore_size: Whether to ignore recorded size
772 _AppendIf(query, ignore_size, ("ignore_size", 1))
774 return self._SendRequest(HTTP_PUT,
775 ("/%s/instances/%s/activate-disks" %
776 (GANETI_RAPI_VERSION, instance)), query, None)
778 def DeactivateInstanceDisks(self, instance):
779 """Deactivates an instance's disks.
781 @type instance: string
782 @param instance: Instance name
787 return self._SendRequest(HTTP_PUT,
788 ("/%s/instances/%s/deactivate-disks" %
789 (GANETI_RAPI_VERSION, instance)), None, None)
791 def RecreateInstanceDisks(self, instance, disks=None, nodes=None):
792 """Recreate an instance's disks.
794 @type instance: string
795 @param instance: Instance name
796 @type disks: list of int
797 @param disks: List of disk indexes
798 @type nodes: list of string
799 @param nodes: New instance nodes, if relocation is desired
805 _SetItemIf(body, disks is not None, "disks", disks)
806 _SetItemIf(body, nodes is not None, "nodes", nodes)
808 return self._SendRequest(HTTP_POST,
809 ("/%s/instances/%s/recreate-disks" %
810 (GANETI_RAPI_VERSION, instance)), None, body)
812 def GrowInstanceDisk(self, instance, disk, amount, wait_for_sync=None):
813 """Grows a disk of an instance.
815 More details for parameters can be found in the RAPI documentation.
817 @type instance: string
818 @param instance: Instance name
820 @param disk: Disk index
821 @type amount: integer
822 @param amount: Grow disk by this amount (MiB)
823 @type wait_for_sync: bool
824 @param wait_for_sync: Wait for disk to synchronize
833 _SetItemIf(body, wait_for_sync is not None, "wait_for_sync", wait_for_sync)
835 return self._SendRequest(HTTP_POST,
836 ("/%s/instances/%s/disk/%s/grow" %
837 (GANETI_RAPI_VERSION, instance, disk)),
840 def GetInstanceTags(self, instance):
841 """Gets tags for an instance.
844 @param instance: instance whose tags to return
847 @return: tags for the instance
850 return self._SendRequest(HTTP_GET,
851 ("/%s/instances/%s/tags" %
852 (GANETI_RAPI_VERSION, instance)), None, None)
854 def AddInstanceTags(self, instance, tags, dry_run=False):
855 """Adds tags to an instance.
858 @param instance: instance to add tags to
859 @type tags: list of str
860 @param tags: tags to add to the instance
862 @param dry_run: whether to perform a dry run
868 query = [("tag", t) for t in tags]
869 _AppendDryRunIf(query, dry_run)
871 return self._SendRequest(HTTP_PUT,
872 ("/%s/instances/%s/tags" %
873 (GANETI_RAPI_VERSION, instance)), query, None)
875 def DeleteInstanceTags(self, instance, tags, dry_run=False):
876 """Deletes tags from an instance.
879 @param instance: instance to delete tags from
880 @type tags: list of str
881 @param tags: tags to delete
883 @param dry_run: whether to perform a dry run
888 query = [("tag", t) for t in tags]
889 _AppendDryRunIf(query, dry_run)
891 return self._SendRequest(HTTP_DELETE,
892 ("/%s/instances/%s/tags" %
893 (GANETI_RAPI_VERSION, instance)), query, None)
895 def RebootInstance(self, instance, reboot_type=None, ignore_secondaries=None,
897 """Reboots an instance.
900 @param instance: instance to rebot
901 @type reboot_type: str
902 @param reboot_type: one of: hard, soft, full
903 @type ignore_secondaries: bool
904 @param ignore_secondaries: if True, ignores errors for the secondary node
905 while re-assembling disks (in hard-reboot mode only)
907 @param dry_run: whether to perform a dry run
913 _AppendDryRunIf(query, dry_run)
914 _AppendIf(query, reboot_type, ("type", reboot_type))
915 _AppendIf(query, ignore_secondaries is not None,
916 ("ignore_secondaries", ignore_secondaries))
918 return self._SendRequest(HTTP_POST,
919 ("/%s/instances/%s/reboot" %
920 (GANETI_RAPI_VERSION, instance)), query, None)
922 def ShutdownInstance(self, instance, dry_run=False, no_remember=False):
923 """Shuts down an instance.
926 @param instance: the instance to shut down
928 @param dry_run: whether to perform a dry run
929 @type no_remember: bool
930 @param no_remember: if true, will not record the state change
936 _AppendDryRunIf(query, dry_run)
937 _AppendIf(query, no_remember, ("no-remember", 1))
939 return self._SendRequest(HTTP_PUT,
940 ("/%s/instances/%s/shutdown" %
941 (GANETI_RAPI_VERSION, instance)), query, None)
943 def StartupInstance(self, instance, dry_run=False, no_remember=False):
944 """Starts up an instance.
947 @param instance: the instance to start up
949 @param dry_run: whether to perform a dry run
950 @type no_remember: bool
951 @param no_remember: if true, will not record the state change
957 _AppendDryRunIf(query, dry_run)
958 _AppendIf(query, no_remember, ("no-remember", 1))
960 return self._SendRequest(HTTP_PUT,
961 ("/%s/instances/%s/startup" %
962 (GANETI_RAPI_VERSION, instance)), query, None)
964 def ReinstallInstance(self, instance, os=None, no_startup=False,
966 """Reinstalls an instance.
969 @param instance: The instance to reinstall
970 @type os: str or None
971 @param os: The operating system to reinstall. If None, the instance's
972 current operating system will be installed again
973 @type no_startup: bool
974 @param no_startup: Whether to start the instance automatically
979 if _INST_REINSTALL_REQV1 in self.GetFeatures():
981 "start": not no_startup,
983 _SetItemIf(body, os is not None, "os", os)
984 _SetItemIf(body, osparams is not None, "osparams", osparams)
985 return self._SendRequest(HTTP_POST,
986 ("/%s/instances/%s/reinstall" %
987 (GANETI_RAPI_VERSION, instance)), None, body)
989 # Use old request format
991 raise GanetiApiError("Server does not support specifying OS parameters"
992 " for instance reinstallation")
995 _AppendIf(query, os, ("os", os))
996 _AppendIf(query, no_startup, ("nostartup", 1))
998 return self._SendRequest(HTTP_POST,
999 ("/%s/instances/%s/reinstall" %
1000 (GANETI_RAPI_VERSION, instance)), query, None)
1002 def ReplaceInstanceDisks(self, instance, disks=None, mode=REPLACE_DISK_AUTO,
1003 remote_node=None, iallocator=None):
1004 """Replaces disks on an instance.
1007 @param instance: instance whose disks to replace
1008 @type disks: list of ints
1009 @param disks: Indexes of disks to replace
1011 @param mode: replacement mode to use (defaults to replace_auto)
1012 @type remote_node: str or None
1013 @param remote_node: new secondary node to use (for use with
1014 replace_new_secondary mode)
1015 @type iallocator: str or None
1016 @param iallocator: instance allocator plugin to use (for use with
1027 # TODO: Convert to body parameters
1029 if disks is not None:
1030 _AppendIf(query, True,
1031 ("disks", ",".join(str(idx) for idx in disks)))
1033 _AppendIf(query, remote_node is not None, ("remote_node", remote_node))
1034 _AppendIf(query, iallocator is not None, ("iallocator", iallocator))
1036 return self._SendRequest(HTTP_POST,
1037 ("/%s/instances/%s/replace-disks" %
1038 (GANETI_RAPI_VERSION, instance)), query, None)
1040 def PrepareExport(self, instance, mode):
1041 """Prepares an instance for an export.
1043 @type instance: string
1044 @param instance: Instance name
1046 @param mode: Export mode
1051 query = [("mode", mode)]
1052 return self._SendRequest(HTTP_PUT,
1053 ("/%s/instances/%s/prepare-export" %
1054 (GANETI_RAPI_VERSION, instance)), query, None)
1056 def ExportInstance(self, instance, mode, destination, shutdown=None,
1057 remove_instance=None,
1058 x509_key_name=None, destination_x509_ca=None):
1059 """Exports an instance.
1061 @type instance: string
1062 @param instance: Instance name
1064 @param mode: Export mode
1070 "destination": destination,
1074 _SetItemIf(body, shutdown is not None, "shutdown", shutdown)
1075 _SetItemIf(body, remove_instance is not None,
1076 "remove_instance", remove_instance)
1077 _SetItemIf(body, x509_key_name is not None, "x509_key_name", x509_key_name)
1078 _SetItemIf(body, destination_x509_ca is not None,
1079 "destination_x509_ca", destination_x509_ca)
1081 return self._SendRequest(HTTP_PUT,
1082 ("/%s/instances/%s/export" %
1083 (GANETI_RAPI_VERSION, instance)), None, body)
1085 def MigrateInstance(self, instance, mode=None, cleanup=None):
1086 """Migrates an instance.
1088 @type instance: string
1089 @param instance: Instance name
1091 @param mode: Migration mode
1093 @param cleanup: Whether to clean up a previously failed migration
1099 _SetItemIf(body, mode is not None, "mode", mode)
1100 _SetItemIf(body, cleanup is not None, "cleanup", cleanup)
1102 return self._SendRequest(HTTP_PUT,
1103 ("/%s/instances/%s/migrate" %
1104 (GANETI_RAPI_VERSION, instance)), None, body)
1106 def FailoverInstance(self, instance, iallocator=None,
1107 ignore_consistency=None, target_node=None):
1108 """Does a failover of an instance.
1110 @type instance: string
1111 @param instance: Instance name
1112 @type iallocator: string
1113 @param iallocator: Iallocator for deciding the target node for
1114 shared-storage instances
1115 @type ignore_consistency: bool
1116 @param ignore_consistency: Whether to ignore disk consistency
1117 @type target_node: string
1118 @param target_node: Target node for shared-storage instances
1124 _SetItemIf(body, iallocator is not None, "iallocator", iallocator)
1125 _SetItemIf(body, ignore_consistency is not None,
1126 "ignore_consistency", ignore_consistency)
1127 _SetItemIf(body, target_node is not None, "target_node", target_node)
1129 return self._SendRequest(HTTP_PUT,
1130 ("/%s/instances/%s/failover" %
1131 (GANETI_RAPI_VERSION, instance)), None, body)
1133 def RenameInstance(self, instance, new_name, ip_check=None, name_check=None):
1134 """Changes the name of an instance.
1136 @type instance: string
1137 @param instance: Instance name
1138 @type new_name: string
1139 @param new_name: New instance name
1140 @type ip_check: bool
1141 @param ip_check: Whether to ensure instance's IP address is inactive
1142 @type name_check: bool
1143 @param name_check: Whether to ensure instance's name is resolvable
1149 "new_name": new_name,
1152 _SetItemIf(body, ip_check is not None, "ip_check", ip_check)
1153 _SetItemIf(body, name_check is not None, "name_check", name_check)
1155 return self._SendRequest(HTTP_PUT,
1156 ("/%s/instances/%s/rename" %
1157 (GANETI_RAPI_VERSION, instance)), None, body)
1159 def GetInstanceConsole(self, instance):
1160 """Request information for connecting to instance's console.
1162 @type instance: string
1163 @param instance: Instance name
1165 @return: dictionary containing information about instance's console
1168 return self._SendRequest(HTTP_GET,
1169 ("/%s/instances/%s/console" %
1170 (GANETI_RAPI_VERSION, instance)), None, None)
1173 """Gets all jobs for the cluster.
1176 @return: job ids for the cluster
1179 return [int(j["id"])
1180 for j in self._SendRequest(HTTP_GET,
1181 "/%s/jobs" % GANETI_RAPI_VERSION,
1184 def GetJobStatus(self, job_id):
1185 """Gets the status of a job.
1187 @type job_id: string
1188 @param job_id: job id whose status to query
1194 return self._SendRequest(HTTP_GET,
1195 "/%s/jobs/%s" % (GANETI_RAPI_VERSION, job_id),
1198 def WaitForJobCompletion(self, job_id, period=5, retries=-1):
1199 """Polls cluster for job status until completion.
1201 Completion is defined as any of the following states listed in
1202 L{JOB_STATUS_FINALIZED}.
1204 @type job_id: string
1205 @param job_id: job id to watch
1207 @param period: how often to poll for status (optional, default 5s)
1209 @param retries: how many time to poll before giving up
1210 (optional, default -1 means unlimited)
1213 @return: C{True} if job succeeded or C{False} if failed/status timeout
1214 @deprecated: It is recommended to use L{WaitForJobChange} wherever
1215 possible; L{WaitForJobChange} returns immediately after a job changed and
1216 does not use polling
1220 job_result = self.GetJobStatus(job_id)
1222 if job_result and job_result["status"] == JOB_STATUS_SUCCESS:
1224 elif not job_result or job_result["status"] in JOB_STATUS_FINALIZED:
1235 def WaitForJobChange(self, job_id, fields, prev_job_info, prev_log_serial):
1236 """Waits for job changes.
1238 @type job_id: string
1239 @param job_id: Job ID for which to wait
1240 @return: C{None} if no changes have been detected and a dict with two keys,
1241 C{job_info} and C{log_entries} otherwise.
1247 "previous_job_info": prev_job_info,
1248 "previous_log_serial": prev_log_serial,
1251 return self._SendRequest(HTTP_GET,
1252 "/%s/jobs/%s/wait" % (GANETI_RAPI_VERSION, job_id),
1255 def CancelJob(self, job_id, dry_run=False):
1258 @type job_id: string
1259 @param job_id: id of the job to delete
1261 @param dry_run: whether to perform a dry run
1263 @return: tuple containing the result, and a message (bool, string)
1267 _AppendDryRunIf(query, dry_run)
1269 return self._SendRequest(HTTP_DELETE,
1270 "/%s/jobs/%s" % (GANETI_RAPI_VERSION, job_id),
1273 def GetNodes(self, bulk=False):
1274 """Gets all nodes in the cluster.
1277 @param bulk: whether to return all information about all instances
1279 @rtype: list of dict or str
1280 @return: if bulk is true, info about nodes in the cluster,
1281 else list of nodes in the cluster
1285 _AppendIf(query, bulk, ("bulk", 1))
1287 nodes = self._SendRequest(HTTP_GET, "/%s/nodes" % GANETI_RAPI_VERSION,
1292 return [n["id"] for n in nodes]
1294 def GetNode(self, node):
1295 """Gets information about a node.
1298 @param node: node whose info to return
1301 @return: info about the node
1304 return self._SendRequest(HTTP_GET,
1305 "/%s/nodes/%s" % (GANETI_RAPI_VERSION, node),
1308 def EvacuateNode(self, node, iallocator=None, remote_node=None,
1309 dry_run=False, early_release=None,
1310 mode=None, accept_old=False):
1311 """Evacuates instances from a Ganeti node.
1314 @param node: node to evacuate
1315 @type iallocator: str or None
1316 @param iallocator: instance allocator to use
1317 @type remote_node: str
1318 @param remote_node: node to evaucate to
1320 @param dry_run: whether to perform a dry run
1321 @type early_release: bool
1322 @param early_release: whether to enable parallelization
1324 @param mode: Node evacuation mode
1325 @type accept_old: bool
1326 @param accept_old: Whether caller is ready to accept old-style (pre-2.5)
1329 @rtype: string, or a list for pre-2.5 results
1330 @return: Job ID or, if C{accept_old} is set and server is pre-2.5,
1331 list of (job ID, instance name, new secondary node); if dry_run was
1332 specified, then the actual move jobs were not submitted and the job IDs
1335 @raises GanetiApiError: if an iallocator and remote_node are both
1339 if iallocator and remote_node:
1340 raise GanetiApiError("Only one of iallocator or remote_node can be used")
1343 _AppendDryRunIf(query, dry_run)
1345 if _NODE_EVAC_RES1 in self.GetFeatures():
1346 # Server supports body parameters
1349 _SetItemIf(body, iallocator is not None, "iallocator", iallocator)
1350 _SetItemIf(body, remote_node is not None, "remote_node", remote_node)
1351 _SetItemIf(body, early_release is not None,
1352 "early_release", early_release)
1353 _SetItemIf(body, mode is not None, "mode", mode)
1355 # Pre-2.5 request format
1359 raise GanetiApiError("Server is version 2.4 or earlier and caller does"
1360 " not accept old-style results (parameter"
1363 # Pre-2.5 servers can only evacuate secondaries
1364 if mode is not None and mode != NODE_EVAC_SEC:
1365 raise GanetiApiError("Server can only evacuate secondary instances")
1367 _AppendIf(query, iallocator, ("iallocator", iallocator))
1368 _AppendIf(query, remote_node, ("remote_node", remote_node))
1369 _AppendIf(query, early_release, ("early_release", 1))
1371 return self._SendRequest(HTTP_POST,
1372 ("/%s/nodes/%s/evacuate" %
1373 (GANETI_RAPI_VERSION, node)), query, body)
1375 def MigrateNode(self, node, mode=None, dry_run=False, iallocator=None,
1377 """Migrates all primary instances from a node.
1380 @param node: node to migrate
1382 @param mode: if passed, it will overwrite the live migration type,
1383 otherwise the hypervisor default will be used
1385 @param dry_run: whether to perform a dry run
1386 @type iallocator: string
1387 @param iallocator: instance allocator to use
1388 @type target_node: string
1389 @param target_node: Target node for shared-storage instances
1396 _AppendDryRunIf(query, dry_run)
1398 if _NODE_MIGRATE_REQV1 in self.GetFeatures():
1401 _SetItemIf(body, mode is not None, "mode", mode)
1402 _SetItemIf(body, iallocator is not None, "iallocator", iallocator)
1403 _SetItemIf(body, target_node is not None, "target_node", target_node)
1405 assert len(query) <= 1
1407 return self._SendRequest(HTTP_POST,
1408 ("/%s/nodes/%s/migrate" %
1409 (GANETI_RAPI_VERSION, node)), query, body)
1411 # Use old request format
1412 if target_node is not None:
1413 raise GanetiApiError("Server does not support specifying target node"
1414 " for node migration")
1416 _AppendIf(query, mode is not None, ("mode", mode))
1418 return self._SendRequest(HTTP_POST,
1419 ("/%s/nodes/%s/migrate" %
1420 (GANETI_RAPI_VERSION, node)), query, None)
1422 def GetNodeRole(self, node):
1423 """Gets the current role for a node.
1426 @param node: node whose role to return
1429 @return: the current role for a node
1432 return self._SendRequest(HTTP_GET,
1433 ("/%s/nodes/%s/role" %
1434 (GANETI_RAPI_VERSION, node)), None, None)
1436 def SetNodeRole(self, node, role, force=False, auto_promote=None):
1437 """Sets the role for a node.
1440 @param node: the node whose role to set
1442 @param role: the role to set for the node
1444 @param force: whether to force the role change
1445 @type auto_promote: bool
1446 @param auto_promote: Whether node(s) should be promoted to master candidate
1454 _AppendForceIf(query, force)
1455 _AppendIf(query, auto_promote is not None, ("auto-promote", auto_promote))
1457 return self._SendRequest(HTTP_PUT,
1458 ("/%s/nodes/%s/role" %
1459 (GANETI_RAPI_VERSION, node)), query, role)
1461 def PowercycleNode(self, node, force=False):
1462 """Powercycles a node.
1465 @param node: Node name
1467 @param force: Whether to force the operation
1473 _AppendForceIf(query, force)
1475 return self._SendRequest(HTTP_POST,
1476 ("/%s/nodes/%s/powercycle" %
1477 (GANETI_RAPI_VERSION, node)), query, None)
1479 def ModifyNode(self, node, **kwargs):
1482 More details for parameters can be found in the RAPI documentation.
1485 @param node: Node name
1490 return self._SendRequest(HTTP_POST,
1491 ("/%s/nodes/%s/modify" %
1492 (GANETI_RAPI_VERSION, node)), None, kwargs)
1494 def GetNodeStorageUnits(self, node, storage_type, output_fields):
1495 """Gets the storage units for a node.
1498 @param node: the node whose storage units to return
1499 @type storage_type: str
1500 @param storage_type: storage type whose units to return
1501 @type output_fields: str
1502 @param output_fields: storage type fields to return
1505 @return: job id where results can be retrieved
1509 ("storage_type", storage_type),
1510 ("output_fields", output_fields),
1513 return self._SendRequest(HTTP_GET,
1514 ("/%s/nodes/%s/storage" %
1515 (GANETI_RAPI_VERSION, node)), query, None)
1517 def ModifyNodeStorageUnits(self, node, storage_type, name, allocatable=None):
1518 """Modifies parameters of storage units on the node.
1521 @param node: node whose storage units to modify
1522 @type storage_type: str
1523 @param storage_type: storage type whose units to modify
1525 @param name: name of the storage unit
1526 @type allocatable: bool or None
1527 @param allocatable: Whether to set the "allocatable" flag on the storage
1528 unit (None=no modification, True=set, False=unset)
1535 ("storage_type", storage_type),
1539 _AppendIf(query, allocatable is not None, ("allocatable", allocatable))
1541 return self._SendRequest(HTTP_PUT,
1542 ("/%s/nodes/%s/storage/modify" %
1543 (GANETI_RAPI_VERSION, node)), query, None)
1545 def RepairNodeStorageUnits(self, node, storage_type, name):
1546 """Repairs a storage unit on the node.
1549 @param node: node whose storage units to repair
1550 @type storage_type: str
1551 @param storage_type: storage type to repair
1553 @param name: name of the storage unit to repair
1560 ("storage_type", storage_type),
1564 return self._SendRequest(HTTP_PUT,
1565 ("/%s/nodes/%s/storage/repair" %
1566 (GANETI_RAPI_VERSION, node)), query, None)
1568 def GetNodeTags(self, node):
1569 """Gets the tags for a node.
1572 @param node: node whose tags to return
1575 @return: tags for the node
1578 return self._SendRequest(HTTP_GET,
1579 ("/%s/nodes/%s/tags" %
1580 (GANETI_RAPI_VERSION, node)), None, None)
1582 def AddNodeTags(self, node, tags, dry_run=False):
1583 """Adds tags to a node.
1586 @param node: node to add tags to
1587 @type tags: list of str
1588 @param tags: tags to add to the node
1590 @param dry_run: whether to perform a dry run
1596 query = [("tag", t) for t in tags]
1597 _AppendDryRunIf(query, dry_run)
1599 return self._SendRequest(HTTP_PUT,
1600 ("/%s/nodes/%s/tags" %
1601 (GANETI_RAPI_VERSION, node)), query, tags)
1603 def DeleteNodeTags(self, node, tags, dry_run=False):
1604 """Delete tags from a node.
1607 @param node: node to remove tags from
1608 @type tags: list of str
1609 @param tags: tags to remove from the node
1611 @param dry_run: whether to perform a dry run
1617 query = [("tag", t) for t in tags]
1618 _AppendDryRunIf(query, dry_run)
1620 return self._SendRequest(HTTP_DELETE,
1621 ("/%s/nodes/%s/tags" %
1622 (GANETI_RAPI_VERSION, node)), query, None)
1624 def GetGroups(self, bulk=False):
1625 """Gets all node groups in the cluster.
1628 @param bulk: whether to return all information about the groups
1630 @rtype: list of dict or str
1631 @return: if bulk is true, a list of dictionaries with info about all node
1632 groups in the cluster, else a list of names of those node groups
1636 _AppendIf(query, bulk, ("bulk", 1))
1638 groups = self._SendRequest(HTTP_GET, "/%s/groups" % GANETI_RAPI_VERSION,
1643 return [g["name"] for g in groups]
1645 def GetGroup(self, group):
1646 """Gets information about a node group.
1649 @param group: name of the node group whose info to return
1652 @return: info about the node group
1655 return self._SendRequest(HTTP_GET,
1656 "/%s/groups/%s" % (GANETI_RAPI_VERSION, group),
1659 def CreateGroup(self, name, alloc_policy=None, dry_run=False):
1660 """Creates a new node group.
1663 @param name: the name of node group to create
1664 @type alloc_policy: str
1665 @param alloc_policy: the desired allocation policy for the group, if any
1667 @param dry_run: whether to peform a dry run
1674 _AppendDryRunIf(query, dry_run)
1678 "alloc_policy": alloc_policy
1681 return self._SendRequest(HTTP_POST, "/%s/groups" % GANETI_RAPI_VERSION,
1684 def ModifyGroup(self, group, **kwargs):
1685 """Modifies a node group.
1687 More details for parameters can be found in the RAPI documentation.
1690 @param group: Node group name
1695 return self._SendRequest(HTTP_PUT,
1696 ("/%s/groups/%s/modify" %
1697 (GANETI_RAPI_VERSION, group)), None, kwargs)
1699 def DeleteGroup(self, group, dry_run=False):
1700 """Deletes a node group.
1703 @param group: the node group to delete
1705 @param dry_run: whether to peform a dry run
1712 _AppendDryRunIf(query, dry_run)
1714 return self._SendRequest(HTTP_DELETE,
1716 (GANETI_RAPI_VERSION, group)), query, None)
1718 def RenameGroup(self, group, new_name):
1719 """Changes the name of a node group.
1722 @param group: Node group name
1723 @type new_name: string
1724 @param new_name: New node group name
1731 "new_name": new_name,
1734 return self._SendRequest(HTTP_PUT,
1735 ("/%s/groups/%s/rename" %
1736 (GANETI_RAPI_VERSION, group)), None, body)
1738 def AssignGroupNodes(self, group, nodes, force=False, dry_run=False):
1739 """Assigns nodes to a group.
1742 @param group: Node gropu name
1743 @type nodes: list of strings
1744 @param nodes: List of nodes to assign to the group
1751 _AppendForceIf(query, force)
1752 _AppendDryRunIf(query, dry_run)
1758 return self._SendRequest(HTTP_PUT,
1759 ("/%s/groups/%s/assign-nodes" %
1760 (GANETI_RAPI_VERSION, group)), query, body)
1762 def GetGroupTags(self, group):
1763 """Gets tags for a node group.
1766 @param group: Node group whose tags to return
1768 @rtype: list of strings
1769 @return: tags for the group
1772 return self._SendRequest(HTTP_GET,
1773 ("/%s/groups/%s/tags" %
1774 (GANETI_RAPI_VERSION, group)), None, None)
1776 def AddGroupTags(self, group, tags, dry_run=False):
1777 """Adds tags to a node group.
1780 @param group: group to add tags to
1781 @type tags: list of string
1782 @param tags: tags to add to the group
1784 @param dry_run: whether to perform a dry run
1790 query = [("tag", t) for t in tags]
1791 _AppendDryRunIf(query, dry_run)
1793 return self._SendRequest(HTTP_PUT,
1794 ("/%s/groups/%s/tags" %
1795 (GANETI_RAPI_VERSION, group)), query, None)
1797 def DeleteGroupTags(self, group, tags, dry_run=False):
1798 """Deletes tags from a node group.
1801 @param group: group to delete tags from
1802 @type tags: list of string
1803 @param tags: tags to delete
1805 @param dry_run: whether to perform a dry run
1810 query = [("tag", t) for t in tags]
1811 _AppendDryRunIf(query, dry_run)
1813 return self._SendRequest(HTTP_DELETE,
1814 ("/%s/groups/%s/tags" %
1815 (GANETI_RAPI_VERSION, group)), query, None)
1817 def Query(self, what, fields, qfilter=None):
1818 """Retrieves information about resources.
1821 @param what: Resource name, one of L{constants.QR_VIA_RAPI}
1822 @type fields: list of string
1823 @param fields: Requested fields
1824 @type qfilter: None or list
1825 @param qfilter: Query filter
1835 _SetItemIf(body, qfilter is not None, "qfilter", qfilter)
1836 # TODO: remove "filter" after 2.7
1837 _SetItemIf(body, qfilter is not None, "filter", qfilter)
1839 return self._SendRequest(HTTP_PUT,
1841 (GANETI_RAPI_VERSION, what)), None, body)
1843 def QueryFields(self, what, fields=None):
1844 """Retrieves available fields for a resource.
1847 @param what: Resource name, one of L{constants.QR_VIA_RAPI}
1848 @type fields: list of string
1849 @param fields: Requested fields
1857 if fields is not None:
1858 _AppendIf(query, True, ("fields", ",".join(fields)))
1860 return self._SendRequest(HTTP_GET,
1861 ("/%s/query/%s/fields" %
1862 (GANETI_RAPI_VERSION, what)), query, None)