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 _INST_CREATE_REQV1 = "instance-create-reqv1"
101 _INST_REINSTALL_REQV1 = "instance-reinstall-reqv1"
102 _NODE_MIGRATE_REQV1 = "node-migrate-reqv1"
103 _NODE_EVAC_RES1 = "node-evac-res1"
104 _INST_NIC_PARAMS = frozenset(["mac", "ip", "mode", "link"])
105 _INST_CREATE_V0_DISK_PARAMS = frozenset(["size"])
106 _INST_CREATE_V0_PARAMS = frozenset([
107 "os", "pnode", "snode", "iallocator", "start", "ip_check", "name_check",
108 "hypervisor", "file_storage_dir", "file_driver", "dry_run",
110 _INST_CREATE_V0_DPARAMS = frozenset(["beparams", "hvparams"])
112 # Older pycURL versions don't have all error constants
114 _CURLE_SSL_CACERT = pycurl.E_SSL_CACERT
115 _CURLE_SSL_CACERT_BADFILE = pycurl.E_SSL_CACERT_BADFILE
116 except AttributeError:
117 _CURLE_SSL_CACERT = 60
118 _CURLE_SSL_CACERT_BADFILE = 77
120 _CURL_SSL_CERT_ERRORS = frozenset([
122 _CURLE_SSL_CACERT_BADFILE,
126 class Error(Exception):
127 """Base error class for this module.
133 class CertificateError(Error):
134 """Raised when a problem is found with the SSL certificate.
140 class GanetiApiError(Error):
141 """Generic error raised from Ganeti API.
144 def __init__(self, msg, code=None):
145 Error.__init__(self, msg)
149 def UsesRapiClient(fn):
150 """Decorator for code using RAPI client to initialize pycURL.
153 def wrapper(*args, **kwargs):
154 # curl_global_init(3) and curl_global_cleanup(3) must be called with only
155 # one thread running. This check is just a safety measure -- it doesn't
157 assert threading.activeCount() == 1, \
158 "Found active threads when initializing pycURL"
160 pycurl.global_init(pycurl.GLOBAL_ALL)
162 return fn(*args, **kwargs)
164 pycurl.global_cleanup()
169 def GenericCurlConfig(verbose=False, use_signal=False,
170 use_curl_cabundle=False, cafile=None, capath=None,
171 proxy=None, verify_hostname=False,
172 connect_timeout=None, timeout=None,
173 _pycurl_version_fn=pycurl.version_info):
174 """Curl configuration function generator.
177 @param verbose: Whether to set cURL to verbose mode
178 @type use_signal: bool
179 @param use_signal: Whether to allow cURL to use signals
180 @type use_curl_cabundle: bool
181 @param use_curl_cabundle: Whether to use cURL's default CA bundle
183 @param cafile: In which file we can find the certificates
185 @param capath: In which directory we can find the certificates
187 @param proxy: Proxy to use, None for default behaviour and empty string for
188 disabling proxies (see curl_easy_setopt(3))
189 @type verify_hostname: bool
190 @param verify_hostname: Whether to verify the remote peer certificate's
192 @type connect_timeout: number
193 @param connect_timeout: Timeout for establishing connection in seconds
194 @type timeout: number
195 @param timeout: Timeout for complete transfer in seconds (see
196 curl_easy_setopt(3)).
199 if use_curl_cabundle and (cafile or capath):
200 raise Error("Can not use default CA bundle when CA file or path is set")
202 def _ConfigCurl(curl, logger):
203 """Configures a cURL object
205 @type curl: pycurl.Curl
206 @param curl: cURL object
209 logger.debug("Using cURL version %s", pycurl.version)
211 # pycurl.version_info returns a tuple with information about the used
212 # version of libcurl. Item 5 is the SSL library linked to it.
213 # e.g.: (3, '7.18.0', 463360, 'x86_64-pc-linux-gnu', 1581, 'GnuTLS/2.0.4',
215 sslver = _pycurl_version_fn()[5]
217 raise Error("No SSL support in cURL")
219 lcsslver = sslver.lower()
220 if lcsslver.startswith("openssl/"):
222 elif lcsslver.startswith("gnutls/"):
224 raise Error("cURL linked against GnuTLS has no support for a"
225 " CA path (%s)" % (pycurl.version, ))
227 raise NotImplementedError("cURL uses unsupported SSL version '%s'" %
230 curl.setopt(pycurl.VERBOSE, verbose)
231 curl.setopt(pycurl.NOSIGNAL, not use_signal)
233 # Whether to verify remote peer's CN
235 # curl_easy_setopt(3): "When CURLOPT_SSL_VERIFYHOST is 2, that
236 # certificate must indicate that the server is the server to which you
237 # meant to connect, or the connection fails. [...] When the value is 1,
238 # the certificate must contain a Common Name field, but it doesn't matter
239 # what name it says. [...]"
240 curl.setopt(pycurl.SSL_VERIFYHOST, 2)
242 curl.setopt(pycurl.SSL_VERIFYHOST, 0)
244 if cafile or capath or use_curl_cabundle:
245 # Require certificates to be checked
246 curl.setopt(pycurl.SSL_VERIFYPEER, True)
248 curl.setopt(pycurl.CAINFO, str(cafile))
250 curl.setopt(pycurl.CAPATH, str(capath))
251 # Not changing anything for using default CA bundle
253 # Disable SSL certificate verification
254 curl.setopt(pycurl.SSL_VERIFYPEER, False)
256 if proxy is not None:
257 curl.setopt(pycurl.PROXY, str(proxy))
260 if connect_timeout is not None:
261 curl.setopt(pycurl.CONNECTTIMEOUT, connect_timeout)
262 if timeout is not None:
263 curl.setopt(pycurl.TIMEOUT, timeout)
268 class GanetiRapiClient(object): # pylint: disable=R0904
269 """Ganeti RAPI client.
272 USER_AGENT = "Ganeti RAPI Client"
273 _json_encoder = simplejson.JSONEncoder(sort_keys=True)
275 def __init__(self, host, port=GANETI_RAPI_PORT,
276 username=None, password=None, logger=logging,
277 curl_config_fn=None, curl_factory=None):
278 """Initializes this class.
281 @param host: the ganeti cluster master to interact with
283 @param port: the port on which the RAPI is running (default is 5080)
284 @type username: string
285 @param username: the username to connect with
286 @type password: string
287 @param password: the password to connect with
288 @type curl_config_fn: callable
289 @param curl_config_fn: Function to configure C{pycurl.Curl} object
290 @param logger: Logging object
293 self._username = username
294 self._password = password
295 self._logger = logger
296 self._curl_config_fn = curl_config_fn
297 self._curl_factory = curl_factory
300 socket.inet_pton(socket.AF_INET6, host)
301 address = "[%s]:%s" % (host, port)
303 address = "%s:%s" % (host, port)
305 self._base_url = "https://%s" % address
307 if username is not None:
309 raise Error("Password not specified")
311 raise Error("Specified password without username")
313 def _CreateCurl(self):
314 """Creates a cURL object.
317 # Create pycURL object if no factory is provided
318 if self._curl_factory:
319 curl = self._curl_factory()
323 # Default cURL settings
324 curl.setopt(pycurl.VERBOSE, False)
325 curl.setopt(pycurl.FOLLOWLOCATION, False)
326 curl.setopt(pycurl.MAXREDIRS, 5)
327 curl.setopt(pycurl.NOSIGNAL, True)
328 curl.setopt(pycurl.USERAGENT, self.USER_AGENT)
329 curl.setopt(pycurl.SSL_VERIFYHOST, 0)
330 curl.setopt(pycurl.SSL_VERIFYPEER, False)
331 curl.setopt(pycurl.HTTPHEADER, [
332 "Accept: %s" % HTTP_APP_JSON,
333 "Content-type: %s" % HTTP_APP_JSON,
336 assert ((self._username is None and self._password is None) ^
337 (self._username is not None and self._password is not None))
340 # Setup authentication
341 curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
342 curl.setopt(pycurl.USERPWD,
343 str("%s:%s" % (self._username, self._password)))
345 # Call external configuration function
346 if self._curl_config_fn:
347 self._curl_config_fn(curl, self._logger)
352 def _EncodeQuery(query):
353 """Encode query values for RAPI URL.
355 @type query: list of two-tuples
356 @param query: Query arguments
358 @return: Query list with encoded values
363 for name, value in query:
365 result.append((name, ""))
367 elif isinstance(value, bool):
368 # Boolean values must be encoded as 0 or 1
369 result.append((name, int(value)))
371 elif isinstance(value, (list, tuple, dict)):
372 raise ValueError("Invalid query data type %r" % type(value).__name__)
375 result.append((name, value))
379 def _SendRequest(self, method, path, query, content):
380 """Sends an HTTP request.
382 This constructs a full URL, encodes and decodes HTTP bodies, and
383 handles invalid responses in a pythonic way.
386 @param method: HTTP method to use
388 @param path: HTTP URL path
389 @type query: list of two-tuples
390 @param query: query arguments to pass to urllib.urlencode
391 @type content: str or None
392 @param content: HTTP body content
395 @return: JSON-Decoded response
397 @raises CertificateError: If an invalid SSL certificate is found
398 @raises GanetiApiError: If an invalid response is returned
401 assert path.startswith("/")
403 curl = self._CreateCurl()
405 if content is not None:
406 encoded_content = self._json_encoder.encode(content)
411 urlparts = [self._base_url, path]
414 urlparts.append(urllib.urlencode(self._EncodeQuery(query)))
416 url = "".join(urlparts)
418 self._logger.debug("Sending request %s %s (content=%r)",
419 method, url, encoded_content)
421 # Buffer for response
422 encoded_resp_body = StringIO()
425 curl.setopt(pycurl.CUSTOMREQUEST, str(method))
426 curl.setopt(pycurl.URL, str(url))
427 curl.setopt(pycurl.POSTFIELDS, str(encoded_content))
428 curl.setopt(pycurl.WRITEFUNCTION, encoded_resp_body.write)
431 # Send request and wait for response
434 except pycurl.error, err:
435 if err.args[0] in _CURL_SSL_CERT_ERRORS:
436 raise CertificateError("SSL certificate error %s" % err)
438 raise GanetiApiError(str(err))
440 # Reset settings to not keep references to large objects in memory
442 curl.setopt(pycurl.POSTFIELDS, "")
443 curl.setopt(pycurl.WRITEFUNCTION, lambda _: None)
445 # Get HTTP response code
446 http_code = curl.getinfo(pycurl.RESPONSE_CODE)
448 # Was anything written to the response buffer?
449 if encoded_resp_body.tell():
450 response_content = simplejson.loads(encoded_resp_body.getvalue())
452 response_content = None
454 if http_code != HTTP_OK:
455 if isinstance(response_content, dict):
457 (response_content["code"],
458 response_content["message"],
459 response_content["explain"]))
461 msg = str(response_content)
463 raise GanetiApiError(msg, code=http_code)
465 return response_content
467 def GetVersion(self):
468 """Gets the Remote API version running on the cluster.
471 @return: Ganeti Remote API version
474 return self._SendRequest(HTTP_GET, "/version", None, None)
476 def GetFeatures(self):
477 """Gets the list of optional features supported by RAPI server.
480 @return: List of optional features
484 return self._SendRequest(HTTP_GET, "/%s/features" % GANETI_RAPI_VERSION,
486 except GanetiApiError, err:
487 # Older RAPI servers don't support this resource
488 if err.code == HTTP_NOT_FOUND:
493 def GetOperatingSystems(self):
494 """Gets the Operating Systems running in the Ganeti cluster.
497 @return: operating systems
500 return self._SendRequest(HTTP_GET, "/%s/os" % GANETI_RAPI_VERSION,
504 """Gets info about the cluster.
507 @return: information about the cluster
510 return self._SendRequest(HTTP_GET, "/%s/info" % GANETI_RAPI_VERSION,
513 def RedistributeConfig(self):
514 """Tells the cluster to redistribute its configuration files.
520 return self._SendRequest(HTTP_PUT,
521 "/%s/redistribute-config" % GANETI_RAPI_VERSION,
524 def ModifyCluster(self, **kwargs):
525 """Modifies cluster parameters.
527 More details for parameters can be found in the RAPI documentation.
535 return self._SendRequest(HTTP_PUT,
536 "/%s/modify" % GANETI_RAPI_VERSION, None, body)
538 def GetClusterTags(self):
539 """Gets the cluster tags.
542 @return: cluster tags
545 return self._SendRequest(HTTP_GET, "/%s/tags" % GANETI_RAPI_VERSION,
548 def AddClusterTags(self, tags, dry_run=False):
549 """Adds tags to the cluster.
551 @type tags: list of str
552 @param tags: tags to add to the cluster
554 @param dry_run: whether to perform a dry run
560 query = [("tag", t) for t in tags]
562 query.append(("dry-run", 1))
564 return self._SendRequest(HTTP_PUT, "/%s/tags" % GANETI_RAPI_VERSION,
567 def DeleteClusterTags(self, tags, dry_run=False):
568 """Deletes tags from the cluster.
570 @type tags: list of str
571 @param tags: tags to delete
573 @param dry_run: whether to perform a dry run
578 query = [("tag", t) for t in tags]
580 query.append(("dry-run", 1))
582 return self._SendRequest(HTTP_DELETE, "/%s/tags" % GANETI_RAPI_VERSION,
585 def GetInstances(self, bulk=False):
586 """Gets information about instances on the cluster.
589 @param bulk: whether to return all information about all instances
591 @rtype: list of dict or list of str
592 @return: if bulk is True, info about the instances, else a list of instances
597 query.append(("bulk", 1))
599 instances = self._SendRequest(HTTP_GET,
600 "/%s/instances" % GANETI_RAPI_VERSION,
605 return [i["id"] for i in instances]
607 def GetInstance(self, instance):
608 """Gets information about an instance.
611 @param instance: instance whose info to return
614 @return: info about the instance
617 return self._SendRequest(HTTP_GET,
618 ("/%s/instances/%s" %
619 (GANETI_RAPI_VERSION, instance)), None, None)
621 def GetInstanceInfo(self, instance, static=None):
622 """Gets information about an instance.
624 @type instance: string
625 @param instance: Instance name
630 if static is not None:
631 query = [("static", static)]
635 return self._SendRequest(HTTP_GET,
636 ("/%s/instances/%s/info" %
637 (GANETI_RAPI_VERSION, instance)), query, None)
639 def CreateInstance(self, mode, name, disk_template, disks, nics,
641 """Creates a new instance.
643 More details for parameters can be found in the RAPI documentation.
646 @param mode: Instance creation mode
648 @param name: Hostname of the instance to create
649 @type disk_template: string
650 @param disk_template: Disk template for instance (e.g. plain, diskless,
652 @type disks: list of dicts
653 @param disks: List of disk definitions
654 @type nics: list of dicts
655 @param nics: List of NIC definitions
657 @keyword dry_run: whether to perform a dry run
665 if kwargs.get("dry_run"):
666 query.append(("dry-run", 1))
668 if _INST_CREATE_REQV1 in self.GetFeatures():
669 # All required fields for request data version 1
671 _REQ_DATA_VERSION_FIELD: 1,
674 "disk_template": disk_template,
679 conflicts = set(kwargs.iterkeys()) & set(body.iterkeys())
681 raise GanetiApiError("Required fields can not be specified as"
682 " keywords: %s" % ", ".join(conflicts))
684 body.update((key, value) for key, value in kwargs.iteritems()
687 raise GanetiApiError("Server does not support new-style (version 1)"
688 " instance creation requests")
690 return self._SendRequest(HTTP_POST, "/%s/instances" % GANETI_RAPI_VERSION,
693 def DeleteInstance(self, instance, dry_run=False):
694 """Deletes an instance.
697 @param instance: the instance to delete
705 query.append(("dry-run", 1))
707 return self._SendRequest(HTTP_DELETE,
708 ("/%s/instances/%s" %
709 (GANETI_RAPI_VERSION, instance)), query, None)
711 def ModifyInstance(self, instance, **kwargs):
712 """Modifies an instance.
714 More details for parameters can be found in the RAPI documentation.
716 @type instance: string
717 @param instance: Instance name
724 return self._SendRequest(HTTP_PUT,
725 ("/%s/instances/%s/modify" %
726 (GANETI_RAPI_VERSION, instance)), None, body)
728 def ActivateInstanceDisks(self, instance, ignore_size=None):
729 """Activates an instance's disks.
731 @type instance: string
732 @param instance: Instance name
733 @type ignore_size: bool
734 @param ignore_size: Whether to ignore recorded size
741 query.append(("ignore_size", 1))
743 return self._SendRequest(HTTP_PUT,
744 ("/%s/instances/%s/activate-disks" %
745 (GANETI_RAPI_VERSION, instance)), query, None)
747 def DeactivateInstanceDisks(self, instance):
748 """Deactivates an instance's disks.
750 @type instance: string
751 @param instance: Instance name
756 return self._SendRequest(HTTP_PUT,
757 ("/%s/instances/%s/deactivate-disks" %
758 (GANETI_RAPI_VERSION, instance)), None, None)
760 def RecreateInstanceDisks(self, instance, disks=None, nodes=None):
761 """Recreate an instance's disks.
763 @type instance: string
764 @param instance: Instance name
765 @type disks: list of int
766 @param disks: List of disk indexes
767 @type nodes: list of string
768 @param nodes: New instance nodes, if relocation is desired
775 if disks is not None:
776 body["disks"] = disks
778 if nodes is not None:
779 body["nodes"] = nodes
781 return self._SendRequest(HTTP_POST,
782 ("/%s/instances/%s/recreate-disks" %
783 (GANETI_RAPI_VERSION, instance)), None, body)
785 def GrowInstanceDisk(self, instance, disk, amount, wait_for_sync=None):
786 """Grows a disk of an instance.
788 More details for parameters can be found in the RAPI documentation.
790 @type instance: string
791 @param instance: Instance name
793 @param disk: Disk index
794 @type amount: integer
795 @param amount: Grow disk by this amount (MiB)
796 @type wait_for_sync: bool
797 @param wait_for_sync: Wait for disk to synchronize
806 if wait_for_sync is not None:
807 body["wait_for_sync"] = wait_for_sync
809 return self._SendRequest(HTTP_POST,
810 ("/%s/instances/%s/disk/%s/grow" %
811 (GANETI_RAPI_VERSION, instance, disk)),
814 def GetInstanceTags(self, instance):
815 """Gets tags for an instance.
818 @param instance: instance whose tags to return
821 @return: tags for the instance
824 return self._SendRequest(HTTP_GET,
825 ("/%s/instances/%s/tags" %
826 (GANETI_RAPI_VERSION, instance)), None, None)
828 def AddInstanceTags(self, instance, tags, dry_run=False):
829 """Adds tags to an instance.
832 @param instance: instance to add tags to
833 @type tags: list of str
834 @param tags: tags to add to the instance
836 @param dry_run: whether to perform a dry run
842 query = [("tag", t) for t in tags]
844 query.append(("dry-run", 1))
846 return self._SendRequest(HTTP_PUT,
847 ("/%s/instances/%s/tags" %
848 (GANETI_RAPI_VERSION, instance)), query, None)
850 def DeleteInstanceTags(self, instance, tags, dry_run=False):
851 """Deletes tags from an instance.
854 @param instance: instance to delete tags from
855 @type tags: list of str
856 @param tags: tags to delete
858 @param dry_run: whether to perform a dry run
863 query = [("tag", t) for t in tags]
865 query.append(("dry-run", 1))
867 return self._SendRequest(HTTP_DELETE,
868 ("/%s/instances/%s/tags" %
869 (GANETI_RAPI_VERSION, instance)), query, None)
871 def RebootInstance(self, instance, reboot_type=None, ignore_secondaries=None,
873 """Reboots an instance.
876 @param instance: instance to rebot
877 @type reboot_type: str
878 @param reboot_type: one of: hard, soft, full
879 @type ignore_secondaries: bool
880 @param ignore_secondaries: if True, ignores errors for the secondary node
881 while re-assembling disks (in hard-reboot mode only)
883 @param dry_run: whether to perform a dry run
890 query.append(("type", reboot_type))
891 if ignore_secondaries is not None:
892 query.append(("ignore_secondaries", ignore_secondaries))
894 query.append(("dry-run", 1))
896 return self._SendRequest(HTTP_POST,
897 ("/%s/instances/%s/reboot" %
898 (GANETI_RAPI_VERSION, instance)), query, None)
900 def ShutdownInstance(self, instance, dry_run=False, no_remember=False):
901 """Shuts down an instance.
904 @param instance: the instance to shut down
906 @param dry_run: whether to perform a dry run
907 @type no_remember: bool
908 @param no_remember: if true, will not record the state change
915 query.append(("dry-run", 1))
917 query.append(("no-remember", 1))
919 return self._SendRequest(HTTP_PUT,
920 ("/%s/instances/%s/shutdown" %
921 (GANETI_RAPI_VERSION, instance)), query, None)
923 def StartupInstance(self, instance, dry_run=False, no_remember=False):
924 """Starts up an instance.
927 @param instance: the instance to start up
929 @param dry_run: whether to perform a dry run
930 @type no_remember: bool
931 @param no_remember: if true, will not record the state change
938 query.append(("dry-run", 1))
940 query.append(("no-remember", 1))
942 return self._SendRequest(HTTP_PUT,
943 ("/%s/instances/%s/startup" %
944 (GANETI_RAPI_VERSION, instance)), query, None)
946 def ReinstallInstance(self, instance, os=None, no_startup=False,
948 """Reinstalls an instance.
951 @param instance: The instance to reinstall
952 @type os: str or None
953 @param os: The operating system to reinstall. If None, the instance's
954 current operating system will be installed again
955 @type no_startup: bool
956 @param no_startup: Whether to start the instance automatically
961 if _INST_REINSTALL_REQV1 in self.GetFeatures():
963 "start": not no_startup,
967 if osparams is not None:
968 body["osparams"] = osparams
969 return self._SendRequest(HTTP_POST,
970 ("/%s/instances/%s/reinstall" %
971 (GANETI_RAPI_VERSION, instance)), None, body)
973 # Use old request format
975 raise GanetiApiError("Server does not support specifying OS parameters"
976 " for instance reinstallation")
980 query.append(("os", os))
982 query.append(("nostartup", 1))
983 return self._SendRequest(HTTP_POST,
984 ("/%s/instances/%s/reinstall" %
985 (GANETI_RAPI_VERSION, instance)), query, None)
987 def ReplaceInstanceDisks(self, instance, disks=None, mode=REPLACE_DISK_AUTO,
988 remote_node=None, iallocator=None):
989 """Replaces disks on an instance.
992 @param instance: instance whose disks to replace
993 @type disks: list of ints
994 @param disks: Indexes of disks to replace
996 @param mode: replacement mode to use (defaults to replace_auto)
997 @type remote_node: str or None
998 @param remote_node: new secondary node to use (for use with
999 replace_new_secondary mode)
1000 @type iallocator: str or None
1001 @param iallocator: instance allocator plugin to use (for use with
1012 # TODO: Convert to body parameters
1014 if disks is not None:
1015 query.append(("disks", ",".join(str(idx) for idx in disks)))
1017 if remote_node is not None:
1018 query.append(("remote_node", remote_node))
1020 if iallocator is not None:
1021 query.append(("iallocator", iallocator))
1023 return self._SendRequest(HTTP_POST,
1024 ("/%s/instances/%s/replace-disks" %
1025 (GANETI_RAPI_VERSION, instance)), query, None)
1027 def PrepareExport(self, instance, mode):
1028 """Prepares an instance for an export.
1030 @type instance: string
1031 @param instance: Instance name
1033 @param mode: Export mode
1038 query = [("mode", mode)]
1039 return self._SendRequest(HTTP_PUT,
1040 ("/%s/instances/%s/prepare-export" %
1041 (GANETI_RAPI_VERSION, instance)), query, None)
1043 def ExportInstance(self, instance, mode, destination, shutdown=None,
1044 remove_instance=None,
1045 x509_key_name=None, destination_x509_ca=None):
1046 """Exports an instance.
1048 @type instance: string
1049 @param instance: Instance name
1051 @param mode: Export mode
1057 "destination": destination,
1061 if shutdown is not None:
1062 body["shutdown"] = shutdown
1064 if remove_instance is not None:
1065 body["remove_instance"] = remove_instance
1067 if x509_key_name is not None:
1068 body["x509_key_name"] = x509_key_name
1070 if destination_x509_ca is not None:
1071 body["destination_x509_ca"] = destination_x509_ca
1073 return self._SendRequest(HTTP_PUT,
1074 ("/%s/instances/%s/export" %
1075 (GANETI_RAPI_VERSION, instance)), None, body)
1077 def MigrateInstance(self, instance, mode=None, cleanup=None):
1078 """Migrates an instance.
1080 @type instance: string
1081 @param instance: Instance name
1083 @param mode: Migration mode
1085 @param cleanup: Whether to clean up a previously failed migration
1092 if mode is not None:
1095 if cleanup is not None:
1096 body["cleanup"] = cleanup
1098 return self._SendRequest(HTTP_PUT,
1099 ("/%s/instances/%s/migrate" %
1100 (GANETI_RAPI_VERSION, instance)), None, body)
1102 def FailoverInstance(self, instance, iallocator=None,
1103 ignore_consistency=None, target_node=None):
1104 """Does a failover of an instance.
1106 @type instance: string
1107 @param instance: Instance name
1108 @type iallocator: string
1109 @param iallocator: Iallocator for deciding the target node for
1110 shared-storage instances
1111 @type ignore_consistency: bool
1112 @param ignore_consistency: Whether to ignore disk consistency
1113 @type target_node: string
1114 @param target_node: Target node for shared-storage instances
1121 if iallocator is not None:
1122 body["iallocator"] = iallocator
1124 if ignore_consistency is not None:
1125 body["ignore_consistency"] = ignore_consistency
1127 if target_node is not None:
1128 body["target_node"] = target_node
1130 return self._SendRequest(HTTP_PUT,
1131 ("/%s/instances/%s/failover" %
1132 (GANETI_RAPI_VERSION, instance)), None, body)
1134 def RenameInstance(self, instance, new_name, ip_check=None, name_check=None):
1135 """Changes the name of an instance.
1137 @type instance: string
1138 @param instance: Instance name
1139 @type new_name: string
1140 @param new_name: New instance name
1141 @type ip_check: bool
1142 @param ip_check: Whether to ensure instance's IP address is inactive
1143 @type name_check: bool
1144 @param name_check: Whether to ensure instance's name is resolvable
1150 "new_name": new_name,
1153 if ip_check is not None:
1154 body["ip_check"] = ip_check
1156 if name_check is not None:
1157 body["name_check"] = name_check
1159 return self._SendRequest(HTTP_PUT,
1160 ("/%s/instances/%s/rename" %
1161 (GANETI_RAPI_VERSION, instance)), None, body)
1163 def GetInstanceConsole(self, instance):
1164 """Request information for connecting to instance's console.
1166 @type instance: string
1167 @param instance: Instance name
1169 @return: dictionary containing information about instance's console
1172 return self._SendRequest(HTTP_GET,
1173 ("/%s/instances/%s/console" %
1174 (GANETI_RAPI_VERSION, instance)), None, None)
1177 """Gets all jobs for the cluster.
1180 @return: job ids for the cluster
1183 return [int(j["id"])
1184 for j in self._SendRequest(HTTP_GET,
1185 "/%s/jobs" % GANETI_RAPI_VERSION,
1188 def GetJobStatus(self, job_id):
1189 """Gets the status of a job.
1191 @type job_id: string
1192 @param job_id: job id whose status to query
1198 return self._SendRequest(HTTP_GET,
1199 "/%s/jobs/%s" % (GANETI_RAPI_VERSION, job_id),
1202 def WaitForJobCompletion(self, job_id, period=5, retries=-1):
1203 """Polls cluster for job status until completion.
1205 Completion is defined as any of the following states listed in
1206 L{JOB_STATUS_FINALIZED}.
1208 @type job_id: string
1209 @param job_id: job id to watch
1211 @param period: how often to poll for status (optional, default 5s)
1213 @param retries: how many time to poll before giving up
1214 (optional, default -1 means unlimited)
1217 @return: C{True} if job succeeded or C{False} if failed/status timeout
1218 @deprecated: It is recommended to use L{WaitForJobChange} wherever
1219 possible; L{WaitForJobChange} returns immediately after a job changed and
1220 does not use polling
1224 job_result = self.GetJobStatus(job_id)
1226 if job_result and job_result["status"] == JOB_STATUS_SUCCESS:
1228 elif not job_result or job_result["status"] in JOB_STATUS_FINALIZED:
1239 def WaitForJobChange(self, job_id, fields, prev_job_info, prev_log_serial):
1240 """Waits for job changes.
1242 @type job_id: string
1243 @param job_id: Job ID for which to wait
1244 @return: C{None} if no changes have been detected and a dict with two keys,
1245 C{job_info} and C{log_entries} otherwise.
1251 "previous_job_info": prev_job_info,
1252 "previous_log_serial": prev_log_serial,
1255 return self._SendRequest(HTTP_GET,
1256 "/%s/jobs/%s/wait" % (GANETI_RAPI_VERSION, job_id),
1259 def CancelJob(self, job_id, dry_run=False):
1262 @type job_id: string
1263 @param job_id: id of the job to delete
1265 @param dry_run: whether to perform a dry run
1267 @return: tuple containing the result, and a message (bool, string)
1272 query.append(("dry-run", 1))
1274 return self._SendRequest(HTTP_DELETE,
1275 "/%s/jobs/%s" % (GANETI_RAPI_VERSION, job_id),
1278 def GetNodes(self, bulk=False):
1279 """Gets all nodes in the cluster.
1282 @param bulk: whether to return all information about all instances
1284 @rtype: list of dict or str
1285 @return: if bulk is true, info about nodes in the cluster,
1286 else list of nodes in the cluster
1291 query.append(("bulk", 1))
1293 nodes = self._SendRequest(HTTP_GET, "/%s/nodes" % GANETI_RAPI_VERSION,
1298 return [n["id"] for n in nodes]
1300 def GetNode(self, node):
1301 """Gets information about a node.
1304 @param node: node whose info to return
1307 @return: info about the node
1310 return self._SendRequest(HTTP_GET,
1311 "/%s/nodes/%s" % (GANETI_RAPI_VERSION, node),
1314 def EvacuateNode(self, node, iallocator=None, remote_node=None,
1315 dry_run=False, early_release=None,
1316 mode=None, accept_old=False):
1317 """Evacuates instances from a Ganeti node.
1320 @param node: node to evacuate
1321 @type iallocator: str or None
1322 @param iallocator: instance allocator to use
1323 @type remote_node: str
1324 @param remote_node: node to evaucate to
1326 @param dry_run: whether to perform a dry run
1327 @type early_release: bool
1328 @param early_release: whether to enable parallelization
1330 @param mode: Node evacuation mode
1331 @type accept_old: bool
1332 @param accept_old: Whether caller is ready to accept old-style (pre-2.5)
1335 @rtype: string, or a list for pre-2.5 results
1336 @return: Job ID or, if C{accept_old} is set and server is pre-2.5,
1337 list of (job ID, instance name, new secondary node); if dry_run was
1338 specified, then the actual move jobs were not submitted and the job IDs
1341 @raises GanetiApiError: if an iallocator and remote_node are both
1345 if iallocator and remote_node:
1346 raise GanetiApiError("Only one of iallocator or remote_node can be used")
1350 query.append(("dry-run", 1))
1352 if _NODE_EVAC_RES1 in self.GetFeatures():
1353 # Server supports body parameters
1356 if iallocator is not None:
1357 body["iallocator"] = iallocator
1358 if remote_node is not None:
1359 body["remote_node"] = remote_node
1360 if early_release is not None:
1361 body["early_release"] = early_release
1362 if mode is not None:
1365 # Pre-2.5 request format
1369 raise GanetiApiError("Server is version 2.4 or earlier and caller does"
1370 " not accept old-style results (parameter"
1373 # Pre-2.5 servers can only evacuate secondaries
1374 if mode is not None and mode != NODE_EVAC_SEC:
1375 raise GanetiApiError("Server can only evacuate secondary instances")
1378 query.append(("iallocator", iallocator))
1380 query.append(("remote_node", remote_node))
1382 query.append(("early_release", 1))
1384 return self._SendRequest(HTTP_POST,
1385 ("/%s/nodes/%s/evacuate" %
1386 (GANETI_RAPI_VERSION, node)), query, body)
1388 def MigrateNode(self, node, mode=None, dry_run=False, iallocator=None,
1390 """Migrates all primary instances from a node.
1393 @param node: node to migrate
1395 @param mode: if passed, it will overwrite the live migration type,
1396 otherwise the hypervisor default will be used
1398 @param dry_run: whether to perform a dry run
1399 @type iallocator: string
1400 @param iallocator: instance allocator to use
1401 @type target_node: string
1402 @param target_node: Target node for shared-storage instances
1410 query.append(("dry-run", 1))
1412 if _NODE_MIGRATE_REQV1 in self.GetFeatures():
1415 if mode is not None:
1417 if iallocator is not None:
1418 body["iallocator"] = iallocator
1419 if target_node is not None:
1420 body["target_node"] = target_node
1422 assert len(query) <= 1
1424 return self._SendRequest(HTTP_POST,
1425 ("/%s/nodes/%s/migrate" %
1426 (GANETI_RAPI_VERSION, node)), query, body)
1428 # Use old request format
1429 if target_node is not None:
1430 raise GanetiApiError("Server does not support specifying target node"
1431 " for node migration")
1433 if mode is not None:
1434 query.append(("mode", mode))
1436 return self._SendRequest(HTTP_POST,
1437 ("/%s/nodes/%s/migrate" %
1438 (GANETI_RAPI_VERSION, node)), query, None)
1440 def GetNodeRole(self, node):
1441 """Gets the current role for a node.
1444 @param node: node whose role to return
1447 @return: the current role for a node
1450 return self._SendRequest(HTTP_GET,
1451 ("/%s/nodes/%s/role" %
1452 (GANETI_RAPI_VERSION, node)), None, None)
1454 def SetNodeRole(self, node, role, force=False, auto_promote=None):
1455 """Sets the role for a node.
1458 @param node: the node whose role to set
1460 @param role: the role to set for the node
1462 @param force: whether to force the role change
1463 @type auto_promote: bool
1464 @param auto_promote: Whether node(s) should be promoted to master candidate
1475 if auto_promote is not None:
1476 query.append(("auto-promote", auto_promote))
1478 return self._SendRequest(HTTP_PUT,
1479 ("/%s/nodes/%s/role" %
1480 (GANETI_RAPI_VERSION, node)), query, role)
1482 def PowercycleNode(self, node, force=False):
1483 """Powercycles a node.
1486 @param node: Node name
1488 @param force: Whether to force the operation
1497 return self._SendRequest(HTTP_POST,
1498 ("/%s/nodes/%s/powercycle" %
1499 (GANETI_RAPI_VERSION, node)), query, None)
1501 def ModifyNode(self, node, **kwargs):
1504 More details for parameters can be found in the RAPI documentation.
1507 @param node: Node name
1512 return self._SendRequest(HTTP_POST,
1513 ("/%s/nodes/%s/modify" %
1514 (GANETI_RAPI_VERSION, node)), None, kwargs)
1516 def GetNodeStorageUnits(self, node, storage_type, output_fields):
1517 """Gets the storage units for a node.
1520 @param node: the node whose storage units to return
1521 @type storage_type: str
1522 @param storage_type: storage type whose units to return
1523 @type output_fields: str
1524 @param output_fields: storage type fields to return
1527 @return: job id where results can be retrieved
1531 ("storage_type", storage_type),
1532 ("output_fields", output_fields),
1535 return self._SendRequest(HTTP_GET,
1536 ("/%s/nodes/%s/storage" %
1537 (GANETI_RAPI_VERSION, node)), query, None)
1539 def ModifyNodeStorageUnits(self, node, storage_type, name, allocatable=None):
1540 """Modifies parameters of storage units on the node.
1543 @param node: node whose storage units to modify
1544 @type storage_type: str
1545 @param storage_type: storage type whose units to modify
1547 @param name: name of the storage unit
1548 @type allocatable: bool or None
1549 @param allocatable: Whether to set the "allocatable" flag on the storage
1550 unit (None=no modification, True=set, False=unset)
1557 ("storage_type", storage_type),
1561 if allocatable is not None:
1562 query.append(("allocatable", allocatable))
1564 return self._SendRequest(HTTP_PUT,
1565 ("/%s/nodes/%s/storage/modify" %
1566 (GANETI_RAPI_VERSION, node)), query, None)
1568 def RepairNodeStorageUnits(self, node, storage_type, name):
1569 """Repairs a storage unit on the node.
1572 @param node: node whose storage units to repair
1573 @type storage_type: str
1574 @param storage_type: storage type to repair
1576 @param name: name of the storage unit to repair
1583 ("storage_type", storage_type),
1587 return self._SendRequest(HTTP_PUT,
1588 ("/%s/nodes/%s/storage/repair" %
1589 (GANETI_RAPI_VERSION, node)), query, None)
1591 def GetNodeTags(self, node):
1592 """Gets the tags for a node.
1595 @param node: node whose tags to return
1598 @return: tags for the node
1601 return self._SendRequest(HTTP_GET,
1602 ("/%s/nodes/%s/tags" %
1603 (GANETI_RAPI_VERSION, node)), None, None)
1605 def AddNodeTags(self, node, tags, dry_run=False):
1606 """Adds tags to a node.
1609 @param node: node to add tags to
1610 @type tags: list of str
1611 @param tags: tags to add to the node
1613 @param dry_run: whether to perform a dry run
1619 query = [("tag", t) for t in tags]
1621 query.append(("dry-run", 1))
1623 return self._SendRequest(HTTP_PUT,
1624 ("/%s/nodes/%s/tags" %
1625 (GANETI_RAPI_VERSION, node)), query, tags)
1627 def DeleteNodeTags(self, node, tags, dry_run=False):
1628 """Delete tags from a node.
1631 @param node: node to remove tags from
1632 @type tags: list of str
1633 @param tags: tags to remove from the node
1635 @param dry_run: whether to perform a dry run
1641 query = [("tag", t) for t in tags]
1643 query.append(("dry-run", 1))
1645 return self._SendRequest(HTTP_DELETE,
1646 ("/%s/nodes/%s/tags" %
1647 (GANETI_RAPI_VERSION, node)), query, None)
1649 def GetGroups(self, bulk=False):
1650 """Gets all node groups in the cluster.
1653 @param bulk: whether to return all information about the groups
1655 @rtype: list of dict or str
1656 @return: if bulk is true, a list of dictionaries with info about all node
1657 groups in the cluster, else a list of names of those node groups
1662 query.append(("bulk", 1))
1664 groups = self._SendRequest(HTTP_GET, "/%s/groups" % GANETI_RAPI_VERSION,
1669 return [g["name"] for g in groups]
1671 def GetGroup(self, group):
1672 """Gets information about a node group.
1675 @param group: name of the node group whose info to return
1678 @return: info about the node group
1681 return self._SendRequest(HTTP_GET,
1682 "/%s/groups/%s" % (GANETI_RAPI_VERSION, group),
1685 def CreateGroup(self, name, alloc_policy=None, dry_run=False):
1686 """Creates a new node group.
1689 @param name: the name of node group to create
1690 @type alloc_policy: str
1691 @param alloc_policy: the desired allocation policy for the group, if any
1693 @param dry_run: whether to peform a dry run
1701 query.append(("dry-run", 1))
1705 "alloc_policy": alloc_policy
1708 return self._SendRequest(HTTP_POST, "/%s/groups" % GANETI_RAPI_VERSION,
1711 def ModifyGroup(self, group, **kwargs):
1712 """Modifies a node group.
1714 More details for parameters can be found in the RAPI documentation.
1717 @param group: Node group name
1722 return self._SendRequest(HTTP_PUT,
1723 ("/%s/groups/%s/modify" %
1724 (GANETI_RAPI_VERSION, group)), None, kwargs)
1726 def DeleteGroup(self, group, dry_run=False):
1727 """Deletes a node group.
1730 @param group: the node group to delete
1732 @param dry_run: whether to peform a dry run
1740 query.append(("dry-run", 1))
1742 return self._SendRequest(HTTP_DELETE,
1744 (GANETI_RAPI_VERSION, group)), query, None)
1746 def RenameGroup(self, group, new_name):
1747 """Changes the name of a node group.
1750 @param group: Node group name
1751 @type new_name: string
1752 @param new_name: New node group name
1759 "new_name": new_name,
1762 return self._SendRequest(HTTP_PUT,
1763 ("/%s/groups/%s/rename" %
1764 (GANETI_RAPI_VERSION, group)), None, body)
1766 def AssignGroupNodes(self, group, nodes, force=False, dry_run=False):
1767 """Assigns nodes to a group.
1770 @param group: Node gropu name
1771 @type nodes: list of strings
1772 @param nodes: List of nodes to assign to the group
1781 query.append(("force", 1))
1784 query.append(("dry-run", 1))
1790 return self._SendRequest(HTTP_PUT,
1791 ("/%s/groups/%s/assign-nodes" %
1792 (GANETI_RAPI_VERSION, group)), query, body)
1794 def GetGroupTags(self, group):
1795 """Gets tags for a node group.
1798 @param group: Node group whose tags to return
1800 @rtype: list of strings
1801 @return: tags for the group
1804 return self._SendRequest(HTTP_GET,
1805 ("/%s/groups/%s/tags" %
1806 (GANETI_RAPI_VERSION, group)), None, None)
1808 def AddGroupTags(self, group, tags, dry_run=False):
1809 """Adds tags to a node group.
1812 @param group: group to add tags to
1813 @type tags: list of string
1814 @param tags: tags to add to the group
1816 @param dry_run: whether to perform a dry run
1822 query = [("tag", t) for t in tags]
1824 query.append(("dry-run", 1))
1826 return self._SendRequest(HTTP_PUT,
1827 ("/%s/groups/%s/tags" %
1828 (GANETI_RAPI_VERSION, group)), query, None)
1830 def DeleteGroupTags(self, group, tags, dry_run=False):
1831 """Deletes tags from a node group.
1834 @param group: group to delete tags from
1835 @type tags: list of string
1836 @param tags: tags to delete
1838 @param dry_run: whether to perform a dry run
1843 query = [("tag", t) for t in tags]
1845 query.append(("dry-run", 1))
1847 return self._SendRequest(HTTP_DELETE,
1848 ("/%s/groups/%s/tags" %
1849 (GANETI_RAPI_VERSION, group)), query, None)
1851 def Query(self, what, fields, qfilter=None):
1852 """Retrieves information about resources.
1855 @param what: Resource name, one of L{constants.QR_VIA_RAPI}
1856 @type fields: list of string
1857 @param fields: Requested fields
1858 @type qfilter: None or list
1859 @param qfilter: Query filter
1869 if qfilter is not None:
1870 body["qfilter"] = qfilter
1871 # TODO: remove this after 2.7
1872 body["filter"] = qfilter
1874 return self._SendRequest(HTTP_PUT,
1876 (GANETI_RAPI_VERSION, what)), None, body)
1878 def QueryFields(self, what, fields=None):
1879 """Retrieves available fields for a resource.
1882 @param what: Resource name, one of L{constants.QR_VIA_RAPI}
1883 @type fields: list of string
1884 @param fields: Requested fields
1892 if fields is not None:
1893 query.append(("fields", ",".join(fields)))
1895 return self._SendRequest(HTTP_GET,
1896 ("/%s/query/%s/fields" %
1897 (GANETI_RAPI_VERSION, what)), query, None)