root / lib / rapi / client.py @ 2a7c3583
History | View | Annotate | Download (33.8 kB)
1 | 95ab4de9 | David Knowles | #
|
---|---|---|---|
2 | 95ab4de9 | David Knowles | #
|
3 | 95ab4de9 | David Knowles | |
4 | 95ab4de9 | David Knowles | # Copyright (C) 2010 Google Inc.
|
5 | 95ab4de9 | David Knowles | #
|
6 | 95ab4de9 | David Knowles | # This program is free software; you can redistribute it and/or modify
|
7 | 95ab4de9 | David Knowles | # it under the terms of the GNU General Public License as published by
|
8 | 95ab4de9 | David Knowles | # the Free Software Foundation; either version 2 of the License, or
|
9 | 95ab4de9 | David Knowles | # (at your option) any later version.
|
10 | 95ab4de9 | David Knowles | #
|
11 | 95ab4de9 | David Knowles | # This program is distributed in the hope that it will be useful, but
|
12 | 95ab4de9 | David Knowles | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 95ab4de9 | David Knowles | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 95ab4de9 | David Knowles | # General Public License for more details.
|
15 | 95ab4de9 | David Knowles | #
|
16 | 95ab4de9 | David Knowles | # You should have received a copy of the GNU General Public License
|
17 | 95ab4de9 | David Knowles | # along with this program; if not, write to the Free Software
|
18 | 95ab4de9 | David Knowles | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 95ab4de9 | David Knowles | # 02110-1301, USA.
|
20 | 95ab4de9 | David Knowles | |
21 | 95ab4de9 | David Knowles | |
22 | 2a7c3583 | Michael Hanselmann | """Ganeti RAPI client.
|
23 | 2a7c3583 | Michael Hanselmann |
|
24 | 2a7c3583 | Michael Hanselmann | @attention: To use the RAPI client, the application B{must} call
|
25 | 2a7c3583 | Michael Hanselmann | C{pycurl.global_init} during initialization and
|
26 | 2a7c3583 | Michael Hanselmann | C{pycurl.global_cleanup} before exiting the process. This is very
|
27 | 2a7c3583 | Michael Hanselmann | important in multi-threaded programs. See curl_global_init(3) and
|
28 | 2a7c3583 | Michael Hanselmann | curl_global_cleanup(3) for details. The decorator L{UsesRapiClient}
|
29 | 2a7c3583 | Michael Hanselmann | can be used.
|
30 | 2a7c3583 | Michael Hanselmann |
|
31 | 2a7c3583 | Michael Hanselmann | """
|
32 | 95ab4de9 | David Knowles | |
33 | 5ef5cfea | Michael Hanselmann | # No Ganeti-specific modules should be imported. The RAPI client is supposed to
|
34 | 5ef5cfea | Michael Hanselmann | # be standalone.
|
35 | 5ef5cfea | Michael Hanselmann | |
36 | 9279e986 | Michael Hanselmann | import logging |
37 | 95ab4de9 | David Knowles | import simplejson |
38 | 95ab4de9 | David Knowles | import urllib |
39 | 2a7c3583 | Michael Hanselmann | import threading |
40 | 2a7c3583 | Michael Hanselmann | import pycurl |
41 | 2a7c3583 | Michael Hanselmann | |
42 | 2a7c3583 | Michael Hanselmann | try:
|
43 | 2a7c3583 | Michael Hanselmann | from cStringIO import StringIO |
44 | 2a7c3583 | Michael Hanselmann | except ImportError: |
45 | 2a7c3583 | Michael Hanselmann | from StringIO import StringIO |
46 | 95ab4de9 | David Knowles | |
47 | 95ab4de9 | David Knowles | |
48 | 9279e986 | Michael Hanselmann | GANETI_RAPI_PORT = 5080
|
49 | a198b2d9 | Michael Hanselmann | GANETI_RAPI_VERSION = 2
|
50 | 9279e986 | Michael Hanselmann | |
51 | 95ab4de9 | David Knowles | HTTP_DELETE = "DELETE"
|
52 | 95ab4de9 | David Knowles | HTTP_GET = "GET"
|
53 | 95ab4de9 | David Knowles | HTTP_PUT = "PUT"
|
54 | 95ab4de9 | David Knowles | HTTP_POST = "POST"
|
55 | 9279e986 | Michael Hanselmann | HTTP_OK = 200
|
56 | 7eac4a4d | Michael Hanselmann | HTTP_NOT_FOUND = 404
|
57 | 9279e986 | Michael Hanselmann | HTTP_APP_JSON = "application/json"
|
58 | 9279e986 | Michael Hanselmann | |
59 | 95ab4de9 | David Knowles | REPLACE_DISK_PRI = "replace_on_primary"
|
60 | 95ab4de9 | David Knowles | REPLACE_DISK_SECONDARY = "replace_on_secondary"
|
61 | 95ab4de9 | David Knowles | REPLACE_DISK_CHG = "replace_new_secondary"
|
62 | 95ab4de9 | David Knowles | REPLACE_DISK_AUTO = "replace_auto"
|
63 | 1068639f | Michael Hanselmann | |
64 | 1068639f | Michael Hanselmann | NODE_ROLE_DRAINED = "drained"
|
65 | 1068639f | Michael Hanselmann | NODE_ROLE_MASTER_CANDIATE = "master-candidate"
|
66 | 1068639f | Michael Hanselmann | NODE_ROLE_MASTER = "master"
|
67 | 1068639f | Michael Hanselmann | NODE_ROLE_OFFLINE = "offline"
|
68 | 1068639f | Michael Hanselmann | NODE_ROLE_REGULAR = "regular"
|
69 | 95ab4de9 | David Knowles | |
70 | 8a47b447 | Michael Hanselmann | # Internal constants
|
71 | 8a47b447 | Michael Hanselmann | _REQ_DATA_VERSION_FIELD = "__version__"
|
72 | 8a47b447 | Michael Hanselmann | _INST_CREATE_REQV1 = "instance-create-reqv1"
|
73 | 8a47b447 | Michael Hanselmann | |
74 | 2a7c3583 | Michael Hanselmann | # Older pycURL versions don't have all error constants
|
75 | 2a7c3583 | Michael Hanselmann | try:
|
76 | 2a7c3583 | Michael Hanselmann | _CURLE_SSL_CACERT = pycurl.E_SSL_CACERT |
77 | 2a7c3583 | Michael Hanselmann | _CURLE_SSL_CACERT_BADFILE = pycurl.E_SSL_CACERT_BADFILE |
78 | 2a7c3583 | Michael Hanselmann | except AttributeError: |
79 | 2a7c3583 | Michael Hanselmann | _CURLE_SSL_CACERT = 60
|
80 | 2a7c3583 | Michael Hanselmann | _CURLE_SSL_CACERT_BADFILE = 77
|
81 | 2a7c3583 | Michael Hanselmann | |
82 | 2a7c3583 | Michael Hanselmann | _CURL_SSL_CERT_ERRORS = frozenset([
|
83 | 2a7c3583 | Michael Hanselmann | _CURLE_SSL_CACERT, |
84 | 2a7c3583 | Michael Hanselmann | _CURLE_SSL_CACERT_BADFILE, |
85 | 2a7c3583 | Michael Hanselmann | ]) |
86 | 2a7c3583 | Michael Hanselmann | |
87 | 95ab4de9 | David Knowles | |
88 | 95ab4de9 | David Knowles | class Error(Exception): |
89 | 95ab4de9 | David Knowles | """Base error class for this module.
|
90 | 95ab4de9 | David Knowles |
|
91 | 95ab4de9 | David Knowles | """
|
92 | 95ab4de9 | David Knowles | pass
|
93 | 95ab4de9 | David Knowles | |
94 | 95ab4de9 | David Knowles | |
95 | 95ab4de9 | David Knowles | class CertificateError(Error): |
96 | 95ab4de9 | David Knowles | """Raised when a problem is found with the SSL certificate.
|
97 | 95ab4de9 | David Knowles |
|
98 | 95ab4de9 | David Knowles | """
|
99 | 95ab4de9 | David Knowles | pass
|
100 | 95ab4de9 | David Knowles | |
101 | 95ab4de9 | David Knowles | |
102 | 95ab4de9 | David Knowles | class GanetiApiError(Error): |
103 | 95ab4de9 | David Knowles | """Generic error raised from Ganeti API.
|
104 | 95ab4de9 | David Knowles |
|
105 | 95ab4de9 | David Knowles | """
|
106 | 8a019a03 | Michael Hanselmann | def __init__(self, msg, code=None): |
107 | 8a019a03 | Michael Hanselmann | Error.__init__(self, msg)
|
108 | 8a019a03 | Michael Hanselmann | self.code = code
|
109 | 95ab4de9 | David Knowles | |
110 | 95ab4de9 | David Knowles | |
111 | 2a7c3583 | Michael Hanselmann | def UsesRapiClient(fn): |
112 | 2a7c3583 | Michael Hanselmann | """Decorator for code using RAPI client to initialize pycURL.
|
113 | 9279e986 | Michael Hanselmann |
|
114 | 9279e986 | Michael Hanselmann | """
|
115 | 2a7c3583 | Michael Hanselmann | def wrapper(*args, **kwargs): |
116 | 2a7c3583 | Michael Hanselmann | # curl_global_init(3) and curl_global_cleanup(3) must be called with only
|
117 | 2a7c3583 | Michael Hanselmann | # one thread running. This check is just a safety measure -- it doesn't
|
118 | 2a7c3583 | Michael Hanselmann | # cover all cases.
|
119 | 2a7c3583 | Michael Hanselmann | assert threading.activeCount() == 1, \ |
120 | 2a7c3583 | Michael Hanselmann | "Found active threads when initializing pycURL"
|
121 | 2a7c3583 | Michael Hanselmann | |
122 | 2a7c3583 | Michael Hanselmann | pycurl.global_init(pycurl.GLOBAL_ALL) |
123 | 2a7c3583 | Michael Hanselmann | try:
|
124 | 2a7c3583 | Michael Hanselmann | return fn(*args, **kwargs)
|
125 | 2a7c3583 | Michael Hanselmann | finally:
|
126 | 2a7c3583 | Michael Hanselmann | pycurl.global_cleanup() |
127 | 2a7c3583 | Michael Hanselmann | |
128 | 2a7c3583 | Michael Hanselmann | return wrapper
|
129 | 2a7c3583 | Michael Hanselmann | |
130 | 2a7c3583 | Michael Hanselmann | |
131 | 2a7c3583 | Michael Hanselmann | def GenericCurlConfig(verbose=False, use_signal=False, |
132 | 2a7c3583 | Michael Hanselmann | use_curl_cabundle=False, cafile=None, capath=None, |
133 | 2a7c3583 | Michael Hanselmann | proxy=None, verify_hostname=False, |
134 | 2a7c3583 | Michael Hanselmann | connect_timeout=None, timeout=None, |
135 | 2a7c3583 | Michael Hanselmann | _pycurl_version_fn=pycurl.version_info): |
136 | 2a7c3583 | Michael Hanselmann | """Curl configuration function generator.
|
137 | 2a7c3583 | Michael Hanselmann |
|
138 | 2a7c3583 | Michael Hanselmann | @type verbose: bool
|
139 | 2a7c3583 | Michael Hanselmann | @param verbose: Whether to set cURL to verbose mode
|
140 | 2a7c3583 | Michael Hanselmann | @type use_signal: bool
|
141 | 2a7c3583 | Michael Hanselmann | @param use_signal: Whether to allow cURL to use signals
|
142 | 2a7c3583 | Michael Hanselmann | @type use_curl_cabundle: bool
|
143 | 2a7c3583 | Michael Hanselmann | @param use_curl_cabundle: Whether to use cURL's default CA bundle
|
144 | 2a7c3583 | Michael Hanselmann | @type cafile: string
|
145 | 2a7c3583 | Michael Hanselmann | @param cafile: In which file we can find the certificates
|
146 | 2a7c3583 | Michael Hanselmann | @type capath: string
|
147 | 2a7c3583 | Michael Hanselmann | @param capath: In which directory we can find the certificates
|
148 | 2a7c3583 | Michael Hanselmann | @type proxy: string
|
149 | 2a7c3583 | Michael Hanselmann | @param proxy: Proxy to use, None for default behaviour and empty string for
|
150 | 2a7c3583 | Michael Hanselmann | disabling proxies (see curl_easy_setopt(3))
|
151 | 2a7c3583 | Michael Hanselmann | @type verify_hostname: bool
|
152 | 2a7c3583 | Michael Hanselmann | @param verify_hostname: Whether to verify the remote peer certificate's
|
153 | 2a7c3583 | Michael Hanselmann | commonName
|
154 | 2a7c3583 | Michael Hanselmann | @type connect_timeout: number
|
155 | 2a7c3583 | Michael Hanselmann | @param connect_timeout: Timeout for establishing connection in seconds
|
156 | 2a7c3583 | Michael Hanselmann | @type timeout: number
|
157 | 2a7c3583 | Michael Hanselmann | @param timeout: Timeout for complete transfer in seconds (see
|
158 | 2a7c3583 | Michael Hanselmann | curl_easy_setopt(3)).
|
159 | 9279e986 | Michael Hanselmann |
|
160 | 9279e986 | Michael Hanselmann | """
|
161 | 2a7c3583 | Michael Hanselmann | if use_curl_cabundle and (cafile or capath): |
162 | 2a7c3583 | Michael Hanselmann | raise Error("Can not use default CA bundle when CA file or path is set") |
163 | 9279e986 | Michael Hanselmann | |
164 | 2a7c3583 | Michael Hanselmann | def _ConfigCurl(curl, logger): |
165 | 2a7c3583 | Michael Hanselmann | """Configures a cURL object
|
166 | 9279e986 | Michael Hanselmann |
|
167 | 2a7c3583 | Michael Hanselmann | @type curl: pycurl.Curl
|
168 | 2a7c3583 | Michael Hanselmann | @param curl: cURL object
|
169 | 9279e986 | Michael Hanselmann |
|
170 | 9279e986 | Michael Hanselmann | """
|
171 | 2a7c3583 | Michael Hanselmann | logger.debug("Using cURL version %s", pycurl.version)
|
172 | 2a7c3583 | Michael Hanselmann | |
173 | 2a7c3583 | Michael Hanselmann | # pycurl.version_info returns a tuple with information about the used
|
174 | 2a7c3583 | Michael Hanselmann | # version of libcurl. Item 5 is the SSL library linked to it.
|
175 | 2a7c3583 | Michael Hanselmann | # e.g.: (3, '7.18.0', 463360, 'x86_64-pc-linux-gnu', 1581, 'GnuTLS/2.0.4',
|
176 | 2a7c3583 | Michael Hanselmann | # 0, '1.2.3.3', ...)
|
177 | 2a7c3583 | Michael Hanselmann | sslver = _pycurl_version_fn()[5]
|
178 | 2a7c3583 | Michael Hanselmann | if not sslver: |
179 | 2a7c3583 | Michael Hanselmann | raise Error("No SSL support in cURL") |
180 | 2a7c3583 | Michael Hanselmann | |
181 | 2a7c3583 | Michael Hanselmann | lcsslver = sslver.lower() |
182 | 2a7c3583 | Michael Hanselmann | if lcsslver.startswith("openssl/"): |
183 | 2a7c3583 | Michael Hanselmann | pass
|
184 | 2a7c3583 | Michael Hanselmann | elif lcsslver.startswith("gnutls/"): |
185 | 2a7c3583 | Michael Hanselmann | if capath:
|
186 | 2a7c3583 | Michael Hanselmann | raise Error("cURL linked against GnuTLS has no support for a" |
187 | 2a7c3583 | Michael Hanselmann | " CA path (%s)" % (pycurl.version, ))
|
188 | 9279e986 | Michael Hanselmann | else:
|
189 | 2a7c3583 | Michael Hanselmann | raise NotImplementedError("cURL uses unsupported SSL version '%s'" % |
190 | 2a7c3583 | Michael Hanselmann | sslver) |
191 | 2a7c3583 | Michael Hanselmann | |
192 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.VERBOSE, verbose) |
193 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.NOSIGNAL, not use_signal)
|
194 | 2a7c3583 | Michael Hanselmann | |
195 | 2a7c3583 | Michael Hanselmann | # Whether to verify remote peer's CN
|
196 | 2a7c3583 | Michael Hanselmann | if verify_hostname:
|
197 | 2a7c3583 | Michael Hanselmann | # curl_easy_setopt(3): "When CURLOPT_SSL_VERIFYHOST is 2, that
|
198 | 2a7c3583 | Michael Hanselmann | # certificate must indicate that the server is the server to which you
|
199 | 2a7c3583 | Michael Hanselmann | # meant to connect, or the connection fails. [...] When the value is 1,
|
200 | 2a7c3583 | Michael Hanselmann | # the certificate must contain a Common Name field, but it doesn't matter
|
201 | 2a7c3583 | Michael Hanselmann | # what name it says. [...]"
|
202 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.SSL_VERIFYHOST, 2)
|
203 | beba56ae | Michael Hanselmann | else:
|
204 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.SSL_VERIFYHOST, 0)
|
205 | 2a7c3583 | Michael Hanselmann | |
206 | 2a7c3583 | Michael Hanselmann | if cafile or capath or use_curl_cabundle: |
207 | 2a7c3583 | Michael Hanselmann | # Require certificates to be checked
|
208 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.SSL_VERIFYPEER, True)
|
209 | 2a7c3583 | Michael Hanselmann | if cafile:
|
210 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.CAINFO, str(cafile))
|
211 | 2a7c3583 | Michael Hanselmann | if capath:
|
212 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.CAPATH, str(capath))
|
213 | 2a7c3583 | Michael Hanselmann | # Not changing anything for using default CA bundle
|
214 | 2a7c3583 | Michael Hanselmann | else:
|
215 | 2a7c3583 | Michael Hanselmann | # Disable SSL certificate verification
|
216 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.SSL_VERIFYPEER, False)
|
217 | 9279e986 | Michael Hanselmann | |
218 | 2a7c3583 | Michael Hanselmann | if proxy is not None: |
219 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.PROXY, str(proxy))
|
220 | 9279e986 | Michael Hanselmann | |
221 | 2a7c3583 | Michael Hanselmann | # Timeouts
|
222 | 2a7c3583 | Michael Hanselmann | if connect_timeout is not None: |
223 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.CONNECTTIMEOUT, connect_timeout) |
224 | 2a7c3583 | Michael Hanselmann | if timeout is not None: |
225 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.TIMEOUT, timeout) |
226 | 9279e986 | Michael Hanselmann | |
227 | 2a7c3583 | Michael Hanselmann | return _ConfigCurl
|
228 | 9279e986 | Michael Hanselmann | |
229 | 9279e986 | Michael Hanselmann | |
230 | 95ab4de9 | David Knowles | class GanetiRapiClient(object): |
231 | 95ab4de9 | David Knowles | """Ganeti RAPI client.
|
232 | 95ab4de9 | David Knowles |
|
233 | 95ab4de9 | David Knowles | """
|
234 | 95ab4de9 | David Knowles | USER_AGENT = "Ganeti RAPI Client"
|
235 | d3844674 | Michael Hanselmann | _json_encoder = simplejson.JSONEncoder(sort_keys=True)
|
236 | 95ab4de9 | David Knowles | |
237 | 9279e986 | Michael Hanselmann | def __init__(self, host, port=GANETI_RAPI_PORT, |
238 | 2a7c3583 | Michael Hanselmann | username=None, password=None, logger=logging, |
239 | 2a7c3583 | Michael Hanselmann | curl_config_fn=None, curl=None): |
240 | 2a7c3583 | Michael Hanselmann | """Initializes this class.
|
241 | 95ab4de9 | David Knowles |
|
242 | 9279e986 | Michael Hanselmann | @type host: string
|
243 | 9279e986 | Michael Hanselmann | @param host: the ganeti cluster master to interact with
|
244 | 95ab4de9 | David Knowles | @type port: int
|
245 | 9279e986 | Michael Hanselmann | @param port: the port on which the RAPI is running (default is 5080)
|
246 | 9279e986 | Michael Hanselmann | @type username: string
|
247 | 95ab4de9 | David Knowles | @param username: the username to connect with
|
248 | 9279e986 | Michael Hanselmann | @type password: string
|
249 | 95ab4de9 | David Knowles | @param password: the password to connect with
|
250 | 2a7c3583 | Michael Hanselmann | @type curl_config_fn: callable
|
251 | 2a7c3583 | Michael Hanselmann | @param curl_config_fn: Function to configure C{pycurl.Curl} object
|
252 | 9279e986 | Michael Hanselmann | @param logger: Logging object
|
253 | 95ab4de9 | David Knowles |
|
254 | 95ab4de9 | David Knowles | """
|
255 | 9279e986 | Michael Hanselmann | self._host = host
|
256 | 95ab4de9 | David Knowles | self._port = port
|
257 | 9279e986 | Michael Hanselmann | self._logger = logger
|
258 | 95ab4de9 | David Knowles | |
259 | 9279e986 | Michael Hanselmann | self._base_url = "https://%s:%s" % (host, port) |
260 | f2f88abf | David Knowles | |
261 | 2a7c3583 | Michael Hanselmann | # Create pycURL object if not supplied
|
262 | 2a7c3583 | Michael Hanselmann | if not curl: |
263 | 2a7c3583 | Michael Hanselmann | curl = pycurl.Curl() |
264 | 2a7c3583 | Michael Hanselmann | |
265 | 2a7c3583 | Michael Hanselmann | # Default cURL settings
|
266 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.VERBOSE, False)
|
267 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.FOLLOWLOCATION, False)
|
268 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.MAXREDIRS, 5)
|
269 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.NOSIGNAL, True)
|
270 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.USERAGENT, self.USER_AGENT)
|
271 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.SSL_VERIFYHOST, 0)
|
272 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.SSL_VERIFYPEER, False)
|
273 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.HTTPHEADER, [ |
274 | 2a7c3583 | Michael Hanselmann | "Accept: %s" % HTTP_APP_JSON,
|
275 | 2a7c3583 | Michael Hanselmann | "Content-type: %s" % HTTP_APP_JSON,
|
276 | 2a7c3583 | Michael Hanselmann | ]) |
277 | 2a7c3583 | Michael Hanselmann | |
278 | 2a7c3583 | Michael Hanselmann | # Setup authentication
|
279 | 9279e986 | Michael Hanselmann | if username is not None: |
280 | 2a7c3583 | Michael Hanselmann | if password is None: |
281 | 2a7c3583 | Michael Hanselmann | raise Error("Password not specified") |
282 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC) |
283 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.USERPWD, str("%s:%s" % (username, password))) |
284 | 9279e986 | Michael Hanselmann | elif password:
|
285 | 9279e986 | Michael Hanselmann | raise Error("Specified password without username") |
286 | 9279e986 | Michael Hanselmann | |
287 | 2a7c3583 | Michael Hanselmann | # Call external configuration function
|
288 | 2a7c3583 | Michael Hanselmann | if curl_config_fn:
|
289 | 2a7c3583 | Michael Hanselmann | curl_config_fn(curl, logger) |
290 | f2f88abf | David Knowles | |
291 | 2a7c3583 | Michael Hanselmann | self._curl = curl
|
292 | 95ab4de9 | David Knowles | |
293 | 10f5ab6c | Michael Hanselmann | @staticmethod
|
294 | 10f5ab6c | Michael Hanselmann | def _EncodeQuery(query): |
295 | 10f5ab6c | Michael Hanselmann | """Encode query values for RAPI URL.
|
296 | 10f5ab6c | Michael Hanselmann |
|
297 | 10f5ab6c | Michael Hanselmann | @type query: list of two-tuples
|
298 | 10f5ab6c | Michael Hanselmann | @param query: Query arguments
|
299 | 10f5ab6c | Michael Hanselmann | @rtype: list
|
300 | 10f5ab6c | Michael Hanselmann | @return: Query list with encoded values
|
301 | 10f5ab6c | Michael Hanselmann |
|
302 | 10f5ab6c | Michael Hanselmann | """
|
303 | 10f5ab6c | Michael Hanselmann | result = [] |
304 | 10f5ab6c | Michael Hanselmann | |
305 | 10f5ab6c | Michael Hanselmann | for name, value in query: |
306 | 10f5ab6c | Michael Hanselmann | if value is None: |
307 | 10f5ab6c | Michael Hanselmann | result.append((name, ""))
|
308 | 10f5ab6c | Michael Hanselmann | |
309 | 10f5ab6c | Michael Hanselmann | elif isinstance(value, bool): |
310 | 10f5ab6c | Michael Hanselmann | # Boolean values must be encoded as 0 or 1
|
311 | 10f5ab6c | Michael Hanselmann | result.append((name, int(value)))
|
312 | 10f5ab6c | Michael Hanselmann | |
313 | 10f5ab6c | Michael Hanselmann | elif isinstance(value, (list, tuple, dict)): |
314 | 10f5ab6c | Michael Hanselmann | raise ValueError("Invalid query data type %r" % type(value).__name__) |
315 | 10f5ab6c | Michael Hanselmann | |
316 | 10f5ab6c | Michael Hanselmann | else:
|
317 | 10f5ab6c | Michael Hanselmann | result.append((name, value)) |
318 | 10f5ab6c | Michael Hanselmann | |
319 | 10f5ab6c | Michael Hanselmann | return result
|
320 | 10f5ab6c | Michael Hanselmann | |
321 | 768747ed | Michael Hanselmann | def _SendRequest(self, method, path, query, content): |
322 | 95ab4de9 | David Knowles | """Sends an HTTP request.
|
323 | 95ab4de9 | David Knowles |
|
324 | 95ab4de9 | David Knowles | This constructs a full URL, encodes and decodes HTTP bodies, and
|
325 | 95ab4de9 | David Knowles | handles invalid responses in a pythonic way.
|
326 | 95ab4de9 | David Knowles |
|
327 | 768747ed | Michael Hanselmann | @type method: string
|
328 | 95ab4de9 | David Knowles | @param method: HTTP method to use
|
329 | 768747ed | Michael Hanselmann | @type path: string
|
330 | 95ab4de9 | David Knowles | @param path: HTTP URL path
|
331 | 95ab4de9 | David Knowles | @type query: list of two-tuples
|
332 | 95ab4de9 | David Knowles | @param query: query arguments to pass to urllib.urlencode
|
333 | 95ab4de9 | David Knowles | @type content: str or None
|
334 | 95ab4de9 | David Knowles | @param content: HTTP body content
|
335 | 95ab4de9 | David Knowles |
|
336 | 95ab4de9 | David Knowles | @rtype: str
|
337 | 95ab4de9 | David Knowles | @return: JSON-Decoded response
|
338 | 95ab4de9 | David Knowles |
|
339 | f2f88abf | David Knowles | @raises CertificateError: If an invalid SSL certificate is found
|
340 | 95ab4de9 | David Knowles | @raises GanetiApiError: If an invalid response is returned
|
341 | 95ab4de9 | David Knowles |
|
342 | 95ab4de9 | David Knowles | """
|
343 | ccd6b542 | Michael Hanselmann | assert path.startswith("/") |
344 | ccd6b542 | Michael Hanselmann | |
345 | 2a7c3583 | Michael Hanselmann | curl = self._curl
|
346 | 2a7c3583 | Michael Hanselmann | |
347 | 95ab4de9 | David Knowles | if content:
|
348 | d3844674 | Michael Hanselmann | encoded_content = self._json_encoder.encode(content)
|
349 | d3844674 | Michael Hanselmann | else:
|
350 | 2a7c3583 | Michael Hanselmann | encoded_content = ""
|
351 | 95ab4de9 | David Knowles | |
352 | ccd6b542 | Michael Hanselmann | # Build URL
|
353 | f961e2ba | Michael Hanselmann | urlparts = [self._base_url, path]
|
354 | ccd6b542 | Michael Hanselmann | if query:
|
355 | f961e2ba | Michael Hanselmann | urlparts.append("?")
|
356 | f961e2ba | Michael Hanselmann | urlparts.append(urllib.urlencode(self._EncodeQuery(query)))
|
357 | 9279e986 | Michael Hanselmann | |
358 | f961e2ba | Michael Hanselmann | url = "".join(urlparts)
|
359 | f961e2ba | Michael Hanselmann | |
360 | 2a7c3583 | Michael Hanselmann | self._logger.debug("Sending request %s %s to %s:%s (content=%r)", |
361 | 2a7c3583 | Michael Hanselmann | method, url, self._host, self._port, encoded_content) |
362 | 2a7c3583 | Michael Hanselmann | |
363 | 2a7c3583 | Michael Hanselmann | # Buffer for response
|
364 | 2a7c3583 | Michael Hanselmann | encoded_resp_body = StringIO() |
365 | f961e2ba | Michael Hanselmann | |
366 | 2a7c3583 | Michael Hanselmann | # Configure cURL
|
367 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.CUSTOMREQUEST, str(method))
|
368 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.URL, str(url))
|
369 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.POSTFIELDS, str(encoded_content))
|
370 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.WRITEFUNCTION, encoded_resp_body.write) |
371 | 9279e986 | Michael Hanselmann | |
372 | f2f88abf | David Knowles | try:
|
373 | 2a7c3583 | Michael Hanselmann | # Send request and wait for response
|
374 | 2a7c3583 | Michael Hanselmann | try:
|
375 | 2a7c3583 | Michael Hanselmann | curl.perform() |
376 | 2a7c3583 | Michael Hanselmann | except pycurl.error, err:
|
377 | 2a7c3583 | Michael Hanselmann | if err.args[0] in _CURL_SSL_CERT_ERRORS: |
378 | 2a7c3583 | Michael Hanselmann | raise CertificateError("SSL certificate error %s" % err) |
379 | 2a7c3583 | Michael Hanselmann | |
380 | 2a7c3583 | Michael Hanselmann | raise GanetiApiError(str(err)) |
381 | 2a7c3583 | Michael Hanselmann | finally:
|
382 | 2a7c3583 | Michael Hanselmann | # Reset settings to not keep references to large objects in memory
|
383 | 2a7c3583 | Michael Hanselmann | # between requests
|
384 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.POSTFIELDS, "")
|
385 | 2a7c3583 | Michael Hanselmann | curl.setopt(pycurl.WRITEFUNCTION, lambda _: None) |
386 | 2a7c3583 | Michael Hanselmann | |
387 | 2a7c3583 | Michael Hanselmann | # Get HTTP response code
|
388 | 2a7c3583 | Michael Hanselmann | http_code = curl.getinfo(pycurl.RESPONSE_CODE) |
389 | 2a7c3583 | Michael Hanselmann | |
390 | 2a7c3583 | Michael Hanselmann | # Was anything written to the response buffer?
|
391 | 2a7c3583 | Michael Hanselmann | if encoded_resp_body.tell():
|
392 | 2a7c3583 | Michael Hanselmann | response_content = simplejson.loads(encoded_resp_body.getvalue()) |
393 | d3844674 | Michael Hanselmann | else:
|
394 | d3844674 | Michael Hanselmann | response_content = None
|
395 | 95ab4de9 | David Knowles | |
396 | 2a7c3583 | Michael Hanselmann | if http_code != HTTP_OK:
|
397 | d3844674 | Michael Hanselmann | if isinstance(response_content, dict): |
398 | 95ab4de9 | David Knowles | msg = ("%s %s: %s" %
|
399 | d3844674 | Michael Hanselmann | (response_content["code"],
|
400 | d3844674 | Michael Hanselmann | response_content["message"],
|
401 | d3844674 | Michael Hanselmann | response_content["explain"]))
|
402 | 95ab4de9 | David Knowles | else:
|
403 | d3844674 | Michael Hanselmann | msg = str(response_content)
|
404 | d3844674 | Michael Hanselmann | |
405 | 2a7c3583 | Michael Hanselmann | raise GanetiApiError(msg, code=http_code)
|
406 | 95ab4de9 | David Knowles | |
407 | d3844674 | Michael Hanselmann | return response_content
|
408 | 95ab4de9 | David Knowles | |
409 | 95ab4de9 | David Knowles | def GetVersion(self): |
410 | cab667cc | David Knowles | """Gets the Remote API version running on the cluster.
|
411 | 95ab4de9 | David Knowles |
|
412 | 95ab4de9 | David Knowles | @rtype: int
|
413 | f2f88abf | David Knowles | @return: Ganeti Remote API version
|
414 | 95ab4de9 | David Knowles |
|
415 | 95ab4de9 | David Knowles | """
|
416 | 768747ed | Michael Hanselmann | return self._SendRequest(HTTP_GET, "/version", None, None) |
417 | 95ab4de9 | David Knowles | |
418 | 7eac4a4d | Michael Hanselmann | def GetFeatures(self): |
419 | 7eac4a4d | Michael Hanselmann | """Gets the list of optional features supported by RAPI server.
|
420 | 7eac4a4d | Michael Hanselmann |
|
421 | 7eac4a4d | Michael Hanselmann | @rtype: list
|
422 | 7eac4a4d | Michael Hanselmann | @return: List of optional features
|
423 | 7eac4a4d | Michael Hanselmann |
|
424 | 7eac4a4d | Michael Hanselmann | """
|
425 | 7eac4a4d | Michael Hanselmann | try:
|
426 | 7eac4a4d | Michael Hanselmann | return self._SendRequest(HTTP_GET, "/%s/features" % GANETI_RAPI_VERSION, |
427 | 7eac4a4d | Michael Hanselmann | None, None) |
428 | 7eac4a4d | Michael Hanselmann | except GanetiApiError, err:
|
429 | 7eac4a4d | Michael Hanselmann | # Older RAPI servers don't support this resource
|
430 | 7eac4a4d | Michael Hanselmann | if err.code == HTTP_NOT_FOUND:
|
431 | 7eac4a4d | Michael Hanselmann | return []
|
432 | 7eac4a4d | Michael Hanselmann | |
433 | 7eac4a4d | Michael Hanselmann | raise
|
434 | 7eac4a4d | Michael Hanselmann | |
435 | 95ab4de9 | David Knowles | def GetOperatingSystems(self): |
436 | 95ab4de9 | David Knowles | """Gets the Operating Systems running in the Ganeti cluster.
|
437 | 95ab4de9 | David Knowles |
|
438 | 95ab4de9 | David Knowles | @rtype: list of str
|
439 | 95ab4de9 | David Knowles | @return: operating systems
|
440 | 95ab4de9 | David Knowles |
|
441 | 95ab4de9 | David Knowles | """
|
442 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_GET, "/%s/os" % GANETI_RAPI_VERSION, |
443 | a198b2d9 | Michael Hanselmann | None, None) |
444 | 95ab4de9 | David Knowles | |
445 | 95ab4de9 | David Knowles | def GetInfo(self): |
446 | 95ab4de9 | David Knowles | """Gets info about the cluster.
|
447 | 95ab4de9 | David Knowles |
|
448 | 95ab4de9 | David Knowles | @rtype: dict
|
449 | 95ab4de9 | David Knowles | @return: information about the cluster
|
450 | 95ab4de9 | David Knowles |
|
451 | 95ab4de9 | David Knowles | """
|
452 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_GET, "/%s/info" % GANETI_RAPI_VERSION, |
453 | a198b2d9 | Michael Hanselmann | None, None) |
454 | 95ab4de9 | David Knowles | |
455 | 95ab4de9 | David Knowles | def GetClusterTags(self): |
456 | 95ab4de9 | David Knowles | """Gets the cluster tags.
|
457 | 95ab4de9 | David Knowles |
|
458 | 95ab4de9 | David Knowles | @rtype: list of str
|
459 | 95ab4de9 | David Knowles | @return: cluster tags
|
460 | 95ab4de9 | David Knowles |
|
461 | 95ab4de9 | David Knowles | """
|
462 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_GET, "/%s/tags" % GANETI_RAPI_VERSION, |
463 | a198b2d9 | Michael Hanselmann | None, None) |
464 | 95ab4de9 | David Knowles | |
465 | 95ab4de9 | David Knowles | def AddClusterTags(self, tags, dry_run=False): |
466 | 95ab4de9 | David Knowles | """Adds tags to the cluster.
|
467 | 95ab4de9 | David Knowles |
|
468 | 95ab4de9 | David Knowles | @type tags: list of str
|
469 | 95ab4de9 | David Knowles | @param tags: tags to add to the cluster
|
470 | 95ab4de9 | David Knowles | @type dry_run: bool
|
471 | 95ab4de9 | David Knowles | @param dry_run: whether to perform a dry run
|
472 | 95ab4de9 | David Knowles |
|
473 | 95ab4de9 | David Knowles | @rtype: int
|
474 | 95ab4de9 | David Knowles | @return: job id
|
475 | 95ab4de9 | David Knowles |
|
476 | 95ab4de9 | David Knowles | """
|
477 | 95ab4de9 | David Knowles | query = [("tag", t) for t in tags] |
478 | 95ab4de9 | David Knowles | if dry_run:
|
479 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
480 | 95ab4de9 | David Knowles | |
481 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_PUT, "/%s/tags" % GANETI_RAPI_VERSION, |
482 | a198b2d9 | Michael Hanselmann | query, None)
|
483 | 95ab4de9 | David Knowles | |
484 | 95ab4de9 | David Knowles | def DeleteClusterTags(self, tags, dry_run=False): |
485 | 95ab4de9 | David Knowles | """Deletes tags from the cluster.
|
486 | 95ab4de9 | David Knowles |
|
487 | 95ab4de9 | David Knowles | @type tags: list of str
|
488 | 95ab4de9 | David Knowles | @param tags: tags to delete
|
489 | 95ab4de9 | David Knowles | @type dry_run: bool
|
490 | 95ab4de9 | David Knowles | @param dry_run: whether to perform a dry run
|
491 | 95ab4de9 | David Knowles |
|
492 | 95ab4de9 | David Knowles | """
|
493 | 95ab4de9 | David Knowles | query = [("tag", t) for t in tags] |
494 | 95ab4de9 | David Knowles | if dry_run:
|
495 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
496 | 95ab4de9 | David Knowles | |
497 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_DELETE, "/%s/tags" % GANETI_RAPI_VERSION, |
498 | a198b2d9 | Michael Hanselmann | query, None)
|
499 | 95ab4de9 | David Knowles | |
500 | 95ab4de9 | David Knowles | def GetInstances(self, bulk=False): |
501 | 95ab4de9 | David Knowles | """Gets information about instances on the cluster.
|
502 | 95ab4de9 | David Knowles |
|
503 | 95ab4de9 | David Knowles | @type bulk: bool
|
504 | 95ab4de9 | David Knowles | @param bulk: whether to return all information about all instances
|
505 | 95ab4de9 | David Knowles |
|
506 | 95ab4de9 | David Knowles | @rtype: list of dict or list of str
|
507 | 95ab4de9 | David Knowles | @return: if bulk is True, info about the instances, else a list of instances
|
508 | 95ab4de9 | David Knowles |
|
509 | 95ab4de9 | David Knowles | """
|
510 | 95ab4de9 | David Knowles | query = [] |
511 | 95ab4de9 | David Knowles | if bulk:
|
512 | 95ab4de9 | David Knowles | query.append(("bulk", 1)) |
513 | 95ab4de9 | David Knowles | |
514 | a198b2d9 | Michael Hanselmann | instances = self._SendRequest(HTTP_GET,
|
515 | a198b2d9 | Michael Hanselmann | "/%s/instances" % GANETI_RAPI_VERSION,
|
516 | a198b2d9 | Michael Hanselmann | query, None)
|
517 | 95ab4de9 | David Knowles | if bulk:
|
518 | 95ab4de9 | David Knowles | return instances
|
519 | 95ab4de9 | David Knowles | else:
|
520 | 95ab4de9 | David Knowles | return [i["id"] for i in instances] |
521 | 95ab4de9 | David Knowles | |
522 | 591e5103 | Michael Hanselmann | def GetInstance(self, instance): |
523 | 95ab4de9 | David Knowles | """Gets information about an instance.
|
524 | 95ab4de9 | David Knowles |
|
525 | 95ab4de9 | David Knowles | @type instance: str
|
526 | 95ab4de9 | David Knowles | @param instance: instance whose info to return
|
527 | 95ab4de9 | David Knowles |
|
528 | 95ab4de9 | David Knowles | @rtype: dict
|
529 | 95ab4de9 | David Knowles | @return: info about the instance
|
530 | 95ab4de9 | David Knowles |
|
531 | 95ab4de9 | David Knowles | """
|
532 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_GET, |
533 | a198b2d9 | Michael Hanselmann | ("/%s/instances/%s" %
|
534 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, instance)), None, None) |
535 | 95ab4de9 | David Knowles | |
536 | 591e5103 | Michael Hanselmann | def GetInstanceInfo(self, instance, static=None): |
537 | 591e5103 | Michael Hanselmann | """Gets information about an instance.
|
538 | 591e5103 | Michael Hanselmann |
|
539 | 591e5103 | Michael Hanselmann | @type instance: string
|
540 | 591e5103 | Michael Hanselmann | @param instance: Instance name
|
541 | 591e5103 | Michael Hanselmann | @rtype: string
|
542 | 591e5103 | Michael Hanselmann | @return: Job ID
|
543 | 591e5103 | Michael Hanselmann |
|
544 | 591e5103 | Michael Hanselmann | """
|
545 | 591e5103 | Michael Hanselmann | if static is not None: |
546 | 591e5103 | Michael Hanselmann | query = [("static", static)]
|
547 | 591e5103 | Michael Hanselmann | else:
|
548 | 591e5103 | Michael Hanselmann | query = None
|
549 | 591e5103 | Michael Hanselmann | |
550 | 591e5103 | Michael Hanselmann | return self._SendRequest(HTTP_GET, |
551 | 591e5103 | Michael Hanselmann | ("/%s/instances/%s/info" %
|
552 | 591e5103 | Michael Hanselmann | (GANETI_RAPI_VERSION, instance)), query, None)
|
553 | 591e5103 | Michael Hanselmann | |
554 | 8a47b447 | Michael Hanselmann | def CreateInstance(self, mode, name, disk_template, disks, nics, |
555 | 8a47b447 | Michael Hanselmann | **kwargs): |
556 | 95ab4de9 | David Knowles | """Creates a new instance.
|
557 | 95ab4de9 | David Knowles |
|
558 | 8a47b447 | Michael Hanselmann | More details for parameters can be found in the RAPI documentation.
|
559 | 8a47b447 | Michael Hanselmann |
|
560 | 8a47b447 | Michael Hanselmann | @type mode: string
|
561 | 8a47b447 | Michael Hanselmann | @param mode: Instance creation mode
|
562 | 8a47b447 | Michael Hanselmann | @type name: string
|
563 | 8a47b447 | Michael Hanselmann | @param name: Hostname of the instance to create
|
564 | 8a47b447 | Michael Hanselmann | @type disk_template: string
|
565 | 8a47b447 | Michael Hanselmann | @param disk_template: Disk template for instance (e.g. plain, diskless,
|
566 | 8a47b447 | Michael Hanselmann | file, or drbd)
|
567 | 8a47b447 | Michael Hanselmann | @type disks: list of dicts
|
568 | 8a47b447 | Michael Hanselmann | @param disks: List of disk definitions
|
569 | 8a47b447 | Michael Hanselmann | @type nics: list of dicts
|
570 | 8a47b447 | Michael Hanselmann | @param nics: List of NIC definitions
|
571 | 95ab4de9 | David Knowles | @type dry_run: bool
|
572 | 8a47b447 | Michael Hanselmann | @keyword dry_run: whether to perform a dry run
|
573 | 95ab4de9 | David Knowles |
|
574 | 95ab4de9 | David Knowles | @rtype: int
|
575 | 95ab4de9 | David Knowles | @return: job id
|
576 | 95ab4de9 | David Knowles |
|
577 | 95ab4de9 | David Knowles | """
|
578 | 95ab4de9 | David Knowles | query = [] |
579 | 8a47b447 | Michael Hanselmann | |
580 | 8a47b447 | Michael Hanselmann | if kwargs.get("dry_run"): |
581 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
582 | 95ab4de9 | David Knowles | |
583 | 8a47b447 | Michael Hanselmann | if _INST_CREATE_REQV1 in self.GetFeatures(): |
584 | 8a47b447 | Michael Hanselmann | # All required fields for request data version 1
|
585 | 8a47b447 | Michael Hanselmann | body = { |
586 | 8a47b447 | Michael Hanselmann | _REQ_DATA_VERSION_FIELD: 1,
|
587 | 8a47b447 | Michael Hanselmann | "mode": mode,
|
588 | 8a47b447 | Michael Hanselmann | "name": name,
|
589 | 8a47b447 | Michael Hanselmann | "disk_template": disk_template,
|
590 | 8a47b447 | Michael Hanselmann | "disks": disks,
|
591 | 8a47b447 | Michael Hanselmann | "nics": nics,
|
592 | 8a47b447 | Michael Hanselmann | } |
593 | 8a47b447 | Michael Hanselmann | |
594 | 8a47b447 | Michael Hanselmann | conflicts = set(kwargs.iterkeys()) & set(body.iterkeys()) |
595 | 8a47b447 | Michael Hanselmann | if conflicts:
|
596 | 8a47b447 | Michael Hanselmann | raise GanetiApiError("Required fields can not be specified as" |
597 | 8a47b447 | Michael Hanselmann | " keywords: %s" % ", ".join(conflicts)) |
598 | 8a47b447 | Michael Hanselmann | |
599 | 8a47b447 | Michael Hanselmann | body.update((key, value) for key, value in kwargs.iteritems() |
600 | 8a47b447 | Michael Hanselmann | if key != "dry_run") |
601 | 8a47b447 | Michael Hanselmann | else:
|
602 | 8a47b447 | Michael Hanselmann | # TODO: Implement instance creation request data version 0
|
603 | 8a47b447 | Michael Hanselmann | # When implementing version 0, care should be taken to refuse unknown
|
604 | 8a47b447 | Michael Hanselmann | # parameters and invalid values. The interface of this function must stay
|
605 | 8a47b447 | Michael Hanselmann | # exactly the same for version 0 and 1 (e.g. they aren't allowed to
|
606 | 8a47b447 | Michael Hanselmann | # require different data types).
|
607 | 8a47b447 | Michael Hanselmann | raise NotImplementedError("Support for instance creation request data" |
608 | 8a47b447 | Michael Hanselmann | " version 0 is not yet implemented")
|
609 | 8a47b447 | Michael Hanselmann | |
610 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_POST, "/%s/instances" % GANETI_RAPI_VERSION, |
611 | 8a47b447 | Michael Hanselmann | query, body) |
612 | 95ab4de9 | David Knowles | |
613 | 95ab4de9 | David Knowles | def DeleteInstance(self, instance, dry_run=False): |
614 | 95ab4de9 | David Knowles | """Deletes an instance.
|
615 | 95ab4de9 | David Knowles |
|
616 | 95ab4de9 | David Knowles | @type instance: str
|
617 | 95ab4de9 | David Knowles | @param instance: the instance to delete
|
618 | 95ab4de9 | David Knowles |
|
619 | cab667cc | David Knowles | @rtype: int
|
620 | cab667cc | David Knowles | @return: job id
|
621 | cab667cc | David Knowles |
|
622 | 95ab4de9 | David Knowles | """
|
623 | 95ab4de9 | David Knowles | query = [] |
624 | 95ab4de9 | David Knowles | if dry_run:
|
625 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
626 | 95ab4de9 | David Knowles | |
627 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_DELETE, |
628 | a198b2d9 | Michael Hanselmann | ("/%s/instances/%s" %
|
629 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, instance)), query, None)
|
630 | 95ab4de9 | David Knowles | |
631 | 95ab4de9 | David Knowles | def GetInstanceTags(self, instance): |
632 | 95ab4de9 | David Knowles | """Gets tags for an instance.
|
633 | 95ab4de9 | David Knowles |
|
634 | 95ab4de9 | David Knowles | @type instance: str
|
635 | 95ab4de9 | David Knowles | @param instance: instance whose tags to return
|
636 | 95ab4de9 | David Knowles |
|
637 | 95ab4de9 | David Knowles | @rtype: list of str
|
638 | 95ab4de9 | David Knowles | @return: tags for the instance
|
639 | 95ab4de9 | David Knowles |
|
640 | 95ab4de9 | David Knowles | """
|
641 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_GET, |
642 | a198b2d9 | Michael Hanselmann | ("/%s/instances/%s/tags" %
|
643 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, instance)), None, None) |
644 | 95ab4de9 | David Knowles | |
645 | 95ab4de9 | David Knowles | def AddInstanceTags(self, instance, tags, dry_run=False): |
646 | 95ab4de9 | David Knowles | """Adds tags to an instance.
|
647 | 95ab4de9 | David Knowles |
|
648 | 95ab4de9 | David Knowles | @type instance: str
|
649 | 95ab4de9 | David Knowles | @param instance: instance to add tags to
|
650 | 95ab4de9 | David Knowles | @type tags: list of str
|
651 | 95ab4de9 | David Knowles | @param tags: tags to add to the instance
|
652 | 95ab4de9 | David Knowles | @type dry_run: bool
|
653 | 95ab4de9 | David Knowles | @param dry_run: whether to perform a dry run
|
654 | 95ab4de9 | David Knowles |
|
655 | 95ab4de9 | David Knowles | @rtype: int
|
656 | 95ab4de9 | David Knowles | @return: job id
|
657 | 95ab4de9 | David Knowles |
|
658 | 95ab4de9 | David Knowles | """
|
659 | 95ab4de9 | David Knowles | query = [("tag", t) for t in tags] |
660 | 95ab4de9 | David Knowles | if dry_run:
|
661 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
662 | 95ab4de9 | David Knowles | |
663 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_PUT, |
664 | a198b2d9 | Michael Hanselmann | ("/%s/instances/%s/tags" %
|
665 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, instance)), query, None)
|
666 | 95ab4de9 | David Knowles | |
667 | 95ab4de9 | David Knowles | def DeleteInstanceTags(self, instance, tags, dry_run=False): |
668 | 95ab4de9 | David Knowles | """Deletes tags from an instance.
|
669 | 95ab4de9 | David Knowles |
|
670 | 95ab4de9 | David Knowles | @type instance: str
|
671 | 95ab4de9 | David Knowles | @param instance: instance to delete tags from
|
672 | 95ab4de9 | David Knowles | @type tags: list of str
|
673 | 95ab4de9 | David Knowles | @param tags: tags to delete
|
674 | 95ab4de9 | David Knowles | @type dry_run: bool
|
675 | 95ab4de9 | David Knowles | @param dry_run: whether to perform a dry run
|
676 | 95ab4de9 | David Knowles |
|
677 | 95ab4de9 | David Knowles | """
|
678 | 95ab4de9 | David Knowles | query = [("tag", t) for t in tags] |
679 | 95ab4de9 | David Knowles | if dry_run:
|
680 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
681 | 95ab4de9 | David Knowles | |
682 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_DELETE, |
683 | a198b2d9 | Michael Hanselmann | ("/%s/instances/%s/tags" %
|
684 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, instance)), query, None)
|
685 | 95ab4de9 | David Knowles | |
686 | 95ab4de9 | David Knowles | def RebootInstance(self, instance, reboot_type=None, ignore_secondaries=None, |
687 | 95ab4de9 | David Knowles | dry_run=False):
|
688 | 95ab4de9 | David Knowles | """Reboots an instance.
|
689 | 95ab4de9 | David Knowles |
|
690 | 95ab4de9 | David Knowles | @type instance: str
|
691 | 95ab4de9 | David Knowles | @param instance: instance to rebot
|
692 | 95ab4de9 | David Knowles | @type reboot_type: str
|
693 | 95ab4de9 | David Knowles | @param reboot_type: one of: hard, soft, full
|
694 | 95ab4de9 | David Knowles | @type ignore_secondaries: bool
|
695 | 95ab4de9 | David Knowles | @param ignore_secondaries: if True, ignores errors for the secondary node
|
696 | 95ab4de9 | David Knowles | while re-assembling disks (in hard-reboot mode only)
|
697 | 95ab4de9 | David Knowles | @type dry_run: bool
|
698 | 95ab4de9 | David Knowles | @param dry_run: whether to perform a dry run
|
699 | 95ab4de9 | David Knowles |
|
700 | 95ab4de9 | David Knowles | """
|
701 | 95ab4de9 | David Knowles | query = [] |
702 | 95ab4de9 | David Knowles | if reboot_type:
|
703 | 95ab4de9 | David Knowles | query.append(("type", reboot_type))
|
704 | 95ab4de9 | David Knowles | if ignore_secondaries is not None: |
705 | 95ab4de9 | David Knowles | query.append(("ignore_secondaries", ignore_secondaries))
|
706 | 95ab4de9 | David Knowles | if dry_run:
|
707 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
708 | 95ab4de9 | David Knowles | |
709 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_POST, |
710 | a198b2d9 | Michael Hanselmann | ("/%s/instances/%s/reboot" %
|
711 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, instance)), query, None)
|
712 | 95ab4de9 | David Knowles | |
713 | 95ab4de9 | David Knowles | def ShutdownInstance(self, instance, dry_run=False): |
714 | 95ab4de9 | David Knowles | """Shuts down an instance.
|
715 | 95ab4de9 | David Knowles |
|
716 | 95ab4de9 | David Knowles | @type instance: str
|
717 | 95ab4de9 | David Knowles | @param instance: the instance to shut down
|
718 | 95ab4de9 | David Knowles | @type dry_run: bool
|
719 | 95ab4de9 | David Knowles | @param dry_run: whether to perform a dry run
|
720 | 95ab4de9 | David Knowles |
|
721 | 95ab4de9 | David Knowles | """
|
722 | 95ab4de9 | David Knowles | query = [] |
723 | 95ab4de9 | David Knowles | if dry_run:
|
724 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
725 | 95ab4de9 | David Knowles | |
726 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_PUT, |
727 | a198b2d9 | Michael Hanselmann | ("/%s/instances/%s/shutdown" %
|
728 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, instance)), query, None)
|
729 | 95ab4de9 | David Knowles | |
730 | 95ab4de9 | David Knowles | def StartupInstance(self, instance, dry_run=False): |
731 | 95ab4de9 | David Knowles | """Starts up an instance.
|
732 | 95ab4de9 | David Knowles |
|
733 | 95ab4de9 | David Knowles | @type instance: str
|
734 | 95ab4de9 | David Knowles | @param instance: the instance to start up
|
735 | 95ab4de9 | David Knowles | @type dry_run: bool
|
736 | 95ab4de9 | David Knowles | @param dry_run: whether to perform a dry run
|
737 | 95ab4de9 | David Knowles |
|
738 | 95ab4de9 | David Knowles | """
|
739 | 95ab4de9 | David Knowles | query = [] |
740 | 95ab4de9 | David Knowles | if dry_run:
|
741 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
742 | 95ab4de9 | David Knowles | |
743 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_PUT, |
744 | a198b2d9 | Michael Hanselmann | ("/%s/instances/%s/startup" %
|
745 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, instance)), query, None)
|
746 | 95ab4de9 | David Knowles | |
747 | 95ab4de9 | David Knowles | def ReinstallInstance(self, instance, os, no_startup=False): |
748 | 95ab4de9 | David Knowles | """Reinstalls an instance.
|
749 | 95ab4de9 | David Knowles |
|
750 | 95ab4de9 | David Knowles | @type instance: str
|
751 | 95ab4de9 | David Knowles | @param instance: the instance to reinstall
|
752 | 95ab4de9 | David Knowles | @type os: str
|
753 | 95ab4de9 | David Knowles | @param os: the os to reinstall
|
754 | 95ab4de9 | David Knowles | @type no_startup: bool
|
755 | 95ab4de9 | David Knowles | @param no_startup: whether to start the instance automatically
|
756 | 95ab4de9 | David Knowles |
|
757 | 95ab4de9 | David Knowles | """
|
758 | 95ab4de9 | David Knowles | query = [("os", os)]
|
759 | 95ab4de9 | David Knowles | if no_startup:
|
760 | 95ab4de9 | David Knowles | query.append(("nostartup", 1)) |
761 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_POST, |
762 | a198b2d9 | Michael Hanselmann | ("/%s/instances/%s/reinstall" %
|
763 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, instance)), query, None)
|
764 | 95ab4de9 | David Knowles | |
765 | bfc2002f | Michael Hanselmann | def ReplaceInstanceDisks(self, instance, disks=None, mode=REPLACE_DISK_AUTO, |
766 | cfc03c54 | Michael Hanselmann | remote_node=None, iallocator=None, dry_run=False): |
767 | 95ab4de9 | David Knowles | """Replaces disks on an instance.
|
768 | 95ab4de9 | David Knowles |
|
769 | 95ab4de9 | David Knowles | @type instance: str
|
770 | 95ab4de9 | David Knowles | @param instance: instance whose disks to replace
|
771 | bfc2002f | Michael Hanselmann | @type disks: list of ints
|
772 | bfc2002f | Michael Hanselmann | @param disks: Indexes of disks to replace
|
773 | 95ab4de9 | David Knowles | @type mode: str
|
774 | cfc03c54 | Michael Hanselmann | @param mode: replacement mode to use (defaults to replace_auto)
|
775 | 95ab4de9 | David Knowles | @type remote_node: str or None
|
776 | 95ab4de9 | David Knowles | @param remote_node: new secondary node to use (for use with
|
777 | cfc03c54 | Michael Hanselmann | replace_new_secondary mode)
|
778 | 95ab4de9 | David Knowles | @type iallocator: str or None
|
779 | 95ab4de9 | David Knowles | @param iallocator: instance allocator plugin to use (for use with
|
780 | cfc03c54 | Michael Hanselmann | replace_auto mode)
|
781 | 95ab4de9 | David Knowles | @type dry_run: bool
|
782 | 95ab4de9 | David Knowles | @param dry_run: whether to perform a dry run
|
783 | 95ab4de9 | David Knowles |
|
784 | 95ab4de9 | David Knowles | @rtype: int
|
785 | 95ab4de9 | David Knowles | @return: job id
|
786 | 95ab4de9 | David Knowles |
|
787 | 95ab4de9 | David Knowles | """
|
788 | cfc03c54 | Michael Hanselmann | query = [ |
789 | cfc03c54 | Michael Hanselmann | ("mode", mode),
|
790 | cfc03c54 | Michael Hanselmann | ] |
791 | 95ab4de9 | David Knowles | |
792 | bfc2002f | Michael Hanselmann | if disks:
|
793 | bfc2002f | Michael Hanselmann | query.append(("disks", ",".join(str(idx) for idx in disks))) |
794 | bfc2002f | Michael Hanselmann | |
795 | bfc2002f | Michael Hanselmann | if remote_node:
|
796 | 95ab4de9 | David Knowles | query.append(("remote_node", remote_node))
|
797 | 95ab4de9 | David Knowles | |
798 | bfc2002f | Michael Hanselmann | if iallocator:
|
799 | bfc2002f | Michael Hanselmann | query.append(("iallocator", iallocator))
|
800 | bfc2002f | Michael Hanselmann | |
801 | 95ab4de9 | David Knowles | if dry_run:
|
802 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
803 | 95ab4de9 | David Knowles | |
804 | 95ab4de9 | David Knowles | return self._SendRequest(HTTP_POST, |
805 | a198b2d9 | Michael Hanselmann | ("/%s/instances/%s/replace-disks" %
|
806 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, instance)), query, None)
|
807 | 95ab4de9 | David Knowles | |
808 | ebeb600f | Michael Hanselmann | def PrepareExport(self, instance, mode): |
809 | ebeb600f | Michael Hanselmann | """Prepares an instance for an export.
|
810 | ebeb600f | Michael Hanselmann |
|
811 | ebeb600f | Michael Hanselmann | @type instance: string
|
812 | ebeb600f | Michael Hanselmann | @param instance: Instance name
|
813 | ebeb600f | Michael Hanselmann | @type mode: string
|
814 | ebeb600f | Michael Hanselmann | @param mode: Export mode
|
815 | ebeb600f | Michael Hanselmann | @rtype: string
|
816 | ebeb600f | Michael Hanselmann | @return: Job ID
|
817 | ebeb600f | Michael Hanselmann |
|
818 | ebeb600f | Michael Hanselmann | """
|
819 | ebeb600f | Michael Hanselmann | query = [("mode", mode)]
|
820 | ebeb600f | Michael Hanselmann | return self._SendRequest(HTTP_PUT, |
821 | ebeb600f | Michael Hanselmann | ("/%s/instances/%s/prepare-export" %
|
822 | ebeb600f | Michael Hanselmann | (GANETI_RAPI_VERSION, instance)), query, None)
|
823 | ebeb600f | Michael Hanselmann | |
824 | ebeb600f | Michael Hanselmann | def ExportInstance(self, instance, mode, destination, shutdown=None, |
825 | ebeb600f | Michael Hanselmann | remove_instance=None,
|
826 | ebeb600f | Michael Hanselmann | x509_key_name=None, destination_x509_ca=None): |
827 | ebeb600f | Michael Hanselmann | """Exports an instance.
|
828 | ebeb600f | Michael Hanselmann |
|
829 | ebeb600f | Michael Hanselmann | @type instance: string
|
830 | ebeb600f | Michael Hanselmann | @param instance: Instance name
|
831 | ebeb600f | Michael Hanselmann | @type mode: string
|
832 | ebeb600f | Michael Hanselmann | @param mode: Export mode
|
833 | ebeb600f | Michael Hanselmann | @rtype: string
|
834 | ebeb600f | Michael Hanselmann | @return: Job ID
|
835 | ebeb600f | Michael Hanselmann |
|
836 | ebeb600f | Michael Hanselmann | """
|
837 | ebeb600f | Michael Hanselmann | body = { |
838 | ebeb600f | Michael Hanselmann | "destination": destination,
|
839 | ebeb600f | Michael Hanselmann | "mode": mode,
|
840 | ebeb600f | Michael Hanselmann | } |
841 | ebeb600f | Michael Hanselmann | |
842 | ebeb600f | Michael Hanselmann | if shutdown is not None: |
843 | ebeb600f | Michael Hanselmann | body["shutdown"] = shutdown
|
844 | ebeb600f | Michael Hanselmann | |
845 | ebeb600f | Michael Hanselmann | if remove_instance is not None: |
846 | ebeb600f | Michael Hanselmann | body["remove_instance"] = remove_instance
|
847 | ebeb600f | Michael Hanselmann | |
848 | ebeb600f | Michael Hanselmann | if x509_key_name is not None: |
849 | ebeb600f | Michael Hanselmann | body["x509_key_name"] = x509_key_name
|
850 | ebeb600f | Michael Hanselmann | |
851 | ebeb600f | Michael Hanselmann | if destination_x509_ca is not None: |
852 | ebeb600f | Michael Hanselmann | body["destination_x509_ca"] = destination_x509_ca
|
853 | ebeb600f | Michael Hanselmann | |
854 | ebeb600f | Michael Hanselmann | return self._SendRequest(HTTP_PUT, |
855 | ebeb600f | Michael Hanselmann | ("/%s/instances/%s/export" %
|
856 | ebeb600f | Michael Hanselmann | (GANETI_RAPI_VERSION, instance)), None, body)
|
857 | ebeb600f | Michael Hanselmann | |
858 | 95ab4de9 | David Knowles | def GetJobs(self): |
859 | 95ab4de9 | David Knowles | """Gets all jobs for the cluster.
|
860 | 95ab4de9 | David Knowles |
|
861 | 95ab4de9 | David Knowles | @rtype: list of int
|
862 | 95ab4de9 | David Knowles | @return: job ids for the cluster
|
863 | 95ab4de9 | David Knowles |
|
864 | 95ab4de9 | David Knowles | """
|
865 | 768747ed | Michael Hanselmann | return [int(j["id"]) |
866 | a198b2d9 | Michael Hanselmann | for j in self._SendRequest(HTTP_GET, |
867 | a198b2d9 | Michael Hanselmann | "/%s/jobs" % GANETI_RAPI_VERSION,
|
868 | a198b2d9 | Michael Hanselmann | None, None)] |
869 | 95ab4de9 | David Knowles | |
870 | 95ab4de9 | David Knowles | def GetJobStatus(self, job_id): |
871 | 95ab4de9 | David Knowles | """Gets the status of a job.
|
872 | 95ab4de9 | David Knowles |
|
873 | 95ab4de9 | David Knowles | @type job_id: int
|
874 | 95ab4de9 | David Knowles | @param job_id: job id whose status to query
|
875 | 95ab4de9 | David Knowles |
|
876 | 95ab4de9 | David Knowles | @rtype: dict
|
877 | 95ab4de9 | David Knowles | @return: job status
|
878 | 95ab4de9 | David Knowles |
|
879 | 95ab4de9 | David Knowles | """
|
880 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_GET, |
881 | a198b2d9 | Michael Hanselmann | "/%s/jobs/%s" % (GANETI_RAPI_VERSION, job_id),
|
882 | a198b2d9 | Michael Hanselmann | None, None) |
883 | 95ab4de9 | David Knowles | |
884 | d9b67f70 | Michael Hanselmann | def WaitForJobChange(self, job_id, fields, prev_job_info, prev_log_serial): |
885 | d9b67f70 | Michael Hanselmann | """Waits for job changes.
|
886 | d9b67f70 | Michael Hanselmann |
|
887 | d9b67f70 | Michael Hanselmann | @type job_id: int
|
888 | d9b67f70 | Michael Hanselmann | @param job_id: Job ID for which to wait
|
889 | d9b67f70 | Michael Hanselmann |
|
890 | d9b67f70 | Michael Hanselmann | """
|
891 | d9b67f70 | Michael Hanselmann | body = { |
892 | d9b67f70 | Michael Hanselmann | "fields": fields,
|
893 | d9b67f70 | Michael Hanselmann | "previous_job_info": prev_job_info,
|
894 | d9b67f70 | Michael Hanselmann | "previous_log_serial": prev_log_serial,
|
895 | d9b67f70 | Michael Hanselmann | } |
896 | d9b67f70 | Michael Hanselmann | |
897 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_GET, |
898 | a198b2d9 | Michael Hanselmann | "/%s/jobs/%s/wait" % (GANETI_RAPI_VERSION, job_id),
|
899 | a198b2d9 | Michael Hanselmann | None, body)
|
900 | d9b67f70 | Michael Hanselmann | |
901 | cf9ada49 | Michael Hanselmann | def CancelJob(self, job_id, dry_run=False): |
902 | cf9ada49 | Michael Hanselmann | """Cancels a job.
|
903 | 95ab4de9 | David Knowles |
|
904 | 95ab4de9 | David Knowles | @type job_id: int
|
905 | 95ab4de9 | David Knowles | @param job_id: id of the job to delete
|
906 | 95ab4de9 | David Knowles | @type dry_run: bool
|
907 | 95ab4de9 | David Knowles | @param dry_run: whether to perform a dry run
|
908 | 95ab4de9 | David Knowles |
|
909 | 95ab4de9 | David Knowles | """
|
910 | 95ab4de9 | David Knowles | query = [] |
911 | 95ab4de9 | David Knowles | if dry_run:
|
912 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
913 | 95ab4de9 | David Knowles | |
914 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_DELETE, |
915 | a198b2d9 | Michael Hanselmann | "/%s/jobs/%s" % (GANETI_RAPI_VERSION, job_id),
|
916 | a198b2d9 | Michael Hanselmann | query, None)
|
917 | 95ab4de9 | David Knowles | |
918 | 95ab4de9 | David Knowles | def GetNodes(self, bulk=False): |
919 | 95ab4de9 | David Knowles | """Gets all nodes in the cluster.
|
920 | 95ab4de9 | David Knowles |
|
921 | 95ab4de9 | David Knowles | @type bulk: bool
|
922 | 95ab4de9 | David Knowles | @param bulk: whether to return all information about all instances
|
923 | 95ab4de9 | David Knowles |
|
924 | 95ab4de9 | David Knowles | @rtype: list of dict or str
|
925 | 95ab4de9 | David Knowles | @return: if bulk is true, info about nodes in the cluster,
|
926 | 95ab4de9 | David Knowles | else list of nodes in the cluster
|
927 | 95ab4de9 | David Knowles |
|
928 | 95ab4de9 | David Knowles | """
|
929 | 95ab4de9 | David Knowles | query = [] |
930 | 95ab4de9 | David Knowles | if bulk:
|
931 | 95ab4de9 | David Knowles | query.append(("bulk", 1)) |
932 | 95ab4de9 | David Knowles | |
933 | a198b2d9 | Michael Hanselmann | nodes = self._SendRequest(HTTP_GET, "/%s/nodes" % GANETI_RAPI_VERSION, |
934 | a198b2d9 | Michael Hanselmann | query, None)
|
935 | 95ab4de9 | David Knowles | if bulk:
|
936 | 95ab4de9 | David Knowles | return nodes
|
937 | 95ab4de9 | David Knowles | else:
|
938 | 95ab4de9 | David Knowles | return [n["id"] for n in nodes] |
939 | 95ab4de9 | David Knowles | |
940 | 591e5103 | Michael Hanselmann | def GetNode(self, node): |
941 | 95ab4de9 | David Knowles | """Gets information about a node.
|
942 | 95ab4de9 | David Knowles |
|
943 | 95ab4de9 | David Knowles | @type node: str
|
944 | 95ab4de9 | David Knowles | @param node: node whose info to return
|
945 | 95ab4de9 | David Knowles |
|
946 | 95ab4de9 | David Knowles | @rtype: dict
|
947 | 95ab4de9 | David Knowles | @return: info about the node
|
948 | 95ab4de9 | David Knowles |
|
949 | 95ab4de9 | David Knowles | """
|
950 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_GET, |
951 | a198b2d9 | Michael Hanselmann | "/%s/nodes/%s" % (GANETI_RAPI_VERSION, node),
|
952 | a198b2d9 | Michael Hanselmann | None, None) |
953 | 95ab4de9 | David Knowles | |
954 | 95ab4de9 | David Knowles | def EvacuateNode(self, node, iallocator=None, remote_node=None, |
955 | 941b9309 | Iustin Pop | dry_run=False, early_release=False): |
956 | 95ab4de9 | David Knowles | """Evacuates instances from a Ganeti node.
|
957 | 95ab4de9 | David Knowles |
|
958 | 95ab4de9 | David Knowles | @type node: str
|
959 | 95ab4de9 | David Knowles | @param node: node to evacuate
|
960 | 95ab4de9 | David Knowles | @type iallocator: str or None
|
961 | 95ab4de9 | David Knowles | @param iallocator: instance allocator to use
|
962 | 95ab4de9 | David Knowles | @type remote_node: str
|
963 | 95ab4de9 | David Knowles | @param remote_node: node to evaucate to
|
964 | 95ab4de9 | David Knowles | @type dry_run: bool
|
965 | 95ab4de9 | David Knowles | @param dry_run: whether to perform a dry run
|
966 | 941b9309 | Iustin Pop | @type early_release: bool
|
967 | 941b9309 | Iustin Pop | @param early_release: whether to enable parallelization
|
968 | 95ab4de9 | David Knowles |
|
969 | 941b9309 | Iustin Pop | @rtype: list
|
970 | 941b9309 | Iustin Pop | @return: list of (job ID, instance name, new secondary node); if
|
971 | 941b9309 | Iustin Pop | dry_run was specified, then the actual move jobs were not
|
972 | 941b9309 | Iustin Pop | submitted and the job IDs will be C{None}
|
973 | 95ab4de9 | David Knowles |
|
974 | 941b9309 | Iustin Pop | @raises GanetiApiError: if an iallocator and remote_node are both
|
975 | 941b9309 | Iustin Pop | specified
|
976 | 95ab4de9 | David Knowles |
|
977 | 95ab4de9 | David Knowles | """
|
978 | 95ab4de9 | David Knowles | if iallocator and remote_node: |
979 | cfc03c54 | Michael Hanselmann | raise GanetiApiError("Only one of iallocator or remote_node can be used") |
980 | 95ab4de9 | David Knowles | |
981 | cfc03c54 | Michael Hanselmann | query = [] |
982 | 95ab4de9 | David Knowles | if iallocator:
|
983 | 95ab4de9 | David Knowles | query.append(("iallocator", iallocator))
|
984 | 95ab4de9 | David Knowles | if remote_node:
|
985 | 95ab4de9 | David Knowles | query.append(("remote_node", remote_node))
|
986 | 95ab4de9 | David Knowles | if dry_run:
|
987 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
988 | 941b9309 | Iustin Pop | if early_release:
|
989 | 941b9309 | Iustin Pop | query.append(("early_release", 1)) |
990 | 95ab4de9 | David Knowles | |
991 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_POST, |
992 | a198b2d9 | Michael Hanselmann | ("/%s/nodes/%s/evacuate" %
|
993 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, node)), query, None)
|
994 | 95ab4de9 | David Knowles | |
995 | 95ab4de9 | David Knowles | def MigrateNode(self, node, live=True, dry_run=False): |
996 | 95ab4de9 | David Knowles | """Migrates all primary instances from a node.
|
997 | 95ab4de9 | David Knowles |
|
998 | 95ab4de9 | David Knowles | @type node: str
|
999 | 95ab4de9 | David Knowles | @param node: node to migrate
|
1000 | 95ab4de9 | David Knowles | @type live: bool
|
1001 | 95ab4de9 | David Knowles | @param live: whether to use live migration
|
1002 | 95ab4de9 | David Knowles | @type dry_run: bool
|
1003 | 95ab4de9 | David Knowles | @param dry_run: whether to perform a dry run
|
1004 | 95ab4de9 | David Knowles |
|
1005 | 95ab4de9 | David Knowles | @rtype: int
|
1006 | 95ab4de9 | David Knowles | @return: job id
|
1007 | 95ab4de9 | David Knowles |
|
1008 | 95ab4de9 | David Knowles | """
|
1009 | 95ab4de9 | David Knowles | query = [] |
1010 | 95ab4de9 | David Knowles | if live:
|
1011 | 95ab4de9 | David Knowles | query.append(("live", 1)) |
1012 | 95ab4de9 | David Knowles | if dry_run:
|
1013 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
1014 | 95ab4de9 | David Knowles | |
1015 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_POST, |
1016 | a198b2d9 | Michael Hanselmann | ("/%s/nodes/%s/migrate" %
|
1017 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, node)), query, None)
|
1018 | 95ab4de9 | David Knowles | |
1019 | 95ab4de9 | David Knowles | def GetNodeRole(self, node): |
1020 | 95ab4de9 | David Knowles | """Gets the current role for a node.
|
1021 | 95ab4de9 | David Knowles |
|
1022 | 95ab4de9 | David Knowles | @type node: str
|
1023 | 95ab4de9 | David Knowles | @param node: node whose role to return
|
1024 | 95ab4de9 | David Knowles |
|
1025 | 95ab4de9 | David Knowles | @rtype: str
|
1026 | 95ab4de9 | David Knowles | @return: the current role for a node
|
1027 | 95ab4de9 | David Knowles |
|
1028 | 95ab4de9 | David Knowles | """
|
1029 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_GET, |
1030 | a198b2d9 | Michael Hanselmann | ("/%s/nodes/%s/role" %
|
1031 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, node)), None, None) |
1032 | 95ab4de9 | David Knowles | |
1033 | 95ab4de9 | David Knowles | def SetNodeRole(self, node, role, force=False): |
1034 | 95ab4de9 | David Knowles | """Sets the role for a node.
|
1035 | 95ab4de9 | David Knowles |
|
1036 | 95ab4de9 | David Knowles | @type node: str
|
1037 | 95ab4de9 | David Knowles | @param node: the node whose role to set
|
1038 | 95ab4de9 | David Knowles | @type role: str
|
1039 | 95ab4de9 | David Knowles | @param role: the role to set for the node
|
1040 | 95ab4de9 | David Knowles | @type force: bool
|
1041 | 95ab4de9 | David Knowles | @param force: whether to force the role change
|
1042 | 95ab4de9 | David Knowles |
|
1043 | 95ab4de9 | David Knowles | @rtype: int
|
1044 | 95ab4de9 | David Knowles | @return: job id
|
1045 | 95ab4de9 | David Knowles |
|
1046 | 95ab4de9 | David Knowles | """
|
1047 | 1068639f | Michael Hanselmann | query = [ |
1048 | 1068639f | Michael Hanselmann | ("force", force),
|
1049 | 1068639f | Michael Hanselmann | ] |
1050 | cfc03c54 | Michael Hanselmann | |
1051 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_PUT, |
1052 | a198b2d9 | Michael Hanselmann | ("/%s/nodes/%s/role" %
|
1053 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, node)), query, role) |
1054 | 95ab4de9 | David Knowles | |
1055 | 95ab4de9 | David Knowles | def GetNodeStorageUnits(self, node, storage_type, output_fields): |
1056 | 95ab4de9 | David Knowles | """Gets the storage units for a node.
|
1057 | 95ab4de9 | David Knowles |
|
1058 | 95ab4de9 | David Knowles | @type node: str
|
1059 | 95ab4de9 | David Knowles | @param node: the node whose storage units to return
|
1060 | 95ab4de9 | David Knowles | @type storage_type: str
|
1061 | 95ab4de9 | David Knowles | @param storage_type: storage type whose units to return
|
1062 | 95ab4de9 | David Knowles | @type output_fields: str
|
1063 | 95ab4de9 | David Knowles | @param output_fields: storage type fields to return
|
1064 | 95ab4de9 | David Knowles |
|
1065 | 95ab4de9 | David Knowles | @rtype: int
|
1066 | 95ab4de9 | David Knowles | @return: job id where results can be retrieved
|
1067 | 95ab4de9 | David Knowles |
|
1068 | 95ab4de9 | David Knowles | """
|
1069 | cfc03c54 | Michael Hanselmann | query = [ |
1070 | cfc03c54 | Michael Hanselmann | ("storage_type", storage_type),
|
1071 | cfc03c54 | Michael Hanselmann | ("output_fields", output_fields),
|
1072 | cfc03c54 | Michael Hanselmann | ] |
1073 | 95ab4de9 | David Knowles | |
1074 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_GET, |
1075 | a198b2d9 | Michael Hanselmann | ("/%s/nodes/%s/storage" %
|
1076 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, node)), query, None)
|
1077 | 95ab4de9 | David Knowles | |
1078 | fde28316 | Michael Hanselmann | def ModifyNodeStorageUnits(self, node, storage_type, name, allocatable=None): |
1079 | 95ab4de9 | David Knowles | """Modifies parameters of storage units on the node.
|
1080 | 95ab4de9 | David Knowles |
|
1081 | 95ab4de9 | David Knowles | @type node: str
|
1082 | 95ab4de9 | David Knowles | @param node: node whose storage units to modify
|
1083 | 95ab4de9 | David Knowles | @type storage_type: str
|
1084 | 95ab4de9 | David Knowles | @param storage_type: storage type whose units to modify
|
1085 | 95ab4de9 | David Knowles | @type name: str
|
1086 | 95ab4de9 | David Knowles | @param name: name of the storage unit
|
1087 | fde28316 | Michael Hanselmann | @type allocatable: bool or None
|
1088 | fde28316 | Michael Hanselmann | @param allocatable: Whether to set the "allocatable" flag on the storage
|
1089 | fde28316 | Michael Hanselmann | unit (None=no modification, True=set, False=unset)
|
1090 | 95ab4de9 | David Knowles |
|
1091 | 95ab4de9 | David Knowles | @rtype: int
|
1092 | 95ab4de9 | David Knowles | @return: job id
|
1093 | 95ab4de9 | David Knowles |
|
1094 | 95ab4de9 | David Knowles | """
|
1095 | 95ab4de9 | David Knowles | query = [ |
1096 | cfc03c54 | Michael Hanselmann | ("storage_type", storage_type),
|
1097 | cfc03c54 | Michael Hanselmann | ("name", name),
|
1098 | cfc03c54 | Michael Hanselmann | ] |
1099 | cfc03c54 | Michael Hanselmann | |
1100 | fde28316 | Michael Hanselmann | if allocatable is not None: |
1101 | fde28316 | Michael Hanselmann | query.append(("allocatable", allocatable))
|
1102 | fde28316 | Michael Hanselmann | |
1103 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_PUT, |
1104 | a198b2d9 | Michael Hanselmann | ("/%s/nodes/%s/storage/modify" %
|
1105 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, node)), query, None)
|
1106 | 95ab4de9 | David Knowles | |
1107 | 95ab4de9 | David Knowles | def RepairNodeStorageUnits(self, node, storage_type, name): |
1108 | 95ab4de9 | David Knowles | """Repairs a storage unit on the node.
|
1109 | 95ab4de9 | David Knowles |
|
1110 | 95ab4de9 | David Knowles | @type node: str
|
1111 | 95ab4de9 | David Knowles | @param node: node whose storage units to repair
|
1112 | 95ab4de9 | David Knowles | @type storage_type: str
|
1113 | 95ab4de9 | David Knowles | @param storage_type: storage type to repair
|
1114 | 95ab4de9 | David Knowles | @type name: str
|
1115 | 95ab4de9 | David Knowles | @param name: name of the storage unit to repair
|
1116 | 95ab4de9 | David Knowles |
|
1117 | 95ab4de9 | David Knowles | @rtype: int
|
1118 | 95ab4de9 | David Knowles | @return: job id
|
1119 | 95ab4de9 | David Knowles |
|
1120 | 95ab4de9 | David Knowles | """
|
1121 | cfc03c54 | Michael Hanselmann | query = [ |
1122 | cfc03c54 | Michael Hanselmann | ("storage_type", storage_type),
|
1123 | cfc03c54 | Michael Hanselmann | ("name", name),
|
1124 | cfc03c54 | Michael Hanselmann | ] |
1125 | 95ab4de9 | David Knowles | |
1126 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_PUT, |
1127 | a198b2d9 | Michael Hanselmann | ("/%s/nodes/%s/storage/repair" %
|
1128 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, node)), query, None)
|
1129 | 95ab4de9 | David Knowles | |
1130 | 95ab4de9 | David Knowles | def GetNodeTags(self, node): |
1131 | 95ab4de9 | David Knowles | """Gets the tags for a node.
|
1132 | 95ab4de9 | David Knowles |
|
1133 | 95ab4de9 | David Knowles | @type node: str
|
1134 | 95ab4de9 | David Knowles | @param node: node whose tags to return
|
1135 | 95ab4de9 | David Knowles |
|
1136 | 95ab4de9 | David Knowles | @rtype: list of str
|
1137 | 95ab4de9 | David Knowles | @return: tags for the node
|
1138 | 95ab4de9 | David Knowles |
|
1139 | 95ab4de9 | David Knowles | """
|
1140 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_GET, |
1141 | a198b2d9 | Michael Hanselmann | ("/%s/nodes/%s/tags" %
|
1142 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, node)), None, None) |
1143 | 95ab4de9 | David Knowles | |
1144 | 95ab4de9 | David Knowles | def AddNodeTags(self, node, tags, dry_run=False): |
1145 | 95ab4de9 | David Knowles | """Adds tags to a node.
|
1146 | 95ab4de9 | David Knowles |
|
1147 | 95ab4de9 | David Knowles | @type node: str
|
1148 | 95ab4de9 | David Knowles | @param node: node to add tags to
|
1149 | 95ab4de9 | David Knowles | @type tags: list of str
|
1150 | 95ab4de9 | David Knowles | @param tags: tags to add to the node
|
1151 | 95ab4de9 | David Knowles | @type dry_run: bool
|
1152 | 95ab4de9 | David Knowles | @param dry_run: whether to perform a dry run
|
1153 | 95ab4de9 | David Knowles |
|
1154 | 95ab4de9 | David Knowles | @rtype: int
|
1155 | 95ab4de9 | David Knowles | @return: job id
|
1156 | 95ab4de9 | David Knowles |
|
1157 | 95ab4de9 | David Knowles | """
|
1158 | 95ab4de9 | David Knowles | query = [("tag", t) for t in tags] |
1159 | 95ab4de9 | David Knowles | if dry_run:
|
1160 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
1161 | 95ab4de9 | David Knowles | |
1162 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_PUT, |
1163 | a198b2d9 | Michael Hanselmann | ("/%s/nodes/%s/tags" %
|
1164 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, node)), query, tags) |
1165 | 95ab4de9 | David Knowles | |
1166 | 95ab4de9 | David Knowles | def DeleteNodeTags(self, node, tags, dry_run=False): |
1167 | 95ab4de9 | David Knowles | """Delete tags from a node.
|
1168 | 95ab4de9 | David Knowles |
|
1169 | 95ab4de9 | David Knowles | @type node: str
|
1170 | 95ab4de9 | David Knowles | @param node: node to remove tags from
|
1171 | 95ab4de9 | David Knowles | @type tags: list of str
|
1172 | 95ab4de9 | David Knowles | @param tags: tags to remove from the node
|
1173 | 95ab4de9 | David Knowles | @type dry_run: bool
|
1174 | 95ab4de9 | David Knowles | @param dry_run: whether to perform a dry run
|
1175 | 95ab4de9 | David Knowles |
|
1176 | 95ab4de9 | David Knowles | @rtype: int
|
1177 | 95ab4de9 | David Knowles | @return: job id
|
1178 | 95ab4de9 | David Knowles |
|
1179 | 95ab4de9 | David Knowles | """
|
1180 | 95ab4de9 | David Knowles | query = [("tag", t) for t in tags] |
1181 | 95ab4de9 | David Knowles | if dry_run:
|
1182 | 95ab4de9 | David Knowles | query.append(("dry-run", 1)) |
1183 | 95ab4de9 | David Knowles | |
1184 | a198b2d9 | Michael Hanselmann | return self._SendRequest(HTTP_DELETE, |
1185 | a198b2d9 | Michael Hanselmann | ("/%s/nodes/%s/tags" %
|
1186 | a198b2d9 | Michael Hanselmann | (GANETI_RAPI_VERSION, node)), query, None) |