Statistics
| Branch: | Tag: | Revision:

root / lib / cli.py @ adb6d685

History | View | Annotate | Download (75.5 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 a8083063 Iustin Pop
# Copyright (C) 2006, 2007 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 73702ee7 Iustin Pop
from cStringIO import StringIO
31 a8083063 Iustin Pop
32 a8083063 Iustin Pop
from ganeti import utils
33 a8083063 Iustin Pop
from ganeti import errors
34 a8083063 Iustin Pop
from ganeti import constants
35 846baef9 Iustin Pop
from ganeti import opcodes
36 ceab32dd Iustin Pop
from ganeti import luxi
37 b33e986b Iustin Pop
from ganeti import ssconf
38 4331f6cd Michael Hanselmann
from ganeti import rpc
39 7e49b6ce Michael Hanselmann
from ganeti import ssh
40 cea881e5 Michael Hanselmann
from ganeti import compat
41 a8083063 Iustin Pop
42 c38c44ad Michael Hanselmann
from optparse import (OptionParser, TitledHelpFormatter,
43 38206f3c Iustin Pop
                      Option, OptionValueError)
44 a8083063 Iustin Pop
45 03298ebe Michael Hanselmann
46 4abc4f1e Iustin Pop
__all__ = [
47 4abc4f1e Iustin Pop
  # Command line options
48 fdad8c4d Balazs Lecz
  "ADD_UIDS_OPT",
49 e7e09483 Iustin Pop
  "ALLOCATABLE_OPT",
50 2d5e7ae1 Iustin Pop
  "ALL_OPT",
51 4c61d894 Iustin Pop
  "AUTO_PROMOTE_OPT",
52 e00f7a05 Iustin Pop
  "AUTO_REPLACE_OPT",
53 087ed2ed Iustin Pop
  "BACKEND_OPT",
54 baef337d Iustin Pop
  "CLEANUP_OPT",
55 3db3eb2a Michael Hanselmann
  "CLUSTER_DOMAIN_SECRET_OPT",
56 4abc4f1e Iustin Pop
  "CONFIRM_OPT",
57 e32df528 Iustin Pop
  "CP_SIZE_OPT",
58 4abc4f1e Iustin Pop
  "DEBUG_OPT",
59 a0c9776a Iustin Pop
  "DEBUG_SIMERR_OPT",
60 4b038a1e Iustin Pop
  "DISKIDX_OPT",
61 e3876ccb Iustin Pop
  "DISK_OPT",
62 4b038a1e Iustin Pop
  "DISK_TEMPLATE_OPT",
63 771734c9 Iustin Pop
  "DRAINED_OPT",
64 7ea7bcf6 Iustin Pop
  "EARLY_RELEASE_OPT",
65 383a3591 Iustin Pop
  "ENABLED_HV_OPT",
66 14e9e7f3 Iustin Pop
  "ERROR_CODES_OPT",
67 4abc4f1e Iustin Pop
  "FIELDS_OPT",
68 4a25828c Iustin Pop
  "FILESTORE_DIR_OPT",
69 0f87c43e Iustin Pop
  "FILESTORE_DRIVER_OPT",
70 06073e85 Guido Trotter
  "FORCE_OPT",
71 06073e85 Guido Trotter
  "FORCE_VARIANT_OPT",
72 29392516 Iustin Pop
  "GLOBAL_FILEDIR_OPT",
73 073271f6 Iustin Pop
  "HVLIST_OPT",
74 48f212d7 Iustin Pop
  "HVOPTS_OPT",
75 236fd9c4 Iustin Pop
  "HYPERVISOR_OPT",
76 4eb62659 Iustin Pop
  "IALLOCATOR_OPT",
77 e588764d Iustin Pop
  "IDENTIFY_DEFAULTS_OPT",
78 82a786d5 Iustin Pop
  "IGNORE_CONSIST_OPT",
79 b6e841a8 Iustin Pop
  "IGNORE_FAILURES_OPT",
80 8d8d650c Michael Hanselmann
  "IGNORE_REMOVE_FAILURES_OPT",
81 ee3f9578 Iustin Pop
  "IGNORE_SECONDARIES_OPT",
82 05586c90 Iustin Pop
  "IGNORE_SIZE_OPT",
83 e3646f22 Iustin Pop
  "MAC_PREFIX_OPT",
84 3953242f Iustin Pop
  "MAINTAIN_NODE_HEALTH_OPT",
85 29392516 Iustin Pop
  "MASTER_NETDEV_OPT",
86 771734c9 Iustin Pop
  "MC_OPT",
87 7d3a9fab Iustin Pop
  "NET_OPT",
88 6d4a1656 Michael Hanselmann
  "NEW_CLUSTER_CERT_OPT",
89 3db3eb2a Michael Hanselmann
  "NEW_CLUSTER_DOMAIN_SECRET_OPT",
90 6b7d5878 Michael Hanselmann
  "NEW_CONFD_HMAC_KEY_OPT",
91 6d4a1656 Michael Hanselmann
  "NEW_RAPI_CERT_OPT",
92 a14db5ff Iustin Pop
  "NEW_SECONDARY_OPT",
93 4fbc93dd Iustin Pop
  "NIC_PARAMS_OPT",
94 7edc4637 Iustin Pop
  "NODE_LIST_OPT",
95 990b7886 Iustin Pop
  "NODE_PLACEMENT_OPT",
96 4abc4f1e Iustin Pop
  "NOHDR_OPT",
97 91e0748c Iustin Pop
  "NOIPCHECK_OPT",
98 25a8792c Iustin Pop
  "NO_INSTALL_OPT",
99 460d22be Iustin Pop
  "NONAMECHECK_OPT",
100 831040bf Iustin Pop
  "NOLVM_STORAGE_OPT",
101 29392516 Iustin Pop
  "NOMODIFY_ETCHOSTS_OPT",
102 b989b9d9 Ken Wehr
  "NOMODIFY_SSH_SETUP_OPT",
103 26023ecd Iustin Pop
  "NONICS_OPT",
104 f2a0828c Iustin Pop
  "NONLIVE_OPT",
105 14e9e7f3 Iustin Pop
  "NONPLUS1_OPT",
106 44c44832 Iustin Pop
  "NOSHUTDOWN_OPT",
107 edeb878a Iustin Pop
  "NOSTART_OPT",
108 fcdde7f2 Iustin Pop
  "NOSSH_KEYCHECK_OPT",
109 58371861 Iustin Pop
  "NOVOTING_OPT",
110 3f75b4f3 Iustin Pop
  "NWSYNC_OPT",
111 a72d0a79 Iustin Pop
  "ON_PRIMARY_OPT",
112 feb09e6a Iustin Pop
  "ON_SECONDARY_OPT",
113 771734c9 Iustin Pop
  "OFFLINE_OPT",
114 062a7100 Iustin Pop
  "OSPARAMS_OPT",
115 d3ed23ff Iustin Pop
  "OS_OPT",
116 ff00c1a7 Iustin Pop
  "OS_SIZE_OPT",
117 6d4a1656 Michael Hanselmann
  "RAPI_CERT_OPT",
118 b8d0f938 Iustin Pop
  "READD_OPT",
119 12054861 Iustin Pop
  "REBOOT_TYPE_OPT",
120 8d8d650c Michael Hanselmann
  "REMOVE_INSTANCE_OPT",
121 fdad8c4d Balazs Lecz
  "REMOVE_UIDS_OPT",
122 31d97b2a Guido Trotter
  "ROMAN_OPT",
123 8d823629 Iustin Pop
  "SECONDARY_IP_OPT",
124 67840b40 Iustin Pop
  "SELECT_OS_OPT",
125 4abc4f1e Iustin Pop
  "SEP_OPT",
126 fdcf4d84 Iustin Pop
  "SHOWCMD_OPT",
127 7e5eaaa8 Guido Trotter
  "SHUTDOWN_TIMEOUT_OPT",
128 f36d7d81 Iustin Pop
  "SINGLE_NODE_OPT",
129 df62e5db Iustin Pop
  "SRC_DIR_OPT",
130 df62e5db Iustin Pop
  "SRC_NODE_OPT",
131 4abc4f1e Iustin Pop
  "SUBMIT_OPT",
132 99a8c799 Iustin Pop
  "STATIC_OPT",
133 4abc4f1e Iustin Pop
  "SYNC_OPT",
134 4abc4f1e Iustin Pop
  "TAG_SRC_OPT",
135 b5762e2a Guido Trotter
  "TIMEOUT_OPT",
136 1338f2b4 Balazs Lecz
  "UIDPOOL_OPT",
137 4abc4f1e Iustin Pop
  "USEUNITS_OPT",
138 74adc100 Iustin Pop
  "USE_REPL_NET_OPT",
139 9cdb9578 Iustin Pop
  "VERBOSE_OPT",
140 b58726e8 Iustin Pop
  "VG_NAME_OPT",
141 1f587d3d Iustin Pop
  "YES_DOIT_OPT",
142 4abc4f1e Iustin Pop
  # Generic functions for CLI programs
143 4abc4f1e Iustin Pop
  "GenericMain",
144 d77490c5 Iustin Pop
  "GenericInstanceCreate",
145 4abc4f1e Iustin Pop
  "GetClient",
146 4abc4f1e Iustin Pop
  "GetOnlineNodes",
147 4abc4f1e Iustin Pop
  "JobExecutor",
148 4abc4f1e Iustin Pop
  "JobSubmittedException",
149 4abc4f1e Iustin Pop
  "ParseTimespec",
150 7e49b6ce Michael Hanselmann
  "RunWhileClusterStopped",
151 4abc4f1e Iustin Pop
  "SubmitOpCode",
152 4abc4f1e Iustin Pop
  "SubmitOrSend",
153 4abc4f1e Iustin Pop
  "UsesRPC",
154 4abc4f1e Iustin Pop
  # Formatting functions
155 4abc4f1e Iustin Pop
  "ToStderr", "ToStdout",
156 4abc4f1e Iustin Pop
  "FormatError",
157 4abc4f1e Iustin Pop
  "GenerateTable",
158 4abc4f1e Iustin Pop
  "AskUser",
159 4abc4f1e Iustin Pop
  "FormatTimestamp",
160 4abc4f1e Iustin Pop
  # Tags functions
161 4abc4f1e Iustin Pop
  "ListTags",
162 4abc4f1e Iustin Pop
  "AddTags",
163 4abc4f1e Iustin Pop
  "RemoveTags",
164 4abc4f1e Iustin Pop
  # command line options support infrastructure
165 4abc4f1e Iustin Pop
  "ARGS_MANY_INSTANCES",
166 4abc4f1e Iustin Pop
  "ARGS_MANY_NODES",
167 4abc4f1e Iustin Pop
  "ARGS_NONE",
168 4abc4f1e Iustin Pop
  "ARGS_ONE_INSTANCE",
169 4abc4f1e Iustin Pop
  "ARGS_ONE_NODE",
170 f9faf9c3 René Nussbaumer
  "ARGS_ONE_OS",
171 4abc4f1e Iustin Pop
  "ArgChoice",
172 4abc4f1e Iustin Pop
  "ArgCommand",
173 4abc4f1e Iustin Pop
  "ArgFile",
174 4abc4f1e Iustin Pop
  "ArgHost",
175 4abc4f1e Iustin Pop
  "ArgInstance",
176 4abc4f1e Iustin Pop
  "ArgJobId",
177 4abc4f1e Iustin Pop
  "ArgNode",
178 f9faf9c3 René Nussbaumer
  "ArgOs",
179 4abc4f1e Iustin Pop
  "ArgSuggest",
180 4abc4f1e Iustin Pop
  "ArgUnknown",
181 4abc4f1e Iustin Pop
  "OPT_COMPL_INST_ADD_NODES",
182 4abc4f1e Iustin Pop
  "OPT_COMPL_MANY_NODES",
183 4abc4f1e Iustin Pop
  "OPT_COMPL_ONE_IALLOCATOR",
184 4abc4f1e Iustin Pop
  "OPT_COMPL_ONE_INSTANCE",
185 4abc4f1e Iustin Pop
  "OPT_COMPL_ONE_NODE",
186 4abc4f1e Iustin Pop
  "OPT_COMPL_ONE_OS",
187 4abc4f1e Iustin Pop
  "cli_option",
188 4abc4f1e Iustin Pop
  "SplitNodeOption",
189 07150497 Guido Trotter
  "CalculateOSNames",
190 4abc4f1e Iustin Pop
  ]
191 846baef9 Iustin Pop
192 8b46606c Guido Trotter
NO_PREFIX = "no_"
193 8b46606c Guido Trotter
UN_PREFIX = "-"
194 846baef9 Iustin Pop
195 03298ebe Michael Hanselmann
196 863d7f46 Michael Hanselmann
class _Argument:
197 7260cfbe Iustin Pop
  def __init__(self, min=0, max=None): # pylint: disable-msg=W0622
198 863d7f46 Michael Hanselmann
    self.min = min
199 863d7f46 Michael Hanselmann
    self.max = max
200 863d7f46 Michael Hanselmann
201 863d7f46 Michael Hanselmann
  def __repr__(self):
202 863d7f46 Michael Hanselmann
    return ("<%s min=%s max=%s>" %
203 863d7f46 Michael Hanselmann
            (self.__class__.__name__, self.min, self.max))
204 863d7f46 Michael Hanselmann
205 863d7f46 Michael Hanselmann
206 863d7f46 Michael Hanselmann
class ArgSuggest(_Argument):
207 863d7f46 Michael Hanselmann
  """Suggesting argument.
208 863d7f46 Michael Hanselmann

209 863d7f46 Michael Hanselmann
  Value can be any of the ones passed to the constructor.
210 863d7f46 Michael Hanselmann

211 863d7f46 Michael Hanselmann
  """
212 7260cfbe Iustin Pop
  # pylint: disable-msg=W0622
213 863d7f46 Michael Hanselmann
  def __init__(self, min=0, max=None, choices=None):
214 863d7f46 Michael Hanselmann
    _Argument.__init__(self, min=min, max=max)
215 863d7f46 Michael Hanselmann
    self.choices = choices
216 863d7f46 Michael Hanselmann
217 863d7f46 Michael Hanselmann
  def __repr__(self):
218 863d7f46 Michael Hanselmann
    return ("<%s min=%s max=%s choices=%r>" %
219 863d7f46 Michael Hanselmann
            (self.__class__.__name__, self.min, self.max, self.choices))
220 863d7f46 Michael Hanselmann
221 863d7f46 Michael Hanselmann
222 863d7f46 Michael Hanselmann
class ArgChoice(ArgSuggest):
223 863d7f46 Michael Hanselmann
  """Choice argument.
224 863d7f46 Michael Hanselmann

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

228 863d7f46 Michael Hanselmann
  """
229 863d7f46 Michael Hanselmann
230 863d7f46 Michael Hanselmann
231 863d7f46 Michael Hanselmann
class ArgUnknown(_Argument):
232 863d7f46 Michael Hanselmann
  """Unknown argument to program (e.g. determined at runtime).
233 863d7f46 Michael Hanselmann

234 863d7f46 Michael Hanselmann
  """
235 863d7f46 Michael Hanselmann
236 863d7f46 Michael Hanselmann
237 863d7f46 Michael Hanselmann
class ArgInstance(_Argument):
238 863d7f46 Michael Hanselmann
  """Instances argument.
239 863d7f46 Michael Hanselmann

240 863d7f46 Michael Hanselmann
  """
241 863d7f46 Michael Hanselmann
242 863d7f46 Michael Hanselmann
243 863d7f46 Michael Hanselmann
class ArgNode(_Argument):
244 863d7f46 Michael Hanselmann
  """Node argument.
245 863d7f46 Michael Hanselmann

246 863d7f46 Michael Hanselmann
  """
247 863d7f46 Michael Hanselmann
248 863d7f46 Michael Hanselmann
class ArgJobId(_Argument):
249 863d7f46 Michael Hanselmann
  """Job ID argument.
250 863d7f46 Michael Hanselmann

251 863d7f46 Michael Hanselmann
  """
252 863d7f46 Michael Hanselmann
253 863d7f46 Michael Hanselmann
254 863d7f46 Michael Hanselmann
class ArgFile(_Argument):
255 863d7f46 Michael Hanselmann
  """File path argument.
256 863d7f46 Michael Hanselmann

257 863d7f46 Michael Hanselmann
  """
258 863d7f46 Michael Hanselmann
259 863d7f46 Michael Hanselmann
260 863d7f46 Michael Hanselmann
class ArgCommand(_Argument):
261 863d7f46 Michael Hanselmann
  """Command argument.
262 863d7f46 Michael Hanselmann

263 863d7f46 Michael Hanselmann
  """
264 863d7f46 Michael Hanselmann
265 863d7f46 Michael Hanselmann
266 83ec7961 Michael Hanselmann
class ArgHost(_Argument):
267 83ec7961 Michael Hanselmann
  """Host argument.
268 83ec7961 Michael Hanselmann

269 83ec7961 Michael Hanselmann
  """
270 83ec7961 Michael Hanselmann
271 83ec7961 Michael Hanselmann
272 f9faf9c3 René Nussbaumer
class ArgOs(_Argument):
273 f9faf9c3 René Nussbaumer
  """OS argument.
274 f9faf9c3 René Nussbaumer

275 f9faf9c3 René Nussbaumer
  """
276 f9faf9c3 René Nussbaumer
277 f9faf9c3 René Nussbaumer
278 4a265c08 Michael Hanselmann
ARGS_NONE = []
279 4a265c08 Michael Hanselmann
ARGS_MANY_INSTANCES = [ArgInstance()]
280 4a265c08 Michael Hanselmann
ARGS_MANY_NODES = [ArgNode()]
281 4a265c08 Michael Hanselmann
ARGS_ONE_INSTANCE = [ArgInstance(min=1, max=1)]
282 4a265c08 Michael Hanselmann
ARGS_ONE_NODE = [ArgNode(min=1, max=1)]
283 f9faf9c3 René Nussbaumer
ARGS_ONE_OS = [ArgOs(min=1, max=1)]
284 4a265c08 Michael Hanselmann
285 4a265c08 Michael Hanselmann
286 846baef9 Iustin Pop
def _ExtractTagsObject(opts, args):
287 846baef9 Iustin Pop
  """Extract the tag type object.
288 846baef9 Iustin Pop

289 846baef9 Iustin Pop
  Note that this function will modify its args parameter.
290 846baef9 Iustin Pop

291 846baef9 Iustin Pop
  """
292 846baef9 Iustin Pop
  if not hasattr(opts, "tag_type"):
293 846baef9 Iustin Pop
    raise errors.ProgrammerError("tag_type not passed to _ExtractTagsObject")
294 846baef9 Iustin Pop
  kind = opts.tag_type
295 846baef9 Iustin Pop
  if kind == constants.TAG_CLUSTER:
296 846baef9 Iustin Pop
    retval = kind, kind
297 846baef9 Iustin Pop
  elif kind == constants.TAG_NODE or kind == constants.TAG_INSTANCE:
298 846baef9 Iustin Pop
    if not args:
299 0c434948 Iustin Pop
      raise errors.OpPrereqError("no arguments passed to the command")
300 846baef9 Iustin Pop
    name = args.pop(0)
301 846baef9 Iustin Pop
    retval = kind, name
302 846baef9 Iustin Pop
  else:
303 846baef9 Iustin Pop
    raise errors.ProgrammerError("Unhandled tag type '%s'" % kind)
304 846baef9 Iustin Pop
  return retval
305 846baef9 Iustin Pop
306 846baef9 Iustin Pop
307 810c50b7 Iustin Pop
def _ExtendTags(opts, args):
308 810c50b7 Iustin Pop
  """Extend the args if a source file has been given.
309 810c50b7 Iustin Pop

310 810c50b7 Iustin Pop
  This function will extend the tags with the contents of the file
311 810c50b7 Iustin Pop
  passed in the 'tags_source' attribute of the opts parameter. A file
312 810c50b7 Iustin Pop
  named '-' will be replaced by stdin.
313 810c50b7 Iustin Pop

314 810c50b7 Iustin Pop
  """
315 810c50b7 Iustin Pop
  fname = opts.tags_source
316 810c50b7 Iustin Pop
  if fname is None:
317 810c50b7 Iustin Pop
    return
318 810c50b7 Iustin Pop
  if fname == "-":
319 810c50b7 Iustin Pop
    new_fh = sys.stdin
320 810c50b7 Iustin Pop
  else:
321 810c50b7 Iustin Pop
    new_fh = open(fname, "r")
322 810c50b7 Iustin Pop
  new_data = []
323 810c50b7 Iustin Pop
  try:
324 810c50b7 Iustin Pop
    # we don't use the nice 'new_data = [line.strip() for line in fh]'
325 810c50b7 Iustin Pop
    # because of python bug 1633941
326 810c50b7 Iustin Pop
    while True:
327 810c50b7 Iustin Pop
      line = new_fh.readline()
328 810c50b7 Iustin Pop
      if not line:
329 810c50b7 Iustin Pop
        break
330 810c50b7 Iustin Pop
      new_data.append(line.strip())
331 810c50b7 Iustin Pop
  finally:
332 810c50b7 Iustin Pop
    new_fh.close()
333 810c50b7 Iustin Pop
  args.extend(new_data)
334 810c50b7 Iustin Pop
335 810c50b7 Iustin Pop
336 846baef9 Iustin Pop
def ListTags(opts, args):
337 846baef9 Iustin Pop
  """List the tags on a given object.
338 846baef9 Iustin Pop

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

344 846baef9 Iustin Pop
  """
345 846baef9 Iustin Pop
  kind, name = _ExtractTagsObject(opts, args)
346 7699c3af Iustin Pop
  cl = GetClient()
347 7699c3af Iustin Pop
  result = cl.QueryTags(kind, name)
348 846baef9 Iustin Pop
  result = list(result)
349 846baef9 Iustin Pop
  result.sort()
350 846baef9 Iustin Pop
  for tag in result:
351 03298ebe Michael Hanselmann
    ToStdout(tag)
352 846baef9 Iustin Pop
353 846baef9 Iustin Pop
354 846baef9 Iustin Pop
def AddTags(opts, args):
355 846baef9 Iustin Pop
  """Add tags on a given object.
356 846baef9 Iustin Pop

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

362 846baef9 Iustin Pop
  """
363 846baef9 Iustin Pop
  kind, name = _ExtractTagsObject(opts, args)
364 810c50b7 Iustin Pop
  _ExtendTags(opts, args)
365 846baef9 Iustin Pop
  if not args:
366 846baef9 Iustin Pop
    raise errors.OpPrereqError("No tags to be added")
367 846baef9 Iustin Pop
  op = opcodes.OpAddTags(kind=kind, name=name, tags=args)
368 846baef9 Iustin Pop
  SubmitOpCode(op)
369 846baef9 Iustin Pop
370 846baef9 Iustin Pop
371 846baef9 Iustin Pop
def RemoveTags(opts, args):
372 846baef9 Iustin Pop
  """Remove tags from a given object.
373 846baef9 Iustin Pop

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

379 846baef9 Iustin Pop
  """
380 846baef9 Iustin Pop
  kind, name = _ExtractTagsObject(opts, args)
381 810c50b7 Iustin Pop
  _ExtendTags(opts, args)
382 846baef9 Iustin Pop
  if not args:
383 846baef9 Iustin Pop
    raise errors.OpPrereqError("No tags to be removed")
384 846baef9 Iustin Pop
  op = opcodes.OpDelTags(kind=kind, name=name, tags=args)
385 846baef9 Iustin Pop
  SubmitOpCode(op)
386 846baef9 Iustin Pop
387 a8083063 Iustin Pop
388 8929d28c Iustin Pop
def check_unit(option, opt, value): # pylint: disable-msg=W0613
389 65fe4693 Iustin Pop
  """OptParsers custom converter for units.
390 65fe4693 Iustin Pop

391 65fe4693 Iustin Pop
  """
392 a8083063 Iustin Pop
  try:
393 a8083063 Iustin Pop
    return utils.ParseUnit(value)
394 a8083063 Iustin Pop
  except errors.UnitParseError, err:
395 3ecf6786 Iustin Pop
    raise OptionValueError("option %s: %s" % (opt, err))
396 a8083063 Iustin Pop
397 a8083063 Iustin Pop
398 a8469393 Iustin Pop
def _SplitKeyVal(opt, data):
399 a8469393 Iustin Pop
  """Convert a KeyVal string into a dict.
400 a8469393 Iustin Pop

401 a8469393 Iustin Pop
  This function will convert a key=val[,...] string into a dict. Empty
402 a8469393 Iustin Pop
  values will be converted specially: keys which have the prefix 'no_'
403 a8469393 Iustin Pop
  will have the value=False and the prefix stripped, the others will
404 a8469393 Iustin Pop
  have value=True.
405 a8469393 Iustin Pop

406 a8469393 Iustin Pop
  @type opt: string
407 a8469393 Iustin Pop
  @param opt: a string holding the option name for which we process the
408 a8469393 Iustin Pop
      data, used in building error messages
409 a8469393 Iustin Pop
  @type data: string
410 a8469393 Iustin Pop
  @param data: a string of the format key=val,key=val,...
411 a8469393 Iustin Pop
  @rtype: dict
412 a8469393 Iustin Pop
  @return: {key=val, key=val}
413 a8469393 Iustin Pop
  @raises errors.ParameterError: if there are duplicate keys
414 a8469393 Iustin Pop

415 a8469393 Iustin Pop
  """
416 a8469393 Iustin Pop
  kv_dict = {}
417 4f31882e Guido Trotter
  if data:
418 1b3a7656 Iustin Pop
    for elem in utils.UnescapeAndSplit(data, sep=","):
419 4f31882e Guido Trotter
      if "=" in elem:
420 4f31882e Guido Trotter
        key, val = elem.split("=", 1)
421 a8469393 Iustin Pop
      else:
422 4f31882e Guido Trotter
        if elem.startswith(NO_PREFIX):
423 4f31882e Guido Trotter
          key, val = elem[len(NO_PREFIX):], False
424 4f31882e Guido Trotter
        elif elem.startswith(UN_PREFIX):
425 4f31882e Guido Trotter
          key, val = elem[len(UN_PREFIX):], None
426 4f31882e Guido Trotter
        else:
427 4f31882e Guido Trotter
          key, val = elem, True
428 4f31882e Guido Trotter
      if key in kv_dict:
429 4f31882e Guido Trotter
        raise errors.ParameterError("Duplicate key '%s' in option %s" %
430 4f31882e Guido Trotter
                                    (key, opt))
431 4f31882e Guido Trotter
      kv_dict[key] = val
432 a8469393 Iustin Pop
  return kv_dict
433 a8469393 Iustin Pop
434 a8469393 Iustin Pop
435 8929d28c Iustin Pop
def check_ident_key_val(option, opt, value):  # pylint: disable-msg=W0613
436 552c8dff Michael Hanselmann
  """Custom parser for ident:key=val,key=val options.
437 552c8dff Michael Hanselmann

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

441 a8469393 Iustin Pop
  """
442 a8469393 Iustin Pop
  if ":" not in value:
443 8b46606c Guido Trotter
    ident, rest = value, ''
444 a8469393 Iustin Pop
  else:
445 a8469393 Iustin Pop
    ident, rest = value.split(":", 1)
446 8b46606c Guido Trotter
447 8b46606c Guido Trotter
  if ident.startswith(NO_PREFIX):
448 8b46606c Guido Trotter
    if rest:
449 8b46606c Guido Trotter
      msg = "Cannot pass options when removing parameter groups: %s" % value
450 8b46606c Guido Trotter
      raise errors.ParameterError(msg)
451 8b46606c Guido Trotter
    retval = (ident[len(NO_PREFIX):], False)
452 8b46606c Guido Trotter
  elif ident.startswith(UN_PREFIX):
453 8b46606c Guido Trotter
    if rest:
454 8b46606c Guido Trotter
      msg = "Cannot pass options when removing parameter groups: %s" % value
455 8b46606c Guido Trotter
      raise errors.ParameterError(msg)
456 8b46606c Guido Trotter
    retval = (ident[len(UN_PREFIX):], None)
457 8b46606c Guido Trotter
  else:
458 a8469393 Iustin Pop
    kv_dict = _SplitKeyVal(opt, rest)
459 a8469393 Iustin Pop
    retval = (ident, kv_dict)
460 a8469393 Iustin Pop
  return retval
461 a8469393 Iustin Pop
462 a8469393 Iustin Pop
463 8929d28c Iustin Pop
def check_key_val(option, opt, value):  # pylint: disable-msg=W0613
464 552c8dff Michael Hanselmann
  """Custom parser class for key=val,key=val options.
465 552c8dff Michael Hanselmann

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

468 a8469393 Iustin Pop
  """
469 a8469393 Iustin Pop
  return _SplitKeyVal(opt, value)
470 a8469393 Iustin Pop
471 a8469393 Iustin Pop
472 e7b61bb0 Iustin Pop
def check_bool(option, opt, value): # pylint: disable-msg=W0613
473 e7b61bb0 Iustin Pop
  """Custom parser for yes/no options.
474 e7b61bb0 Iustin Pop

475 e7b61bb0 Iustin Pop
  This will store the parsed value as either True or False.
476 e7b61bb0 Iustin Pop

477 e7b61bb0 Iustin Pop
  """
478 e7b61bb0 Iustin Pop
  value = value.lower()
479 e7b61bb0 Iustin Pop
  if value == constants.VALUE_FALSE or value == "no":
480 e7b61bb0 Iustin Pop
    return False
481 e7b61bb0 Iustin Pop
  elif value == constants.VALUE_TRUE or value == "yes":
482 e7b61bb0 Iustin Pop
    return True
483 e7b61bb0 Iustin Pop
  else:
484 e7b61bb0 Iustin Pop
    raise errors.ParameterError("Invalid boolean value '%s'" % value)
485 e7b61bb0 Iustin Pop
486 e7b61bb0 Iustin Pop
487 63d44c55 Michael Hanselmann
# completion_suggestion is normally a list. Using numeric values not evaluating
488 63d44c55 Michael Hanselmann
# to False for dynamic completion.
489 63d44c55 Michael Hanselmann
(OPT_COMPL_MANY_NODES,
490 63d44c55 Michael Hanselmann
 OPT_COMPL_ONE_NODE,
491 63d44c55 Michael Hanselmann
 OPT_COMPL_ONE_INSTANCE,
492 63d44c55 Michael Hanselmann
 OPT_COMPL_ONE_OS,
493 2d3ed64b Michael Hanselmann
 OPT_COMPL_ONE_IALLOCATOR,
494 2d3ed64b Michael Hanselmann
 OPT_COMPL_INST_ADD_NODES) = range(100, 106)
495 63d44c55 Michael Hanselmann
496 63d44c55 Michael Hanselmann
OPT_COMPL_ALL = frozenset([
497 63d44c55 Michael Hanselmann
  OPT_COMPL_MANY_NODES,
498 63d44c55 Michael Hanselmann
  OPT_COMPL_ONE_NODE,
499 63d44c55 Michael Hanselmann
  OPT_COMPL_ONE_INSTANCE,
500 63d44c55 Michael Hanselmann
  OPT_COMPL_ONE_OS,
501 63d44c55 Michael Hanselmann
  OPT_COMPL_ONE_IALLOCATOR,
502 2d3ed64b Michael Hanselmann
  OPT_COMPL_INST_ADD_NODES,
503 63d44c55 Michael Hanselmann
  ])
504 63d44c55 Michael Hanselmann
505 63d44c55 Michael Hanselmann
506 552c8dff Michael Hanselmann
class CliOption(Option):
507 552c8dff Michael Hanselmann
  """Custom option class for optparse.
508 a8469393 Iustin Pop

509 a8469393 Iustin Pop
  """
510 863d7f46 Michael Hanselmann
  ATTRS = Option.ATTRS + [
511 863d7f46 Michael Hanselmann
    "completion_suggest",
512 863d7f46 Michael Hanselmann
    ]
513 552c8dff Michael Hanselmann
  TYPES = Option.TYPES + (
514 552c8dff Michael Hanselmann
    "identkeyval",
515 552c8dff Michael Hanselmann
    "keyval",
516 552c8dff Michael Hanselmann
    "unit",
517 e7b61bb0 Iustin Pop
    "bool",
518 552c8dff Michael Hanselmann
    )
519 552c8dff Michael Hanselmann
  TYPE_CHECKER = Option.TYPE_CHECKER.copy()
520 552c8dff Michael Hanselmann
  TYPE_CHECKER["identkeyval"] = check_ident_key_val
521 a8469393 Iustin Pop
  TYPE_CHECKER["keyval"] = check_key_val
522 552c8dff Michael Hanselmann
  TYPE_CHECKER["unit"] = check_unit
523 e7b61bb0 Iustin Pop
  TYPE_CHECKER["bool"] = check_bool
524 a8469393 Iustin Pop
525 a8469393 Iustin Pop
526 a8083063 Iustin Pop
# optparse.py sets make_option, so we do it for our own option class, too
527 a8083063 Iustin Pop
cli_option = CliOption
528 a8083063 Iustin Pop
529 a8083063 Iustin Pop
530 771734c9 Iustin Pop
_YORNO = "yes|no"
531 771734c9 Iustin Pop
532 ea34193f Iustin Pop
DEBUG_OPT = cli_option("-d", "--debug", default=0, action="count",
533 ea34193f Iustin Pop
                       help="Increase debugging level")
534 c38c44ad Michael Hanselmann
535 c38c44ad Michael Hanselmann
NOHDR_OPT = cli_option("--no-headers", default=False,
536 c38c44ad Michael Hanselmann
                       action="store_true", dest="no_headers",
537 c38c44ad Michael Hanselmann
                       help="Don't display column headers")
538 c38c44ad Michael Hanselmann
539 c38c44ad Michael Hanselmann
SEP_OPT = cli_option("--separator", default=None,
540 c38c44ad Michael Hanselmann
                     action="store", dest="separator",
541 c38c44ad Michael Hanselmann
                     help=("Separator between output fields"
542 c38c44ad Michael Hanselmann
                           " (defaults to one space)"))
543 c38c44ad Michael Hanselmann
544 c38c44ad Michael Hanselmann
USEUNITS_OPT = cli_option("--units", default=None,
545 c38c44ad Michael Hanselmann
                          dest="units", choices=('h', 'm', 'g', 't'),
546 c38c44ad Michael Hanselmann
                          help="Specify units for output (one of hmgt)")
547 c38c44ad Michael Hanselmann
548 c38c44ad Michael Hanselmann
FIELDS_OPT = cli_option("-o", "--output", dest="output", action="store",
549 c38c44ad Michael Hanselmann
                        type="string", metavar="FIELDS",
550 c38c44ad Michael Hanselmann
                        help="Comma separated list of output fields")
551 c38c44ad Michael Hanselmann
552 c38c44ad Michael Hanselmann
FORCE_OPT = cli_option("-f", "--force", dest="force", action="store_true",
553 c38c44ad Michael Hanselmann
                       default=False, help="Force the operation")
554 c38c44ad Michael Hanselmann
555 c38c44ad Michael Hanselmann
CONFIRM_OPT = cli_option("--yes", dest="confirm", action="store_true",
556 c38c44ad Michael Hanselmann
                         default=False, help="Do not require confirmation")
557 c38c44ad Michael Hanselmann
558 c38c44ad Michael Hanselmann
TAG_SRC_OPT = cli_option("--from", dest="tags_source",
559 c38c44ad Michael Hanselmann
                         default=None, help="File with tag names")
560 c38c44ad Michael Hanselmann
561 c38c44ad Michael Hanselmann
SUBMIT_OPT = cli_option("--submit", dest="submit_only",
562 c38c44ad Michael Hanselmann
                        default=False, action="store_true",
563 c38c44ad Michael Hanselmann
                        help=("Submit the job and return the job ID, but"
564 c38c44ad Michael Hanselmann
                              " don't wait for the job to finish"))
565 c38c44ad Michael Hanselmann
566 c38c44ad Michael Hanselmann
SYNC_OPT = cli_option("--sync", dest="do_locking",
567 c38c44ad Michael Hanselmann
                      default=False, action="store_true",
568 c38c44ad Michael Hanselmann
                      help=("Grab locks while doing the queries"
569 c38c44ad Michael Hanselmann
                            " in order to ensure more consistent results"))
570 c38c44ad Michael Hanselmann
571 c38c44ad Michael Hanselmann
_DRY_RUN_OPT = cli_option("--dry-run", default=False,
572 c38c44ad Michael Hanselmann
                          action="store_true",
573 c38c44ad Michael Hanselmann
                          help=("Do not execute the operation, just run the"
574 c38c44ad Michael Hanselmann
                                " check steps and verify it it could be"
575 c38c44ad Michael Hanselmann
                                " executed"))
576 c38c44ad Michael Hanselmann
577 9cdb9578 Iustin Pop
VERBOSE_OPT = cli_option("-v", "--verbose", default=False,
578 9cdb9578 Iustin Pop
                         action="store_true",
579 9cdb9578 Iustin Pop
                         help="Increase the verbosity of the operation")
580 9cdb9578 Iustin Pop
581 a0c9776a Iustin Pop
DEBUG_SIMERR_OPT = cli_option("--debug-simulate-errors", default=False,
582 a0c9776a Iustin Pop
                              action="store_true", dest="simulate_errors",
583 a0c9776a Iustin Pop
                              help="Debugging option that makes the operation"
584 a0c9776a Iustin Pop
                              " treat most runtime checks as failed")
585 a0c9776a Iustin Pop
586 3f75b4f3 Iustin Pop
NWSYNC_OPT = cli_option("--no-wait-for-sync", dest="wait_for_sync",
587 3f75b4f3 Iustin Pop
                        default=True, action="store_false",
588 3f75b4f3 Iustin Pop
                        help="Don't wait for sync (DANGEROUS!)")
589 3f75b4f3 Iustin Pop
590 4f365444 Iustin Pop
DISK_TEMPLATE_OPT = cli_option("-t", "--disk-template", dest="disk_template",
591 4f365444 Iustin Pop
                               help="Custom disk setup (diskless, file,"
592 4f365444 Iustin Pop
                               " plain or drbd)",
593 4f365444 Iustin Pop
                               default=None, metavar="TEMPL",
594 4f365444 Iustin Pop
                               choices=list(constants.DISK_TEMPLATES))
595 4f365444 Iustin Pop
596 26023ecd Iustin Pop
NONICS_OPT = cli_option("--no-nics", default=False, action="store_true",
597 26023ecd Iustin Pop
                        help="Do not create any network cards for"
598 26023ecd Iustin Pop
                        " the instance")
599 26023ecd Iustin Pop
600 4a25828c Iustin Pop
FILESTORE_DIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
601 4a25828c Iustin Pop
                               help="Relative path under default cluster-wide"
602 4a25828c Iustin Pop
                               " file storage dir to store file-based disks",
603 4a25828c Iustin Pop
                               default=None, metavar="<DIR>")
604 4a25828c Iustin Pop
605 0f87c43e Iustin Pop
FILESTORE_DRIVER_OPT = cli_option("--file-driver", dest="file_driver",
606 0f87c43e Iustin Pop
                                  help="Driver to use for image files",
607 0f87c43e Iustin Pop
                                  default="loop", metavar="<DRIVER>",
608 0f87c43e Iustin Pop
                                  choices=list(constants.FILE_DRIVER))
609 0f87c43e Iustin Pop
610 4eb62659 Iustin Pop
IALLOCATOR_OPT = cli_option("-I", "--iallocator", metavar="<NAME>",
611 4eb62659 Iustin Pop
                            help="Select nodes for the instance automatically"
612 4eb62659 Iustin Pop
                            " using the <NAME> iallocator plugin",
613 4eb62659 Iustin Pop
                            default=None, type="string",
614 4eb62659 Iustin Pop
                            completion_suggest=OPT_COMPL_ONE_IALLOCATOR)
615 4eb62659 Iustin Pop
616 d3ed23ff Iustin Pop
OS_OPT = cli_option("-o", "--os-type", dest="os", help="What OS to run",
617 d3ed23ff Iustin Pop
                    metavar="<os>",
618 d3ed23ff Iustin Pop
                    completion_suggest=OPT_COMPL_ONE_OS)
619 d3ed23ff Iustin Pop
620 062a7100 Iustin Pop
OSPARAMS_OPT = cli_option("-O", "--os-parameters", dest="osparams",
621 062a7100 Iustin Pop
                         type="keyval", default={},
622 062a7100 Iustin Pop
                         help="OS parameters")
623 062a7100 Iustin Pop
624 06073e85 Guido Trotter
FORCE_VARIANT_OPT = cli_option("--force-variant", dest="force_variant",
625 06073e85 Guido Trotter
                               action="store_true", default=False,
626 06073e85 Guido Trotter
                               help="Force an unknown variant")
627 06073e85 Guido Trotter
628 25a8792c Iustin Pop
NO_INSTALL_OPT = cli_option("--no-install", dest="no_install",
629 25a8792c Iustin Pop
                            action="store_true", default=False,
630 25a8792c Iustin Pop
                            help="Do not install the OS (will"
631 25a8792c Iustin Pop
                            " enable no-start)")
632 25a8792c Iustin Pop
633 087ed2ed Iustin Pop
BACKEND_OPT = cli_option("-B", "--backend-parameters", dest="beparams",
634 087ed2ed Iustin Pop
                         type="keyval", default={},
635 087ed2ed Iustin Pop
                         help="Backend parameters")
636 48f212d7 Iustin Pop
637 48f212d7 Iustin Pop
HVOPTS_OPT =  cli_option("-H", "--hypervisor-parameters", type="keyval",
638 48f212d7 Iustin Pop
                         default={}, dest="hvparams",
639 48f212d7 Iustin Pop
                         help="Hypervisor parameters")
640 087ed2ed Iustin Pop
641 236fd9c4 Iustin Pop
HYPERVISOR_OPT = cli_option("-H", "--hypervisor-parameters", dest="hypervisor",
642 236fd9c4 Iustin Pop
                            help="Hypervisor and hypervisor options, in the"
643 236fd9c4 Iustin Pop
                            " format hypervisor:option=value,option=value,...",
644 236fd9c4 Iustin Pop
                            default=None, type="identkeyval")
645 073271f6 Iustin Pop
646 073271f6 Iustin Pop
HVLIST_OPT = cli_option("-H", "--hypervisor-parameters", dest="hvparams",
647 073271f6 Iustin Pop
                        help="Hypervisor and hypervisor options, in the"
648 073271f6 Iustin Pop
                        " format hypervisor:option=value,option=value,...",
649 073271f6 Iustin Pop
                        default=[], action="append", type="identkeyval")
650 236fd9c4 Iustin Pop
651 91e0748c Iustin Pop
NOIPCHECK_OPT = cli_option("--no-ip-check", dest="ip_check", default=True,
652 91e0748c Iustin Pop
                           action="store_false",
653 91e0748c Iustin Pop
                           help="Don't check that the instance's IP"
654 91e0748c Iustin Pop
                           " is alive")
655 91e0748c Iustin Pop
656 460d22be Iustin Pop
NONAMECHECK_OPT = cli_option("--no-name-check", dest="name_check",
657 460d22be Iustin Pop
                             default=True, action="store_false",
658 460d22be Iustin Pop
                             help="Don't check that the instance's name"
659 460d22be Iustin Pop
                             " is resolvable")
660 460d22be Iustin Pop
661 7d3a9fab Iustin Pop
NET_OPT = cli_option("--net",
662 7d3a9fab Iustin Pop
                     help="NIC parameters", default=[],
663 7d3a9fab Iustin Pop
                     dest="nics", action="append", type="identkeyval")
664 e3876ccb Iustin Pop
665 e3876ccb Iustin Pop
DISK_OPT = cli_option("--disk", help="Disk parameters", default=[],
666 e3876ccb Iustin Pop
                      dest="disks", action="append", type="identkeyval")
667 91e0748c Iustin Pop
668 4b038a1e Iustin Pop
DISKIDX_OPT = cli_option("--disks", dest="disks", default=None,
669 4b038a1e Iustin Pop
                         help="Comma-separated list of disks"
670 4b038a1e Iustin Pop
                         " indices to act on (e.g. 0,2) (optional,"
671 4b038a1e Iustin Pop
                         " defaults to all disks)")
672 4b038a1e Iustin Pop
673 ff00c1a7 Iustin Pop
OS_SIZE_OPT = cli_option("-s", "--os-size", dest="sd_size",
674 ff00c1a7 Iustin Pop
                         help="Enforces a single-disk configuration using the"
675 ff00c1a7 Iustin Pop
                         " given disk size, in MiB unless a suffix is used",
676 ff00c1a7 Iustin Pop
                         default=None, type="unit", metavar="<size>")
677 ff00c1a7 Iustin Pop
678 82a786d5 Iustin Pop
IGNORE_CONSIST_OPT = cli_option("--ignore-consistency",
679 82a786d5 Iustin Pop
                                dest="ignore_consistency",
680 82a786d5 Iustin Pop
                                action="store_true", default=False,
681 82a786d5 Iustin Pop
                                help="Ignore the consistency of the disks on"
682 82a786d5 Iustin Pop
                                " the secondary")
683 82a786d5 Iustin Pop
684 f2a0828c Iustin Pop
NONLIVE_OPT = cli_option("--non-live", dest="live",
685 f2a0828c Iustin Pop
                         default=True, action="store_false",
686 f2a0828c Iustin Pop
                         help="Do a non-live migration (this usually means"
687 f2a0828c Iustin Pop
                         " freeze the instance, save the state, transfer and"
688 f2a0828c Iustin Pop
                         " only then resume running on the secondary node)")
689 f2a0828c Iustin Pop
690 990b7886 Iustin Pop
NODE_PLACEMENT_OPT = cli_option("-n", "--node", dest="node",
691 990b7886 Iustin Pop
                                help="Target node and optional secondary node",
692 990b7886 Iustin Pop
                                metavar="<pnode>[:<snode>]",
693 990b7886 Iustin Pop
                                completion_suggest=OPT_COMPL_INST_ADD_NODES)
694 990b7886 Iustin Pop
695 7edc4637 Iustin Pop
NODE_LIST_OPT = cli_option("-n", "--node", dest="nodes", default=[],
696 7edc4637 Iustin Pop
                           action="append", metavar="<node>",
697 7edc4637 Iustin Pop
                           help="Use only this node (can be used multiple"
698 7edc4637 Iustin Pop
                           " times, if not given defaults to all nodes)",
699 7edc4637 Iustin Pop
                           completion_suggest=OPT_COMPL_ONE_NODE)
700 f36d7d81 Iustin Pop
701 f36d7d81 Iustin Pop
SINGLE_NODE_OPT = cli_option("-n", "--node", dest="node", help="Target node",
702 f36d7d81 Iustin Pop
                             metavar="<node>",
703 f36d7d81 Iustin Pop
                             completion_suggest=OPT_COMPL_ONE_NODE)
704 7edc4637 Iustin Pop
705 edeb878a Iustin Pop
NOSTART_OPT = cli_option("--no-start", dest="start", default=True,
706 edeb878a Iustin Pop
                         action="store_false",
707 edeb878a Iustin Pop
                         help="Don't start the instance after creation")
708 edeb878a Iustin Pop
709 fdcf4d84 Iustin Pop
SHOWCMD_OPT = cli_option("--show-cmd", dest="show_command",
710 fdcf4d84 Iustin Pop
                         action="store_true", default=False,
711 fdcf4d84 Iustin Pop
                         help="Show command instead of executing it")
712 fdcf4d84 Iustin Pop
713 baef337d Iustin Pop
CLEANUP_OPT = cli_option("--cleanup", dest="cleanup",
714 baef337d Iustin Pop
                         default=False, action="store_true",
715 baef337d Iustin Pop
                         help="Instead of performing the migration, try to"
716 baef337d Iustin Pop
                         " recover from a failed cleanup. This is safe"
717 baef337d Iustin Pop
                         " to run even if the instance is healthy, but it"
718 baef337d Iustin Pop
                         " will create extra replication traffic and "
719 baef337d Iustin Pop
                         " disrupt briefly the replication (like during the"
720 baef337d Iustin Pop
                         " migration")
721 baef337d Iustin Pop
722 99a8c799 Iustin Pop
STATIC_OPT = cli_option("-s", "--static", dest="static",
723 99a8c799 Iustin Pop
                        action="store_true", default=False,
724 99a8c799 Iustin Pop
                        help="Only show configuration data, not runtime data")
725 99a8c799 Iustin Pop
726 2d5e7ae1 Iustin Pop
ALL_OPT = cli_option("--all", dest="show_all",
727 2d5e7ae1 Iustin Pop
                     default=False, action="store_true",
728 2d5e7ae1 Iustin Pop
                     help="Show info on all instances on the cluster."
729 2d5e7ae1 Iustin Pop
                     " This can take a long time to run, use wisely")
730 2d5e7ae1 Iustin Pop
731 67840b40 Iustin Pop
SELECT_OS_OPT = cli_option("--select-os", dest="select_os",
732 67840b40 Iustin Pop
                           action="store_true", default=False,
733 67840b40 Iustin Pop
                           help="Interactive OS reinstall, lists available"
734 67840b40 Iustin Pop
                           " OS templates for selection")
735 2d5e7ae1 Iustin Pop
736 b6e841a8 Iustin Pop
IGNORE_FAILURES_OPT = cli_option("--ignore-failures", dest="ignore_failures",
737 b6e841a8 Iustin Pop
                                 action="store_true", default=False,
738 b6e841a8 Iustin Pop
                                 help="Remove the instance from the cluster"
739 b6e841a8 Iustin Pop
                                 " configuration even if there are failures"
740 b6e841a8 Iustin Pop
                                 " during the removal process")
741 b6e841a8 Iustin Pop
742 8d8d650c Michael Hanselmann
IGNORE_REMOVE_FAILURES_OPT = cli_option("--ignore-remove-failures",
743 8d8d650c Michael Hanselmann
                                        dest="ignore_remove_failures",
744 8d8d650c Michael Hanselmann
                                        action="store_true", default=False,
745 8d8d650c Michael Hanselmann
                                        help="Remove the instance from the"
746 8d8d650c Michael Hanselmann
                                        " cluster configuration even if there"
747 8d8d650c Michael Hanselmann
                                        " are failures during the removal"
748 8d8d650c Michael Hanselmann
                                        " process")
749 8d8d650c Michael Hanselmann
750 8d8d650c Michael Hanselmann
REMOVE_INSTANCE_OPT = cli_option("--remove-instance", dest="remove_instance",
751 8d8d650c Michael Hanselmann
                                 action="store_true", default=False,
752 8d8d650c Michael Hanselmann
                                 help="Remove the instance from the cluster")
753 8d8d650c Michael Hanselmann
754 a14db5ff Iustin Pop
NEW_SECONDARY_OPT = cli_option("-n", "--new-secondary", dest="dst_node",
755 a14db5ff Iustin Pop
                               help="Specifies the new secondary node",
756 a14db5ff Iustin Pop
                               metavar="NODE", default=None,
757 a14db5ff Iustin Pop
                               completion_suggest=OPT_COMPL_ONE_NODE)
758 a14db5ff Iustin Pop
759 a72d0a79 Iustin Pop
ON_PRIMARY_OPT = cli_option("-p", "--on-primary", dest="on_primary",
760 a72d0a79 Iustin Pop
                            default=False, action="store_true",
761 a72d0a79 Iustin Pop
                            help="Replace the disk(s) on the primary"
762 a72d0a79 Iustin Pop
                            " node (only for the drbd template)")
763 feb09e6a Iustin Pop
764 feb09e6a Iustin Pop
ON_SECONDARY_OPT = cli_option("-s", "--on-secondary", dest="on_secondary",
765 feb09e6a Iustin Pop
                              default=False, action="store_true",
766 feb09e6a Iustin Pop
                              help="Replace the disk(s) on the secondary"
767 feb09e6a Iustin Pop
                              " node (only for the drbd template)")
768 e00f7a05 Iustin Pop
769 4c61d894 Iustin Pop
AUTO_PROMOTE_OPT = cli_option("--auto-promote", dest="auto_promote",
770 4c61d894 Iustin Pop
                              default=False, action="store_true",
771 4c61d894 Iustin Pop
                              help="Lock all nodes and auto-promote as needed"
772 4c61d894 Iustin Pop
                              " to MC status")
773 4c61d894 Iustin Pop
774 e00f7a05 Iustin Pop
AUTO_REPLACE_OPT = cli_option("-a", "--auto", dest="auto",
775 e00f7a05 Iustin Pop
                              default=False, action="store_true",
776 e00f7a05 Iustin Pop
                              help="Automatically replace faulty disks"
777 e00f7a05 Iustin Pop
                              " (only for the drbd template)")
778 a72d0a79 Iustin Pop
779 05586c90 Iustin Pop
IGNORE_SIZE_OPT = cli_option("--ignore-size", dest="ignore_size",
780 05586c90 Iustin Pop
                             default=False, action="store_true",
781 05586c90 Iustin Pop
                             help="Ignore current recorded size"
782 05586c90 Iustin Pop
                             " (useful for forcing activation when"
783 05586c90 Iustin Pop
                             " the recorded size is wrong)")
784 05586c90 Iustin Pop
785 df62e5db Iustin Pop
SRC_NODE_OPT = cli_option("--src-node", dest="src_node", help="Source node",
786 df62e5db Iustin Pop
                          metavar="<node>",
787 df62e5db Iustin Pop
                          completion_suggest=OPT_COMPL_ONE_NODE)
788 df62e5db Iustin Pop
789 df62e5db Iustin Pop
SRC_DIR_OPT = cli_option("--src-dir", dest="src_dir", help="Source directory",
790 df62e5db Iustin Pop
                         metavar="<dir>")
791 df62e5db Iustin Pop
792 8d823629 Iustin Pop
SECONDARY_IP_OPT = cli_option("-s", "--secondary-ip", dest="secondary_ip",
793 8d823629 Iustin Pop
                              help="Specify the secondary ip for the node",
794 8d823629 Iustin Pop
                              metavar="ADDRESS", default=None)
795 8d823629 Iustin Pop
796 b8d0f938 Iustin Pop
READD_OPT = cli_option("--readd", dest="readd",
797 b8d0f938 Iustin Pop
                       default=False, action="store_true",
798 b8d0f938 Iustin Pop
                       help="Readd old node after replacing it")
799 b8d0f938 Iustin Pop
800 fcdde7f2 Iustin Pop
NOSSH_KEYCHECK_OPT = cli_option("--no-ssh-key-check", dest="ssh_key_check",
801 fcdde7f2 Iustin Pop
                                default=True, action="store_false",
802 fcdde7f2 Iustin Pop
                                help="Disable SSH key fingerprint checking")
803 fcdde7f2 Iustin Pop
804 c38c44ad Michael Hanselmann
805 771734c9 Iustin Pop
MC_OPT = cli_option("-C", "--master-candidate", dest="master_candidate",
806 e7b61bb0 Iustin Pop
                    type="bool", default=None, metavar=_YORNO,
807 771734c9 Iustin Pop
                    help="Set the master_candidate flag on the node")
808 771734c9 Iustin Pop
809 771734c9 Iustin Pop
OFFLINE_OPT = cli_option("-O", "--offline", dest="offline", metavar=_YORNO,
810 e7b61bb0 Iustin Pop
                         type="bool", default=None,
811 771734c9 Iustin Pop
                         help="Set the offline flag on the node")
812 771734c9 Iustin Pop
813 771734c9 Iustin Pop
DRAINED_OPT = cli_option("-D", "--drained", dest="drained", metavar=_YORNO,
814 e7b61bb0 Iustin Pop
                         type="bool", default=None,
815 771734c9 Iustin Pop
                         help="Set the drained flag on the node")
816 771734c9 Iustin Pop
817 e7e09483 Iustin Pop
ALLOCATABLE_OPT = cli_option("--allocatable", dest="allocatable",
818 e7b61bb0 Iustin Pop
                             type="bool", default=None, metavar=_YORNO,
819 e7e09483 Iustin Pop
                             help="Set the allocatable flag on a volume")
820 e7e09483 Iustin Pop
821 831040bf Iustin Pop
NOLVM_STORAGE_OPT = cli_option("--no-lvm-storage", dest="lvm_storage",
822 831040bf Iustin Pop
                               help="Disable support for lvm based instances"
823 831040bf Iustin Pop
                               " (cluster-wide)",
824 831040bf Iustin Pop
                               action="store_false", default=True)
825 831040bf Iustin Pop
826 383a3591 Iustin Pop
ENABLED_HV_OPT = cli_option("--enabled-hypervisors",
827 383a3591 Iustin Pop
                            dest="enabled_hypervisors",
828 383a3591 Iustin Pop
                            help="Comma-separated list of hypervisors",
829 383a3591 Iustin Pop
                            type="string", default=None)
830 383a3591 Iustin Pop
831 4fbc93dd Iustin Pop
NIC_PARAMS_OPT = cli_option("-N", "--nic-parameters", dest="nicparams",
832 4fbc93dd Iustin Pop
                            type="keyval", default={},
833 4fbc93dd Iustin Pop
                            help="NIC parameters")
834 4fbc93dd Iustin Pop
835 e32df528 Iustin Pop
CP_SIZE_OPT = cli_option("-C", "--candidate-pool-size", default=None,
836 e32df528 Iustin Pop
                         dest="candidate_pool_size", type="int",
837 e32df528 Iustin Pop
                         help="Set the candidate pool size")
838 e32df528 Iustin Pop
839 b58726e8 Iustin Pop
VG_NAME_OPT = cli_option("-g", "--vg-name", dest="vg_name",
840 b58726e8 Iustin Pop
                         help="Enables LVM and specifies the volume group"
841 b58726e8 Iustin Pop
                         " name (cluster-wide) for disk allocation [xenvg]",
842 b58726e8 Iustin Pop
                         metavar="VG", default=None)
843 b58726e8 Iustin Pop
844 1f587d3d Iustin Pop
YES_DOIT_OPT = cli_option("--yes-do-it", dest="yes_do_it",
845 1f587d3d Iustin Pop
                          help="Destroy cluster", action="store_true")
846 b58726e8 Iustin Pop
847 58371861 Iustin Pop
NOVOTING_OPT = cli_option("--no-voting", dest="no_voting",
848 58371861 Iustin Pop
                          help="Skip node agreement check (dangerous)",
849 58371861 Iustin Pop
                          action="store_true", default=False)
850 58371861 Iustin Pop
851 e3646f22 Iustin Pop
MAC_PREFIX_OPT = cli_option("-m", "--mac-prefix", dest="mac_prefix",
852 e3646f22 Iustin Pop
                            help="Specify the mac prefix for the instance IP"
853 e3646f22 Iustin Pop
                            " addresses, in the format XX:XX:XX",
854 e3646f22 Iustin Pop
                            metavar="PREFIX",
855 e3646f22 Iustin Pop
                            default=None)
856 e3646f22 Iustin Pop
857 29392516 Iustin Pop
MASTER_NETDEV_OPT = cli_option("--master-netdev", dest="master_netdev",
858 29392516 Iustin Pop
                               help="Specify the node interface (cluster-wide)"
859 29392516 Iustin Pop
                               " on which the master IP address will be added "
860 29392516 Iustin Pop
                               " [%s]" % constants.DEFAULT_BRIDGE,
861 29392516 Iustin Pop
                               metavar="NETDEV",
862 29392516 Iustin Pop
                               default=constants.DEFAULT_BRIDGE)
863 29392516 Iustin Pop
864 29392516 Iustin Pop
GLOBAL_FILEDIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir",
865 29392516 Iustin Pop
                                help="Specify the default directory (cluster-"
866 29392516 Iustin Pop
                                "wide) for storing the file-based disks [%s]" %
867 29392516 Iustin Pop
                                constants.DEFAULT_FILE_STORAGE_DIR,
868 29392516 Iustin Pop
                                metavar="DIR",
869 29392516 Iustin Pop
                                default=constants.DEFAULT_FILE_STORAGE_DIR)
870 29392516 Iustin Pop
871 29392516 Iustin Pop
NOMODIFY_ETCHOSTS_OPT = cli_option("--no-etc-hosts", dest="modify_etc_hosts",
872 29392516 Iustin Pop
                                   help="Don't modify /etc/hosts",
873 29392516 Iustin Pop
                                   action="store_false", default=True)
874 29392516 Iustin Pop
875 b989b9d9 Ken Wehr
NOMODIFY_SSH_SETUP_OPT = cli_option("--no-ssh-init", dest="modify_ssh_setup",
876 b989b9d9 Ken Wehr
                                    help="Don't initialize SSH keys",
877 b989b9d9 Ken Wehr
                                    action="store_false", default=True)
878 b989b9d9 Ken Wehr
879 14e9e7f3 Iustin Pop
ERROR_CODES_OPT = cli_option("--error-codes", dest="error_codes",
880 14e9e7f3 Iustin Pop
                             help="Enable parseable error messages",
881 14e9e7f3 Iustin Pop
                             action="store_true", default=False)
882 14e9e7f3 Iustin Pop
883 14e9e7f3 Iustin Pop
NONPLUS1_OPT = cli_option("--no-nplus1-mem", dest="skip_nplusone_mem",
884 14e9e7f3 Iustin Pop
                          help="Skip N+1 memory redundancy tests",
885 14e9e7f3 Iustin Pop
                          action="store_true", default=False)
886 14e9e7f3 Iustin Pop
887 12054861 Iustin Pop
REBOOT_TYPE_OPT = cli_option("-t", "--type", dest="reboot_type",
888 12054861 Iustin Pop
                             help="Type of reboot: soft/hard/full",
889 12054861 Iustin Pop
                             default=constants.INSTANCE_REBOOT_HARD,
890 12054861 Iustin Pop
                             metavar="<REBOOT>",
891 12054861 Iustin Pop
                             choices=list(constants.REBOOT_TYPES))
892 12054861 Iustin Pop
893 ee3f9578 Iustin Pop
IGNORE_SECONDARIES_OPT = cli_option("--ignore-secondaries",
894 ee3f9578 Iustin Pop
                                    dest="ignore_secondaries",
895 ee3f9578 Iustin Pop
                                    default=False, action="store_true",
896 ee3f9578 Iustin Pop
                                    help="Ignore errors from secondaries")
897 ee3f9578 Iustin Pop
898 69b99987 Michael Hanselmann
NOSHUTDOWN_OPT = cli_option("--noshutdown", dest="shutdown",
899 44c44832 Iustin Pop
                            action="store_false", default=True,
900 44c44832 Iustin Pop
                            help="Don't shutdown the instance (unsafe)")
901 44c44832 Iustin Pop
902 b5762e2a Guido Trotter
TIMEOUT_OPT = cli_option("--timeout", dest="timeout", type="int",
903 b5762e2a Guido Trotter
                         default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
904 b5762e2a Guido Trotter
                         help="Maximum time to wait")
905 44c44832 Iustin Pop
906 4d98c565 Guido Trotter
SHUTDOWN_TIMEOUT_OPT = cli_option("--shutdown-timeout",
907 4d98c565 Guido Trotter
                         dest="shutdown_timeout", type="int",
908 7e5eaaa8 Guido Trotter
                         default=constants.DEFAULT_SHUTDOWN_TIMEOUT,
909 7e5eaaa8 Guido Trotter
                         help="Maximum time to wait for instance shutdown")
910 7e5eaaa8 Guido Trotter
911 7ea7bcf6 Iustin Pop
EARLY_RELEASE_OPT = cli_option("--early-release",
912 7ea7bcf6 Iustin Pop
                               dest="early_release", default=False,
913 7ea7bcf6 Iustin Pop
                               action="store_true",
914 7ea7bcf6 Iustin Pop
                               help="Release the locks on the secondary"
915 7ea7bcf6 Iustin Pop
                               " node(s) early")
916 7ea7bcf6 Iustin Pop
917 6d4a1656 Michael Hanselmann
NEW_CLUSTER_CERT_OPT = cli_option("--new-cluster-certificate",
918 6d4a1656 Michael Hanselmann
                                  dest="new_cluster_cert",
919 6d4a1656 Michael Hanselmann
                                  default=False, action="store_true",
920 6d4a1656 Michael Hanselmann
                                  help="Generate a new cluster certificate")
921 6d4a1656 Michael Hanselmann
922 6d4a1656 Michael Hanselmann
RAPI_CERT_OPT = cli_option("--rapi-certificate", dest="rapi_cert",
923 6d4a1656 Michael Hanselmann
                           default=None,
924 6d4a1656 Michael Hanselmann
                           help="File containing new RAPI certificate")
925 6d4a1656 Michael Hanselmann
926 6d4a1656 Michael Hanselmann
NEW_RAPI_CERT_OPT = cli_option("--new-rapi-certificate", dest="new_rapi_cert",
927 6d4a1656 Michael Hanselmann
                               default=None, action="store_true",
928 6d4a1656 Michael Hanselmann
                               help=("Generate a new self-signed RAPI"
929 6d4a1656 Michael Hanselmann
                                     " certificate"))
930 6d4a1656 Michael Hanselmann
931 6b7d5878 Michael Hanselmann
NEW_CONFD_HMAC_KEY_OPT = cli_option("--new-confd-hmac-key",
932 6b7d5878 Michael Hanselmann
                                    dest="new_confd_hmac_key",
933 6b7d5878 Michael Hanselmann
                                    default=False, action="store_true",
934 6b7d5878 Michael Hanselmann
                                    help=("Create a new HMAC key for %s" %
935 6b7d5878 Michael Hanselmann
                                          constants.CONFD))
936 6d4a1656 Michael Hanselmann
937 3db3eb2a Michael Hanselmann
CLUSTER_DOMAIN_SECRET_OPT = cli_option("--cluster-domain-secret",
938 3db3eb2a Michael Hanselmann
                                       dest="cluster_domain_secret",
939 3db3eb2a Michael Hanselmann
                                       default=None,
940 3db3eb2a Michael Hanselmann
                                       help=("Load new new cluster domain"
941 3db3eb2a Michael Hanselmann
                                             " secret from file"))
942 3db3eb2a Michael Hanselmann
943 3db3eb2a Michael Hanselmann
NEW_CLUSTER_DOMAIN_SECRET_OPT = cli_option("--new-cluster-domain-secret",
944 3db3eb2a Michael Hanselmann
                                           dest="new_cluster_domain_secret",
945 3db3eb2a Michael Hanselmann
                                           default=False, action="store_true",
946 3db3eb2a Michael Hanselmann
                                           help=("Create a new cluster domain"
947 3db3eb2a Michael Hanselmann
                                                 " secret"))
948 3db3eb2a Michael Hanselmann
949 74adc100 Iustin Pop
USE_REPL_NET_OPT = cli_option("--use-replication-network",
950 74adc100 Iustin Pop
                              dest="use_replication_network",
951 74adc100 Iustin Pop
                              help="Whether to use the replication network"
952 74adc100 Iustin Pop
                              " for talking to the nodes",
953 74adc100 Iustin Pop
                              action="store_true", default=False)
954 74adc100 Iustin Pop
955 3953242f Iustin Pop
MAINTAIN_NODE_HEALTH_OPT = \
956 3953242f Iustin Pop
    cli_option("--maintain-node-health", dest="maintain_node_health",
957 3953242f Iustin Pop
               metavar=_YORNO, default=None, type="bool",
958 3953242f Iustin Pop
               help="Configure the cluster to automatically maintain node"
959 3953242f Iustin Pop
               " health, by shutting down unknown instances, shutting down"
960 3953242f Iustin Pop
               " unknown DRBD devices, etc.")
961 3953242f Iustin Pop
962 e588764d Iustin Pop
IDENTIFY_DEFAULTS_OPT = \
963 e588764d Iustin Pop
    cli_option("--identify-defaults", dest="identify_defaults",
964 e588764d Iustin Pop
               default=False, action="store_true",
965 e588764d Iustin Pop
               help="Identify which saved instance parameters are equal to"
966 e588764d Iustin Pop
               " the current cluster defaults and set them as such, instead"
967 e588764d Iustin Pop
               " of marking them as overridden")
968 e588764d Iustin Pop
969 1338f2b4 Balazs Lecz
UIDPOOL_OPT = cli_option("--uid-pool", default=None,
970 1338f2b4 Balazs Lecz
                         action="store", dest="uid_pool",
971 1338f2b4 Balazs Lecz
                         help=("A list of user-ids or user-id"
972 1338f2b4 Balazs Lecz
                               " ranges separated by commas"))
973 1338f2b4 Balazs Lecz
974 fdad8c4d Balazs Lecz
ADD_UIDS_OPT = cli_option("--add-uids", default=None,
975 fdad8c4d Balazs Lecz
                          action="store", dest="add_uids",
976 fdad8c4d Balazs Lecz
                          help=("A list of user-ids or user-id"
977 fdad8c4d Balazs Lecz
                                " ranges separated by commas, to be"
978 fdad8c4d Balazs Lecz
                                " added to the user-id pool"))
979 fdad8c4d Balazs Lecz
980 fdad8c4d Balazs Lecz
REMOVE_UIDS_OPT = cli_option("--remove-uids", default=None,
981 fdad8c4d Balazs Lecz
                             action="store", dest="remove_uids",
982 fdad8c4d Balazs Lecz
                             help=("A list of user-ids or user-id"
983 fdad8c4d Balazs Lecz
                                   " ranges separated by commas, to be"
984 fdad8c4d Balazs Lecz
                                   " removed from the user-id pool"))
985 fdad8c4d Balazs Lecz
986 31d97b2a Guido Trotter
ROMAN_OPT = cli_option("--roman",
987 31d97b2a Guido Trotter
                       dest="roman_integers", default=False,
988 31d97b2a Guido Trotter
                       action="store_true",
989 31d97b2a Guido Trotter
                       help="Use roman numbers for positive integers")
990 31d97b2a Guido Trotter
991 31d97b2a Guido Trotter
992 771734c9 Iustin Pop
993 de47cf8f Guido Trotter
def _ParseArgs(argv, commands, aliases):
994 c41eea6e Iustin Pop
  """Parser for the command line arguments.
995 a8083063 Iustin Pop

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

999 c41eea6e Iustin Pop
  @param argv: the command line
1000 c41eea6e Iustin Pop
  @param commands: dictionary with special contents, see the design
1001 c41eea6e Iustin Pop
      doc for cmdline handling
1002 c41eea6e Iustin Pop
  @param aliases: dictionary with command aliases {'alias': 'target, ...}
1003 098c0958 Michael Hanselmann

1004 a8083063 Iustin Pop
  """
1005 a8083063 Iustin Pop
  if len(argv) == 0:
1006 a8083063 Iustin Pop
    binary = "<command>"
1007 a8083063 Iustin Pop
  else:
1008 a8083063 Iustin Pop
    binary = argv[0].split("/")[-1]
1009 a8083063 Iustin Pop
1010 a8083063 Iustin Pop
  if len(argv) > 1 and argv[1] == "--version":
1011 03298ebe Michael Hanselmann
    ToStdout("%s (ganeti) %s", binary, constants.RELEASE_VERSION)
1012 a8083063 Iustin Pop
    # Quit right away. That way we don't have to care about this special
1013 a8083063 Iustin Pop
    # argument. optparse.py does it the same.
1014 a8083063 Iustin Pop
    sys.exit(0)
1015 a8083063 Iustin Pop
1016 de47cf8f Guido Trotter
  if len(argv) < 2 or not (argv[1] in commands or
1017 70a35b6f Guido Trotter
                           argv[1] in aliases):
1018 a8083063 Iustin Pop
    # let's do a nice thing
1019 a8083063 Iustin Pop
    sortedcmds = commands.keys()
1020 a8083063 Iustin Pop
    sortedcmds.sort()
1021 03298ebe Michael Hanselmann
1022 03298ebe Michael Hanselmann
    ToStdout("Usage: %s {command} [options...] [argument...]", binary)
1023 03298ebe Michael Hanselmann
    ToStdout("%s <command> --help to see details, or man %s", binary, binary)
1024 03298ebe Michael Hanselmann
    ToStdout("")
1025 03298ebe Michael Hanselmann
1026 a8083063 Iustin Pop
    # compute the max line length for cmd + usage
1027 4e713df6 Iustin Pop
    mlen = max([len(" %s" % cmd) for cmd in commands])
1028 a8083063 Iustin Pop
    mlen = min(60, mlen) # should not get here...
1029 03298ebe Michael Hanselmann
1030 a8083063 Iustin Pop
    # and format a nice command list
1031 03298ebe Michael Hanselmann
    ToStdout("Commands:")
1032 a8083063 Iustin Pop
    for cmd in sortedcmds:
1033 4e713df6 Iustin Pop
      cmdstr = " %s" % (cmd,)
1034 9a033156 Iustin Pop
      help_text = commands[cmd][4]
1035 03298ebe Michael Hanselmann
      help_lines = textwrap.wrap(help_text, 79 - 3 - mlen)
1036 03298ebe Michael Hanselmann
      ToStdout("%-*s - %s", mlen, cmdstr, help_lines.pop(0))
1037 a8083063 Iustin Pop
      for line in help_lines:
1038 03298ebe Michael Hanselmann
        ToStdout("%-*s   %s", mlen, "", line)
1039 03298ebe Michael Hanselmann
1040 03298ebe Michael Hanselmann
    ToStdout("")
1041 03298ebe Michael Hanselmann
1042 a8083063 Iustin Pop
    return None, None, None
1043 de47cf8f Guido Trotter
1044 de47cf8f Guido Trotter
  # get command, unalias it, and look it up in commands
1045 a8083063 Iustin Pop
  cmd = argv.pop(1)
1046 de47cf8f Guido Trotter
  if cmd in aliases:
1047 de47cf8f Guido Trotter
    if cmd in commands:
1048 de47cf8f Guido Trotter
      raise errors.ProgrammerError("Alias '%s' overrides an existing"
1049 de47cf8f Guido Trotter
                                   " command" % cmd)
1050 de47cf8f Guido Trotter
1051 de47cf8f Guido Trotter
    if aliases[cmd] not in commands:
1052 de47cf8f Guido Trotter
      raise errors.ProgrammerError("Alias '%s' maps to non-existing"
1053 de47cf8f Guido Trotter
                                   " command '%s'" % (cmd, aliases[cmd]))
1054 de47cf8f Guido Trotter
1055 de47cf8f Guido Trotter
    cmd = aliases[cmd]
1056 de47cf8f Guido Trotter
1057 a8005e17 Michael Hanselmann
  func, args_def, parser_opts, usage, description = commands[cmd]
1058 064c21f8 Iustin Pop
  parser = OptionParser(option_list=parser_opts + [_DRY_RUN_OPT, DEBUG_OPT],
1059 a8083063 Iustin Pop
                        description=description,
1060 a8083063 Iustin Pop
                        formatter=TitledHelpFormatter(),
1061 a8083063 Iustin Pop
                        usage="%%prog %s %s" % (cmd, usage))
1062 a8083063 Iustin Pop
  parser.disable_interspersed_args()
1063 a8083063 Iustin Pop
  options, args = parser.parse_args()
1064 a8005e17 Michael Hanselmann
1065 a8005e17 Michael Hanselmann
  if not _CheckArguments(cmd, args_def, args):
1066 a8083063 Iustin Pop
    return None, None, None
1067 a8083063 Iustin Pop
1068 a8083063 Iustin Pop
  return func, options, args
1069 a8083063 Iustin Pop
1070 a8083063 Iustin Pop
1071 a8005e17 Michael Hanselmann
def _CheckArguments(cmd, args_def, args):
1072 a8005e17 Michael Hanselmann
  """Verifies the arguments using the argument definition.
1073 a8005e17 Michael Hanselmann

1074 a8005e17 Michael Hanselmann
  Algorithm:
1075 a8005e17 Michael Hanselmann

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

1078 a8005e17 Michael Hanselmann
    1. For each argument in definition
1079 a8005e17 Michael Hanselmann

1080 a8005e17 Michael Hanselmann
      1. Keep running count of minimum number of values (min_count)
1081 a8005e17 Michael Hanselmann
      1. Keep running count of maximum number of values (max_count)
1082 a8005e17 Michael Hanselmann
      1. If it has an unlimited number of values
1083 a8005e17 Michael Hanselmann

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

1086 a8005e17 Michael Hanselmann
    1. If last argument has limited number of values
1087 a8005e17 Michael Hanselmann

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

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

1092 a8005e17 Michael Hanselmann
  """
1093 a8005e17 Michael Hanselmann
  if args and not args_def:
1094 a8005e17 Michael Hanselmann
    ToStderr("Error: Command %s expects no arguments", cmd)
1095 a8005e17 Michael Hanselmann
    return False
1096 a8005e17 Michael Hanselmann
1097 a8005e17 Michael Hanselmann
  min_count = None
1098 a8005e17 Michael Hanselmann
  max_count = None
1099 a8005e17 Michael Hanselmann
  check_max = None
1100 a8005e17 Michael Hanselmann
1101 a8005e17 Michael Hanselmann
  last_idx = len(args_def) - 1
1102 a8005e17 Michael Hanselmann
1103 a8005e17 Michael Hanselmann
  for idx, arg in enumerate(args_def):
1104 a8005e17 Michael Hanselmann
    if min_count is None:
1105 a8005e17 Michael Hanselmann
      min_count = arg.min
1106 a8005e17 Michael Hanselmann
    elif arg.min is not None:
1107 a8005e17 Michael Hanselmann
      min_count += arg.min
1108 a8005e17 Michael Hanselmann
1109 a8005e17 Michael Hanselmann
    if max_count is None:
1110 a8005e17 Michael Hanselmann
      max_count = arg.max
1111 a8005e17 Michael Hanselmann
    elif arg.max is not None:
1112 a8005e17 Michael Hanselmann
      max_count += arg.max
1113 a8005e17 Michael Hanselmann
1114 a8005e17 Michael Hanselmann
    if idx == last_idx:
1115 a8005e17 Michael Hanselmann
      check_max = (arg.max is not None)
1116 a8005e17 Michael Hanselmann
1117 a8005e17 Michael Hanselmann
    elif arg.max is None:
1118 a8005e17 Michael Hanselmann
      raise errors.ProgrammerError("Only the last argument can have max=None")
1119 a8005e17 Michael Hanselmann
1120 a8005e17 Michael Hanselmann
  if check_max:
1121 a8005e17 Michael Hanselmann
    # Command with exact number of arguments
1122 a8005e17 Michael Hanselmann
    if (min_count is not None and max_count is not None and
1123 a8005e17 Michael Hanselmann
        min_count == max_count and len(args) != min_count):
1124 a8005e17 Michael Hanselmann
      ToStderr("Error: Command %s expects %d argument(s)", cmd, min_count)
1125 a8005e17 Michael Hanselmann
      return False
1126 a8005e17 Michael Hanselmann
1127 a8005e17 Michael Hanselmann
    # Command with limited number of arguments
1128 a8005e17 Michael Hanselmann
    if max_count is not None and len(args) > max_count:
1129 a8005e17 Michael Hanselmann
      ToStderr("Error: Command %s expects only %d argument(s)",
1130 a8005e17 Michael Hanselmann
               cmd, max_count)
1131 a8005e17 Michael Hanselmann
      return False
1132 a8005e17 Michael Hanselmann
1133 a8005e17 Michael Hanselmann
  # Command with some required arguments
1134 a8005e17 Michael Hanselmann
  if min_count is not None and len(args) < min_count:
1135 a8005e17 Michael Hanselmann
    ToStderr("Error: Command %s expects at least %d argument(s)",
1136 a8005e17 Michael Hanselmann
             cmd, min_count)
1137 a8005e17 Michael Hanselmann
    return False
1138 a8005e17 Michael Hanselmann
1139 a8005e17 Michael Hanselmann
  return True
1140 a8005e17 Michael Hanselmann
1141 a8005e17 Michael Hanselmann
1142 60d49723 Michael Hanselmann
def SplitNodeOption(value):
1143 60d49723 Michael Hanselmann
  """Splits the value of a --node option.
1144 60d49723 Michael Hanselmann

1145 60d49723 Michael Hanselmann
  """
1146 60d49723 Michael Hanselmann
  if value and ':' in value:
1147 60d49723 Michael Hanselmann
    return value.split(':', 1)
1148 60d49723 Michael Hanselmann
  else:
1149 60d49723 Michael Hanselmann
    return (value, None)
1150 60d49723 Michael Hanselmann
1151 60d49723 Michael Hanselmann
1152 07150497 Guido Trotter
def CalculateOSNames(os_name, os_variants):
1153 07150497 Guido Trotter
  """Calculates all the names an OS can be called, according to its variants.
1154 07150497 Guido Trotter

1155 07150497 Guido Trotter
  @type os_name: string
1156 07150497 Guido Trotter
  @param os_name: base name of the os
1157 07150497 Guido Trotter
  @type os_variants: list or None
1158 07150497 Guido Trotter
  @param os_variants: list of supported variants
1159 07150497 Guido Trotter
  @rtype: list
1160 07150497 Guido Trotter
  @return: list of valid names
1161 07150497 Guido Trotter

1162 07150497 Guido Trotter
  """
1163 07150497 Guido Trotter
  if os_variants:
1164 07150497 Guido Trotter
    return ['%s+%s' % (os_name, v) for v in os_variants]
1165 07150497 Guido Trotter
  else:
1166 07150497 Guido Trotter
    return [os_name]
1167 07150497 Guido Trotter
1168 07150497 Guido Trotter
1169 4331f6cd Michael Hanselmann
def UsesRPC(fn):
1170 4331f6cd Michael Hanselmann
  def wrapper(*args, **kwargs):
1171 4331f6cd Michael Hanselmann
    rpc.Init()
1172 4331f6cd Michael Hanselmann
    try:
1173 4331f6cd Michael Hanselmann
      return fn(*args, **kwargs)
1174 4331f6cd Michael Hanselmann
    finally:
1175 4331f6cd Michael Hanselmann
      rpc.Shutdown()
1176 4331f6cd Michael Hanselmann
  return wrapper
1177 4331f6cd Michael Hanselmann
1178 4331f6cd Michael Hanselmann
1179 47988778 Iustin Pop
def AskUser(text, choices=None):
1180 47988778 Iustin Pop
  """Ask the user a question.
1181 a8083063 Iustin Pop

1182 c41eea6e Iustin Pop
  @param text: the question to ask
1183 a8083063 Iustin Pop

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

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

1193 a8083063 Iustin Pop
  """
1194 47988778 Iustin Pop
  if choices is None:
1195 47988778 Iustin Pop
    choices = [('y', True, 'Perform the operation'),
1196 47988778 Iustin Pop
               ('n', False, 'Do not perform the operation')]
1197 47988778 Iustin Pop
  if not choices or not isinstance(choices, list):
1198 5bbd3f7f Michael Hanselmann
    raise errors.ProgrammerError("Invalid choices argument to AskUser")
1199 47988778 Iustin Pop
  for entry in choices:
1200 47988778 Iustin Pop
    if not isinstance(entry, tuple) or len(entry) < 3 or entry[0] == '?':
1201 5bbd3f7f Michael Hanselmann
      raise errors.ProgrammerError("Invalid choices element to AskUser")
1202 47988778 Iustin Pop
1203 47988778 Iustin Pop
  answer = choices[-1][1]
1204 47988778 Iustin Pop
  new_text = []
1205 47988778 Iustin Pop
  for line in text.splitlines():
1206 47988778 Iustin Pop
    new_text.append(textwrap.fill(line, 70, replace_whitespace=False))
1207 47988778 Iustin Pop
  text = "\n".join(new_text)
1208 a8083063 Iustin Pop
  try:
1209 3023170f Iustin Pop
    f = file("/dev/tty", "a+")
1210 a8083063 Iustin Pop
  except IOError:
1211 47988778 Iustin Pop
    return answer
1212 a8083063 Iustin Pop
  try:
1213 47988778 Iustin Pop
    chars = [entry[0] for entry in choices]
1214 47988778 Iustin Pop
    chars[-1] = "[%s]" % chars[-1]
1215 47988778 Iustin Pop
    chars.append('?')
1216 47988778 Iustin Pop
    maps = dict([(entry[0], entry[1]) for entry in choices])
1217 47988778 Iustin Pop
    while True:
1218 47988778 Iustin Pop
      f.write(text)
1219 47988778 Iustin Pop
      f.write('\n')
1220 47988778 Iustin Pop
      f.write("/".join(chars))
1221 47988778 Iustin Pop
      f.write(": ")
1222 47988778 Iustin Pop
      line = f.readline(2).strip().lower()
1223 47988778 Iustin Pop
      if line in maps:
1224 47988778 Iustin Pop
        answer = maps[line]
1225 47988778 Iustin Pop
        break
1226 47988778 Iustin Pop
      elif line == '?':
1227 47988778 Iustin Pop
        for entry in choices:
1228 47988778 Iustin Pop
          f.write(" %s - %s\n" % (entry[0], entry[2]))
1229 47988778 Iustin Pop
        f.write("\n")
1230 47988778 Iustin Pop
        continue
1231 a8083063 Iustin Pop
  finally:
1232 a8083063 Iustin Pop
    f.close()
1233 a8083063 Iustin Pop
  return answer
1234 a8083063 Iustin Pop
1235 a8083063 Iustin Pop
1236 e9d741b6 Iustin Pop
class JobSubmittedException(Exception):
1237 e9d741b6 Iustin Pop
  """Job was submitted, client should exit.
1238 e9d741b6 Iustin Pop

1239 e9d741b6 Iustin Pop
  This exception has one argument, the ID of the job that was
1240 e9d741b6 Iustin Pop
  submitted. The handler should print this ID.
1241 e9d741b6 Iustin Pop

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

1244 e9d741b6 Iustin Pop
  """
1245 e9d741b6 Iustin Pop
1246 e9d741b6 Iustin Pop
1247 0a1e74d9 Iustin Pop
def SendJob(ops, cl=None):
1248 0a1e74d9 Iustin Pop
  """Function to submit an opcode without waiting for the results.
1249 a8083063 Iustin Pop

1250 0a1e74d9 Iustin Pop
  @type ops: list
1251 0a1e74d9 Iustin Pop
  @param ops: list of opcodes
1252 0a1e74d9 Iustin Pop
  @type cl: luxi.Client
1253 0a1e74d9 Iustin Pop
  @param cl: the luxi client to use for communicating with the master;
1254 0a1e74d9 Iustin Pop
             if None, a new client will be created
1255 a8083063 Iustin Pop

1256 a8083063 Iustin Pop
  """
1257 e2212007 Iustin Pop
  if cl is None:
1258 b33e986b Iustin Pop
    cl = GetClient()
1259 685ee993 Iustin Pop
1260 0a1e74d9 Iustin Pop
  job_id = cl.SubmitJob(ops)
1261 0a1e74d9 Iustin Pop
1262 0a1e74d9 Iustin Pop
  return job_id
1263 0a1e74d9 Iustin Pop
1264 0a1e74d9 Iustin Pop
1265 4e338533 Michael Hanselmann
def GenericPollJob(job_id, cbs, report_cbs):
1266 4e338533 Michael Hanselmann
  """Generic job-polling function.
1267 0a1e74d9 Iustin Pop

1268 4e338533 Michael Hanselmann
  @type job_id: number
1269 4e338533 Michael Hanselmann
  @param job_id: Job ID
1270 4e338533 Michael Hanselmann
  @type cbs: Instance of L{JobPollCbBase}
1271 4e338533 Michael Hanselmann
  @param cbs: Data callbacks
1272 4e338533 Michael Hanselmann
  @type report_cbs: Instance of L{JobPollReportCbBase}
1273 4e338533 Michael Hanselmann
  @param report_cbs: Reporting callbacks
1274 0a1e74d9 Iustin Pop

1275 0a1e74d9 Iustin Pop
  """
1276 6c5a7090 Michael Hanselmann
  prev_job_info = None
1277 6c5a7090 Michael Hanselmann
  prev_logmsg_serial = None
1278 6c5a7090 Michael Hanselmann
1279 f4484122 Michael Hanselmann
  status = None
1280 f4484122 Michael Hanselmann
1281 685ee993 Iustin Pop
  while True:
1282 4e338533 Michael Hanselmann
    result = cbs.WaitForJobChangeOnce(job_id, ["status"], prev_job_info,
1283 4e338533 Michael Hanselmann
                                      prev_logmsg_serial)
1284 6c5a7090 Michael Hanselmann
    if not result:
1285 685ee993 Iustin Pop
      # job not found, go away!
1286 0bbe448c Michael Hanselmann
      raise errors.JobLost("Job with id %s lost" % job_id)
1287 4e338533 Michael Hanselmann
1288 4e338533 Michael Hanselmann
    if result == constants.JOB_NOTCHANGED:
1289 4e338533 Michael Hanselmann
      report_cbs.ReportNotChanged(job_id, status)
1290 f4484122 Michael Hanselmann
1291 f4484122 Michael Hanselmann
      # Wait again
1292 f4484122 Michael Hanselmann
      continue
1293 685ee993 Iustin Pop
1294 6c5a7090 Michael Hanselmann
    # Split result, a tuple of (field values, log entries)
1295 6c5a7090 Michael Hanselmann
    (job_info, log_entries) = result
1296 6c5a7090 Michael Hanselmann
    (status, ) = job_info
1297 6c5a7090 Michael Hanselmann
1298 6c5a7090 Michael Hanselmann
    if log_entries:
1299 6c5a7090 Michael Hanselmann
      for log_entry in log_entries:
1300 4e338533 Michael Hanselmann
        (serial, timestamp, log_type, message) = log_entry
1301 4e338533 Michael Hanselmann
        report_cbs.ReportLogMessage(job_id, serial, timestamp,
1302 4e338533 Michael Hanselmann
                                    log_type, message)
1303 6c5a7090 Michael Hanselmann
        prev_logmsg_serial = max(prev_logmsg_serial, serial)
1304 6c5a7090 Michael Hanselmann
1305 0bbe448c Michael Hanselmann
    # TODO: Handle canceled and archived jobs
1306 fbf0262f Michael Hanselmann
    elif status in (constants.JOB_STATUS_SUCCESS,
1307 fbf0262f Michael Hanselmann
                    constants.JOB_STATUS_ERROR,
1308 fbf0262f Michael Hanselmann
                    constants.JOB_STATUS_CANCELING,
1309 fbf0262f Michael Hanselmann
                    constants.JOB_STATUS_CANCELED):
1310 685ee993 Iustin Pop
      break
1311 6c5a7090 Michael Hanselmann
1312 6c5a7090 Michael Hanselmann
    prev_job_info = job_info
1313 685ee993 Iustin Pop
1314 4e338533 Michael Hanselmann
  jobs = cbs.QueryJobs([job_id], ["status", "opstatus", "opresult"])
1315 0bbe448c Michael Hanselmann
  if not jobs:
1316 0bbe448c Michael Hanselmann
    raise errors.JobLost("Job with id %s lost" % job_id)
1317 685ee993 Iustin Pop
1318 0e050889 Iustin Pop
  status, opstatus, result = jobs[0]
1319 4e338533 Michael Hanselmann
1320 0bbe448c Michael Hanselmann
  if status == constants.JOB_STATUS_SUCCESS:
1321 53c04d04 Iustin Pop
    return result
1322 4e338533 Michael Hanselmann
1323 4e338533 Michael Hanselmann
  if status in (constants.JOB_STATUS_CANCELING, constants.JOB_STATUS_CANCELED):
1324 fbf0262f Michael Hanselmann
    raise errors.OpExecError("Job was canceled")
1325 4e338533 Michael Hanselmann
1326 4e338533 Michael Hanselmann
  has_ok = False
1327 4e338533 Michael Hanselmann
  for idx, (status, msg) in enumerate(zip(opstatus, result)):
1328 4e338533 Michael Hanselmann
    if status == constants.OP_STATUS_SUCCESS:
1329 4e338533 Michael Hanselmann
      has_ok = True
1330 4e338533 Michael Hanselmann
    elif status == constants.OP_STATUS_ERROR:
1331 4e338533 Michael Hanselmann
      errors.MaybeRaise(msg)
1332 4e338533 Michael Hanselmann
1333 4e338533 Michael Hanselmann
      if has_ok:
1334 4e338533 Michael Hanselmann
        raise errors.OpExecError("partial failure (opcode %d): %s" %
1335 4e338533 Michael Hanselmann
                                 (idx, msg))
1336 4e338533 Michael Hanselmann
1337 4e338533 Michael Hanselmann
      raise errors.OpExecError(str(msg))
1338 4e338533 Michael Hanselmann
1339 4e338533 Michael Hanselmann
  # default failure mode
1340 4e338533 Michael Hanselmann
  raise errors.OpExecError(result)
1341 4e338533 Michael Hanselmann
1342 4e338533 Michael Hanselmann
1343 4e338533 Michael Hanselmann
class JobPollCbBase:
1344 4e338533 Michael Hanselmann
  """Base class for L{GenericPollJob} callbacks.
1345 4e338533 Michael Hanselmann

1346 4e338533 Michael Hanselmann
  """
1347 4e338533 Michael Hanselmann
  def __init__(self):
1348 4e338533 Michael Hanselmann
    """Initializes this class.
1349 4e338533 Michael Hanselmann

1350 4e338533 Michael Hanselmann
    """
1351 4e338533 Michael Hanselmann
1352 4e338533 Michael Hanselmann
  def WaitForJobChangeOnce(self, job_id, fields,
1353 4e338533 Michael Hanselmann
                           prev_job_info, prev_log_serial):
1354 4e338533 Michael Hanselmann
    """Waits for changes on a job.
1355 4e338533 Michael Hanselmann

1356 4e338533 Michael Hanselmann
    """
1357 4e338533 Michael Hanselmann
    raise NotImplementedError()
1358 4e338533 Michael Hanselmann
1359 4e338533 Michael Hanselmann
  def QueryJobs(self, job_ids, fields):
1360 4e338533 Michael Hanselmann
    """Returns the selected fields for the selected job IDs.
1361 4e338533 Michael Hanselmann

1362 4e338533 Michael Hanselmann
    @type job_ids: list of numbers
1363 4e338533 Michael Hanselmann
    @param job_ids: Job IDs
1364 4e338533 Michael Hanselmann
    @type fields: list of strings
1365 4e338533 Michael Hanselmann
    @param fields: Fields
1366 4e338533 Michael Hanselmann

1367 4e338533 Michael Hanselmann
    """
1368 4e338533 Michael Hanselmann
    raise NotImplementedError()
1369 4e338533 Michael Hanselmann
1370 4e338533 Michael Hanselmann
1371 4e338533 Michael Hanselmann
class JobPollReportCbBase:
1372 4e338533 Michael Hanselmann
  """Base class for L{GenericPollJob} reporting callbacks.
1373 4e338533 Michael Hanselmann

1374 4e338533 Michael Hanselmann
  """
1375 4e338533 Michael Hanselmann
  def __init__(self):
1376 4e338533 Michael Hanselmann
    """Initializes this class.
1377 4e338533 Michael Hanselmann

1378 4e338533 Michael Hanselmann
    """
1379 4e338533 Michael Hanselmann
1380 4e338533 Michael Hanselmann
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
1381 4e338533 Michael Hanselmann
    """Handles a log message.
1382 4e338533 Michael Hanselmann

1383 4e338533 Michael Hanselmann
    """
1384 4e338533 Michael Hanselmann
    raise NotImplementedError()
1385 4e338533 Michael Hanselmann
1386 4e338533 Michael Hanselmann
  def ReportNotChanged(self, job_id, status):
1387 4e338533 Michael Hanselmann
    """Called for if a job hasn't changed in a while.
1388 4e338533 Michael Hanselmann

1389 4e338533 Michael Hanselmann
    @type job_id: number
1390 4e338533 Michael Hanselmann
    @param job_id: Job ID
1391 4e338533 Michael Hanselmann
    @type status: string or None
1392 4e338533 Michael Hanselmann
    @param status: Job status if available
1393 4e338533 Michael Hanselmann

1394 4e338533 Michael Hanselmann
    """
1395 4e338533 Michael Hanselmann
    raise NotImplementedError()
1396 4e338533 Michael Hanselmann
1397 4e338533 Michael Hanselmann
1398 4e338533 Michael Hanselmann
class _LuxiJobPollCb(JobPollCbBase):
1399 4e338533 Michael Hanselmann
  def __init__(self, cl):
1400 4e338533 Michael Hanselmann
    """Initializes this class.
1401 4e338533 Michael Hanselmann

1402 4e338533 Michael Hanselmann
    """
1403 4e338533 Michael Hanselmann
    JobPollCbBase.__init__(self)
1404 4e338533 Michael Hanselmann
    self.cl = cl
1405 4e338533 Michael Hanselmann
1406 4e338533 Michael Hanselmann
  def WaitForJobChangeOnce(self, job_id, fields,
1407 4e338533 Michael Hanselmann
                           prev_job_info, prev_log_serial):
1408 4e338533 Michael Hanselmann
    """Waits for changes on a job.
1409 4e338533 Michael Hanselmann

1410 4e338533 Michael Hanselmann
    """
1411 4e338533 Michael Hanselmann
    return self.cl.WaitForJobChangeOnce(job_id, fields,
1412 4e338533 Michael Hanselmann
                                        prev_job_info, prev_log_serial)
1413 4e338533 Michael Hanselmann
1414 4e338533 Michael Hanselmann
  def QueryJobs(self, job_ids, fields):
1415 4e338533 Michael Hanselmann
    """Returns the selected fields for the selected job IDs.
1416 4e338533 Michael Hanselmann

1417 4e338533 Michael Hanselmann
    """
1418 4e338533 Michael Hanselmann
    return self.cl.QueryJobs(job_ids, fields)
1419 4e338533 Michael Hanselmann
1420 4e338533 Michael Hanselmann
1421 4e338533 Michael Hanselmann
class FeedbackFnJobPollReportCb(JobPollReportCbBase):
1422 4e338533 Michael Hanselmann
  def __init__(self, feedback_fn):
1423 4e338533 Michael Hanselmann
    """Initializes this class.
1424 4e338533 Michael Hanselmann

1425 4e338533 Michael Hanselmann
    """
1426 4e338533 Michael Hanselmann
    JobPollReportCbBase.__init__(self)
1427 4e338533 Michael Hanselmann
1428 4e338533 Michael Hanselmann
    self.feedback_fn = feedback_fn
1429 4e338533 Michael Hanselmann
1430 4e338533 Michael Hanselmann
    assert callable(feedback_fn)
1431 4e338533 Michael Hanselmann
1432 4e338533 Michael Hanselmann
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
1433 4e338533 Michael Hanselmann
    """Handles a log message.
1434 4e338533 Michael Hanselmann

1435 4e338533 Michael Hanselmann
    """
1436 4e338533 Michael Hanselmann
    self.feedback_fn((timestamp, log_type, log_msg))
1437 4e338533 Michael Hanselmann
1438 4e338533 Michael Hanselmann
  def ReportNotChanged(self, job_id, status):
1439 4e338533 Michael Hanselmann
    """Called if a job hasn't changed in a while.
1440 4e338533 Michael Hanselmann

1441 4e338533 Michael Hanselmann
    """
1442 4e338533 Michael Hanselmann
    # Ignore
1443 4e338533 Michael Hanselmann
1444 4e338533 Michael Hanselmann
1445 4e338533 Michael Hanselmann
class StdioJobPollReportCb(JobPollReportCbBase):
1446 4e338533 Michael Hanselmann
  def __init__(self):
1447 4e338533 Michael Hanselmann
    """Initializes this class.
1448 4e338533 Michael Hanselmann

1449 4e338533 Michael Hanselmann
    """
1450 4e338533 Michael Hanselmann
    JobPollReportCbBase.__init__(self)
1451 4e338533 Michael Hanselmann
1452 4e338533 Michael Hanselmann
    self.notified_queued = False
1453 4e338533 Michael Hanselmann
    self.notified_waitlock = False
1454 4e338533 Michael Hanselmann
1455 4e338533 Michael Hanselmann
  def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
1456 4e338533 Michael Hanselmann
    """Handles a log message.
1457 4e338533 Michael Hanselmann

1458 4e338533 Michael Hanselmann
    """
1459 4e338533 Michael Hanselmann
    ToStdout("%s %s", time.ctime(utils.MergeTime(timestamp)),
1460 4e338533 Michael Hanselmann
             utils.SafeEncode(log_msg))
1461 4e338533 Michael Hanselmann
1462 4e338533 Michael Hanselmann
  def ReportNotChanged(self, job_id, status):
1463 4e338533 Michael Hanselmann
    """Called if a job hasn't changed in a while.
1464 4e338533 Michael Hanselmann

1465 4e338533 Michael Hanselmann
    """
1466 4e338533 Michael Hanselmann
    if status is None:
1467 4e338533 Michael Hanselmann
      return
1468 4e338533 Michael Hanselmann
1469 4e338533 Michael Hanselmann
    if status == constants.JOB_STATUS_QUEUED and not self.notified_queued:
1470 4e338533 Michael Hanselmann
      ToStderr("Job %s is waiting in queue", job_id)
1471 4e338533 Michael Hanselmann
      self.notified_queued = True
1472 4e338533 Michael Hanselmann
1473 4e338533 Michael Hanselmann
    elif status == constants.JOB_STATUS_WAITLOCK and not self.notified_waitlock:
1474 4e338533 Michael Hanselmann
      ToStderr("Job %s is trying to acquire all necessary locks", job_id)
1475 4e338533 Michael Hanselmann
      self.notified_waitlock = True
1476 4e338533 Michael Hanselmann
1477 4e338533 Michael Hanselmann
1478 4e338533 Michael Hanselmann
def PollJob(job_id, cl=None, feedback_fn=None):
1479 4e338533 Michael Hanselmann
  """Function to poll for the result of a job.
1480 4e338533 Michael Hanselmann

1481 4e338533 Michael Hanselmann
  @type job_id: job identified
1482 4e338533 Michael Hanselmann
  @param job_id: the job to poll for results
1483 4e338533 Michael Hanselmann
  @type cl: luxi.Client
1484 4e338533 Michael Hanselmann
  @param cl: the luxi client to use for communicating with the master;
1485 4e338533 Michael Hanselmann
             if None, a new client will be created
1486 4e338533 Michael Hanselmann

1487 4e338533 Michael Hanselmann
  """
1488 4e338533 Michael Hanselmann
  if cl is None:
1489 4e338533 Michael Hanselmann
    cl = GetClient()
1490 4e338533 Michael Hanselmann
1491 4e338533 Michael Hanselmann
  if feedback_fn:
1492 4e338533 Michael Hanselmann
    reporter = FeedbackFnJobPollReportCb(feedback_fn)
1493 0bbe448c Michael Hanselmann
  else:
1494 4e338533 Michael Hanselmann
    reporter = StdioJobPollReportCb()
1495 4e338533 Michael Hanselmann
1496 4e338533 Michael Hanselmann
  return GenericPollJob(job_id, _LuxiJobPollCb(cl), reporter)
1497 ceab32dd Iustin Pop
1498 ceab32dd Iustin Pop
1499 293ba2d8 Iustin Pop
def SubmitOpCode(op, cl=None, feedback_fn=None, opts=None):
1500 0a1e74d9 Iustin Pop
  """Legacy function to submit an opcode.
1501 0a1e74d9 Iustin Pop

1502 0a1e74d9 Iustin Pop
  This is just a simple wrapper over the construction of the processor
1503 0a1e74d9 Iustin Pop
  instance. It should be extended to better handle feedback and
1504 0a1e74d9 Iustin Pop
  interaction functions.
1505 0a1e74d9 Iustin Pop

1506 0a1e74d9 Iustin Pop
  """
1507 0a1e74d9 Iustin Pop
  if cl is None:
1508 0a1e74d9 Iustin Pop
    cl = GetClient()
1509 0a1e74d9 Iustin Pop
1510 293ba2d8 Iustin Pop
  SetGenericOpcodeOpts([op], opts)
1511 293ba2d8 Iustin Pop
1512 0a1e74d9 Iustin Pop
  job_id = SendJob([op], cl)
1513 0a1e74d9 Iustin Pop
1514 53c04d04 Iustin Pop
  op_results = PollJob(job_id, cl=cl, feedback_fn=feedback_fn)
1515 53c04d04 Iustin Pop
1516 53c04d04 Iustin Pop
  return op_results[0]
1517 0a1e74d9 Iustin Pop
1518 0a1e74d9 Iustin Pop
1519 94428652 Iustin Pop
def SubmitOrSend(op, opts, cl=None, feedback_fn=None):
1520 94428652 Iustin Pop
  """Wrapper around SubmitOpCode or SendJob.
1521 94428652 Iustin Pop

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

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

1530 94428652 Iustin Pop
  """
1531 94428652 Iustin Pop
  if opts and opts.submit_only:
1532 293ba2d8 Iustin Pop
    job = [op]
1533 293ba2d8 Iustin Pop
    SetGenericOpcodeOpts(job, opts)
1534 293ba2d8 Iustin Pop
    job_id = SendJob(job, cl=cl)
1535 e9d741b6 Iustin Pop
    raise JobSubmittedException(job_id)
1536 94428652 Iustin Pop
  else:
1537 293ba2d8 Iustin Pop
    return SubmitOpCode(op, cl=cl, feedback_fn=feedback_fn, opts=opts)
1538 293ba2d8 Iustin Pop
1539 293ba2d8 Iustin Pop
1540 293ba2d8 Iustin Pop
def SetGenericOpcodeOpts(opcode_list, options):
1541 293ba2d8 Iustin Pop
  """Processor for generic options.
1542 293ba2d8 Iustin Pop

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

1546 293ba2d8 Iustin Pop
  @param opcode_list: list of opcodes
1547 293ba2d8 Iustin Pop
  @param options: command line options or None
1548 293ba2d8 Iustin Pop
  @return: None (in-place modification)
1549 293ba2d8 Iustin Pop

1550 293ba2d8 Iustin Pop
  """
1551 293ba2d8 Iustin Pop
  if not options:
1552 293ba2d8 Iustin Pop
    return
1553 293ba2d8 Iustin Pop
  for op in opcode_list:
1554 293ba2d8 Iustin Pop
    op.dry_run = options.dry_run
1555 293ba2d8 Iustin Pop
    op.debug_level = options.debug
1556 94428652 Iustin Pop
1557 94428652 Iustin Pop
1558 af30b2fd Michael Hanselmann
def GetClient():
1559 af30b2fd Michael Hanselmann
  # TODO: Cache object?
1560 b33e986b Iustin Pop
  try:
1561 b33e986b Iustin Pop
    client = luxi.Client()
1562 b33e986b Iustin Pop
  except luxi.NoMasterError:
1563 d9a51679 Michael Hanselmann
    ss = ssconf.SimpleStore()
1564 d9a51679 Michael Hanselmann
1565 d9a51679 Michael Hanselmann
    # Try to read ssconf file
1566 d9a51679 Michael Hanselmann
    try:
1567 d9a51679 Michael Hanselmann
      ss.GetMasterNode()
1568 d9a51679 Michael Hanselmann
    except errors.ConfigurationError:
1569 d9a51679 Michael Hanselmann
      raise errors.OpPrereqError("Cluster not initialized or this machine is"
1570 d9a51679 Michael Hanselmann
                                 " not part of a cluster")
1571 d9a51679 Michael Hanselmann
1572 d9a51679 Michael Hanselmann
    master, myself = ssconf.GetMasterAndMyself(ss=ss)
1573 b33e986b Iustin Pop
    if master != myself:
1574 b33e986b Iustin Pop
      raise errors.OpPrereqError("This is not the master node, please connect"
1575 b33e986b Iustin Pop
                                 " to node '%s' and rerun the command" %
1576 b33e986b Iustin Pop
                                 master)
1577 d9a51679 Michael Hanselmann
    raise
1578 b33e986b Iustin Pop
  return client
1579 af30b2fd Michael Hanselmann
1580 af30b2fd Michael Hanselmann
1581 73702ee7 Iustin Pop
def FormatError(err):
1582 73702ee7 Iustin Pop
  """Return a formatted error message for a given error.
1583 73702ee7 Iustin Pop

1584 73702ee7 Iustin Pop
  This function takes an exception instance and returns a tuple
1585 73702ee7 Iustin Pop
  consisting of two values: first, the recommended exit code, and
1586 73702ee7 Iustin Pop
  second, a string describing the error message (not
1587 73702ee7 Iustin Pop
  newline-terminated).
1588 73702ee7 Iustin Pop

1589 73702ee7 Iustin Pop
  """
1590 73702ee7 Iustin Pop
  retcode = 1
1591 73702ee7 Iustin Pop
  obuf = StringIO()
1592 e2e521d0 Iustin Pop
  msg = str(err)
1593 73702ee7 Iustin Pop
  if isinstance(err, errors.ConfigurationError):
1594 e2e521d0 Iustin Pop
    txt = "Corrupt configuration file: %s" % msg
1595 46fbdd04 Iustin Pop
    logging.error(txt)
1596 e2e521d0 Iustin Pop
    obuf.write(txt + "\n")
1597 73702ee7 Iustin Pop
    obuf.write("Aborting.")
1598 73702ee7 Iustin Pop
    retcode = 2
1599 73702ee7 Iustin Pop
  elif isinstance(err, errors.HooksAbort):
1600 73702ee7 Iustin Pop
    obuf.write("Failure: hooks execution failed:\n")
1601 73702ee7 Iustin Pop
    for node, script, out in err.args[0]:
1602 73702ee7 Iustin Pop
      if out:
1603 73702ee7 Iustin Pop
        obuf.write("  node: %s, script: %s, output: %s\n" %
1604 73702ee7 Iustin Pop
                   (node, script, out))
1605 73702ee7 Iustin Pop
      else:
1606 73702ee7 Iustin Pop
        obuf.write("  node: %s, script: %s (no output)\n" %
1607 73702ee7 Iustin Pop
                   (node, script))
1608 73702ee7 Iustin Pop
  elif isinstance(err, errors.HooksFailure):
1609 e2e521d0 Iustin Pop
    obuf.write("Failure: hooks general failure: %s" % msg)
1610 73702ee7 Iustin Pop
  elif isinstance(err, errors.ResolverError):
1611 73702ee7 Iustin Pop
    this_host = utils.HostInfo.SysName()
1612 73702ee7 Iustin Pop
    if err.args[0] == this_host:
1613 73702ee7 Iustin Pop
      msg = "Failure: can't resolve my own hostname ('%s')"
1614 73702ee7 Iustin Pop
    else:
1615 73702ee7 Iustin Pop
      msg = "Failure: can't resolve hostname '%s'"
1616 73702ee7 Iustin Pop
    obuf.write(msg % err.args[0])
1617 73702ee7 Iustin Pop
  elif isinstance(err, errors.OpPrereqError):
1618 5c983ee5 Iustin Pop
    if len(err.args) == 2:
1619 5c983ee5 Iustin Pop
      obuf.write("Failure: prerequisites not met for this"
1620 5c983ee5 Iustin Pop
               " operation:\nerror type: %s, error details:\n%s" %
1621 5c983ee5 Iustin Pop
                 (err.args[1], err.args[0]))
1622 5c983ee5 Iustin Pop
    else:
1623 5c983ee5 Iustin Pop
      obuf.write("Failure: prerequisites not met for this"
1624 5c983ee5 Iustin Pop
                 " operation:\n%s" % msg)
1625 73702ee7 Iustin Pop
  elif isinstance(err, errors.OpExecError):
1626 e2e521d0 Iustin Pop
    obuf.write("Failure: command execution error:\n%s" % msg)
1627 73702ee7 Iustin Pop
  elif isinstance(err, errors.TagError):
1628 e2e521d0 Iustin Pop
    obuf.write("Failure: invalid tag(s) given:\n%s" % msg)
1629 686d7433 Iustin Pop
  elif isinstance(err, errors.JobQueueDrainError):
1630 686d7433 Iustin Pop
    obuf.write("Failure: the job queue is marked for drain and doesn't"
1631 686d7433 Iustin Pop
               " accept new requests\n")
1632 f87b405e Michael Hanselmann
  elif isinstance(err, errors.JobQueueFull):
1633 f87b405e Michael Hanselmann
    obuf.write("Failure: the job queue is full and doesn't accept new"
1634 f87b405e Michael Hanselmann
               " job submissions until old jobs are archived\n")
1635 a5728081 Guido Trotter
  elif isinstance(err, errors.TypeEnforcementError):
1636 a5728081 Guido Trotter
    obuf.write("Parameter Error: %s" % msg)
1637 c1ce76bb Iustin Pop
  elif isinstance(err, errors.ParameterError):
1638 c1ce76bb Iustin Pop
    obuf.write("Failure: unknown/wrong parameter name '%s'" % msg)
1639 03a8dbdc Iustin Pop
  elif isinstance(err, luxi.NoMasterError):
1640 03a8dbdc Iustin Pop
    obuf.write("Cannot communicate with the master daemon.\nIs it running"
1641 082c5adb Michael Hanselmann
               " and listening for connections?")
1642 03a8dbdc Iustin Pop
  elif isinstance(err, luxi.TimeoutError):
1643 03a8dbdc Iustin Pop
    obuf.write("Timeout while talking to the master daemon. Error:\n"
1644 03a8dbdc Iustin Pop
               "%s" % msg)
1645 03a8dbdc Iustin Pop
  elif isinstance(err, luxi.ProtocolError):
1646 03a8dbdc Iustin Pop
    obuf.write("Unhandled protocol error while talking to the master daemon:\n"
1647 03a8dbdc Iustin Pop
               "%s" % msg)
1648 797506fc Michael Hanselmann
  elif isinstance(err, errors.GenericError):
1649 797506fc Michael Hanselmann
    obuf.write("Unhandled Ganeti error: %s" % msg)
1650 e9d741b6 Iustin Pop
  elif isinstance(err, JobSubmittedException):
1651 e9d741b6 Iustin Pop
    obuf.write("JobID: %s\n" % err.args[0])
1652 e9d741b6 Iustin Pop
    retcode = 0
1653 73702ee7 Iustin Pop
  else:
1654 e2e521d0 Iustin Pop
    obuf.write("Unhandled exception: %s" % msg)
1655 73702ee7 Iustin Pop
  return retcode, obuf.getvalue().rstrip('\n')
1656 73702ee7 Iustin Pop
1657 73702ee7 Iustin Pop
1658 de47cf8f Guido Trotter
def GenericMain(commands, override=None, aliases=None):
1659 a8083063 Iustin Pop
  """Generic main function for all the gnt-* commands.
1660 a8083063 Iustin Pop

1661 334d1483 Iustin Pop
  Arguments:
1662 334d1483 Iustin Pop
    - commands: a dictionary with a special structure, see the design doc
1663 334d1483 Iustin Pop
                for command line handling.
1664 334d1483 Iustin Pop
    - override: if not None, we expect a dictionary with keys that will
1665 334d1483 Iustin Pop
                override command line options; this can be used to pass
1666 334d1483 Iustin Pop
                options from the scripts to generic functions
1667 de47cf8f Guido Trotter
    - aliases: dictionary with command aliases {'alias': 'target, ...}
1668 a8083063 Iustin Pop

1669 a8083063 Iustin Pop
  """
1670 a8083063 Iustin Pop
  # save the program name and the entire command line for later logging
1671 a8083063 Iustin Pop
  if sys.argv:
1672 a8083063 Iustin Pop
    binary = os.path.basename(sys.argv[0]) or sys.argv[0]
1673 a8083063 Iustin Pop
    if len(sys.argv) >= 2:
1674 a8083063 Iustin Pop
      binary += " " + sys.argv[1]
1675 a8083063 Iustin Pop
      old_cmdline = " ".join(sys.argv[2:])
1676 a8083063 Iustin Pop
    else:
1677 a8083063 Iustin Pop
      old_cmdline = ""
1678 a8083063 Iustin Pop
  else:
1679 a8083063 Iustin Pop
    binary = "<unknown program>"
1680 a8083063 Iustin Pop
    old_cmdline = ""
1681 a8083063 Iustin Pop
1682 de47cf8f Guido Trotter
  if aliases is None:
1683 de47cf8f Guido Trotter
    aliases = {}
1684 de47cf8f Guido Trotter
1685 3126878d Guido Trotter
  try:
1686 3126878d Guido Trotter
    func, options, args = _ParseArgs(sys.argv, commands, aliases)
1687 3126878d Guido Trotter
  except errors.ParameterError, err:
1688 3126878d Guido Trotter
    result, err_msg = FormatError(err)
1689 3126878d Guido Trotter
    ToStderr(err_msg)
1690 3126878d Guido Trotter
    return 1
1691 3126878d Guido Trotter
1692 a8083063 Iustin Pop
  if func is None: # parse error
1693 a8083063 Iustin Pop
    return 1
1694 a8083063 Iustin Pop
1695 334d1483 Iustin Pop
  if override is not None:
1696 334d1483 Iustin Pop
    for key, val in override.iteritems():
1697 334d1483 Iustin Pop
      setattr(options, key, val)
1698 334d1483 Iustin Pop
1699 82d9caef Iustin Pop
  utils.SetupLogging(constants.LOG_COMMANDS, debug=options.debug,
1700 82d9caef Iustin Pop
                     stderr_logging=True, program=binary)
1701 a8083063 Iustin Pop
1702 a8083063 Iustin Pop
  if old_cmdline:
1703 46fbdd04 Iustin Pop
    logging.info("run with arguments '%s'", old_cmdline)
1704 a8083063 Iustin Pop
  else:
1705 46fbdd04 Iustin Pop
    logging.info("run with no arguments")
1706 a8083063 Iustin Pop
1707 a8083063 Iustin Pop
  try:
1708 a4af651e Iustin Pop
    result = func(options, args)
1709 d8353c3a Iustin Pop
  except (errors.GenericError, luxi.ProtocolError,
1710 d8353c3a Iustin Pop
          JobSubmittedException), err:
1711 a4af651e Iustin Pop
    result, err_msg = FormatError(err)
1712 5bbd3f7f Michael Hanselmann
    logging.exception("Error during command processing")
1713 46fbdd04 Iustin Pop
    ToStderr(err_msg)
1714 a8083063 Iustin Pop
1715 a8083063 Iustin Pop
  return result
1716 137161c9 Michael Hanselmann
1717 137161c9 Michael Hanselmann
1718 d77490c5 Iustin Pop
def GenericInstanceCreate(mode, opts, args):
1719 d77490c5 Iustin Pop
  """Add an instance to the cluster via either creation or import.
1720 d77490c5 Iustin Pop

1721 d77490c5 Iustin Pop
  @param mode: constants.INSTANCE_CREATE or constants.INSTANCE_IMPORT
1722 d77490c5 Iustin Pop
  @param opts: the command line options selected by the user
1723 d77490c5 Iustin Pop
  @type args: list
1724 d77490c5 Iustin Pop
  @param args: should contain only one element, the new instance name
1725 d77490c5 Iustin Pop
  @rtype: int
1726 d77490c5 Iustin Pop
  @return: the desired exit code
1727 d77490c5 Iustin Pop

1728 d77490c5 Iustin Pop
  """
1729 d77490c5 Iustin Pop
  instance = args[0]
1730 d77490c5 Iustin Pop
1731 d77490c5 Iustin Pop
  (pnode, snode) = SplitNodeOption(opts.node)
1732 d77490c5 Iustin Pop
1733 d77490c5 Iustin Pop
  hypervisor = None
1734 d77490c5 Iustin Pop
  hvparams = {}
1735 d77490c5 Iustin Pop
  if opts.hypervisor:
1736 d77490c5 Iustin Pop
    hypervisor, hvparams = opts.hypervisor
1737 d77490c5 Iustin Pop
1738 d77490c5 Iustin Pop
  if opts.nics:
1739 d77490c5 Iustin Pop
    try:
1740 21bcb9aa Michael Hanselmann
      nic_max = max(int(nidx[0]) + 1 for nidx in opts.nics)
1741 d77490c5 Iustin Pop
    except ValueError, err:
1742 d77490c5 Iustin Pop
      raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err))
1743 d77490c5 Iustin Pop
    nics = [{}] * nic_max
1744 d77490c5 Iustin Pop
    for nidx, ndict in opts.nics:
1745 d77490c5 Iustin Pop
      nidx = int(nidx)
1746 d77490c5 Iustin Pop
      if not isinstance(ndict, dict):
1747 d77490c5 Iustin Pop
        msg = "Invalid nic/%d value: expected dict, got %s" % (nidx, ndict)
1748 d77490c5 Iustin Pop
        raise errors.OpPrereqError(msg)
1749 d77490c5 Iustin Pop
      nics[nidx] = ndict
1750 d77490c5 Iustin Pop
  elif opts.no_nics:
1751 d77490c5 Iustin Pop
    # no nics
1752 d77490c5 Iustin Pop
    nics = []
1753 0af0f641 Iustin Pop
  elif mode == constants.INSTANCE_CREATE:
1754 d77490c5 Iustin Pop
    # default of one nic, all auto
1755 d77490c5 Iustin Pop
    nics = [{}]
1756 0af0f641 Iustin Pop
  else:
1757 0af0f641 Iustin Pop
    # mode == import
1758 0af0f641 Iustin Pop
    nics = []
1759 d77490c5 Iustin Pop
1760 d77490c5 Iustin Pop
  if opts.disk_template == constants.DT_DISKLESS:
1761 d77490c5 Iustin Pop
    if opts.disks or opts.sd_size is not None:
1762 d77490c5 Iustin Pop
      raise errors.OpPrereqError("Diskless instance but disk"
1763 d77490c5 Iustin Pop
                                 " information passed")
1764 d77490c5 Iustin Pop
    disks = []
1765 d77490c5 Iustin Pop
  else:
1766 9b12ed0f Iustin Pop
    if (not opts.disks and not opts.sd_size
1767 9b12ed0f Iustin Pop
        and mode == constants.INSTANCE_CREATE):
1768 d77490c5 Iustin Pop
      raise errors.OpPrereqError("No disk information specified")
1769 d77490c5 Iustin Pop
    if opts.disks and opts.sd_size is not None:
1770 d77490c5 Iustin Pop
      raise errors.OpPrereqError("Please use either the '--disk' or"
1771 d77490c5 Iustin Pop
                                 " '-s' option")
1772 d77490c5 Iustin Pop
    if opts.sd_size is not None:
1773 d77490c5 Iustin Pop
      opts.disks = [(0, {"size": opts.sd_size})]
1774 9b12ed0f Iustin Pop
1775 9b12ed0f Iustin Pop
    if opts.disks:
1776 9b12ed0f Iustin Pop
      try:
1777 9b12ed0f Iustin Pop
        disk_max = max(int(didx[0]) + 1 for didx in opts.disks)
1778 9b12ed0f Iustin Pop
      except ValueError, err:
1779 9b12ed0f Iustin Pop
        raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err))
1780 9b12ed0f Iustin Pop
      disks = [{}] * disk_max
1781 9b12ed0f Iustin Pop
    else:
1782 9b12ed0f Iustin Pop
      disks = []
1783 d77490c5 Iustin Pop
    for didx, ddict in opts.disks:
1784 d77490c5 Iustin Pop
      didx = int(didx)
1785 d77490c5 Iustin Pop
      if not isinstance(ddict, dict):
1786 d77490c5 Iustin Pop
        msg = "Invalid disk/%d value: expected dict, got %s" % (didx, ddict)
1787 d77490c5 Iustin Pop
        raise errors.OpPrereqError(msg)
1788 5029db65 Iustin Pop
      elif "size" in ddict:
1789 5029db65 Iustin Pop
        if "adopt" in ddict:
1790 5029db65 Iustin Pop
          raise errors.OpPrereqError("Only one of 'size' and 'adopt' allowed"
1791 5029db65 Iustin Pop
                                     " (disk %d)" % didx)
1792 5029db65 Iustin Pop
        try:
1793 5029db65 Iustin Pop
          ddict["size"] = utils.ParseUnit(ddict["size"])
1794 5029db65 Iustin Pop
        except ValueError, err:
1795 5029db65 Iustin Pop
          raise errors.OpPrereqError("Invalid disk size for disk %d: %s" %
1796 5029db65 Iustin Pop
                                     (didx, err))
1797 5029db65 Iustin Pop
      elif "adopt" in ddict:
1798 5029db65 Iustin Pop
        if mode == constants.INSTANCE_IMPORT:
1799 5029db65 Iustin Pop
          raise errors.OpPrereqError("Disk adoption not allowed for instance"
1800 5029db65 Iustin Pop
                                     " import")
1801 5029db65 Iustin Pop
        ddict["size"] = 0
1802 5029db65 Iustin Pop
      else:
1803 5029db65 Iustin Pop
        raise errors.OpPrereqError("Missing size or adoption source for"
1804 5029db65 Iustin Pop
                                   " disk %d" % didx)
1805 d77490c5 Iustin Pop
      disks[didx] = ddict
1806 d77490c5 Iustin Pop
1807 d77490c5 Iustin Pop
  utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_TYPES)
1808 d77490c5 Iustin Pop
  utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
1809 d77490c5 Iustin Pop
1810 d77490c5 Iustin Pop
  if mode == constants.INSTANCE_CREATE:
1811 d77490c5 Iustin Pop
    start = opts.start
1812 d77490c5 Iustin Pop
    os_type = opts.os
1813 d77490c5 Iustin Pop
    src_node = None
1814 d77490c5 Iustin Pop
    src_path = None
1815 25a8792c Iustin Pop
    no_install = opts.no_install
1816 e588764d Iustin Pop
    identify_defaults = False
1817 d77490c5 Iustin Pop
  elif mode == constants.INSTANCE_IMPORT:
1818 d77490c5 Iustin Pop
    start = False
1819 d77490c5 Iustin Pop
    os_type = None
1820 d77490c5 Iustin Pop
    src_node = opts.src_node
1821 d77490c5 Iustin Pop
    src_path = opts.src_dir
1822 25a8792c Iustin Pop
    no_install = None
1823 e588764d Iustin Pop
    identify_defaults = opts.identify_defaults
1824 d77490c5 Iustin Pop
  else:
1825 d77490c5 Iustin Pop
    raise errors.ProgrammerError("Invalid creation mode %s" % mode)
1826 d77490c5 Iustin Pop
1827 d77490c5 Iustin Pop
  op = opcodes.OpCreateInstance(instance_name=instance,
1828 d77490c5 Iustin Pop
                                disks=disks,
1829 d77490c5 Iustin Pop
                                disk_template=opts.disk_template,
1830 d77490c5 Iustin Pop
                                nics=nics,
1831 d77490c5 Iustin Pop
                                pnode=pnode, snode=snode,
1832 d77490c5 Iustin Pop
                                ip_check=opts.ip_check,
1833 460d22be Iustin Pop
                                name_check=opts.name_check,
1834 d77490c5 Iustin Pop
                                wait_for_sync=opts.wait_for_sync,
1835 d77490c5 Iustin Pop
                                file_storage_dir=opts.file_storage_dir,
1836 d77490c5 Iustin Pop
                                file_driver=opts.file_driver,
1837 d77490c5 Iustin Pop
                                iallocator=opts.iallocator,
1838 d77490c5 Iustin Pop
                                hypervisor=hypervisor,
1839 d77490c5 Iustin Pop
                                hvparams=hvparams,
1840 d77490c5 Iustin Pop
                                beparams=opts.beparams,
1841 062a7100 Iustin Pop
                                osparams=opts.osparams,
1842 d77490c5 Iustin Pop
                                mode=mode,
1843 d77490c5 Iustin Pop
                                start=start,
1844 d77490c5 Iustin Pop
                                os_type=os_type,
1845 d77490c5 Iustin Pop
                                src_node=src_node,
1846 25a8792c Iustin Pop
                                src_path=src_path,
1847 e588764d Iustin Pop
                                no_install=no_install,
1848 e588764d Iustin Pop
                                identify_defaults=identify_defaults)
1849 d77490c5 Iustin Pop
1850 d77490c5 Iustin Pop
  SubmitOrSend(op, opts)
1851 d77490c5 Iustin Pop
  return 0
1852 d77490c5 Iustin Pop
1853 d77490c5 Iustin Pop
1854 7e49b6ce Michael Hanselmann
class _RunWhileClusterStoppedHelper:
1855 7e49b6ce Michael Hanselmann
  """Helper class for L{RunWhileClusterStopped} to simplify state management
1856 7e49b6ce Michael Hanselmann

1857 7e49b6ce Michael Hanselmann
  """
1858 7e49b6ce Michael Hanselmann
  def __init__(self, feedback_fn, cluster_name, master_node, online_nodes):
1859 7e49b6ce Michael Hanselmann
    """Initializes this class.
1860 7e49b6ce Michael Hanselmann

1861 7e49b6ce Michael Hanselmann
    @type feedback_fn: callable
1862 7e49b6ce Michael Hanselmann
    @param feedback_fn: Feedback function
1863 7e49b6ce Michael Hanselmann
    @type cluster_name: string
1864 7e49b6ce Michael Hanselmann
    @param cluster_name: Cluster name
1865 7e49b6ce Michael Hanselmann
    @type master_node: string
1866 7e49b6ce Michael Hanselmann
    @param master_node Master node name
1867 7e49b6ce Michael Hanselmann
    @type online_nodes: list
1868 7e49b6ce Michael Hanselmann
    @param online_nodes: List of names of online nodes
1869 7e49b6ce Michael Hanselmann

1870 7e49b6ce Michael Hanselmann
    """
1871 7e49b6ce Michael Hanselmann
    self.feedback_fn = feedback_fn
1872 7e49b6ce Michael Hanselmann
    self.cluster_name = cluster_name
1873 7e49b6ce Michael Hanselmann
    self.master_node = master_node
1874 7e49b6ce Michael Hanselmann
    self.online_nodes = online_nodes
1875 7e49b6ce Michael Hanselmann
1876 7e49b6ce Michael Hanselmann
    self.ssh = ssh.SshRunner(self.cluster_name)
1877 7e49b6ce Michael Hanselmann
1878 7e49b6ce Michael Hanselmann
    self.nonmaster_nodes = [name for name in online_nodes
1879 7e49b6ce Michael Hanselmann
                            if name != master_node]
1880 7e49b6ce Michael Hanselmann
1881 7e49b6ce Michael Hanselmann
    assert self.master_node not in self.nonmaster_nodes
1882 7e49b6ce Michael Hanselmann
1883 7e49b6ce Michael Hanselmann
  def _RunCmd(self, node_name, cmd):
1884 7e49b6ce Michael Hanselmann
    """Runs a command on the local or a remote machine.
1885 7e49b6ce Michael Hanselmann

1886 7e49b6ce Michael Hanselmann
    @type node_name: string
1887 7e49b6ce Michael Hanselmann
    @param node_name: Machine name
1888 7e49b6ce Michael Hanselmann
    @type cmd: list
1889 7e49b6ce Michael Hanselmann
    @param cmd: Command
1890 7e49b6ce Michael Hanselmann

1891 7e49b6ce Michael Hanselmann
    """
1892 7e49b6ce Michael Hanselmann
    if node_name is None or node_name == self.master_node:
1893 7e49b6ce Michael Hanselmann
      # No need to use SSH
1894 7e49b6ce Michael Hanselmann
      result = utils.RunCmd(cmd)
1895 7e49b6ce Michael Hanselmann
    else:
1896 7e49b6ce Michael Hanselmann
      result = self.ssh.Run(node_name, "root", utils.ShellQuoteArgs(cmd))
1897 7e49b6ce Michael Hanselmann
1898 7e49b6ce Michael Hanselmann
    if result.failed:
1899 7e49b6ce Michael Hanselmann
      errmsg = ["Failed to run command %s" % result.cmd]
1900 7e49b6ce Michael Hanselmann
      if node_name:
1901 7e49b6ce Michael Hanselmann
        errmsg.append("on node %s" % node_name)
1902 7e49b6ce Michael Hanselmann
      errmsg.append(": exitcode %s and error %s" %
1903 7e49b6ce Michael Hanselmann
                    (result.exit_code, result.output))
1904 7e49b6ce Michael Hanselmann
      raise errors.OpExecError(" ".join(errmsg))
1905 7e49b6ce Michael Hanselmann
1906 7e49b6ce Michael Hanselmann
  def Call(self, fn, *args):
1907 7e49b6ce Michael Hanselmann
    """Call function while all daemons are stopped.
1908 7e49b6ce Michael Hanselmann

1909 7e49b6ce Michael Hanselmann
    @type fn: callable
1910 7e49b6ce Michael Hanselmann
    @param fn: Function to be called
1911 7e49b6ce Michael Hanselmann

1912 7e49b6ce Michael Hanselmann
    """
1913 7e49b6ce Michael Hanselmann
    # Pause watcher by acquiring an exclusive lock on watcher state file
1914 7e49b6ce Michael Hanselmann
    self.feedback_fn("Blocking watcher")
1915 7e49b6ce Michael Hanselmann
    watcher_block = utils.FileLock.Open(constants.WATCHER_STATEFILE)
1916 7e49b6ce Michael Hanselmann
    try:
1917 7e49b6ce Michael Hanselmann
      # TODO: Currently, this just blocks. There's no timeout.
1918 7e49b6ce Michael Hanselmann
      # TODO: Should it be a shared lock?
1919 7e49b6ce Michael Hanselmann
      watcher_block.Exclusive(blocking=True)
1920 7e49b6ce Michael Hanselmann
1921 7e49b6ce Michael Hanselmann
      # Stop master daemons, so that no new jobs can come in and all running
1922 7e49b6ce Michael Hanselmann
      # ones are finished
1923 7e49b6ce Michael Hanselmann
      self.feedback_fn("Stopping master daemons")
1924 7e49b6ce Michael Hanselmann
      self._RunCmd(None, [constants.DAEMON_UTIL, "stop-master"])
1925 7e49b6ce Michael Hanselmann
      try:
1926 7e49b6ce Michael Hanselmann
        # Stop daemons on all nodes
1927 7e49b6ce Michael Hanselmann
        for node_name in self.online_nodes:
1928 7e49b6ce Michael Hanselmann
          self.feedback_fn("Stopping daemons on %s" % node_name)
1929 7e49b6ce Michael Hanselmann
          self._RunCmd(node_name, [constants.DAEMON_UTIL, "stop-all"])
1930 7e49b6ce Michael Hanselmann
1931 7e49b6ce Michael Hanselmann
        # All daemons are shut down now
1932 7e49b6ce Michael Hanselmann
        try:
1933 7e49b6ce Michael Hanselmann
          return fn(self, *args)
1934 d512e84b Michael Hanselmann
        except Exception, err:
1935 d512e84b Michael Hanselmann
          _, errmsg = FormatError(err)
1936 7e49b6ce Michael Hanselmann
          logging.exception("Caught exception")
1937 d512e84b Michael Hanselmann
          self.feedback_fn(errmsg)
1938 7e49b6ce Michael Hanselmann
          raise
1939 7e49b6ce Michael Hanselmann
      finally:
1940 7e49b6ce Michael Hanselmann
        # Start cluster again, master node last
1941 7e49b6ce Michael Hanselmann
        for node_name in self.nonmaster_nodes + [self.master_node]:
1942 7e49b6ce Michael Hanselmann
          self.feedback_fn("Starting daemons on %s" % node_name)
1943 7e49b6ce Michael Hanselmann
          self._RunCmd(node_name, [constants.DAEMON_UTIL, "start-all"])
1944 7e49b6ce Michael Hanselmann
    finally:
1945 7e49b6ce Michael Hanselmann
      # Resume watcher
1946 7e49b6ce Michael Hanselmann
      watcher_block.Close()
1947 7e49b6ce Michael Hanselmann
1948 7e49b6ce Michael Hanselmann
1949 7e49b6ce Michael Hanselmann
def RunWhileClusterStopped(feedback_fn, fn, *args):
1950 7e49b6ce Michael Hanselmann
  """Calls a function while all cluster daemons are stopped.
1951 7e49b6ce Michael Hanselmann

1952 7e49b6ce Michael Hanselmann
  @type feedback_fn: callable
1953 7e49b6ce Michael Hanselmann
  @param feedback_fn: Feedback function
1954 7e49b6ce Michael Hanselmann
  @type fn: callable
1955 7e49b6ce Michael Hanselmann
  @param fn: Function to be called when daemons are stopped
1956 7e49b6ce Michael Hanselmann

1957 7e49b6ce Michael Hanselmann
  """
1958 7e49b6ce Michael Hanselmann
  feedback_fn("Gathering cluster information")
1959 7e49b6ce Michael Hanselmann
1960 7e49b6ce Michael Hanselmann
  # This ensures we're running on the master daemon
1961 7e49b6ce Michael Hanselmann
  cl = GetClient()
1962 7e49b6ce Michael Hanselmann
1963 7e49b6ce Michael Hanselmann
  (cluster_name, master_node) = \
1964 7e49b6ce Michael Hanselmann
    cl.QueryConfigValues(["cluster_name", "master_node"])
1965 7e49b6ce Michael Hanselmann
1966 7e49b6ce Michael Hanselmann
  online_nodes = GetOnlineNodes([], cl=cl)
1967 7e49b6ce Michael Hanselmann
1968 7e49b6ce Michael Hanselmann
  # Don't keep a reference to the client. The master daemon will go away.
1969 7e49b6ce Michael Hanselmann
  del cl
1970 7e49b6ce Michael Hanselmann
1971 7e49b6ce Michael Hanselmann
  assert master_node in online_nodes
1972 7e49b6ce Michael Hanselmann
1973 7e49b6ce Michael Hanselmann
  return _RunWhileClusterStoppedHelper(feedback_fn, cluster_name, master_node,
1974 7e49b6ce Michael Hanselmann
                                       online_nodes).Call(fn, *args)
1975 7e49b6ce Michael Hanselmann
1976 7e49b6ce Michael Hanselmann
1977 16be8703 Iustin Pop
def GenerateTable(headers, fields, separator, data,
1978 9fbfbb7b Iustin Pop
                  numfields=None, unitfields=None,
1979 9fbfbb7b Iustin Pop
                  units=None):
1980 137161c9 Michael Hanselmann
  """Prints a table with headers and different fields.
1981 137161c9 Michael Hanselmann

1982 9fbfbb7b Iustin Pop
  @type headers: dict
1983 9fbfbb7b Iustin Pop
  @param headers: dictionary mapping field names to headers for
1984 9fbfbb7b Iustin Pop
      the table
1985 9fbfbb7b Iustin Pop
  @type fields: list
1986 9fbfbb7b Iustin Pop
  @param fields: the field names corresponding to each row in
1987 9fbfbb7b Iustin Pop
      the data field
1988 9fbfbb7b Iustin Pop
  @param separator: the separator to be used; if this is None,
1989 9fbfbb7b Iustin Pop
      the default 'smart' algorithm is used which computes optimal
1990 9fbfbb7b Iustin Pop
      field width, otherwise just the separator is used between
1991 9fbfbb7b Iustin Pop
      each field
1992 9fbfbb7b Iustin Pop
  @type data: list
1993 9fbfbb7b Iustin Pop
  @param data: a list of lists, each sublist being one row to be output
1994 9fbfbb7b Iustin Pop
  @type numfields: list
1995 9fbfbb7b Iustin Pop
  @param numfields: a list with the fields that hold numeric
1996 9fbfbb7b Iustin Pop
      values and thus should be right-aligned
1997 9fbfbb7b Iustin Pop
  @type unitfields: list
1998 9fbfbb7b Iustin Pop
  @param unitfields: a list with the fields that hold numeric
1999 9fbfbb7b Iustin Pop
      values that should be formatted with the units field
2000 9fbfbb7b Iustin Pop
  @type units: string or None
2001 9fbfbb7b Iustin Pop
  @param units: the units we should use for formatting, or None for
2002 9fbfbb7b Iustin Pop
      automatic choice (human-readable for non-separator usage, otherwise
2003 9fbfbb7b Iustin Pop
      megabytes); this is a one-letter string
2004 137161c9 Michael Hanselmann

2005 137161c9 Michael Hanselmann
  """
2006 9fbfbb7b Iustin Pop
  if units is None:
2007 9fbfbb7b Iustin Pop
    if separator:
2008 9fbfbb7b Iustin Pop
      units = "m"
2009 9fbfbb7b Iustin Pop
    else:
2010 9fbfbb7b Iustin Pop
      units = "h"
2011 9fbfbb7b Iustin Pop
2012 137161c9 Michael Hanselmann
  if numfields is None:
2013 137161c9 Michael Hanselmann
    numfields = []
2014 137161c9 Michael Hanselmann
  if unitfields is None:
2015 137161c9 Michael Hanselmann
    unitfields = []
2016 137161c9 Michael Hanselmann
2017 fe267188 Iustin Pop
  numfields = utils.FieldSet(*numfields)   # pylint: disable-msg=W0142
2018 fe267188 Iustin Pop
  unitfields = utils.FieldSet(*unitfields) # pylint: disable-msg=W0142
2019 00430f8e Iustin Pop
2020 137161c9 Michael Hanselmann
  format_fields = []
2021 137161c9 Michael Hanselmann
  for field in fields:
2022 01ca31ae Iustin Pop
    if headers and field not in headers:
2023 ea5a5b74 Guido Trotter
      # TODO: handle better unknown fields (either revert to old
2024 71c1af58 Iustin Pop
      # style of raising exception, or deal more intelligently with
2025 71c1af58 Iustin Pop
      # variable fields)
2026 71c1af58 Iustin Pop
      headers[field] = field
2027 137161c9 Michael Hanselmann
    if separator is not None:
2028 137161c9 Michael Hanselmann
      format_fields.append("%s")
2029 00430f8e Iustin Pop
    elif numfields.Matches(field):
2030 137161c9 Michael Hanselmann
      format_fields.append("%*s")
2031 137161c9 Michael Hanselmann
    else:
2032 137161c9 Michael Hanselmann
      format_fields.append("%-*s")
2033 137161c9 Michael Hanselmann
2034 137161c9 Michael Hanselmann
  if separator is None:
2035 137161c9 Michael Hanselmann
    mlens = [0 for name in fields]
2036 c04bc777 Iustin Pop
    format_str = ' '.join(format_fields)
2037 137161c9 Michael Hanselmann
  else:
2038 c04bc777 Iustin Pop
    format_str = separator.replace("%", "%%").join(format_fields)
2039 137161c9 Michael Hanselmann
2040 137161c9 Michael Hanselmann
  for row in data:
2041 dcbd6288 Guido Trotter
    if row is None:
2042 dcbd6288 Guido Trotter
      continue
2043 137161c9 Michael Hanselmann
    for idx, val in enumerate(row):
2044 00430f8e Iustin Pop
      if unitfields.Matches(fields[idx]):
2045 137161c9 Michael Hanselmann
        try:
2046 137161c9 Michael Hanselmann
          val = int(val)
2047 691744c4 Iustin Pop
        except (TypeError, ValueError):
2048 137161c9 Michael Hanselmann
          pass
2049 137161c9 Michael Hanselmann
        else:
2050 9fbfbb7b Iustin Pop
          val = row[idx] = utils.FormatUnit(val, units)
2051 01ca31ae Iustin Pop
      val = row[idx] = str(val)
2052 137161c9 Michael Hanselmann
      if separator is None:
2053 137161c9 Michael Hanselmann
        mlens[idx] = max(mlens[idx], len(val))
2054 137161c9 Michael Hanselmann
2055 16be8703 Iustin Pop
  result = []
2056 137161c9 Michael Hanselmann
  if headers:
2057 137161c9 Michael Hanselmann
    args = []
2058 137161c9 Michael Hanselmann
    for idx, name in enumerate(fields):
2059 137161c9 Michael Hanselmann
      hdr = headers[name]
2060 137161c9 Michael Hanselmann
      if separator is None:
2061 137161c9 Michael Hanselmann
        mlens[idx] = max(mlens[idx], len(hdr))
2062 137161c9 Michael Hanselmann
        args.append(mlens[idx])
2063 137161c9 Michael Hanselmann
      args.append(hdr)
2064 c04bc777 Iustin Pop
    result.append(format_str % tuple(args))
2065 137161c9 Michael Hanselmann
2066 ec39d63c Michael Hanselmann
  if separator is None:
2067 ec39d63c Michael Hanselmann
    assert len(mlens) == len(fields)
2068 ec39d63c Michael Hanselmann
2069 ec39d63c Michael Hanselmann
    if fields and not numfields.Matches(fields[-1]):
2070 ec39d63c Michael Hanselmann
      mlens[-1] = 0
2071 ec39d63c Michael Hanselmann
2072 137161c9 Michael Hanselmann
  for line in data:
2073 137161c9 Michael Hanselmann
    args = []
2074 dcbd6288 Guido Trotter
    if line is None:
2075 dcbd6288 Guido Trotter
      line = ['-' for _ in fields]
2076 f1501b3f Michael Hanselmann
    for idx in range(len(fields)):
2077 137161c9 Michael Hanselmann
      if separator is None:
2078 137161c9 Michael Hanselmann
        args.append(mlens[idx])
2079 137161c9 Michael Hanselmann
      args.append(line[idx])
2080 c04bc777 Iustin Pop
    result.append(format_str % tuple(args))
2081 16be8703 Iustin Pop
2082 16be8703 Iustin Pop
  return result
2083 3386e7a9 Iustin Pop
2084 3386e7a9 Iustin Pop
2085 3386e7a9 Iustin Pop
def FormatTimestamp(ts):
2086 3386e7a9 Iustin Pop
  """Formats a given timestamp.
2087 3386e7a9 Iustin Pop

2088 3386e7a9 Iustin Pop
  @type ts: timestamp
2089 3386e7a9 Iustin Pop
  @param ts: a timeval-type timestamp, a tuple of seconds and microseconds
2090 3386e7a9 Iustin Pop

2091 3386e7a9 Iustin Pop
  @rtype: string
2092 5fcc718f Iustin Pop
  @return: a string with the formatted timestamp
2093 3386e7a9 Iustin Pop

2094 3386e7a9 Iustin Pop
  """
2095 e0ec0ff6 Iustin Pop
  if not isinstance (ts, (tuple, list)) or len(ts) != 2:
2096 e0ec0ff6 Iustin Pop
    return '?'
2097 3386e7a9 Iustin Pop
  sec, usec = ts
2098 3386e7a9 Iustin Pop
  return time.strftime("%F %T", time.localtime(sec)) + ".%06d" % usec
2099 2241e2b9 Iustin Pop
2100 2241e2b9 Iustin Pop
2101 2241e2b9 Iustin Pop
def ParseTimespec(value):
2102 2241e2b9 Iustin Pop
  """Parse a time specification.
2103 2241e2b9 Iustin Pop

2104 2241e2b9 Iustin Pop
  The following suffixed will be recognized:
2105 2241e2b9 Iustin Pop

2106 2241e2b9 Iustin Pop
    - s: seconds
2107 2241e2b9 Iustin Pop
    - m: minutes
2108 2241e2b9 Iustin Pop
    - h: hours
2109 2241e2b9 Iustin Pop
    - d: day
2110 2241e2b9 Iustin Pop
    - w: weeks
2111 2241e2b9 Iustin Pop

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

2114 2241e2b9 Iustin Pop
  """
2115 2241e2b9 Iustin Pop
  value = str(value)
2116 2241e2b9 Iustin Pop
  if not value:
2117 2241e2b9 Iustin Pop
    raise errors.OpPrereqError("Empty time specification passed")
2118 2241e2b9 Iustin Pop
  suffix_map = {
2119 2241e2b9 Iustin Pop
    's': 1,
2120 2241e2b9 Iustin Pop
    'm': 60,
2121 2241e2b9 Iustin Pop
    'h': 3600,
2122 2241e2b9 Iustin Pop
    'd': 86400,
2123 2241e2b9 Iustin Pop
    'w': 604800,
2124 2241e2b9 Iustin Pop
    }
2125 2241e2b9 Iustin Pop
  if value[-1] not in suffix_map:
2126 2241e2b9 Iustin Pop
    try:
2127 2241e2b9 Iustin Pop
      value = int(value)
2128 691744c4 Iustin Pop
    except (TypeError, ValueError):
2129 2241e2b9 Iustin Pop
      raise errors.OpPrereqError("Invalid time specification '%s'" % value)
2130 2241e2b9 Iustin Pop
  else:
2131 2241e2b9 Iustin Pop
    multiplier = suffix_map[value[-1]]
2132 2241e2b9 Iustin Pop
    value = value[:-1]
2133 2241e2b9 Iustin Pop
    if not value: # no data left after stripping the suffix
2134 2241e2b9 Iustin Pop
      raise errors.OpPrereqError("Invalid time specification (only"
2135 2241e2b9 Iustin Pop
                                 " suffix passed)")
2136 2241e2b9 Iustin Pop
    try:
2137 2241e2b9 Iustin Pop
      value = int(value) * multiplier
2138 691744c4 Iustin Pop
    except (TypeError, ValueError):
2139 2241e2b9 Iustin Pop
      raise errors.OpPrereqError("Invalid time specification '%s'" % value)
2140 2241e2b9 Iustin Pop
  return value
2141 46fbdd04 Iustin Pop
2142 46fbdd04 Iustin Pop
2143 e9e26bb3 Iustin Pop
def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False,
2144 e9e26bb3 Iustin Pop
                   filter_master=False):
2145 4040a784 Iustin Pop
  """Returns the names of online nodes.
2146 4040a784 Iustin Pop

2147 4040a784 Iustin Pop
  This function will also log a warning on stderr with the names of
2148 4040a784 Iustin Pop
  the online nodes.
2149 4040a784 Iustin Pop

2150 4040a784 Iustin Pop
  @param nodes: if not empty, use only this subset of nodes (minus the
2151 4040a784 Iustin Pop
      offline ones)
2152 4040a784 Iustin Pop
  @param cl: if not None, luxi client to use
2153 4040a784 Iustin Pop
  @type nowarn: boolean
2154 4040a784 Iustin Pop
  @param nowarn: by default, this function will output a note with the
2155 4040a784 Iustin Pop
      offline nodes that are skipped; if this parameter is True the
2156 4040a784 Iustin Pop
      note is not displayed
2157 e9e26bb3 Iustin Pop
  @type secondary_ips: boolean
2158 e9e26bb3 Iustin Pop
  @param secondary_ips: if True, return the secondary IPs instead of the
2159 e9e26bb3 Iustin Pop
      names, useful for doing network traffic over the replication interface
2160 e9e26bb3 Iustin Pop
      (if any)
2161 e9e26bb3 Iustin Pop
  @type filter_master: boolean
2162 e9e26bb3 Iustin Pop
  @param filter_master: if True, do not return the master node in the list
2163 e9e26bb3 Iustin Pop
      (useful in coordination with secondary_ips where we cannot check our
2164 e9e26bb3 Iustin Pop
      node name against the list)
2165 4040a784 Iustin Pop

2166 4040a784 Iustin Pop
  """
2167 4040a784 Iustin Pop
  if cl is None:
2168 4040a784 Iustin Pop
    cl = GetClient()
2169 4040a784 Iustin Pop
2170 e9e26bb3 Iustin Pop
  if secondary_ips:
2171 e9e26bb3 Iustin Pop
    name_idx = 2
2172 e9e26bb3 Iustin Pop
  else:
2173 e9e26bb3 Iustin Pop
    name_idx = 0
2174 e9e26bb3 Iustin Pop
2175 e9e26bb3 Iustin Pop
  if filter_master:
2176 e9e26bb3 Iustin Pop
    master_node = cl.QueryConfigValues(["master_node"])[0]
2177 e9e26bb3 Iustin Pop
    filter_fn = lambda x: x != master_node
2178 e9e26bb3 Iustin Pop
  else:
2179 e9e26bb3 Iustin Pop
    filter_fn = lambda _: True
2180 e9e26bb3 Iustin Pop
2181 e9e26bb3 Iustin Pop
  result = cl.QueryNodes(names=nodes, fields=["name", "offline", "sip"],
2182 2e7b8369 Iustin Pop
                         use_locking=False)
2183 4040a784 Iustin Pop
  offline = [row[0] for row in result if row[1]]
2184 4040a784 Iustin Pop
  if offline and not nowarn:
2185 1f864b60 Iustin Pop
    ToStderr("Note: skipping offline node(s): %s" % utils.CommaJoin(offline))
2186 e9e26bb3 Iustin Pop
  return [row[name_idx] for row in result if not row[1] and filter_fn(row[0])]
2187 4040a784 Iustin Pop
2188 4040a784 Iustin Pop
2189 46fbdd04 Iustin Pop
def _ToStream(stream, txt, *args):
2190 46fbdd04 Iustin Pop
  """Write a message to a stream, bypassing the logging system
2191 46fbdd04 Iustin Pop

2192 46fbdd04 Iustin Pop
  @type stream: file object
2193 46fbdd04 Iustin Pop
  @param stream: the file to which we should write
2194 46fbdd04 Iustin Pop
  @type txt: str
2195 46fbdd04 Iustin Pop
  @param txt: the message
2196 46fbdd04 Iustin Pop

2197 46fbdd04 Iustin Pop
  """
2198 46fbdd04 Iustin Pop
  if args:
2199 46fbdd04 Iustin Pop
    args = tuple(args)
2200 46fbdd04 Iustin Pop
    stream.write(txt % args)
2201 46fbdd04 Iustin Pop
  else:
2202 46fbdd04 Iustin Pop
    stream.write(txt)
2203 46fbdd04 Iustin Pop
  stream.write('\n')
2204 46fbdd04 Iustin Pop
  stream.flush()
2205 46fbdd04 Iustin Pop
2206 46fbdd04 Iustin Pop
2207 46fbdd04 Iustin Pop
def ToStdout(txt, *args):
2208 46fbdd04 Iustin Pop
  """Write a message to stdout only, bypassing the logging system
2209 46fbdd04 Iustin Pop

2210 46fbdd04 Iustin Pop
  This is just a wrapper over _ToStream.
2211 46fbdd04 Iustin Pop

2212 46fbdd04 Iustin Pop
  @type txt: str
2213 46fbdd04 Iustin Pop
  @param txt: the message
2214 46fbdd04 Iustin Pop

2215 46fbdd04 Iustin Pop
  """
2216 46fbdd04 Iustin Pop
  _ToStream(sys.stdout, txt, *args)
2217 46fbdd04 Iustin Pop
2218 46fbdd04 Iustin Pop
2219 46fbdd04 Iustin Pop
def ToStderr(txt, *args):
2220 46fbdd04 Iustin Pop
  """Write a message to stderr only, bypassing the logging system
2221 46fbdd04 Iustin Pop

2222 46fbdd04 Iustin Pop
  This is just a wrapper over _ToStream.
2223 46fbdd04 Iustin Pop

2224 46fbdd04 Iustin Pop
  @type txt: str
2225 46fbdd04 Iustin Pop
  @param txt: the message
2226 46fbdd04 Iustin Pop

2227 46fbdd04 Iustin Pop
  """
2228 46fbdd04 Iustin Pop
  _ToStream(sys.stderr, txt, *args)
2229 479636a3 Iustin Pop
2230 479636a3 Iustin Pop
2231 479636a3 Iustin Pop
class JobExecutor(object):
2232 479636a3 Iustin Pop
  """Class which manages the submission and execution of multiple jobs.
2233 479636a3 Iustin Pop

2234 479636a3 Iustin Pop
  Note that instances of this class should not be reused between
2235 479636a3 Iustin Pop
  GetResults() calls.
2236 479636a3 Iustin Pop

2237 479636a3 Iustin Pop
  """
2238 919ca415 Iustin Pop
  def __init__(self, cl=None, verbose=True, opts=None, feedback_fn=None):
2239 479636a3 Iustin Pop
    self.queue = []
2240 479636a3 Iustin Pop
    if cl is None:
2241 479636a3 Iustin Pop
      cl = GetClient()
2242 479636a3 Iustin Pop
    self.cl = cl
2243 479636a3 Iustin Pop
    self.verbose = verbose
2244 23b4b983 Iustin Pop
    self.jobs = []
2245 cff5fa7f Iustin Pop
    self.opts = opts
2246 919ca415 Iustin Pop
    self.feedback_fn = feedback_fn
2247 479636a3 Iustin Pop
2248 479636a3 Iustin Pop
  def QueueJob(self, name, *ops):
2249 23b4b983 Iustin Pop
    """Record a job for later submit.
2250 479636a3 Iustin Pop

2251 479636a3 Iustin Pop
    @type name: string
2252 479636a3 Iustin Pop
    @param name: a description of the job, will be used in WaitJobSet
2253 479636a3 Iustin Pop
    """
2254 cff5fa7f Iustin Pop
    SetGenericOpcodeOpts(ops, self.opts)
2255 23b4b983 Iustin Pop
    self.queue.append((name, ops))
2256 23b4b983 Iustin Pop
2257 66ecc479 Guido Trotter
  def SubmitPending(self, each=False):
2258 23b4b983 Iustin Pop
    """Submit all pending jobs.
2259 23b4b983 Iustin Pop

2260 23b4b983 Iustin Pop
    """
2261 66ecc479 Guido Trotter
    if each:
2262 66ecc479 Guido Trotter
      results = []
2263 66ecc479 Guido Trotter
      for row in self.queue:
2264 66ecc479 Guido Trotter
        # SubmitJob will remove the success status, but raise an exception if
2265 66ecc479 Guido Trotter
        # the submission fails, so we'll notice that anyway.
2266 66ecc479 Guido Trotter
        results.append([True, self.cl.SubmitJob(row[1])])
2267 66ecc479 Guido Trotter
    else:
2268 66ecc479 Guido Trotter
      results = self.cl.SubmitManyJobs([row[1] for row in self.queue])
2269 5299e61f Iustin Pop
    for (idx, ((status, data), (name, _))) in enumerate(zip(results,
2270 5299e61f Iustin Pop
                                                            self.queue)):
2271 5299e61f Iustin Pop
      self.jobs.append((idx, status, data, name))
2272 5299e61f Iustin Pop
2273 5299e61f Iustin Pop
  def _ChooseJob(self):
2274 5299e61f Iustin Pop
    """Choose a non-waiting/queued job to poll next.
2275 5299e61f Iustin Pop

2276 5299e61f Iustin Pop
    """
2277 5299e61f Iustin Pop
    assert self.jobs, "_ChooseJob called with empty job list"
2278 5299e61f Iustin Pop
2279 5299e61f Iustin Pop
    result = self.cl.QueryJobs([i[2] for i in self.jobs], ["status"])
2280 5299e61f Iustin Pop
    assert result
2281 5299e61f Iustin Pop
2282 5299e61f Iustin Pop
    for job_data, status in zip(self.jobs, result):
2283 5299e61f Iustin Pop
      if status[0] in (constants.JOB_STATUS_QUEUED,
2284 5299e61f Iustin Pop
                    constants.JOB_STATUS_WAITLOCK,
2285 5299e61f Iustin Pop
                    constants.JOB_STATUS_CANCELING):
2286 5299e61f Iustin Pop
        # job is still waiting
2287 5299e61f Iustin Pop
        continue
2288 5299e61f Iustin Pop
      # good candidate found
2289 5299e61f Iustin Pop
      self.jobs.remove(job_data)
2290 5299e61f Iustin Pop
      return job_data
2291 5299e61f Iustin Pop
2292 5299e61f Iustin Pop
    # no job found
2293 5299e61f Iustin Pop
    return self.jobs.pop(0)
2294 479636a3 Iustin Pop
2295 479636a3 Iustin Pop
  def GetResults(self):
2296 479636a3 Iustin Pop
    """Wait for and return the results of all jobs.
2297 479636a3 Iustin Pop

2298 479636a3 Iustin Pop
    @rtype: list
2299 479636a3 Iustin Pop
    @return: list of tuples (success, job results), in the same order
2300 479636a3 Iustin Pop
        as the submitted jobs; if a job has failed, instead of the result
2301 479636a3 Iustin Pop
        there will be the error message
2302 479636a3 Iustin Pop

2303 479636a3 Iustin Pop
    """
2304 23b4b983 Iustin Pop
    if not self.jobs:
2305 23b4b983 Iustin Pop
      self.SubmitPending()
2306 479636a3 Iustin Pop
    results = []
2307 479636a3 Iustin Pop
    if self.verbose:
2308 5299e61f Iustin Pop
      ok_jobs = [row[2] for row in self.jobs if row[1]]
2309 23b4b983 Iustin Pop
      if ok_jobs:
2310 1f864b60 Iustin Pop
        ToStdout("Submitted jobs %s", utils.CommaJoin(ok_jobs))
2311 5299e61f Iustin Pop
2312 5299e61f Iustin Pop
    # first, remove any non-submitted jobs
2313 cea881e5 Michael Hanselmann
    self.jobs, failures = compat.partition(self.jobs, lambda x: x[1])
2314 5299e61f Iustin Pop
    for idx, _, jid, name in failures:
2315 c63355f2 Iustin Pop
      ToStderr("Failed to submit job for %s: %s", name, jid)
2316 c63355f2 Iustin Pop
      results.append((idx, False, jid))
2317 5299e61f Iustin Pop
2318 5299e61f Iustin Pop
    while self.jobs:
2319 5299e61f Iustin Pop
      (idx, _, jid, name) = self._ChooseJob()
2320 5299e61f Iustin Pop
      ToStdout("Waiting for job %s for %s...", jid, name)
2321 479636a3 Iustin Pop
      try:
2322 919ca415 Iustin Pop
        job_result = PollJob(jid, cl=self.cl, feedback_fn=self.feedback_fn)
2323 479636a3 Iustin Pop
        success = True
2324 479636a3 Iustin Pop
      except (errors.GenericError, luxi.ProtocolError), err:
2325 479636a3 Iustin Pop
        _, job_result = FormatError(err)
2326 479636a3 Iustin Pop
        success = False
2327 479636a3 Iustin Pop
        # the error message will always be shown, verbose or not
2328 479636a3 Iustin Pop
        ToStderr("Job %s for %s has failed: %s", jid, name, job_result)
2329 479636a3 Iustin Pop
2330 5299e61f Iustin Pop
      results.append((idx, success, job_result))
2331 5299e61f Iustin Pop
2332 5299e61f Iustin Pop
    # sort based on the index, then drop it
2333 5299e61f Iustin Pop
    results.sort()
2334 5299e61f Iustin Pop
    results = [i[1:] for i in results]
2335 5299e61f Iustin Pop
2336 479636a3 Iustin Pop
    return results
2337 479636a3 Iustin Pop
2338 479636a3 Iustin Pop
  def WaitOrShow(self, wait):
2339 479636a3 Iustin Pop
    """Wait for job results or only print the job IDs.
2340 479636a3 Iustin Pop

2341 479636a3 Iustin Pop
    @type wait: boolean
2342 479636a3 Iustin Pop
    @param wait: whether to wait or not
2343 479636a3 Iustin Pop

2344 479636a3 Iustin Pop
    """
2345 479636a3 Iustin Pop
    if wait:
2346 479636a3 Iustin Pop
      return self.GetResults()
2347 479636a3 Iustin Pop
    else:
2348 23b4b983 Iustin Pop
      if not self.jobs:
2349 23b4b983 Iustin Pop
        self.SubmitPending()
2350 71834b2a Guido Trotter
      for _, status, result, name in self.jobs:
2351 23b4b983 Iustin Pop
        if status:
2352 23b4b983 Iustin Pop
          ToStdout("%s: %s", result, name)
2353 23b4b983 Iustin Pop
        else:
2354 23b4b983 Iustin Pop
          ToStderr("Failure for %s: %s", name, result)