Statistics
| Branch: | Tag: | Revision:

root / lib / cli.py @ 10889e0c

History | View | Annotate | Download (108 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 ef9fa5b9 René Nussbaumer
import shlex
33 73702ee7 Iustin Pop
from cStringIO import StringIO
34 a8083063 Iustin Pop
35 a8083063 Iustin Pop
from ganeti import utils
36 a8083063 Iustin Pop
from ganeti import errors
37 a8083063 Iustin Pop
from ganeti import constants
38 846baef9 Iustin Pop
from ganeti import opcodes
39 ceab32dd Iustin Pop
from ganeti import luxi
40 b33e986b Iustin Pop
from ganeti import ssconf
41 4331f6cd Michael Hanselmann
from ganeti import rpc
42 7e49b6ce Michael Hanselmann
from ganeti import ssh
43 cea881e5 Michael Hanselmann
from ganeti import compat
44 a744b676 Manuel Franceschini
from ganeti import netutils
45 ee3aedff Michael Hanselmann
from ganeti import qlang
46 a8083063 Iustin Pop
47 c38c44ad Michael Hanselmann
from optparse import (OptionParser, TitledHelpFormatter,
48 38206f3c Iustin Pop
                      Option, OptionValueError)
49 a8083063 Iustin Pop
50 03298ebe Michael Hanselmann
51 4abc4f1e Iustin Pop
__all__ = [
52 4abc4f1e Iustin Pop
  # Command line options
53 fdad8c4d Balazs Lecz
  "ADD_UIDS_OPT",
54 e7e09483 Iustin Pop
  "ALLOCATABLE_OPT",
55 90e99856 Adeodato Simo
  "ALLOC_POLICY_OPT",
56 2d5e7ae1 Iustin Pop
  "ALL_OPT",
57 e9c487be René Nussbaumer
  "ALLOW_FAILOVER_OPT",
58 4c61d894 Iustin Pop
  "AUTO_PROMOTE_OPT",
59 e00f7a05 Iustin Pop
  "AUTO_REPLACE_OPT",
60 087ed2ed Iustin Pop
  "BACKEND_OPT",
61 61a14bb3 Iustin Pop
  "BLK_OS_OPT",
62 f91e255a Iustin Pop
  "CAPAB_MASTER_OPT",
63 53919782 Iustin Pop
  "CAPAB_VM_OPT",
64 baef337d Iustin Pop
  "CLEANUP_OPT",
65 3db3eb2a Michael Hanselmann
  "CLUSTER_DOMAIN_SECRET_OPT",
66 4abc4f1e Iustin Pop
  "CONFIRM_OPT",
67 e32df528 Iustin Pop
  "CP_SIZE_OPT",
68 4abc4f1e Iustin Pop
  "DEBUG_OPT",
69 a0c9776a Iustin Pop
  "DEBUG_SIMERR_OPT",
70 4b038a1e Iustin Pop
  "DISKIDX_OPT",
71 e3876ccb Iustin Pop
  "DISK_OPT",
72 bc5d0215 Andrea Spadaccini
  "DISK_PARAMS_OPT",
73 4b038a1e Iustin Pop
  "DISK_TEMPLATE_OPT",
74 771734c9 Iustin Pop
  "DRAINED_OPT",
75 a0a6ff34 Iustin Pop
  "DRY_RUN_OPT",
76 26591bfd Luca Bigliardi
  "DRBD_HELPER_OPT",
77 1b7761fd Apollon Oikonomopoulos
  "DST_NODE_OPT",
78 7ea7bcf6 Iustin Pop
  "EARLY_RELEASE_OPT",
79 383a3591 Iustin Pop
  "ENABLED_HV_OPT",
80 14e9e7f3 Iustin Pop
  "ERROR_CODES_OPT",
81 4abc4f1e Iustin Pop
  "FIELDS_OPT",
82 4a25828c Iustin Pop
  "FILESTORE_DIR_OPT",
83 0f87c43e Iustin Pop
  "FILESTORE_DRIVER_OPT",
84 442587bf Michael Hanselmann
  "FORCE_FILTER_OPT",
85 06073e85 Guido Trotter
  "FORCE_OPT",
86 06073e85 Guido Trotter
  "FORCE_VARIANT_OPT",
87 29392516 Iustin Pop
  "GLOBAL_FILEDIR_OPT",
88 61a14bb3 Iustin Pop
  "HID_OS_OPT",
89 4b97f902 Apollon Oikonomopoulos
  "GLOBAL_SHARED_FILEDIR_OPT",
90 073271f6 Iustin Pop
  "HVLIST_OPT",
91 48f212d7 Iustin Pop
  "HVOPTS_OPT",
92 236fd9c4 Iustin Pop
  "HYPERVISOR_OPT",
93 4eb62659 Iustin Pop
  "IALLOCATOR_OPT",
94 bf4af505 Apollon Oikonomopoulos
  "DEFAULT_IALLOCATOR_OPT",
95 e588764d Iustin Pop
  "IDENTIFY_DEFAULTS_OPT",
96 82a786d5 Iustin Pop
  "IGNORE_CONSIST_OPT",
97 93f2399e Andrea Spadaccini
  "IGNORE_ERRORS_OPT",
98 b6e841a8 Iustin Pop
  "IGNORE_FAILURES_OPT",
99 b44bd844 Michael Hanselmann
  "IGNORE_OFFLINE_OPT",
100 8d8d650c Michael Hanselmann
  "IGNORE_REMOVE_FAILURES_OPT",
101 ee3f9578 Iustin Pop
  "IGNORE_SECONDARIES_OPT",
102 05586c90 Iustin Pop
  "IGNORE_SIZE_OPT",
103 19b9ba9a Michael Hanselmann
  "INTERVAL_OPT",
104 e3646f22 Iustin Pop
  "MAC_PREFIX_OPT",
105 3953242f Iustin Pop
  "MAINTAIN_NODE_HEALTH_OPT",
106 29392516 Iustin Pop
  "MASTER_NETDEV_OPT",
107 5a8648eb Andrea Spadaccini
  "MASTER_NETMASK_OPT",
108 771734c9 Iustin Pop
  "MC_OPT",
109 783a6c0b Iustin Pop
  "MIGRATION_MODE_OPT",
110 7d3a9fab Iustin Pop
  "NET_OPT",
111 6d4a1656 Michael Hanselmann
  "NEW_CLUSTER_CERT_OPT",
112 3db3eb2a Michael Hanselmann
  "NEW_CLUSTER_DOMAIN_SECRET_OPT",
113 6b7d5878 Michael Hanselmann
  "NEW_CONFD_HMAC_KEY_OPT",
114 6d4a1656 Michael Hanselmann
  "NEW_RAPI_CERT_OPT",
115 a14db5ff Iustin Pop
  "NEW_SECONDARY_OPT",
116 b6267745 Andrea Spadaccini
  "NEW_SPICE_CERT_OPT",
117 4fbc93dd Iustin Pop
  "NIC_PARAMS_OPT",
118 61413377 Stephen Shirley
  "NODE_FORCE_JOIN_OPT",
119 7edc4637 Iustin Pop
  "NODE_LIST_OPT",
120 990b7886 Iustin Pop
  "NODE_PLACEMENT_OPT",
121 5fbbd028 Guido Trotter
  "NODEGROUP_OPT",
122 a535cef7 René Nussbaumer
  "NODE_PARAMS_OPT",
123 dd94e9f6 René Nussbaumer
  "NODE_POWERED_OPT",
124 26591bfd Luca Bigliardi
  "NODRBD_STORAGE_OPT",
125 4abc4f1e Iustin Pop
  "NOHDR_OPT",
126 91e0748c Iustin Pop
  "NOIPCHECK_OPT",
127 25a8792c Iustin Pop
  "NO_INSTALL_OPT",
128 460d22be Iustin Pop
  "NONAMECHECK_OPT",
129 831040bf Iustin Pop
  "NOLVM_STORAGE_OPT",
130 29392516 Iustin Pop
  "NOMODIFY_ETCHOSTS_OPT",
131 b989b9d9 Ken Wehr
  "NOMODIFY_SSH_SETUP_OPT",
132 26023ecd Iustin Pop
  "NONICS_OPT",
133 f2a0828c Iustin Pop
  "NONLIVE_OPT",
134 14e9e7f3 Iustin Pop
  "NONPLUS1_OPT",
135 44c44832 Iustin Pop
  "NOSHUTDOWN_OPT",
136 edeb878a Iustin Pop
  "NOSTART_OPT",
137 fcdde7f2 Iustin Pop
  "NOSSH_KEYCHECK_OPT",
138 58371861 Iustin Pop
  "NOVOTING_OPT",
139 885a0fc4 Iustin Pop
  "NO_REMEMBER_OPT",
140 3f75b4f3 Iustin Pop
  "NWSYNC_OPT",
141 57de31c0 Agata Murawska
  "OFFLINE_INST_OPT",
142 57de31c0 Agata Murawska
  "ONLINE_INST_OPT",
143 a72d0a79 Iustin Pop
  "ON_PRIMARY_OPT",
144 feb09e6a Iustin Pop
  "ON_SECONDARY_OPT",
145 771734c9 Iustin Pop
  "OFFLINE_OPT",
146 062a7100 Iustin Pop
  "OSPARAMS_OPT",
147 d3ed23ff Iustin Pop
  "OS_OPT",
148 ff00c1a7 Iustin Pop
  "OS_SIZE_OPT",
149 41543d8b René Nussbaumer
  "OOB_TIMEOUT_OPT",
150 0c086a13 René Nussbaumer
  "POWER_DELAY_OPT",
151 b883637f René Nussbaumer
  "PREALLOC_WIPE_DISKS_OPT",
152 e7323b5e Manuel Franceschini
  "PRIMARY_IP_VERSION_OPT",
153 aafee533 Michael Hanselmann
  "PRIMARY_ONLY_OPT",
154 84a5b33c Michael Hanselmann
  "PRIORITY_OPT",
155 6d4a1656 Michael Hanselmann
  "RAPI_CERT_OPT",
156 b8d0f938 Iustin Pop
  "READD_OPT",
157 12054861 Iustin Pop
  "REBOOT_TYPE_OPT",
158 8d8d650c Michael Hanselmann
  "REMOVE_INSTANCE_OPT",
159 fdad8c4d Balazs Lecz
  "REMOVE_UIDS_OPT",
160 f38ea602 Iustin Pop
  "RESERVED_LVS_OPT",
161 31d97b2a Guido Trotter
  "ROMAN_OPT",
162 8d823629 Iustin Pop
  "SECONDARY_IP_OPT",
163 aafee533 Michael Hanselmann
  "SECONDARY_ONLY_OPT",
164 67840b40 Iustin Pop
  "SELECT_OS_OPT",
165 4abc4f1e Iustin Pop
  "SEP_OPT",
166 fdcf4d84 Iustin Pop
  "SHOWCMD_OPT",
167 7e5eaaa8 Guido Trotter
  "SHUTDOWN_TIMEOUT_OPT",
168 f36d7d81 Iustin Pop
  "SINGLE_NODE_OPT",
169 32017174 Agata Murawska
  "SPECS_CPU_COUNT_OPT",
170 32017174 Agata Murawska
  "SPECS_DISK_COUNT_OPT",
171 32017174 Agata Murawska
  "SPECS_DISK_SIZE_OPT",
172 32017174 Agata Murawska
  "SPECS_MEM_SIZE_OPT",
173 32017174 Agata Murawska
  "SPECS_NIC_COUNT_OPT",
174 b6267745 Andrea Spadaccini
  "SPICE_CACERT_OPT",
175 b6267745 Andrea Spadaccini
  "SPICE_CERT_OPT",
176 df62e5db Iustin Pop
  "SRC_DIR_OPT",
177 df62e5db Iustin Pop
  "SRC_NODE_OPT",
178 4abc4f1e Iustin Pop
  "SUBMIT_OPT",
179 323f9095 Stephen Shirley
  "STARTUP_PAUSED_OPT",
180 99a8c799 Iustin Pop
  "STATIC_OPT",
181 4abc4f1e Iustin Pop
  "SYNC_OPT",
182 a57981c5 Apollon Oikonomopoulos
  "TAG_ADD_OPT",
183 4abc4f1e Iustin Pop
  "TAG_SRC_OPT",
184 b5762e2a Guido Trotter
  "TIMEOUT_OPT",
185 f6eb380d Michael Hanselmann
  "TO_GROUP_OPT",
186 1338f2b4 Balazs Lecz
  "UIDPOOL_OPT",
187 4abc4f1e Iustin Pop
  "USEUNITS_OPT",
188 bf689b7a Andrea Spadaccini
  "USE_EXTERNAL_MIP_SCRIPT",
189 74adc100 Iustin Pop
  "USE_REPL_NET_OPT",
190 9cdb9578 Iustin Pop
  "VERBOSE_OPT",
191 b58726e8 Iustin Pop
  "VG_NAME_OPT",
192 1f587d3d Iustin Pop
  "YES_DOIT_OPT",
193 38f29a36 René Nussbaumer
  "DISK_STATE_OPT",
194 38f29a36 René Nussbaumer
  "HV_STATE_OPT",
195 b6aaf437 René Nussbaumer
  "IGNORE_IPOLICY_OPT",
196 4abc4f1e Iustin Pop
  # Generic functions for CLI programs
197 25bd815c René Nussbaumer
  "ConfirmOperation",
198 4abc4f1e Iustin Pop
  "GenericMain",
199 d77490c5 Iustin Pop
  "GenericInstanceCreate",
200 ee3aedff Michael Hanselmann
  "GenericList",
201 ee3aedff Michael Hanselmann
  "GenericListFields",
202 4abc4f1e Iustin Pop
  "GetClient",
203 4abc4f1e Iustin Pop
  "GetOnlineNodes",
204 4abc4f1e Iustin Pop
  "JobExecutor",
205 4abc4f1e Iustin Pop
  "JobSubmittedException",
206 4abc4f1e Iustin Pop
  "ParseTimespec",
207 7e49b6ce Michael Hanselmann
  "RunWhileClusterStopped",
208 4abc4f1e Iustin Pop
  "SubmitOpCode",
209 4abc4f1e Iustin Pop
  "SubmitOrSend",
210 4abc4f1e Iustin Pop
  "UsesRPC",
211 4abc4f1e Iustin Pop
  # Formatting functions
212 4abc4f1e Iustin Pop
  "ToStderr", "ToStdout",
213 4abc4f1e Iustin Pop
  "FormatError",
214 ee3aedff Michael Hanselmann
  "FormatQueryResult",
215 acd19189 René Nussbaumer
  "FormatParameterDict",
216 4abc4f1e Iustin Pop
  "GenerateTable",
217 4abc4f1e Iustin Pop
  "AskUser",
218 4abc4f1e Iustin Pop
  "FormatTimestamp",
219 8a7f1c61 Michael Hanselmann
  "FormatLogMessage",
220 4abc4f1e Iustin Pop
  # Tags functions
221 4abc4f1e Iustin Pop
  "ListTags",
222 4abc4f1e Iustin Pop
  "AddTags",
223 4abc4f1e Iustin Pop
  "RemoveTags",
224 4abc4f1e Iustin Pop
  # command line options support infrastructure
225 4abc4f1e Iustin Pop
  "ARGS_MANY_INSTANCES",
226 4abc4f1e Iustin Pop
  "ARGS_MANY_NODES",
227 667dbd6b Adeodato Simo
  "ARGS_MANY_GROUPS",
228 4abc4f1e Iustin Pop
  "ARGS_NONE",
229 4abc4f1e Iustin Pop
  "ARGS_ONE_INSTANCE",
230 4abc4f1e Iustin Pop
  "ARGS_ONE_NODE",
231 667dbd6b Adeodato Simo
  "ARGS_ONE_GROUP",
232 f9faf9c3 René Nussbaumer
  "ARGS_ONE_OS",
233 4abc4f1e Iustin Pop
  "ArgChoice",
234 4abc4f1e Iustin Pop
  "ArgCommand",
235 4abc4f1e Iustin Pop
  "ArgFile",
236 667dbd6b Adeodato Simo
  "ArgGroup",
237 4abc4f1e Iustin Pop
  "ArgHost",
238 4abc4f1e Iustin Pop
  "ArgInstance",
239 4abc4f1e Iustin Pop
  "ArgJobId",
240 4abc4f1e Iustin Pop
  "ArgNode",
241 f9faf9c3 René Nussbaumer
  "ArgOs",
242 4abc4f1e Iustin Pop
  "ArgSuggest",
243 4abc4f1e Iustin Pop
  "ArgUnknown",
244 4abc4f1e Iustin Pop
  "OPT_COMPL_INST_ADD_NODES",
245 4abc4f1e Iustin Pop
  "OPT_COMPL_MANY_NODES",
246 4abc4f1e Iustin Pop
  "OPT_COMPL_ONE_IALLOCATOR",
247 4abc4f1e Iustin Pop
  "OPT_COMPL_ONE_INSTANCE",
248 4abc4f1e Iustin Pop
  "OPT_COMPL_ONE_NODE",
249 36e247e1 Guido Trotter
  "OPT_COMPL_ONE_NODEGROUP",
250 4abc4f1e Iustin Pop
  "OPT_COMPL_ONE_OS",
251 4abc4f1e Iustin Pop
  "cli_option",
252 4abc4f1e Iustin Pop
  "SplitNodeOption",
253 07150497 Guido Trotter
  "CalculateOSNames",
254 a4ebd726 Michael Hanselmann
  "ParseFields",
255 eb28ecf6 Guido Trotter
  "COMMON_CREATE_OPTS",
256 4abc4f1e Iustin Pop
  ]
257 846baef9 Iustin Pop
258 8b46606c Guido Trotter
NO_PREFIX = "no_"
259 8b46606c Guido Trotter
UN_PREFIX = "-"
260 846baef9 Iustin Pop
261 84a5b33c Michael Hanselmann
#: Priorities (sorted)
262 84a5b33c Michael Hanselmann
_PRIORITY_NAMES = [
263 84a5b33c Michael Hanselmann
  ("low", constants.OP_PRIO_LOW),
264 84a5b33c Michael Hanselmann
  ("normal", constants.OP_PRIO_NORMAL),
265 84a5b33c Michael Hanselmann
  ("high", constants.OP_PRIO_HIGH),
266 84a5b33c Michael Hanselmann
  ]
267 84a5b33c Michael Hanselmann
268 84a5b33c Michael Hanselmann
#: Priority dictionary for easier lookup
269 84a5b33c Michael Hanselmann
# TODO: Replace this and _PRIORITY_NAMES with a single sorted dictionary once
270 84a5b33c Michael Hanselmann
# we migrate to Python 2.6
271 84a5b33c Michael Hanselmann
_PRIONAME_TO_VALUE = dict(_PRIORITY_NAMES)
272 84a5b33c Michael Hanselmann
273 ee3aedff Michael Hanselmann
# Query result status for clients
274 ee3aedff Michael Hanselmann
(QR_NORMAL,
275 ee3aedff Michael Hanselmann
 QR_UNKNOWN,
276 ee3aedff Michael Hanselmann
 QR_INCOMPLETE) = range(3)
277 ee3aedff Michael Hanselmann
278 11705e3d Iustin Pop
#: Maximum batch size for ChooseJob
279 11705e3d Iustin Pop
_CHOOSE_BATCH = 25
280 11705e3d Iustin Pop
281 03298ebe Michael Hanselmann
282 863d7f46 Michael Hanselmann
class _Argument:
283 b459a848 Andrea Spadaccini
  def __init__(self, min=0, max=None): # pylint: disable=W0622
284 863d7f46 Michael Hanselmann
    self.min = min
285 863d7f46 Michael Hanselmann
    self.max = max
286 863d7f46 Michael Hanselmann
287 863d7f46 Michael Hanselmann
  def __repr__(self):
288 863d7f46 Michael Hanselmann
    return ("<%s min=%s max=%s>" %
289 863d7f46 Michael Hanselmann
            (self.__class__.__name__, self.min, self.max))
290 863d7f46 Michael Hanselmann
291 863d7f46 Michael Hanselmann
292 863d7f46 Michael Hanselmann
class ArgSuggest(_Argument):
293 863d7f46 Michael Hanselmann
  """Suggesting argument.
294 863d7f46 Michael Hanselmann

295 863d7f46 Michael Hanselmann
  Value can be any of the ones passed to the constructor.
296 863d7f46 Michael Hanselmann

297 863d7f46 Michael Hanselmann
  """
298 b459a848 Andrea Spadaccini
  # pylint: disable=W0622
299 863d7f46 Michael Hanselmann
  def __init__(self, min=0, max=None, choices=None):
300 863d7f46 Michael Hanselmann
    _Argument.__init__(self, min=min, max=max)
301 863d7f46 Michael Hanselmann
    self.choices = choices
302 863d7f46 Michael Hanselmann
303 863d7f46 Michael Hanselmann
  def __repr__(self):
304 863d7f46 Michael Hanselmann
    return ("<%s min=%s max=%s choices=%r>" %
305 863d7f46 Michael Hanselmann
            (self.__class__.__name__, self.min, self.max, self.choices))
306 863d7f46 Michael Hanselmann
307 863d7f46 Michael Hanselmann
308 863d7f46 Michael Hanselmann
class ArgChoice(ArgSuggest):
309 863d7f46 Michael Hanselmann
  """Choice argument.
310 863d7f46 Michael Hanselmann

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

314 863d7f46 Michael Hanselmann
  """
315 863d7f46 Michael Hanselmann
316 863d7f46 Michael Hanselmann
317 863d7f46 Michael Hanselmann
class ArgUnknown(_Argument):
318 863d7f46 Michael Hanselmann
  """Unknown argument to program (e.g. determined at runtime).
319 863d7f46 Michael Hanselmann

320 863d7f46 Michael Hanselmann
  """
321 863d7f46 Michael Hanselmann
322 863d7f46 Michael Hanselmann
323 863d7f46 Michael Hanselmann
class ArgInstance(_Argument):
324 863d7f46 Michael Hanselmann
  """Instances argument.
325 863d7f46 Michael Hanselmann

326 863d7f46 Michael Hanselmann
  """
327 863d7f46 Michael Hanselmann
328 863d7f46 Michael Hanselmann
329 863d7f46 Michael Hanselmann
class ArgNode(_Argument):
330 863d7f46 Michael Hanselmann
  """Node argument.
331 863d7f46 Michael Hanselmann

332 863d7f46 Michael Hanselmann
  """
333 863d7f46 Michael Hanselmann
334 667dbd6b Adeodato Simo
335 667dbd6b Adeodato Simo
class ArgGroup(_Argument):
336 667dbd6b Adeodato Simo
  """Node group argument.
337 667dbd6b Adeodato Simo

338 667dbd6b Adeodato Simo
  """
339 667dbd6b Adeodato Simo
340 667dbd6b Adeodato Simo
341 863d7f46 Michael Hanselmann
class ArgJobId(_Argument):
342 863d7f46 Michael Hanselmann
  """Job ID argument.
343 863d7f46 Michael Hanselmann

344 863d7f46 Michael Hanselmann
  """
345 863d7f46 Michael Hanselmann
346 863d7f46 Michael Hanselmann
347 863d7f46 Michael Hanselmann
class ArgFile(_Argument):
348 863d7f46 Michael Hanselmann
  """File path argument.
349 863d7f46 Michael Hanselmann

350 863d7f46 Michael Hanselmann
  """
351 863d7f46 Michael Hanselmann
352 863d7f46 Michael Hanselmann
353 863d7f46 Michael Hanselmann
class ArgCommand(_Argument):
354 863d7f46 Michael Hanselmann
  """Command argument.
355 863d7f46 Michael Hanselmann

356 863d7f46 Michael Hanselmann
  """
357 863d7f46 Michael Hanselmann
358 863d7f46 Michael Hanselmann
359 83ec7961 Michael Hanselmann
class ArgHost(_Argument):
360 83ec7961 Michael Hanselmann
  """Host argument.
361 83ec7961 Michael Hanselmann

362 83ec7961 Michael Hanselmann
  """
363 83ec7961 Michael Hanselmann
364 83ec7961 Michael Hanselmann
365 f9faf9c3 René Nussbaumer
class ArgOs(_Argument):
366 f9faf9c3 René Nussbaumer
  """OS argument.
367 f9faf9c3 René Nussbaumer

368 f9faf9c3 René Nussbaumer
  """
369 f9faf9c3 René Nussbaumer
370 f9faf9c3 René Nussbaumer
371 4a265c08 Michael Hanselmann
ARGS_NONE = []
372 4a265c08 Michael Hanselmann
ARGS_MANY_INSTANCES = [ArgInstance()]
373 4a265c08 Michael Hanselmann
ARGS_MANY_NODES = [ArgNode()]
374 667dbd6b Adeodato Simo
ARGS_MANY_GROUPS = [ArgGroup()]
375 4a265c08 Michael Hanselmann
ARGS_ONE_INSTANCE = [ArgInstance(min=1, max=1)]
376 4a265c08 Michael Hanselmann
ARGS_ONE_NODE = [ArgNode(min=1, max=1)]
377 dadf6b7d Michael Hanselmann
# TODO
378 dadf6b7d Michael Hanselmann
ARGS_ONE_GROUP = [ArgGroup(min=1, max=1)]
379 f9faf9c3 René Nussbaumer
ARGS_ONE_OS = [ArgOs(min=1, max=1)]
380 4a265c08 Michael Hanselmann
381 4a265c08 Michael Hanselmann
382 846baef9 Iustin Pop
def _ExtractTagsObject(opts, args):
383 846baef9 Iustin Pop
  """Extract the tag type object.
384 846baef9 Iustin Pop

385 846baef9 Iustin Pop
  Note that this function will modify its args parameter.
386 846baef9 Iustin Pop

387 846baef9 Iustin Pop
  """
388 846baef9 Iustin Pop
  if not hasattr(opts, "tag_type"):
389 846baef9 Iustin Pop
    raise errors.ProgrammerError("tag_type not passed to _ExtractTagsObject")
390 846baef9 Iustin Pop
  kind = opts.tag_type
391 846baef9 Iustin Pop
  if kind == constants.TAG_CLUSTER:
392 846baef9 Iustin Pop
    retval = kind, kind
393 819cbfe5 Michael Hanselmann
  elif kind in (constants.TAG_NODEGROUP,
394 819cbfe5 Michael Hanselmann
                constants.TAG_NODE,
395 819cbfe5 Michael Hanselmann
                constants.TAG_INSTANCE):
396 846baef9 Iustin Pop
    if not args:
397 0c434948 Iustin Pop
      raise errors.OpPrereqError("no arguments passed to the command")
398 846baef9 Iustin Pop
    name = args.pop(0)
399 846baef9 Iustin Pop
    retval = kind, name
400 846baef9 Iustin Pop
  else:
401 846baef9 Iustin Pop
    raise errors.ProgrammerError("Unhandled tag type '%s'" % kind)
402 846baef9 Iustin Pop
  return retval
403 846baef9 Iustin Pop
404 846baef9 Iustin Pop
405 810c50b7 Iustin Pop
def _ExtendTags(opts, args):
406 810c50b7 Iustin Pop
  """Extend the args if a source file has been given.
407 810c50b7 Iustin Pop

408 810c50b7 Iustin Pop
  This function will extend the tags with the contents of the file
409 810c50b7 Iustin Pop
  passed in the 'tags_source' attribute of the opts parameter. A file
410 810c50b7 Iustin Pop
  named '-' will be replaced by stdin.
411 810c50b7 Iustin Pop

412 810c50b7 Iustin Pop
  """
413 810c50b7 Iustin Pop
  fname = opts.tags_source
414 810c50b7 Iustin Pop
  if fname is None:
415 810c50b7 Iustin Pop
    return
416 810c50b7 Iustin Pop
  if fname == "-":
417 810c50b7 Iustin Pop
    new_fh = sys.stdin
418 810c50b7 Iustin Pop
  else:
419 810c50b7 Iustin Pop
    new_fh = open(fname, "r")
420 810c50b7 Iustin Pop
  new_data = []
421 810c50b7 Iustin Pop
  try:
422 810c50b7 Iustin Pop
    # we don't use the nice 'new_data = [line.strip() for line in fh]'
423 810c50b7 Iustin Pop
    # because of python bug 1633941
424 810c50b7 Iustin Pop
    while True:
425 810c50b7 Iustin Pop
      line = new_fh.readline()
426 810c50b7 Iustin Pop
      if not line:
427 810c50b7 Iustin Pop
        break
428 810c50b7 Iustin Pop
      new_data.append(line.strip())
429 810c50b7 Iustin Pop
  finally:
430 810c50b7 Iustin Pop
    new_fh.close()
431 810c50b7 Iustin Pop
  args.extend(new_data)
432 810c50b7 Iustin Pop
433 810c50b7 Iustin Pop
434 846baef9 Iustin Pop
def ListTags(opts, args):
435 846baef9 Iustin Pop
  """List the tags on a given object.
436 846baef9 Iustin Pop

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

442 846baef9 Iustin Pop
  """
443 846baef9 Iustin Pop
  kind, name = _ExtractTagsObject(opts, args)
444 7699c3af Iustin Pop
  cl = GetClient()
445 7699c3af Iustin Pop
  result = cl.QueryTags(kind, name)
446 846baef9 Iustin Pop
  result = list(result)
447 846baef9 Iustin Pop
  result.sort()
448 846baef9 Iustin Pop
  for tag in result:
449 03298ebe Michael Hanselmann
    ToStdout(tag)
450 846baef9 Iustin Pop
451 846baef9 Iustin Pop
452 846baef9 Iustin Pop
def AddTags(opts, args):
453 846baef9 Iustin Pop
  """Add tags on a given object.
454 846baef9 Iustin Pop

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

460 846baef9 Iustin Pop
  """
461 846baef9 Iustin Pop
  kind, name = _ExtractTagsObject(opts, args)
462 810c50b7 Iustin Pop
  _ExtendTags(opts, args)
463 846baef9 Iustin Pop
  if not args:
464 846baef9 Iustin Pop
    raise errors.OpPrereqError("No tags to be added")
465 d1602edc Iustin Pop
  op = opcodes.OpTagsSet(kind=kind, name=name, tags=args)
466 af1a81d1 Michael Hanselmann
  SubmitOpCode(op, opts=opts)
467 846baef9 Iustin Pop
468 846baef9 Iustin Pop
469 846baef9 Iustin Pop
def RemoveTags(opts, args):
470 846baef9 Iustin Pop
  """Remove tags from a given object.
471 846baef9 Iustin Pop

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

477 846baef9 Iustin Pop
  """
478 846baef9 Iustin Pop
  kind, name = _ExtractTagsObject(opts, args)
479 810c50b7 Iustin Pop
  _ExtendTags(opts, args)
480 846baef9 Iustin Pop
  if not args:
481 846baef9 Iustin Pop
    raise errors.OpPrereqError("No tags to be removed")
482 3f0ab95f Iustin Pop
  op = opcodes.OpTagsDel(kind=kind, name=name, tags=args)
483 af1a81d1 Michael Hanselmann
  SubmitOpCode(op, opts=opts)
484 846baef9 Iustin Pop
485 a8083063 Iustin Pop
486 b459a848 Andrea Spadaccini
def check_unit(option, opt, value): # pylint: disable=W0613
487 65fe4693 Iustin Pop
  """OptParsers custom converter for units.
488 65fe4693 Iustin Pop

489 65fe4693 Iustin Pop
  """
490 a8083063 Iustin Pop
  try:
491 a8083063 Iustin Pop
    return utils.ParseUnit(value)
492 a8083063 Iustin Pop
  except errors.UnitParseError, err:
493 3ecf6786 Iustin Pop
    raise OptionValueError("option %s: %s" % (opt, err))
494 a8083063 Iustin Pop
495 a8083063 Iustin Pop
496 a8469393 Iustin Pop
def _SplitKeyVal(opt, data):
497 a8469393 Iustin Pop
  """Convert a KeyVal string into a dict.
498 a8469393 Iustin Pop

499 a8469393 Iustin Pop
  This function will convert a key=val[,...] string into a dict. Empty
500 a8469393 Iustin Pop
  values will be converted specially: keys which have the prefix 'no_'
501 a8469393 Iustin Pop
  will have the value=False and the prefix stripped, the others will
502 a8469393 Iustin Pop
  have value=True.
503 a8469393 Iustin Pop

504 a8469393 Iustin Pop
  @type opt: string
505 a8469393 Iustin Pop
  @param opt: a string holding the option name for which we process the
506 a8469393 Iustin Pop
      data, used in building error messages
507 a8469393 Iustin Pop
  @type data: string
508 a8469393 Iustin Pop
  @param data: a string of the format key=val,key=val,...
509 a8469393 Iustin Pop
  @rtype: dict
510 a8469393 Iustin Pop
  @return: {key=val, key=val}
511 a8469393 Iustin Pop
  @raises errors.ParameterError: if there are duplicate keys
512 a8469393 Iustin Pop

513 a8469393 Iustin Pop
  """
514 a8469393 Iustin Pop
  kv_dict = {}
515 4f31882e Guido Trotter
  if data:
516 1b3a7656 Iustin Pop
    for elem in utils.UnescapeAndSplit(data, sep=","):
517 4f31882e Guido Trotter
      if "=" in elem:
518 4f31882e Guido Trotter
        key, val = elem.split("=", 1)
519 a8469393 Iustin Pop
      else:
520 4f31882e Guido Trotter
        if elem.startswith(NO_PREFIX):
521 4f31882e Guido Trotter
          key, val = elem[len(NO_PREFIX):], False
522 4f31882e Guido Trotter
        elif elem.startswith(UN_PREFIX):
523 4f31882e Guido Trotter
          key, val = elem[len(UN_PREFIX):], None
524 4f31882e Guido Trotter
        else:
525 4f31882e Guido Trotter
          key, val = elem, True
526 4f31882e Guido Trotter
      if key in kv_dict:
527 4f31882e Guido Trotter
        raise errors.ParameterError("Duplicate key '%s' in option %s" %
528 4f31882e Guido Trotter
                                    (key, opt))
529 4f31882e Guido Trotter
      kv_dict[key] = val
530 a8469393 Iustin Pop
  return kv_dict
531 a8469393 Iustin Pop
532 a8469393 Iustin Pop
533 b459a848 Andrea Spadaccini
def check_ident_key_val(option, opt, value):  # pylint: disable=W0613
534 552c8dff Michael Hanselmann
  """Custom parser for ident:key=val,key=val options.
535 552c8dff Michael Hanselmann

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

539 a8469393 Iustin Pop
  """
540 a8469393 Iustin Pop
  if ":" not in value:
541 d0c8c01d Iustin Pop
    ident, rest = value, ""
542 a8469393 Iustin Pop
  else:
543 a8469393 Iustin Pop
    ident, rest = value.split(":", 1)
544 8b46606c Guido Trotter
545 8b46606c Guido Trotter
  if ident.startswith(NO_PREFIX):
546 8b46606c Guido Trotter
    if rest:
547 8b46606c Guido Trotter
      msg = "Cannot pass options when removing parameter groups: %s" % value
548 8b46606c Guido Trotter
      raise errors.ParameterError(msg)
549 8b46606c Guido Trotter
    retval = (ident[len(NO_PREFIX):], False)
550 8b46606c Guido Trotter
  elif ident.startswith(UN_PREFIX):
551 8b46606c Guido Trotter
    if rest:
552 8b46606c Guido Trotter
      msg = "Cannot pass options when removing parameter groups: %s" % value
553 8b46606c Guido Trotter
      raise errors.ParameterError(msg)
554 8b46606c Guido Trotter
    retval = (ident[len(UN_PREFIX):], None)
555 8b46606c Guido Trotter
  else:
556 a8469393 Iustin Pop
    kv_dict = _SplitKeyVal(opt, rest)
557 a8469393 Iustin Pop
    retval = (ident, kv_dict)
558 a8469393 Iustin Pop
  return retval
559 a8469393 Iustin Pop
560 a8469393 Iustin Pop
561 b459a848 Andrea Spadaccini
def check_key_val(option, opt, value):  # pylint: disable=W0613
562 552c8dff Michael Hanselmann
  """Custom parser class for key=val,key=val options.
563 552c8dff Michael Hanselmann

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

566 a8469393 Iustin Pop
  """
567 a8469393 Iustin Pop
  return _SplitKeyVal(opt, value)
568 a8469393 Iustin Pop
569 a8469393 Iustin Pop
570 b459a848 Andrea Spadaccini
def check_bool(option, opt, value): # pylint: disable=W0613
571 e7b61bb0 Iustin Pop
  """Custom parser for yes/no options.
572 e7b61bb0 Iustin Pop

573 e7b61bb0 Iustin Pop
  This will store the parsed value as either True or False.
574 e7b61bb0 Iustin Pop

575 e7b61bb0 Iustin Pop
  """
576 e7b61bb0 Iustin Pop
  value = value.lower()
577 e7b61bb0 Iustin Pop
  if value == constants.VALUE_FALSE or value == "no":
578 e7b61bb0 Iustin Pop
    return False
579 e7b61bb0 Iustin Pop
  elif value == constants.VALUE_TRUE or value == "yes":
580 e7b61bb0 Iustin Pop
    return True
581 e7b61bb0 Iustin Pop
  else:
582 e7b61bb0 Iustin Pop
    raise errors.ParameterError("Invalid boolean value '%s'" % value)
583 e7b61bb0 Iustin Pop
584 e7b61bb0 Iustin Pop
585 63d44c55 Michael Hanselmann
# completion_suggestion is normally a list. Using numeric values not evaluating
586 63d44c55 Michael Hanselmann
# to False for dynamic completion.
587 63d44c55 Michael Hanselmann
(OPT_COMPL_MANY_NODES,
588 63d44c55 Michael Hanselmann
 OPT_COMPL_ONE_NODE,
589 63d44c55 Michael Hanselmann
 OPT_COMPL_ONE_INSTANCE,
590 63d44c55 Michael Hanselmann
 OPT_COMPL_ONE_OS,
591 2d3ed64b Michael Hanselmann
 OPT_COMPL_ONE_IALLOCATOR,
592 36e247e1 Guido Trotter
 OPT_COMPL_INST_ADD_NODES,
593 36e247e1 Guido Trotter
 OPT_COMPL_ONE_NODEGROUP) = range(100, 107)
594 63d44c55 Michael Hanselmann
595 63d44c55 Michael Hanselmann
OPT_COMPL_ALL = frozenset([
596 63d44c55 Michael Hanselmann
  OPT_COMPL_MANY_NODES,
597 63d44c55 Michael Hanselmann
  OPT_COMPL_ONE_NODE,
598 63d44c55 Michael Hanselmann
  OPT_COMPL_ONE_INSTANCE,
599 63d44c55 Michael Hanselmann
  OPT_COMPL_ONE_OS,
600 63d44c55 Michael Hanselmann
  OPT_COMPL_ONE_IALLOCATOR,
601 2d3ed64b Michael Hanselmann
  OPT_COMPL_INST_ADD_NODES,
602 36e247e1 Guido Trotter
  OPT_COMPL_ONE_NODEGROUP,
603 63d44c55 Michael Hanselmann
  ])
604 63d44c55 Michael Hanselmann
605 63d44c55 Michael Hanselmann
606 552c8dff Michael Hanselmann
class CliOption(Option):
607 552c8dff Michael Hanselmann
  """Custom option class for optparse.
608 a8469393 Iustin Pop

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

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

1377 c41eea6e Iustin Pop
  @param argv: the command line
1378 c41eea6e Iustin Pop
  @param commands: dictionary with special contents, see the design
1379 c41eea6e Iustin Pop
      doc for cmdline handling
1380 c41eea6e Iustin Pop
  @param aliases: dictionary with command aliases {'alias': 'target, ...}
1381 ef9fa5b9 René Nussbaumer
  @param env_override: list of env variables allowed for default args
1382 098c0958 Michael Hanselmann

1383 a8083063 Iustin Pop
  """
1384 ef9fa5b9 René Nussbaumer
  assert not (env_override - set(commands))
1385 ef9fa5b9 René Nussbaumer
1386 a8083063 Iustin Pop
  if len(argv) == 0:
1387 a8083063 Iustin Pop
    binary = "<command>"
1388 a8083063 Iustin Pop
  else:
1389 a8083063 Iustin Pop
    binary = argv[0].split("/")[-1]
1390 a8083063 Iustin Pop
1391 a8083063 Iustin Pop
  if len(argv) > 1 and argv[1] == "--version":
1392 84a12e40 Iustin Pop
    ToStdout("%s (ganeti %s) %s", binary, constants.VCS_VERSION,
1393 84a12e40 Iustin Pop
             constants.RELEASE_VERSION)
1394 a8083063 Iustin Pop
    # Quit right away. That way we don't have to care about this special
1395 a8083063 Iustin Pop
    # argument. optparse.py does it the same.
1396 a8083063 Iustin Pop
    sys.exit(0)
1397 a8083063 Iustin Pop
1398 de47cf8f Guido Trotter
  if len(argv) < 2 or not (argv[1] in commands or
1399 70a35b6f Guido Trotter
                           argv[1] in aliases):
1400 a8083063 Iustin Pop
    # let's do a nice thing
1401 a8083063 Iustin Pop
    sortedcmds = commands.keys()
1402 a8083063 Iustin Pop
    sortedcmds.sort()
1403 03298ebe Michael Hanselmann
1404 03298ebe Michael Hanselmann
    ToStdout("Usage: %s {command} [options...] [argument...]", binary)
1405 03298ebe Michael Hanselmann
    ToStdout("%s <command> --help to see details, or man %s", binary, binary)
1406 03298ebe Michael Hanselmann
    ToStdout("")
1407 03298ebe Michael Hanselmann
1408 a8083063 Iustin Pop
    # compute the max line length for cmd + usage
1409 4e713df6 Iustin Pop
    mlen = max([len(" %s" % cmd) for cmd in commands])
1410 a8083063 Iustin Pop
    mlen = min(60, mlen) # should not get here...
1411 03298ebe Michael Hanselmann
1412 a8083063 Iustin Pop
    # and format a nice command list
1413 03298ebe Michael Hanselmann
    ToStdout("Commands:")
1414 a8083063 Iustin Pop
    for cmd in sortedcmds:
1415 4e713df6 Iustin Pop
      cmdstr = " %s" % (cmd,)
1416 9a033156 Iustin Pop
      help_text = commands[cmd][4]
1417 03298ebe Michael Hanselmann
      help_lines = textwrap.wrap(help_text, 79 - 3 - mlen)
1418 03298ebe Michael Hanselmann
      ToStdout("%-*s - %s", mlen, cmdstr, help_lines.pop(0))
1419 a8083063 Iustin Pop
      for line in help_lines:
1420 03298ebe Michael Hanselmann
        ToStdout("%-*s   %s", mlen, "", line)
1421 03298ebe Michael Hanselmann
1422 03298ebe Michael Hanselmann
    ToStdout("")
1423 03298ebe Michael Hanselmann
1424 a8083063 Iustin Pop
    return None, None, None
1425 de47cf8f Guido Trotter
1426 de47cf8f Guido Trotter
  # get command, unalias it, and look it up in commands
1427 a8083063 Iustin Pop
  cmd = argv.pop(1)
1428 de47cf8f Guido Trotter
  if cmd in aliases:
1429 de47cf8f Guido Trotter
    if cmd in commands:
1430 de47cf8f Guido Trotter
      raise errors.ProgrammerError("Alias '%s' overrides an existing"
1431 de47cf8f Guido Trotter
                                   " command" % cmd)
1432 de47cf8f Guido Trotter
1433 de47cf8f Guido Trotter
    if aliases[cmd] not in commands:
1434 de47cf8f Guido Trotter
      raise errors.ProgrammerError("Alias '%s' maps to non-existing"
1435 de47cf8f Guido Trotter
                                   " command '%s'" % (cmd, aliases[cmd]))
1436 de47cf8f Guido Trotter
1437 de47cf8f Guido Trotter
    cmd = aliases[cmd]
1438 de47cf8f Guido Trotter
1439 ef9fa5b9 René Nussbaumer
  if cmd in env_override:
1440 ef9fa5b9 René Nussbaumer
    args_env_name = ("%s_%s" % (binary.replace("-", "_"), cmd)).upper()
1441 ef9fa5b9 René Nussbaumer
    env_args = os.environ.get(args_env_name)
1442 ef9fa5b9 René Nussbaumer
    if env_args:
1443 ef9fa5b9 René Nussbaumer
      argv = utils.InsertAtPos(argv, 1, shlex.split(env_args))
1444 ef9fa5b9 René Nussbaumer
1445 a8005e17 Michael Hanselmann
  func, args_def, parser_opts, usage, description = commands[cmd]
1446 5786c087 Michael Hanselmann
  parser = OptionParser(option_list=parser_opts + COMMON_OPTS,
1447 a8083063 Iustin Pop
                        description=description,
1448 a8083063 Iustin Pop
                        formatter=TitledHelpFormatter(),
1449 a8083063 Iustin Pop
                        usage="%%prog %s %s" % (cmd, usage))
1450 a8083063 Iustin Pop
  parser.disable_interspersed_args()
1451 ef9fa5b9 René Nussbaumer
  options, args = parser.parse_args(args=argv[1:])
1452 a8005e17 Michael Hanselmann
1453 a8005e17 Michael Hanselmann
  if not _CheckArguments(cmd, args_def, args):
1454 a8083063 Iustin Pop
    return None, None, None
1455 a8083063 Iustin Pop
1456 a8083063 Iustin Pop
  return func, options, args
1457 a8083063 Iustin Pop
1458 a8083063 Iustin Pop
1459 a8005e17 Michael Hanselmann
def _CheckArguments(cmd, args_def, args):
1460 a8005e17 Michael Hanselmann
  """Verifies the arguments using the argument definition.
1461 a8005e17 Michael Hanselmann

1462 a8005e17 Michael Hanselmann
  Algorithm:
1463 a8005e17 Michael Hanselmann

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

1466 a8005e17 Michael Hanselmann
    1. For each argument in definition
1467 a8005e17 Michael Hanselmann

1468 a8005e17 Michael Hanselmann
      1. Keep running count of minimum number of values (min_count)
1469 a8005e17 Michael Hanselmann
      1. Keep running count of maximum number of values (max_count)
1470 a8005e17 Michael Hanselmann
      1. If it has an unlimited number of values
1471 a8005e17 Michael Hanselmann

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

1474 a8005e17 Michael Hanselmann
    1. If last argument has limited number of values
1475 a8005e17 Michael Hanselmann

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

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

1480 a8005e17 Michael Hanselmann
  """
1481 a8005e17 Michael Hanselmann
  if args and not args_def:
1482 a8005e17 Michael Hanselmann
    ToStderr("Error: Command %s expects no arguments", cmd)
1483 a8005e17 Michael Hanselmann
    return False
1484 a8005e17 Michael Hanselmann
1485 a8005e17 Michael Hanselmann
  min_count = None
1486 a8005e17 Michael Hanselmann
  max_count = None
1487 a8005e17 Michael Hanselmann
  check_max = None
1488 a8005e17 Michael Hanselmann
1489 a8005e17 Michael Hanselmann
  last_idx = len(args_def) - 1
1490 a8005e17 Michael Hanselmann
1491 a8005e17 Michael Hanselmann
  for idx, arg in enumerate(args_def):
1492 a8005e17 Michael Hanselmann
    if min_count is None:
1493 a8005e17 Michael Hanselmann
      min_count = arg.min
1494 a8005e17 Michael Hanselmann
    elif arg.min is not None:
1495 a8005e17 Michael Hanselmann
      min_count += arg.min
1496 a8005e17 Michael Hanselmann
1497 a8005e17 Michael Hanselmann
    if max_count is None:
1498 a8005e17 Michael Hanselmann
      max_count = arg.max
1499 a8005e17 Michael Hanselmann
    elif arg.max is not None:
1500 a8005e17 Michael Hanselmann
      max_count += arg.max
1501 a8005e17 Michael Hanselmann
1502 a8005e17 Michael Hanselmann
    if idx == last_idx:
1503 a8005e17 Michael Hanselmann
      check_max = (arg.max is not None)
1504 a8005e17 Michael Hanselmann
1505 a8005e17 Michael Hanselmann
    elif arg.max is None:
1506 a8005e17 Michael Hanselmann
      raise errors.ProgrammerError("Only the last argument can have max=None")
1507 a8005e17 Michael Hanselmann
1508 a8005e17 Michael Hanselmann
  if check_max:
1509 a8005e17 Michael Hanselmann
    # Command with exact number of arguments
1510 a8005e17 Michael Hanselmann
    if (min_count is not None and max_count is not None and
1511 a8005e17 Michael Hanselmann
        min_count == max_count and len(args) != min_count):
1512 a8005e17 Michael Hanselmann
      ToStderr("Error: Command %s expects %d argument(s)", cmd, min_count)
1513 a8005e17 Michael Hanselmann
      return False
1514 a8005e17 Michael Hanselmann
1515 a8005e17 Michael Hanselmann
    # Command with limited number of arguments
1516 a8005e17 Michael Hanselmann
    if max_count is not None and len(args) > max_count:
1517 a8005e17 Michael Hanselmann
      ToStderr("Error: Command %s expects only %d argument(s)",
1518 a8005e17 Michael Hanselmann
               cmd, max_count)
1519 a8005e17 Michael Hanselmann
      return False
1520 a8005e17 Michael Hanselmann
1521 a8005e17 Michael Hanselmann
  # Command with some required arguments
1522 a8005e17 Michael Hanselmann
  if min_count is not None and len(args) < min_count:
1523 a8005e17 Michael Hanselmann
    ToStderr("Error: Command %s expects at least %d argument(s)",
1524 a8005e17 Michael Hanselmann
             cmd, min_count)
1525 a8005e17 Michael Hanselmann
    return False
1526 a8005e17 Michael Hanselmann
1527 a8005e17 Michael Hanselmann
  return True
1528 a8005e17 Michael Hanselmann
1529 a8005e17 Michael Hanselmann
1530 60d49723 Michael Hanselmann
def SplitNodeOption(value):
1531 60d49723 Michael Hanselmann
  """Splits the value of a --node option.
1532 60d49723 Michael Hanselmann

1533 60d49723 Michael Hanselmann
  """
1534 d0c8c01d Iustin Pop
  if value and ":" in value:
1535 d0c8c01d Iustin Pop
    return value.split(":", 1)
1536 60d49723 Michael Hanselmann
  else:
1537 60d49723 Michael Hanselmann
    return (value, None)
1538 60d49723 Michael Hanselmann
1539 60d49723 Michael Hanselmann
1540 07150497 Guido Trotter
def CalculateOSNames(os_name, os_variants):
1541 07150497 Guido Trotter
  """Calculates all the names an OS can be called, according to its variants.
1542 07150497 Guido Trotter

1543 07150497 Guido Trotter
  @type os_name: string
1544 07150497 Guido Trotter
  @param os_name: base name of the os
1545 07150497 Guido Trotter
  @type os_variants: list or None
1546 07150497 Guido Trotter
  @param os_variants: list of supported variants
1547 07150497 Guido Trotter
  @rtype: list
1548 07150497 Guido Trotter
  @return: list of valid names
1549 07150497 Guido Trotter

1550 07150497 Guido Trotter
  """
1551 07150497 Guido Trotter
  if os_variants:
1552 d0c8c01d Iustin Pop
    return ["%s+%s" % (os_name, v) for v in os_variants]
1553 07150497 Guido Trotter
  else:
1554 07150497 Guido Trotter
    return [os_name]
1555 07150497 Guido Trotter
1556 07150497 Guido Trotter
1557 a4ebd726 Michael Hanselmann
def ParseFields(selected, default):
1558 a4ebd726 Michael Hanselmann
  """Parses the values of "--field"-like options.
1559 a4ebd726 Michael Hanselmann

1560 a4ebd726 Michael Hanselmann
  @type selected: string or None
1561 a4ebd726 Michael Hanselmann
  @param selected: User-selected options
1562 a4ebd726 Michael Hanselmann
  @type default: list
1563 a4ebd726 Michael Hanselmann
  @param default: Default fields
1564 a4ebd726 Michael Hanselmann

1565 a4ebd726 Michael Hanselmann
  """
1566 a4ebd726 Michael Hanselmann
  if selected is None:
1567 a4ebd726 Michael Hanselmann
    return default
1568 a4ebd726 Michael Hanselmann
1569 a4ebd726 Michael Hanselmann
  if selected.startswith("+"):
1570 a4ebd726 Michael Hanselmann
    return default + selected[1:].split(",")
1571 a4ebd726 Michael Hanselmann
1572 a4ebd726 Michael Hanselmann
  return selected.split(",")
1573 a4ebd726 Michael Hanselmann
1574 a4ebd726 Michael Hanselmann
1575 e0e916fe Iustin Pop
UsesRPC = rpc.RunWithRPC
1576 4331f6cd Michael Hanselmann
1577 4331f6cd Michael Hanselmann
1578 47988778 Iustin Pop
def AskUser(text, choices=None):
1579 47988778 Iustin Pop
  """Ask the user a question.
1580 a8083063 Iustin Pop

1581 c41eea6e Iustin Pop
  @param text: the question to ask
1582 a8083063 Iustin Pop

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

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

1592 a8083063 Iustin Pop
  """
1593 47988778 Iustin Pop
  if choices is None:
1594 d0c8c01d Iustin Pop
    choices = [("y", True, "Perform the operation"),
1595 d0c8c01d Iustin Pop
               ("n", False, "Do not perform the operation")]
1596 47988778 Iustin Pop
  if not choices or not isinstance(choices, list):
1597 5bbd3f7f Michael Hanselmann
    raise errors.ProgrammerError("Invalid choices argument to AskUser")
1598 47988778 Iustin Pop
  for entry in choices:
1599 d0c8c01d Iustin Pop
    if not isinstance(entry, tuple) or len(entry) < 3 or entry[0] == "?":
1600 5bbd3f7f Michael Hanselmann
      raise errors.ProgrammerError("Invalid choices element to AskUser")
1601 47988778 Iustin Pop
1602 47988778 Iustin Pop
  answer = choices[-1][1]
1603 47988778 Iustin Pop
  new_text = []
1604 47988778 Iustin Pop
  for line in text.splitlines():
1605 47988778 Iustin Pop
    new_text.append(textwrap.fill(line, 70, replace_whitespace=False))
1606 47988778 Iustin Pop
  text = "\n".join(new_text)
1607 a8083063 Iustin Pop
  try:
1608 3023170f Iustin Pop
    f = file("/dev/tty", "a+")
1609 a8083063 Iustin Pop
  except IOError:
1610 47988778 Iustin Pop
    return answer
1611 a8083063 Iustin Pop
  try:
1612 47988778 Iustin Pop
    chars = [entry[0] for entry in choices]
1613 47988778 Iustin Pop
    chars[-1] = "[%s]" % chars[-1]
1614 d0c8c01d Iustin Pop
    chars.append("?")
1615 47988778 Iustin Pop
    maps = dict([(entry[0], entry[1]) for entry in choices])
1616 47988778 Iustin Pop
    while True:
1617 47988778 Iustin Pop
      f.write(text)
1618 d0c8c01d Iustin Pop
      f.write("\n")
1619 47988778 Iustin Pop
      f.write("/".join(chars))
1620 47988778 Iustin Pop
      f.write(": ")
1621 47988778 Iustin Pop
      line = f.readline(2).strip().lower()
1622 47988778 Iustin Pop
      if line in maps:
1623 47988778 Iustin Pop
        answer = maps[line]
1624 47988778 Iustin Pop
        break
1625 d0c8c01d Iustin Pop
      elif line == "?":
1626 47988778 Iustin Pop
        for entry in choices:
1627 47988778 Iustin Pop
          f.write(" %s - %s\n" % (entry[0], entry[2]))
1628 47988778 Iustin Pop
        f.write("\n")
1629 47988778 Iustin Pop
        continue
1630 a8083063 Iustin Pop
  finally:
1631 a8083063 Iustin Pop
    f.close()
1632 a8083063 Iustin Pop
  return answer
1633 a8083063 Iustin Pop
1634 a8083063 Iustin Pop
1635 e9d741b6 Iustin Pop
class JobSubmittedException(Exception):
1636 e9d741b6 Iustin Pop
  """Job was submitted, client should exit.
1637 e9d741b6 Iustin Pop

1638 e9d741b6 Iustin Pop
  This exception has one argument, the ID of the job that was
1639 e9d741b6 Iustin Pop
  submitted. The handler should print this ID.
1640 e9d741b6 Iustin Pop

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

1643 e9d741b6 Iustin Pop
  """
1644 e9d741b6 Iustin Pop
1645 e9d741b6 Iustin Pop
1646 0a1e74d9 Iustin Pop
def SendJob(ops, cl=None):
1647 0a1e74d9 Iustin Pop
  """Function to submit an opcode without waiting for the results.
1648 a8083063 Iustin Pop

1649 0a1e74d9 Iustin Pop
  @type ops: list
1650 0a1e74d9 Iustin Pop
  @param ops: list of opcodes
1651 0a1e74d9 Iustin Pop
  @type cl: luxi.Client
1652 0a1e74d9 Iustin Pop
  @param cl: the luxi client to use for communicating with the master;
1653 0a1e74d9 Iustin Pop
             if None, a new client will be created
1654 a8083063 Iustin Pop

1655 a8083063 Iustin Pop
  """
1656 e2212007 Iustin Pop
  if cl is None:
1657 b33e986b Iustin Pop
    cl = GetClient()
1658 685ee993 Iustin Pop
1659 0a1e74d9 Iustin Pop
  job_id = cl.SubmitJob(ops)
1660 0a1e74d9 Iustin Pop
1661 0a1e74d9 Iustin Pop
  return job_id
1662 0a1e74d9 Iustin Pop
1663 0a1e74d9 Iustin Pop
1664 4e338533 Michael Hanselmann
def GenericPollJob(job_id, cbs, report_cbs):
1665 4e338533 Michael Hanselmann
  """Generic job-polling function.
1666 0a1e74d9 Iustin Pop

1667 4e338533 Michael Hanselmann
  @type job_id: number
1668 4e338533 Michael Hanselmann
  @param job_id: Job ID
1669 4e338533 Michael Hanselmann
  @type cbs: Instance of L{JobPollCbBase}
1670 4e338533 Michael Hanselmann
  @param cbs: Data callbacks
1671 4e338533 Michael Hanselmann
  @type report_cbs: Instance of L{JobPollReportCbBase}
1672 4e338533 Michael Hanselmann
  @param report_cbs: Reporting callbacks
1673 0a1e74d9 Iustin Pop

1674 0a1e74d9 Iustin Pop
  """
1675 6c5a7090 Michael Hanselmann
  prev_job_info = None
1676 6c5a7090 Michael Hanselmann
  prev_logmsg_serial = None
1677 6c5a7090 Michael Hanselmann
1678 f4484122 Michael Hanselmann
  status = None
1679 f4484122 Michael Hanselmann
1680 685ee993 Iustin Pop
  while True:
1681 4e338533 Michael Hanselmann
    result = cbs.WaitForJobChangeOnce(job_id, ["status"], prev_job_info,
1682 4e338533 Michael Hanselmann
                                      prev_logmsg_serial)
1683 6c5a7090 Michael Hanselmann
    if not result:
1684 685ee993 Iustin Pop
      # job not found, go away!
1685 0bbe448c Michael Hanselmann
      raise errors.JobLost("Job with id %s lost" % job_id)
1686 4e338533 Michael Hanselmann
1687 4e338533 Michael Hanselmann
    if result == constants.JOB_NOTCHANGED:
1688 4e338533 Michael Hanselmann
      report_cbs.ReportNotChanged(job_id, status)
1689 f4484122 Michael Hanselmann
1690 f4484122 Michael Hanselmann
      # Wait again
1691 f4484122 Michael Hanselmann
      continue
1692 685ee993 Iustin Pop
1693 6c5a7090 Michael Hanselmann
    # Split result, a tuple of (field values, log entries)
1694 6c5a7090 Michael Hanselmann
    (job_info, log_entries) = result
1695 6c5a7090 Michael Hanselmann
    (status, ) = job_info
1696 6c5a7090 Michael Hanselmann
1697 6c5a7090 Michael Hanselmann
    if log_entries:
1698 6c5a7090 Michael Hanselmann
      for log_entry in log_entries:
1699 4e338533 Michael Hanselmann
        (serial, timestamp, log_type, message) = log_entry
1700 4e338533 Michael Hanselmann
        report_cbs.ReportLogMessage(job_id, serial, timestamp,
1701 4e338533 Michael Hanselmann
                                    log_type, message)
1702 6c5a7090 Michael Hanselmann
        prev_logmsg_serial = max(prev_logmsg_serial, serial)
1703 6c5a7090 Michael Hanselmann
1704 0bbe448c Michael Hanselmann
    # TODO: Handle canceled and archived jobs
1705 fbf0262f Michael Hanselmann
    elif status in (constants.JOB_STATUS_SUCCESS,
1706 fbf0262f Michael Hanselmann
                    constants.JOB_STATUS_ERROR,
1707 fbf0262f Michael Hanselmann
                    constants.JOB_STATUS_CANCELING,
1708 fbf0262f Michael Hanselmann
                    constants.JOB_STATUS_CANCELED):
1709 685ee993 Iustin Pop
      break
1710 6c5a7090 Michael Hanselmann
1711 6c5a7090 Michael Hanselmann
    prev_job_info = job_info
1712 685ee993 Iustin Pop
1713 4e338533 Michael Hanselmann
  jobs = cbs.QueryJobs([job_id], ["status", "opstatus", "opresult"])
1714 0bbe448c Michael Hanselmann
  if not jobs:
1715 0bbe448c Michael Hanselmann
    raise errors.JobLost("Job with id %s lost" % job_id)
1716 685ee993 Iustin Pop
1717 0e050889 Iustin Pop
  status, opstatus, result = jobs[0]
1718 4e338533 Michael Hanselmann
1719 0bbe448c Michael Hanselmann
  if status == constants.JOB_STATUS_SUCCESS:
1720 53c04d04 Iustin Pop
    return result
1721 4e338533 Michael Hanselmann
1722 4e338533 Michael Hanselmann
  if status in (constants.JOB_STATUS_CANCELING, constants.JOB_STATUS_CANCELED):
1723 fbf0262f Michael Hanselmann
    raise errors.OpExecError("Job was canceled")
1724 4e338533 Michael Hanselmann
1725 4e338533 Michael Hanselmann
  has_ok = False
1726 4e338533 Michael Hanselmann
  for idx, (status, msg) in enumerate(zip(opstatus, result)):
1727 4e338533 Michael Hanselmann
    if status == constants.OP_STATUS_SUCCESS:
1728 4e338533 Michael Hanselmann
      has_ok = True
1729 4e338533 Michael Hanselmann
    elif status == constants.OP_STATUS_ERROR:
1730 4e338533 Michael Hanselmann
      errors.MaybeRaise(msg)
1731 4e338533 Michael Hanselmann
1732 4e338533 Michael Hanselmann
      if has_ok:
1733 4e338533 Michael Hanselmann
        raise errors.OpExecError("partial failure (opcode %d): %s" %
1734 4e338533 Michael Hanselmann
                                 (idx, msg))
1735 4e338533 Michael Hanselmann
1736 4e338533 Michael Hanselmann
      raise errors.OpExecError(str(msg))
1737 4e338533 Michael Hanselmann
1738 4e338533 Michael Hanselmann
  # default failure mode
1739 4e338533 Michael Hanselmann
  raise errors.OpExecError(result)
1740 4e338533 Michael Hanselmann
1741 4e338533 Michael Hanselmann
1742 4e338533 Michael Hanselmann
class JobPollCbBase:
1743 4e338533 Michael Hanselmann
  """Base class for L{GenericPollJob} callbacks.
1744 4e338533 Michael Hanselmann

1745 4e338533 Michael Hanselmann
  """
1746 4e338533 Michael Hanselmann
  def __init__(self):
1747 4e338533 Michael Hanselmann
    """Initializes this class.
1748 4e338533 Michael Hanselmann

1749 4e338533 Michael Hanselmann
    """
1750 4e338533 Michael Hanselmann
1751 4e338533 Michael Hanselmann
  def WaitForJobChangeOnce(self, job_id, fields,
1752 4e338533 Michael Hanselmann
                           prev_job_info, prev_log_serial):
1753 4e338533 Michael Hanselmann
    """Waits for changes on a job.
1754 4e338533 Michael Hanselmann

1755 4e338533 Michael Hanselmann
    """
1756 4e338533 Michael Hanselmann
    raise NotImplementedError()
1757 4e338533 Michael Hanselmann
1758 4e338533 Michael Hanselmann
  def QueryJobs(self, job_ids, fields):
1759 4e338533 Michael Hanselmann
    """Returns the selected fields for the selected job IDs.
1760 4e338533 Michael Hanselmann

1761 4e338533 Michael Hanselmann
    @type job_ids: list of numbers
1762 4e338533 Michael Hanselmann
    @param job_ids: Job IDs
1763 4e338533 Michael Hanselmann
    @type fields: list of strings
1764 4e338533 Michael Hanselmann
    @param fields: Fields
1765 4e338533 Michael Hanselmann

1766 4e338533 Michael Hanselmann
    """
1767 4e338533 Michael Hanselmann
    raise NotImplementedError()
1768 4e338533 Michael Hanselmann
1769 4e338533 Michael Hanselmann
1770 4e338533 Michael Hanselmann
class JobPollReportCbBase:
1771 4e338533 Michael Hanselmann
  """Base class for L{GenericPollJob} reporting callbacks.
1772 4e338533 Michael Hanselmann

1773 4e338533 Michael Hanselmann
  """
1774 4e338533 Michael Hanselmann
  def __init__(self):
1775 4e338533 Michael Hanselmann
    """Initializes this class.
1776 4e338533 Michael Hanselmann

1777 4e338533 Michael Hanselmann
    """
1778 4e338533 Michael Hanselmann
1779 4e338533 Michael Hanselmann
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
1780 4e338533 Michael Hanselmann
    """Handles a log message.
1781 4e338533 Michael Hanselmann

1782 4e338533 Michael Hanselmann
    """
1783 4e338533 Michael Hanselmann
    raise NotImplementedError()
1784 4e338533 Michael Hanselmann
1785 4e338533 Michael Hanselmann
  def ReportNotChanged(self, job_id, status):
1786 4e338533 Michael Hanselmann
    """Called for if a job hasn't changed in a while.
1787 4e338533 Michael Hanselmann

1788 4e338533 Michael Hanselmann
    @type job_id: number
1789 4e338533 Michael Hanselmann
    @param job_id: Job ID
1790 4e338533 Michael Hanselmann
    @type status: string or None
1791 4e338533 Michael Hanselmann
    @param status: Job status if available
1792 4e338533 Michael Hanselmann

1793 4e338533 Michael Hanselmann
    """
1794 4e338533 Michael Hanselmann
    raise NotImplementedError()
1795 4e338533 Michael Hanselmann
1796 4e338533 Michael Hanselmann
1797 4e338533 Michael Hanselmann
class _LuxiJobPollCb(JobPollCbBase):
1798 4e338533 Michael Hanselmann
  def __init__(self, cl):
1799 4e338533 Michael Hanselmann
    """Initializes this class.
1800 4e338533 Michael Hanselmann

1801 4e338533 Michael Hanselmann
    """
1802 4e338533 Michael Hanselmann
    JobPollCbBase.__init__(self)
1803 4e338533 Michael Hanselmann
    self.cl = cl
1804 4e338533 Michael Hanselmann
1805 4e338533 Michael Hanselmann
  def WaitForJobChangeOnce(self, job_id, fields,
1806 4e338533 Michael Hanselmann
                           prev_job_info, prev_log_serial):
1807 4e338533 Michael Hanselmann
    """Waits for changes on a job.
1808 4e338533 Michael Hanselmann

1809 4e338533 Michael Hanselmann
    """
1810 4e338533 Michael Hanselmann
    return self.cl.WaitForJobChangeOnce(job_id, fields,
1811 4e338533 Michael Hanselmann
                                        prev_job_info, prev_log_serial)
1812 4e338533 Michael Hanselmann
1813 4e338533 Michael Hanselmann
  def QueryJobs(self, job_ids, fields):
1814 4e338533 Michael Hanselmann
    """Returns the selected fields for the selected job IDs.
1815 4e338533 Michael Hanselmann

1816 4e338533 Michael Hanselmann
    """
1817 4e338533 Michael Hanselmann
    return self.cl.QueryJobs(job_ids, fields)
1818 4e338533 Michael Hanselmann
1819 4e338533 Michael Hanselmann
1820 4e338533 Michael Hanselmann
class FeedbackFnJobPollReportCb(JobPollReportCbBase):
1821 4e338533 Michael Hanselmann
  def __init__(self, feedback_fn):
1822 4e338533 Michael Hanselmann
    """Initializes this class.
1823 4e338533 Michael Hanselmann

1824 4e338533 Michael Hanselmann
    """
1825 4e338533 Michael Hanselmann
    JobPollReportCbBase.__init__(self)
1826 4e338533 Michael Hanselmann
1827 4e338533 Michael Hanselmann
    self.feedback_fn = feedback_fn
1828 4e338533 Michael Hanselmann
1829 4e338533 Michael Hanselmann
    assert callable(feedback_fn)
1830 4e338533 Michael Hanselmann
1831 4e338533 Michael Hanselmann
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
1832 4e338533 Michael Hanselmann
    """Handles a log message.
1833 4e338533 Michael Hanselmann

1834 4e338533 Michael Hanselmann
    """
1835 4e338533 Michael Hanselmann
    self.feedback_fn((timestamp, log_type, log_msg))
1836 4e338533 Michael Hanselmann
1837 4e338533 Michael Hanselmann
  def ReportNotChanged(self, job_id, status):
1838 4e338533 Michael Hanselmann
    """Called if a job hasn't changed in a while.
1839 4e338533 Michael Hanselmann

1840 4e338533 Michael Hanselmann
    """
1841 4e338533 Michael Hanselmann
    # Ignore
1842 4e338533 Michael Hanselmann
1843 4e338533 Michael Hanselmann
1844 4e338533 Michael Hanselmann
class StdioJobPollReportCb(JobPollReportCbBase):
1845 4e338533 Michael Hanselmann
  def __init__(self):
1846 4e338533 Michael Hanselmann
    """Initializes this class.
1847 4e338533 Michael Hanselmann

1848 4e338533 Michael Hanselmann
    """
1849 4e338533 Michael Hanselmann
    JobPollReportCbBase.__init__(self)
1850 4e338533 Michael Hanselmann
1851 4e338533 Michael Hanselmann
    self.notified_queued = False
1852 4e338533 Michael Hanselmann
    self.notified_waitlock = False
1853 4e338533 Michael Hanselmann
1854 4e338533 Michael Hanselmann
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
1855 4e338533 Michael Hanselmann
    """Handles a log message.
1856 4e338533 Michael Hanselmann

1857 4e338533 Michael Hanselmann
    """
1858 4e338533 Michael Hanselmann
    ToStdout("%s %s", time.ctime(utils.MergeTime(timestamp)),
1859 8a7f1c61 Michael Hanselmann
             FormatLogMessage(log_type, log_msg))
1860 4e338533 Michael Hanselmann
1861 4e338533 Michael Hanselmann
  def ReportNotChanged(self, job_id, status):
1862 4e338533 Michael Hanselmann
    """Called if a job hasn't changed in a while.
1863 4e338533 Michael Hanselmann

1864 4e338533 Michael Hanselmann
    """
1865 4e338533 Michael Hanselmann
    if status is None:
1866 4e338533 Michael Hanselmann
      return
1867 4e338533 Michael Hanselmann
1868 4e338533 Michael Hanselmann
    if status == constants.JOB_STATUS_QUEUED and not self.notified_queued:
1869 4e338533 Michael Hanselmann
      ToStderr("Job %s is waiting in queue", job_id)
1870 4e338533 Michael Hanselmann
      self.notified_queued = True
1871 4e338533 Michael Hanselmann
1872 47099cd1 Michael Hanselmann
    elif status == constants.JOB_STATUS_WAITING and not self.notified_waitlock:
1873 4e338533 Michael Hanselmann
      ToStderr("Job %s is trying to acquire all necessary locks", job_id)
1874 4e338533 Michael Hanselmann
      self.notified_waitlock = True
1875 4e338533 Michael Hanselmann
1876 4e338533 Michael Hanselmann
1877 8a7f1c61 Michael Hanselmann
def FormatLogMessage(log_type, log_msg):
1878 8a7f1c61 Michael Hanselmann
  """Formats a job message according to its type.
1879 8a7f1c61 Michael Hanselmann

1880 8a7f1c61 Michael Hanselmann
  """
1881 8a7f1c61 Michael Hanselmann
  if log_type != constants.ELOG_MESSAGE:
1882 8a7f1c61 Michael Hanselmann
    log_msg = str(log_msg)
1883 8a7f1c61 Michael Hanselmann
1884 8a7f1c61 Michael Hanselmann
  return utils.SafeEncode(log_msg)
1885 8a7f1c61 Michael Hanselmann
1886 8a7f1c61 Michael Hanselmann
1887 583163a6 Michael Hanselmann
def PollJob(job_id, cl=None, feedback_fn=None, reporter=None):
1888 4e338533 Michael Hanselmann
  """Function to poll for the result of a job.
1889 4e338533 Michael Hanselmann

1890 4e338533 Michael Hanselmann
  @type job_id: job identified
1891 4e338533 Michael Hanselmann
  @param job_id: the job to poll for results
1892 4e338533 Michael Hanselmann
  @type cl: luxi.Client
1893 4e338533 Michael Hanselmann
  @param cl: the luxi client to use for communicating with the master;
1894 4e338533 Michael Hanselmann
             if None, a new client will be created
1895 4e338533 Michael Hanselmann

1896 4e338533 Michael Hanselmann
  """
1897 4e338533 Michael Hanselmann
  if cl is None:
1898 4e338533 Michael Hanselmann
    cl = GetClient()
1899 4e338533 Michael Hanselmann
1900 583163a6 Michael Hanselmann
  if reporter is None:
1901 583163a6 Michael Hanselmann
    if feedback_fn:
1902 583163a6 Michael Hanselmann
      reporter = FeedbackFnJobPollReportCb(feedback_fn)
1903 583163a6 Michael Hanselmann
    else:
1904 583163a6 Michael Hanselmann
      reporter = StdioJobPollReportCb()
1905 583163a6 Michael Hanselmann
  elif feedback_fn:
1906 583163a6 Michael Hanselmann
    raise errors.ProgrammerError("Can't specify reporter and feedback function")
1907 4e338533 Michael Hanselmann
1908 4e338533 Michael Hanselmann
  return GenericPollJob(job_id, _LuxiJobPollCb(cl), reporter)
1909 ceab32dd Iustin Pop
1910 ceab32dd Iustin Pop
1911 583163a6 Michael Hanselmann
def SubmitOpCode(op, cl=None, feedback_fn=None, opts=None, reporter=None):
1912 0a1e74d9 Iustin Pop
  """Legacy function to submit an opcode.
1913 0a1e74d9 Iustin Pop

1914 0a1e74d9 Iustin Pop
  This is just a simple wrapper over the construction of the processor
1915 0a1e74d9 Iustin Pop
  instance. It should be extended to better handle feedback and
1916 0a1e74d9 Iustin Pop
  interaction functions.
1917 0a1e74d9 Iustin Pop

1918 0a1e74d9 Iustin Pop
  """
1919 0a1e74d9 Iustin Pop
  if cl is None:
1920 0a1e74d9 Iustin Pop
    cl = GetClient()
1921 0a1e74d9 Iustin Pop
1922 293ba2d8 Iustin Pop
  SetGenericOpcodeOpts([op], opts)
1923 293ba2d8 Iustin Pop
1924 5d297d8a Michael Hanselmann
  job_id = SendJob([op], cl=cl)
1925 0a1e74d9 Iustin Pop
1926 583163a6 Michael Hanselmann
  op_results = PollJob(job_id, cl=cl, feedback_fn=feedback_fn,
1927 583163a6 Michael Hanselmann
                       reporter=reporter)
1928 53c04d04 Iustin Pop
1929 53c04d04 Iustin Pop
  return op_results[0]
1930 0a1e74d9 Iustin Pop
1931 0a1e74d9 Iustin Pop
1932 94428652 Iustin Pop
def SubmitOrSend(op, opts, cl=None, feedback_fn=None):
1933 94428652 Iustin Pop
  """Wrapper around SubmitOpCode or SendJob.
1934 94428652 Iustin Pop

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

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

1943 94428652 Iustin Pop
  """
1944 94428652 Iustin Pop
  if opts and opts.submit_only:
1945 293ba2d8 Iustin Pop
    job = [op]
1946 293ba2d8 Iustin Pop
    SetGenericOpcodeOpts(job, opts)
1947 293ba2d8 Iustin Pop
    job_id = SendJob(job, cl=cl)
1948 e9d741b6 Iustin Pop
    raise JobSubmittedException(job_id)
1949 94428652 Iustin Pop
  else:
1950 293ba2d8 Iustin Pop
    return SubmitOpCode(op, cl=cl, feedback_fn=feedback_fn, opts=opts)
1951 293ba2d8 Iustin Pop
1952 293ba2d8 Iustin Pop
1953 293ba2d8 Iustin Pop
def SetGenericOpcodeOpts(opcode_list, options):
1954 293ba2d8 Iustin Pop
  """Processor for generic options.
1955 293ba2d8 Iustin Pop

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

1959 293ba2d8 Iustin Pop
  @param opcode_list: list of opcodes
1960 293ba2d8 Iustin Pop
  @param options: command line options or None
1961 293ba2d8 Iustin Pop
  @return: None (in-place modification)
1962 293ba2d8 Iustin Pop

1963 293ba2d8 Iustin Pop
  """
1964 293ba2d8 Iustin Pop
  if not options:
1965 293ba2d8 Iustin Pop
    return
1966 293ba2d8 Iustin Pop
  for op in opcode_list:
1967 aa06f8c6 Michael Hanselmann
    op.debug_level = options.debug
1968 a0a6ff34 Iustin Pop
    if hasattr(options, "dry_run"):
1969 a0a6ff34 Iustin Pop
      op.dry_run = options.dry_run
1970 aa06f8c6 Michael Hanselmann
    if getattr(options, "priority", None) is not None:
1971 aa06f8c6 Michael Hanselmann
      op.priority = _PRIONAME_TO_VALUE[options.priority]
1972 94428652 Iustin Pop
1973 94428652 Iustin Pop
1974 af30b2fd Michael Hanselmann
def GetClient():
1975 af30b2fd Michael Hanselmann
  # TODO: Cache object?
1976 b33e986b Iustin Pop
  try:
1977 b33e986b Iustin Pop
    client = luxi.Client()
1978 b33e986b Iustin Pop
  except luxi.NoMasterError:
1979 d9a51679 Michael Hanselmann
    ss = ssconf.SimpleStore()
1980 d9a51679 Michael Hanselmann
1981 d9a51679 Michael Hanselmann
    # Try to read ssconf file
1982 d9a51679 Michael Hanselmann
    try:
1983 d9a51679 Michael Hanselmann
      ss.GetMasterNode()
1984 d9a51679 Michael Hanselmann
    except errors.ConfigurationError:
1985 d9a51679 Michael Hanselmann
      raise errors.OpPrereqError("Cluster not initialized or this machine is"
1986 d9a51679 Michael Hanselmann
                                 " not part of a cluster")
1987 d9a51679 Michael Hanselmann
1988 d9a51679 Michael Hanselmann
    master, myself = ssconf.GetMasterAndMyself(ss=ss)
1989 b33e986b Iustin Pop
    if master != myself:
1990 b33e986b Iustin Pop
      raise errors.OpPrereqError("This is not the master node, please connect"
1991 b33e986b Iustin Pop
                                 " to node '%s' and rerun the command" %
1992 b33e986b Iustin Pop
                                 master)
1993 d9a51679 Michael Hanselmann
    raise
1994 b33e986b Iustin Pop
  return client
1995 af30b2fd Michael Hanselmann
1996 af30b2fd Michael Hanselmann
1997 73702ee7 Iustin Pop
def FormatError(err):
1998 73702ee7 Iustin Pop
  """Return a formatted error message for a given error.
1999 73702ee7 Iustin Pop

2000 73702ee7 Iustin Pop
  This function takes an exception instance and returns a tuple
2001 73702ee7 Iustin Pop
  consisting of two values: first, the recommended exit code, and
2002 73702ee7 Iustin Pop
  second, a string describing the error message (not
2003 73702ee7 Iustin Pop
  newline-terminated).
2004 73702ee7 Iustin Pop

2005 73702ee7 Iustin Pop
  """
2006 73702ee7 Iustin Pop
  retcode = 1
2007 73702ee7 Iustin Pop
  obuf = StringIO()
2008 e2e521d0 Iustin Pop
  msg = str(err)
2009 73702ee7 Iustin Pop
  if isinstance(err, errors.ConfigurationError):
2010 e2e521d0 Iustin Pop
    txt = "Corrupt configuration file: %s" % msg
2011 46fbdd04 Iustin Pop
    logging.error(txt)
2012 e2e521d0 Iustin Pop
    obuf.write(txt + "\n")
2013 73702ee7 Iustin Pop
    obuf.write("Aborting.")
2014 73702ee7 Iustin Pop
    retcode = 2
2015 73702ee7 Iustin Pop
  elif isinstance(err, errors.HooksAbort):
2016 73702ee7 Iustin Pop
    obuf.write("Failure: hooks execution failed:\n")
2017 73702ee7 Iustin Pop
    for node, script, out in err.args[0]:
2018 73702ee7 Iustin Pop
      if out:
2019 73702ee7 Iustin Pop
        obuf.write("  node: %s, script: %s, output: %s\n" %
2020 73702ee7 Iustin Pop
                   (node, script, out))
2021 73702ee7 Iustin Pop
      else:
2022 73702ee7 Iustin Pop
        obuf.write("  node: %s, script: %s (no output)\n" %
2023 73702ee7 Iustin Pop
                   (node, script))
2024 73702ee7 Iustin Pop
  elif isinstance(err, errors.HooksFailure):
2025 e2e521d0 Iustin Pop
    obuf.write("Failure: hooks general failure: %s" % msg)
2026 73702ee7 Iustin Pop
  elif isinstance(err, errors.ResolverError):
2027 b705c7a6 Manuel Franceschini
    this_host = netutils.Hostname.GetSysName()
2028 73702ee7 Iustin Pop
    if err.args[0] == this_host:
2029 73702ee7 Iustin Pop
      msg = "Failure: can't resolve my own hostname ('%s')"
2030 73702ee7 Iustin Pop
    else:
2031 73702ee7 Iustin Pop
      msg = "Failure: can't resolve hostname '%s'"
2032 73702ee7 Iustin Pop
    obuf.write(msg % err.args[0])
2033 73702ee7 Iustin Pop
  elif isinstance(err, errors.OpPrereqError):
2034 5c983ee5 Iustin Pop
    if len(err.args) == 2:
2035 5c983ee5 Iustin Pop
      obuf.write("Failure: prerequisites not met for this"
2036 5c983ee5 Iustin Pop
               " operation:\nerror type: %s, error details:\n%s" %
2037 5c983ee5 Iustin Pop
                 (err.args[1], err.args[0]))
2038 5c983ee5 Iustin Pop
    else:
2039 5c983ee5 Iustin Pop
      obuf.write("Failure: prerequisites not met for this"
2040 5c983ee5 Iustin Pop
                 " operation:\n%s" % msg)
2041 73702ee7 Iustin Pop
  elif isinstance(err, errors.OpExecError):
2042 e2e521d0 Iustin Pop
    obuf.write("Failure: command execution error:\n%s" % msg)
2043 73702ee7 Iustin Pop
  elif isinstance(err, errors.TagError):
2044 e2e521d0 Iustin Pop
    obuf.write("Failure: invalid tag(s) given:\n%s" % msg)
2045 686d7433 Iustin Pop
  elif isinstance(err, errors.JobQueueDrainError):
2046 686d7433 Iustin Pop
    obuf.write("Failure: the job queue is marked for drain and doesn't"
2047 686d7433 Iustin Pop
               " accept new requests\n")
2048 f87b405e Michael Hanselmann
  elif isinstance(err, errors.JobQueueFull):
2049 f87b405e Michael Hanselmann
    obuf.write("Failure: the job queue is full and doesn't accept new"
2050 f87b405e Michael Hanselmann
               " job submissions until old jobs are archived\n")
2051 a5728081 Guido Trotter
  elif isinstance(err, errors.TypeEnforcementError):
2052 a5728081 Guido Trotter
    obuf.write("Parameter Error: %s" % msg)
2053 c1ce76bb Iustin Pop
  elif isinstance(err, errors.ParameterError):
2054 c1ce76bb Iustin Pop
    obuf.write("Failure: unknown/wrong parameter name '%s'" % msg)
2055 03a8dbdc Iustin Pop
  elif isinstance(err, luxi.NoMasterError):
2056 03a8dbdc Iustin Pop
    obuf.write("Cannot communicate with the master daemon.\nIs it running"
2057 082c5adb Michael Hanselmann
               " and listening for connections?")
2058 03a8dbdc Iustin Pop
  elif isinstance(err, luxi.TimeoutError):
2059 cd4c86a8 Michael Hanselmann
    obuf.write("Timeout while talking to the master daemon. Jobs might have"
2060 cd4c86a8 Michael Hanselmann
               " been submitted and will continue to run even if the call"
2061 cd4c86a8 Michael Hanselmann
               " timed out. Useful commands in this situation are \"gnt-job"
2062 cd4c86a8 Michael Hanselmann
               " list\", \"gnt-job cancel\" and \"gnt-job watch\". Error:\n")
2063 cd4c86a8 Michael Hanselmann
    obuf.write(msg)
2064 5a1c22fe Iustin Pop
  elif isinstance(err, luxi.PermissionError):
2065 5a1c22fe Iustin Pop
    obuf.write("It seems you don't have permissions to connect to the"
2066 5a1c22fe Iustin Pop
               " master daemon.\nPlease retry as a different user.")
2067 03a8dbdc Iustin Pop
  elif isinstance(err, luxi.ProtocolError):
2068 03a8dbdc Iustin Pop
    obuf.write("Unhandled protocol error while talking to the master daemon:\n"
2069 03a8dbdc Iustin Pop
               "%s" % msg)
2070 91c622a8 Iustin Pop
  elif isinstance(err, errors.JobLost):
2071 91c622a8 Iustin Pop
    obuf.write("Error checking job status: %s" % msg)
2072 cb1e6c3c Michael Hanselmann
  elif isinstance(err, errors.QueryFilterParseError):
2073 cb1e6c3c Michael Hanselmann
    obuf.write("Error while parsing query filter: %s\n" % err.args[0])
2074 cb1e6c3c Michael Hanselmann
    obuf.write("\n".join(err.GetDetails()))
2075 797506fc Michael Hanselmann
  elif isinstance(err, errors.GenericError):
2076 797506fc Michael Hanselmann
    obuf.write("Unhandled Ganeti error: %s" % msg)
2077 e9d741b6 Iustin Pop
  elif isinstance(err, JobSubmittedException):
2078 e9d741b6 Iustin Pop
    obuf.write("JobID: %s\n" % err.args[0])
2079 e9d741b6 Iustin Pop
    retcode = 0
2080 73702ee7 Iustin Pop
  else:
2081 e2e521d0 Iustin Pop
    obuf.write("Unhandled exception: %s" % msg)
2082 d0c8c01d Iustin Pop
  return retcode, obuf.getvalue().rstrip("\n")
2083 73702ee7 Iustin Pop
2084 73702ee7 Iustin Pop
2085 ef9fa5b9 René Nussbaumer
def GenericMain(commands, override=None, aliases=None,
2086 ef9fa5b9 René Nussbaumer
                env_override=frozenset()):
2087 a8083063 Iustin Pop
  """Generic main function for all the gnt-* commands.
2088 a8083063 Iustin Pop

2089 ef9fa5b9 René Nussbaumer
  @param commands: a dictionary with a special structure, see the design doc
2090 ef9fa5b9 René Nussbaumer
                   for command line handling.
2091 ef9fa5b9 René Nussbaumer
  @param override: if not None, we expect a dictionary with keys that will
2092 ef9fa5b9 René Nussbaumer
                   override command line options; this can be used to pass
2093 ef9fa5b9 René Nussbaumer
                   options from the scripts to generic functions
2094 ef9fa5b9 René Nussbaumer
  @param aliases: dictionary with command aliases {'alias': 'target, ...}
2095 ef9fa5b9 René Nussbaumer
  @param env_override: list of environment names which are allowed to submit
2096 ef9fa5b9 René Nussbaumer
                       default args for commands
2097 a8083063 Iustin Pop

2098 a8083063 Iustin Pop
  """
2099 a8083063 Iustin Pop
  # save the program name and the entire command line for later logging
2100 a8083063 Iustin Pop
  if sys.argv:
2101 a8083063 Iustin Pop
    binary = os.path.basename(sys.argv[0]) or sys.argv[0]
2102 a8083063 Iustin Pop
    if len(sys.argv) >= 2:
2103 a8083063 Iustin Pop
      binary += " " + sys.argv[1]
2104 a8083063 Iustin Pop
      old_cmdline = " ".join(sys.argv[2:])
2105 a8083063 Iustin Pop
    else:
2106 a8083063 Iustin Pop
      old_cmdline = ""
2107 a8083063 Iustin Pop
  else:
2108 a8083063 Iustin Pop
    binary = "<unknown program>"
2109 a8083063 Iustin Pop
    old_cmdline = ""
2110 a8083063 Iustin Pop
2111 de47cf8f Guido Trotter
  if aliases is None:
2112 de47cf8f Guido Trotter
    aliases = {}
2113 de47cf8f Guido Trotter
2114 3126878d Guido Trotter
  try:
2115 ef9fa5b9 René Nussbaumer
    func, options, args = _ParseArgs(sys.argv, commands, aliases, env_override)
2116 3126878d Guido Trotter
  except errors.ParameterError, err:
2117 3126878d Guido Trotter
    result, err_msg = FormatError(err)
2118 3126878d Guido Trotter
    ToStderr(err_msg)
2119 3126878d Guido Trotter
    return 1
2120 3126878d Guido Trotter
2121 a8083063 Iustin Pop
  if func is None: # parse error
2122 a8083063 Iustin Pop
    return 1
2123 a8083063 Iustin Pop
2124 334d1483 Iustin Pop
  if override is not None:
2125 334d1483 Iustin Pop
    for key, val in override.iteritems():
2126 334d1483 Iustin Pop
      setattr(options, key, val)
2127 334d1483 Iustin Pop
2128 cfcc79c6 Michael Hanselmann
  utils.SetupLogging(constants.LOG_COMMANDS, binary, debug=options.debug,
2129 cfcc79c6 Michael Hanselmann
                     stderr_logging=True)
2130 a8083063 Iustin Pop
2131 a8083063 Iustin Pop
  if old_cmdline:
2132 46fbdd04 Iustin Pop
    logging.info("run with arguments '%s'", old_cmdline)
2133 a8083063 Iustin Pop
  else:
2134 46fbdd04 Iustin Pop
    logging.info("run with no arguments")
2135 a8083063 Iustin Pop
2136 a8083063 Iustin Pop
  try:
2137 a4af651e Iustin Pop
    result = func(options, args)
2138 d8353c3a Iustin Pop
  except (errors.GenericError, luxi.ProtocolError,
2139 d8353c3a Iustin Pop
          JobSubmittedException), err:
2140 a4af651e Iustin Pop
    result, err_msg = FormatError(err)
2141 5bbd3f7f Michael Hanselmann
    logging.exception("Error during command processing")
2142 46fbdd04 Iustin Pop
    ToStderr(err_msg)
2143 8a53b55f Iustin Pop
  except KeyboardInterrupt:
2144 8a53b55f Iustin Pop
    result = constants.EXIT_FAILURE
2145 8a53b55f Iustin Pop
    ToStderr("Aborted. Note that if the operation created any jobs, they"
2146 8a53b55f Iustin Pop
             " might have been submitted and"
2147 8a53b55f Iustin Pop
             " will continue to run in the background.")
2148 225e2544 Iustin Pop
  except IOError, err:
2149 225e2544 Iustin Pop
    if err.errno == errno.EPIPE:
2150 225e2544 Iustin Pop
      # our terminal went away, we'll exit
2151 225e2544 Iustin Pop
      sys.exit(constants.EXIT_FAILURE)
2152 225e2544 Iustin Pop
    else:
2153 225e2544 Iustin Pop
      raise
2154 a8083063 Iustin Pop
2155 a8083063 Iustin Pop
  return result
2156 137161c9 Michael Hanselmann
2157 137161c9 Michael Hanselmann
2158 845c79d8 Michael Hanselmann
def ParseNicOption(optvalue):
2159 845c79d8 Michael Hanselmann
  """Parses the value of the --net option(s).
2160 845c79d8 Michael Hanselmann

2161 845c79d8 Michael Hanselmann
  """
2162 845c79d8 Michael Hanselmann
  try:
2163 845c79d8 Michael Hanselmann
    nic_max = max(int(nidx[0]) + 1 for nidx in optvalue)
2164 845c79d8 Michael Hanselmann
  except (TypeError, ValueError), err:
2165 845c79d8 Michael Hanselmann
    raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err))
2166 845c79d8 Michael Hanselmann
2167 845c79d8 Michael Hanselmann
  nics = [{}] * nic_max
2168 845c79d8 Michael Hanselmann
  for nidx, ndict in optvalue:
2169 845c79d8 Michael Hanselmann
    nidx = int(nidx)
2170 845c79d8 Michael Hanselmann
2171 845c79d8 Michael Hanselmann
    if not isinstance(ndict, dict):
2172 845c79d8 Michael Hanselmann
      raise errors.OpPrereqError("Invalid nic/%d value: expected dict,"
2173 845c79d8 Michael Hanselmann
                                 " got %s" % (nidx, ndict))
2174 845c79d8 Michael Hanselmann
2175 845c79d8 Michael Hanselmann
    utils.ForceDictType(ndict, constants.INIC_PARAMS_TYPES)
2176 845c79d8 Michael Hanselmann
2177 845c79d8 Michael Hanselmann
    nics[nidx] = ndict
2178 845c79d8 Michael Hanselmann
2179 845c79d8 Michael Hanselmann
  return nics
2180 845c79d8 Michael Hanselmann
2181 845c79d8 Michael Hanselmann
2182 d77490c5 Iustin Pop
def GenericInstanceCreate(mode, opts, args):
2183 d77490c5 Iustin Pop
  """Add an instance to the cluster via either creation or import.
2184 d77490c5 Iustin Pop

2185 d77490c5 Iustin Pop
  @param mode: constants.INSTANCE_CREATE or constants.INSTANCE_IMPORT
2186 d77490c5 Iustin Pop
  @param opts: the command line options selected by the user
2187 d77490c5 Iustin Pop
  @type args: list
2188 d77490c5 Iustin Pop
  @param args: should contain only one element, the new instance name
2189 d77490c5 Iustin Pop
  @rtype: int
2190 d77490c5 Iustin Pop
  @return: the desired exit code
2191 d77490c5 Iustin Pop

2192 d77490c5 Iustin Pop
  """
2193 d77490c5 Iustin Pop
  instance = args[0]
2194 d77490c5 Iustin Pop
2195 d77490c5 Iustin Pop
  (pnode, snode) = SplitNodeOption(opts.node)
2196 d77490c5 Iustin Pop
2197 d77490c5 Iustin Pop
  hypervisor = None
2198 d77490c5 Iustin Pop
  hvparams = {}
2199 d77490c5 Iustin Pop
  if opts.hypervisor:
2200 d77490c5 Iustin Pop
    hypervisor, hvparams = opts.hypervisor
2201 d77490c5 Iustin Pop
2202 d77490c5 Iustin Pop
  if opts.nics:
2203 845c79d8 Michael Hanselmann
    nics = ParseNicOption(opts.nics)
2204 d77490c5 Iustin Pop
  elif opts.no_nics:
2205 d77490c5 Iustin Pop
    # no nics
2206 d77490c5 Iustin Pop
    nics = []
2207 0af0f641 Iustin Pop
  elif mode == constants.INSTANCE_CREATE:
2208 d77490c5 Iustin Pop
    # default of one nic, all auto
2209 d77490c5 Iustin Pop
    nics = [{}]
2210 0af0f641 Iustin Pop
  else:
2211 0af0f641 Iustin Pop
    # mode == import
2212 0af0f641 Iustin Pop
    nics = []
2213 d77490c5 Iustin Pop
2214 d77490c5 Iustin Pop
  if opts.disk_template == constants.DT_DISKLESS:
2215 d77490c5 Iustin Pop
    if opts.disks or opts.sd_size is not None:
2216 d77490c5 Iustin Pop
      raise errors.OpPrereqError("Diskless instance but disk"
2217 d77490c5 Iustin Pop
                                 " information passed")
2218 d77490c5 Iustin Pop
    disks = []
2219 d77490c5 Iustin Pop
  else:
2220 9b12ed0f Iustin Pop
    if (not opts.disks and not opts.sd_size
2221 9b12ed0f Iustin Pop
        and mode == constants.INSTANCE_CREATE):
2222 d77490c5 Iustin Pop
      raise errors.OpPrereqError("No disk information specified")
2223 d77490c5 Iustin Pop
    if opts.disks and opts.sd_size is not None:
2224 d77490c5 Iustin Pop
      raise errors.OpPrereqError("Please use either the '--disk' or"
2225 d77490c5 Iustin Pop
                                 " '-s' option")
2226 d77490c5 Iustin Pop
    if opts.sd_size is not None:
2227 ccfa86ba Michael Hanselmann
      opts.disks = [(0, {constants.IDISK_SIZE: opts.sd_size})]
2228 9b12ed0f Iustin Pop
2229 9b12ed0f Iustin Pop
    if opts.disks:
2230 9b12ed0f Iustin Pop
      try:
2231 9b12ed0f Iustin Pop
        disk_max = max(int(didx[0]) + 1 for didx in opts.disks)
2232 9b12ed0f Iustin Pop
      except ValueError, err:
2233 9b12ed0f Iustin Pop
        raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err))
2234 9b12ed0f Iustin Pop
      disks = [{}] * disk_max
2235 9b12ed0f Iustin Pop
    else:
2236 9b12ed0f Iustin Pop
      disks = []
2237 d77490c5 Iustin Pop
    for didx, ddict in opts.disks:
2238 d77490c5 Iustin Pop
      didx = int(didx)
2239 d77490c5 Iustin Pop
      if not isinstance(ddict, dict):
2240 d77490c5 Iustin Pop
        msg = "Invalid disk/%d value: expected dict, got %s" % (didx, ddict)
2241 d77490c5 Iustin Pop
        raise errors.OpPrereqError(msg)
2242 ccfa86ba Michael Hanselmann
      elif constants.IDISK_SIZE in ddict:
2243 ccfa86ba Michael Hanselmann
        if constants.IDISK_ADOPT in ddict:
2244 5029db65 Iustin Pop
          raise errors.OpPrereqError("Only one of 'size' and 'adopt' allowed"
2245 5029db65 Iustin Pop
                                     " (disk %d)" % didx)
2246 5029db65 Iustin Pop
        try:
2247 ccfa86ba Michael Hanselmann
          ddict[constants.IDISK_SIZE] = \
2248 ccfa86ba Michael Hanselmann
            utils.ParseUnit(ddict[constants.IDISK_SIZE])
2249 5029db65 Iustin Pop
        except ValueError, err:
2250 5029db65 Iustin Pop
          raise errors.OpPrereqError("Invalid disk size for disk %d: %s" %
2251 5029db65 Iustin Pop
                                     (didx, err))
2252 ccfa86ba Michael Hanselmann
      elif constants.IDISK_ADOPT in ddict:
2253 5029db65 Iustin Pop
        if mode == constants.INSTANCE_IMPORT:
2254 5029db65 Iustin Pop
          raise errors.OpPrereqError("Disk adoption not allowed for instance"
2255 5029db65 Iustin Pop
                                     " import")
2256 ccfa86ba Michael Hanselmann
        ddict[constants.IDISK_SIZE] = 0
2257 5029db65 Iustin Pop
      else:
2258 5029db65 Iustin Pop
        raise errors.OpPrereqError("Missing size or adoption source for"
2259 5029db65 Iustin Pop
                                   " disk %d" % didx)
2260 d77490c5 Iustin Pop
      disks[didx] = ddict
2261 d77490c5 Iustin Pop
2262 a57981c5 Apollon Oikonomopoulos
  if opts.tags is not None:
2263 0f8810df Michael Hanselmann
    tags = opts.tags.split(",")
2264 a57981c5 Apollon Oikonomopoulos
  else:
2265 a57981c5 Apollon Oikonomopoulos
    tags = []
2266 a57981c5 Apollon Oikonomopoulos
2267 b2e233a5 Guido Trotter
  utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_COMPAT)
2268 d77490c5 Iustin Pop
  utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
2269 d77490c5 Iustin Pop
2270 d77490c5 Iustin Pop
  if mode == constants.INSTANCE_CREATE:
2271 d77490c5 Iustin Pop
    start = opts.start
2272 d77490c5 Iustin Pop
    os_type = opts.os
2273 1ee8e01a Guido Trotter
    force_variant = opts.force_variant
2274 d77490c5 Iustin Pop
    src_node = None
2275 d77490c5 Iustin Pop
    src_path = None
2276 25a8792c Iustin Pop
    no_install = opts.no_install
2277 e588764d Iustin Pop
    identify_defaults = False
2278 d77490c5 Iustin Pop
  elif mode == constants.INSTANCE_IMPORT:
2279 d77490c5 Iustin Pop
    start = False
2280 d77490c5 Iustin Pop
    os_type = None
2281 1ee8e01a Guido Trotter
    force_variant = False
2282 d77490c5 Iustin Pop
    src_node = opts.src_node
2283 d77490c5 Iustin Pop
    src_path = opts.src_dir
2284 25a8792c Iustin Pop
    no_install = None
2285 e588764d Iustin Pop
    identify_defaults = opts.identify_defaults
2286 d77490c5 Iustin Pop
  else:
2287 d77490c5 Iustin Pop
    raise errors.ProgrammerError("Invalid creation mode %s" % mode)
2288 d77490c5 Iustin Pop
2289 e1530b10 Iustin Pop
  op = opcodes.OpInstanceCreate(instance_name=instance,
2290 d77490c5 Iustin Pop
                                disks=disks,
2291 d77490c5 Iustin Pop
                                disk_template=opts.disk_template,
2292 d77490c5 Iustin Pop
                                nics=nics,
2293 d77490c5 Iustin Pop
                                pnode=pnode, snode=snode,
2294 d77490c5 Iustin Pop
                                ip_check=opts.ip_check,
2295 460d22be Iustin Pop
                                name_check=opts.name_check,
2296 d77490c5 Iustin Pop
                                wait_for_sync=opts.wait_for_sync,
2297 d77490c5 Iustin Pop
                                file_storage_dir=opts.file_storage_dir,
2298 d77490c5 Iustin Pop
                                file_driver=opts.file_driver,
2299 d77490c5 Iustin Pop
                                iallocator=opts.iallocator,
2300 d77490c5 Iustin Pop
                                hypervisor=hypervisor,
2301 d77490c5 Iustin Pop
                                hvparams=hvparams,
2302 d77490c5 Iustin Pop
                                beparams=opts.beparams,
2303 062a7100 Iustin Pop
                                osparams=opts.osparams,
2304 d77490c5 Iustin Pop
                                mode=mode,
2305 d77490c5 Iustin Pop
                                start=start,
2306 d77490c5 Iustin Pop
                                os_type=os_type,
2307 1ee8e01a Guido Trotter
                                force_variant=force_variant,
2308 d77490c5 Iustin Pop
                                src_node=src_node,
2309 25a8792c Iustin Pop
                                src_path=src_path,
2310 a57981c5 Apollon Oikonomopoulos
                                tags=tags,
2311 e588764d Iustin Pop
                                no_install=no_install,
2312 10889e0c René Nussbaumer
                                identify_defaults=identify_defaults,
2313 10889e0c René Nussbaumer
                                ignore_ipolicy=opts.ignore_ipolicy)
2314 d77490c5 Iustin Pop
2315 d77490c5 Iustin Pop
  SubmitOrSend(op, opts)
2316 d77490c5 Iustin Pop
  return 0
2317 d77490c5 Iustin Pop
2318 d77490c5 Iustin Pop
2319 7e49b6ce Michael Hanselmann
class _RunWhileClusterStoppedHelper:
2320 7e49b6ce Michael Hanselmann
  """Helper class for L{RunWhileClusterStopped} to simplify state management
2321 7e49b6ce Michael Hanselmann

2322 7e49b6ce Michael Hanselmann
  """
2323 7e49b6ce Michael Hanselmann
  def __init__(self, feedback_fn, cluster_name, master_node, online_nodes):
2324 7e49b6ce Michael Hanselmann
    """Initializes this class.
2325 7e49b6ce Michael Hanselmann

2326 7e49b6ce Michael Hanselmann
    @type feedback_fn: callable
2327 7e49b6ce Michael Hanselmann
    @param feedback_fn: Feedback function
2328 7e49b6ce Michael Hanselmann
    @type cluster_name: string
2329 7e49b6ce Michael Hanselmann
    @param cluster_name: Cluster name
2330 7e49b6ce Michael Hanselmann
    @type master_node: string
2331 7e49b6ce Michael Hanselmann
    @param master_node Master node name
2332 7e49b6ce Michael Hanselmann
    @type online_nodes: list
2333 7e49b6ce Michael Hanselmann
    @param online_nodes: List of names of online nodes
2334 7e49b6ce Michael Hanselmann

2335 7e49b6ce Michael Hanselmann
    """
2336 7e49b6ce Michael Hanselmann
    self.feedback_fn = feedback_fn
2337 7e49b6ce Michael Hanselmann
    self.cluster_name = cluster_name
2338 7e49b6ce Michael Hanselmann
    self.master_node = master_node
2339 7e49b6ce Michael Hanselmann
    self.online_nodes = online_nodes
2340 7e49b6ce Michael Hanselmann
2341 7e49b6ce Michael Hanselmann
    self.ssh = ssh.SshRunner(self.cluster_name)
2342 7e49b6ce Michael Hanselmann
2343 7e49b6ce Michael Hanselmann
    self.nonmaster_nodes = [name for name in online_nodes
2344 7e49b6ce Michael Hanselmann
                            if name != master_node]
2345 7e49b6ce Michael Hanselmann
2346 7e49b6ce Michael Hanselmann
    assert self.master_node not in self.nonmaster_nodes
2347 7e49b6ce Michael Hanselmann
2348 7e49b6ce Michael Hanselmann
  def _RunCmd(self, node_name, cmd):
2349 7e49b6ce Michael Hanselmann
    """Runs a command on the local or a remote machine.
2350 7e49b6ce Michael Hanselmann

2351 7e49b6ce Michael Hanselmann
    @type node_name: string
2352 7e49b6ce Michael Hanselmann
    @param node_name: Machine name
2353 7e49b6ce Michael Hanselmann
    @type cmd: list
2354 7e49b6ce Michael Hanselmann
    @param cmd: Command
2355 7e49b6ce Michael Hanselmann

2356 7e49b6ce Michael Hanselmann
    """
2357 7e49b6ce Michael Hanselmann
    if node_name is None or node_name == self.master_node:
2358 7e49b6ce Michael Hanselmann
      # No need to use SSH
2359 7e49b6ce Michael Hanselmann
      result = utils.RunCmd(cmd)
2360 7e49b6ce Michael Hanselmann
    else:
2361 7e49b6ce Michael Hanselmann
      result = self.ssh.Run(node_name, "root", utils.ShellQuoteArgs(cmd))
2362 7e49b6ce Michael Hanselmann
2363 7e49b6ce Michael Hanselmann
    if result.failed:
2364 7e49b6ce Michael Hanselmann
      errmsg = ["Failed to run command %s" % result.cmd]
2365 7e49b6ce Michael Hanselmann
      if node_name:
2366 7e49b6ce Michael Hanselmann
        errmsg.append("on node %s" % node_name)
2367 7e49b6ce Michael Hanselmann
      errmsg.append(": exitcode %s and error %s" %
2368 7e49b6ce Michael Hanselmann
                    (result.exit_code, result.output))
2369 7e49b6ce Michael Hanselmann
      raise errors.OpExecError(" ".join(errmsg))
2370 7e49b6ce Michael Hanselmann
2371 7e49b6ce Michael Hanselmann
  def Call(self, fn, *args):
2372 7e49b6ce Michael Hanselmann
    """Call function while all daemons are stopped.
2373 7e49b6ce Michael Hanselmann

2374 7e49b6ce Michael Hanselmann
    @type fn: callable
2375 7e49b6ce Michael Hanselmann
    @param fn: Function to be called
2376 7e49b6ce Michael Hanselmann

2377 7e49b6ce Michael Hanselmann
    """
2378 7e49b6ce Michael Hanselmann
    # Pause watcher by acquiring an exclusive lock on watcher state file
2379 7e49b6ce Michael Hanselmann
    self.feedback_fn("Blocking watcher")
2380 16e0b9c9 Michael Hanselmann
    watcher_block = utils.FileLock.Open(constants.WATCHER_LOCK_FILE)
2381 7e49b6ce Michael Hanselmann
    try:
2382 7e49b6ce Michael Hanselmann
      # TODO: Currently, this just blocks. There's no timeout.
2383 7e49b6ce Michael Hanselmann
      # TODO: Should it be a shared lock?
2384 7e49b6ce Michael Hanselmann
      watcher_block.Exclusive(blocking=True)
2385 7e49b6ce Michael Hanselmann
2386 7e49b6ce Michael Hanselmann
      # Stop master daemons, so that no new jobs can come in and all running
2387 7e49b6ce Michael Hanselmann
      # ones are finished
2388 7e49b6ce Michael Hanselmann
      self.feedback_fn("Stopping master daemons")
2389 7e49b6ce Michael Hanselmann
      self._RunCmd(None, [constants.DAEMON_UTIL, "stop-master"])
2390 7e49b6ce Michael Hanselmann
      try:
2391 7e49b6ce Michael Hanselmann
        # Stop daemons on all nodes
2392 7e49b6ce Michael Hanselmann
        for node_name in self.online_nodes:
2393 7e49b6ce Michael Hanselmann
          self.feedback_fn("Stopping daemons on %s" % node_name)
2394 7e49b6ce Michael Hanselmann
          self._RunCmd(node_name, [constants.DAEMON_UTIL, "stop-all"])
2395 7e49b6ce Michael Hanselmann
2396 7e49b6ce Michael Hanselmann
        # All daemons are shut down now
2397 7e49b6ce Michael Hanselmann
        try:
2398 7e49b6ce Michael Hanselmann
          return fn(self, *args)
2399 d512e84b Michael Hanselmann
        except Exception, err:
2400 d512e84b Michael Hanselmann
          _, errmsg = FormatError(err)
2401 7e49b6ce Michael Hanselmann
          logging.exception("Caught exception")
2402 d512e84b Michael Hanselmann
          self.feedback_fn(errmsg)
2403 7e49b6ce Michael Hanselmann
          raise
2404 7e49b6ce Michael Hanselmann
      finally:
2405 7e49b6ce Michael Hanselmann
        # Start cluster again, master node last
2406 7e49b6ce Michael Hanselmann
        for node_name in self.nonmaster_nodes + [self.master_node]:
2407 7e49b6ce Michael Hanselmann
          self.feedback_fn("Starting daemons on %s" % node_name)
2408 7e49b6ce Michael Hanselmann
          self._RunCmd(node_name, [constants.DAEMON_UTIL, "start-all"])
2409 7e49b6ce Michael Hanselmann
    finally:
2410 7e49b6ce Michael Hanselmann
      # Resume watcher
2411 7e49b6ce Michael Hanselmann
      watcher_block.Close()
2412 7e49b6ce Michael Hanselmann
2413 7e49b6ce Michael Hanselmann
2414 7e49b6ce Michael Hanselmann
def RunWhileClusterStopped(feedback_fn, fn, *args):
2415 7e49b6ce Michael Hanselmann
  """Calls a function while all cluster daemons are stopped.
2416 7e49b6ce Michael Hanselmann

2417 7e49b6ce Michael Hanselmann
  @type feedback_fn: callable
2418 7e49b6ce Michael Hanselmann
  @param feedback_fn: Feedback function
2419 7e49b6ce Michael Hanselmann
  @type fn: callable
2420 7e49b6ce Michael Hanselmann
  @param fn: Function to be called when daemons are stopped
2421 7e49b6ce Michael Hanselmann

2422 7e49b6ce Michael Hanselmann
  """
2423 7e49b6ce Michael Hanselmann
  feedback_fn("Gathering cluster information")
2424 7e49b6ce Michael Hanselmann
2425 7e49b6ce Michael Hanselmann
  # This ensures we're running on the master daemon
2426 7e49b6ce Michael Hanselmann
  cl = GetClient()
2427 7e49b6ce Michael Hanselmann
2428 7e49b6ce Michael Hanselmann
  (cluster_name, master_node) = \
2429 7e49b6ce Michael Hanselmann
    cl.QueryConfigValues(["cluster_name", "master_node"])
2430 7e49b6ce Michael Hanselmann
2431 7e49b6ce Michael Hanselmann
  online_nodes = GetOnlineNodes([], cl=cl)
2432 7e49b6ce Michael Hanselmann
2433 7e49b6ce Michael Hanselmann
  # Don't keep a reference to the client. The master daemon will go away.
2434 7e49b6ce Michael Hanselmann
  del cl
2435 7e49b6ce Michael Hanselmann
2436 7e49b6ce Michael Hanselmann
  assert master_node in online_nodes
2437 7e49b6ce Michael Hanselmann
2438 7e49b6ce Michael Hanselmann
  return _RunWhileClusterStoppedHelper(feedback_fn, cluster_name, master_node,
2439 7e49b6ce Michael Hanselmann
                                       online_nodes).Call(fn, *args)
2440 7e49b6ce Michael Hanselmann
2441 7e49b6ce Michael Hanselmann
2442 16be8703 Iustin Pop
def GenerateTable(headers, fields, separator, data,
2443 9fbfbb7b Iustin Pop
                  numfields=None, unitfields=None,
2444 9fbfbb7b Iustin Pop
                  units=None):
2445 137161c9 Michael Hanselmann
  """Prints a table with headers and different fields.
2446 137161c9 Michael Hanselmann

2447 9fbfbb7b Iustin Pop
  @type headers: dict
2448 9fbfbb7b Iustin Pop
  @param headers: dictionary mapping field names to headers for
2449 9fbfbb7b Iustin Pop
      the table
2450 9fbfbb7b Iustin Pop
  @type fields: list
2451 9fbfbb7b Iustin Pop
  @param fields: the field names corresponding to each row in
2452 9fbfbb7b Iustin Pop
      the data field
2453 9fbfbb7b Iustin Pop
  @param separator: the separator to be used; if this is None,
2454 9fbfbb7b Iustin Pop
      the default 'smart' algorithm is used which computes optimal
2455 9fbfbb7b Iustin Pop
      field width, otherwise just the separator is used between
2456 9fbfbb7b Iustin Pop
      each field
2457 9fbfbb7b Iustin Pop
  @type data: list
2458 9fbfbb7b Iustin Pop
  @param data: a list of lists, each sublist being one row to be output
2459 9fbfbb7b Iustin Pop
  @type numfields: list
2460 9fbfbb7b Iustin Pop
  @param numfields: a list with the fields that hold numeric
2461 9fbfbb7b Iustin Pop
      values and thus should be right-aligned
2462 9fbfbb7b Iustin Pop
  @type unitfields: list
2463 9fbfbb7b Iustin Pop
  @param unitfields: a list with the fields that hold numeric
2464 9fbfbb7b Iustin Pop
      values that should be formatted with the units field
2465 9fbfbb7b Iustin Pop
  @type units: string or None
2466 9fbfbb7b Iustin Pop
  @param units: the units we should use for formatting, or None for
2467 9fbfbb7b Iustin Pop
      automatic choice (human-readable for non-separator usage, otherwise
2468 9fbfbb7b Iustin Pop
      megabytes); this is a one-letter string
2469 137161c9 Michael Hanselmann

2470 137161c9 Michael Hanselmann
  """
2471 9fbfbb7b Iustin Pop
  if units is None:
2472 9fbfbb7b Iustin Pop
    if separator:
2473 9fbfbb7b Iustin Pop
      units = "m"
2474 9fbfbb7b Iustin Pop
    else:
2475 9fbfbb7b Iustin Pop
      units = "h"
2476 9fbfbb7b Iustin Pop
2477 137161c9 Michael Hanselmann
  if numfields is None:
2478 137161c9 Michael Hanselmann
    numfields = []
2479 137161c9 Michael Hanselmann
  if unitfields is None:
2480 137161c9 Michael Hanselmann
    unitfields = []
2481 137161c9 Michael Hanselmann
2482 b459a848 Andrea Spadaccini
  numfields = utils.FieldSet(*numfields)   # pylint: disable=W0142
2483 b459a848 Andrea Spadaccini
  unitfields = utils.FieldSet(*unitfields) # pylint: disable=W0142
2484 00430f8e Iustin Pop
2485 137161c9 Michael Hanselmann
  format_fields = []
2486 137161c9 Michael Hanselmann
  for field in fields:
2487 01ca31ae Iustin Pop
    if headers and field not in headers:
2488 ea5a5b74 Guido Trotter
      # TODO: handle better unknown fields (either revert to old
2489 71c1af58 Iustin Pop
      # style of raising exception, or deal more intelligently with
2490 71c1af58 Iustin Pop
      # variable fields)
2491 71c1af58 Iustin Pop
      headers[field] = field
2492 137161c9 Michael Hanselmann
    if separator is not None:
2493 137161c9 Michael Hanselmann
      format_fields.append("%s")
2494 00430f8e Iustin Pop
    elif numfields.Matches(field):
2495 137161c9 Michael Hanselmann
      format_fields.append("%*s")
2496 137161c9 Michael Hanselmann
    else:
2497 137161c9 Michael Hanselmann
      format_fields.append("%-*s")
2498 137161c9 Michael Hanselmann
2499 137161c9 Michael Hanselmann
  if separator is None:
2500 137161c9 Michael Hanselmann
    mlens = [0 for name in fields]
2501 d0c8c01d Iustin Pop
    format_str = " ".join(format_fields)
2502 137161c9 Michael Hanselmann
  else:
2503 c04bc777 Iustin Pop
    format_str = separator.replace("%", "%%").join(format_fields)
2504 137161c9 Michael Hanselmann
2505 137161c9 Michael Hanselmann
  for row in data:
2506 dcbd6288 Guido Trotter
    if row is None:
2507 dcbd6288 Guido Trotter
      continue
2508 137161c9 Michael Hanselmann
    for idx, val in enumerate(row):
2509 00430f8e Iustin Pop
      if unitfields.Matches(fields[idx]):
2510 137161c9 Michael Hanselmann
        try:
2511 137161c9 Michael Hanselmann
          val = int(val)
2512 691744c4 Iustin Pop
        except (TypeError, ValueError):
2513 137161c9 Michael Hanselmann
          pass
2514 137161c9 Michael Hanselmann
        else:
2515 9fbfbb7b Iustin Pop
          val = row[idx] = utils.FormatUnit(val, units)
2516 01ca31ae Iustin Pop
      val = row[idx] = str(val)
2517 137161c9 Michael Hanselmann
      if separator is None:
2518 137161c9 Michael Hanselmann
        mlens[idx] = max(mlens[idx], len(val))
2519 137161c9 Michael Hanselmann
2520 16be8703 Iustin Pop
  result = []
2521 137161c9 Michael Hanselmann
  if headers:
2522 137161c9 Michael Hanselmann
    args = []
2523 137161c9 Michael Hanselmann
    for idx, name in enumerate(fields):
2524 137161c9 Michael Hanselmann
      hdr = headers[name]
2525 137161c9 Michael Hanselmann
      if separator is None:
2526 137161c9 Michael Hanselmann
        mlens[idx] = max(mlens[idx], len(hdr))
2527 137161c9 Michael Hanselmann
        args.append(mlens[idx])
2528 137161c9 Michael Hanselmann
      args.append(hdr)
2529 c04bc777 Iustin Pop
    result.append(format_str % tuple(args))
2530 137161c9 Michael Hanselmann
2531 ec39d63c Michael Hanselmann
  if separator is None:
2532 ec39d63c Michael Hanselmann
    assert len(mlens) == len(fields)
2533 ec39d63c Michael Hanselmann
2534 ec39d63c Michael Hanselmann
    if fields and not numfields.Matches(fields[-1]):
2535 ec39d63c Michael Hanselmann
      mlens[-1] = 0
2536 ec39d63c Michael Hanselmann
2537 137161c9 Michael Hanselmann
  for line in data:
2538 137161c9 Michael Hanselmann
    args = []
2539 dcbd6288 Guido Trotter
    if line is None:
2540 d0c8c01d Iustin Pop
      line = ["-" for _ in fields]
2541 f1501b3f Michael Hanselmann
    for idx in range(len(fields)):
2542 137161c9 Michael Hanselmann
      if separator is None:
2543 137161c9 Michael Hanselmann
        args.append(mlens[idx])
2544 137161c9 Michael Hanselmann
      args.append(line[idx])
2545 c04bc777 Iustin Pop
    result.append(format_str % tuple(args))
2546 16be8703 Iustin Pop
2547 16be8703 Iustin Pop
  return result
2548 3386e7a9 Iustin Pop
2549 3386e7a9 Iustin Pop
2550 ee3aedff Michael Hanselmann
def _FormatBool(value):
2551 ee3aedff Michael Hanselmann
  """Formats a boolean value as a string.
2552 ee3aedff Michael Hanselmann

2553 ee3aedff Michael Hanselmann
  """
2554 ee3aedff Michael Hanselmann
  if value:
2555 ee3aedff Michael Hanselmann
    return "Y"
2556 ee3aedff Michael Hanselmann
  return "N"
2557 ee3aedff Michael Hanselmann
2558 ee3aedff Michael Hanselmann
2559 ee3aedff Michael Hanselmann
#: Default formatting for query results; (callback, align right)
2560 ee3aedff Michael Hanselmann
_DEFAULT_FORMAT_QUERY = {
2561 ee3aedff Michael Hanselmann
  constants.QFT_TEXT: (str, False),
2562 ee3aedff Michael Hanselmann
  constants.QFT_BOOL: (_FormatBool, False),
2563 ee3aedff Michael Hanselmann
  constants.QFT_NUMBER: (str, True),
2564 ee3aedff Michael Hanselmann
  constants.QFT_TIMESTAMP: (utils.FormatTime, False),
2565 ee3aedff Michael Hanselmann
  constants.QFT_OTHER: (str, False),
2566 ee3aedff Michael Hanselmann
  constants.QFT_UNKNOWN: (str, False),
2567 ee3aedff Michael Hanselmann
  }
2568 ee3aedff Michael Hanselmann
2569 ee3aedff Michael Hanselmann
2570 ee3aedff Michael Hanselmann
def _GetColumnFormatter(fdef, override, unit):
2571 ee3aedff Michael Hanselmann
  """Returns formatting function for a field.
2572 ee3aedff Michael Hanselmann

2573 ee3aedff Michael Hanselmann
  @type fdef: L{objects.QueryFieldDefinition}
2574 ee3aedff Michael Hanselmann
  @type override: dict
2575 ee3aedff Michael Hanselmann
  @param override: Dictionary for overriding field formatting functions,
2576 ee3aedff Michael Hanselmann
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
2577 ee3aedff Michael Hanselmann
  @type unit: string
2578 ee3aedff Michael Hanselmann
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT}
2579 ee3aedff Michael Hanselmann
  @rtype: tuple; (callable, bool)
2580 ee3aedff Michael Hanselmann
  @return: Returns the function to format a value (takes one parameter) and a
2581 ee3aedff Michael Hanselmann
    boolean for aligning the value on the right-hand side
2582 ee3aedff Michael Hanselmann

2583 ee3aedff Michael Hanselmann
  """
2584 ee3aedff Michael Hanselmann
  fmt = override.get(fdef.name, None)
2585 ee3aedff Michael Hanselmann
  if fmt is not None:
2586 ee3aedff Michael Hanselmann
    return fmt
2587 ee3aedff Michael Hanselmann
2588 ee3aedff Michael Hanselmann
  assert constants.QFT_UNIT not in _DEFAULT_FORMAT_QUERY
2589 ee3aedff Michael Hanselmann
2590 ee3aedff Michael Hanselmann
  if fdef.kind == constants.QFT_UNIT:
2591 ee3aedff Michael Hanselmann
    # Can't keep this information in the static dictionary
2592 ee3aedff Michael Hanselmann
    return (lambda value: utils.FormatUnit(value, unit), True)
2593 ee3aedff Michael Hanselmann
2594 ee3aedff Michael Hanselmann
  fmt = _DEFAULT_FORMAT_QUERY.get(fdef.kind, None)
2595 ee3aedff Michael Hanselmann
  if fmt is not None:
2596 ee3aedff Michael Hanselmann
    return fmt
2597 ee3aedff Michael Hanselmann
2598 ee3aedff Michael Hanselmann
  raise NotImplementedError("Can't format column type '%s'" % fdef.kind)
2599 ee3aedff Michael Hanselmann
2600 ee3aedff Michael Hanselmann
2601 ee3aedff Michael Hanselmann
class _QueryColumnFormatter:
2602 ee3aedff Michael Hanselmann
  """Callable class for formatting fields of a query.
2603 ee3aedff Michael Hanselmann

2604 ee3aedff Michael Hanselmann
  """
2605 f0b1bafe Iustin Pop
  def __init__(self, fn, status_fn, verbose):
2606 ee3aedff Michael Hanselmann
    """Initializes this class.
2607 ee3aedff Michael Hanselmann

2608 ee3aedff Michael Hanselmann
    @type fn: callable
2609 ee3aedff Michael Hanselmann
    @param fn: Formatting function
2610 ee3aedff Michael Hanselmann
    @type status_fn: callable
2611 ee3aedff Michael Hanselmann
    @param status_fn: Function to report fields' status
2612 f0b1bafe Iustin Pop
    @type verbose: boolean
2613 f0b1bafe Iustin Pop
    @param verbose: whether to use verbose field descriptions or not
2614 ee3aedff Michael Hanselmann

2615 ee3aedff Michael Hanselmann
    """
2616 ee3aedff Michael Hanselmann
    self._fn = fn
2617 ee3aedff Michael Hanselmann
    self._status_fn = status_fn
2618 cbfa4f0f Michael Hanselmann
    self._verbose = verbose
2619 ee3aedff Michael Hanselmann
2620 ee3aedff Michael Hanselmann
  def __call__(self, data):
2621 ee3aedff Michael Hanselmann
    """Returns a field's string representation.
2622 ee3aedff Michael Hanselmann

2623 ee3aedff Michael Hanselmann
    """
2624 ee3aedff Michael Hanselmann
    (status, value) = data
2625 ee3aedff Michael Hanselmann
2626 ee3aedff Michael Hanselmann
    # Report status
2627 ee3aedff Michael Hanselmann
    self._status_fn(status)
2628 ee3aedff Michael Hanselmann
2629 cfb084ae René Nussbaumer
    if status == constants.RS_NORMAL:
2630 ee3aedff Michael Hanselmann
      return self._fn(value)
2631 ee3aedff Michael Hanselmann
2632 ee3aedff Michael Hanselmann
    assert value is None, \
2633 ee3aedff Michael Hanselmann
           "Found value %r for abnormal status %s" % (value, status)
2634 ee3aedff Michael Hanselmann
2635 f2c6673d Michael Hanselmann
    return FormatResultError(status, self._verbose)
2636 ee3aedff Michael Hanselmann
2637 ee3aedff Michael Hanselmann
2638 f2c6673d Michael Hanselmann
def FormatResultError(status, verbose):
2639 ae95e419 René Nussbaumer
  """Formats result status other than L{constants.RS_NORMAL}.
2640 ee3aedff Michael Hanselmann

2641 ae95e419 René Nussbaumer
  @param status: The result status
2642 f2c6673d Michael Hanselmann
  @type verbose: boolean
2643 f2c6673d Michael Hanselmann
  @param verbose: Whether to return the verbose text
2644 ae95e419 René Nussbaumer
  @return: Text of result status
2645 a6070ef7 Michael Hanselmann

2646 ae95e419 René Nussbaumer
  """
2647 ae95e419 René Nussbaumer
  assert status != constants.RS_NORMAL, \
2648 cbfa4f0f Michael Hanselmann
         "FormatResultError called with status equal to constants.RS_NORMAL"
2649 ae95e419 René Nussbaumer
  try:
2650 cbfa4f0f Michael Hanselmann
    (verbose_text, normal_text) = constants.RSS_DESCRIPTION[status]
2651 ae95e419 René Nussbaumer
  except KeyError:
2652 ee3aedff Michael Hanselmann
    raise NotImplementedError("Unknown status %s" % status)
2653 cbfa4f0f Michael Hanselmann
  else:
2654 cbfa4f0f Michael Hanselmann
    if verbose:
2655 cbfa4f0f Michael Hanselmann
      return verbose_text
2656 cbfa4f0f Michael Hanselmann
    return normal_text
2657 ee3aedff Michael Hanselmann
2658 ee3aedff Michael Hanselmann
2659 ee3aedff Michael Hanselmann
def FormatQueryResult(result, unit=None, format_override=None, separator=None,
2660 f0b1bafe Iustin Pop
                      header=False, verbose=False):
2661 ee3aedff Michael Hanselmann
  """Formats data in L{objects.QueryResponse}.
2662 ee3aedff Michael Hanselmann

2663 ee3aedff Michael Hanselmann
  @type result: L{objects.QueryResponse}
2664 ee3aedff Michael Hanselmann
  @param result: result of query operation
2665 ee3aedff Michael Hanselmann
  @type unit: string
2666 ee3aedff Michael Hanselmann
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT},
2667 18009c1e Iustin Pop
    see L{utils.text.FormatUnit}
2668 ee3aedff Michael Hanselmann
  @type format_override: dict
2669 ee3aedff Michael Hanselmann
  @param format_override: Dictionary for overriding field formatting functions,
2670 ee3aedff Michael Hanselmann
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
2671 ee3aedff Michael Hanselmann
  @type separator: string or None
2672 ee3aedff Michael Hanselmann
  @param separator: String used to separate fields
2673 ee3aedff Michael Hanselmann
  @type header: bool
2674 ee3aedff Michael Hanselmann
  @param header: Whether to output header row
2675 f0b1bafe Iustin Pop
  @type verbose: boolean
2676 f0b1bafe Iustin Pop
  @param verbose: whether to use verbose field descriptions or not
2677 ee3aedff Michael Hanselmann

2678 ee3aedff Michael Hanselmann
  """
2679 ee3aedff Michael Hanselmann
  if unit is None:
2680 ee3aedff Michael Hanselmann
    if separator:
2681 ee3aedff Michael Hanselmann
      unit = "m"
2682 ee3aedff Michael Hanselmann
    else:
2683 ee3aedff Michael Hanselmann
      unit = "h"
2684 ee3aedff Michael Hanselmann
2685 ee3aedff Michael Hanselmann
  if format_override is None:
2686 ee3aedff Michael Hanselmann
    format_override = {}
2687 ee3aedff Michael Hanselmann
2688 cfb084ae René Nussbaumer
  stats = dict.fromkeys(constants.RS_ALL, 0)
2689 ee3aedff Michael Hanselmann
2690 ee3aedff Michael Hanselmann
  def _RecordStatus(status):
2691 ee3aedff Michael Hanselmann
    if status in stats:
2692 ee3aedff Michael Hanselmann
      stats[status] += 1
2693 ee3aedff Michael Hanselmann
2694 ee3aedff Michael Hanselmann
  columns = []
2695 ee3aedff Michael Hanselmann
  for fdef in result.fields:
2696 ee3aedff Michael Hanselmann
    assert fdef.title and fdef.name
2697 ee3aedff Michael Hanselmann
    (fn, align_right) = _GetColumnFormatter(fdef, format_override, unit)
2698 ee3aedff Michael Hanselmann
    columns.append(TableColumn(fdef.title,
2699 f0b1bafe Iustin Pop
                               _QueryColumnFormatter(fn, _RecordStatus,
2700 f0b1bafe Iustin Pop
                                                     verbose),
2701 ee3aedff Michael Hanselmann
                               align_right))
2702 ee3aedff Michael Hanselmann
2703 ee3aedff Michael Hanselmann
  table = FormatTable(result.data, columns, header, separator)
2704 ee3aedff Michael Hanselmann
2705 ee3aedff Michael Hanselmann
  # Collect statistics
2706 cfb084ae René Nussbaumer
  assert len(stats) == len(constants.RS_ALL)
2707 ee3aedff Michael Hanselmann
  assert compat.all(count >= 0 for count in stats.values())
2708 ee3aedff Michael Hanselmann
2709 ee3aedff Michael Hanselmann
  # Determine overall status. If there was no data, unknown fields must be
2710 ee3aedff Michael Hanselmann
  # detected via the field definitions.
2711 cfb084ae René Nussbaumer
  if (stats[constants.RS_UNKNOWN] or
2712 ee3aedff Michael Hanselmann
      (not result.data and _GetUnknownFields(result.fields))):
2713 ee3aedff Michael Hanselmann
    status = QR_UNKNOWN
2714 ee3aedff Michael Hanselmann
  elif compat.any(count > 0 for key, count in stats.items()
2715 cfb084ae René Nussbaumer
                  if key != constants.RS_NORMAL):
2716 ee3aedff Michael Hanselmann
    status = QR_INCOMPLETE
2717 ee3aedff Michael Hanselmann
  else:
2718 ee3aedff Michael Hanselmann
    status = QR_NORMAL
2719 ee3aedff Michael Hanselmann
2720 ee3aedff Michael Hanselmann
  return (status, table)
2721 ee3aedff Michael Hanselmann
2722 ee3aedff Michael Hanselmann
2723 ee3aedff Michael Hanselmann
def _GetUnknownFields(fdefs):
2724 ee3aedff Michael Hanselmann
  """Returns list of unknown fields included in C{fdefs}.
2725 ee3aedff Michael Hanselmann

2726 ee3aedff Michael Hanselmann
  @type fdefs: list of L{objects.QueryFieldDefinition}
2727 ee3aedff Michael Hanselmann

2728 ee3aedff Michael Hanselmann
  """
2729 ee3aedff Michael Hanselmann
  return [fdef for fdef in fdefs
2730 ee3aedff Michael Hanselmann
          if fdef.kind == constants.QFT_UNKNOWN]
2731 ee3aedff Michael Hanselmann
2732 ee3aedff Michael Hanselmann
2733 ee3aedff Michael Hanselmann
def _WarnUnknownFields(fdefs):
2734 ee3aedff Michael Hanselmann
  """Prints a warning to stderr if a query included unknown fields.
2735 ee3aedff Michael Hanselmann

2736 ee3aedff Michael Hanselmann
  @type fdefs: list of L{objects.QueryFieldDefinition}
2737 ee3aedff Michael Hanselmann

2738 ee3aedff Michael Hanselmann
  """
2739 ee3aedff Michael Hanselmann
  unknown = _GetUnknownFields(fdefs)
2740 ee3aedff Michael Hanselmann
  if unknown:
2741 ee3aedff Michael Hanselmann
    ToStderr("Warning: Queried for unknown fields %s",
2742 ee3aedff Michael Hanselmann
             utils.CommaJoin(fdef.name for fdef in unknown))
2743 ee3aedff Michael Hanselmann
    return True
2744 ee3aedff Michael Hanselmann
2745 ee3aedff Michael Hanselmann
  return False
2746 ee3aedff Michael Hanselmann
2747 ee3aedff Michael Hanselmann
2748 ee3aedff Michael Hanselmann
def GenericList(resource, fields, names, unit, separator, header, cl=None,
2749 2928de47 Michael Hanselmann
                format_override=None, verbose=False, force_filter=False):
2750 ee3aedff Michael Hanselmann
  """Generic implementation for listing all items of a resource.
2751 ee3aedff Michael Hanselmann

2752 abd66bf8 Michael Hanselmann
  @param resource: One of L{constants.QR_VIA_LUXI}
2753 ee3aedff Michael Hanselmann
  @type fields: list of strings
2754 ee3aedff Michael Hanselmann
  @param fields: List of fields to query for
2755 ee3aedff Michael Hanselmann
  @type names: list of strings
2756 ee3aedff Michael Hanselmann
  @param names: Names of items to query for
2757 ee3aedff Michael Hanselmann
  @type unit: string or None
2758 ee3aedff Michael Hanselmann
  @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT} or
2759 ee3aedff Michael Hanselmann
    None for automatic choice (human-readable for non-separator usage,
2760 ee3aedff Michael Hanselmann
    otherwise megabytes); this is a one-letter string
2761 ee3aedff Michael Hanselmann
  @type separator: string or None
2762 ee3aedff Michael Hanselmann
  @param separator: String used to separate fields
2763 ee3aedff Michael Hanselmann
  @type header: bool
2764 ee3aedff Michael Hanselmann
  @param header: Whether to show header row
2765 2928de47 Michael Hanselmann
  @type force_filter: bool
2766 2928de47 Michael Hanselmann
  @param force_filter: Whether to always treat names as filter
2767 ee3aedff Michael Hanselmann
  @type format_override: dict
2768 ee3aedff Michael Hanselmann
  @param format_override: Dictionary for overriding field formatting functions,
2769 ee3aedff Michael Hanselmann
    indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY}
2770 f0b1bafe Iustin Pop
  @type verbose: boolean
2771 f0b1bafe Iustin Pop
  @param verbose: whether to use verbose field descriptions or not
2772 ee3aedff Michael Hanselmann

2773 ee3aedff Michael Hanselmann
  """
2774 ee3aedff Michael Hanselmann
  if not names:
2775 ee3aedff Michael Hanselmann
    names = None
2776 ee3aedff Michael Hanselmann
2777 2e5c33db Iustin Pop
  qfilter = qlang.MakeFilter(names, force_filter)
2778 2928de47 Michael Hanselmann
2779 727274dd Iustin Pop
  if cl is None:
2780 727274dd Iustin Pop
    cl = GetClient()
2781 727274dd Iustin Pop
2782 2e5c33db Iustin Pop
  response = cl.Query(resource, fields, qfilter)
2783 ee3aedff Michael Hanselmann
2784 ee3aedff Michael Hanselmann
  found_unknown = _WarnUnknownFields(response.fields)
2785 ee3aedff Michael Hanselmann
2786 ee3aedff Michael Hanselmann
  (status, data) = FormatQueryResult(response, unit=unit, separator=separator,
2787 ee3aedff Michael Hanselmann
                                     header=header,
2788 f0b1bafe Iustin Pop
                                     format_override=format_override,
2789 f0b1bafe Iustin Pop
                                     verbose=verbose)
2790 ee3aedff Michael Hanselmann
2791 ee3aedff Michael Hanselmann
  for line in data:
2792 ee3aedff Michael Hanselmann
    ToStdout(line)
2793 ee3aedff Michael Hanselmann
2794 ee3aedff Michael Hanselmann
  assert ((found_unknown and status == QR_UNKNOWN) or
2795 ee3aedff Michael Hanselmann
          (not found_unknown and status != QR_UNKNOWN))
2796 ee3aedff Michael Hanselmann
2797 ee3aedff Michael Hanselmann
  if status == QR_UNKNOWN:
2798 ee3aedff Michael Hanselmann
    return constants.EXIT_UNKNOWN_FIELD
2799 ee3aedff Michael Hanselmann
2800 ee3aedff Michael Hanselmann
  # TODO: Should the list command fail if not all data could be collected?
2801 ee3aedff Michael Hanselmann
  return constants.EXIT_SUCCESS
2802 ee3aedff Michael Hanselmann
2803 ee3aedff Michael Hanselmann
2804 ee3aedff Michael Hanselmann
def GenericListFields(resource, fields, separator, header, cl=None):
2805 ee3aedff Michael Hanselmann
  """Generic implementation for listing fields for a resource.
2806 ee3aedff Michael Hanselmann

2807 abd66bf8 Michael Hanselmann
  @param resource: One of L{constants.QR_VIA_LUXI}
2808 ee3aedff Michael Hanselmann
  @type fields: list of strings
2809 ee3aedff Michael Hanselmann
  @param fields: List of fields to query for
2810 ee3aedff Michael Hanselmann
  @type separator: string or None
2811 ee3aedff Michael Hanselmann
  @param separator: String used to separate fields
2812 ee3aedff Michael Hanselmann
  @type header: bool
2813 ee3aedff Michael Hanselmann
  @param header: Whether to show header row
2814 ee3aedff Michael Hanselmann

2815 ee3aedff Michael Hanselmann
  """
2816 ee3aedff Michael Hanselmann
  if cl is None:
2817 ee3aedff Michael Hanselmann
    cl = GetClient()
2818 ee3aedff Michael Hanselmann
2819 ee3aedff Michael Hanselmann
  if not fields:
2820 ee3aedff Michael Hanselmann
    fields = None
2821 ee3aedff Michael Hanselmann
2822 ee3aedff Michael Hanselmann
  response = cl.QueryFields(resource, fields)
2823 ee3aedff Michael Hanselmann
2824 ee3aedff Michael Hanselmann
  found_unknown = _WarnUnknownFields(response.fields)
2825 ee3aedff Michael Hanselmann
2826 ee3aedff Michael Hanselmann
  columns = [
2827 ee3aedff Michael Hanselmann
    TableColumn("Name", str, False),
2828 ee3aedff Michael Hanselmann
    TableColumn("Title", str, False),
2829 ea1440c1 Michael Hanselmann
    TableColumn("Description", str, False),
2830 ee3aedff Michael Hanselmann
    ]
2831 ee3aedff Michael Hanselmann
2832 ea1440c1 Michael Hanselmann
  rows = [[fdef.name, fdef.title, fdef.doc] for fdef in response.fields]
2833 ee3aedff Michael Hanselmann
2834 ee3aedff Michael Hanselmann
  for line in FormatTable(rows, columns, header, separator):
2835 ee3aedff Michael Hanselmann
    ToStdout(line)
2836 ee3aedff Michael Hanselmann
2837 ee3aedff Michael Hanselmann
  if found_unknown:
2838 ee3aedff Michael Hanselmann
    return constants.EXIT_UNKNOWN_FIELD
2839 ee3aedff Michael Hanselmann
2840 ee3aedff Michael Hanselmann
  return constants.EXIT_SUCCESS
2841 ee3aedff Michael Hanselmann
2842 ee3aedff Michael Hanselmann
2843 ee3aedff Michael Hanselmann
class TableColumn:
2844 ee3aedff Michael Hanselmann
  """Describes a column for L{FormatTable}.
2845 ee3aedff Michael Hanselmann

2846 ee3aedff Michael Hanselmann
  """
2847 ee3aedff Michael Hanselmann
  def __init__(self, title, fn, align_right):
2848 ee3aedff Michael Hanselmann
    """Initializes this class.
2849 ee3aedff Michael Hanselmann

2850 ee3aedff Michael Hanselmann
    @type title: string
2851 ee3aedff Michael Hanselmann
    @param title: Column title
2852 ee3aedff Michael Hanselmann
    @type fn: callable
2853 ee3aedff Michael Hanselmann
    @param fn: Formatting function
2854 ee3aedff Michael Hanselmann
    @type align_right: bool
2855 ee3aedff Michael Hanselmann
    @param align_right: Whether to align values on the right-hand side
2856 ee3aedff Michael Hanselmann

2857 ee3aedff Michael Hanselmann
    """
2858 ee3aedff Michael Hanselmann
    self.title = title
2859 ee3aedff Michael Hanselmann
    self.format = fn
2860 ee3aedff Michael Hanselmann
    self.align_right = align_right
2861 ee3aedff Michael Hanselmann
2862 ee3aedff Michael Hanselmann
2863 ee3aedff Michael Hanselmann
def _GetColFormatString(width, align_right):
2864 ee3aedff Michael Hanselmann
  """Returns the format string for a field.
2865 ee3aedff Michael Hanselmann

2866 ee3aedff Michael Hanselmann
  """
2867 ee3aedff Michael Hanselmann
  if align_right:
2868 ee3aedff Michael Hanselmann
    sign = ""
2869 ee3aedff Michael Hanselmann
  else:
2870 ee3aedff Michael Hanselmann
    sign = "-"
2871 ee3aedff Michael Hanselmann
2872 ee3aedff Michael Hanselmann
  return "%%%s%ss" % (sign, width)
2873 ee3aedff Michael Hanselmann
2874 ee3aedff Michael Hanselmann
2875 ee3aedff Michael Hanselmann
def FormatTable(rows, columns, header, separator):
2876 ee3aedff Michael Hanselmann
  """Formats data as a table.
2877 ee3aedff Michael Hanselmann

2878 ee3aedff Michael Hanselmann
  @type rows: list of lists
2879 ee3aedff Michael Hanselmann
  @param rows: Row data, one list per row
2880 ee3aedff Michael Hanselmann
  @type columns: list of L{TableColumn}
2881 ee3aedff Michael Hanselmann
  @param columns: Column descriptions
2882 ee3aedff Michael Hanselmann
  @type header: bool
2883 ee3aedff Michael Hanselmann
  @param header: Whether to show header row
2884 ee3aedff Michael Hanselmann
  @type separator: string or None
2885 ee3aedff Michael Hanselmann
  @param separator: String used to separate columns
2886 ee3aedff Michael Hanselmann

2887 ee3aedff Michael Hanselmann
  """
2888 ee3aedff Michael Hanselmann
  if header:
2889 ee3aedff Michael Hanselmann
    data = [[col.title for col in columns]]
2890 ee3aedff Michael Hanselmann
    colwidth = [len(col.title) for col in columns]
2891 ee3aedff Michael Hanselmann
  else:
2892 ee3aedff Michael Hanselmann
    data = []
2893 ee3aedff Michael Hanselmann
    colwidth = [0 for _ in columns]
2894 ee3aedff Michael Hanselmann
2895 ee3aedff Michael Hanselmann
  # Format row data
2896 ee3aedff Michael Hanselmann
  for row in rows:
2897 ee3aedff Michael Hanselmann
    assert len(row) == len(columns)
2898 ee3aedff Michael Hanselmann
2899 ee3aedff Michael Hanselmann
    formatted = [col.format(value) for value, col in zip(row, columns)]
2900 ee3aedff Michael Hanselmann
2901 ee3aedff Michael Hanselmann
    if separator is None:
2902 ee3aedff Michael Hanselmann
      # Update column widths
2903 ee3aedff Michael Hanselmann
      for idx, (oldwidth, value) in enumerate(zip(colwidth, formatted)):
2904 ee3aedff Michael Hanselmann
        # Modifying a list's items while iterating is fine
2905 ee3aedff Michael Hanselmann
        colwidth[idx] = max(oldwidth, len(value))
2906 ee3aedff Michael Hanselmann
2907 ee3aedff Michael Hanselmann
    data.append(formatted)
2908 ee3aedff Michael Hanselmann
2909 ee3aedff Michael Hanselmann
  if separator is not None:
2910 ee3aedff Michael Hanselmann
    # Return early if a separator is used
2911 ee3aedff Michael Hanselmann
    return [separator.join(row) for row in data]
2912 ee3aedff Michael Hanselmann
2913 ee3aedff Michael Hanselmann
  if columns and not columns[-1].align_right:
2914 ee3aedff Michael Hanselmann
    # Avoid unnecessary spaces at end of line
2915 ee3aedff Michael Hanselmann
    colwidth[-1] = 0
2916 ee3aedff Michael Hanselmann
2917 ee3aedff Michael Hanselmann
  # Build format string
2918 ee3aedff Michael Hanselmann
  fmt = " ".join([_GetColFormatString(width, col.align_right)
2919 ee3aedff Michael Hanselmann
                  for col, width in zip(columns, colwidth)])
2920 ee3aedff Michael Hanselmann
2921 ee3aedff Michael Hanselmann
  return [fmt % tuple(row) for row in data]
2922 ee3aedff Michael Hanselmann
2923 ee3aedff Michael Hanselmann
2924 3386e7a9 Iustin Pop
def FormatTimestamp(ts):
2925 3386e7a9 Iustin Pop
  """Formats a given timestamp.
2926 3386e7a9 Iustin Pop

2927 3386e7a9 Iustin Pop
  @type ts: timestamp
2928 3386e7a9 Iustin Pop
  @param ts: a timeval-type timestamp, a tuple of seconds and microseconds
2929 3386e7a9 Iustin Pop

2930 3386e7a9 Iustin Pop
  @rtype: string
2931 5fcc718f Iustin Pop
  @return: a string with the formatted timestamp
2932 3386e7a9 Iustin Pop

2933 3386e7a9 Iustin Pop
  """
2934 e687ec01 Michael Hanselmann
  if not isinstance(ts, (tuple, list)) or len(ts) != 2:
2935 d0c8c01d Iustin Pop
    return "?"
2936 3386e7a9 Iustin Pop
  sec, usec = ts
2937 3386e7a9 Iustin Pop
  return time.strftime("%F %T", time.localtime(sec)) + ".%06d" % usec
2938 2241e2b9 Iustin Pop
2939 2241e2b9 Iustin Pop
2940 2241e2b9 Iustin Pop
def ParseTimespec(value):
2941 2241e2b9 Iustin Pop
  """Parse a time specification.
2942 2241e2b9 Iustin Pop

2943 2241e2b9 Iustin Pop
  The following suffixed will be recognized:
2944 2241e2b9 Iustin Pop

2945 2241e2b9 Iustin Pop
    - s: seconds
2946 2241e2b9 Iustin Pop
    - m: minutes
2947 2241e2b9 Iustin Pop
    - h: hours
2948 2241e2b9 Iustin Pop
    - d: day
2949 2241e2b9 Iustin Pop
    - w: weeks
2950 2241e2b9 Iustin Pop

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

2953 2241e2b9 Iustin Pop
  """
2954 2241e2b9 Iustin Pop
  value = str(value)
2955 2241e2b9 Iustin Pop
  if not value:
2956 2241e2b9 Iustin Pop
    raise errors.OpPrereqError("Empty time specification passed")
2957 2241e2b9 Iustin Pop
  suffix_map = {
2958 d0c8c01d Iustin Pop
    "s": 1,
2959 d0c8c01d Iustin Pop
    "m": 60,
2960 d0c8c01d Iustin Pop
    "h": 3600,
2961 d0c8c01d Iustin Pop
    "d": 86400,
2962 d0c8c01d Iustin Pop
    "w": 604800,
2963 2241e2b9 Iustin Pop
    }
2964 2241e2b9 Iustin Pop
  if value[-1] not in suffix_map:
2965 2241e2b9 Iustin Pop
    try:
2966 2241e2b9 Iustin Pop
      value = int(value)
2967 691744c4 Iustin Pop
    except (TypeError, ValueError):
2968 2241e2b9 Iustin Pop
      raise errors.OpPrereqError("Invalid time specification '%s'" % value)
2969 2241e2b9 Iustin Pop
  else:
2970 2241e2b9 Iustin Pop
    multiplier = suffix_map[value[-1]]
2971 2241e2b9 Iustin Pop
    value = value[:-1]
2972 2241e2b9 Iustin Pop
    if not value: # no data left after stripping the suffix
2973 2241e2b9 Iustin Pop
      raise errors.OpPrereqError("Invalid time specification (only"
2974 2241e2b9 Iustin Pop
                                 " suffix passed)")
2975 2241e2b9 Iustin Pop
    try:
2976 2241e2b9 Iustin Pop
      value = int(value) * multiplier
2977 691744c4 Iustin Pop
    except (TypeError, ValueError):
2978 2241e2b9 Iustin Pop
      raise errors.OpPrereqError("Invalid time specification '%s'" % value)
2979 2241e2b9 Iustin Pop
  return value
2980 46fbdd04 Iustin Pop
2981 46fbdd04 Iustin Pop
2982 e9e26bb3 Iustin Pop
def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False,
2983 05484a24 Michael Hanselmann
                   filter_master=False, nodegroup=None):
2984 4040a784 Iustin Pop
  """Returns the names of online nodes.
2985 4040a784 Iustin Pop

2986 4040a784 Iustin Pop
  This function will also log a warning on stderr with the names of
2987 4040a784 Iustin Pop
  the online nodes.
2988 4040a784 Iustin Pop

2989 4040a784 Iustin Pop
  @param nodes: if not empty, use only this subset of nodes (minus the
2990 4040a784 Iustin Pop
      offline ones)
2991 4040a784 Iustin Pop
  @param cl: if not None, luxi client to use
2992 4040a784 Iustin Pop
  @type nowarn: boolean
2993 4040a784 Iustin Pop
  @param nowarn: by default, this function will output a note with the
2994 4040a784 Iustin Pop
      offline nodes that are skipped; if this parameter is True the
2995 4040a784 Iustin Pop
      note is not displayed
2996 e9e26bb3 Iustin Pop
  @type secondary_ips: boolean
2997 e9e26bb3 Iustin Pop
  @param secondary_ips: if True, return the secondary IPs instead of the
2998 e9e26bb3 Iustin Pop
      names, useful for doing network traffic over the replication interface
2999 e9e26bb3 Iustin Pop
      (if any)
3000 e9e26bb3 Iustin Pop
  @type filter_master: boolean
3001 e9e26bb3 Iustin Pop
  @param filter_master: if True, do not return the master node in the list
3002 e9e26bb3 Iustin Pop
      (useful in coordination with secondary_ips where we cannot check our
3003 e9e26bb3 Iustin Pop
      node name against the list)
3004 05484a24 Michael Hanselmann
  @type nodegroup: string
3005 05484a24 Michael Hanselmann
  @param nodegroup: If set, only return nodes in this node group
3006 4040a784 Iustin Pop

3007 4040a784 Iustin Pop
  """
3008 4040a784 Iustin Pop
  if cl is None:
3009 4040a784 Iustin Pop
    cl = GetClient()
3010 4040a784 Iustin Pop
3011 2e5c33db Iustin Pop
  qfilter = []
3012 05484a24 Michael Hanselmann
3013 05484a24 Michael Hanselmann
  if nodes:
3014 2e5c33db Iustin Pop
    qfilter.append(qlang.MakeSimpleFilter("name", nodes))
3015 05484a24 Michael Hanselmann
3016 05484a24 Michael Hanselmann
  if nodegroup is not None:
3017 2e5c33db Iustin Pop
    qfilter.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup],
3018 05484a24 Michael Hanselmann
                                 [qlang.OP_EQUAL, "group.uuid", nodegroup]])
3019 e9e26bb3 Iustin Pop
3020 e9e26bb3 Iustin Pop
  if filter_master:
3021 2e5c33db Iustin Pop
    qfilter.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
3022 05484a24 Michael Hanselmann
3023 2e5c33db Iustin Pop
  if qfilter:
3024 2e5c33db Iustin Pop
    if len(qfilter) > 1:
3025 2e5c33db Iustin Pop
      final_filter = [qlang.OP_AND] + qfilter
3026 05484a24 Michael Hanselmann
    else:
3027 2e5c33db Iustin Pop
      assert len(qfilter) == 1
3028 2e5c33db Iustin Pop
      final_filter = qfilter[0]
3029 e9e26bb3 Iustin Pop
  else:
3030 05484a24 Michael Hanselmann
    final_filter = None
3031 05484a24 Michael Hanselmann
3032 05484a24 Michael Hanselmann
  result = cl.Query(constants.QR_NODE, ["name", "offline", "sip"], final_filter)
3033 05484a24 Michael Hanselmann
3034 05484a24 Michael Hanselmann
  def _IsOffline(row):
3035 05484a24 Michael Hanselmann
    (_, (_, offline), _) = row
3036 05484a24 Michael Hanselmann
    return offline
3037 05484a24 Michael Hanselmann
3038 05484a24 Michael Hanselmann
  def _GetName(row):
3039 05484a24 Michael Hanselmann
    ((_, name), _, _) = row
3040 05484a24 Michael Hanselmann
    return name
3041 05484a24 Michael Hanselmann
3042 05484a24 Michael Hanselmann
  def _GetSip(row):
3043 05484a24 Michael Hanselmann
    (_, _, (_, sip)) = row
3044 05484a24 Michael Hanselmann
    return sip
3045 05484a24 Michael Hanselmann
3046 05484a24 Michael Hanselmann
  (offline, online) = compat.partition(result.data, _IsOffline)
3047 e9e26bb3 Iustin Pop
3048 4040a784 Iustin Pop
  if offline and not nowarn:
3049 05484a24 Michael Hanselmann
    ToStderr("Note: skipping offline node(s): %s" %
3050 05484a24 Michael Hanselmann
             utils.CommaJoin(map(_GetName, offline)))
3051 05484a24 Michael Hanselmann
3052 05484a24 Michael Hanselmann
  if secondary_ips:
3053 05484a24 Michael Hanselmann
    fn = _GetSip
3054 05484a24 Michael Hanselmann
  else:
3055 05484a24 Michael Hanselmann
    fn = _GetName
3056 05484a24 Michael Hanselmann
3057 05484a24 Michael Hanselmann
  return map(fn, online)
3058 4040a784 Iustin Pop
3059 4040a784 Iustin Pop
3060 46fbdd04 Iustin Pop
def _ToStream(stream, txt, *args):
3061 46fbdd04 Iustin Pop
  """Write a message to a stream, bypassing the logging system
3062 46fbdd04 Iustin Pop

3063 46fbdd04 Iustin Pop
  @type stream: file object
3064 46fbdd04 Iustin Pop
  @param stream: the file to which we should write
3065 46fbdd04 Iustin Pop
  @type txt: str
3066 46fbdd04 Iustin Pop
  @param txt: the message
3067 46fbdd04 Iustin Pop

3068 46fbdd04 Iustin Pop
  """
3069 225e2544 Iustin Pop
  try:
3070 225e2544 Iustin Pop
    if args:
3071 225e2544 Iustin Pop
      args = tuple(args)
3072 225e2544 Iustin Pop
      stream.write(txt % args)
3073 225e2544 Iustin Pop
    else:
3074 225e2544 Iustin Pop
      stream.write(txt)
3075 d0c8c01d Iustin Pop
    stream.write("\n")
3076 225e2544 Iustin Pop
    stream.flush()
3077 225e2544 Iustin Pop
  except IOError, err:
3078 225e2544 Iustin Pop
    if err.errno == errno.EPIPE:
3079 225e2544 Iustin Pop
      # our terminal went away, we'll exit
3080 225e2544 Iustin Pop
      sys.exit(constants.EXIT_FAILURE)
3081 225e2544 Iustin Pop
    else:
3082 225e2544 Iustin Pop
      raise
3083 46fbdd04 Iustin Pop
3084 46fbdd04 Iustin Pop
3085 46fbdd04 Iustin Pop
def ToStdout(txt, *args):
3086 46fbdd04 Iustin Pop
  """Write a message to stdout only, bypassing the logging system
3087 46fbdd04 Iustin Pop

3088 46fbdd04 Iustin Pop
  This is just a wrapper over _ToStream.
3089 46fbdd04 Iustin Pop

3090 46fbdd04 Iustin Pop
  @type txt: str
3091 46fbdd04 Iustin Pop
  @param txt: the message
3092 46fbdd04 Iustin Pop

3093 46fbdd04 Iustin Pop
  """
3094 46fbdd04 Iustin Pop
  _ToStream(sys.stdout, txt, *args)
3095 46fbdd04 Iustin Pop
3096 46fbdd04 Iustin Pop
3097 46fbdd04 Iustin Pop
def ToStderr(txt, *args):
3098 46fbdd04 Iustin Pop
  """Write a message to stderr only, bypassing the logging system
3099 46fbdd04 Iustin Pop

3100 46fbdd04 Iustin Pop
  This is just a wrapper over _ToStream.
3101 46fbdd04 Iustin Pop

3102 46fbdd04 Iustin Pop
  @type txt: str
3103 46fbdd04 Iustin Pop
  @param txt: the message
3104 46fbdd04 Iustin Pop

3105 46fbdd04 Iustin Pop
  """
3106 46fbdd04 Iustin Pop
  _ToStream(sys.stderr, txt, *args)
3107 479636a3 Iustin Pop
3108 479636a3 Iustin Pop
3109 479636a3 Iustin Pop
class JobExecutor(object):
3110 479636a3 Iustin Pop
  """Class which manages the submission and execution of multiple jobs.
3111 479636a3 Iustin Pop

3112 479636a3 Iustin Pop
  Note that instances of this class should not be reused between
3113 479636a3 Iustin Pop
  GetResults() calls.
3114 479636a3 Iustin Pop

3115 479636a3 Iustin Pop
  """
3116 919ca415 Iustin Pop
  def __init__(self, cl=None, verbose=True, opts=None, feedback_fn=None):
3117 479636a3 Iustin Pop
    self.queue = []
3118 479636a3 Iustin Pop
    if cl is None:
3119 479636a3 Iustin Pop
      cl = GetClient()
3120 479636a3 Iustin Pop
    self.cl = cl
3121 479636a3 Iustin Pop
    self.verbose = verbose
3122 23b4b983 Iustin Pop
    self.jobs = []
3123 cff5fa7f Iustin Pop
    self.opts = opts
3124 919ca415 Iustin Pop
    self.feedback_fn = feedback_fn
3125 60452edf Michael Hanselmann
    self._counter = itertools.count()
3126 479636a3 Iustin Pop
3127 8d99a8bf Michael Hanselmann
  @staticmethod
3128 8d99a8bf Michael Hanselmann
  def _IfName(name, fmt):
3129 8d99a8bf Michael Hanselmann
    """Helper function for formatting name.
3130 8d99a8bf Michael Hanselmann

3131 8d99a8bf Michael Hanselmann
    """
3132 8d99a8bf Michael Hanselmann
    if name:
3133 8d99a8bf Michael Hanselmann
      return fmt % name
3134 8d99a8bf Michael Hanselmann
3135 8d99a8bf Michael Hanselmann
    return ""
3136 8d99a8bf Michael Hanselmann
3137 479636a3 Iustin Pop
  def QueueJob(self, name, *ops):
3138 23b4b983 Iustin Pop
    """Record a job for later submit.
3139 479636a3 Iustin Pop

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

3143 479636a3 Iustin Pop
    """
3144 cff5fa7f Iustin Pop
    SetGenericOpcodeOpts(ops, self.opts)
3145 60452edf Michael Hanselmann
    self.queue.append((self._counter.next(), name, ops))
3146 23b4b983 Iustin Pop
3147 8d99a8bf Michael Hanselmann
  def AddJobId(self, name, status, job_id):
3148 8d99a8bf Michael Hanselmann
    """Adds a job ID to the internal queue.
3149 8d99a8bf Michael Hanselmann

3150 8d99a8bf Michael Hanselmann
    """
3151 8d99a8bf Michael Hanselmann
    self.jobs.append((self._counter.next(), status, job_id, name))
3152 8d99a8bf Michael Hanselmann
3153 66ecc479 Guido Trotter
  def SubmitPending(self, each=False):
3154 23b4b983 Iustin Pop
    """Submit all pending jobs.
3155 23b4b983 Iustin Pop

3156 23b4b983 Iustin Pop
    """
3157 66ecc479 Guido Trotter
    if each:
3158 66ecc479 Guido Trotter
      results = []
3159 60452edf Michael Hanselmann
      for (_, _, ops) in self.queue:
3160 66ecc479 Guido Trotter
        # SubmitJob will remove the success status, but raise an exception if
3161 66ecc479 Guido Trotter
        # the submission fails, so we'll notice that anyway.
3162 519fafa1 Andrea Spadaccini
        results.append([True, self.cl.SubmitJob(ops)[0]])
3163 66ecc479 Guido Trotter
    else:
3164 60452edf Michael Hanselmann
      results = self.cl.SubmitManyJobs([ops for (_, _, ops) in self.queue])
3165 60452edf Michael Hanselmann
    for ((status, data), (idx, name, _)) in zip(results, self.queue):
3166 5299e61f Iustin Pop
      self.jobs.append((idx, status, data, name))
3167 5299e61f Iustin Pop
3168 5299e61f Iustin Pop
  def _ChooseJob(self):
3169 5299e61f Iustin Pop
    """Choose a non-waiting/queued job to poll next.
3170 5299e61f Iustin Pop

3171 5299e61f Iustin Pop
    """
3172 5299e61f Iustin Pop
    assert self.jobs, "_ChooseJob called with empty job list"
3173 5299e61f Iustin Pop
3174 11705e3d Iustin Pop
    result = self.cl.QueryJobs([i[2] for i in self.jobs[:_CHOOSE_BATCH]],
3175 11705e3d Iustin Pop
                               ["status"])
3176 5299e61f Iustin Pop
    assert result
3177 5299e61f Iustin Pop
3178 5299e61f Iustin Pop
    for job_data, status in zip(self.jobs, result):
3179 91c622a8 Iustin Pop
      if (isinstance(status, list) and status and
3180 91c622a8 Iustin Pop
          status[0] in (constants.JOB_STATUS_QUEUED,
3181 47099cd1 Michael Hanselmann
                        constants.JOB_STATUS_WAITING,
3182 91c622a8 Iustin Pop
                        constants.JOB_STATUS_CANCELING)):
3183 91c622a8 Iustin Pop
        # job is still present and waiting
3184 5299e61f Iustin Pop
        continue
3185 91c622a8 Iustin Pop
      # good candidate found (either running job or lost job)
3186 5299e61f Iustin Pop
      self.jobs.remove(job_data)
3187 5299e61f Iustin Pop
      return job_data
3188 5299e61f Iustin Pop
3189 5299e61f Iustin Pop
    # no job found
3190 5299e61f Iustin Pop
    return self.jobs.pop(0)
3191 479636a3 Iustin Pop
3192 479636a3 Iustin Pop
  def GetResults(self):
3193 479636a3 Iustin Pop
    """Wait for and return the results of all jobs.
3194 479636a3 Iustin Pop

3195 479636a3 Iustin Pop
    @rtype: list
3196 479636a3 Iustin Pop
    @return: list of tuples (success, job results), in the same order
3197 479636a3 Iustin Pop
        as the submitted jobs; if a job has failed, instead of the result
3198 479636a3 Iustin Pop
        there will be the error message
3199 479636a3 Iustin Pop

3200 479636a3 Iustin Pop
    """
3201 23b4b983 Iustin Pop
    if not self.jobs:
3202 23b4b983 Iustin Pop
      self.SubmitPending()
3203 479636a3 Iustin Pop
    results = []
3204 479636a3 Iustin Pop
    if self.verbose:
3205 5299e61f Iustin Pop
      ok_jobs = [row[2] for row in self.jobs if row[1]]
3206 23b4b983 Iustin Pop
      if ok_jobs:
3207 4474f112 Iustin Pop
        ToStdout("Submitted jobs %s", utils.CommaJoin(ok_jobs))
3208 5299e61f Iustin Pop
3209 5299e61f Iustin Pop
    # first, remove any non-submitted jobs
3210 cea881e5 Michael Hanselmann
    self.jobs, failures = compat.partition(self.jobs, lambda x: x[1])
3211 5299e61f Iustin Pop
    for idx, _, jid, name in failures:
3212 4474f112 Iustin Pop
      ToStderr("Failed to submit job%s: %s", self._IfName(name, " for %s"), jid)
3213 c63355f2 Iustin Pop
      results.append((idx, False, jid))
3214 5299e61f Iustin Pop
3215 5299e61f Iustin Pop
    while self.jobs:
3216 5299e61f Iustin Pop
      (idx, _, jid, name) = self._ChooseJob()
3217 4474f112 Iustin Pop
      ToStdout("Waiting for job %s%s ...", jid, self._IfName(name, " for %s"))
3218 479636a3 Iustin Pop
      try:
3219 919ca415 Iustin Pop
        job_result = PollJob(jid, cl=self.cl, feedback_fn=self.feedback_fn)
3220 479636a3 Iustin Pop
        success = True
3221 91c622a8 Iustin Pop
      except errors.JobLost, err:
3222 91c622a8 Iustin Pop
        _, job_result = FormatError(err)
3223 4474f112 Iustin Pop
        ToStderr("Job %s%s has been archived, cannot check its result",
3224 4474f112 Iustin Pop
                 jid, self._IfName(name, " for %s"))
3225 91c622a8 Iustin Pop
        success = False
3226 479636a3 Iustin Pop
      except (errors.GenericError, luxi.ProtocolError), err:
3227 479636a3 Iustin Pop
        _, job_result = FormatError(err)
3228 479636a3 Iustin Pop
        success = False
3229 479636a3 Iustin Pop
        # the error message will always be shown, verbose or not
3230 4474f112 Iustin Pop
        ToStderr("Job %s%s has failed: %s",
3231 4474f112 Iustin Pop
                 jid, self._IfName(name, " for %s"), job_result)
3232 479636a3 Iustin Pop
3233 5299e61f Iustin Pop
      results.append((idx, success, job_result))
3234 5299e61f Iustin Pop
3235 5299e61f Iustin Pop
    # sort based on the index, then drop it
3236 5299e61f Iustin Pop
    results.sort()
3237 5299e61f Iustin Pop
    results = [i[1:] for i in results]
3238 5299e61f Iustin Pop
3239 479636a3 Iustin Pop
    return results
3240 479636a3 Iustin Pop
3241 479636a3 Iustin Pop
  def WaitOrShow(self, wait):
3242 479636a3 Iustin Pop
    """Wait for job results or only print the job IDs.
3243 479636a3 Iustin Pop

3244 479636a3 Iustin Pop
    @type wait: boolean
3245 479636a3 Iustin Pop
    @param wait: whether to wait or not
3246 479636a3 Iustin Pop

3247 479636a3 Iustin Pop
    """
3248 479636a3 Iustin Pop
    if wait:
3249 479636a3 Iustin Pop
      return self.GetResults()
3250 479636a3 Iustin Pop
    else:
3251 23b4b983 Iustin Pop
      if not self.jobs:
3252 23b4b983 Iustin Pop
        self.SubmitPending()
3253 71834b2a Guido Trotter
      for _, status, result, name in self.jobs:
3254 23b4b983 Iustin Pop
        if status:
3255 4474f112 Iustin Pop
          ToStdout("%s: %s", result, name)
3256 23b4b983 Iustin Pop
        else:
3257 4474f112 Iustin Pop
          ToStderr("Failure for %s: %s", name, result)
3258 53a8a54d Iustin Pop
      return [row[1:3] for row in self.jobs]
3259 acd19189 René Nussbaumer
3260 acd19189 René Nussbaumer
3261 acd19189 René Nussbaumer
def FormatParameterDict(buf, param_dict, actual, level=1):
3262 acd19189 René Nussbaumer
  """Formats a parameter dictionary.
3263 acd19189 René Nussbaumer

3264 acd19189 René Nussbaumer
  @type buf: L{StringIO}
3265 acd19189 René Nussbaumer
  @param buf: the buffer into which to write
3266 acd19189 René Nussbaumer
  @type param_dict: dict
3267 acd19189 René Nussbaumer
  @param param_dict: the own parameters
3268 acd19189 René Nussbaumer
  @type actual: dict
3269 acd19189 René Nussbaumer
  @param actual: the current parameter set (including defaults)
3270 acd19189 René Nussbaumer
  @param level: Level of indent
3271 acd19189 René Nussbaumer

3272 acd19189 René Nussbaumer
  """
3273 acd19189 René Nussbaumer
  indent = "  " * level
3274 acd19189 René Nussbaumer
  for key in sorted(actual):
3275 acd19189 René Nussbaumer
    val = param_dict.get(key, "default (%s)" % actual[key])
3276 acd19189 René Nussbaumer
    buf.write("%s- %s: %s\n" % (indent, key, val))
3277 25bd815c René Nussbaumer
3278 25bd815c René Nussbaumer
3279 25bd815c René Nussbaumer
def ConfirmOperation(names, list_type, text, extra=""):
3280 25bd815c René Nussbaumer
  """Ask the user to confirm an operation on a list of list_type.
3281 25bd815c René Nussbaumer

3282 25bd815c René Nussbaumer
  This function is used to request confirmation for doing an operation
3283 25bd815c René Nussbaumer
  on a given list of list_type.
3284 25bd815c René Nussbaumer

3285 25bd815c René Nussbaumer
  @type names: list
3286 25bd815c René Nussbaumer
  @param names: the list of names that we display when
3287 25bd815c René Nussbaumer
      we ask for confirmation
3288 25bd815c René Nussbaumer
  @type list_type: str
3289 25bd815c René Nussbaumer
  @param list_type: Human readable name for elements in the list (e.g. nodes)
3290 25bd815c René Nussbaumer
  @type text: str
3291 25bd815c René Nussbaumer
  @param text: the operation that the user should confirm
3292 25bd815c René Nussbaumer
  @rtype: boolean
3293 25bd815c René Nussbaumer
  @return: True or False depending on user's confirmation.
3294 25bd815c René Nussbaumer

3295 25bd815c René Nussbaumer
  """
3296 25bd815c René Nussbaumer
  count = len(names)
3297 25bd815c René Nussbaumer
  msg = ("The %s will operate on %d %s.\n%s"
3298 25bd815c René Nussbaumer
         "Do you want to continue?" % (text, count, list_type, extra))
3299 25bd815c René Nussbaumer
  affected = (("\nAffected %s:\n" % list_type) +
3300 25bd815c René Nussbaumer
              "\n".join(["  %s" % name for name in names]))
3301 25bd815c René Nussbaumer
3302 25bd815c René Nussbaumer
  choices = [("y", True, "Yes, execute the %s" % text),
3303 25bd815c René Nussbaumer
             ("n", False, "No, abort the %s" % text)]
3304 25bd815c René Nussbaumer
3305 25bd815c René Nussbaumer
  if count > 20:
3306 25bd815c René Nussbaumer
    choices.insert(1, ("v", "v", "View the list of affected %s" % list_type))
3307 25bd815c René Nussbaumer
    question = msg
3308 25bd815c René Nussbaumer
  else:
3309 25bd815c René Nussbaumer
    question = msg + affected
3310 25bd815c René Nussbaumer
3311 25bd815c René Nussbaumer
  choice = AskUser(question, choices)
3312 25bd815c René Nussbaumer
  if choice == "v":
3313 25bd815c René Nussbaumer
    choices.pop(1)
3314 25bd815c René Nussbaumer
    choice = AskUser(msg + affected, choices)
3315 25bd815c René Nussbaumer
  return choice