root / lib / luxi.py @ fc6ccde4
History | View | Annotate | Download (7.2 kB)
1 | c2a03789 | Iustin Pop | #
|
---|---|---|---|
2 | c2a03789 | Iustin Pop | #
|
3 | c2a03789 | Iustin Pop | |
4 | ff1012ef | Petr Pudlak | # Copyright (C) 2006, 2007, 2011, 2012, 2013 Google Inc.
|
5 | c2a03789 | Iustin Pop | #
|
6 | c2a03789 | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | c2a03789 | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | c2a03789 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | c2a03789 | Iustin Pop | # (at your option) any later version.
|
10 | c2a03789 | Iustin Pop | #
|
11 | c2a03789 | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | c2a03789 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | c2a03789 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | c2a03789 | Iustin Pop | # General Public License for more details.
|
15 | c2a03789 | Iustin Pop | #
|
16 | c2a03789 | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | c2a03789 | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | c2a03789 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | c2a03789 | Iustin Pop | # 02110-1301, USA.
|
20 | c2a03789 | Iustin Pop | |
21 | c2a03789 | Iustin Pop | |
22 | ff1012ef | Petr Pudlak | """Module for the LUXI protocol
|
23 | c2a03789 | Iustin Pop |
|
24 | 8d5b316c | Iustin Pop | This module implements the local unix socket protocol. You only need
|
25 | c2a03789 | Iustin Pop | this module and the opcodes module in the client program in order to
|
26 | c2a03789 | Iustin Pop | communicate with the master.
|
27 | c2a03789 | Iustin Pop |
|
28 | 7577196d | Guido Trotter | The module is also used by the master daemon.
|
29 | c2a03789 | Iustin Pop |
|
30 | c2a03789 | Iustin Pop | """
|
31 | c2a03789 | Iustin Pop | |
32 | ceab32dd | Iustin Pop | from ganeti import constants |
33 | 28b71a76 | Michael Hanselmann | from ganeti import objects |
34 | 912b2278 | Petr Pudlak | import ganeti.rpc.client as cl |
35 | 912b2278 | Petr Pudlak | from ganeti.rpc.transport import Transport |
36 | ff1012ef | Petr Pudlak | |
37 | ff1012ef | Petr Pudlak | __all__ = [ |
38 | ff1012ef | Petr Pudlak | # classes:
|
39 | ff1012ef | Petr Pudlak | "Client"
|
40 | ff1012ef | Petr Pudlak | ] |
41 | c2a03789 | Iustin Pop | |
42 | fbb05686 | Jose A. Lopes | REQ_SUBMIT_JOB = constants.LUXI_REQ_SUBMIT_JOB |
43 | fbb05686 | Jose A. Lopes | REQ_SUBMIT_JOB_TO_DRAINED_QUEUE = constants.LUXI_REQ_SUBMIT_JOB_TO_DRAINED_QUEUE |
44 | fbb05686 | Jose A. Lopes | REQ_SUBMIT_MANY_JOBS = constants.LUXI_REQ_SUBMIT_MANY_JOBS |
45 | a4417db4 | Thomas Thrainer | REQ_PICKUP_JOB = constants.LUXI_REQ_PICKUP_JOB |
46 | fbb05686 | Jose A. Lopes | REQ_WAIT_FOR_JOB_CHANGE = constants.LUXI_REQ_WAIT_FOR_JOB_CHANGE |
47 | fbb05686 | Jose A. Lopes | REQ_CANCEL_JOB = constants.LUXI_REQ_CANCEL_JOB |
48 | fbb05686 | Jose A. Lopes | REQ_ARCHIVE_JOB = constants.LUXI_REQ_ARCHIVE_JOB |
49 | fbb05686 | Jose A. Lopes | REQ_CHANGE_JOB_PRIORITY = constants.LUXI_REQ_CHANGE_JOB_PRIORITY |
50 | fbb05686 | Jose A. Lopes | REQ_AUTO_ARCHIVE_JOBS = constants.LUXI_REQ_AUTO_ARCHIVE_JOBS |
51 | fbb05686 | Jose A. Lopes | REQ_QUERY = constants.LUXI_REQ_QUERY |
52 | fbb05686 | Jose A. Lopes | REQ_QUERY_FIELDS = constants.LUXI_REQ_QUERY_FIELDS |
53 | fbb05686 | Jose A. Lopes | REQ_QUERY_JOBS = constants.LUXI_REQ_QUERY_JOBS |
54 | fbb05686 | Jose A. Lopes | REQ_QUERY_INSTANCES = constants.LUXI_REQ_QUERY_INSTANCES |
55 | fbb05686 | Jose A. Lopes | REQ_QUERY_NODES = constants.LUXI_REQ_QUERY_NODES |
56 | fbb05686 | Jose A. Lopes | REQ_QUERY_GROUPS = constants.LUXI_REQ_QUERY_GROUPS |
57 | fbb05686 | Jose A. Lopes | REQ_QUERY_NETWORKS = constants.LUXI_REQ_QUERY_NETWORKS |
58 | fbb05686 | Jose A. Lopes | REQ_QUERY_EXPORTS = constants.LUXI_REQ_QUERY_EXPORTS |
59 | fbb05686 | Jose A. Lopes | REQ_QUERY_CONFIG_VALUES = constants.LUXI_REQ_QUERY_CONFIG_VALUES |
60 | fbb05686 | Jose A. Lopes | REQ_QUERY_CLUSTER_INFO = constants.LUXI_REQ_QUERY_CLUSTER_INFO |
61 | fbb05686 | Jose A. Lopes | REQ_QUERY_TAGS = constants.LUXI_REQ_QUERY_TAGS |
62 | fbb05686 | Jose A. Lopes | REQ_SET_DRAIN_FLAG = constants.LUXI_REQ_SET_DRAIN_FLAG |
63 | fbb05686 | Jose A. Lopes | REQ_SET_WATCHER_PAUSE = constants.LUXI_REQ_SET_WATCHER_PAUSE |
64 | fbb05686 | Jose A. Lopes | REQ_ALL = constants.LUXI_REQ_ALL |
65 | fbb05686 | Jose A. Lopes | |
66 | fbb05686 | Jose A. Lopes | DEF_RWTO = constants.LUXI_DEF_RWTO |
67 | fbb05686 | Jose A. Lopes | WFJC_TIMEOUT = constants.LUXI_WFJC_TIMEOUT |
68 | 793a8f7c | Michael Hanselmann | |
69 | c2a03789 | Iustin Pop | |
70 | 912b2278 | Petr Pudlak | class Client(cl.AbstractClient): |
71 | c2a03789 | Iustin Pop | """High-level client implementation.
|
72 | c2a03789 | Iustin Pop |
|
73 | c2a03789 | Iustin Pop | This uses a backing Transport-like class on top of which it
|
74 | c2a03789 | Iustin Pop | implements data serialization/deserialization.
|
75 | c2a03789 | Iustin Pop |
|
76 | c2a03789 | Iustin Pop | """
|
77 | 912b2278 | Petr Pudlak | def __init__(self, address=None, timeouts=None, transport=Transport): |
78 | c2a03789 | Iustin Pop | """Constructor for the Client class.
|
79 | c2a03789 | Iustin Pop |
|
80 | 912b2278 | Petr Pudlak | Arguments are the same as for L{AbstractClient}.
|
81 | 3d8548c4 | Michael Hanselmann |
|
82 | 231db3a5 | Michael Hanselmann | """
|
83 | 912b2278 | Petr Pudlak | super(Client, self).__init__(address, timeouts, transport) |
84 | cda215a9 | Petr Pudlak | # Override the version of the protocol:
|
85 | cda215a9 | Petr Pudlak | self.version = constants.LUXI_VERSION
|
86 | c2a03789 | Iustin Pop | |
87 | 3ccafd0e | Iustin Pop | def SetQueueDrainFlag(self, drain_flag): |
88 | 83c046a2 | Iustin Pop | return self.CallMethod(REQ_SET_DRAIN_FLAG, (drain_flag, )) |
89 | 3ccafd0e | Iustin Pop | |
90 | 05e50653 | Michael Hanselmann | def SetWatcherPause(self, until): |
91 | a629ecb9 | Iustin Pop | return self.CallMethod(REQ_SET_WATCHER_PAUSE, (until, )) |
92 | 05e50653 | Michael Hanselmann | |
93 | d9d1e541 | Klaus Aehlig | def PickupJob(self, job): |
94 | d9d1e541 | Klaus Aehlig | return self.CallMethod(REQ_PICKUP_JOB, (job,)) |
95 | d9d1e541 | Klaus Aehlig | |
96 | 0bbe448c | Michael Hanselmann | def SubmitJob(self, ops): |
97 | 0bbe448c | Michael Hanselmann | ops_state = map(lambda op: op.__getstate__(), ops) |
98 | 734a2a7c | René Nussbaumer | return self.CallMethod(REQ_SUBMIT_JOB, (ops_state, )) |
99 | 0bbe448c | Michael Hanselmann | |
100 | 346c3037 | Klaus Aehlig | def SubmitJobToDrainedQueue(self, ops): |
101 | 346c3037 | Klaus Aehlig | ops_state = map(lambda op: op.__getstate__(), ops) |
102 | 346c3037 | Klaus Aehlig | return self.CallMethod(REQ_SUBMIT_JOB_TO_DRAINED_QUEUE, (ops_state, )) |
103 | 346c3037 | Klaus Aehlig | |
104 | 2971c913 | Iustin Pop | def SubmitManyJobs(self, jobs): |
105 | 2971c913 | Iustin Pop | jobs_state = [] |
106 | 2971c913 | Iustin Pop | for ops in jobs: |
107 | 2971c913 | Iustin Pop | jobs_state.append([op.__getstate__() for op in ops]) |
108 | 734a2a7c | René Nussbaumer | return self.CallMethod(REQ_SUBMIT_MANY_JOBS, (jobs_state, )) |
109 | 2971c913 | Iustin Pop | |
110 | 0bbe448c | Michael Hanselmann | def CancelJob(self, job_id): |
111 | a629ecb9 | Iustin Pop | return self.CallMethod(REQ_CANCEL_JOB, (job_id, )) |
112 | 0bbe448c | Michael Hanselmann | |
113 | 0bbe448c | Michael Hanselmann | def ArchiveJob(self, job_id): |
114 | a629ecb9 | Iustin Pop | return self.CallMethod(REQ_ARCHIVE_JOB, (job_id, )) |
115 | 0bbe448c | Michael Hanselmann | |
116 | f63ffb37 | Michael Hanselmann | def ChangeJobPriority(self, job_id, priority): |
117 | f63ffb37 | Michael Hanselmann | return self.CallMethod(REQ_CHANGE_JOB_PRIORITY, (job_id, priority)) |
118 | f63ffb37 | Michael Hanselmann | |
119 | 07cd723a | Iustin Pop | def AutoArchiveJobs(self, age): |
120 | f8ad5591 | Michael Hanselmann | timeout = (DEF_RWTO - 1) / 2 |
121 | 83c046a2 | Iustin Pop | return self.CallMethod(REQ_AUTO_ARCHIVE_JOBS, (age, timeout)) |
122 | 07cd723a | Iustin Pop | |
123 | f4484122 | Michael Hanselmann | def WaitForJobChangeOnce(self, job_id, fields, |
124 | 793a8f7c | Michael Hanselmann | prev_job_info, prev_log_serial, |
125 | 793a8f7c | Michael Hanselmann | timeout=WFJC_TIMEOUT): |
126 | 793a8f7c | Michael Hanselmann | """Waits for changes on a job.
|
127 | 793a8f7c | Michael Hanselmann |
|
128 | 793a8f7c | Michael Hanselmann | @param job_id: Job ID
|
129 | 793a8f7c | Michael Hanselmann | @type fields: list
|
130 | 793a8f7c | Michael Hanselmann | @param fields: List of field names to be observed
|
131 | 793a8f7c | Michael Hanselmann | @type prev_job_info: None or list
|
132 | 793a8f7c | Michael Hanselmann | @param prev_job_info: Previously received job information
|
133 | 793a8f7c | Michael Hanselmann | @type prev_log_serial: None or int/long
|
134 | 793a8f7c | Michael Hanselmann | @param prev_log_serial: Highest log serial number previously received
|
135 | 793a8f7c | Michael Hanselmann | @type timeout: int/float
|
136 | 793a8f7c | Michael Hanselmann | @param timeout: Timeout in seconds (values larger than L{WFJC_TIMEOUT} will
|
137 | 793a8f7c | Michael Hanselmann | be capped to that value)
|
138 | 793a8f7c | Michael Hanselmann |
|
139 | 793a8f7c | Michael Hanselmann | """
|
140 | 793a8f7c | Michael Hanselmann | assert timeout >= 0, "Timeout can not be negative" |
141 | f4484122 | Michael Hanselmann | return self.CallMethod(REQ_WAIT_FOR_JOB_CHANGE, |
142 | f4484122 | Michael Hanselmann | (job_id, fields, prev_job_info, |
143 | 793a8f7c | Michael Hanselmann | prev_log_serial, |
144 | 793a8f7c | Michael Hanselmann | min(WFJC_TIMEOUT, timeout)))
|
145 | f4484122 | Michael Hanselmann | |
146 | f4484122 | Michael Hanselmann | def WaitForJobChange(self, job_id, fields, prev_job_info, prev_log_serial): |
147 | 5c735209 | Iustin Pop | while True: |
148 | f4484122 | Michael Hanselmann | result = self.WaitForJobChangeOnce(job_id, fields,
|
149 | f4484122 | Michael Hanselmann | prev_job_info, prev_log_serial) |
150 | 5c735209 | Iustin Pop | if result != constants.JOB_NOTCHANGED:
|
151 | 5c735209 | Iustin Pop | break
|
152 | 5c735209 | Iustin Pop | return result
|
153 | dfe57c22 | Michael Hanselmann | |
154 | 2e5c33db | Iustin Pop | def Query(self, what, fields, qfilter): |
155 | 28b71a76 | Michael Hanselmann | """Query for resources/items.
|
156 | 28b71a76 | Michael Hanselmann |
|
157 | abd66bf8 | Michael Hanselmann | @param what: One of L{constants.QR_VIA_LUXI}
|
158 | 28b71a76 | Michael Hanselmann | @type fields: List of strings
|
159 | 28b71a76 | Michael Hanselmann | @param fields: List of requested fields
|
160 | 2e5c33db | Iustin Pop | @type qfilter: None or list
|
161 | 2e5c33db | Iustin Pop | @param qfilter: Query filter
|
162 | 28b71a76 | Michael Hanselmann | @rtype: L{objects.QueryResponse}
|
163 | 28b71a76 | Michael Hanselmann |
|
164 | 28b71a76 | Michael Hanselmann | """
|
165 | a629ecb9 | Iustin Pop | result = self.CallMethod(REQ_QUERY, (what, fields, qfilter))
|
166 | 28b71a76 | Michael Hanselmann | return objects.QueryResponse.FromDict(result)
|
167 | 28b71a76 | Michael Hanselmann | |
168 | 28b71a76 | Michael Hanselmann | def QueryFields(self, what, fields): |
169 | 28b71a76 | Michael Hanselmann | """Query for available fields.
|
170 | 28b71a76 | Michael Hanselmann |
|
171 | abd66bf8 | Michael Hanselmann | @param what: One of L{constants.QR_VIA_LUXI}
|
172 | 28b71a76 | Michael Hanselmann | @type fields: None or list of strings
|
173 | 28b71a76 | Michael Hanselmann | @param fields: List of requested fields
|
174 | 28b71a76 | Michael Hanselmann | @rtype: L{objects.QueryFieldsResponse}
|
175 | 28b71a76 | Michael Hanselmann |
|
176 | 28b71a76 | Michael Hanselmann | """
|
177 | a629ecb9 | Iustin Pop | result = self.CallMethod(REQ_QUERY_FIELDS, (what, fields))
|
178 | 28b71a76 | Michael Hanselmann | return objects.QueryFieldsResponse.FromDict(result)
|
179 | 28b71a76 | Michael Hanselmann | |
180 | 0bbe448c | Michael Hanselmann | def QueryJobs(self, job_ids, fields): |
181 | 0bbe448c | Michael Hanselmann | return self.CallMethod(REQ_QUERY_JOBS, (job_ids, fields)) |
182 | 3d8548c4 | Michael Hanselmann | |
183 | ec79568d | Iustin Pop | def QueryInstances(self, names, fields, use_locking): |
184 | ec79568d | Iustin Pop | return self.CallMethod(REQ_QUERY_INSTANCES, (names, fields, use_locking)) |
185 | ee6c7b94 | Michael Hanselmann | |
186 | ec79568d | Iustin Pop | def QueryNodes(self, names, fields, use_locking): |
187 | ec79568d | Iustin Pop | return self.CallMethod(REQ_QUERY_NODES, (names, fields, use_locking)) |
188 | 02f7fe54 | Michael Hanselmann | |
189 | a79ef2a5 | Adeodato Simo | def QueryGroups(self, names, fields, use_locking): |
190 | a79ef2a5 | Adeodato Simo | return self.CallMethod(REQ_QUERY_GROUPS, (names, fields, use_locking)) |
191 | a79ef2a5 | Adeodato Simo | |
192 | 306bed0e | Apollon Oikonomopoulos | def QueryNetworks(self, names, fields, use_locking): |
193 | 306bed0e | Apollon Oikonomopoulos | return self.CallMethod(REQ_QUERY_NETWORKS, (names, fields, use_locking)) |
194 | 306bed0e | Apollon Oikonomopoulos | |
195 | ec79568d | Iustin Pop | def QueryExports(self, nodes, use_locking): |
196 | ec79568d | Iustin Pop | return self.CallMethod(REQ_QUERY_EXPORTS, (nodes, use_locking)) |
197 | 32f93223 | Michael Hanselmann | |
198 | 66baeccc | Iustin Pop | def QueryClusterInfo(self): |
199 | 66baeccc | Iustin Pop | return self.CallMethod(REQ_QUERY_CLUSTER_INFO, ()) |
200 | 66baeccc | Iustin Pop | |
201 | ae5849b5 | Michael Hanselmann | def QueryConfigValues(self, fields): |
202 | a629ecb9 | Iustin Pop | return self.CallMethod(REQ_QUERY_CONFIG_VALUES, (fields, )) |
203 | ae5849b5 | Michael Hanselmann | |
204 | 7699c3af | Iustin Pop | def QueryTags(self, kind, name): |
205 | 7699c3af | Iustin Pop | return self.CallMethod(REQ_QUERY_TAGS, (kind, name)) |