Statistics
| Branch: | Tag: | Revision:

root / lib / cli.py @ f8a4adfa

History | View | Annotate | Download (102.7 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 18009c1e Iustin Pop
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Google Inc.
5 a8083063 Iustin Pop
#
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
#
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
#
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 a8083063 Iustin Pop
"""Module dealing with command line parsing"""
23 a8083063 Iustin Pop
24 a8083063 Iustin Pop
25 a8083063 Iustin Pop
import sys
26 a8083063 Iustin Pop
import textwrap
27 a8083063 Iustin Pop
import os.path
28 685ee993 Iustin Pop
import time
29 46fbdd04 Iustin Pop
import logging
30 225e2544 Iustin Pop
import errno
31 60452edf Michael Hanselmann
import itertools
32 73702ee7 Iustin Pop
from cStringIO import StringIO
33 a8083063 Iustin Pop
34 a8083063 Iustin Pop
from ganeti import utils
35 a8083063 Iustin Pop
from ganeti import errors
36 a8083063 Iustin Pop
from ganeti import constants
37 846baef9 Iustin Pop
from ganeti import opcodes
38 ceab32dd Iustin Pop
from ganeti import luxi
39 b33e986b Iustin Pop
from ganeti import ssconf
40 4331f6cd Michael Hanselmann
from ganeti import rpc
41 7e49b6ce Michael Hanselmann
from ganeti import ssh
42 cea881e5 Michael Hanselmann
from ganeti import compat
43 a744b676 Manuel Franceschini
from ganeti import netutils
44 ee3aedff Michael Hanselmann
from ganeti import qlang
45 a8083063 Iustin Pop
46 c38c44ad Michael Hanselmann
from optparse import (OptionParser, TitledHelpFormatter,
47 38206f3c Iustin Pop
                      Option, OptionValueError)
48 a8083063 Iustin Pop
49 03298ebe Michael Hanselmann
50 4abc4f1e Iustin Pop
__all__ = [
51 4abc4f1e Iustin Pop
  # Command line options
52 fdad8c4d Balazs Lecz
  "ADD_UIDS_OPT",
53 e7e09483 Iustin Pop
  "ALLOCATABLE_OPT",
54 90e99856 Adeodato Simo
  "ALLOC_POLICY_OPT",
55 2d5e7ae1 Iustin Pop
  "ALL_OPT",
56 e9c487be René Nussbaumer
  "ALLOW_FAILOVER_OPT",
57 4c61d894 Iustin Pop
  "AUTO_PROMOTE_OPT",
58 e00f7a05 Iustin Pop
  "AUTO_REPLACE_OPT",
59 087ed2ed Iustin Pop
  "BACKEND_OPT",
60 61a14bb3 Iustin Pop
  "BLK_OS_OPT",
61 f91e255a Iustin Pop
  "CAPAB_MASTER_OPT",
62 53919782 Iustin Pop
  "CAPAB_VM_OPT",
63 baef337d Iustin Pop
  "CLEANUP_OPT",
64 3db3eb2a Michael Hanselmann
  "CLUSTER_DOMAIN_SECRET_OPT",
65 4abc4f1e Iustin Pop
  "CONFIRM_OPT",
66 e32df528 Iustin Pop
  "CP_SIZE_OPT",
67 4abc4f1e Iustin Pop
  "DEBUG_OPT",
68 a0c9776a Iustin Pop
  "DEBUG_SIMERR_OPT",
69 4b038a1e Iustin Pop
  "DISKIDX_OPT",
70 e3876ccb Iustin Pop
  "DISK_OPT",
71 4b038a1e Iustin Pop
  "DISK_TEMPLATE_OPT",
72 771734c9 Iustin Pop
  "DRAINED_OPT",
73 a0a6ff34 Iustin Pop
  "DRY_RUN_OPT",
74 26591bfd Luca Bigliardi
  "DRBD_HELPER_OPT",
75 1b7761fd Apollon Oikonomopoulos
  "DST_NODE_OPT",
76 7ea7bcf6 Iustin Pop
  "EARLY_RELEASE_OPT",
77 383a3591 Iustin Pop
  "ENABLED_HV_OPT",
78 14e9e7f3 Iustin Pop
  "ERROR_CODES_OPT",
79 4abc4f1e Iustin Pop
  "FIELDS_OPT",
80 4a25828c Iustin Pop
  "FILESTORE_DIR_OPT",
81 0f87c43e Iustin Pop
  "FILESTORE_DRIVER_OPT",
82 442587bf Michael Hanselmann
  "FORCE_FILTER_OPT",
83 06073e85 Guido Trotter
  "FORCE_OPT",
84 06073e85 Guido Trotter
  "FORCE_VARIANT_OPT",
85 29392516 Iustin Pop
  "GLOBAL_FILEDIR_OPT",
86 61a14bb3 Iustin Pop
  "HID_OS_OPT",
87 4b97f902 Apollon Oikonomopoulos
  "GLOBAL_SHARED_FILEDIR_OPT",
88 073271f6 Iustin Pop
  "HVLIST_OPT",
89 48f212d7 Iustin Pop
  "HVOPTS_OPT",
90 236fd9c4 Iustin Pop
  "HYPERVISOR_OPT",
91 4eb62659 Iustin Pop
  "IALLOCATOR_OPT",
92 bf4af505 Apollon Oikonomopoulos
  "DEFAULT_IALLOCATOR_OPT",
93 e588764d Iustin Pop
  "IDENTIFY_DEFAULTS_OPT",
94 82a786d5 Iustin Pop
  "IGNORE_CONSIST_OPT",
95 b6e841a8 Iustin Pop
  "IGNORE_FAILURES_OPT",
96 b44bd844 Michael Hanselmann
  "IGNORE_OFFLINE_OPT",
97 8d8d650c Michael Hanselmann
  "IGNORE_REMOVE_FAILURES_OPT",
98 ee3f9578 Iustin Pop
  "IGNORE_SECONDARIES_OPT",
99 05586c90 Iustin Pop
  "IGNORE_SIZE_OPT",
100 19b9ba9a Michael Hanselmann
  "INTERVAL_OPT",
101 e3646f22 Iustin Pop
  "MAC_PREFIX_OPT",
102 3953242f Iustin Pop
  "MAINTAIN_NODE_HEALTH_OPT",
103 29392516 Iustin Pop
  "MASTER_NETDEV_OPT",
104 771734c9 Iustin Pop
  "MC_OPT",
105 783a6c0b Iustin Pop
  "MIGRATION_MODE_OPT",
106 7d3a9fab Iustin Pop
  "NET_OPT",
107 6d4a1656 Michael Hanselmann
  "NEW_CLUSTER_CERT_OPT",
108 3db3eb2a Michael Hanselmann
  "NEW_CLUSTER_DOMAIN_SECRET_OPT",
109 6b7d5878 Michael Hanselmann
  "NEW_CONFD_HMAC_KEY_OPT",
110 6d4a1656 Michael Hanselmann
  "NEW_RAPI_CERT_OPT",
111 a14db5ff Iustin Pop
  "NEW_SECONDARY_OPT",
112 4fbc93dd Iustin Pop
  "NIC_PARAMS_OPT",
113 61413377 Stephen Shirley
  "NODE_FORCE_JOIN_OPT",
114 7edc4637 Iustin Pop
  "NODE_LIST_OPT",
115 990b7886 Iustin Pop
  "NODE_PLACEMENT_OPT",
116 5fbbd028 Guido Trotter
  "NODEGROUP_OPT",
117 a535cef7 René Nussbaumer
  "NODE_PARAMS_OPT",
118 dd94e9f6 René Nussbaumer
  "NODE_POWERED_OPT",
119 26591bfd Luca Bigliardi
  "NODRBD_STORAGE_OPT",
120 4abc4f1e Iustin Pop
  "NOHDR_OPT",
121 91e0748c Iustin Pop
  "NOIPCHECK_OPT",
122 25a8792c Iustin Pop
  "NO_INSTALL_OPT",
123 460d22be Iustin Pop
  "NONAMECHECK_OPT",
124 831040bf Iustin Pop
  "NOLVM_STORAGE_OPT",
125 29392516 Iustin Pop
  "NOMODIFY_ETCHOSTS_OPT",
126 b989b9d9 Ken Wehr
  "NOMODIFY_SSH_SETUP_OPT",
127 26023ecd Iustin Pop
  "NONICS_OPT",
128 f2a0828c Iustin Pop
  "NONLIVE_OPT",
129 14e9e7f3 Iustin Pop
  "NONPLUS1_OPT",
130 44c44832 Iustin Pop
  "NOSHUTDOWN_OPT",
131 edeb878a Iustin Pop
  "NOSTART_OPT",
132 fcdde7f2 Iustin Pop
  "NOSSH_KEYCHECK_OPT",
133 58371861 Iustin Pop
  "NOVOTING_OPT",
134 885a0fc4 Iustin Pop
  "NO_REMEMBER_OPT",
135 3f75b4f3 Iustin Pop
  "NWSYNC_OPT",
136 a72d0a79 Iustin Pop
  "ON_PRIMARY_OPT",
137 feb09e6a Iustin Pop
  "ON_SECONDARY_OPT",
138 771734c9 Iustin Pop
  "OFFLINE_OPT",
139 062a7100 Iustin Pop
  "OSPARAMS_OPT",
140 d3ed23ff Iustin Pop
  "OS_OPT",
141 ff00c1a7 Iustin Pop
  "OS_SIZE_OPT",
142 41543d8b René Nussbaumer
  "OOB_TIMEOUT_OPT",
143 0c086a13 René Nussbaumer
  "POWER_DELAY_OPT",
144 b883637f René Nussbaumer
  "PREALLOC_WIPE_DISKS_OPT",
145 e7323b5e Manuel Franceschini
  "PRIMARY_IP_VERSION_OPT",
146 aafee533 Michael Hanselmann
  "PRIMARY_ONLY_OPT",
147 84a5b33c Michael Hanselmann
  "PRIORITY_OPT",
148 6d4a1656 Michael Hanselmann
  "RAPI_CERT_OPT",
149 b8d0f938 Iustin Pop
  "READD_OPT",
150 12054861 Iustin Pop
  "REBOOT_TYPE_OPT",
151 8d8d650c Michael Hanselmann
  "REMOVE_INSTANCE_OPT",
152 fdad8c4d Balazs Lecz
  "REMOVE_UIDS_OPT",
153 f38ea602 Iustin Pop
  "RESERVED_LVS_OPT",
154 31d97b2a Guido Trotter
  "ROMAN_OPT",
155 8d823629 Iustin Pop
  "SECONDARY_IP_OPT",
156 aafee533 Michael Hanselmann
  "SECONDARY_ONLY_OPT",
157 67840b40 Iustin Pop
  "SELECT_OS_OPT",
158 4abc4f1e Iustin Pop
  "SEP_OPT",
159 fdcf4d84 Iustin Pop
  "SHOWCMD_OPT",
160 7e5eaaa8 Guido Trotter
  "SHUTDOWN_TIMEOUT_OPT",
161 f36d7d81 Iustin Pop
  "SINGLE_NODE_OPT",
162 df62e5db Iustin Pop
  "SRC_DIR_OPT",
163 df62e5db Iustin Pop
  "SRC_NODE_OPT",
164 4abc4f1e Iustin Pop
  "SUBMIT_OPT",
165 323f9095 Stephen Shirley
  "STARTUP_PAUSED_OPT",
166 99a8c799 Iustin Pop
  "STATIC_OPT",
167 4abc4f1e Iustin Pop
  "SYNC_OPT",
168 a57981c5 Apollon Oikonomopoulos
  "TAG_ADD_OPT",
169 4abc4f1e Iustin Pop
  "TAG_SRC_OPT",
170 b5762e2a Guido Trotter
  "TIMEOUT_OPT",
171 1338f2b4 Balazs Lecz
  "UIDPOOL_OPT",
172 4abc4f1e Iustin Pop
  "USEUNITS_OPT",
173 74adc100 Iustin Pop
  "USE_REPL_NET_OPT",
174 9cdb9578 Iustin Pop
  "VERBOSE_OPT",
175 b58726e8 Iustin Pop
  "VG_NAME_OPT",
176 1f587d3d Iustin Pop
  "YES_DOIT_OPT",
177 4abc4f1e Iustin Pop
  # Generic functions for CLI programs
178 25bd815c René Nussbaumer
  "ConfirmOperation",
179 4abc4f1e Iustin Pop
  "GenericMain",
180 d77490c5 Iustin Pop
  "GenericInstanceCreate",
181 ee3aedff Michael Hanselmann
  "GenericList",
182 ee3aedff Michael Hanselmann
  "GenericListFields",
183 4abc4f1e Iustin Pop
  "GetClient",
184 4abc4f1e Iustin Pop
  "GetOnlineNodes",
185 4abc4f1e Iustin Pop
  "JobExecutor",
186 4abc4f1e Iustin Pop
  "JobSubmittedException",
187 4abc4f1e Iustin Pop
  "ParseTimespec",
188 7e49b6ce Michael Hanselmann
  "RunWhileClusterStopped",
189 4abc4f1e Iustin Pop
  "SubmitOpCode",
190 4abc4f1e Iustin Pop
  "SubmitOrSend",
191 4abc4f1e Iustin Pop
  "UsesRPC",
192 4abc4f1e Iustin Pop
  # Formatting functions
193 4abc4f1e Iustin Pop
  "ToStderr", "ToStdout",
194 4abc4f1e Iustin Pop
  "FormatError",
195 ee3aedff Michael Hanselmann
  "FormatQueryResult",
196 acd19189 René Nussbaumer
  "FormatParameterDict",
197 4abc4f1e Iustin Pop
  "GenerateTable",
198 4abc4f1e Iustin Pop
  "AskUser",
199 4abc4f1e Iustin Pop
  "FormatTimestamp",
200 8a7f1c61 Michael Hanselmann
  "FormatLogMessage",
201 4abc4f1e Iustin Pop
  # Tags functions
202 4abc4f1e Iustin Pop
  "ListTags",
203 4abc4f1e Iustin Pop
  "AddTags",
204 4abc4f1e Iustin Pop
  "RemoveTags",
205 4abc4f1e Iustin Pop
  # command line options support infrastructure
206 4abc4f1e Iustin Pop
  "ARGS_MANY_INSTANCES",
207 4abc4f1e Iustin Pop
  "ARGS_MANY_NODES",
208 667dbd6b Adeodato Simo
  "ARGS_MANY_GROUPS",
209 4abc4f1e Iustin Pop
  "ARGS_NONE",
210 4abc4f1e Iustin Pop
  "ARGS_ONE_INSTANCE",
211 4abc4f1e Iustin Pop
  "ARGS_ONE_NODE",
212 667dbd6b Adeodato Simo
  "ARGS_ONE_GROUP",
213 f9faf9c3 René Nussbaumer
  "ARGS_ONE_OS",
214 4abc4f1e Iustin Pop
  "ArgChoice",
215 4abc4f1e Iustin Pop
  "ArgCommand",
216 4abc4f1e Iustin Pop
  "ArgFile",
217 667dbd6b Adeodato Simo
  "ArgGroup",
218 4abc4f1e Iustin Pop
  "ArgHost",
219 4abc4f1e Iustin Pop
  "ArgInstance",
220 4abc4f1e Iustin Pop
  "ArgJobId",
221 4abc4f1e Iustin Pop
  "ArgNode",
222 f9faf9c3 René Nussbaumer
  "ArgOs",
223 4abc4f1e Iustin Pop
  "ArgSuggest",
224 4abc4f1e Iustin Pop
  "ArgUnknown",
225 4abc4f1e Iustin Pop
  "OPT_COMPL_INST_ADD_NODES",
226 4abc4f1e Iustin Pop
  "OPT_COMPL_MANY_NODES",
227 4abc4f1e Iustin Pop
  "OPT_COMPL_ONE_IALLOCATOR",
228 4abc4f1e Iustin Pop
  "OPT_COMPL_ONE_INSTANCE",
229 4abc4f1e Iustin Pop
  "OPT_COMPL_ONE_NODE",
230 36e247e1 Guido Trotter
  "OPT_COMPL_ONE_NODEGROUP",
231 4abc4f1e Iustin Pop
  "OPT_COMPL_ONE_OS",
232 4abc4f1e Iustin Pop
  "cli_option",
233 4abc4f1e Iustin Pop
  "SplitNodeOption",
234 07150497 Guido Trotter
  "CalculateOSNames",
235 a4ebd726 Michael Hanselmann
  "ParseFields",
236 eb28ecf6 Guido Trotter
  "COMMON_CREATE_OPTS",
237 4abc4f1e Iustin Pop
  ]
238 846baef9 Iustin Pop
239 8b46606c Guido Trotter
NO_PREFIX = "no_"
240 8b46606c Guido Trotter
UN_PREFIX = "-"
241 846baef9 Iustin Pop
242 84a5b33c Michael Hanselmann
#: Priorities (sorted)
243 84a5b33c Michael Hanselmann
_PRIORITY_NAMES = [
244 84a5b33c Michael Hanselmann
  ("low", constants.OP_PRIO_LOW),
245 84a5b33c Michael Hanselmann
  ("normal", constants.OP_PRIO_NORMAL),
246 84a5b33c Michael Hanselmann
  ("high", constants.OP_PRIO_HIGH),
247 84a5b33c Michael Hanselmann
  ]
248 84a5b33c Michael Hanselmann
249 84a5b33c Michael Hanselmann
#: Priority dictionary for easier lookup
250 84a5b33c Michael Hanselmann
# TODO: Replace this and _PRIORITY_NAMES with a single sorted dictionary once
251 84a5b33c Michael Hanselmann
# we migrate to Python 2.6
252 84a5b33c Michael Hanselmann
_PRIONAME_TO_VALUE = dict(_PRIORITY_NAMES)
253 84a5b33c Michael Hanselmann
254 ee3aedff Michael Hanselmann
# Query result status for clients
255 ee3aedff Michael Hanselmann
(QR_NORMAL,
256 ee3aedff Michael Hanselmann
 QR_UNKNOWN,
257 ee3aedff Michael Hanselmann
 QR_INCOMPLETE) = range(3)
258 ee3aedff Michael Hanselmann
259 03298ebe Michael Hanselmann
260 863d7f46 Michael Hanselmann
class _Argument:
261 7260cfbe Iustin Pop
  def __init__(self, min=0, max=None): # pylint: disable-msg=W0622
262 863d7f46 Michael Hanselmann
    self.min = min
263 863d7f46 Michael Hanselmann
    self.max = max
264 863d7f46 Michael Hanselmann
265 863d7f46 Michael Hanselmann
  def __repr__(self):
266 863d7f46 Michael Hanselmann
    return ("<%s min=%s max=%s>" %
267 863d7f46 Michael Hanselmann
            (self.__class__.__name__, self.min, self.max))
268 863d7f46 Michael Hanselmann
269 863d7f46 Michael Hanselmann
270 863d7f46 Michael Hanselmann
class ArgSuggest(_Argument):
271 863d7f46 Michael Hanselmann
  """Suggesting argument.
272 863d7f46 Michael Hanselmann

273 863d7f46 Michael Hanselmann
  Value can be any of the ones passed to the constructor.
274 863d7f46 Michael Hanselmann

275 863d7f46 Michael Hanselmann
  """
276 7260cfbe Iustin Pop
  # pylint: disable-msg=W0622
277 863d7f46 Michael Hanselmann
  def __init__(self, min=0, max=None, choices=None):
278 863d7f46 Michael Hanselmann
    _Argument.__init__(self, min=min, max=max)
279 863d7f46 Michael Hanselmann
    self.choices = choices
280 863d7f46 Michael Hanselmann
281 863d7f46 Michael Hanselmann
  def __repr__(self):
282 863d7f46 Michael Hanselmann
    return ("<%s min=%s max=%s choices=%r>" %
283 863d7f46 Michael Hanselmann
            (self.__class__.__name__, self.min, self.max, self.choices))
284 863d7f46 Michael Hanselmann
285 863d7f46 Michael Hanselmann
286 863d7f46 Michael Hanselmann
class ArgChoice(ArgSuggest):
287 863d7f46 Michael Hanselmann
  """Choice argument.
288 863d7f46 Michael Hanselmann

289 863d7f46 Michael Hanselmann
  Value can be any of the ones passed to the constructor. Like L{ArgSuggest},
290 863d7f46 Michael Hanselmann
  but value must be one of the choices.
291 863d7f46 Michael Hanselmann

292 863d7f46 Michael Hanselmann
  """
293 863d7f46 Michael Hanselmann
294 863d7f46 Michael Hanselmann
295 863d7f46 Michael Hanselmann
class ArgUnknown(_Argument):
296 863d7f46 Michael Hanselmann
  """Unknown argument to program (e.g. determined at runtime).
297 863d7f46 Michael Hanselmann

298 863d7f46 Michael Hanselmann
  """
299 863d7f46 Michael Hanselmann
300 863d7f46 Michael Hanselmann
301 863d7f46 Michael Hanselmann
class ArgInstance(_Argument):
302 863d7f46 Michael Hanselmann
  """Instances argument.
303 863d7f46 Michael Hanselmann

304 863d7f46 Michael Hanselmann
  """
305 863d7f46 Michael Hanselmann
306 863d7f46 Michael Hanselmann
307 863d7f46 Michael Hanselmann
class ArgNode(_Argument):
308 863d7f46 Michael Hanselmann
  """Node argument.
309 863d7f46 Michael Hanselmann

310 863d7f46 Michael Hanselmann
  """
311 863d7f46 Michael Hanselmann
312 667dbd6b Adeodato Simo
313 667dbd6b Adeodato Simo
class ArgGroup(_Argument):
314 667dbd6b Adeodato Simo
  """Node group argument.
315 667dbd6b Adeodato Simo

316 667dbd6b Adeodato Simo
  """
317 667dbd6b Adeodato Simo
318 667dbd6b Adeodato Simo
319 863d7f46 Michael Hanselmann
class ArgJobId(_Argument):
320 863d7f46 Michael Hanselmann
  """Job ID argument.
321 863d7f46 Michael Hanselmann

322 863d7f46 Michael Hanselmann
  """
323 863d7f46 Michael Hanselmann
324 863d7f46 Michael Hanselmann
325 863d7f46 Michael Hanselmann
class ArgFile(_Argument):
326 863d7f46 Michael Hanselmann
  """File path argument.
327 863d7f46 Michael Hanselmann

328 863d7f46 Michael Hanselmann
  """
329 863d7f46 Michael Hanselmann
330 863d7f46 Michael Hanselmann
331 863d7f46 Michael Hanselmann
class ArgCommand(_Argument):
332 863d7f46 Michael Hanselmann
  """Command argument.
333 863d7f46 Michael Hanselmann

334 863d7f46 Michael Hanselmann
  """
335 863d7f46 Michael Hanselmann
336 863d7f46 Michael Hanselmann
337 83ec7961 Michael Hanselmann
class ArgHost(_Argument):
338 83ec7961 Michael Hanselmann
  """Host argument.
339 83ec7961 Michael Hanselmann

340 83ec7961 Michael Hanselmann
  """
341 83ec7961 Michael Hanselmann
342 83ec7961 Michael Hanselmann
343 f9faf9c3 René Nussbaumer
class ArgOs(_Argument):
344 f9faf9c3 René Nussbaumer
  """OS argument.
345 f9faf9c3 René Nussbaumer

346 f9faf9c3 René Nussbaumer
  """
347 f9faf9c3 René Nussbaumer
348 f9faf9c3 René Nussbaumer
349 4a265c08 Michael Hanselmann
ARGS_NONE = []
350 4a265c08 Michael Hanselmann
ARGS_MANY_INSTANCES = [ArgInstance()]
351 4a265c08 Michael Hanselmann
ARGS_MANY_NODES = [ArgNode()]
352 667dbd6b Adeodato Simo
ARGS_MANY_GROUPS = [ArgGroup()]
353 4a265c08 Michael Hanselmann
ARGS_ONE_INSTANCE = [ArgInstance(min=1, max=1)]
354 4a265c08 Michael Hanselmann
ARGS_ONE_NODE = [ArgNode(min=1, max=1)]
355 dadf6b7d Michael Hanselmann
# TODO
356 dadf6b7d Michael Hanselmann
ARGS_ONE_GROUP = [ArgGroup(min=1, max=1)]
357 f9faf9c3 René Nussbaumer
ARGS_ONE_OS = [ArgOs(min=1, max=1)]
358 4a265c08 Michael Hanselmann
359 4a265c08 Michael Hanselmann
360 846baef9 Iustin Pop
def _ExtractTagsObject(opts, args):
361 846baef9 Iustin Pop
  """Extract the tag type object.
362 846baef9 Iustin Pop

363 846baef9 Iustin Pop
  Note that this function will modify its args parameter.
364 846baef9 Iustin Pop

365 846baef9 Iustin Pop
  """
366 846baef9 Iustin Pop
  if not hasattr(opts, "tag_type"):
367 846baef9 Iustin Pop
    raise errors.ProgrammerError("tag_type not passed to _ExtractTagsObject")
368 846baef9 Iustin Pop
  kind = opts.tag_type
369 846baef9 Iustin Pop
  if kind == constants.TAG_CLUSTER:
370 846baef9 Iustin Pop
    retval = kind, kind
371 819cbfe5 Michael Hanselmann
  elif kind in (constants.TAG_NODEGROUP,
372 819cbfe5 Michael Hanselmann
                constants.TAG_NODE,
373 819cbfe5 Michael Hanselmann
                constants.TAG_INSTANCE):
374 846baef9 Iustin Pop
    if not args:
375 0c434948 Iustin Pop
      raise errors.OpPrereqError("no arguments passed to the command")
376 846baef9 Iustin Pop
    name = args.pop(0)
377 846baef9 Iustin Pop
    retval = kind, name
378 846baef9 Iustin Pop
  else:
379 846baef9 Iustin Pop
    raise errors.ProgrammerError("Unhandled tag type '%s'" % kind)
380 846baef9 Iustin Pop
  return retval
381 846baef9 Iustin Pop
382 846baef9 Iustin Pop
383 810c50b7 Iustin Pop
def _ExtendTags(opts, args):
384 810c50b7 Iustin Pop
  """Extend the args if a source file has been given.
385 810c50b7 Iustin Pop

386 810c50b7 Iustin Pop
  This function will extend the tags with the contents of the file
387 810c50b7 Iustin Pop
  passed in the 'tags_source' attribute of the opts parameter. A file
388 810c50b7 Iustin Pop
  named '-' will be replaced by stdin.
389 810c50b7 Iustin Pop

390 810c50b7 Iustin Pop
  """
391 810c50b7 Iustin Pop
  fname = opts.tags_source
392 810c50b7 Iustin Pop
  if fname is None:
393 810c50b7 Iustin Pop
    return
394 810c50b7 Iustin Pop
  if fname == "-":
395 810c50b7 Iustin Pop
    new_fh = sys.stdin
396 810c50b7 Iustin Pop
  else:
397 810c50b7 Iustin Pop
    new_fh = open(fname, "r")
398 810c50b7 Iustin Pop
  new_data = []
399 810c50b7 Iustin Pop
  try:
400 810c50b7 Iustin Pop
    # we don't use the nice 'new_data = [line.strip() for line in fh]'
401 810c50b7 Iustin Pop
    # because of python bug 1633941
402 810c50b7 Iustin Pop
    while True:
403 810c50b7 Iustin Pop
      line = new_fh.readline()
404 810c50b7 Iustin Pop
      if not line:
405 810c50b7 Iustin Pop
        break
406 810c50b7 Iustin Pop
      new_data.append(line.strip())
407 810c50b7 Iustin Pop
  finally:
408 810c50b7 Iustin Pop
    new_fh.close()
409 810c50b7 Iustin Pop
  args.extend(new_data)
410 810c50b7 Iustin Pop
411 810c50b7 Iustin Pop
412 846baef9 Iustin Pop
def ListTags(opts, args):
413 846baef9 Iustin Pop
  """List the tags on a given object.
414 846baef9 Iustin Pop

415 846baef9 Iustin Pop
  This is a generic implementation that knows how to deal with all
416 846baef9 Iustin Pop
  three cases of tag objects (cluster, node, instance). The opts
417 846baef9 Iustin Pop
  argument is expected to contain a tag_type field denoting what
418 846baef9 Iustin Pop
  object type we work on.
419 846baef9 Iustin Pop

420 846baef9 Iustin Pop
  """
421 846baef9 Iustin Pop
  kind, name = _ExtractTagsObject(opts, args)
422 7699c3af Iustin Pop
  cl = GetClient()
423 7699c3af Iustin Pop
  result = cl.QueryTags(kind, name)
424 846baef9 Iustin Pop
  result = list(result)
425 846baef9 Iustin Pop
  result.sort()
426 846baef9 Iustin Pop
  for tag in result:
427 03298ebe Michael Hanselmann
    ToStdout(tag)
428 846baef9 Iustin Pop
429 846baef9 Iustin Pop
430 846baef9 Iustin Pop
def AddTags(opts, args):
431 846baef9 Iustin Pop
  """Add tags on a given object.
432 846baef9 Iustin Pop

433 846baef9 Iustin Pop
  This is a generic implementation that knows how to deal with all
434 846baef9 Iustin Pop
  three cases of tag objects (cluster, node, instance). The opts
435 846baef9 Iustin Pop
  argument is expected to contain a tag_type field denoting what
436 846baef9 Iustin Pop
  object type we work on.
437 846baef9 Iustin Pop

438 846baef9 Iustin Pop
  """
439 846baef9 Iustin Pop
  kind, name = _ExtractTagsObject(opts, args)
440 810c50b7 Iustin Pop
  _ExtendTags(opts, args)
441 846baef9 Iustin Pop
  if not args:
442 846baef9 Iustin Pop
    raise errors.OpPrereqError("No tags to be added")
443 d1602edc Iustin Pop
  op = opcodes.OpTagsSet(kind=kind, name=name, tags=args)
444 af1a81d1 Michael Hanselmann
  SubmitOpCode(op, opts=opts)
445 846baef9 Iustin Pop
446 846baef9 Iustin Pop
447 846baef9 Iustin Pop
def RemoveTags(opts, args):
448 846baef9 Iustin Pop
  """Remove tags from a given object.
449 846baef9 Iustin Pop

450 846baef9 Iustin Pop
  This is a generic implementation that knows how to deal with all
451 846baef9 Iustin Pop
  three cases of tag objects (cluster, node, instance). The opts
452 846baef9 Iustin Pop
  argument is expected to contain a tag_type field denoting what
453 846baef9 Iustin Pop
  object type we work on.
454 846baef9 Iustin Pop

455 846baef9 Iustin Pop
  """
456 846baef9 Iustin Pop
  kind, name = _ExtractTagsObject(opts, args)
457 810c50b7 Iustin Pop
  _ExtendTags(opts, args)
458 846baef9 Iustin Pop
  if not args:
459 846baef9 Iustin Pop
    raise errors.OpPrereqError("No tags to be removed")
460 3f0ab95f Iustin Pop
  op = opcodes.OpTagsDel(kind=kind, name=name, tags=args)
461 af1a81d1 Michael Hanselmann
  SubmitOpCode(op, opts=opts)
462 846baef9 Iustin Pop
463 a8083063 Iustin Pop
464 8929d28c Iustin Pop
def check_unit(option, opt, value): # pylint: disable-msg=W0613
465 65fe4693 Iustin Pop
  """OptParsers custom converter for units.
466 65fe4693 Iustin Pop

467 65fe4693 Iustin Pop
  """
468 a8083063 Iustin Pop
  try:
469 a8083063 Iustin Pop
    return utils.ParseUnit(value)
470 a8083063 Iustin Pop
  except errors.UnitParseError, err:
471 3ecf6786 Iustin Pop
    raise OptionValueError("option %s: %s" % (opt, err))
472 a8083063 Iustin Pop
473 a8083063 Iustin Pop
474 a8469393 Iustin Pop
def _SplitKeyVal(opt, data):
475 a8469393 Iustin Pop
  """Convert a KeyVal string into a dict.
476 a8469393 Iustin Pop

477 a8469393 Iustin Pop
  This function will convert a key=val[,...] string into a dict. Empty
478 a8469393 Iustin Pop
  values will be converted specially: keys which have the prefix 'no_'
479 a8469393 Iustin Pop
  will have the value=False and the prefix stripped, the others will
480 a8469393 Iustin Pop
  have value=True.
481 a8469393 Iustin Pop

482 a8469393 Iustin Pop
  @type opt: string
483 a8469393 Iustin Pop
  @param opt: a string holding the option name for which we process the
484 a8469393 Iustin Pop
      data, used in building error messages
485 a8469393 Iustin Pop
  @type data: string
486 a8469393 Iustin Pop
  @param data: a string of the format key=val,key=val,...
487 a8469393 Iustin Pop
  @rtype: dict
488 a8469393 Iustin Pop
  @return: {key=val, key=val}
489 a8469393 Iustin Pop
  @raises errors.ParameterError: if there are duplicate keys
490 a8469393 Iustin Pop

491 a8469393 Iustin Pop
  """
492 a8469393 Iustin Pop
  kv_dict = {}
493 4f31882e Guido Trotter
  if data:
494 1b3a7656 Iustin Pop
    for elem in utils.UnescapeAndSplit(data, sep=","):
495 4f31882e Guido Trotter
      if "=" in elem:
496 4f31882e Guido Trotter
        key, val = elem.split("=", 1)
497 a8469393 Iustin Pop
      else:
498 4f31882e Guido Trotter
        if elem.startswith(NO_PREFIX):
499 4f31882e Guido Trotter
          key, val = elem[len(NO_PREFIX):], False
500 4f31882e Guido Trotter
        elif elem.startswith(UN_PREFIX):
501 4f31882e Guido Trotter
          key, val = elem[len(UN_PREFIX):], None
502 4f31882e Guido Trotter
        else:
503 4f31882e Guido Trotter
          key, val = elem, True
504 4f31882e Guido Trotter
      if key in kv_dict:
505 4f31882e Guido Trotter
        raise errors.ParameterError("Duplicate key '%s' in option %s" %
506 4f31882e Guido Trotter
                                    (key, opt))
507 4f31882e Guido Trotter
      kv_dict[key] = val
508 a8469393 Iustin Pop
  return kv_dict
509 a8469393 Iustin Pop
510 a8469393 Iustin Pop
511 8929d28c Iustin Pop
def check_ident_key_val(option, opt, value):  # pylint: disable-msg=W0613
512 552c8dff Michael Hanselmann
  """Custom parser for ident:key=val,key=val options.
513 552c8dff Michael Hanselmann

514 552c8dff Michael Hanselmann
  This will store the parsed values as a tuple (ident, {key: val}). As such,
515 552c8dff Michael Hanselmann
  multiple uses of this option via action=append is possible.
516 a8469393 Iustin Pop

517 a8469393 Iustin Pop
  """
518 a8469393 Iustin Pop
  if ":" not in value:
519 8b46606c Guido Trotter
    ident, rest = value, ''
520 a8469393 Iustin Pop
  else:
521 a8469393 Iustin Pop
    ident, rest = value.split(":", 1)
522 8b46606c Guido Trotter
523 8b46606c Guido Trotter
  if ident.startswith(NO_PREFIX):
524 8b46606c Guido Trotter
    if rest:
525 8b46606c Guido Trotter
      msg = "Cannot pass options when removing parameter groups: %s" % value
526 8b46606c Guido Trotter
      raise errors.ParameterError(msg)
527 8b46606c Guido Trotter
    retval = (ident[len(NO_PREFIX):], False)
528 8b46606c Guido Trotter
  elif ident.startswith(UN_PREFIX):
529 8b46606c Guido Trotter
    if rest:
530 8b46606c Guido Trotter
      msg = "Cannot pass options when removing parameter groups: %s" % value
531 8b46606c Guido Trotter
      raise errors.ParameterError(msg)
532 8b46606c Guido Trotter
    retval = (ident[len(UN_PREFIX):], None)
533 8b46606c Guido Trotter
  else:
534 a8469393 Iustin Pop
    kv_dict = _SplitKeyVal(opt, rest)
535 a8469393 Iustin Pop
    retval = (ident, kv_dict)
536 a8469393 Iustin Pop
  return retval
537 a8469393 Iustin Pop
538 a8469393 Iustin Pop
539 8929d28c Iustin Pop
def check_key_val(option, opt, value):  # pylint: disable-msg=W0613
540 552c8dff Michael Hanselmann
  """Custom parser class for key=val,key=val options.
541 552c8dff Michael Hanselmann

542 552c8dff Michael Hanselmann
  This will store the parsed values as a dict {key: val}.
543 a8469393 Iustin Pop

544 a8469393 Iustin Pop
  """
545 a8469393 Iustin Pop
  return _SplitKeyVal(opt, value)
546 a8469393 Iustin Pop
547 a8469393 Iustin Pop
548 e7b61bb0 Iustin Pop
def check_bool(option, opt, value): # pylint: disable-msg=W0613
549 e7b61bb0 Iustin Pop
  """Custom parser for yes/no options.
550 e7b61bb0 Iustin Pop

551 e7b61bb0 Iustin Pop
  This will store the parsed value as either True or False.
552 e7b61bb0 Iustin Pop

553 e7b61bb0 Iustin Pop
  """
554 e7b61bb0 Iustin Pop
  value = value.lower()
555 e7b61bb0 Iustin Pop
  if value == constants.VALUE_FALSE or value == "no":
556 e7b61bb0 Iustin Pop
    return False
557 e7b61bb0 Iustin Pop
  elif value == constants.VALUE_TRUE or value == "yes":
558 e7b61bb0 Iustin Pop
    return True
559 e7b61bb0 Iustin Pop
  else:
560 e7b61bb0 Iustin Pop
    raise errors.ParameterError("Invalid boolean value '%s'" % value)
561 e7b61bb0 Iustin Pop
562 e7b61bb0 Iustin Pop
563 63d44c55 Michael Hanselmann
# completion_suggestion is normally a list. Using numeric values not evaluating
564 63d44c55 Michael Hanselmann
# to False for dynamic completion.
565 63d44c55 Michael Hanselmann
(OPT_COMPL_MANY_NODES,
566 63d44c55 Michael Hanselmann
 OPT_COMPL_ONE_NODE,
567 63d44c55 Michael Hanselmann
 OPT_COMPL_ONE_INSTANCE,
568 63d44c55 Michael Hanselmann
 OPT_COMPL_ONE_OS,
569 2d3ed64b Michael Hanselmann
 OPT_COMPL_ONE_IALLOCATOR,
570 36e247e1 Guido Trotter
 OPT_COMPL_INST_ADD_NODES,
571 36e247e1 Guido Trotter
 OPT_COMPL_ONE_NODEGROUP) = range(100, 107)
572 63d44c55 Michael Hanselmann
573 63d44c55 Michael Hanselmann
OPT_COMPL_ALL = frozenset([
574 63d44c55 Michael Hanselmann
  OPT_COMPL_MANY_NODES,
575 63d44c55 Michael Hanselmann
  OPT_COMPL_ONE_NODE,
576 63d44c55 Michael Hanselmann
  OPT_COMPL_ONE_INSTANCE,
577 63d44c55 Michael Hanselmann
  OPT_COMPL_ONE_OS,
578 63d44c55 Michael Hanselmann
  OPT_COMPL_ONE_IALLOCATOR,
579 2d3ed64b Michael Hanselmann
  OPT_COMPL_INST_ADD_NODES,
580 36e247e1 Guido Trotter
  OPT_COMPL_ONE_NODEGROUP,
581 63d44c55 Michael Hanselmann
  ])
582 63d44c55 Michael Hanselmann
583 63d44c55 Michael Hanselmann
584 552c8dff Michael Hanselmann
class CliOption(Option):
585 552c8dff Michael Hanselmann
  """Custom option class for optparse.
586 a8469393 Iustin Pop

587 a8469393 Iustin Pop
  """
588 863d7f46 Michael Hanselmann
  ATTRS = Option.ATTRS + [
589 863d7f46 Michael Hanselmann
    "completion_suggest",
590 863d7f46 Michael Hanselmann
    ]
591 552c8dff Michael Hanselmann
  TYPES = Option.TYPES + (
592 552c8dff Michael Hanselmann
    "identkeyval",
593 552c8dff Michael Hanselmann
    "keyval",
594 552c8dff Michael Hanselmann
    "unit",
595 e7b61bb0 Iustin Pop
    "bool",
596 552c8dff Michael Hanselmann
    )
597 552c8dff Michael Hanselmann
  TYPE_CHECKER = Option.TYPE_CHECKER.copy()
598 552c8dff Michael Hanselmann
  TYPE_CHECKER["identkeyval"] = check_ident_key_val
599 a8469393 Iustin Pop
  TYPE_CHECKER["keyval"] = check_key_val
600 552c8dff Michael Hanselmann
  TYPE_CHECKER["unit"] = check_unit
601 e7b61bb0 Iustin Pop
  TYPE_CHECKER["bool"] = check_bool
602 a8469393 Iustin Pop
603 a8469393 Iustin Pop
604 a8083063 Iustin Pop
# optparse.py sets make_option, so we do it for our own option class, too
605 a8083063 Iustin Pop
cli_option = CliOption
606 a8083063 Iustin Pop
607 a8083063 Iustin Pop
608 771734c9 Iustin Pop
_YORNO = "yes|no"
609 771734c9 Iustin Pop
610 ea34193f Iustin Pop
DEBUG_OPT = cli_option("-d", "--debug", default=0, action="count",
611 ea34193f Iustin Pop
                       help="Increase debugging level")
612 c38c44ad Michael Hanselmann
613 c38c44ad Michael Hanselmann
NOHDR_OPT = cli_option("--no-headers", default=False,
614 c38c44ad Michael Hanselmann
                       action="store_true", dest="no_headers",
615 c38c44ad Michael Hanselmann
                       help="Don't display column headers")
616 c38c44ad Michael Hanselmann
617 c38c44ad Michael Hanselmann
SEP_OPT = cli_option("--separator", default=None,
618 c38c44ad Michael Hanselmann
                     action="store", dest="separator",
619 c38c44ad Michael Hanselmann
                     help=("Separator between output fields"
620 c38c44ad Michael Hanselmann
                           " (defaults to one space)"))
621 c38c44ad Michael Hanselmann
622 c38c44ad Michael Hanselmann
USEUNITS_OPT = cli_option("--units", default=None,
623 c38c44ad Michael Hanselmann
                          dest="units", choices=('h', 'm', 'g', 't'),
624 b714765a Adeodato Simo
                          help="Specify units for output (one of h/m/g/t)")
625 c38c44ad Michael Hanselmann
626 c38c44ad Michael Hanselmann
FIELDS_OPT = cli_option("-o", "--output", dest="output", action="store",
627 c38c44ad Michael Hanselmann
                        type="string", metavar="FIELDS",
628 c38c44ad Michael Hanselmann
                        help="Comma separated list of output fields")
629 c38c44ad Michael Hanselmann
630 c38c44ad Michael Hanselmann
FORCE_OPT = cli_option("-f", "--force", dest="force", action="store_true",
631 c38c44ad Michael Hanselmann
                       default=False, help="Force the operation")
632 c38c44ad Michael Hanselmann
633 c38c44ad Michael Hanselmann
CONFIRM_OPT = cli_option("--yes", dest="confirm", action="store_true",
634 c38c44ad Michael Hanselmann
                         default=False, help="Do not require confirmation")
635 c38c44ad Michael Hanselmann
636 b44bd844 Michael Hanselmann
IGNORE_OFFLINE_OPT = cli_option("--ignore-offline", dest="ignore_offline",
637 b44bd844 Michael Hanselmann
                                  action="store_true", default=False,
638 b44bd844 Michael Hanselmann
                                  help=("Ignore offline nodes and do as much"
639 b44bd844 Michael Hanselmann
                                        " as possible"))
640 b44bd844 Michael Hanselmann
641 a57981c5 Apollon Oikonomopoulos
TAG_ADD_OPT = cli_option("--tags", dest="tags",
642 a57981c5 Apollon Oikonomopoulos
                         default=None, help="Comma-separated list of instance"
643 a57981c5 Apollon Oikonomopoulos
                                            " tags")
644 a57981c5 Apollon Oikonomopoulos
645 c38c44ad Michael Hanselmann
TAG_SRC_OPT = cli_option("--from", dest="tags_source",
646 c38c44ad Michael Hanselmann
                         default=None, help="File with tag names")
647 c38c44ad Michael Hanselmann
648 c38c44ad Michael Hanselmann
SUBMIT_OPT = cli_option("--submit", dest="submit_only",
649 c38c44ad Michael Hanselmann
                        default=False, action="store_true",
650 c38c44ad Michael Hanselmann
                        help=("Submit the job and return the job ID, but"
651 c38c44ad Michael Hanselmann
                              " don't wait for the job to finish"))
652 c38c44ad Michael Hanselmann
653 c38c44ad Michael Hanselmann
SYNC_OPT = cli_option("--sync", dest="do_locking",
654 c38c44ad Michael Hanselmann
                      default=False, action="store_true",
655 c38c44ad Michael Hanselmann
                      help=("Grab locks while doing the queries"
656 c38c44ad Michael Hanselmann
                            " in order to ensure more consistent results"))
657 c38c44ad Michael Hanselmann
658 a0a6ff34 Iustin Pop
DRY_RUN_OPT = cli_option("--dry-run", default=False,
659 a0a6ff34 Iustin Pop
                         action="store_true",
660 a0a6ff34 Iustin Pop
                         help=("Do not execute the operation, just run the"
661 a0a6ff34 Iustin Pop
                               " check steps and verify it it could be"
662 a0a6ff34 Iustin Pop
                               " executed"))
663 c38c44ad Michael Hanselmann
664 9cdb9578 Iustin Pop
VERBOSE_OPT = cli_option("-v", "--verbose", default=False,
665 9cdb9578 Iustin Pop
                         action="store_true",
666 9cdb9578 Iustin Pop
                         help="Increase the verbosity of the operation")
667 9cdb9578 Iustin Pop
668 a0c9776a Iustin Pop
DEBUG_SIMERR_OPT = cli_option("--debug-simulate-errors", default=False,
669 a0c9776a Iustin Pop
                              action="store_true", dest="simulate_errors",
670 a0c9776a Iustin Pop
                              help="Debugging option that makes the operation"
671 a0c9776a Iustin Pop
                              " treat most runtime checks as failed")
672 a0c9776a Iustin Pop
673 3f75b4f3 Iustin Pop
NWSYNC_OPT = cli_option("--no-wait-for-sync", dest="wait_for_sync",
674 3f75b4f3 Iustin Pop
                        default=True, action="store_false",
675 3f75b4f3 Iustin Pop
                        help="Don't wait for sync (DANGEROUS!)")
676 3f75b4f3 Iustin Pop
677 4f365444 Iustin Pop
DISK_TEMPLATE_OPT = cli_option("-t", "--disk-template", dest="disk_template",
678 235407ba Michael Hanselmann
                               help=("Custom disk setup (%s)" %
679 235407ba Michael Hanselmann
                                     utils.CommaJoin(constants.DISK_TEMPLATES)),
680 4f365444 Iustin Pop
                               default=None, metavar="TEMPL",
681 4f365444 Iustin Pop
                               choices=list(constants.DISK_TEMPLATES))
682 4f365444 Iustin Pop
683 26023ecd Iustin Pop
NONICS_OPT = cli_option("--no-nics", default=False, action="store_true",
684 26023ecd Iustin Pop
                        help="Do not create any network cards for"
685 26023ecd Iustin Pop
                        " the instance")
686 26023ecd Iustin Pop
687 4a25828c Iustin Pop
FILESTORE_DIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
688 4a25828c Iustin Pop
                               help="Relative path under default cluster-wide"
689 4a25828c Iustin Pop
                               " file storage dir to store file-based disks",
690 4a25828c Iustin Pop
                               default=None, metavar="<DIR>")
691 4a25828c Iustin Pop
692 0f87c43e Iustin Pop
FILESTORE_DRIVER_OPT = cli_option("--file-driver", dest="file_driver",
693 0f87c43e Iustin Pop
                                  help="Driver to use for image files",
694 0f87c43e Iustin Pop
                                  default="loop", metavar="<DRIVER>",
695 0f87c43e Iustin Pop
                                  choices=list(constants.FILE_DRIVER))
696 0f87c43e Iustin Pop
697 4eb62659 Iustin Pop
IALLOCATOR_OPT = cli_option("-I", "--iallocator", metavar="<NAME>",
698 4eb62659 Iustin Pop
                            help="Select nodes for the instance automatically"
699 4eb62659 Iustin Pop
                            " using the <NAME> iallocator plugin",
700 4eb62659 Iustin Pop
                            default=None, type="string",
701 4eb62659 Iustin Pop
                            completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
702 4eb62659 Iustin Pop
703 bf4af505 Apollon Oikonomopoulos
DEFAULT_IALLOCATOR_OPT = cli_option("-I", "--default-iallocator",
704 bf4af505 Apollon Oikonomopoulos
                            metavar="<NAME>",
705 bf4af505 Apollon Oikonomopoulos
                            help="Set the default instance allocator plugin",
706 bf4af505 Apollon Oikonomopoulos
                            default=None, type="string",
707 bf4af505 Apollon Oikonomopoulos
                            completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
708 bf4af505 Apollon Oikonomopoulos
709 d3ed23ff Iustin Pop
OS_OPT = cli_option("-o", "--os-type", dest="os", help="What OS to run",
710 d3ed23ff Iustin Pop
                    metavar="<os>",
711 d3ed23ff Iustin Pop
                    completion_suggest=OPT_COMPL_ONE_OS)
712 d3ed23ff Iustin Pop
713 062a7100 Iustin Pop
OSPARAMS_OPT = cli_option("-O", "--os-parameters", dest="osparams",
714 062a7100 Iustin Pop
                         type="keyval", default={},
715 062a7100 Iustin Pop
                         help="OS parameters")
716 062a7100 Iustin Pop
717 06073e85 Guido Trotter
FORCE_VARIANT_OPT = cli_option("--force-variant", dest="force_variant",
718 06073e85 Guido Trotter
                               action="store_true", default=False,
719 06073e85 Guido Trotter
                               help="Force an unknown variant")
720 06073e85 Guido Trotter
721 25a8792c Iustin Pop
NO_INSTALL_OPT = cli_option("--no-install", dest="no_install",
722 25a8792c Iustin Pop
                            action="store_true", default=False,
723 25a8792c Iustin Pop
                            help="Do not install the OS (will"
724 25a8792c Iustin Pop
                            " enable no-start)")
725 25a8792c Iustin Pop
726 087ed2ed Iustin Pop
BACKEND_OPT = cli_option("-B", "--backend-parameters", dest="beparams",
727 087ed2ed Iustin Pop
                         type="keyval", default={},
728 087ed2ed Iustin Pop
                         help="Backend parameters")
729 48f212d7 Iustin Pop
730 48f212d7 Iustin Pop
HVOPTS_OPT =  cli_option("-H", "--hypervisor-parameters", type="keyval",
731 48f212d7 Iustin Pop
                         default={}, dest="hvparams",
732 48f212d7 Iustin Pop
                         help="Hypervisor parameters")
733 087ed2ed Iustin Pop
734 236fd9c4 Iustin Pop
HYPERVISOR_OPT = cli_option("-H", "--hypervisor-parameters", dest="hypervisor",
735 236fd9c4 Iustin Pop
                            help="Hypervisor and hypervisor options, in the"
736 236fd9c4 Iustin Pop
                            " format hypervisor:option=value,option=value,...",
737 236fd9c4 Iustin Pop
                            default=None, type="identkeyval")
738 073271f6 Iustin Pop
739 073271f6 Iustin Pop
HVLIST_OPT = cli_option("-H", "--hypervisor-parameters", dest="hvparams",
740 073271f6 Iustin Pop
                        help="Hypervisor and hypervisor options, in the"
741 073271f6 Iustin Pop
                        " format hypervisor:option=value,option=value,...",
742 073271f6 Iustin Pop
                        default=[], action="append", type="identkeyval")
743 236fd9c4 Iustin Pop
744 91e0748c Iustin Pop
NOIPCHECK_OPT = cli_option("--no-ip-check", dest="ip_check", default=True,
745 91e0748c Iustin Pop
                           action="store_false",
746 91e0748c Iustin Pop
                           help="Don't check that the instance's IP"
747 91e0748c Iustin Pop
                           " is alive")
748 91e0748c Iustin Pop
749 460d22be Iustin Pop
NONAMECHECK_OPT = cli_option("--no-name-check", dest="name_check",
750 460d22be Iustin Pop
                             default=True, action="store_false",
751 460d22be Iustin Pop
                             help="Don't check that the instance's name"
752 460d22be Iustin Pop
                             " is resolvable")
753 460d22be Iustin Pop
754 7d3a9fab Iustin Pop
NET_OPT = cli_option("--net",
755 7d3a9fab Iustin Pop
                     help="NIC parameters", default=[],
756 7d3a9fab Iustin Pop
                     dest="nics", action="append", type="identkeyval")
757 e3876ccb Iustin Pop
758 e3876ccb Iustin Pop
DISK_OPT = cli_option("--disk", help="Disk parameters", default=[],
759 e3876ccb Iustin Pop
                      dest="disks", action="append", type="identkeyval")
760 91e0748c Iustin Pop
761 4b038a1e Iustin Pop
DISKIDX_OPT = cli_option("--disks", dest="disks", default=None,
762 4b038a1e Iustin Pop
                         help="Comma-separated list of disks"
763 4b038a1e Iustin Pop
                         " indices to act on (e.g. 0,2) (optional,"
764 4b038a1e Iustin Pop
                         " defaults to all disks)")
765 4b038a1e Iustin Pop
766 ff00c1a7 Iustin Pop
OS_SIZE_OPT = cli_option("-s", "--os-size", dest="sd_size",
767 ff00c1a7 Iustin Pop
                         help="Enforces a single-disk configuration using the"
768 ff00c1a7 Iustin Pop
                         " given disk size, in MiB unless a suffix is used",
769 ff00c1a7 Iustin Pop
                         default=None, type="unit", metavar="<size>")
770 ff00c1a7 Iustin Pop
771 82a786d5 Iustin Pop
IGNORE_CONSIST_OPT = cli_option("--ignore-consistency",
772 82a786d5 Iustin Pop
                                dest="ignore_consistency",
773 82a786d5 Iustin Pop
                                action="store_true", default=False,
774 82a786d5 Iustin Pop
                                help="Ignore the consistency of the disks on"
775 82a786d5 Iustin Pop
                                " the secondary")
776 82a786d5 Iustin Pop
777 e9c487be René Nussbaumer
ALLOW_FAILOVER_OPT = cli_option("--allow-failover",
778 e9c487be René Nussbaumer
                                dest="allow_failover",
779 e9c487be René Nussbaumer
                                action="store_true", default=False,
780 e9c487be René Nussbaumer
                                help="If migration is not possible fallback to"
781 e9c487be René Nussbaumer
                                     " failover")
782 e9c487be René Nussbaumer
783 f2a0828c Iustin Pop
NONLIVE_OPT = cli_option("--non-live", dest="live",
784 f2a0828c Iustin Pop
                         default=True, action="store_false",
785 f2a0828c Iustin Pop
                         help="Do a non-live migration (this usually means"
786 f2a0828c Iustin Pop
                         " freeze the instance, save the state, transfer and"
787 f2a0828c Iustin Pop
                         " only then resume running on the secondary node)")
788 f2a0828c Iustin Pop
789 783a6c0b Iustin Pop
MIGRATION_MODE_OPT = cli_option("--migration-mode", dest="migration_mode",
790 e71b9ef4 Iustin Pop
                                default=None,
791 783a6c0b Iustin Pop
                                choices=list(constants.HT_MIGRATION_MODES),
792 783a6c0b Iustin Pop
                                help="Override default migration mode (choose"
793 e71b9ef4 Iustin Pop
                                " either live or non-live")
794 e71b9ef4 Iustin Pop
795 990b7886 Iustin Pop
NODE_PLACEMENT_OPT = cli_option("-n", "--node", dest="node",
796 990b7886 Iustin Pop
                                help="Target node and optional secondary node",
797 990b7886 Iustin Pop
                                metavar="<pnode>[:<snode>]",
798 990b7886 Iustin Pop
                                completion_suggest=OPT_COMPL_INST_ADD_NODES)
799 990b7886 Iustin Pop
800 7edc4637 Iustin Pop
NODE_LIST_OPT = cli_option("-n", "--node", dest="nodes", default=[],
801 7edc4637 Iustin Pop
                           action="append", metavar="<node>",
802 7edc4637 Iustin Pop
                           help="Use only this node (can be used multiple"
803 7edc4637 Iustin Pop
                           " times, if not given defaults to all nodes)",
804 7edc4637 Iustin Pop
                           completion_suggest=OPT_COMPL_ONE_NODE)
805 f36d7d81 Iustin Pop
806 a285fcfd Iustin Pop
NODEGROUP_OPT = cli_option("-g", "--node-group",
807 5fbbd028 Guido Trotter
                           dest="nodegroup",
808 5fbbd028 Guido Trotter
                           help="Node group (name or uuid)",
809 5fbbd028 Guido Trotter
                           metavar="<nodegroup>",
810 5fbbd028 Guido Trotter
                           default=None, type="string",
811 5fbbd028 Guido Trotter
                           completion_suggest=OPT_COMPL_ONE_NODEGROUP)
812 5fbbd028 Guido Trotter
813 f36d7d81 Iustin Pop
SINGLE_NODE_OPT = cli_option("-n", "--node", dest="node", help="Target node",
814 f36d7d81 Iustin Pop
                             metavar="<node>",
815 f36d7d81 Iustin Pop
                             completion_suggest=OPT_COMPL_ONE_NODE)
816 7edc4637 Iustin Pop
817 edeb878a Iustin Pop
NOSTART_OPT = cli_option("--no-start", dest="start", default=True,
818 edeb878a Iustin Pop
                         action="store_false",
819 edeb878a Iustin Pop
                         help="Don't start the instance after creation")
820 edeb878a Iustin Pop
821 fdcf4d84 Iustin Pop
SHOWCMD_OPT = cli_option("--show-cmd", dest="show_command",
822 fdcf4d84 Iustin Pop
                         action="store_true", default=False,
823 fdcf4d84 Iustin Pop
                         help="Show command instead of executing it")
824 fdcf4d84 Iustin Pop
825 baef337d Iustin Pop
CLEANUP_OPT = cli_option("--cleanup", dest="cleanup",
826 baef337d Iustin Pop
                         default=False, action="store_true",
827 baef337d Iustin Pop
                         help="Instead of performing the migration, try to"
828 baef337d Iustin Pop
                         " recover from a failed cleanup. This is safe"
829 baef337d Iustin Pop
                         " to run even if the instance is healthy, but it"
830 baef337d Iustin Pop
                         " will create extra replication traffic and "
831 baef337d Iustin Pop
                         " disrupt briefly the replication (like during the"
832 baef337d Iustin Pop
                         " migration")
833 baef337d Iustin Pop
834 99a8c799 Iustin Pop
STATIC_OPT = cli_option("-s", "--static", dest="static",
835 99a8c799 Iustin Pop
                        action="store_true", default=False,
836 99a8c799 Iustin Pop
                        help="Only show configuration data, not runtime data")
837 99a8c799 Iustin Pop
838 2d5e7ae1 Iustin Pop
ALL_OPT = cli_option("--all", dest="show_all",
839 2d5e7ae1 Iustin Pop
                     default=False, action="store_true",
840 2d5e7ae1 Iustin Pop
                     help="Show info on all instances on the cluster."
841 2d5e7ae1 Iustin Pop
                     " This can take a long time to run, use wisely")
842 2d5e7ae1 Iustin Pop
843 67840b40 Iustin Pop
SELECT_OS_OPT = cli_option("--select-os", dest="select_os",
844 67840b40 Iustin Pop
                           action="store_true", default=False,
845 67840b40 Iustin Pop
                           help="Interactive OS reinstall, lists available"
846 67840b40 Iustin Pop
                           " OS templates for selection")
847 2d5e7ae1 Iustin Pop
848 b6e841a8 Iustin Pop
IGNORE_FAILURES_OPT = cli_option("--ignore-failures", dest="ignore_failures",
849 b6e841a8 Iustin Pop
                                 action="store_true", default=False,
850 b6e841a8 Iustin Pop
                                 help="Remove the instance from the cluster"
851 b6e841a8 Iustin Pop
                                 " configuration even if there are failures"
852 b6e841a8 Iustin Pop
                                 " during the removal process")
853 b6e841a8 Iustin Pop
854 8d8d650c Michael Hanselmann
IGNORE_REMOVE_FAILURES_OPT = cli_option("--ignore-remove-failures",
855 8d8d650c Michael Hanselmann
                                        dest="ignore_remove_failures",
856 8d8d650c Michael Hanselmann
                                        action="store_true", default=False,
857 8d8d650c Michael Hanselmann
                                        help="Remove the instance from the"
858 8d8d650c Michael Hanselmann
                                        " cluster configuration even if there"
859 8d8d650c Michael Hanselmann
                                        " are failures during the removal"
860 8d8d650c Michael Hanselmann
                                        " process")
861 8d8d650c Michael Hanselmann
862 8d8d650c Michael Hanselmann
REMOVE_INSTANCE_OPT = cli_option("--remove-instance", dest="remove_instance",
863 8d8d650c Michael Hanselmann
                                 action="store_true", default=False,
864 8d8d650c Michael Hanselmann
                                 help="Remove the instance from the cluster")
865 8d8d650c Michael Hanselmann
866 1b7761fd Apollon Oikonomopoulos
DST_NODE_OPT = cli_option("-n", "--target-node", dest="dst_node",
867 1b7761fd Apollon Oikonomopoulos
                               help="Specifies the new node for the instance",
868 1b7761fd Apollon Oikonomopoulos
                               metavar="NODE", default=None,
869 1b7761fd Apollon Oikonomopoulos
                               completion_suggest=OPT_COMPL_ONE_NODE)
870 1b7761fd Apollon Oikonomopoulos
871 a14db5ff Iustin Pop
NEW_SECONDARY_OPT = cli_option("-n", "--new-secondary", dest="dst_node",
872 a14db5ff Iustin Pop
                               help="Specifies the new secondary node",
873 a14db5ff Iustin Pop
                               metavar="NODE", default=None,
874 a14db5ff Iustin Pop
                               completion_suggest=OPT_COMPL_ONE_NODE)
875 a14db5ff Iustin Pop
876 a72d0a79 Iustin Pop
ON_PRIMARY_OPT = cli_option("-p", "--on-primary", dest="on_primary",
877 a72d0a79 Iustin Pop
                            default=False, action="store_true",
878 a72d0a79 Iustin Pop
                            help="Replace the disk(s) on the primary"
879 235407ba Michael Hanselmann
                                 " node (applies only to internally mirrored"
880 235407ba Michael Hanselmann
                                 " disk templates, e.g. %s)" %
881 235407ba Michael Hanselmann
                                 utils.CommaJoin(constants.DTS_INT_MIRROR))
882 feb09e6a Iustin Pop
883 feb09e6a Iustin Pop
ON_SECONDARY_OPT = cli_option("-s", "--on-secondary", dest="on_secondary",
884 feb09e6a Iustin Pop
                              default=False, action="store_true",
885 feb09e6a Iustin Pop
                              help="Replace the disk(s) on the secondary"
886 235407ba Michael Hanselmann
                                   " node (applies only to internally mirrored"
887 235407ba Michael Hanselmann
                                   " disk templates, e.g. %s)" %
888 235407ba Michael Hanselmann
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
889 e00f7a05 Iustin Pop
890 4c61d894 Iustin Pop
AUTO_PROMOTE_OPT = cli_option("--auto-promote", dest="auto_promote",
891 4c61d894 Iustin Pop
                              default=False, action="store_true",
892 4c61d894 Iustin Pop
                              help="Lock all nodes and auto-promote as needed"
893 4c61d894 Iustin Pop
                              " to MC status")
894 4c61d894 Iustin Pop
895 e00f7a05 Iustin Pop
AUTO_REPLACE_OPT = cli_option("-a", "--auto", dest="auto",
896 e00f7a05 Iustin Pop
                              default=False, action="store_true",
897 e00f7a05 Iustin Pop
                              help="Automatically replace faulty disks"
898 235407ba Michael Hanselmann
                                   " (applies only to internally mirrored"
899 235407ba Michael Hanselmann
                                   " disk templates, e.g. %s)" %
900 235407ba Michael Hanselmann
                                   utils.CommaJoin(constants.DTS_INT_MIRROR))
901 a72d0a79 Iustin Pop
902 05586c90 Iustin Pop
IGNORE_SIZE_OPT = cli_option("--ignore-size", dest="ignore_size",
903 05586c90 Iustin Pop
                             default=False, action="store_true",
904 05586c90 Iustin Pop
                             help="Ignore current recorded size"
905 05586c90 Iustin Pop
                             " (useful for forcing activation when"
906 05586c90 Iustin Pop
                             " the recorded size is wrong)")
907 05586c90 Iustin Pop
908 df62e5db Iustin Pop
SRC_NODE_OPT = cli_option("--src-node", dest="src_node", help="Source node",
909 df62e5db Iustin Pop
                          metavar="<node>",
910 df62e5db Iustin Pop
                          completion_suggest=OPT_COMPL_ONE_NODE)
911 df62e5db Iustin Pop
912 df62e5db Iustin Pop
SRC_DIR_OPT = cli_option("--src-dir", dest="src_dir", help="Source directory",
913 df62e5db Iustin Pop
                         metavar="<dir>")
914 df62e5db Iustin Pop
915 8d823629 Iustin Pop
SECONDARY_IP_OPT = cli_option("-s", "--secondary-ip", dest="secondary_ip",
916 8d823629 Iustin Pop
                              help="Specify the secondary ip for the node",
917 8d823629 Iustin Pop
                              metavar="ADDRESS", default=None)
918 8d823629 Iustin Pop
919 b8d0f938 Iustin Pop
READD_OPT = cli_option("--readd", dest="readd",
920 b8d0f938 Iustin Pop
                       default=False, action="store_true",
921 b8d0f938 Iustin Pop
                       help="Readd old node after replacing it")
922 b8d0f938 Iustin Pop
923 fcdde7f2 Iustin Pop
NOSSH_KEYCHECK_OPT = cli_option("--no-ssh-key-check", dest="ssh_key_check",
924 fcdde7f2 Iustin Pop
                                default=True, action="store_false",
925 fcdde7f2 Iustin Pop
                                help="Disable SSH key fingerprint checking")
926 fcdde7f2 Iustin Pop
927 61413377 Stephen Shirley
NODE_FORCE_JOIN_OPT = cli_option("--force-join", dest="force_join",
928 61413377 Stephen Shirley
                                 default=False, action="store_true",
929 50769215 Stephen Shirley
                                 help="Force the joining of a node")
930 61413377 Stephen Shirley
931 771734c9 Iustin Pop
MC_OPT = cli_option("-C", "--master-candidate", dest="master_candidate",
932 e7b61bb0 Iustin Pop
                    type="bool", default=None, metavar=_YORNO,
933 771734c9 Iustin Pop
                    help="Set the master_candidate flag on the node")
934 771734c9 Iustin Pop
935 771734c9 Iustin Pop
OFFLINE_OPT = cli_option("-O", "--offline", dest="offline", metavar=_YORNO,
936 e7b61bb0 Iustin Pop
                         type="bool", default=None,
937 34616379 Michael Hanselmann
                         help=("Set the offline flag on the node"
938 34616379 Michael Hanselmann
                               " (cluster does not communicate with offline"
939 34616379 Michael Hanselmann
                               " nodes)"))
940 771734c9 Iustin Pop
941 771734c9 Iustin Pop
DRAINED_OPT = cli_option("-D", "--drained", dest="drained", metavar=_YORNO,
942 e7b61bb0 Iustin Pop
                         type="bool", default=None,
943 34616379 Michael Hanselmann
                         help=("Set the drained flag on the node"
944 34616379 Michael Hanselmann
                               " (excluded from allocation operations)"))
945 771734c9 Iustin Pop
946 f91e255a Iustin Pop
CAPAB_MASTER_OPT = cli_option("--master-capable", dest="master_capable",
947 f91e255a Iustin Pop
                    type="bool", default=None, metavar=_YORNO,
948 f91e255a Iustin Pop
                    help="Set the master_capable flag on the node")
949 f91e255a Iustin Pop
950 53919782 Iustin Pop
CAPAB_VM_OPT = cli_option("--vm-capable", dest="vm_capable",
951 53919782 Iustin Pop
                    type="bool", default=None, metavar=_YORNO,
952 53919782 Iustin Pop
                    help="Set the vm_capable flag on the node")
953 53919782 Iustin Pop
954 e7e09483 Iustin Pop
ALLOCATABLE_OPT = cli_option("--allocatable", dest="allocatable",
955 e7b61bb0 Iustin Pop
                             type="bool", default=None, metavar=_YORNO,
956 e7e09483 Iustin Pop
                             help="Set the allocatable flag on a volume")
957 e7e09483 Iustin Pop
958 831040bf Iustin Pop
NOLVM_STORAGE_OPT = cli_option("--no-lvm-storage", dest="lvm_storage",
959 831040bf Iustin Pop
                               help="Disable support for lvm based instances"
960 831040bf Iustin Pop
                               " (cluster-wide)",
961 831040bf Iustin Pop
                               action="store_false", default=True)
962 831040bf Iustin Pop
963 383a3591 Iustin Pop
ENABLED_HV_OPT = cli_option("--enabled-hypervisors",
964 383a3591 Iustin Pop
                            dest="enabled_hypervisors",
965 383a3591 Iustin Pop
                            help="Comma-separated list of hypervisors",
966 383a3591 Iustin Pop
                            type="string", default=None)
967 383a3591 Iustin Pop
968 4fbc93dd Iustin Pop
NIC_PARAMS_OPT = cli_option("-N", "--nic-parameters", dest="nicparams",
969 4fbc93dd Iustin Pop
                            type="keyval", default={},
970 4fbc93dd Iustin Pop
                            help="NIC parameters")
971 4fbc93dd Iustin Pop
972 e32df528 Iustin Pop
CP_SIZE_OPT = cli_option("-C", "--candidate-pool-size", default=None,
973 e32df528 Iustin Pop
                         dest="candidate_pool_size", type="int",
974 e32df528 Iustin Pop
                         help="Set the candidate pool size")
975 e32df528 Iustin Pop
976 04367e70 Guido Trotter
VG_NAME_OPT = cli_option("--vg-name", dest="vg_name",
977 34616379 Michael Hanselmann
                         help=("Enables LVM and specifies the volume group"
978 34616379 Michael Hanselmann
                               " name (cluster-wide) for disk allocation"
979 34616379 Michael Hanselmann
                               " [%s]" % constants.DEFAULT_VG),
980 b58726e8 Iustin Pop
                         metavar="VG", default=None)
981 b58726e8 Iustin Pop
982 1f587d3d Iustin Pop
YES_DOIT_OPT = cli_option("--yes-do-it", dest="yes_do_it",
983 1f587d3d Iustin Pop
                          help="Destroy cluster", action="store_true")
984 b58726e8 Iustin Pop
985 58371861 Iustin Pop
NOVOTING_OPT = cli_option("--no-voting", dest="no_voting",
986 58371861 Iustin Pop
                          help="Skip node agreement check (dangerous)",
987 58371861 Iustin Pop
                          action="store_true", default=False)
988 58371861 Iustin Pop
989 e3646f22 Iustin Pop
MAC_PREFIX_OPT = cli_option("-m", "--mac-prefix", dest="mac_prefix",
990 e3646f22 Iustin Pop
                            help="Specify the mac prefix for the instance IP"
991 e3646f22 Iustin Pop
                            " addresses, in the format XX:XX:XX",
992 e3646f22 Iustin Pop
                            metavar="PREFIX",
993 e3646f22 Iustin Pop
                            default=None)
994 e3646f22 Iustin Pop
995 29392516 Iustin Pop
MASTER_NETDEV_OPT = cli_option("--master-netdev", dest="master_netdev",
996 29392516 Iustin Pop
                               help="Specify the node interface (cluster-wide)"
997 25be0c75 Guido Trotter
                               " on which the master IP address will be added"
998 25be0c75 Guido Trotter
                               " (cluster init default: %s)" %
999 25be0c75 Guido Trotter
                               constants.DEFAULT_BRIDGE,
1000 29392516 Iustin Pop
                               metavar="NETDEV",
1001 25be0c75 Guido Trotter
                               default=None)
1002 29392516 Iustin Pop
1003 29392516 Iustin Pop
GLOBAL_FILEDIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
1004 29392516 Iustin Pop
                                help="Specify the default directory (cluster-"
1005 29392516 Iustin Pop
                                "wide) for storing the file-based disks [%s]" %
1006 29392516 Iustin Pop
                                constants.DEFAULT_FILE_STORAGE_DIR,
1007 29392516 Iustin Pop
                                metavar="DIR",
1008 29392516 Iustin Pop
                                default=constants.DEFAULT_FILE_STORAGE_DIR)
1009 4b97f902 Apollon Oikonomopoulos
1010 4b97f902 Apollon Oikonomopoulos
GLOBAL_SHARED_FILEDIR_OPT = cli_option("--shared-file-storage-dir",
1011 4b97f902 Apollon Oikonomopoulos
                            dest="shared_file_storage_dir",
1012 4b97f902 Apollon Oikonomopoulos
                            help="Specify the default directory (cluster-"
1013 4b97f902 Apollon Oikonomopoulos
                            "wide) for storing the shared file-based"
1014 4b97f902 Apollon Oikonomopoulos
                            " disks [%s]" %
1015 4b97f902 Apollon Oikonomopoulos
                            constants.DEFAULT_SHARED_FILE_STORAGE_DIR,
1016 4b97f902 Apollon Oikonomopoulos
                            metavar="SHAREDDIR",
1017 4b97f902 Apollon Oikonomopoulos
                            default=constants.DEFAULT_SHARED_FILE_STORAGE_DIR)
1018 29392516 Iustin Pop
1019 29392516 Iustin Pop
NOMODIFY_ETCHOSTS_OPT = cli_option("--no-etc-hosts", dest="modify_etc_hosts",
1020 29392516 Iustin Pop
                                   help="Don't modify /etc/hosts",
1021 29392516 Iustin Pop
                                   action="store_false", default=True)
1022 29392516 Iustin Pop
1023 b989b9d9 Ken Wehr
NOMODIFY_SSH_SETUP_OPT = cli_option("--no-ssh-init", dest="modify_ssh_setup",
1024 b989b9d9 Ken Wehr
                                    help="Don't initialize SSH keys",
1025 b989b9d9 Ken Wehr
                                    action="store_false", default=True)
1026 b989b9d9 Ken Wehr
1027 14e9e7f3 Iustin Pop
ERROR_CODES_OPT = cli_option("--error-codes", dest="error_codes",
1028 14e9e7f3 Iustin Pop
                             help="Enable parseable error messages",
1029 14e9e7f3 Iustin Pop
                             action="store_true", default=False)
1030 14e9e7f3 Iustin Pop
1031 14e9e7f3 Iustin Pop
NONPLUS1_OPT = cli_option("--no-nplus1-mem", dest="skip_nplusone_mem",
1032 14e9e7f3 Iustin Pop
                          help="Skip N+1 memory redundancy tests",
1033 14e9e7f3 Iustin Pop
                          action="store_true", default=False)
1034 14e9e7f3 Iustin Pop
1035 12054861 Iustin Pop
REBOOT_TYPE_OPT = cli_option("-t", "--type", dest="reboot_type",
1036 12054861 Iustin Pop
                             help="Type of reboot: soft/hard/full",
1037 12054861 Iustin Pop
                             default=constants.INSTANCE_REBOOT_HARD,
1038 12054861 Iustin Pop
                             metavar="<REBOOT>",
1039 12054861 Iustin Pop
                             choices=list(constants.REBOOT_TYPES))
1040 12054861 Iustin Pop
1041 ee3f9578 Iustin Pop
IGNORE_SECONDARIES_OPT = cli_option("--ignore-secondaries",
1042 ee3f9578 Iustin Pop
                                    dest="ignore_secondaries",
1043 ee3f9578 Iustin Pop
                                    default=False, action="store_true",
1044 ee3f9578 Iustin Pop
                                    help="Ignore errors from secondaries")
1045 ee3f9578 Iustin Pop
1046 69b99987 Michael Hanselmann
NOSHUTDOWN_OPT = cli_option("--noshutdown", dest="shutdown",
1047 44c44832 Iustin Pop
                            action="store_false", default=True,
1048 44c44832 Iustin Pop
                            help="Don't shutdown the instance (unsafe)")
1049 44c44832 Iustin Pop
1050 b5762e2a Guido Trotter
TIMEOUT_OPT = cli_option("--timeout", dest="timeout", type="int",
1051 b5762e2a Guido Trotter
                         default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1052 b5762e2a Guido Trotter
                         help="Maximum time to wait")
1053 44c44832 Iustin Pop
1054 4d98c565 Guido Trotter
SHUTDOWN_TIMEOUT_OPT = cli_option("--shutdown-timeout",
1055 4d98c565 Guido Trotter
                         dest="shutdown_timeout", type="int",
1056 7e5eaaa8 Guido Trotter
                         default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
1057 7e5eaaa8 Guido Trotter
                         help="Maximum time to wait for instance shutdown")
1058 7e5eaaa8 Guido Trotter
1059 19b9ba9a Michael Hanselmann
INTERVAL_OPT = cli_option("--interval", dest="interval", type="int",
1060 19b9ba9a Michael Hanselmann
                          default=None,
1061 19b9ba9a Michael Hanselmann
                          help=("Number of seconds between repetions of the"
1062 19b9ba9a Michael Hanselmann
                                " command"))
1063 19b9ba9a Michael Hanselmann
1064 7ea7bcf6 Iustin Pop
EARLY_RELEASE_OPT = cli_option("--early-release",
1065 7ea7bcf6 Iustin Pop
                               dest="early_release", default=False,
1066 7ea7bcf6 Iustin Pop
                               action="store_true",
1067 7ea7bcf6 Iustin Pop
                               help="Release the locks on the secondary"
1068 7ea7bcf6 Iustin Pop
                               " node(s) early")
1069 7ea7bcf6 Iustin Pop
1070 6d4a1656 Michael Hanselmann
NEW_CLUSTER_CERT_OPT = cli_option("--new-cluster-certificate",
1071 6d4a1656 Michael Hanselmann
                                  dest="new_cluster_cert",
1072 6d4a1656 Michael Hanselmann
                                  default=False, action="store_true",
1073 6d4a1656 Michael Hanselmann
                                  help="Generate a new cluster certificate")
1074 6d4a1656 Michael Hanselmann
1075 6d4a1656 Michael Hanselmann
RAPI_CERT_OPT = cli_option("--rapi-certificate", dest="rapi_cert",
1076 6d4a1656 Michael Hanselmann
                           default=None,
1077 6d4a1656 Michael Hanselmann
                           help="File containing new RAPI certificate")
1078 6d4a1656 Michael Hanselmann
1079 6d4a1656 Michael Hanselmann
NEW_RAPI_CERT_OPT = cli_option("--new-rapi-certificate", dest="new_rapi_cert",
1080 6d4a1656 Michael Hanselmann
                               default=None, action="store_true",
1081 6d4a1656 Michael Hanselmann
                               help=("Generate a new self-signed RAPI"
1082 6d4a1656 Michael Hanselmann
                                     " certificate"))
1083 6d4a1656 Michael Hanselmann
1084 6b7d5878 Michael Hanselmann
NEW_CONFD_HMAC_KEY_OPT = cli_option("--new-confd-hmac-key",
1085 6b7d5878 Michael Hanselmann
                                    dest="new_confd_hmac_key",
1086 6b7d5878 Michael Hanselmann
                                    default=False, action="store_true",
1087 6b7d5878 Michael Hanselmann
                                    help=("Create a new HMAC key for %s" %
1088 6b7d5878 Michael Hanselmann
                                          constants.CONFD))
1089 6d4a1656 Michael Hanselmann
1090 3db3eb2a Michael Hanselmann
CLUSTER_DOMAIN_SECRET_OPT = cli_option("--cluster-domain-secret",
1091 3db3eb2a Michael Hanselmann
                                       dest="cluster_domain_secret",
1092 3db3eb2a Michael Hanselmann
                                       default=None,
1093 3db3eb2a Michael Hanselmann
                                       help=("Load new new cluster domain"
1094 3db3eb2a Michael Hanselmann
                                             " secret from file"))
1095 3db3eb2a Michael Hanselmann
1096 3db3eb2a Michael Hanselmann
NEW_CLUSTER_DOMAIN_SECRET_OPT = cli_option("--new-cluster-domain-secret",
1097 3db3eb2a Michael Hanselmann
                                           dest="new_cluster_domain_secret",
1098 3db3eb2a Michael Hanselmann
                                           default=False, action="store_true",
1099 3db3eb2a Michael Hanselmann
                                           help=("Create a new cluster domain"
1100 3db3eb2a Michael Hanselmann
                                                 " secret"))
1101 3db3eb2a Michael Hanselmann
1102 74adc100 Iustin Pop
USE_REPL_NET_OPT = cli_option("--use-replication-network",
1103 74adc100 Iustin Pop
                              dest="use_replication_network",
1104 74adc100 Iustin Pop
                              help="Whether to use the replication network"
1105 74adc100 Iustin Pop
                              " for talking to the nodes",
1106 74adc100 Iustin Pop
                              action="store_true", default=False)
1107 74adc100 Iustin Pop
1108 3953242f Iustin Pop
MAINTAIN_NODE_HEALTH_OPT = \
1109 3953242f Iustin Pop
    cli_option("--maintain-node-health", dest="maintain_node_health",
1110 3953242f Iustin Pop
               metavar=_YORNO, default=None, type="bool",
1111 3953242f Iustin Pop
               help="Configure the cluster to automatically maintain node"
1112 3953242f Iustin Pop
               " health, by shutting down unknown instances, shutting down"
1113 3953242f Iustin Pop
               " unknown DRBD devices, etc.")
1114 3953242f Iustin Pop
1115 e588764d Iustin Pop
IDENTIFY_DEFAULTS_OPT = \
1116 e588764d Iustin Pop
    cli_option("--identify-defaults", dest="identify_defaults",
1117 e588764d Iustin Pop
               default=False, action="store_true",
1118 e588764d Iustin Pop
               help="Identify which saved instance parameters are equal to"
1119 e588764d Iustin Pop
               " the current cluster defaults and set them as such, instead"
1120 e588764d Iustin Pop
               " of marking them as overridden")
1121 e588764d Iustin Pop
1122 1338f2b4 Balazs Lecz
UIDPOOL_OPT = cli_option("--uid-pool", default=None,
1123 1338f2b4 Balazs Lecz
                         action="store", dest="uid_pool",
1124 1338f2b4 Balazs Lecz
                         help=("A list of user-ids or user-id"
1125 1338f2b4 Balazs Lecz
                               " ranges separated by commas"))
1126 1338f2b4 Balazs Lecz
1127 fdad8c4d Balazs Lecz
ADD_UIDS_OPT = cli_option("--add-uids", default=None,
1128 fdad8c4d Balazs Lecz
                          action="store", dest="add_uids",
1129 fdad8c4d Balazs Lecz
                          help=("A list of user-ids or user-id"
1130 fdad8c4d Balazs Lecz
                                " ranges separated by commas, to be"
1131 fdad8c4d Balazs Lecz
                                " added to the user-id pool"))
1132 fdad8c4d Balazs Lecz
1133 fdad8c4d Balazs Lecz
REMOVE_UIDS_OPT = cli_option("--remove-uids", default=None,
1134 fdad8c4d Balazs Lecz
                             action="store", dest="remove_uids",
1135 fdad8c4d Balazs Lecz
                             help=("A list of user-ids or user-id"
1136 fdad8c4d Balazs Lecz
                                   " ranges separated by commas, to be"
1137 fdad8c4d Balazs Lecz
                                   " removed from the user-id pool"))
1138 fdad8c4d Balazs Lecz
1139 f38ea602 Iustin Pop
RESERVED_LVS_OPT = cli_option("--reserved-lvs", default=None,
1140 f38ea602 Iustin Pop
                             action="store", dest="reserved_lvs",
1141 f38ea602 Iustin Pop
                             help=("A comma-separated list of reserved"
1142 f38ea602 Iustin Pop
                                   " logical volumes names, that will be"
1143 f38ea602 Iustin Pop
                                   " ignored by cluster verify"))
1144 f38ea602 Iustin Pop
1145 31d97b2a Guido Trotter
ROMAN_OPT = cli_option("--roman",
1146 31d97b2a Guido Trotter
                       dest="roman_integers", default=False,
1147 31d97b2a Guido Trotter
                       action="store_true",
1148 31d97b2a Guido Trotter
                       help="Use roman numbers for positive integers")
1149 31d97b2a Guido Trotter
1150 26591bfd Luca Bigliardi
DRBD_HELPER_OPT = cli_option("--drbd-usermode-helper", dest="drbd_helper",
1151 26591bfd Luca Bigliardi
                             action="store", default=None,
1152 26591bfd Luca Bigliardi
                             help="Specifies usermode helper for DRBD")
1153 26591bfd Luca Bigliardi
1154 26591bfd Luca Bigliardi
NODRBD_STORAGE_OPT = cli_option("--no-drbd-storage", dest="drbd_storage",
1155 26591bfd Luca Bigliardi
                                action="store_false", default=True,
1156 26591bfd Luca Bigliardi
                                help="Disable support for DRBD")
1157 31d97b2a Guido Trotter
1158 e7323b5e Manuel Franceschini
PRIMARY_IP_VERSION_OPT = \
1159 e7323b5e Manuel Franceschini
    cli_option("--primary-ip-version", default=constants.IP4_VERSION,
1160 e7323b5e Manuel Franceschini
               action="store", dest="primary_ip_version",
1161 e7323b5e Manuel Franceschini
               metavar="%d|%d" % (constants.IP4_VERSION,
1162 e7323b5e Manuel Franceschini
                                  constants.IP6_VERSION),
1163 e7323b5e Manuel Franceschini
               help="Cluster-wide IP version for primary IP")
1164 e7323b5e Manuel Franceschini
1165 84a5b33c Michael Hanselmann
PRIORITY_OPT = cli_option("--priority", default=None, dest="priority",
1166 84a5b33c Michael Hanselmann
                          metavar="|".join(name for name, _ in _PRIORITY_NAMES),
1167 84a5b33c Michael Hanselmann
                          choices=_PRIONAME_TO_VALUE.keys(),
1168 aa06f8c6 Michael Hanselmann
                          help="Priority for opcode processing")
1169 84a5b33c Michael Hanselmann
1170 61a14bb3 Iustin Pop
HID_OS_OPT = cli_option("--hidden", dest="hidden",
1171 61a14bb3 Iustin Pop
                        type="bool", default=None, metavar=_YORNO,
1172 61a14bb3 Iustin Pop
                        help="Sets the hidden flag on the OS")
1173 61a14bb3 Iustin Pop
1174 61a14bb3 Iustin Pop
BLK_OS_OPT = cli_option("--blacklisted", dest="blacklisted",
1175 61a14bb3 Iustin Pop
                        type="bool", default=None, metavar=_YORNO,
1176 61a14bb3 Iustin Pop
                        help="Sets the blacklisted flag on the OS")
1177 61a14bb3 Iustin Pop
1178 b883637f René Nussbaumer
PREALLOC_WIPE_DISKS_OPT = cli_option("--prealloc-wipe-disks", default=None,
1179 b883637f René Nussbaumer
                                     type="bool", metavar=_YORNO,
1180 b883637f René Nussbaumer
                                     dest="prealloc_wipe_disks",
1181 b883637f René Nussbaumer
                                     help=("Wipe disks prior to instance"
1182 b883637f René Nussbaumer
                                           " creation"))
1183 b883637f René Nussbaumer
1184 a535cef7 René Nussbaumer
NODE_PARAMS_OPT = cli_option("--node-parameters", dest="ndparams",
1185 3c2b6a98 René Nussbaumer
                             type="keyval", default=None,
1186 a535cef7 René Nussbaumer
                             help="Node parameters")
1187 a535cef7 René Nussbaumer
1188 90e99856 Adeodato Simo
ALLOC_POLICY_OPT = cli_option("--alloc-policy", dest="alloc_policy",
1189 90e99856 Adeodato Simo
                              action="store", metavar="POLICY", default=None,
1190 90e99856 Adeodato Simo
                              help="Allocation policy for the node group")
1191 90e99856 Adeodato Simo
1192 d2881c71 René Nussbaumer
NODE_POWERED_OPT = cli_option("--node-powered", default=None,
1193 d2881c71 René Nussbaumer
                              type="bool", metavar=_YORNO,
1194 d2881c71 René Nussbaumer
                              dest="node_powered",
1195 d2881c71 René Nussbaumer
                              help="Specify if the SoR for node is powered")
1196 d2881c71 René Nussbaumer
1197 41543d8b René Nussbaumer
OOB_TIMEOUT_OPT = cli_option("--oob-timeout", dest="oob_timeout", type="int",
1198 41543d8b René Nussbaumer
                         default=constants.OOB_TIMEOUT,
1199 41543d8b René Nussbaumer
                         help="Maximum time to wait for out-of-band helper")
1200 41543d8b René Nussbaumer
1201 0c086a13 René Nussbaumer
POWER_DELAY_OPT = cli_option("--power-delay", dest="power_delay", type="float",
1202 0c086a13 René Nussbaumer
                             default=constants.OOB_POWER_DELAY,
1203 0c086a13 René Nussbaumer
                             help="Time in seconds to wait between power-ons")
1204 0c086a13 René Nussbaumer
1205 442587bf Michael Hanselmann
FORCE_FILTER_OPT = cli_option("-F", "--filter", dest="force_filter",
1206 442587bf Michael Hanselmann
                              action="store_true", default=False,
1207 442587bf Michael Hanselmann
                              help=("Whether command argument should be treated"
1208 442587bf Michael Hanselmann
                                    " as filter"))
1209 442587bf Michael Hanselmann
1210 885a0fc4 Iustin Pop
NO_REMEMBER_OPT = cli_option("--no-remember",
1211 885a0fc4 Iustin Pop
                             dest="no_remember",
1212 885a0fc4 Iustin Pop
                             action="store_true", default=False,
1213 885a0fc4 Iustin Pop
                             help="Perform but do not record the change"
1214 885a0fc4 Iustin Pop
                             " in the configuration")
1215 885a0fc4 Iustin Pop
1216 aafee533 Michael Hanselmann
PRIMARY_ONLY_OPT = cli_option("-p", "--primary-only",
1217 aafee533 Michael Hanselmann
                              default=False, action="store_true",
1218 aafee533 Michael Hanselmann
                              help="Evacuate primary instances only")
1219 aafee533 Michael Hanselmann
1220 aafee533 Michael Hanselmann
SECONDARY_ONLY_OPT = cli_option("-s", "--secondary-only",
1221 aafee533 Michael Hanselmann
                                default=False, action="store_true",
1222 aafee533 Michael Hanselmann
                                help="Evacuate secondary instances only"
1223 aafee533 Michael Hanselmann
                                     " (applies only to internally mirrored"
1224 aafee533 Michael Hanselmann
                                     " disk templates, e.g. %s)" %
1225 aafee533 Michael Hanselmann
                                     utils.CommaJoin(constants.DTS_INT_MIRROR))
1226 aafee533 Michael Hanselmann
1227 323f9095 Stephen Shirley
STARTUP_PAUSED_OPT = cli_option("--paused", dest="startup_paused",
1228 323f9095 Stephen Shirley
                                action="store_true", default=False,
1229 323f9095 Stephen Shirley
                                help="Pause instance at startup")
1230 323f9095 Stephen Shirley
1231 61a14bb3 Iustin Pop
1232 5786c087 Michael Hanselmann
#: Options provided by all commands
1233 5786c087 Michael Hanselmann
COMMON_OPTS = [DEBUG_OPT]
1234 5786c087 Michael Hanselmann
1235 eb28ecf6 Guido Trotter
# common options for creating instances. add and import then add their own
1236 eb28ecf6 Guido Trotter
# specific ones.
1237 eb28ecf6 Guido Trotter
COMMON_CREATE_OPTS = [
1238 eb28ecf6 Guido Trotter
  BACKEND_OPT,
1239 eb28ecf6 Guido Trotter
  DISK_OPT,
1240 eb28ecf6 Guido Trotter
  DISK_TEMPLATE_OPT,
1241 eb28ecf6 Guido Trotter
  FILESTORE_DIR_OPT,
1242 eb28ecf6 Guido Trotter
  FILESTORE_DRIVER_OPT,
1243 eb28ecf6 Guido Trotter
  HYPERVISOR_OPT,
1244 eb28ecf6 Guido Trotter
  IALLOCATOR_OPT,
1245 eb28ecf6 Guido Trotter
  NET_OPT,
1246 eb28ecf6 Guido Trotter
  NODE_PLACEMENT_OPT,
1247 eb28ecf6 Guido Trotter
  NOIPCHECK_OPT,
1248 eb28ecf6 Guido Trotter
  NONAMECHECK_OPT,
1249 eb28ecf6 Guido Trotter
  NONICS_OPT,
1250 eb28ecf6 Guido Trotter
  NWSYNC_OPT,
1251 eb28ecf6 Guido Trotter
  OSPARAMS_OPT,
1252 eb28ecf6 Guido Trotter
  OS_SIZE_OPT,
1253 eb28ecf6 Guido Trotter
  SUBMIT_OPT,
1254 0f8810df Michael Hanselmann
  TAG_ADD_OPT,
1255 eb28ecf6 Guido Trotter
  DRY_RUN_OPT,
1256 eb28ecf6 Guido Trotter
  PRIORITY_OPT,
1257 eb28ecf6 Guido Trotter
  ]
1258 eb28ecf6 Guido Trotter
1259 771734c9 Iustin Pop
1260 de47cf8f Guido Trotter
def _ParseArgs(argv, commands, aliases):
1261 c41eea6e Iustin Pop
  """Parser for the command line arguments.
1262 a8083063 Iustin Pop

1263 5bbd3f7f Michael Hanselmann
  This function parses the arguments and returns the function which
1264 c41eea6e Iustin Pop
  must be executed together with its (modified) arguments.
1265 a8083063 Iustin Pop

1266 c41eea6e Iustin Pop
  @param argv: the command line
1267 c41eea6e Iustin Pop
  @param commands: dictionary with special contents, see the design
1268 c41eea6e Iustin Pop
      doc for cmdline handling
1269 c41eea6e Iustin Pop
  @param aliases: dictionary with command aliases {'alias': 'target, ...}
1270 098c0958 Michael Hanselmann

1271 a8083063 Iustin Pop
  """
1272 a8083063 Iustin Pop
  if len(argv) == 0:
1273 a8083063 Iustin Pop
    binary = "<command>"
1274 a8083063 Iustin Pop
  else:
1275 a8083063 Iustin Pop
    binary = argv[0].split("/")[-1]
1276 a8083063 Iustin Pop
1277 a8083063 Iustin Pop
  if len(argv) > 1 and argv[1] == "--version":
1278 84a12e40 Iustin Pop
    ToStdout("%s (ganeti %s) %s", binary, constants.VCS_VERSION,
1279 84a12e40 Iustin Pop
             constants.RELEASE_VERSION)
1280 a8083063 Iustin Pop
    # Quit right away. That way we don't have to care about this special
1281 a8083063 Iustin Pop
    # argument. optparse.py does it the same.
1282 a8083063 Iustin Pop
    sys.exit(0)
1283 a8083063 Iustin Pop
1284 de47cf8f Guido Trotter
  if len(argv) < 2 or not (argv[1] in commands or
1285 70a35b6f Guido Trotter
                           argv[1] in aliases):
1286 a8083063 Iustin Pop
    # let's do a nice thing
1287 a8083063 Iustin Pop
    sortedcmds = commands.keys()
1288 a8083063 Iustin Pop
    sortedcmds.sort()
1289 03298ebe Michael Hanselmann
1290 03298ebe Michael Hanselmann
    ToStdout("Usage: %s {command} [options...] [argument...]", binary)
1291 03298ebe Michael Hanselmann
    ToStdout("%s <command> --help to see details, or man %s", binary, binary)
1292 03298ebe Michael Hanselmann
    ToStdout("")
1293 03298ebe Michael Hanselmann
1294 a8083063 Iustin Pop
    # compute the max line length for cmd + usage
1295 4e713df6 Iustin Pop
    mlen = max([len(" %s" % cmd) for cmd in commands])
1296 a8083063 Iustin Pop
    mlen = min(60, mlen) # should not get here...
1297 03298ebe Michael Hanselmann
1298 a8083063 Iustin Pop
    # and format a nice command list
1299 03298ebe Michael Hanselmann
    ToStdout("Commands:")
1300 a8083063 Iustin Pop
    for cmd in sortedcmds:
1301 4e713df6 Iustin Pop
      cmdstr = " %s" % (cmd,)
1302 9a033156 Iustin Pop
      help_text = commands[cmd][4]
1303 03298ebe Michael Hanselmann
      help_lines = textwrap.wrap(help_text, 79 - 3 - mlen)
1304 03298ebe Michael Hanselmann
      ToStdout("%-*s - %s", mlen, cmdstr, help_lines.pop(0))
1305 a8083063 Iustin Pop
      for line in help_lines:
1306 03298ebe Michael Hanselmann
        ToStdout("%-*s   %s", mlen, "", line)
1307 03298ebe Michael Hanselmann
1308 03298ebe Michael Hanselmann
    ToStdout("")
1309 03298ebe Michael Hanselmann
1310 a8083063 Iustin Pop
    return None, None, None
1311 de47cf8f Guido Trotter
1312 de47cf8f Guido Trotter
  # get command, unalias it, and look it up in commands
1313 a8083063 Iustin Pop
  cmd = argv.pop(1)
1314 de47cf8f Guido Trotter
  if cmd in aliases:
1315 de47cf8f Guido Trotter
    if cmd in commands:
1316 de47cf8f Guido Trotter
      raise errors.ProgrammerError("Alias '%s' overrides an existing"
1317 de47cf8f Guido Trotter
                                   " command" % cmd)
1318 de47cf8f Guido Trotter
1319 de47cf8f Guido Trotter
    if aliases[cmd] not in commands:
1320 de47cf8f Guido Trotter
      raise errors.ProgrammerError("Alias '%s' maps to non-existing"
1321 de47cf8f Guido Trotter
                                   " command '%s'" % (cmd, aliases[cmd]))
1322 de47cf8f Guido Trotter
1323 de47cf8f Guido Trotter
    cmd = aliases[cmd]
1324 de47cf8f Guido Trotter
1325 a8005e17 Michael Hanselmann
  func, args_def, parser_opts, usage, description = commands[cmd]
1326 5786c087 Michael Hanselmann
  parser = OptionParser(option_list=parser_opts + COMMON_OPTS,
1327 a8083063 Iustin Pop
                        description=description,
1328 a8083063 Iustin Pop
                        formatter=TitledHelpFormatter(),
1329 a8083063 Iustin Pop
                        usage="%%prog %s %s" % (cmd, usage))
1330 a8083063 Iustin Pop
  parser.disable_interspersed_args()
1331 a8083063 Iustin Pop
  options, args = parser.parse_args()
1332 a8005e17 Michael Hanselmann
1333 a8005e17 Michael Hanselmann
  if not _CheckArguments(cmd, args_def, args):
1334 a8083063 Iustin Pop
    return None, None, None
1335 a8083063 Iustin Pop
1336 a8083063 Iustin Pop
  return func, options, args
1337 a8083063 Iustin Pop
1338 a8083063 Iustin Pop
1339 a8005e17 Michael Hanselmann
def _CheckArguments(cmd, args_def, args):
1340 a8005e17 Michael Hanselmann
  """Verifies the arguments using the argument definition.
1341 a8005e17 Michael Hanselmann

1342 a8005e17 Michael Hanselmann
  Algorithm:
1343 a8005e17 Michael Hanselmann

1344 a8005e17 Michael Hanselmann
    1. Abort with error if values specified by user but none expected.
1345 a8005e17 Michael Hanselmann

1346 a8005e17 Michael Hanselmann
    1. For each argument in definition
1347 a8005e17 Michael Hanselmann

1348 a8005e17 Michael Hanselmann
      1. Keep running count of minimum number of values (min_count)
1349 a8005e17 Michael Hanselmann
      1. Keep running count of maximum number of values (max_count)
1350 a8005e17 Michael Hanselmann
      1. If it has an unlimited number of values
1351 a8005e17 Michael Hanselmann

1352 a8005e17 Michael Hanselmann
        1. Abort with error if it's not the last argument in the definition
1353 a8005e17 Michael Hanselmann

1354 a8005e17 Michael Hanselmann
    1. If last argument has limited number of values
1355 a8005e17 Michael Hanselmann

1356 a8005e17 Michael Hanselmann
      1. Abort with error if number of values doesn't match or is too large
1357 a8005e17 Michael Hanselmann

1358 a8005e17 Michael Hanselmann
    1. Abort with error if user didn't pass enough values (min_count)
1359 a8005e17 Michael Hanselmann

1360 a8005e17 Michael Hanselmann
  """
1361 a8005e17 Michael Hanselmann
  if args and not args_def:
1362 a8005e17 Michael Hanselmann
    ToStderr("Error: Command %s expects no arguments", cmd)
1363 a8005e17 Michael Hanselmann
    return False
1364 a8005e17 Michael Hanselmann
1365 a8005e17 Michael Hanselmann
  min_count = None
1366 a8005e17 Michael Hanselmann
  max_count = None
1367 a8005e17 Michael Hanselmann
  check_max = None
1368 a8005e17 Michael Hanselmann
1369 a8005e17 Michael Hanselmann
  last_idx = len(args_def) - 1
1370 a8005e17 Michael Hanselmann
1371 a8005e17 Michael Hanselmann
  for idx, arg in enumerate(args_def):
1372 a8005e17 Michael Hanselmann
    if min_count is None:
1373 a8005e17 Michael Hanselmann
      min_count = arg.min
1374 a8005e17 Michael Hanselmann
    elif arg.min is not None:
1375 a8005e17 Michael Hanselmann
      min_count += arg.min
1376 a8005e17 Michael Hanselmann
1377 a8005e17 Michael Hanselmann
    if max_count is None:
1378 a8005e17 Michael Hanselmann
      max_count = arg.max
1379 a8005e17 Michael Hanselmann
    elif arg.max is not None:
1380 a8005e17 Michael Hanselmann
      max_count += arg.max
1381 a8005e17 Michael Hanselmann
1382 a8005e17 Michael Hanselmann
    if idx == last_idx:
1383 a8005e17 Michael Hanselmann
      check_max = (arg.max is not None)
1384 a8005e17 Michael Hanselmann
1385 a8005e17 Michael Hanselmann
    elif arg.max is None:
1386 a8005e17 Michael Hanselmann
      raise errors.ProgrammerError("Only the last argument can have max=None")
1387 a8005e17 Michael Hanselmann
1388 a8005e17 Michael Hanselmann
  if check_max:
1389 a8005e17 Michael Hanselmann
    # Command with exact number of arguments
1390 a8005e17 Michael Hanselmann
    if (min_count is not None and max_count is not None and
1391 a8005e17 Michael Hanselmann
        min_count == max_count and len(args) != min_count):
1392 a8005e17 Michael Hanselmann
      ToStderr("Error: Command %s expects %d argument(s)", cmd, min_count)
1393 a8005e17 Michael Hanselmann
      return False
1394 a8005e17 Michael Hanselmann
1395 a8005e17 Michael Hanselmann
    # Command with limited number of arguments
1396 a8005e17 Michael Hanselmann
    if max_count is not None and len(args) > max_count:
1397 a8005e17 Michael Hanselmann
      ToStderr("Error: Command %s expects only %d argument(s)",
1398 a8005e17 Michael Hanselmann
               cmd, max_count)
1399 a8005e17 Michael Hanselmann
      return False
1400 a8005e17 Michael Hanselmann
1401 a8005e17 Michael Hanselmann
  # Command with some required arguments
1402 a8005e17 Michael Hanselmann
  if min_count is not None and len(args) < min_count:
1403 a8005e17 Michael Hanselmann
    ToStderr("Error: Command %s expects at least %d argument(s)",
1404 a8005e17 Michael Hanselmann
             cmd, min_count)
1405 a8005e17 Michael Hanselmann
    return False
1406 a8005e17 Michael Hanselmann
1407 a8005e17 Michael Hanselmann
  return True
1408 a8005e17 Michael Hanselmann
1409 a8005e17 Michael Hanselmann
1410 60d49723 Michael Hanselmann
def SplitNodeOption(value):
1411 60d49723 Michael Hanselmann
  """Splits the value of a --node option.
1412 60d49723 Michael Hanselmann

1413 60d49723 Michael Hanselmann
  """
1414 60d49723 Michael Hanselmann
  if value and ':' in value:
1415 60d49723 Michael Hanselmann
    return value.split(':', 1)
1416 60d49723 Michael Hanselmann
  else:
1417 60d49723 Michael Hanselmann
    return (value, None)
1418 60d49723 Michael Hanselmann
1419 60d49723 Michael Hanselmann
1420 07150497 Guido Trotter
def CalculateOSNames(os_name, os_variants):
1421 07150497 Guido Trotter
  """Calculates all the names an OS can be called, according to its variants.
1422 07150497 Guido Trotter

1423 07150497 Guido Trotter
  @type os_name: string
1424 07150497 Guido Trotter
  @param os_name: base name of the os
1425 07150497 Guido Trotter
  @type os_variants: list or None
1426 07150497 Guido Trotter
  @param os_variants: list of supported variants
1427 07150497 Guido Trotter
  @rtype: list
1428 07150497 Guido Trotter
  @return: list of valid names
1429 07150497 Guido Trotter

1430 07150497 Guido Trotter
  """
1431 07150497 Guido Trotter
  if os_variants:
1432 07150497 Guido Trotter
    return ['%s+%s' % (os_name, v) for v in os_variants]
1433 07150497 Guido Trotter
  else:
1434 07150497 Guido Trotter
    return [os_name]
1435 07150497 Guido Trotter
1436 07150497 Guido Trotter
1437 a4ebd726 Michael Hanselmann
def ParseFields(selected, default):
1438 a4ebd726 Michael Hanselmann
  """Parses the values of "--field"-like options.
1439 a4ebd726 Michael Hanselmann

1440 a4ebd726 Michael Hanselmann
  @type selected: string or None
1441 a4ebd726 Michael Hanselmann
  @param selected: User-selected options
1442 a4ebd726 Michael Hanselmann
  @type default: list
1443 a4ebd726 Michael Hanselmann
  @param default: Default fields
1444 a4ebd726 Michael Hanselmann

1445 a4ebd726 Michael Hanselmann
  """
1446 a4ebd726 Michael Hanselmann
  if selected is None:
1447 a4ebd726 Michael Hanselmann
    return default
1448 a4ebd726 Michael Hanselmann
1449 a4ebd726 Michael Hanselmann
  if selected.startswith("+"):
1450 a4ebd726 Michael Hanselmann
    return default + selected[1:].split(",")
1451 a4ebd726 Michael Hanselmann
1452 a4ebd726 Michael Hanselmann
  return selected.split(",")
1453 a4ebd726 Michael Hanselmann
1454 a4ebd726 Michael Hanselmann
1455 e0e916fe Iustin Pop
UsesRPC = rpc.RunWithRPC
1456 4331f6cd Michael Hanselmann
1457 4331f6cd Michael Hanselmann
1458 47988778 Iustin Pop
def AskUser(text, choices=None):
1459 47988778 Iustin Pop
  """Ask the user a question.
1460 a8083063 Iustin Pop

1461 c41eea6e Iustin Pop
  @param text: the question to ask
1462 a8083063 Iustin Pop

1463 c41eea6e Iustin Pop
  @param choices: list with elements tuples (input_char, return_value,
1464 c41eea6e Iustin Pop
      description); if not given, it will default to: [('y', True,
1465 c41eea6e Iustin Pop
      'Perform the operation'), ('n', False, 'Do no do the operation')];
1466 c41eea6e Iustin Pop
      note that the '?' char is reserved for help
1467 47988778 Iustin Pop

1468 c41eea6e Iustin Pop
  @return: one of the return values from the choices list; if input is
1469 c41eea6e Iustin Pop
      not possible (i.e. not running with a tty, we return the last
1470 c41eea6e Iustin Pop
      entry from the list
1471 a8083063 Iustin Pop

1472 a8083063 Iustin Pop
  """
1473 47988778 Iustin Pop
  if choices is None:
1474 47988778 Iustin Pop
    choices = [('y', True, 'Perform the operation'),
1475 47988778 Iustin Pop
               ('n', False, 'Do not perform the operation')]
1476 47988778 Iustin Pop
  if not choices or not isinstance(choices, list):
1477 5bbd3f7f Michael Hanselmann
    raise errors.ProgrammerError("Invalid choices argument to AskUser")
1478 47988778 Iustin Pop
  for entry in choices:
1479 47988778 Iustin Pop
    if not isinstance(entry, tuple) or len(entry) < 3 or entry[0] == '?':
1480 5bbd3f7f Michael Hanselmann
      raise errors.ProgrammerError("Invalid choices element to AskUser")
1481 47988778 Iustin Pop
1482 47988778 Iustin Pop
  answer = choices[-1][1]
1483 47988778 Iustin Pop
  new_text = []
1484 47988778 Iustin Pop
  for line in text.splitlines():
1485 47988778 Iustin Pop
    new_text.append(textwrap.fill(line, 70, replace_whitespace=False))
1486 47988778 Iustin Pop
  text = "\n".join(new_text)
1487 a8083063 Iustin Pop
  try:
1488 3023170f Iustin Pop
    f = file("/dev/tty", "a+")
1489 a8083063 Iustin Pop
  except IOError:
1490 47988778 Iustin Pop
    return answer
1491 a8083063 Iustin Pop
  try:
1492 47988778 Iustin Pop
    chars = [entry[0] for entry in choices]
1493 47988778 Iustin Pop
    chars[-1] = "[%s]" % chars[-1]
1494 47988778 Iustin Pop
    chars.append('?')
1495 47988778 Iustin Pop
    maps = dict([(entry[0], entry[1]) for entry in choices])
1496 47988778 Iustin Pop
    while True:
1497 47988778 Iustin Pop
      f.write(text)
1498 47988778 Iustin Pop
      f.write('\n')
1499 47988778 Iustin Pop
      f.write("/".join(chars))
1500 47988778 Iustin Pop
      f.write(": ")
1501 47988778 Iustin Pop
      line = f.readline(2).strip().lower()
1502 47988778 Iustin Pop
      if line in maps:
1503 47988778 Iustin Pop
        answer = maps[line]
1504 47988778 Iustin Pop
        break
1505 47988778 Iustin Pop
      elif line == '?':
1506 47988778 Iustin Pop
        for entry in choices:
1507 47988778 Iustin Pop
          f.write(" %s - %s\n" % (entry[0], entry[2]))
1508 47988778 Iustin Pop
        f.write("\n")
1509 47988778 Iustin Pop
        continue
1510 a8083063 Iustin Pop
  finally:
1511 a8083063 Iustin Pop
    f.close()
1512 a8083063 Iustin Pop
  return answer
1513 a8083063 Iustin Pop
1514 a8083063 Iustin Pop
1515 e9d741b6 Iustin Pop
class JobSubmittedException(Exception):
1516 e9d741b6 Iustin Pop
  """Job was submitted, client should exit.
1517 e9d741b6 Iustin Pop

1518 e9d741b6 Iustin Pop
  This exception has one argument, the ID of the job that was
1519 e9d741b6 Iustin Pop
  submitted. The handler should print this ID.
1520 e9d741b6 Iustin Pop

1521 e9d741b6 Iustin Pop
  This is not an error, just a structured way to exit from clients.
1522 e9d741b6 Iustin Pop

1523 e9d741b6 Iustin Pop
  """
1524 e9d741b6 Iustin Pop
1525 e9d741b6 Iustin Pop
1526 0a1e74d9 Iustin Pop
def SendJob(ops, cl=None):
1527 0a1e74d9 Iustin Pop
  """Function to submit an opcode without waiting for the results.
1528 a8083063 Iustin Pop

1529 0a1e74d9 Iustin Pop
  @type ops: list
1530 0a1e74d9 Iustin Pop
  @param ops: list of opcodes
1531 0a1e74d9 Iustin Pop
  @type cl: luxi.Client
1532 0a1e74d9 Iustin Pop
  @param cl: the luxi client to use for communicating with the master;
1533 0a1e74d9 Iustin Pop
             if None, a new client will be created
1534 a8083063 Iustin Pop

1535 a8083063 Iustin Pop
  """
1536 e2212007 Iustin Pop
  if cl is None:
1537 b33e986b Iustin Pop
    cl = GetClient()
1538 685ee993 Iustin Pop
1539 0a1e74d9 Iustin Pop
  job_id = cl.SubmitJob(ops)
1540 0a1e74d9 Iustin Pop
1541 0a1e74d9 Iustin Pop
  return job_id
1542 0a1e74d9 Iustin Pop
1543 0a1e74d9 Iustin Pop
1544 4e338533 Michael Hanselmann
def GenericPollJob(job_id, cbs, report_cbs):
1545 4e338533 Michael Hanselmann
  """Generic job-polling function.
1546 0a1e74d9 Iustin Pop

1547 4e338533 Michael Hanselmann
  @type job_id: number
1548 4e338533 Michael Hanselmann
  @param job_id: Job ID
1549 4e338533 Michael Hanselmann
  @type cbs: Instance of L{JobPollCbBase}
1550 4e338533 Michael Hanselmann
  @param cbs: Data callbacks
1551 4e338533 Michael Hanselmann
  @type report_cbs: Instance of L{JobPollReportCbBase}
1552 4e338533 Michael Hanselmann
  @param report_cbs: Reporting callbacks
1553 0a1e74d9 Iustin Pop

1554 0a1e74d9 Iustin Pop
  """
1555 6c5a7090 Michael Hanselmann
  prev_job_info = None
1556 6c5a7090 Michael Hanselmann
  prev_logmsg_serial = None
1557 6c5a7090 Michael Hanselmann
1558 f4484122 Michael Hanselmann
  status = None
1559 f4484122 Michael Hanselmann
1560 685ee993 Iustin Pop
  while True:
1561 4e338533 Michael Hanselmann
    result = cbs.WaitForJobChangeOnce(job_id, ["status"], prev_job_info,
1562 4e338533 Michael Hanselmann
                                      prev_logmsg_serial)
1563 6c5a7090 Michael Hanselmann
    if not result:
1564 685ee993 Iustin Pop
      # job not found, go away!
1565 0bbe448c Michael Hanselmann
      raise errors.JobLost("Job with id %s lost" % job_id)
1566 4e338533 Michael Hanselmann
1567 4e338533 Michael Hanselmann
    if result == constants.JOB_NOTCHANGED:
1568 4e338533 Michael Hanselmann
      report_cbs.ReportNotChanged(job_id, status)
1569 f4484122 Michael Hanselmann
1570 f4484122 Michael Hanselmann
      # Wait again
1571 f4484122 Michael Hanselmann
      continue
1572 685ee993 Iustin Pop
1573 6c5a7090 Michael Hanselmann
    # Split result, a tuple of (field values, log entries)
1574 6c5a7090 Michael Hanselmann
    (job_info, log_entries) = result
1575 6c5a7090 Michael Hanselmann
    (status, ) = job_info
1576 6c5a7090 Michael Hanselmann
1577 6c5a7090 Michael Hanselmann
    if log_entries:
1578 6c5a7090 Michael Hanselmann
      for log_entry in log_entries:
1579 4e338533 Michael Hanselmann
        (serial, timestamp, log_type, message) = log_entry
1580 4e338533 Michael Hanselmann
        report_cbs.ReportLogMessage(job_id, serial, timestamp,
1581 4e338533 Michael Hanselmann
                                    log_type, message)
1582 6c5a7090 Michael Hanselmann
        prev_logmsg_serial = max(prev_logmsg_serial, serial)
1583 6c5a7090 Michael Hanselmann
1584 0bbe448c Michael Hanselmann
    # TODO: Handle canceled and archived jobs
1585 fbf0262f Michael Hanselmann
    elif status in (constants.JOB_STATUS_SUCCESS,
1586 fbf0262f Michael Hanselmann
                    constants.JOB_STATUS_ERROR,
1587 fbf0262f Michael Hanselmann
                    constants.JOB_STATUS_CANCELING,
1588 fbf0262f Michael Hanselmann
                    constants.JOB_STATUS_CANCELED):
1589 685ee993 Iustin Pop
      break
1590 6c5a7090 Michael Hanselmann
1591 6c5a7090 Michael Hanselmann
    prev_job_info = job_info
1592 685ee993 Iustin Pop
1593 4e338533 Michael Hanselmann
  jobs = cbs.QueryJobs([job_id], ["status", "opstatus", "opresult"])
1594 0bbe448c Michael Hanselmann
  if not jobs:
1595 0bbe448c Michael Hanselmann
    raise errors.JobLost("Job with id %s lost" % job_id)
1596 685ee993 Iustin Pop
1597 0e050889 Iustin Pop
  status, opstatus, result = jobs[0]
1598 4e338533 Michael Hanselmann
1599 0bbe448c Michael Hanselmann
  if status == constants.JOB_STATUS_SUCCESS:
1600 53c04d04 Iustin Pop
    return result
1601 4e338533 Michael Hanselmann
1602 4e338533 Michael Hanselmann
  if status in (constants.JOB_STATUS_CANCELING, constants.JOB_STATUS_CANCELED):
1603 fbf0262f Michael Hanselmann
    raise errors.OpExecError("Job was canceled")
1604 4e338533 Michael Hanselmann
1605 4e338533 Michael Hanselmann
  has_ok = False
1606 4e338533 Michael Hanselmann
  for idx, (status, msg) in enumerate(zip(opstatus, result)):
1607 4e338533 Michael Hanselmann
    if status == constants.OP_STATUS_SUCCESS:
1608 4e338533 Michael Hanselmann
      has_ok = True
1609 4e338533 Michael Hanselmann
    elif status == constants.OP_STATUS_ERROR:
1610 4e338533 Michael Hanselmann
      errors.MaybeRaise(msg)
1611 4e338533 Michael Hanselmann
1612 4e338533 Michael Hanselmann
      if has_ok:
1613 4e338533 Michael Hanselmann
        raise errors.OpExecError("partial failure (opcode %d): %s" %
1614 4e338533 Michael Hanselmann
                                 (idx, msg))
1615 4e338533 Michael Hanselmann
1616 4e338533 Michael Hanselmann
      raise errors.OpExecError(str(msg))
1617 4e338533 Michael Hanselmann
1618 4e338533 Michael Hanselmann
  # default failure mode
1619 4e338533 Michael Hanselmann
  raise errors.OpExecError(result)
1620 4e338533 Michael Hanselmann
1621 4e338533 Michael Hanselmann
1622 4e338533 Michael Hanselmann
class JobPollCbBase:
1623 4e338533 Michael Hanselmann
  """Base class for L{GenericPollJob} callbacks.
1624 4e338533 Michael Hanselmann

1625 4e338533 Michael Hanselmann
  """
1626 4e338533 Michael Hanselmann
  def __init__(self):
1627 4e338533 Michael Hanselmann
    """Initializes this class.
1628 4e338533 Michael Hanselmann

1629 4e338533 Michael Hanselmann
    """
1630 4e338533 Michael Hanselmann
1631 4e338533 Michael Hanselmann
  def WaitForJobChangeOnce(self, job_id, fields,
1632 4e338533 Michael Hanselmann
                           prev_job_info, prev_log_serial):
1633 4e338533 Michael Hanselmann
    """Waits for changes on a job.
1634 4e338533 Michael Hanselmann

1635 4e338533 Michael Hanselmann
    """
1636 4e338533 Michael Hanselmann
    raise NotImplementedError()
1637 4e338533 Michael Hanselmann
1638 4e338533 Michael Hanselmann
  def QueryJobs(self, job_ids, fields):
1639 4e338533 Michael Hanselmann
    """Returns the selected fields for the selected job IDs.
1640 4e338533 Michael Hanselmann

1641 4e338533 Michael Hanselmann
    @type job_ids: list of numbers
1642 4e338533 Michael Hanselmann
    @param job_ids: Job IDs
1643 4e338533 Michael Hanselmann
    @type fields: list of strings
1644 4e338533 Michael Hanselmann
    @param fields: Fields
1645 4e338533 Michael Hanselmann

1646 4e338533 Michael Hanselmann
    """
1647 4e338533 Michael Hanselmann
    raise NotImplementedError()
1648 4e338533 Michael Hanselmann
1649 4e338533 Michael Hanselmann
1650 4e338533 Michael Hanselmann
class JobPollReportCbBase:
1651 4e338533 Michael Hanselmann
  """Base class for L{GenericPollJob} reporting callbacks.
1652 4e338533 Michael Hanselmann

1653 4e338533 Michael Hanselmann
  """
1654 4e338533 Michael Hanselmann
  def __init__(self):
1655 4e338533 Michael Hanselmann
    """Initializes this class.
1656 4e338533 Michael Hanselmann

1657 4e338533 Michael Hanselmann
    """
1658 4e338533 Michael Hanselmann
1659 4e338533 Michael Hanselmann
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
1660 4e338533 Michael Hanselmann
    """Handles a log message.
1661 4e338533 Michael Hanselmann

1662 4e338533 Michael Hanselmann
    """
1663 4e338533 Michael Hanselmann
    raise NotImplementedError()
1664 4e338533 Michael Hanselmann
1665 4e338533 Michael Hanselmann
  def ReportNotChanged(self, job_id, status):
1666 4e338533 Michael Hanselmann
    """Called for if a job hasn't changed in a while.
1667 4e338533 Michael Hanselmann

1668 4e338533 Michael Hanselmann
    @type job_id: number
1669 4e338533 Michael Hanselmann
    @param job_id: Job ID
1670 4e338533 Michael Hanselmann
    @type status: string or None
1671 4e338533 Michael Hanselmann
    @param status: Job status if available
1672 4e338533 Michael Hanselmann

1673 4e338533 Michael Hanselmann
    """
1674 4e338533 Michael Hanselmann
    raise NotImplementedError()
1675 4e338533 Michael Hanselmann
1676 4e338533 Michael Hanselmann
1677 4e338533 Michael Hanselmann
class _LuxiJobPollCb(JobPollCbBase):
1678 4e338533 Michael Hanselmann
  def __init__(self, cl):
1679 4e338533 Michael Hanselmann
    """Initializes this class.
1680 4e338533 Michael Hanselmann

1681 4e338533 Michael Hanselmann
    """
1682 4e338533 Michael Hanselmann
    JobPollCbBase.__init__(self)
1683 4e338533 Michael Hanselmann
    self.cl = cl
1684 4e338533 Michael Hanselmann
1685 4e338533 Michael Hanselmann
  def WaitForJobChangeOnce(self, job_id, fields,
1686 4e338533 Michael Hanselmann
                           prev_job_info, prev_log_serial):
1687 4e338533 Michael Hanselmann
    """Waits for changes on a job.
1688 4e338533 Michael Hanselmann

1689 4e338533 Michael Hanselmann
    """
1690 4e338533 Michael Hanselmann
    return self.cl.WaitForJobChangeOnce(job_id, fields,
1691 4e338533 Michael Hanselmann
                                        prev_job_info, prev_log_serial)
1692 4e338533 Michael Hanselmann
1693 4e338533 Michael Hanselmann
  def QueryJobs(self, job_ids, fields):
1694 4e338533 Michael Hanselmann
    """Returns the selected fields for the selected job IDs.
1695 4e338533 Michael Hanselmann

1696 4e338533 Michael Hanselmann
    """
1697 4e338533 Michael Hanselmann
    return self.cl.QueryJobs(job_ids, fields)
1698 4e338533 Michael Hanselmann
1699 4e338533 Michael Hanselmann
1700 4e338533 Michael Hanselmann
class FeedbackFnJobPollReportCb(JobPollReportCbBase):
1701 4e338533 Michael Hanselmann
  def __init__(self, feedback_fn):
1702 4e338533 Michael Hanselmann
    """Initializes this class.
1703 4e338533 Michael Hanselmann

1704 4e338533 Michael Hanselmann
    """
1705 4e338533 Michael Hanselmann
    JobPollReportCbBase.__init__(self)
1706 4e338533 Michael Hanselmann
1707 4e338533 Michael Hanselmann
    self.feedback_fn = feedback_fn
1708 4e338533 Michael Hanselmann
1709 4e338533 Michael Hanselmann
    assert callable(feedback_fn)
1710 4e338533 Michael Hanselmann
1711 4e338533 Michael Hanselmann
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
1712 4e338533 Michael Hanselmann
    """Handles a log message.
1713 4e338533 Michael Hanselmann

1714 4e338533 Michael Hanselmann
    """
1715 4e338533 Michael Hanselmann
    self.feedback_fn((timestamp, log_type, log_msg))
1716 4e338533 Michael Hanselmann
1717 4e338533 Michael Hanselmann
  def ReportNotChanged(self, job_id, status):
1718 4e338533 Michael Hanselmann
    """Called if a job hasn't changed in a while.
1719 4e338533 Michael Hanselmann

1720 4e338533 Michael Hanselmann
    """
1721 4e338533 Michael Hanselmann
    # Ignore
1722 4e338533 Michael Hanselmann
1723 4e338533 Michael Hanselmann
1724 4e338533 Michael Hanselmann
class StdioJobPollReportCb(JobPollReportCbBase):
1725 4e338533 Michael Hanselmann
  def __init__(self):
1726 4e338533 Michael Hanselmann
    """Initializes this class.
1727 4e338533 Michael Hanselmann

1728 4e338533 Michael Hanselmann
    """
1729 4e338533 Michael Hanselmann
    JobPollReportCbBase.__init__(self)
1730 4e338533 Michael Hanselmann
1731 4e338533 Michael Hanselmann
    self.notified_queued = False
1732 4e338533 Michael Hanselmann
    self.notified_waitlock = False
1733 4e338533 Michael Hanselmann
1734 4e338533 Michael Hanselmann
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
1735 4e338533 Michael Hanselmann
    """Handles a log message.
1736 4e338533 Michael Hanselmann

1737 4e338533 Michael Hanselmann
    """
1738 4e338533 Michael Hanselmann
    ToStdout("%s %s", time.ctime(utils.MergeTime(timestamp)),
1739 8a7f1c61 Michael Hanselmann
             FormatLogMessage(log_type, log_msg))
1740 4e338533 Michael Hanselmann
1741 4e338533 Michael Hanselmann
  def ReportNotChanged(self, job_id, status):
1742 4e338533 Michael Hanselmann
    """Called if a job hasn't changed in a while.
1743 4e338533 Michael Hanselmann

1744 4e338533 Michael Hanselmann
    """
1745 4e338533 Michael Hanselmann
    if status is None:
1746 4e338533 Michael Hanselmann
      return
1747 4e338533 Michael Hanselmann
1748 4e338533 Michael Hanselmann
    if status == constants.JOB_STATUS_QUEUED and not self.notified_queued:
1749 4e338533 Michael Hanselmann
      ToStderr("Job %s is waiting in queue", job_id)
1750 4e338533 Michael Hanselmann
      self.notified_queued = True
1751 4e338533 Michael Hanselmann
1752 4e338533 Michael Hanselmann
    elif status == constants.JOB_STATUS_WAITLOCK and not self.notified_waitlock:
1753 4e338533 Michael Hanselmann
      ToStderr("Job %s is trying to acquire all necessary locks", job_id)
1754 4e338533 Michael Hanselmann
      self.notified_waitlock = True
1755 4e338533 Michael Hanselmann
1756 4e338533 Michael Hanselmann
1757 8a7f1c61 Michael Hanselmann
def FormatLogMessage(log_type, log_msg):
1758 8a7f1c61 Michael Hanselmann
  """Formats a job message according to its type.
1759 8a7f1c61 Michael Hanselmann

1760 8a7f1c61 Michael Hanselmann
  """
1761 8a7f1c61 Michael Hanselmann
  if log_type != constants.ELOG_MESSAGE:
1762 8a7f1c61 Michael Hanselmann
    log_msg = str(log_msg)
1763 8a7f1c61 Michael Hanselmann
1764 8a7f1c61 Michael Hanselmann
  return utils.SafeEncode(log_msg)
1765 8a7f1c61 Michael Hanselmann
1766 8a7f1c61 Michael Hanselmann
1767 583163a6 Michael Hanselmann
def PollJob(job_id, cl=None, feedback_fn=None, reporter=None):
1768 4e338533 Michael Hanselmann
  """Function to poll for the result of a job.
1769 4e338533 Michael Hanselmann

1770 4e338533 Michael Hanselmann
  @type job_id: job identified
1771 4e338533 Michael Hanselmann
  @param job_id: the job to poll for results
1772 4e338533 Michael Hanselmann
  @type cl: luxi.Client
1773 4e338533 Michael Hanselmann
  @param cl: the luxi client to use for communicating with the master;
1774 4e338533 Michael Hanselmann
             if None, a new client will be created
1775 4e338533 Michael Hanselmann

1776 4e338533 Michael Hanselmann
  """
1777 4e338533 Michael Hanselmann
  if cl is None:
1778 4e338533 Michael Hanselmann
    cl = GetClient()
1779 4e338533 Michael Hanselmann
1780 583163a6 Michael Hanselmann
  if reporter is None:
1781 583163a6 Michael Hanselmann
    if feedback_fn:
1782 583163a6 Michael Hanselmann
      reporter = FeedbackFnJobPollReportCb(feedback_fn)
1783 583163a6 Michael Hanselmann
    else:
1784 583163a6 Michael Hanselmann
      reporter = StdioJobPollReportCb()
1785 583163a6 Michael Hanselmann
  elif feedback_fn:
1786 583163a6 Michael Hanselmann
    raise errors.ProgrammerError("Can't specify reporter and feedback function")
1787 4e338533 Michael Hanselmann
1788 4e338533 Michael Hanselmann
  return GenericPollJob(job_id, _LuxiJobPollCb(cl), reporter)
1789 ceab32dd Iustin Pop
1790 ceab32dd Iustin Pop
1791 583163a6 Michael Hanselmann
def SubmitOpCode(op, cl=None, feedback_fn=None, opts=None, reporter=None):
1792 0a1e74d9 Iustin Pop
  """Legacy function to submit an opcode.
1793 0a1e74d9 Iustin Pop

1794 0a1e74d9 Iustin Pop
  This is just a simple wrapper over the construction of the processor
1795 0a1e74d9 Iustin Pop
  instance. It should be extended to better handle feedback and
1796 0a1e74d9 Iustin Pop
  interaction functions.
1797 0a1e74d9 Iustin Pop

1798 0a1e74d9 Iustin Pop
  """
1799 0a1e74d9 Iustin Pop
  if cl is None:
1800 0a1e74d9 Iustin Pop
    cl = GetClient()
1801 0a1e74d9 Iustin Pop
1802 293ba2d8 Iustin Pop
  SetGenericOpcodeOpts([op], opts)
1803 293ba2d8 Iustin Pop
1804 5d297d8a Michael Hanselmann
  job_id = SendJob([op], cl=cl)
1805 0a1e74d9 Iustin Pop
1806 583163a6 Michael Hanselmann
  op_results = PollJob(job_id, cl=cl, feedback_fn=feedback_fn,
1807 583163a6 Michael Hanselmann
                       reporter=reporter)
1808 53c04d04 Iustin Pop
1809 53c04d04 Iustin Pop
  return op_results[0]
1810 0a1e74d9 Iustin Pop
1811 0a1e74d9 Iustin Pop
1812 94428652 Iustin Pop
def SubmitOrSend(op, opts, cl=None, feedback_fn=None):
1813 94428652 Iustin Pop
  """Wrapper around SubmitOpCode or SendJob.
1814 94428652 Iustin Pop

1815 94428652 Iustin Pop
  This function will decide, based on the 'opts' parameter, whether to
1816 94428652 Iustin Pop
  submit and wait for the result of the opcode (and return it), or
1817 94428652 Iustin Pop
  whether to just send the job and print its identifier. It is used in
1818 94428652 Iustin Pop
  order to simplify the implementation of the '--submit' option.
1819 94428652 Iustin Pop

1820 293ba2d8 Iustin Pop
  It will also process the opcodes if we're sending the via SendJob
1821 293ba2d8 Iustin Pop
  (otherwise SubmitOpCode does it).
1822 64c65a2a Iustin Pop

1823 94428652 Iustin Pop
  """
1824 94428652 Iustin Pop
  if opts and opts.submit_only:
1825 293ba2d8 Iustin Pop
    job = [op]
1826 293ba2d8 Iustin Pop
    SetGenericOpcodeOpts(job, opts)
1827 293ba2d8 Iustin Pop
    job_id = SendJob(job, cl=cl)
1828 e9d741b6 Iustin Pop
    raise JobSubmittedException(job_id)
1829 94428652 Iustin Pop
  else:
1830 293ba2d8 Iustin Pop
    return SubmitOpCode(op, cl=cl, feedback_fn=feedback_fn, opts=opts)
1831 293ba2d8 Iustin Pop
1832 293ba2d8 Iustin Pop
1833 293ba2d8 Iustin Pop
def SetGenericOpcodeOpts(opcode_list, options):
1834 293ba2d8 Iustin Pop
  """Processor for generic options.
1835 293ba2d8 Iustin Pop

1836 293ba2d8 Iustin Pop
  This function updates the given opcodes based on generic command
1837 293ba2d8 Iustin Pop
  line options (like debug, dry-run, etc.).
1838 293ba2d8 Iustin Pop

1839 293ba2d8 Iustin Pop
  @param opcode_list: list of opcodes
1840 293ba2d8 Iustin Pop
  @param options: command line options or None
1841 293ba2d8 Iustin Pop
  @return: None (in-place modification)
1842 293ba2d8 Iustin Pop

1843 293ba2d8 Iustin Pop
  """
1844 293ba2d8 Iustin Pop
  if not options:
1845 293ba2d8 Iustin Pop
    return
1846 293ba2d8 Iustin Pop
  for op in opcode_list:
1847 aa06f8c6 Michael Hanselmann
    op.debug_level = options.debug
1848 a0a6ff34 Iustin Pop
    if hasattr(options, "dry_run"):
1849 a0a6ff34 Iustin Pop
      op.dry_run = options.dry_run
1850 aa06f8c6 Michael Hanselmann
    if getattr(options, "priority", None) is not None:
1851 aa06f8c6 Michael Hanselmann
      op.priority = _PRIONAME_TO_VALUE[options.priority]
1852 94428652 Iustin Pop
1853 94428652 Iustin Pop
1854 af30b2fd Michael Hanselmann
def GetClient():
1855 af30b2fd Michael Hanselmann
  # TODO: Cache object?
1856 b33e986b Iustin Pop
  try:
1857 b33e986b Iustin Pop
    client = luxi.Client()
1858 b33e986b Iustin Pop
  except luxi.NoMasterError:
1859 d9a51679 Michael Hanselmann
    ss = ssconf.SimpleStore()
1860 d9a51679 Michael Hanselmann
1861 d9a51679 Michael Hanselmann
    # Try to read ssconf file
1862 d9a51679 Michael Hanselmann
    try:
1863 d9a51679 Michael Hanselmann
      ss.GetMasterNode()
1864 d9a51679 Michael Hanselmann
    except errors.ConfigurationError:
1865 d9a51679 Michael Hanselmann
      raise errors.OpPrereqError("Cluster not initialized or this machine is"
1866 d9a51679 Michael Hanselmann
                                 " not part of a cluster")
1867 d9a51679 Michael Hanselmann
1868 d9a51679 Michael Hanselmann
    master, myself = ssconf.GetMasterAndMyself(ss=ss)
1869 b33e986b Iustin Pop
    if master != myself:
1870 b33e986b Iustin Pop
      raise errors.OpPrereqError("This is not the master node, please connect"
1871 b33e986b Iustin Pop
                                 " to node '%s' and rerun the command" %
1872 b33e986b Iustin Pop
                                 master)
1873 d9a51679 Michael Hanselmann
    raise
1874 b33e986b Iustin Pop
  return client
1875 af30b2fd Michael Hanselmann
1876 af30b2fd Michael Hanselmann
1877 73702ee7 Iustin Pop
def FormatError(err):
1878 73702ee7 Iustin Pop
  """Return a formatted error message for a given error.
1879 73702ee7 Iustin Pop

1880 73702ee7 Iustin Pop
  This function takes an exception instance and returns a tuple
1881 73702ee7 Iustin Pop
  consisting of two values: first, the recommended exit code, and
1882 73702ee7 Iustin Pop
  second, a string describing the error message (not
1883 73702ee7 Iustin Pop
  newline-terminated).
1884 73702ee7 Iustin Pop

1885 73702ee7 Iustin Pop
  """
1886 73702ee7 Iustin Pop
  retcode = 1
1887 73702ee7 Iustin Pop
  obuf = StringIO()
1888 e2e521d0 Iustin Pop
  msg = str(err)
1889 73702ee7 Iustin Pop
  if isinstance(err, errors.ConfigurationError):
1890 e2e521d0 Iustin Pop
    txt = "Corrupt configuration file: %s" % msg
1891 46fbdd04 Iustin Pop
    logging.error(txt)
1892 e2e521d0 Iustin Pop
    obuf.write(txt + "\n")
1893 73702ee7 Iustin Pop
    obuf.write("Aborting.")
1894 73702ee7 Iustin Pop
    retcode = 2
1895 73702ee7 Iustin Pop
  elif isinstance(err, errors.HooksAbort):
1896 73702ee7 Iustin Pop
    obuf.write("Failure: hooks execution failed:\n")
1897 73702ee7 Iustin Pop
    for node, script, out in err.args[0]:
1898 73702ee7 Iustin Pop
      if out:
1899 73702ee7 Iustin Pop
        obuf.write("  node: %s, script: %s, output: %s\n" %
1900 73702ee7 Iustin Pop
                   (node, script, out))
1901 73702ee7 Iustin Pop
      else:
1902 73702ee7 Iustin Pop
        obuf.write("  node: %s, script: %s (no output)\n" %
1903 73702ee7 Iustin Pop
                   (node, script))
1904 73702ee7 Iustin Pop
  elif isinstance(err, errors.HooksFailure):
1905 e2e521d0 Iustin Pop
    obuf.write("Failure: hooks general failure: %s" % msg)
1906 73702ee7 Iustin Pop
  elif isinstance(err, errors.ResolverError):
1907 b705c7a6 Manuel Franceschini
    this_host = netutils.Hostname.GetSysName()
1908 73702ee7 Iustin Pop
    if err.args[0] == this_host:
1909 73702ee7 Iustin Pop
      msg = "Failure: can't resolve my own hostname ('%s')"
1910 73702ee7 Iustin Pop
    else:
1911 73702ee7 Iustin Pop
      msg = "Failure: can't resolve hostname '%s'"
1912 73702ee7 Iustin Pop
    obuf.write(msg % err.args[0])
1913 73702ee7 Iustin Pop
  elif isinstance(err, errors.OpPrereqError):
1914 5c983ee5 Iustin Pop
    if len(err.args) == 2:
1915 5c983ee5 Iustin Pop
      obuf.write("Failure: prerequisites not met for this"
1916 5c983ee5 Iustin Pop
               " operation:\nerror type: %s, error details:\n%s" %
1917 5c983ee5 Iustin Pop
                 (err.args[1], err.args[0]))
1918 5c983ee5 Iustin Pop
    else:
1919 5c983ee5 Iustin Pop
      obuf.write("Failure: prerequisites not met for this"
1920 5c983ee5 Iustin Pop
                 " operation:\n%s" % msg)
1921 73702ee7 Iustin Pop
  elif isinstance(err, errors.OpExecError):
1922 e2e521d0 Iustin Pop
    obuf.write("Failure: command execution error:\n%s" % msg)
1923 73702ee7 Iustin Pop
  elif isinstance(err, errors.TagError):
1924 e2e521d0 Iustin Pop
    obuf.write("Failure: invalid tag(s) given:\n%s" % msg)
1925 686d7433 Iustin Pop
  elif isinstance(err, errors.JobQueueDrainError):
1926 686d7433 Iustin Pop
    obuf.write("Failure: the job queue is marked for drain and doesn't"
1927 686d7433 Iustin Pop
               " accept new requests\n")
1928 f87b405e Michael Hanselmann
  elif isinstance(err, errors.JobQueueFull):
1929 f87b405e Michael Hanselmann
    obuf.write("Failure: the job queue is full and doesn't accept new"
1930 f87b405e Michael Hanselmann
               " job submissions until old jobs are archived\n")
1931 a5728081 Guido Trotter
  elif isinstance(err, errors.TypeEnforcementError):
1932 a5728081 Guido Trotter
    obuf.write("Parameter Error: %s" % msg)
1933 c1ce76bb Iustin Pop
  elif isinstance(err, errors.ParameterError):
1934 c1ce76bb Iustin Pop
    obuf.write("Failure: unknown/wrong parameter name '%s'" % msg)
1935 03a8dbdc Iustin Pop
  elif isinstance(err, luxi.NoMasterError):
1936 03a8dbdc Iustin Pop
    obuf.write("Cannot communicate with the master daemon.\nIs it running"
1937 082c5adb Michael Hanselmann
               " and listening for connections?")
1938 03a8dbdc Iustin Pop
  elif isinstance(err, luxi.TimeoutError):
1939 cd4c86a8 Michael Hanselmann
    obuf.write("Timeout while talking to the master daemon. Jobs might have"
1940 cd4c86a8 Michael Hanselmann
               " been submitted and will continue to run even if the call"
1941 cd4c86a8 Michael Hanselmann
               " timed out. Useful commands in this situation are \"gnt-job"
1942 cd4c86a8 Michael Hanselmann
               " list\", \"gnt-job cancel\" and \"gnt-job watch\". Error:\n")
1943 cd4c86a8 Michael Hanselmann
    obuf.write(msg)
1944 5a1c22fe Iustin Pop
  elif isinstance(err, luxi.PermissionError):
1945 5a1c22fe Iustin Pop
    obuf.write("It seems you don't have permissions to connect to the"
1946 5a1c22fe Iustin Pop
               " master daemon.\nPlease retry as a different user.")
1947 03a8dbdc Iustin Pop
  elif isinstance(err, luxi.ProtocolError):
1948 03a8dbdc Iustin Pop
    obuf.write("Unhandled protocol error while talking to the master daemon:\n"
1949 03a8dbdc Iustin Pop
               "%s" % msg)
1950 91c622a8 Iustin Pop
  elif isinstance(err, errors.JobLost):
1951 91c622a8 Iustin Pop
    obuf.write("Error checking job status: %s" % msg)
1952 cb1e6c3c Michael Hanselmann
  elif isinstance(err, errors.QueryFilterParseError):
1953 cb1e6c3c Michael Hanselmann
    obuf.write("Error while parsing query filter: %s\n" % err.args[0])
1954 cb1e6c3c Michael Hanselmann
    obuf.write("\n".join(err.GetDetails()))
1955 797506fc Michael Hanselmann
  elif isinstance(err, errors.GenericError):
1956 797506fc Michael Hanselmann
    obuf.write("Unhandled Ganeti error: %s" % msg)
1957 e9d741b6 Iustin Pop
  elif isinstance(err, JobSubmittedException):
1958 e9d741b6 Iustin Pop
    obuf.write("JobID: %s\n" % err.args[0])
1959 e9d741b6 Iustin Pop
    retcode = 0
1960 73702ee7 Iustin Pop
  else:
1961 e2e521d0 Iustin Pop
    obuf.write("Unhandled exception: %s" % msg)
1962 73702ee7 Iustin Pop
  return retcode, obuf.getvalue().rstrip('\n')
1963 73702ee7 Iustin Pop
1964 73702ee7 Iustin Pop
1965 de47cf8f Guido Trotter
def GenericMain(commands, override=None, aliases=None):
1966 a8083063 Iustin Pop
  """Generic main function for all the gnt-* commands.
1967 a8083063 Iustin Pop

1968 334d1483 Iustin Pop
  Arguments:
1969 334d1483 Iustin Pop
    - commands: a dictionary with a special structure, see the design doc
1970 334d1483 Iustin Pop
                for command line handling.
1971 334d1483 Iustin Pop
    - override: if not None, we expect a dictionary with keys that will
1972 334d1483 Iustin Pop
                override command line options; this can be used to pass
1973 334d1483 Iustin Pop
                options from the scripts to generic functions
1974 de47cf8f Guido Trotter
    - aliases: dictionary with command aliases {'alias': 'target, ...}
1975 a8083063 Iustin Pop

1976 a8083063 Iustin Pop
  """
1977 a8083063 Iustin Pop
  # save the program name and the entire command line for later logging
1978 a8083063 Iustin Pop
  if sys.argv:
1979 a8083063 Iustin Pop
    binary = os.path.basename(sys.argv[0]) or sys.argv[0]
1980 a8083063 Iustin Pop
    if len(sys.argv) >= 2:
1981 a8083063 Iustin Pop
      binary += " " + sys.argv[1]
1982 a8083063 Iustin Pop
      old_cmdline = " ".join(sys.argv[2:])
1983 a8083063 Iustin Pop
    else:
1984 a8083063 Iustin Pop
      old_cmdline = ""
1985 a8083063 Iustin Pop
  else:
1986 a8083063 Iustin Pop
    binary = "<unknown program>"
1987 a8083063 Iustin Pop
    old_cmdline = ""
1988 a8083063 Iustin Pop
1989 de47cf8f Guido Trotter
  if aliases is None:
1990 de47cf8f Guido Trotter
    aliases = {}
1991 de47cf8f Guido Trotter
1992 3126878d Guido Trotter
  try:
1993 3126878d Guido Trotter
    func, options, args = _ParseArgs(sys.argv, commands, aliases)
1994 3126878d Guido Trotter
  except errors.ParameterError, err:
1995 3126878d Guido Trotter
    result, err_msg = FormatError(err)
1996 3126878d Guido Trotter
    ToStderr(err_msg)
1997 3126878d Guido Trotter
    return 1
1998 3126878d Guido Trotter
1999 a8083063 Iustin Pop
  if func is None: # parse error
2000 a8083063 Iustin Pop
    return 1
2001 a8083063 Iustin Pop
2002 334d1483 Iustin Pop
  if override is not None:
2003 334d1483 Iustin Pop
    for key, val in override.iteritems():
2004 334d1483 Iustin Pop
      setattr(options, key, val)
2005 334d1483 Iustin Pop
2006 cfcc79c6 Michael Hanselmann
  utils.SetupLogging(constants.LOG_COMMANDS, binary, debug=options.debug,
2007 cfcc79c6 Michael Hanselmann
                     stderr_logging=True)
2008 a8083063 Iustin Pop
2009 a8083063 Iustin Pop
  if old_cmdline:
2010 46fbdd04 Iustin Pop
    logging.info("run with arguments '%s'", old_cmdline)
2011 a8083063 Iustin Pop
  else:
2012 46fbdd04 Iustin Pop
    logging.info("run with no arguments")
2013 a8083063 Iustin Pop
2014 a8083063 Iustin Pop
  try:
2015 a4af651e Iustin Pop
    result = func(options, args)
2016 d8353c3a Iustin Pop
  except (errors.GenericError, luxi.ProtocolError,
2017 d8353c3a Iustin Pop
          JobSubmittedException), err:
2018 a4af651e Iustin Pop
    result, err_msg = FormatError(err)
2019 5bbd3f7f Michael Hanselmann
    logging.exception("Error during command processing")
2020 46fbdd04 Iustin Pop
    ToStderr(err_msg)
2021 8a53b55f Iustin Pop
  except KeyboardInterrupt:
2022 8a53b55f Iustin Pop
    result = constants.EXIT_FAILURE
2023 8a53b55f Iustin Pop
    ToStderr("Aborted. Note that if the operation created any jobs, they"
2024 8a53b55f Iustin Pop
             " might have been submitted and"
2025 8a53b55f Iustin Pop
             " will continue to run in the background.")
2026 225e2544 Iustin Pop
  except IOError, err:
2027 225e2544 Iustin Pop
    if err.errno == errno.EPIPE:
2028 225e2544 Iustin Pop
      # our terminal went away, we'll exit
2029 225e2544 Iustin Pop
      sys.exit(constants.EXIT_FAILURE)
2030 225e2544 Iustin Pop
    else:
2031 225e2544 Iustin Pop
      raise
2032 a8083063 Iustin Pop
2033 a8083063 Iustin Pop
  return result
2034 137161c9 Michael Hanselmann
2035 137161c9 Michael Hanselmann
2036 845c79d8 Michael Hanselmann
def ParseNicOption(optvalue):
2037 845c79d8 Michael Hanselmann
  """Parses the value of the --net option(s).
2038 845c79d8 Michael Hanselmann

2039 845c79d8 Michael Hanselmann
  """
2040 845c79d8 Michael Hanselmann
  try:
2041 845c79d8 Michael Hanselmann
    nic_max = max(int(nidx[0]) + 1 for nidx in optvalue)
2042 845c79d8 Michael Hanselmann
  except (TypeError, ValueError), err:
2043 845c79d8 Michael Hanselmann
    raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err))
2044 845c79d8 Michael Hanselmann
2045 845c79d8 Michael Hanselmann
  nics = [{}] * nic_max
2046 845c79d8 Michael Hanselmann
  for nidx, ndict in optvalue:
2047 845c79d8 Michael Hanselmann
    nidx = int(nidx)
2048 845c79d8 Michael Hanselmann
2049 845c79d8 Michael Hanselmann
    if not isinstance(ndict, dict):
2050 845c79d8 Michael Hanselmann
      raise errors.OpPrereqError("Invalid nic/%d value: expected dict,"
2051 845c79d8 Michael Hanselmann
                                 " got %s" % (nidx, ndict))
2052 845c79d8 Michael Hanselmann
2053 845c79d8 Michael Hanselmann
    utils.ForceDictType(ndict, constants.INIC_PARAMS_TYPES)
2054 845c79d8 Michael Hanselmann
2055 845c79d8 Michael Hanselmann
    nics[nidx] = ndict
2056 845c79d8 Michael Hanselmann
2057 845c79d8 Michael Hanselmann
  return nics
2058 845c79d8 Michael Hanselmann
2059 845c79d8 Michael Hanselmann
2060 d77490c5 Iustin Pop
def GenericInstanceCreate(mode, opts, args):
2061 d77490c5 Iustin Pop
  """Add an instance to the cluster via either creation or import.
2062 d77490c5 Iustin Pop

2063 d77490c5 Iustin Pop
  @param mode: constants.INSTANCE_CREATE or constants.INSTANCE_IMPORT
2064 d77490c5 Iustin Pop
  @param opts: the command line options selected by the user
2065 d77490c5 Iustin Pop
  @type args: list
2066 d77490c5 Iustin Pop
  @param args: should contain only one element, the new instance name
2067 d77490c5 Iustin Pop
  @rtype: int
2068 d77490c5 Iustin Pop
  @return: the desired exit code
2069 d77490c5 Iustin Pop

2070 d77490c5 Iustin Pop
  """
2071 d77490c5 Iustin Pop
  instance = args[0]
2072 d77490c5 Iustin Pop
2073 d77490c5 Iustin Pop
  (pnode, snode) = SplitNodeOption(opts.node)
2074 d77490c5 Iustin Pop
2075 d77490c5 Iustin Pop
  hypervisor = None
2076 d77490c5 Iustin Pop
  hvparams = {}
2077 d77490c5 Iustin Pop
  if opts.hypervisor:
2078 d77490c5 Iustin Pop
    hypervisor, hvparams = opts.hypervisor
2079 d77490c5 Iustin Pop
2080 d77490c5 Iustin Pop
  if opts.nics:
2081 845c79d8 Michael Hanselmann
    nics = ParseNicOption(opts.nics)
2082 d77490c5 Iustin Pop
  elif opts.no_nics:
2083 d77490c5 Iustin Pop
    # no nics
2084 d77490c5 Iustin Pop
    nics = []
2085 0af0f641 Iustin Pop
  elif mode == constants.INSTANCE_CREATE:
2086 d77490c5 Iustin Pop
    # default of one nic, all auto
2087 d77490c5 Iustin Pop
    nics = [{}]
2088 0af0f641 Iustin Pop
  else:
2089 0af0f641 Iustin Pop
    # mode == import
2090 0af0f641 Iustin Pop
    nics = []
2091 d77490c5 Iustin Pop
2092 d77490c5 Iustin Pop
  if opts.disk_template == constants.DT_DISKLESS:
2093 d77490c5 Iustin Pop
    if opts.disks or opts.sd_size is not None:
2094 d77490c5 Iustin Pop
      raise errors.OpPrereqError("Diskless instance but disk"
2095 d77490c5 Iustin Pop
                                 " information passed")
2096 d77490c5 Iustin Pop
    disks = []
2097 d77490c5 Iustin Pop
  else:
2098 9b12ed0f Iustin Pop
    if (not opts.disks and not opts.sd_size
2099 9b12ed0f Iustin Pop
        and mode == constants.INSTANCE_CREATE):
2100 d77490c5 Iustin Pop
      raise errors.OpPrereqError("No disk information specified")
2101 d77490c5 Iustin Pop
    if opts.disks and opts.sd_size is not None:
2102 d77490c5 Iustin Pop
      raise errors.OpPrereqError("Please use either the '--disk' or"
2103 d77490c5 Iustin Pop
                                 " '-s' option")
2104 d77490c5 Iustin Pop
    if opts.sd_size is not None:
2105 ccfa86ba Michael Hanselmann
      opts.disks = [(0, {constants.IDISK_SIZE: opts.sd_size})]
2106 9b12ed0f Iustin Pop
2107 9b12ed0f Iustin Pop
    if opts.disks:
2108 9b12ed0f Iustin Pop
      try:
2109 9b12ed0f Iustin Pop
        disk_max = max(int(didx[0]) + 1 for didx in opts.disks)
2110 9b12ed0f Iustin Pop
      except ValueError, err:
2111 9b12ed0f Iustin Pop
        raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err))
2112 9b12ed0f Iustin Pop
      disks = [{}] * disk_max
2113 9b12ed0f Iustin Pop
    else:
2114 9b12ed0f Iustin Pop
      disks = []
2115 d77490c5 Iustin Pop
    for didx, ddict in opts.disks:
2116 d77490c5 Iustin Pop
      didx = int(didx)
2117 d77490c5 Iustin Pop
      if not isinstance(ddict, dict):
2118 d77490c5 Iustin Pop
        msg = "Invalid disk/%d value: expected dict, got %s" % (didx, ddict)
2119 d77490c5 Iustin Pop
        raise errors.OpPrereqError(msg)
2120 ccfa86ba Michael Hanselmann
      elif constants.IDISK_SIZE in ddict:
2121 ccfa86ba Michael Hanselmann
        if constants.IDISK_ADOPT in ddict:
2122 5029db65 Iustin Pop
          raise errors.OpPrereqError("Only one of 'size' and 'adopt' allowed"
2123 5029db65 Iustin Pop
                                     " (disk %d)" % didx)
2124 5029db65 Iustin Pop
        try:
2125 ccfa86ba Michael Hanselmann
          ddict[constants.IDISK_SIZE] = \
2126 ccfa86ba Michael Hanselmann
            utils.ParseUnit(ddict[constants.IDISK_SIZE])
2127 5029db65 Iustin Pop
        except ValueError, err:
2128 5029db65 Iustin Pop
          raise errors.OpPrereqError("Invalid disk size for disk %d: %s" %
2129 5029db65 Iustin Pop
                                     (didx, err))
2130 ccfa86ba Michael Hanselmann
      elif constants.IDISK_ADOPT in ddict:
2131 5029db65 Iustin Pop
        if mode == constants.INSTANCE_IMPORT:
2132 5029db65 Iustin Pop
          raise errors.OpPrereqError("Disk adoption not allowed for instance"
2133 5029db65 Iustin Pop
                                     " import")
2134 ccfa86ba Michael Hanselmann
        ddict[constants.IDISK_SIZE] = 0
2135 5029db65 Iustin Pop
      else:
2136 5029db65 Iustin Pop
        raise errors.OpPrereqError("Missing size or adoption source for"
2137 5029db65 Iustin Pop
                                   " disk %d" % didx)
2138 d77490c5 Iustin Pop
      disks[didx] = ddict
2139 d77490c5 Iustin Pop
2140 a57981c5 Apollon Oikonomopoulos
  if opts.tags is not None:
2141 0f8810df Michael Hanselmann
    tags = opts.tags.split(",")
2142 a57981c5 Apollon Oikonomopoulos
  else:
2143 a57981c5 Apollon Oikonomopoulos
    tags = []
2144 a57981c5 Apollon Oikonomopoulos
2145 d77490c5 Iustin Pop
  utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_TYPES)
2146 d77490c5 Iustin Pop
  utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
2147 d77490c5 Iustin Pop
2148 d77490c5 Iustin Pop
  if mode == constants.INSTANCE_CREATE:
2149 d77490c5 Iustin Pop
    start = opts.start
2150 d77490c5 Iustin Pop
    os_type = opts.os
2151 1ee8e01a Guido Trotter
    force_variant = opts.force_variant
2152 d77490c5 Iustin Pop
    src_node = None
2153 d77490c5 Iustin Pop
    src_path = None
2154 25a8792c Iustin Pop
    no_install = opts.no_install
2155 e588764d Iustin Pop
    identify_defaults = False
2156 d77490c5 Iustin Pop
  elif mode == constants.INSTANCE_IMPORT:
2157 d77490c5 Iustin Pop
    start = False
2158 d77490c5 Iustin Pop
    os_type = None
2159 1ee8e01a Guido Trotter
    force_variant = False
2160 d77490c5 Iustin Pop
    src_node = opts.src_node
2161 d77490c5 Iustin Pop
    src_path = opts.src_dir
2162 25a8792c Iustin Pop
    no_install = None
2163 e588764d Iustin Pop
    identify_defaults = opts.identify_defaults
2164 d77490c5 Iustin Pop
  else:
2165 d77490c5 Iustin Pop
    raise errors.ProgrammerError("Invalid creation mode %s" % mode)
2166 d77490c5 Iustin Pop
2167 e1530b10 Iustin Pop
  op = opcodes.OpInstanceCreate(instance_name=instance,
2168 d77490c5 Iustin Pop
                                disks=disks,
2169 d77490c5 Iustin Pop
                                disk_template=opts.disk_template,
2170 d77490c5 Iustin Pop
                                nics=nics,
2171 d77490c5 Iustin Pop
                                pnode=pnode, snode=snode,
2172 d77490c5 Iustin Pop
                                ip_check=opts.ip_check,
2173 460d22be Iustin Pop
                                name_check=opts.name_check,
2174 d77490c5 Iustin Pop
                                wait_for_sync=opts.wait_for_sync,
2175 d77490c5 Iustin Pop
                                file_storage_dir=opts.file_storage_dir,
2176 d77490c5 Iustin Pop
                                file_driver=opts.file_driver,
2177 d77490c5 Iustin Pop
                                iallocator=opts.iallocator,
2178 d77490c5 Iustin Pop
                                hypervisor=hypervisor,
2179 d77490c5 Iustin Pop
                                hvparams=hvparams,
2180 d77490c5 Iustin Pop
                                beparams=opts.beparams,
2181 062a7100 Iustin Pop
                                osparams=opts.osparams,
2182 d77490c5 Iustin Pop
                                mode=mode,
2183 d77490c5 Iustin Pop
                                start=start,
2184 d77490c5 Iustin Pop
                                os_type=os_type,
2185 1ee8e01a Guido Trotter
                                force_variant=force_variant,
2186 d77490c5 Iustin Pop
                                src_node=src_node,
2187 25a8792c Iustin Pop
                                src_path=src_path,
2188 a57981c5 Apollon Oikonomopoulos
                                tags=tags,
2189 e588764d Iustin Pop
                                no_install=no_install,
2190 e588764d Iustin Pop
                                identify_defaults=identify_defaults)
2191 d77490c5 Iustin Pop
2192 d77490c5 Iustin Pop
  SubmitOrSend(op, opts)
2193 d77490c5 Iustin Pop
  return 0
2194 d77490c5 Iustin Pop
2195 d77490c5 Iustin Pop
2196 7e49b6ce Michael Hanselmann
class _RunWhileClusterStoppedHelper:
2197 7e49b6ce Michael Hanselmann
  """Helper class for L{RunWhileClusterStopped} to simplify state management
2198 7e49b6ce Michael Hanselmann

2199 7e49b6ce Michael Hanselmann
  """
2200 7e49b6ce Michael Hanselmann
  def __init__(self, feedback_fn, cluster_name, master_node, online_nodes):
2201 7e49b6ce Michael Hanselmann
    """Initializes this class.
2202 7e49b6ce Michael Hanselmann

2203 7e49b6ce Michael Hanselmann
    @type feedback_fn: callable
2204 7e49b6ce Michael Hanselmann
    @param feedback_fn: Feedback function
2205 7e49b6ce Michael Hanselmann
    @type cluster_name: string
2206 7e49b6ce Michael Hanselmann
    @param cluster_name: Cluster name
2207 7e49b6ce Michael Hanselmann
    @type master_node: string
2208 7e49b6ce Michael Hanselmann
    @param master_node Master node name
2209 7e49b6ce Michael Hanselmann
    @type online_nodes: list
2210 7e49b6ce Michael Hanselmann
    @param online_nodes: List of names of online nodes
2211 7e49b6ce Michael Hanselmann

2212 7e49b6ce Michael Hanselmann
    """
2213 7e49b6ce Michael Hanselmann
    self.feedback_fn = feedback_fn
2214 7e49b6ce Michael Hanselmann
    self.cluster_name = cluster_name
2215 7e49b6ce Michael Hanselmann
    self.master_node = master_node
2216 7e49b6ce Michael Hanselmann
    self.online_nodes = online_nodes
2217 7e49b6ce Michael Hanselmann
2218 7e49b6ce Michael Hanselmann
    self.ssh = ssh.SshRunner(self.cluster_name)
2219 7e49b6ce Michael Hanselmann
2220 7e49b6ce Michael Hanselmann
    self.nonmaster_nodes = [name for name in online_nodes
2221 7e49b6ce Michael Hanselmann
                            if name != master_node]
2222 7e49b6ce Michael Hanselmann
2223 7e49b6ce Michael Hanselmann
    assert self.master_node not in self.nonmaster_nodes
2224 7e49b6ce Michael Hanselmann
2225 7e49b6ce Michael Hanselmann
  def _RunCmd(self, node_name, cmd):
2226 7e49b6ce Michael Hanselmann
    """Runs a command on the local or a remote machine.
2227 7e49b6ce Michael Hanselmann

2228 7e49b6ce Michael Hanselmann
    @type node_name: string
2229 7e49b6ce Michael Hanselmann
    @param node_name: Machine name
2230 7e49b6ce Michael Hanselmann
    @type cmd: list
2231 7e49b6ce Michael Hanselmann
    @param cmd: Command
2232 7e49b6ce Michael Hanselmann

2233 7e49b6ce Michael Hanselmann
    """
2234 7e49b6ce Michael Hanselmann
    if node_name is None or node_name == self.master_node:
2235 7e49b6ce Michael Hanselmann
      # No need to use SSH
2236 7e49b6ce Michael Hanselmann
      result = utils.RunCmd(cmd)
2237 7e49b6ce Michael Hanselmann
    else:
2238 7e49b6ce Michael Hanselmann
      result = self.ssh.Run(node_name, "root", utils.ShellQuoteArgs(cmd))
2239 7e49b6ce Michael Hanselmann
2240 7e49b6ce Michael Hanselmann
    if result.failed:
2241 7e49b6ce Michael Hanselmann
      errmsg = ["Failed to run command %s" % result.cmd]
2242 7e49b6ce Michael Hanselmann
      if node_name:
2243 7e49b6ce Michael Hanselmann
        errmsg.append("on node %s" % node_name)
2244 7e49b6ce Michael Hanselmann
      errmsg.append(": exitcode %s and error %s" %
2245 7e49b6ce Michael Hanselmann
                    (result.exit_code, result.output))
2246 7e49b6ce Michael Hanselmann
      raise errors.OpExecError(" ".join(errmsg))
2247 7e49b6ce Michael Hanselmann
2248 7e49b6ce Michael Hanselmann
  def Call(self, fn, *args):
2249 7e49b6ce Michael Hanselmann
    """Call function while all daemons are stopped.
2250 7e49b6ce Michael Hanselmann

2251 7e49b6ce Michael Hanselmann
    @type fn: callable
2252 7e49b6ce Michael Hanselmann
    @param fn: Function to be called
2253 7e49b6ce Michael Hanselmann

2254 7e49b6ce Michael Hanselmann
    """
2255 7e49b6ce Michael Hanselmann
    # Pause watcher by acquiring an exclusive lock on watcher state file
2256 7e49b6ce Michael Hanselmann
    self.feedback_fn("Blocking watcher")
2257 7e49b6ce Michael Hanselmann
    watcher_block = utils.FileLock.Open(constants.WATCHER_STATEFILE)
2258 7e49b6ce Michael Hanselmann
    try:
2259 7e49b6ce Michael Hanselmann
      # TODO: Currently, this just blocks. There's no timeout.
2260 7e49b6ce Michael Hanselmann
      # TODO: Should it be a shared lock?
2261 7e49b6ce Michael Hanselmann
      watcher_block.Exclusive(blocking=True)
2262 7e49b6ce Michael Hanselmann
2263 7e49b6ce Michael Hanselmann
      # Stop master daemons, so that no new jobs can come in and all running
2264 7e49b6ce Michael Hanselmann
      # ones are finished
2265 7e49b6ce Michael Hanselmann
      self.feedback_fn("Stopping master daemons")
2266 7e49b6ce Michael Hanselmann
      self._RunCmd(None, [constants.DAEMON_UTIL, "stop-master"])
2267 7e49b6ce Michael Hanselmann
      try:
2268 7e49b6ce Michael Hanselmann
        # Stop daemons on all nodes
2269 7e49b6ce Michael Hanselmann
        for node_name in self.online_nodes:
2270 7e49b6ce Michael Hanselmann
          self.feedback_fn("Stopping daemons on %s" % node_name)
2271 7e49b6ce Michael Hanselmann
          self._RunCmd(node_name, [constants.DAEMON_UTIL, "stop-all"])
2272 7e49b6ce Michael Hanselmann
2273 7e49b6ce Michael Hanselmann
        # All daemons are shut down now
2274 7e49b6ce Michael Hanselmann
        try:
2275 7e49b6ce Michael Hanselmann
          return fn(self, *args)
2276 d512e84b Michael Hanselmann
        except Exception, err:
2277 d512e84b Michael Hanselmann
          _, errmsg = FormatError(err)
2278 7e49b6ce Michael Hanselmann
          logging.exception("Caught exception")
2279 d512e84b Michael Hanselmann
          self.feedback_fn(errmsg)
2280 7e49b6ce Michael Hanselmann
          raise
2281 7e49b6ce Michael Hanselmann
      finally:
2282 7e49b6ce Michael Hanselmann
        # Start cluster again, master node last
2283 7e49b6ce Michael Hanselmann
        for node_name in self.nonmaster_nodes + [self.master_node]:
2284 7e49b6ce Michael Hanselmann
          self.feedback_fn("Starting daemons on %s" % node_name)
2285 7e49b6ce Michael Hanselmann
          self._RunCmd(node_name, [constants.DAEMON_UTIL, "start-all"])
2286 7e49b6ce Michael Hanselmann
    finally:
2287 7e49b6ce Michael Hanselmann
      # Resume watcher
2288 7e49b6ce Michael Hanselmann
      watcher_block.Close()
2289 7e49b6ce Michael Hanselmann
2290 7e49b6ce Michael Hanselmann
2291 7e49b6ce Michael Hanselmann
def RunWhileClusterStopped(feedback_fn, fn, *args):
2292 7e49b6ce Michael Hanselmann
  """Calls a function while all cluster daemons are stopped.
2293 7e49b6ce Michael Hanselmann

2294 7e49b6ce Michael Hanselmann
  @type feedback_fn: callable
2295 7e49b6ce Michael Hanselmann
  @param feedback_fn: Feedback function
2296 7e49b6ce Michael Hanselmann
  @type fn: callable
2297 7e49b6ce Michael Hanselmann
  @param fn: Function to be called when daemons are stopped
2298 7e49b6ce Michael Hanselmann

2299 7e49b6ce Michael Hanselmann
  """
2300 7e49b6ce Michael Hanselmann
  feedback_fn("Gathering cluster information")
2301 7e49b6ce Michael Hanselmann
2302 7e49b6ce Michael Hanselmann
  # This ensures we're running on the master daemon
2303 7e49b6ce Michael Hanselmann
  cl = GetClient()
2304 7e49b6ce Michael Hanselmann
2305 7e49b6ce Michael Hanselmann
  (cluster_name, master_node) = \
2306 7e49b6ce Michael Hanselmann
    cl.QueryConfigValues(["cluster_name", "master_node"])
2307 7e49b6ce Michael Hanselmann
2308 7e49b6ce Michael Hanselmann
  online_nodes = GetOnlineNodes([], cl=cl)
2309 7e49b6ce Michael Hanselmann
2310 7e49b6ce Michael Hanselmann
  # Don't keep a reference to the client. The master daemon will go away.
2311 7e49b6ce Michael Hanselmann
  del cl
2312 7e49b6ce Michael Hanselmann
2313 7e49b6ce Michael Hanselmann
  assert master_node in online_nodes
2314 7e49b6ce Michael Hanselmann
2315 7e49b6ce Michael Hanselmann
  return _RunWhileClusterStoppedHelper(feedback_fn, cluster_name, master_node,
2316 7e49b6ce Michael Hanselmann
                                       online_nodes).Call(fn, *args)
2317 7e49b6ce Michael Hanselmann
2318 7e49b6ce Michael Hanselmann
2319 16be8703 Iustin Pop
def GenerateTable(headers, fields, separator, data,
2320 9fbfbb7b Iustin Pop
                  numfields=None, unitfields=None,
2321 9fbfbb7b Iustin Pop
                  units=None):
2322 137161c9 Michael Hanselmann
  """Prints a table with headers and different fields.
2323 137161c9 Michael Hanselmann

2324 9fbfbb7b Iustin Pop
  @type headers: dict
2325 9fbfbb7b Iustin Pop
  @param headers: dictionary mapping field names to headers for
2326 9fbfbb7b Iustin Pop
      the table
2327 9fbfbb7b Iustin Pop
  @type fields: list
2328 9fbfbb7b Iustin Pop
  @param fields: the field names corresponding to each row in
2329 9fbfbb7b Iustin Pop
      the data field
2330 9fbfbb7b Iustin Pop
  @param separator: the separator to be used; if this is None,
2331 9fbfbb7b Iustin Pop
      the default 'smart' algorithm is used which computes optimal
2332 9fbfbb7b Iustin Pop
      field width, otherwise just the separator is used between
2333 9fbfbb7b Iustin Pop
      each field
2334 9fbfbb7b Iustin Pop
  @type data: list
2335 9fbfbb7b Iustin Pop
  @param data: a list of lists, each sublist being one row to be output
2336 9fbfbb7b Iustin Pop
  @type numfields: list
2337 9fbfbb7b Iustin Pop
  @param numfields: a list with the fields that hold numeric
2338 9fbfbb7b Iustin Pop
      values and thus should be right-aligned
2339 9fbfbb7b Iustin Pop
  @type unitfields: list
2340 9fbfbb7b Iustin Pop
  @param unitfields: a list with the fields that hold numeric
2341 9fbfbb7b Iustin Pop
      values that should be formatted with the units field
2342 9fbfbb7b Iustin Pop
  @type units: string or None
2343 9fbfbb7b Iustin Pop
  @param units: the units we should use for formatting, or None for
2344 9fbfbb7b Iustin Pop
      automatic choice (human-readable for non-separator usage, otherwise
2345 9fbfbb7b Iustin Pop
      megabytes); this is a one-letter string
2346 137161c9 Michael Hanselmann

2347 137161c9 Michael Hanselmann
  """
2348 9fbfbb7b Iustin Pop
  if units is None:
2349 9fbfbb7b Iustin Pop
    if separator:
2350 9fbfbb7b Iustin Pop
      units = "m"
2351 9fbfbb7b Iustin Pop
    else:
2352 9fbfbb7b Iustin Pop
      units = "h"
2353 9fbfbb7b Iustin Pop
2354 137161c9 Michael Hanselmann
  if numfields is None:
2355 137161c9 Michael Hanselmann
    numfields = []
2356 137161c9 Michael Hanselmann
  if unitfields is None:
2357 137161c9 Michael Hanselmann
    unitfields = []
2358 137161c9 Michael Hanselmann
2359 fe267188 Iustin Pop
  numfields = utils.FieldSet(*numfields)   # pylint: disable-msg=W0142
2360 fe267188 Iustin Pop
  unitfields = utils.FieldSet(*unitfields) # pylint: disable-msg=W0142
2361 00430f8e Iustin Pop
2362 137161c9 Michael Hanselmann
  format_fields = []
2363 137161c9 Michael Hanselmann
  for field in fields:
2364 01ca31ae Iustin Pop
    if headers and field not in headers:
2365 ea5a5b74 Guido Trotter
      # TODO: handle better unknown fields (either revert to old
2366 71c1af58 Iustin Pop
      # style of raising exception, or deal more intelligently with
2367 71c1af58 Iustin Pop
      # variable fields)
2368 71c1af58 Iustin Pop
      headers[field] = field
2369 137161c9 Michael Hanselmann
    if separator is not None:
2370 137161c9 Michael Hanselmann
      format_fields.append("%s")
2371 00430f8e Iustin Pop
    elif numfields.Matches(field):
2372 137161c9 Michael Hanselmann
      format_fields.append("%*s")
2373 137161c9 Michael Hanselmann
    else:
2374 137161c9 Michael Hanselmann
      format_fields.append("%-*s")
2375 137161c9 Michael Hanselmann
2376 137161c9 Michael Hanselmann
  if separator is None:
2377 137161c9 Michael Hanselmann
    mlens = [0 for name in fields]
2378 c04bc777 Iustin Pop
    format_str = ' '.join(format_fields)
2379 137161c9 Michael Hanselmann
  else:
2380 c04bc777 Iustin Pop
    format_str = separator.replace("%", "%%").join(format_fields)
2381 137161c9 Michael Hanselmann
2382 137161c9 Michael Hanselmann
  for row in data:
2383 dcbd6288 Guido Trotter
    if row is None:
2384 dcbd6288 Guido Trotter
      continue
2385 137161c9 Michael Hanselmann
    for idx, val in enumerate(row):
2386 00430f8e Iustin Pop
      if unitfields.Matches(fields[idx]):
2387 137161c9 Michael Hanselmann
        try:
2388 137161c9 Michael Hanselmann
          val = int(val)
2389 691744c4 Iustin Pop
        except (TypeError, ValueError):
2390 137161c9 Michael Hanselmann
          pass
2391 137161c9 Michael Hanselmann
        else:
2392 9fbfbb7b Iustin Pop
          val = row[idx] = utils.FormatUnit(val, units)
2393 01ca31ae Iustin Pop
      val = row[idx] = str(val)
2394 137161c9 Michael Hanselmann
      if separator is None:
2395 137161c9 Michael Hanselmann
        mlens[idx] = max(mlens[idx], len(val))
2396 137161c9 Michael Hanselmann
2397 16be8703 Iustin Pop
  result = []
2398 137161c9 Michael Hanselmann
  if headers:
2399 137161c9 Michael Hanselmann
    args = []
2400 137161c9 Michael Hanselmann
    for idx, name in enumerate(fields):
2401 137161c9 Michael Hanselmann
      hdr = headers[name]
2402 137161c9 Michael Hanselmann
      if separator is None:
2403 137161c9 Michael Hanselmann
        mlens[idx] = max(mlens[idx], len(hdr))
2404 137161c9 Michael Hanselmann
        args.append(mlens[idx])
2405 137161c9 Michael Hanselmann
      args.append(hdr)
2406 c04bc777 Iustin Pop
    result.append(format_str % tuple(args))
2407 137161c9 Michael Hanselmann
2408 ec39d63c Michael Hanselmann
  if separator is None:
2409 ec39d63c Michael Hanselmann
    assert len(mlens) == len(fields)
2410 ec39d63c Michael Hanselmann
2411 ec39d63c Michael Hanselmann
    if fields and not numfields.Matches(fields[-1]):
2412 ec39d63c Michael Hanselmann
      mlens[-1] = 0
2413 ec39d63c Michael Hanselmann
2414 137161c9 Michael Hanselmann
  for line in data:
2415 137161c9 Michael Hanselmann
    args = []
2416 dcbd6288 Guido Trotter
    if line is None:
2417 dcbd6288 Guido Trotter
      line = ['-' for _ in fields]
2418 f1501b3f Michael Hanselmann
    for idx in range(len(fields)):
2419 137161c9 Michael Hanselmann
      if separator is None:
2420 137161c9 Michael Hanselmann
        args.append(mlens[idx])
2421 137161c9 Michael Hanselmann
      args.append(line[idx])
2422 c04bc777 Iustin Pop
    result.append(format_str % tuple(args))
2423 16be8703 Iustin Pop
2424 16be8703 Iustin Pop
  return result
2425 3386e7a9 Iustin Pop
2426 3386e7a9 Iustin Pop
2427 ee3aedff Michael Hanselmann
def _FormatBool(value):
2428 ee3aedff Michael Hanselmann
  """Formats a boolean value as a string.
2429 ee3aedff Michael Hanselmann

2430 ee3aedff Michael Hanselmann
  """
2431 ee3aedff Michael Hanselmann
  if value:
2432 ee3aedff Michael Hanselmann
    return "Y"
2433 ee3aedff Michael Hanselmann
  return "N"
2434 ee3aedff Michael Hanselmann
2435 ee3aedff Michael Hanselmann
2436 ee3aedff Michael Hanselmann
#: Default formatting for query results; (callback, align right)
2437 ee3aedff Michael Hanselmann
_DEFAULT_FORMAT_QUERY = {
2438 ee3aedff Michael Hanselmann
  constants.QFT_TEXT: (str, False),
2439 ee3aedff Michael Hanselmann
  constants.QFT_BOOL: (_FormatBool, False),
2440 ee3aedff Michael Hanselmann
  constants.QFT_NUMBER: (str, True),
2441 ee3aedff Michael Hanselmann
  constants.QFT_TIMESTAMP: (utils.FormatTime, False),
2442 ee3aedff Michael Hanselmann
  constants.QFT_OTHER: (str, False),
2443 ee3aedff Michael Hanselmann
  constants.QFT_UNKNOWN: (str, False),
2444 ee3aedff Michael Hanselmann
  }
2445 ee3aedff Michael Hanselmann
2446 ee3aedff Michael Hanselmann
2447 ee3aedff Michael Hanselmann
def _GetColumnFormatter(fdef, override, unit):
2448 ee3aedff Michael Hanselmann
  """Returns formatting function for a field.
2449 ee3aedff Michael Hanselmann

2450 ee3aedff Michael Hanselmann
  @type fdef: L{objects.QueryFieldDefinition}
2451 ee3aedff Michael Hanselmann
  @type override: dict
2452 ee3aedff Michael Hanselmann
  @param override: Dictionary for overriding field formatting functions,
2453 ee3aedff Michael Hanselmann
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
2454 ee3aedff Michael Hanselmann
  @type unit: string
2455 ee3aedff Michael Hanselmann
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT}
2456 ee3aedff Michael Hanselmann
  @rtype: tuple; (callable, bool)
2457 ee3aedff Michael Hanselmann
  @return: Returns the function to format a value (takes one parameter) and a
2458 ee3aedff Michael Hanselmann
    boolean for aligning the value on the right-hand side
2459 ee3aedff Michael Hanselmann

2460 ee3aedff Michael Hanselmann
  """
2461 ee3aedff Michael Hanselmann
  fmt = override.get(fdef.name, None)
2462 ee3aedff Michael Hanselmann
  if fmt is not None:
2463 ee3aedff Michael Hanselmann
    return fmt
2464 ee3aedff Michael Hanselmann
2465 ee3aedff Michael Hanselmann
  assert constants.QFT_UNIT not in _DEFAULT_FORMAT_QUERY
2466 ee3aedff Michael Hanselmann
2467 ee3aedff Michael Hanselmann
  if fdef.kind == constants.QFT_UNIT:
2468 ee3aedff Michael Hanselmann
    # Can't keep this information in the static dictionary
2469 ee3aedff Michael Hanselmann
    return (lambda value: utils.FormatUnit(value, unit), True)
2470 ee3aedff Michael Hanselmann
2471 ee3aedff Michael Hanselmann
  fmt = _DEFAULT_FORMAT_QUERY.get(fdef.kind, None)
2472 ee3aedff Michael Hanselmann
  if fmt is not None:
2473 ee3aedff Michael Hanselmann
    return fmt
2474 ee3aedff Michael Hanselmann
2475 ee3aedff Michael Hanselmann
  raise NotImplementedError("Can't format column type '%s'" % fdef.kind)
2476 ee3aedff Michael Hanselmann
2477 ee3aedff Michael Hanselmann
2478 ee3aedff Michael Hanselmann
class _QueryColumnFormatter:
2479 ee3aedff Michael Hanselmann
  """Callable class for formatting fields of a query.
2480 ee3aedff Michael Hanselmann

2481 ee3aedff Michael Hanselmann
  """
2482 f0b1bafe Iustin Pop
  def __init__(self, fn, status_fn, verbose):
2483 ee3aedff Michael Hanselmann
    """Initializes this class.
2484 ee3aedff Michael Hanselmann

2485 ee3aedff Michael Hanselmann
    @type fn: callable
2486 ee3aedff Michael Hanselmann
    @param fn: Formatting function
2487 ee3aedff Michael Hanselmann
    @type status_fn: callable
2488 ee3aedff Michael Hanselmann
    @param status_fn: Function to report fields' status
2489 f0b1bafe Iustin Pop
    @type verbose: boolean
2490 f0b1bafe Iustin Pop
    @param verbose: whether to use verbose field descriptions or not
2491 ee3aedff Michael Hanselmann

2492 ee3aedff Michael Hanselmann
    """
2493 ee3aedff Michael Hanselmann
    self._fn = fn
2494 ee3aedff Michael Hanselmann
    self._status_fn = status_fn
2495 cbfa4f0f Michael Hanselmann
    self._verbose = verbose
2496 ee3aedff Michael Hanselmann
2497 ee3aedff Michael Hanselmann
  def __call__(self, data):
2498 ee3aedff Michael Hanselmann
    """Returns a field's string representation.
2499 ee3aedff Michael Hanselmann

2500 ee3aedff Michael Hanselmann
    """
2501 ee3aedff Michael Hanselmann
    (status, value) = data
2502 ee3aedff Michael Hanselmann
2503 ee3aedff Michael Hanselmann
    # Report status
2504 ee3aedff Michael Hanselmann
    self._status_fn(status)
2505 ee3aedff Michael Hanselmann
2506 cfb084ae René Nussbaumer
    if status == constants.RS_NORMAL:
2507 ee3aedff Michael Hanselmann
      return self._fn(value)
2508 ee3aedff Michael Hanselmann
2509 ee3aedff Michael Hanselmann
    assert value is None, \
2510 ee3aedff Michael Hanselmann
           "Found value %r for abnormal status %s" % (value, status)
2511 ee3aedff Michael Hanselmann
2512 f2c6673d Michael Hanselmann
    return FormatResultError(status, self._verbose)
2513 ee3aedff Michael Hanselmann
2514 ee3aedff Michael Hanselmann
2515 f2c6673d Michael Hanselmann
def FormatResultError(status, verbose):
2516 ae95e419 René Nussbaumer
  """Formats result status other than L{constants.RS_NORMAL}.
2517 ee3aedff Michael Hanselmann

2518 ae95e419 René Nussbaumer
  @param status: The result status
2519 f2c6673d Michael Hanselmann
  @type verbose: boolean
2520 f2c6673d Michael Hanselmann
  @param verbose: Whether to return the verbose text
2521 ae95e419 René Nussbaumer
  @return: Text of result status
2522 a6070ef7 Michael Hanselmann

2523 ae95e419 René Nussbaumer
  """
2524 ae95e419 René Nussbaumer
  assert status != constants.RS_NORMAL, \
2525 cbfa4f0f Michael Hanselmann
         "FormatResultError called with status equal to constants.RS_NORMAL"
2526 ae95e419 René Nussbaumer
  try:
2527 cbfa4f0f Michael Hanselmann
    (verbose_text, normal_text) = constants.RSS_DESCRIPTION[status]
2528 ae95e419 René Nussbaumer
  except KeyError:
2529 ee3aedff Michael Hanselmann
    raise NotImplementedError("Unknown status %s" % status)
2530 cbfa4f0f Michael Hanselmann
  else:
2531 cbfa4f0f Michael Hanselmann
    if verbose:
2532 cbfa4f0f Michael Hanselmann
      return verbose_text
2533 cbfa4f0f Michael Hanselmann
    return normal_text
2534 ee3aedff Michael Hanselmann
2535 ee3aedff Michael Hanselmann
2536 ee3aedff Michael Hanselmann
def FormatQueryResult(result, unit=None, format_override=None, separator=None,
2537 f0b1bafe Iustin Pop
                      header=False, verbose=False):
2538 ee3aedff Michael Hanselmann
  """Formats data in L{objects.QueryResponse}.
2539 ee3aedff Michael Hanselmann

2540 ee3aedff Michael Hanselmann
  @type result: L{objects.QueryResponse}
2541 ee3aedff Michael Hanselmann
  @param result: result of query operation
2542 ee3aedff Michael Hanselmann
  @type unit: string
2543 ee3aedff Michael Hanselmann
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT},
2544 18009c1e Iustin Pop
    see L{utils.text.FormatUnit}
2545 ee3aedff Michael Hanselmann
  @type format_override: dict
2546 ee3aedff Michael Hanselmann
  @param format_override: Dictionary for overriding field formatting functions,
2547 ee3aedff Michael Hanselmann
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
2548 ee3aedff Michael Hanselmann
  @type separator: string or None
2549 ee3aedff Michael Hanselmann
  @param separator: String used to separate fields
2550 ee3aedff Michael Hanselmann
  @type header: bool
2551 ee3aedff Michael Hanselmann
  @param header: Whether to output header row
2552 f0b1bafe Iustin Pop
  @type verbose: boolean
2553 f0b1bafe Iustin Pop
  @param verbose: whether to use verbose field descriptions or not
2554 ee3aedff Michael Hanselmann

2555 ee3aedff Michael Hanselmann
  """
2556 ee3aedff Michael Hanselmann
  if unit is None:
2557 ee3aedff Michael Hanselmann
    if separator:
2558 ee3aedff Michael Hanselmann
      unit = "m"
2559 ee3aedff Michael Hanselmann
    else:
2560 ee3aedff Michael Hanselmann
      unit = "h"
2561 ee3aedff Michael Hanselmann
2562 ee3aedff Michael Hanselmann
  if format_override is None:
2563 ee3aedff Michael Hanselmann
    format_override = {}
2564 ee3aedff Michael Hanselmann
2565 cfb084ae René Nussbaumer
  stats = dict.fromkeys(constants.RS_ALL, 0)
2566 ee3aedff Michael Hanselmann
2567 ee3aedff Michael Hanselmann
  def _RecordStatus(status):
2568 ee3aedff Michael Hanselmann
    if status in stats:
2569 ee3aedff Michael Hanselmann
      stats[status] += 1
2570 ee3aedff Michael Hanselmann
2571 ee3aedff Michael Hanselmann
  columns = []
2572 ee3aedff Michael Hanselmann
  for fdef in result.fields:
2573 ee3aedff Michael Hanselmann
    assert fdef.title and fdef.name
2574 ee3aedff Michael Hanselmann
    (fn, align_right) = _GetColumnFormatter(fdef, format_override, unit)
2575 ee3aedff Michael Hanselmann
    columns.append(TableColumn(fdef.title,
2576 f0b1bafe Iustin Pop
                               _QueryColumnFormatter(fn, _RecordStatus,
2577 f0b1bafe Iustin Pop
                                                     verbose),
2578 ee3aedff Michael Hanselmann
                               align_right))
2579 ee3aedff Michael Hanselmann
2580 ee3aedff Michael Hanselmann
  table = FormatTable(result.data, columns, header, separator)
2581 ee3aedff Michael Hanselmann
2582 ee3aedff Michael Hanselmann
  # Collect statistics
2583 cfb084ae René Nussbaumer
  assert len(stats) == len(constants.RS_ALL)
2584 ee3aedff Michael Hanselmann
  assert compat.all(count >= 0 for count in stats.values())
2585 ee3aedff Michael Hanselmann
2586 ee3aedff Michael Hanselmann
  # Determine overall status. If there was no data, unknown fields must be
2587 ee3aedff Michael Hanselmann
  # detected via the field definitions.
2588 cfb084ae René Nussbaumer
  if (stats[constants.RS_UNKNOWN] or
2589 ee3aedff Michael Hanselmann
      (not result.data and _GetUnknownFields(result.fields))):
2590 ee3aedff Michael Hanselmann
    status = QR_UNKNOWN
2591 ee3aedff Michael Hanselmann
  elif compat.any(count > 0 for key, count in stats.items()
2592 cfb084ae René Nussbaumer
                  if key != constants.RS_NORMAL):
2593 ee3aedff Michael Hanselmann
    status = QR_INCOMPLETE
2594 ee3aedff Michael Hanselmann
  else:
2595 ee3aedff Michael Hanselmann
    status = QR_NORMAL
2596 ee3aedff Michael Hanselmann
2597 ee3aedff Michael Hanselmann
  return (status, table)
2598 ee3aedff Michael Hanselmann
2599 ee3aedff Michael Hanselmann
2600 ee3aedff Michael Hanselmann
def _GetUnknownFields(fdefs):
2601 ee3aedff Michael Hanselmann
  """Returns list of unknown fields included in C{fdefs}.
2602 ee3aedff Michael Hanselmann

2603 ee3aedff Michael Hanselmann
  @type fdefs: list of L{objects.QueryFieldDefinition}
2604 ee3aedff Michael Hanselmann

2605 ee3aedff Michael Hanselmann
  """
2606 ee3aedff Michael Hanselmann
  return [fdef for fdef in fdefs
2607 ee3aedff Michael Hanselmann
          if fdef.kind == constants.QFT_UNKNOWN]
2608 ee3aedff Michael Hanselmann
2609 ee3aedff Michael Hanselmann
2610 ee3aedff Michael Hanselmann
def _WarnUnknownFields(fdefs):
2611 ee3aedff Michael Hanselmann
  """Prints a warning to stderr if a query included unknown fields.
2612 ee3aedff Michael Hanselmann

2613 ee3aedff Michael Hanselmann
  @type fdefs: list of L{objects.QueryFieldDefinition}
2614 ee3aedff Michael Hanselmann

2615 ee3aedff Michael Hanselmann
  """
2616 ee3aedff Michael Hanselmann
  unknown = _GetUnknownFields(fdefs)
2617 ee3aedff Michael Hanselmann
  if unknown:
2618 ee3aedff Michael Hanselmann
    ToStderr("Warning: Queried for unknown fields %s",
2619 ee3aedff Michael Hanselmann
             utils.CommaJoin(fdef.name for fdef in unknown))
2620 ee3aedff Michael Hanselmann
    return True
2621 ee3aedff Michael Hanselmann
2622 ee3aedff Michael Hanselmann
  return False
2623 ee3aedff Michael Hanselmann
2624 ee3aedff Michael Hanselmann
2625 ee3aedff Michael Hanselmann
def GenericList(resource, fields, names, unit, separator, header, cl=None,
2626 2928de47 Michael Hanselmann
                format_override=None, verbose=False, force_filter=False):
2627 ee3aedff Michael Hanselmann
  """Generic implementation for listing all items of a resource.
2628 ee3aedff Michael Hanselmann

2629 abd66bf8 Michael Hanselmann
  @param resource: One of L{constants.QR_VIA_LUXI}
2630 ee3aedff Michael Hanselmann
  @type fields: list of strings
2631 ee3aedff Michael Hanselmann
  @param fields: List of fields to query for
2632 ee3aedff Michael Hanselmann
  @type names: list of strings
2633 ee3aedff Michael Hanselmann
  @param names: Names of items to query for
2634 ee3aedff Michael Hanselmann
  @type unit: string or None
2635 ee3aedff Michael Hanselmann
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT} or
2636 ee3aedff Michael Hanselmann
    None for automatic choice (human-readable for non-separator usage,
2637 ee3aedff Michael Hanselmann
    otherwise megabytes); this is a one-letter string
2638 ee3aedff Michael Hanselmann
  @type separator: string or None
2639 ee3aedff Michael Hanselmann
  @param separator: String used to separate fields
2640 ee3aedff Michael Hanselmann
  @type header: bool
2641 ee3aedff Michael Hanselmann
  @param header: Whether to show header row
2642 2928de47 Michael Hanselmann
  @type force_filter: bool
2643 2928de47 Michael Hanselmann
  @param force_filter: Whether to always treat names as filter
2644 ee3aedff Michael Hanselmann
  @type format_override: dict
2645 ee3aedff Michael Hanselmann
  @param format_override: Dictionary for overriding field formatting functions,
2646 ee3aedff Michael Hanselmann
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
2647 f0b1bafe Iustin Pop
  @type verbose: boolean
2648 f0b1bafe Iustin Pop
  @param verbose: whether to use verbose field descriptions or not
2649 ee3aedff Michael Hanselmann

2650 ee3aedff Michael Hanselmann
  """
2651 ee3aedff Michael Hanselmann
  if cl is None:
2652 ee3aedff Michael Hanselmann
    cl = GetClient()
2653 ee3aedff Michael Hanselmann
2654 ee3aedff Michael Hanselmann
  if not names:
2655 ee3aedff Michael Hanselmann
    names = None
2656 ee3aedff Michael Hanselmann
2657 2928de47 Michael Hanselmann
  if (force_filter or
2658 2928de47 Michael Hanselmann
      (names and len(names) == 1 and qlang.MaybeFilter(names[0]))):
2659 2928de47 Michael Hanselmann
    try:
2660 2928de47 Michael Hanselmann
      (filter_text, ) = names
2661 2928de47 Michael Hanselmann
    except ValueError:
2662 2928de47 Michael Hanselmann
      raise errors.OpPrereqError("Exactly one argument must be given as a"
2663 2928de47 Michael Hanselmann
                                 " filter")
2664 2928de47 Michael Hanselmann
2665 2928de47 Michael Hanselmann
    logging.debug("Parsing '%s' as filter", filter_text)
2666 2928de47 Michael Hanselmann
    filter_ = qlang.ParseFilter(filter_text)
2667 2928de47 Michael Hanselmann
  else:
2668 2928de47 Michael Hanselmann
    filter_ = qlang.MakeSimpleFilter("name", names)
2669 2928de47 Michael Hanselmann
2670 2928de47 Michael Hanselmann
  response = cl.Query(resource, fields, filter_)
2671 ee3aedff Michael Hanselmann
2672 ee3aedff Michael Hanselmann
  found_unknown = _WarnUnknownFields(response.fields)
2673 ee3aedff Michael Hanselmann
2674 ee3aedff Michael Hanselmann
  (status, data) = FormatQueryResult(response, unit=unit, separator=separator,
2675 ee3aedff Michael Hanselmann
                                     header=header,
2676 f0b1bafe Iustin Pop
                                     format_override=format_override,
2677 f0b1bafe Iustin Pop
                                     verbose=verbose)
2678 ee3aedff Michael Hanselmann
2679 ee3aedff Michael Hanselmann
  for line in data:
2680 ee3aedff Michael Hanselmann
    ToStdout(line)
2681 ee3aedff Michael Hanselmann
2682 ee3aedff Michael Hanselmann
  assert ((found_unknown and status == QR_UNKNOWN) or
2683 ee3aedff Michael Hanselmann
          (not found_unknown and status != QR_UNKNOWN))
2684 ee3aedff Michael Hanselmann
2685 ee3aedff Michael Hanselmann
  if status == QR_UNKNOWN:
2686 ee3aedff Michael Hanselmann
    return constants.EXIT_UNKNOWN_FIELD
2687 ee3aedff Michael Hanselmann
2688 ee3aedff Michael Hanselmann
  # TODO: Should the list command fail if not all data could be collected?
2689 ee3aedff Michael Hanselmann
  return constants.EXIT_SUCCESS
2690 ee3aedff Michael Hanselmann
2691 ee3aedff Michael Hanselmann
2692 ee3aedff Michael Hanselmann
def GenericListFields(resource, fields, separator, header, cl=None):
2693 ee3aedff Michael Hanselmann
  """Generic implementation for listing fields for a resource.
2694 ee3aedff Michael Hanselmann

2695 abd66bf8 Michael Hanselmann
  @param resource: One of L{constants.QR_VIA_LUXI}
2696 ee3aedff Michael Hanselmann
  @type fields: list of strings
2697 ee3aedff Michael Hanselmann
  @param fields: List of fields to query for
2698 ee3aedff Michael Hanselmann
  @type separator: string or None
2699 ee3aedff Michael Hanselmann
  @param separator: String used to separate fields
2700 ee3aedff Michael Hanselmann
  @type header: bool
2701 ee3aedff Michael Hanselmann
  @param header: Whether to show header row
2702 ee3aedff Michael Hanselmann

2703 ee3aedff Michael Hanselmann
  """
2704 ee3aedff Michael Hanselmann
  if cl is None:
2705 ee3aedff Michael Hanselmann
    cl = GetClient()
2706 ee3aedff Michael Hanselmann
2707 ee3aedff Michael Hanselmann
  if not fields:
2708 ee3aedff Michael Hanselmann
    fields = None
2709 ee3aedff Michael Hanselmann
2710 ee3aedff Michael Hanselmann
  response = cl.QueryFields(resource, fields)
2711 ee3aedff Michael Hanselmann
2712 ee3aedff Michael Hanselmann
  found_unknown = _WarnUnknownFields(response.fields)
2713 ee3aedff Michael Hanselmann
2714 ee3aedff Michael Hanselmann
  columns = [
2715 ee3aedff Michael Hanselmann
    TableColumn("Name", str, False),
2716 ee3aedff Michael Hanselmann
    TableColumn("Title", str, False),
2717 ea1440c1 Michael Hanselmann
    TableColumn("Description", str, False),
2718 ee3aedff Michael Hanselmann
    ]
2719 ee3aedff Michael Hanselmann
2720 ea1440c1 Michael Hanselmann
  rows = [[fdef.name, fdef.title, fdef.doc] for fdef in response.fields]
2721 ee3aedff Michael Hanselmann
2722 ee3aedff Michael Hanselmann
  for line in FormatTable(rows, columns, header, separator):
2723 ee3aedff Michael Hanselmann
    ToStdout(line)
2724 ee3aedff Michael Hanselmann
2725 ee3aedff Michael Hanselmann
  if found_unknown:
2726 ee3aedff Michael Hanselmann
    return constants.EXIT_UNKNOWN_FIELD
2727 ee3aedff Michael Hanselmann
2728 ee3aedff Michael Hanselmann
  return constants.EXIT_SUCCESS
2729 ee3aedff Michael Hanselmann
2730 ee3aedff Michael Hanselmann
2731 ee3aedff Michael Hanselmann
class TableColumn:
2732 ee3aedff Michael Hanselmann
  """Describes a column for L{FormatTable}.
2733 ee3aedff Michael Hanselmann

2734 ee3aedff Michael Hanselmann
  """
2735 ee3aedff Michael Hanselmann
  def __init__(self, title, fn, align_right):
2736 ee3aedff Michael Hanselmann
    """Initializes this class.
2737 ee3aedff Michael Hanselmann

2738 ee3aedff Michael Hanselmann
    @type title: string
2739 ee3aedff Michael Hanselmann
    @param title: Column title
2740 ee3aedff Michael Hanselmann
    @type fn: callable
2741 ee3aedff Michael Hanselmann
    @param fn: Formatting function
2742 ee3aedff Michael Hanselmann
    @type align_right: bool
2743 ee3aedff Michael Hanselmann
    @param align_right: Whether to align values on the right-hand side
2744 ee3aedff Michael Hanselmann

2745 ee3aedff Michael Hanselmann
    """
2746 ee3aedff Michael Hanselmann
    self.title = title
2747 ee3aedff Michael Hanselmann
    self.format = fn
2748 ee3aedff Michael Hanselmann
    self.align_right = align_right
2749 ee3aedff Michael Hanselmann
2750 ee3aedff Michael Hanselmann
2751 ee3aedff Michael Hanselmann
def _GetColFormatString(width, align_right):
2752 ee3aedff Michael Hanselmann
  """Returns the format string for a field.
2753 ee3aedff Michael Hanselmann

2754 ee3aedff Michael Hanselmann
  """
2755 ee3aedff Michael Hanselmann
  if align_right:
2756 ee3aedff Michael Hanselmann
    sign = ""
2757 ee3aedff Michael Hanselmann
  else:
2758 ee3aedff Michael Hanselmann
    sign = "-"
2759 ee3aedff Michael Hanselmann
2760 ee3aedff Michael Hanselmann
  return "%%%s%ss" % (sign, width)
2761 ee3aedff Michael Hanselmann
2762 ee3aedff Michael Hanselmann
2763 ee3aedff Michael Hanselmann
def FormatTable(rows, columns, header, separator):
2764 ee3aedff Michael Hanselmann
  """Formats data as a table.
2765 ee3aedff Michael Hanselmann

2766 ee3aedff Michael Hanselmann
  @type rows: list of lists
2767 ee3aedff Michael Hanselmann
  @param rows: Row data, one list per row
2768 ee3aedff Michael Hanselmann
  @type columns: list of L{TableColumn}
2769 ee3aedff Michael Hanselmann
  @param columns: Column descriptions
2770 ee3aedff Michael Hanselmann
  @type header: bool
2771 ee3aedff Michael Hanselmann
  @param header: Whether to show header row
2772 ee3aedff Michael Hanselmann
  @type separator: string or None
2773 ee3aedff Michael Hanselmann
  @param separator: String used to separate columns
2774 ee3aedff Michael Hanselmann

2775 ee3aedff Michael Hanselmann
  """
2776 ee3aedff Michael Hanselmann
  if header:
2777 ee3aedff Michael Hanselmann
    data = [[col.title for col in columns]]
2778 ee3aedff Michael Hanselmann
    colwidth = [len(col.title) for col in columns]
2779 ee3aedff Michael Hanselmann
  else:
2780 ee3aedff Michael Hanselmann
    data = []
2781 ee3aedff Michael Hanselmann
    colwidth = [0 for _ in columns]
2782 ee3aedff Michael Hanselmann
2783 ee3aedff Michael Hanselmann
  # Format row data
2784 ee3aedff Michael Hanselmann
  for row in rows:
2785 ee3aedff Michael Hanselmann
    assert len(row) == len(columns)
2786 ee3aedff Michael Hanselmann
2787 ee3aedff Michael Hanselmann
    formatted = [col.format(value) for value, col in zip(row, columns)]
2788 ee3aedff Michael Hanselmann
2789 ee3aedff Michael Hanselmann
    if separator is None:
2790 ee3aedff Michael Hanselmann
      # Update column widths
2791 ee3aedff Michael Hanselmann
      for idx, (oldwidth, value) in enumerate(zip(colwidth, formatted)):
2792 ee3aedff Michael Hanselmann
        # Modifying a list's items while iterating is fine
2793 ee3aedff Michael Hanselmann
        colwidth[idx] = max(oldwidth, len(value))
2794 ee3aedff Michael Hanselmann
2795 ee3aedff Michael Hanselmann
    data.append(formatted)
2796 ee3aedff Michael Hanselmann
2797 ee3aedff Michael Hanselmann
  if separator is not None:
2798 ee3aedff Michael Hanselmann
    # Return early if a separator is used
2799 ee3aedff Michael Hanselmann
    return [separator.join(row) for row in data]
2800 ee3aedff Michael Hanselmann
2801 ee3aedff Michael Hanselmann
  if columns and not columns[-1].align_right:
2802 ee3aedff Michael Hanselmann
    # Avoid unnecessary spaces at end of line
2803 ee3aedff Michael Hanselmann
    colwidth[-1] = 0
2804 ee3aedff Michael Hanselmann
2805 ee3aedff Michael Hanselmann
  # Build format string
2806 ee3aedff Michael Hanselmann
  fmt = " ".join([_GetColFormatString(width, col.align_right)
2807 ee3aedff Michael Hanselmann
                  for col, width in zip(columns, colwidth)])
2808 ee3aedff Michael Hanselmann
2809 ee3aedff Michael Hanselmann
  return [fmt % tuple(row) for row in data]
2810 ee3aedff Michael Hanselmann
2811 ee3aedff Michael Hanselmann
2812 3386e7a9 Iustin Pop
def FormatTimestamp(ts):
2813 3386e7a9 Iustin Pop
  """Formats a given timestamp.
2814 3386e7a9 Iustin Pop

2815 3386e7a9 Iustin Pop
  @type ts: timestamp
2816 3386e7a9 Iustin Pop
  @param ts: a timeval-type timestamp, a tuple of seconds and microseconds
2817 3386e7a9 Iustin Pop

2818 3386e7a9 Iustin Pop
  @rtype: string
2819 5fcc718f Iustin Pop
  @return: a string with the formatted timestamp
2820 3386e7a9 Iustin Pop

2821 3386e7a9 Iustin Pop
  """
2822 e0ec0ff6 Iustin Pop
  if not isinstance (ts, (tuple, list)) or len(ts) != 2:
2823 e0ec0ff6 Iustin Pop
    return '?'
2824 3386e7a9 Iustin Pop
  sec, usec = ts
2825 3386e7a9 Iustin Pop
  return time.strftime("%F %T", time.localtime(sec)) + ".%06d" % usec
2826 2241e2b9 Iustin Pop
2827 2241e2b9 Iustin Pop
2828 2241e2b9 Iustin Pop
def ParseTimespec(value):
2829 2241e2b9 Iustin Pop
  """Parse a time specification.
2830 2241e2b9 Iustin Pop

2831 2241e2b9 Iustin Pop
  The following suffixed will be recognized:
2832 2241e2b9 Iustin Pop

2833 2241e2b9 Iustin Pop
    - s: seconds
2834 2241e2b9 Iustin Pop
    - m: minutes
2835 2241e2b9 Iustin Pop
    - h: hours
2836 2241e2b9 Iustin Pop
    - d: day
2837 2241e2b9 Iustin Pop
    - w: weeks
2838 2241e2b9 Iustin Pop

2839 2241e2b9 Iustin Pop
  Without any suffix, the value will be taken to be in seconds.
2840 2241e2b9 Iustin Pop

2841 2241e2b9 Iustin Pop
  """
2842 2241e2b9 Iustin Pop
  value = str(value)
2843 2241e2b9 Iustin Pop
  if not value:
2844 2241e2b9 Iustin Pop
    raise errors.OpPrereqError("Empty time specification passed")
2845 2241e2b9 Iustin Pop
  suffix_map = {
2846 2241e2b9 Iustin Pop
    's': 1,
2847 2241e2b9 Iustin Pop
    'm': 60,
2848 2241e2b9 Iustin Pop
    'h': 3600,
2849 2241e2b9 Iustin Pop
    'd': 86400,
2850 2241e2b9 Iustin Pop
    'w': 604800,
2851 2241e2b9 Iustin Pop
    }
2852 2241e2b9 Iustin Pop
  if value[-1] not in suffix_map:
2853 2241e2b9 Iustin Pop
    try:
2854 2241e2b9 Iustin Pop
      value = int(value)
2855 691744c4 Iustin Pop
    except (TypeError, ValueError):
2856 2241e2b9 Iustin Pop
      raise errors.OpPrereqError("Invalid time specification '%s'" % value)
2857 2241e2b9 Iustin Pop
  else:
2858 2241e2b9 Iustin Pop
    multiplier = suffix_map[value[-1]]
2859 2241e2b9 Iustin Pop
    value = value[:-1]
2860 2241e2b9 Iustin Pop
    if not value: # no data left after stripping the suffix
2861 2241e2b9 Iustin Pop
      raise errors.OpPrereqError("Invalid time specification (only"
2862 2241e2b9 Iustin Pop
                                 " suffix passed)")
2863 2241e2b9 Iustin Pop
    try:
2864 2241e2b9 Iustin Pop
      value = int(value) * multiplier
2865 691744c4 Iustin Pop
    except (TypeError, ValueError):
2866 2241e2b9 Iustin Pop
      raise errors.OpPrereqError("Invalid time specification '%s'" % value)
2867 2241e2b9 Iustin Pop
  return value
2868 46fbdd04 Iustin Pop
2869 46fbdd04 Iustin Pop
2870 e9e26bb3 Iustin Pop
def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False,
2871 05484a24 Michael Hanselmann
                   filter_master=False, nodegroup=None):
2872 4040a784 Iustin Pop
  """Returns the names of online nodes.
2873 4040a784 Iustin Pop

2874 4040a784 Iustin Pop
  This function will also log a warning on stderr with the names of
2875 4040a784 Iustin Pop
  the online nodes.
2876 4040a784 Iustin Pop

2877 4040a784 Iustin Pop
  @param nodes: if not empty, use only this subset of nodes (minus the
2878 4040a784 Iustin Pop
      offline ones)
2879 4040a784 Iustin Pop
  @param cl: if not None, luxi client to use
2880 4040a784 Iustin Pop
  @type nowarn: boolean
2881 4040a784 Iustin Pop
  @param nowarn: by default, this function will output a note with the
2882 4040a784 Iustin Pop
      offline nodes that are skipped; if this parameter is True the
2883 4040a784 Iustin Pop
      note is not displayed
2884 e9e26bb3 Iustin Pop
  @type secondary_ips: boolean
2885 e9e26bb3 Iustin Pop
  @param secondary_ips: if True, return the secondary IPs instead of the
2886 e9e26bb3 Iustin Pop
      names, useful for doing network traffic over the replication interface
2887 e9e26bb3 Iustin Pop
      (if any)
2888 e9e26bb3 Iustin Pop
  @type filter_master: boolean
2889 e9e26bb3 Iustin Pop
  @param filter_master: if True, do not return the master node in the list
2890 e9e26bb3 Iustin Pop
      (useful in coordination with secondary_ips where we cannot check our
2891 e9e26bb3 Iustin Pop
      node name against the list)
2892 05484a24 Michael Hanselmann
  @type nodegroup: string
2893 05484a24 Michael Hanselmann
  @param nodegroup: If set, only return nodes in this node group
2894 4040a784 Iustin Pop

2895 4040a784 Iustin Pop
  """
2896 4040a784 Iustin Pop
  if cl is None:
2897 4040a784 Iustin Pop
    cl = GetClient()
2898 4040a784 Iustin Pop
2899 05484a24 Michael Hanselmann
  filter_ = []
2900 05484a24 Michael Hanselmann
2901 05484a24 Michael Hanselmann
  if nodes:
2902 05484a24 Michael Hanselmann
    filter_.append(qlang.MakeSimpleFilter("name", nodes))
2903 05484a24 Michael Hanselmann
2904 05484a24 Michael Hanselmann
  if nodegroup is not None:
2905 05484a24 Michael Hanselmann
    filter_.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup],
2906 05484a24 Michael Hanselmann
                                 [qlang.OP_EQUAL, "group.uuid", nodegroup]])
2907 e9e26bb3 Iustin Pop
2908 e9e26bb3 Iustin Pop
  if filter_master:
2909 05484a24 Michael Hanselmann
    filter_.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
2910 05484a24 Michael Hanselmann
2911 05484a24 Michael Hanselmann
  if filter_:
2912 05484a24 Michael Hanselmann
    if len(filter_) > 1:
2913 05484a24 Michael Hanselmann
      final_filter = [qlang.OP_AND] + filter_
2914 05484a24 Michael Hanselmann
    else:
2915 05484a24 Michael Hanselmann
      assert len(filter_) == 1
2916 05484a24 Michael Hanselmann
      final_filter = filter_[0]
2917 e9e26bb3 Iustin Pop
  else:
2918 05484a24 Michael Hanselmann
    final_filter = None
2919 05484a24 Michael Hanselmann
2920 05484a24 Michael Hanselmann
  result = cl.Query(constants.QR_NODE, ["name", "offline", "sip"], final_filter)
2921 05484a24 Michael Hanselmann
2922 05484a24 Michael Hanselmann
  def _IsOffline(row):
2923 05484a24 Michael Hanselmann
    (_, (_, offline), _) = row
2924 05484a24 Michael Hanselmann
    return offline
2925 05484a24 Michael Hanselmann
2926 05484a24 Michael Hanselmann
  def _GetName(row):
2927 05484a24 Michael Hanselmann
    ((_, name), _, _) = row
2928 05484a24 Michael Hanselmann
    return name
2929 05484a24 Michael Hanselmann
2930 05484a24 Michael Hanselmann
  def _GetSip(row):
2931 05484a24 Michael Hanselmann
    (_, _, (_, sip)) = row
2932 05484a24 Michael Hanselmann
    return sip
2933 05484a24 Michael Hanselmann
2934 05484a24 Michael Hanselmann
  (offline, online) = compat.partition(result.data, _IsOffline)
2935 e9e26bb3 Iustin Pop
2936 4040a784 Iustin Pop
  if offline and not nowarn:
2937 05484a24 Michael Hanselmann
    ToStderr("Note: skipping offline node(s): %s" %
2938 05484a24 Michael Hanselmann
             utils.CommaJoin(map(_GetName, offline)))
2939 05484a24 Michael Hanselmann
2940 05484a24 Michael Hanselmann
  if secondary_ips:
2941 05484a24 Michael Hanselmann
    fn = _GetSip
2942 05484a24 Michael Hanselmann
  else:
2943 05484a24 Michael Hanselmann
    fn = _GetName
2944 05484a24 Michael Hanselmann
2945 05484a24 Michael Hanselmann
  return map(fn, online)
2946 4040a784 Iustin Pop
2947 4040a784 Iustin Pop
2948 46fbdd04 Iustin Pop
def _ToStream(stream, txt, *args):
2949 46fbdd04 Iustin Pop
  """Write a message to a stream, bypassing the logging system
2950 46fbdd04 Iustin Pop

2951 46fbdd04 Iustin Pop
  @type stream: file object
2952 46fbdd04 Iustin Pop
  @param stream: the file to which we should write
2953 46fbdd04 Iustin Pop
  @type txt: str
2954 46fbdd04 Iustin Pop
  @param txt: the message
2955 46fbdd04 Iustin Pop

2956 46fbdd04 Iustin Pop
  """
2957 225e2544 Iustin Pop
  try:
2958 225e2544 Iustin Pop
    if args:
2959 225e2544 Iustin Pop
      args = tuple(args)
2960 225e2544 Iustin Pop
      stream.write(txt % args)
2961 225e2544 Iustin Pop
    else:
2962 225e2544 Iustin Pop
      stream.write(txt)
2963 225e2544 Iustin Pop
    stream.write('\n')
2964 225e2544 Iustin Pop
    stream.flush()
2965 225e2544 Iustin Pop
  except IOError, err:
2966 225e2544 Iustin Pop
    if err.errno == errno.EPIPE:
2967 225e2544 Iustin Pop
      # our terminal went away, we'll exit
2968 225e2544 Iustin Pop
      sys.exit(constants.EXIT_FAILURE)
2969 225e2544 Iustin Pop
    else:
2970 225e2544 Iustin Pop
      raise
2971 46fbdd04 Iustin Pop
2972 46fbdd04 Iustin Pop
2973 46fbdd04 Iustin Pop
def ToStdout(txt, *args):
2974 46fbdd04 Iustin Pop
  """Write a message to stdout only, bypassing the logging system
2975 46fbdd04 Iustin Pop

2976 46fbdd04 Iustin Pop
  This is just a wrapper over _ToStream.
2977 46fbdd04 Iustin Pop

2978 46fbdd04 Iustin Pop
  @type txt: str
2979 46fbdd04 Iustin Pop
  @param txt: the message
2980 46fbdd04 Iustin Pop

2981 46fbdd04 Iustin Pop
  """
2982 46fbdd04 Iustin Pop
  _ToStream(sys.stdout, txt, *args)
2983 46fbdd04 Iustin Pop
2984 46fbdd04 Iustin Pop
2985 46fbdd04 Iustin Pop
def ToStderr(txt, *args):
2986 46fbdd04 Iustin Pop
  """Write a message to stderr only, bypassing the logging system
2987 46fbdd04 Iustin Pop

2988 46fbdd04 Iustin Pop
  This is just a wrapper over _ToStream.
2989 46fbdd04 Iustin Pop

2990 46fbdd04 Iustin Pop
  @type txt: str
2991 46fbdd04 Iustin Pop
  @param txt: the message
2992 46fbdd04 Iustin Pop

2993 46fbdd04 Iustin Pop
  """
2994 46fbdd04 Iustin Pop
  _ToStream(sys.stderr, txt, *args)
2995 479636a3 Iustin Pop
2996 479636a3 Iustin Pop
2997 479636a3 Iustin Pop
class JobExecutor(object):
2998 479636a3 Iustin Pop
  """Class which manages the submission and execution of multiple jobs.
2999 479636a3 Iustin Pop

3000 479636a3 Iustin Pop
  Note that instances of this class should not be reused between
3001 479636a3 Iustin Pop
  GetResults() calls.
3002 479636a3 Iustin Pop

3003 479636a3 Iustin Pop
  """
3004 919ca415 Iustin Pop
  def __init__(self, cl=None, verbose=True, opts=None, feedback_fn=None):
3005 479636a3 Iustin Pop
    self.queue = []
3006 479636a3 Iustin Pop
    if cl is None:
3007 479636a3 Iustin Pop
      cl = GetClient()
3008 479636a3 Iustin Pop
    self.cl = cl
3009 479636a3 Iustin Pop
    self.verbose = verbose
3010 23b4b983 Iustin Pop
    self.jobs = []
3011 cff5fa7f Iustin Pop
    self.opts = opts
3012 919ca415 Iustin Pop
    self.feedback_fn = feedback_fn
3013 60452edf Michael Hanselmann
    self._counter = itertools.count()
3014 479636a3 Iustin Pop
3015 8d99a8bf Michael Hanselmann
  @staticmethod
3016 8d99a8bf Michael Hanselmann
  def _IfName(name, fmt):
3017 8d99a8bf Michael Hanselmann
    """Helper function for formatting name.
3018 8d99a8bf Michael Hanselmann

3019 8d99a8bf Michael Hanselmann
    """
3020 8d99a8bf Michael Hanselmann
    if name:
3021 8d99a8bf Michael Hanselmann
      return fmt % name
3022 8d99a8bf Michael Hanselmann
3023 8d99a8bf Michael Hanselmann
    return ""
3024 8d99a8bf Michael Hanselmann
3025 479636a3 Iustin Pop
  def QueueJob(self, name, *ops):
3026 23b4b983 Iustin Pop
    """Record a job for later submit.
3027 479636a3 Iustin Pop

3028 479636a3 Iustin Pop
    @type name: string
3029 479636a3 Iustin Pop
    @param name: a description of the job, will be used in WaitJobSet
3030 8d99a8bf Michael Hanselmann

3031 479636a3 Iustin Pop
    """
3032 cff5fa7f Iustin Pop
    SetGenericOpcodeOpts(ops, self.opts)
3033 60452edf Michael Hanselmann
    self.queue.append((self._counter.next(), name, ops))
3034 23b4b983 Iustin Pop
3035 8d99a8bf Michael Hanselmann
  def AddJobId(self, name, status, job_id):
3036 8d99a8bf Michael Hanselmann
    """Adds a job ID to the internal queue.
3037 8d99a8bf Michael Hanselmann

3038 8d99a8bf Michael Hanselmann
    """
3039 8d99a8bf Michael Hanselmann
    self.jobs.append((self._counter.next(), status, job_id, name))
3040 8d99a8bf Michael Hanselmann
3041 66ecc479 Guido Trotter
  def SubmitPending(self, each=False):
3042 23b4b983 Iustin Pop
    """Submit all pending jobs.
3043 23b4b983 Iustin Pop

3044 23b4b983 Iustin Pop
    """
3045 66ecc479 Guido Trotter
    if each:
3046 66ecc479 Guido Trotter
      results = []
3047 60452edf Michael Hanselmann
      for (_, _, ops) in self.queue:
3048 66ecc479 Guido Trotter
        # SubmitJob will remove the success status, but raise an exception if
3049 66ecc479 Guido Trotter
        # the submission fails, so we'll notice that anyway.
3050 60452edf Michael Hanselmann
        results.append([True, self.cl.SubmitJob(ops)])
3051 66ecc479 Guido Trotter
    else:
3052 60452edf Michael Hanselmann
      results = self.cl.SubmitManyJobs([ops for (_, _, ops) in self.queue])
3053 60452edf Michael Hanselmann
    for ((status, data), (idx, name, _)) in zip(results, self.queue):
3054 5299e61f Iustin Pop
      self.jobs.append((idx, status, data, name))
3055 5299e61f Iustin Pop
3056 5299e61f Iustin Pop
  def _ChooseJob(self):
3057 5299e61f Iustin Pop
    """Choose a non-waiting/queued job to poll next.
3058 5299e61f Iustin Pop

3059 5299e61f Iustin Pop
    """
3060 5299e61f Iustin Pop
    assert self.jobs, "_ChooseJob called with empty job list"
3061 5299e61f Iustin Pop
3062 5299e61f Iustin Pop
    result = self.cl.QueryJobs([i[2] for i in self.jobs], ["status"])
3063 5299e61f Iustin Pop
    assert result
3064 5299e61f Iustin Pop
3065 5299e61f Iustin Pop
    for job_data, status in zip(self.jobs, result):
3066 91c622a8 Iustin Pop
      if (isinstance(status, list) and status and
3067 91c622a8 Iustin Pop
          status[0] in (constants.JOB_STATUS_QUEUED,
3068 91c622a8 Iustin Pop
                        constants.JOB_STATUS_WAITLOCK,
3069 91c622a8 Iustin Pop
                        constants.JOB_STATUS_CANCELING)):
3070 91c622a8 Iustin Pop
        # job is still present and waiting
3071 5299e61f Iustin Pop
        continue
3072 91c622a8 Iustin Pop
      # good candidate found (either running job or lost job)
3073 5299e61f Iustin Pop
      self.jobs.remove(job_data)
3074 5299e61f Iustin Pop
      return job_data
3075 5299e61f Iustin Pop
3076 5299e61f Iustin Pop
    # no job found
3077 5299e61f Iustin Pop
    return self.jobs.pop(0)
3078 479636a3 Iustin Pop
3079 479636a3 Iustin Pop
  def GetResults(self):
3080 479636a3 Iustin Pop
    """Wait for and return the results of all jobs.
3081 479636a3 Iustin Pop

3082 479636a3 Iustin Pop
    @rtype: list
3083 479636a3 Iustin Pop
    @return: list of tuples (success, job results), in the same order
3084 479636a3 Iustin Pop
        as the submitted jobs; if a job has failed, instead of the result
3085 479636a3 Iustin Pop
        there will be the error message
3086 479636a3 Iustin Pop

3087 479636a3 Iustin Pop
    """
3088 23b4b983 Iustin Pop
    if not self.jobs:
3089 23b4b983 Iustin Pop
      self.SubmitPending()
3090 479636a3 Iustin Pop
    results = []
3091 479636a3 Iustin Pop
    if self.verbose:
3092 5299e61f Iustin Pop
      ok_jobs = [row[2] for row in self.jobs if row[1]]
3093 23b4b983 Iustin Pop
      if ok_jobs:
3094 1f864b60 Iustin Pop
        ToStdout("Submitted jobs %s", utils.CommaJoin(ok_jobs))
3095 5299e61f Iustin Pop
3096 5299e61f Iustin Pop
    # first, remove any non-submitted jobs
3097 cea881e5 Michael Hanselmann
    self.jobs, failures = compat.partition(self.jobs, lambda x: x[1])
3098 5299e61f Iustin Pop
    for idx, _, jid, name in failures:
3099 8d99a8bf Michael Hanselmann
      ToStderr("Failed to submit job%s: %s", self._IfName(name, " for %s"), jid)
3100 c63355f2 Iustin Pop
      results.append((idx, False, jid))
3101 5299e61f Iustin Pop
3102 5299e61f Iustin Pop
    while self.jobs:
3103 5299e61f Iustin Pop
      (idx, _, jid, name) = self._ChooseJob()
3104 8d99a8bf Michael Hanselmann
      ToStdout("Waiting for job %s%s ...", jid, self._IfName(name, " for %s"))
3105 479636a3 Iustin Pop
      try:
3106 919ca415 Iustin Pop
        job_result = PollJob(jid, cl=self.cl, feedback_fn=self.feedback_fn)
3107 479636a3 Iustin Pop
        success = True
3108 91c622a8 Iustin Pop
      except errors.JobLost, err:
3109 91c622a8 Iustin Pop
        _, job_result = FormatError(err)
3110 8d99a8bf Michael Hanselmann
        ToStderr("Job %s%s has been archived, cannot check its result",
3111 8d99a8bf Michael Hanselmann
                 jid, self._IfName(name, " for %s"))
3112 91c622a8 Iustin Pop
        success = False
3113 479636a3 Iustin Pop
      except (errors.GenericError, luxi.ProtocolError), err:
3114 479636a3 Iustin Pop
        _, job_result = FormatError(err)
3115 479636a3 Iustin Pop
        success = False
3116 479636a3 Iustin Pop
        # the error message will always be shown, verbose or not
3117 8d99a8bf Michael Hanselmann
        ToStderr("Job %s%s has failed: %s",
3118 8d99a8bf Michael Hanselmann
                 jid, self._IfName(name, " for %s"), job_result)
3119 479636a3 Iustin Pop
3120 5299e61f Iustin Pop
      results.append((idx, success, job_result))
3121 5299e61f Iustin Pop
3122 5299e61f Iustin Pop
    # sort based on the index, then drop it
3123 5299e61f Iustin Pop
    results.sort()
3124 5299e61f Iustin Pop
    results = [i[1:] for i in results]
3125 5299e61f Iustin Pop
3126 479636a3 Iustin Pop
    return results
3127 479636a3 Iustin Pop
3128 479636a3 Iustin Pop
  def WaitOrShow(self, wait):
3129 479636a3 Iustin Pop
    """Wait for job results or only print the job IDs.
3130 479636a3 Iustin Pop

3131 479636a3 Iustin Pop
    @type wait: boolean
3132 479636a3 Iustin Pop
    @param wait: whether to wait or not
3133 479636a3 Iustin Pop

3134 479636a3 Iustin Pop
    """
3135 479636a3 Iustin Pop
    if wait:
3136 479636a3 Iustin Pop
      return self.GetResults()
3137 479636a3 Iustin Pop
    else:
3138 23b4b983 Iustin Pop
      if not self.jobs:
3139 23b4b983 Iustin Pop
        self.SubmitPending()
3140 71834b2a Guido Trotter
      for _, status, result, name in self.jobs:
3141 23b4b983 Iustin Pop
        if status:
3142 23b4b983 Iustin Pop
          ToStdout("%s: %s", result, name)
3143 23b4b983 Iustin Pop
        else:
3144 23b4b983 Iustin Pop
          ToStderr("Failure for %s: %s", name, result)
3145 53a8a54d Iustin Pop
      return [row[1:3] for row in self.jobs]
3146 acd19189 René Nussbaumer
3147 acd19189 René Nussbaumer
3148 acd19189 René Nussbaumer
def FormatParameterDict(buf, param_dict, actual, level=1):
3149 acd19189 René Nussbaumer
  """Formats a parameter dictionary.
3150 acd19189 René Nussbaumer

3151 acd19189 René Nussbaumer
  @type buf: L{StringIO}
3152 acd19189 René Nussbaumer
  @param buf: the buffer into which to write
3153 acd19189 René Nussbaumer
  @type param_dict: dict
3154 acd19189 René Nussbaumer
  @param param_dict: the own parameters
3155 acd19189 René Nussbaumer
  @type actual: dict
3156 acd19189 René Nussbaumer
  @param actual: the current parameter set (including defaults)
3157 acd19189 René Nussbaumer
  @param level: Level of indent
3158 acd19189 René Nussbaumer

3159 acd19189 René Nussbaumer
  """
3160 acd19189 René Nussbaumer
  indent = "  " * level
3161 acd19189 René Nussbaumer
  for key in sorted(actual):
3162 acd19189 René Nussbaumer
    val = param_dict.get(key, "default (%s)" % actual[key])
3163 acd19189 René Nussbaumer
    buf.write("%s- %s: %s\n" % (indent, key, val))
3164 25bd815c René Nussbaumer
3165 25bd815c René Nussbaumer
3166 25bd815c René Nussbaumer
def ConfirmOperation(names, list_type, text, extra=""):
3167 25bd815c René Nussbaumer
  """Ask the user to confirm an operation on a list of list_type.
3168 25bd815c René Nussbaumer

3169 25bd815c René Nussbaumer
  This function is used to request confirmation for doing an operation
3170 25bd815c René Nussbaumer
  on a given list of list_type.
3171 25bd815c René Nussbaumer

3172 25bd815c René Nussbaumer
  @type names: list
3173 25bd815c René Nussbaumer
  @param names: the list of names that we display when
3174 25bd815c René Nussbaumer
      we ask for confirmation
3175 25bd815c René Nussbaumer
  @type list_type: str
3176 25bd815c René Nussbaumer
  @param list_type: Human readable name for elements in the list (e.g. nodes)
3177 25bd815c René Nussbaumer
  @type text: str
3178 25bd815c René Nussbaumer
  @param text: the operation that the user should confirm
3179 25bd815c René Nussbaumer
  @rtype: boolean
3180 25bd815c René Nussbaumer
  @return: True or False depending on user's confirmation.
3181 25bd815c René Nussbaumer

3182 25bd815c René Nussbaumer
  """
3183 25bd815c René Nussbaumer
  count = len(names)
3184 25bd815c René Nussbaumer
  msg = ("The %s will operate on %d %s.\n%s"
3185 25bd815c René Nussbaumer
         "Do you want to continue?" % (text, count, list_type, extra))
3186 25bd815c René Nussbaumer
  affected = (("\nAffected %s:\n" % list_type) +
3187 25bd815c René Nussbaumer
              "\n".join(["  %s" % name for name in names]))
3188 25bd815c René Nussbaumer
3189 25bd815c René Nussbaumer
  choices = [("y", True, "Yes, execute the %s" % text),
3190 25bd815c René Nussbaumer
             ("n", False, "No, abort the %s" % text)]
3191 25bd815c René Nussbaumer
3192 25bd815c René Nussbaumer
  if count > 20:
3193 25bd815c René Nussbaumer
    choices.insert(1, ("v", "v", "View the list of affected %s" % list_type))
3194 25bd815c René Nussbaumer
    question = msg
3195 25bd815c René Nussbaumer
  else:
3196 25bd815c René Nussbaumer
    question = msg + affected
3197 25bd815c René Nussbaumer
3198 25bd815c René Nussbaumer
  choice = AskUser(question, choices)
3199 25bd815c René Nussbaumer
  if choice == "v":
3200 25bd815c René Nussbaumer
    choices.pop(1)
3201 25bd815c René Nussbaumer
    choice = AskUser(msg + affected, choices)
3202 25bd815c René Nussbaumer
  return choice