Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-instance @ cd55576a

History | View | Annotate | Download (43.5 kB)

1 a8083063 Iustin Pop
#!/usr/bin/python
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
import sys
23 a8083063 Iustin Pop
import os
24 312ac745 Iustin Pop
import itertools
25 a8083063 Iustin Pop
from optparse import make_option
26 a8083063 Iustin Pop
from cStringIO import StringIO
27 a8083063 Iustin Pop
28 a8083063 Iustin Pop
from ganeti.cli import *
29 a8083063 Iustin Pop
from ganeti import opcodes
30 a8083063 Iustin Pop
from ganeti import logger
31 a8083063 Iustin Pop
from ganeti import constants
32 a8083063 Iustin Pop
from ganeti import utils
33 312ac745 Iustin Pop
from ganeti import errors
34 312ac745 Iustin Pop
35 312ac745 Iustin Pop
36 312ac745 Iustin Pop
_SHUTDOWN_CLUSTER = "cluster"
37 312ac745 Iustin Pop
_SHUTDOWN_NODES_BOTH = "nodes"
38 312ac745 Iustin Pop
_SHUTDOWN_NODES_PRI = "nodes-pri"
39 312ac745 Iustin Pop
_SHUTDOWN_NODES_SEC = "nodes-sec"
40 312ac745 Iustin Pop
_SHUTDOWN_INSTANCES = "instances"
41 312ac745 Iustin Pop
42 7c0d6283 Michael Hanselmann
43 31a853d2 Iustin Pop
_VALUE_TRUE = "true"
44 31a853d2 Iustin Pop
45 48c4dfa8 Iustin Pop
_LIST_DEF_FIELDS = [
46 48c4dfa8 Iustin Pop
  "name", "os", "pnode", "status", "oper_ram",
47 48c4dfa8 Iustin Pop
  ]
48 48c4dfa8 Iustin Pop
49 bdb7d4e8 Michael Hanselmann
50 312ac745 Iustin Pop
def _ExpandMultiNames(mode, names):
51 312ac745 Iustin Pop
  """Expand the given names using the passed mode.
52 312ac745 Iustin Pop
53 312ac745 Iustin Pop
  Args:
54 312ac745 Iustin Pop
    - mode, which can be one of _SHUTDOWN_CLUSTER, _SHUTDOWN_NODES_BOTH,
55 312ac745 Iustin Pop
      _SHUTDOWN_NODES_PRI, _SHUTDOWN_NODES_SEC or _SHUTDOWN_INSTANCES
56 312ac745 Iustin Pop
    - names, which is a list of names; for cluster, it must be empty,
57 312ac745 Iustin Pop
      and for node and instance it must be a list of valid item
58 312ac745 Iustin Pop
      names (short names are valid as usual, e.g. node1 instead of
59 312ac745 Iustin Pop
      node1.example.com)
60 312ac745 Iustin Pop
61 312ac745 Iustin Pop
  For _SHUTDOWN_CLUSTER, all instances will be returned. For
62 312ac745 Iustin Pop
  _SHUTDOWN_NODES_PRI/SEC, all instances having those nodes as
63 312ac745 Iustin Pop
  primary/secondary will be shutdown. For _SHUTDOWN_NODES_BOTH, all
64 312ac745 Iustin Pop
  instances having those nodes as either primary or secondary will be
65 312ac745 Iustin Pop
  returned. For _SHUTDOWN_INSTANCES, the given instances will be
66 312ac745 Iustin Pop
  returned.
67 312ac745 Iustin Pop
68 312ac745 Iustin Pop
  """
69 312ac745 Iustin Pop
  if mode == _SHUTDOWN_CLUSTER:
70 312ac745 Iustin Pop
    if names:
71 312ac745 Iustin Pop
      raise errors.OpPrereqError("Cluster filter mode takes no arguments")
72 b7329c9c Iustin Pop
    client = GetClient()
73 b7329c9c Iustin Pop
    idata = client.QueryInstances([], ["name"])
74 312ac745 Iustin Pop
    inames = [row[0] for row in idata]
75 312ac745 Iustin Pop
76 312ac745 Iustin Pop
  elif mode in (_SHUTDOWN_NODES_BOTH,
77 312ac745 Iustin Pop
                _SHUTDOWN_NODES_PRI,
78 312ac745 Iustin Pop
                _SHUTDOWN_NODES_SEC):
79 312ac745 Iustin Pop
    if not names:
80 312ac745 Iustin Pop
      raise errors.OpPrereqError("No node names passed")
81 b7329c9c Iustin Pop
    client = GetClient()
82 b7329c9c Iustin Pop
    ndata = client.QueryNodes(names, ["name", "pinst_list", "sinst_list"])
83 312ac745 Iustin Pop
    ipri = [row[1] for row in ndata]
84 312ac745 Iustin Pop
    pri_names = list(itertools.chain(*ipri))
85 312ac745 Iustin Pop
    isec = [row[2] for row in ndata]
86 312ac745 Iustin Pop
    sec_names = list(itertools.chain(*isec))
87 312ac745 Iustin Pop
    if mode == _SHUTDOWN_NODES_BOTH:
88 312ac745 Iustin Pop
      inames = pri_names + sec_names
89 312ac745 Iustin Pop
    elif mode == _SHUTDOWN_NODES_PRI:
90 312ac745 Iustin Pop
      inames = pri_names
91 312ac745 Iustin Pop
    elif mode == _SHUTDOWN_NODES_SEC:
92 312ac745 Iustin Pop
      inames = sec_names
93 312ac745 Iustin Pop
    else:
94 312ac745 Iustin Pop
      raise errors.ProgrammerError("Unhandled shutdown type")
95 312ac745 Iustin Pop
96 312ac745 Iustin Pop
  elif mode == _SHUTDOWN_INSTANCES:
97 312ac745 Iustin Pop
    if not names:
98 312ac745 Iustin Pop
      raise errors.OpPrereqError("No instance names passed")
99 b7329c9c Iustin Pop
    client = GetClient()
100 b7329c9c Iustin Pop
    idata = client.QueryInstances(names, ["name"])
101 312ac745 Iustin Pop
    inames = [row[0] for row in idata]
102 312ac745 Iustin Pop
103 312ac745 Iustin Pop
  else:
104 312ac745 Iustin Pop
    raise errors.OpPrereqError("Unknown mode '%s'" % mode)
105 312ac745 Iustin Pop
106 312ac745 Iustin Pop
  return inames
107 a8083063 Iustin Pop
108 a8083063 Iustin Pop
109 804a1e8e Iustin Pop
def _ConfirmOperation(inames, text):
110 804a1e8e Iustin Pop
  """Ask the user to confirm an operation on a list of instances.
111 804a1e8e Iustin Pop
112 804a1e8e Iustin Pop
  This function is used to request confirmation for doing an operation
113 804a1e8e Iustin Pop
  on a given list of instances.
114 804a1e8e Iustin Pop
115 804a1e8e Iustin Pop
  The inames argument is what the selection algorithm computed, and
116 804a1e8e Iustin Pop
  the text argument is the operation we should tell the user to
117 804a1e8e Iustin Pop
  confirm (e.g. 'shutdown' or 'startup').
118 804a1e8e Iustin Pop
119 804a1e8e Iustin Pop
  Returns: boolean depending on user's confirmation.
120 804a1e8e Iustin Pop
121 804a1e8e Iustin Pop
  """
122 804a1e8e Iustin Pop
  count = len(inames)
123 804a1e8e Iustin Pop
  msg = ("The %s will operate on %d instances.\n"
124 804a1e8e Iustin Pop
         "Do you want to continue?" % (text, count))
125 804a1e8e Iustin Pop
  affected = ("\nAffected instances:\n" +
126 804a1e8e Iustin Pop
              "\n".join(["  %s" % name for name in inames]))
127 804a1e8e Iustin Pop
128 804a1e8e Iustin Pop
  choices = [('y', True, 'Yes, execute the %s' % text),
129 804a1e8e Iustin Pop
             ('n', False, 'No, abort the %s' % text)]
130 804a1e8e Iustin Pop
131 804a1e8e Iustin Pop
  if count > 20:
132 804a1e8e Iustin Pop
    choices.insert(1, ('v', 'v', 'View the list of affected instances'))
133 804a1e8e Iustin Pop
    ask = msg
134 804a1e8e Iustin Pop
  else:
135 804a1e8e Iustin Pop
    ask = msg + affected
136 804a1e8e Iustin Pop
137 804a1e8e Iustin Pop
  choice = AskUser(ask, choices)
138 804a1e8e Iustin Pop
  if choice == 'v':
139 804a1e8e Iustin Pop
    choices.pop(1)
140 5e66b7e6 Iustin Pop
    choice = AskUser(msg + affected, choices)
141 804a1e8e Iustin Pop
  return choice
142 804a1e8e Iustin Pop
143 804a1e8e Iustin Pop
144 973d7867 Iustin Pop
def _TransformPath(user_input):
145 973d7867 Iustin Pop
  """Transform a user path into a canonical value.
146 973d7867 Iustin Pop
147 973d7867 Iustin Pop
  This function transforms the a path passed as textual information
148 973d7867 Iustin Pop
  into the constants that the LU code expects.
149 973d7867 Iustin Pop
150 973d7867 Iustin Pop
  """
151 973d7867 Iustin Pop
  if user_input:
152 973d7867 Iustin Pop
    if user_input.lower() == "default":
153 973d7867 Iustin Pop
      result_path = constants.VALUE_DEFAULT
154 973d7867 Iustin Pop
    elif user_input.lower() == "none":
155 973d7867 Iustin Pop
      result_path = constants.VALUE_NONE
156 973d7867 Iustin Pop
    else:
157 973d7867 Iustin Pop
      if not os.path.isabs(user_input):
158 973d7867 Iustin Pop
        raise errors.OpPrereqError("Path '%s' is not an absolute filename" %
159 973d7867 Iustin Pop
                                   user_input)
160 973d7867 Iustin Pop
      result_path = user_input
161 973d7867 Iustin Pop
  else:
162 973d7867 Iustin Pop
    result_path = constants.VALUE_DEFAULT
163 973d7867 Iustin Pop
164 973d7867 Iustin Pop
  return result_path
165 973d7867 Iustin Pop
166 973d7867 Iustin Pop
167 a8083063 Iustin Pop
def ListInstances(opts, args):
168 f5abe9bd Oleksiy Mishchenko
  """List instances and their properties.
169 a8083063 Iustin Pop
170 a8083063 Iustin Pop
  """
171 a8083063 Iustin Pop
  if opts.output is None:
172 48c4dfa8 Iustin Pop
    selected_fields = _LIST_DEF_FIELDS
173 48c4dfa8 Iustin Pop
  elif opts.output.startswith("+"):
174 48c4dfa8 Iustin Pop
    selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
175 a8083063 Iustin Pop
  else:
176 a8083063 Iustin Pop
    selected_fields = opts.output.split(",")
177 a8083063 Iustin Pop
178 1f05af2b Michael Hanselmann
  output = GetClient().QueryInstances([], selected_fields)
179 a8083063 Iustin Pop
180 a8083063 Iustin Pop
  if not opts.no_headers:
181 d8052456 Iustin Pop
    headers = {
182 d8052456 Iustin Pop
      "name": "Instance", "os": "OS", "pnode": "Primary_node",
183 d8052456 Iustin Pop
      "snodes": "Secondary_Nodes", "admin_state": "Autostart",
184 d8052456 Iustin Pop
      "oper_state": "Running", "admin_ram": "Configured_memory",
185 d8052456 Iustin Pop
      "oper_ram": "Memory", "disk_template": "Disk_template",
186 3fb1e1c5 Alexander Schreiber
      "ip": "IP_address", "mac": "MAC_address",
187 d8052456 Iustin Pop
      "bridge": "Bridge", "vcpus": "VCPUs",
188 d8052456 Iustin Pop
      "sda_size": "Disk/0", "sdb_size": "Disk/1",
189 130a6a6f Iustin Pop
      "status": "Status", "tags": "Tags",
190 3fb1e1c5 Alexander Schreiber
      "network_port": "Network_port",
191 3fb1e1c5 Alexander Schreiber
      "kernel_path": "Kernel_path",
192 3fb1e1c5 Alexander Schreiber
      "initrd_path": "Initrd_path",
193 3fb1e1c5 Alexander Schreiber
      "hvm_boot_order": "HVM_boot_order",
194 3fb1e1c5 Alexander Schreiber
      "hvm_acpi": "HVM_ACPI",
195 3fb1e1c5 Alexander Schreiber
      "hvm_pae": "HVM_PAE",
196 3fb1e1c5 Alexander Schreiber
      "hvm_cdrom_image_path": "HVM_CDROM_image_path",
197 3fb1e1c5 Alexander Schreiber
      "hvm_nic_type": "HVM_NIC_type",
198 3fb1e1c5 Alexander Schreiber
      "hvm_disk_type": "HVM_disk_type",
199 3fb1e1c5 Alexander Schreiber
      "vnc_bind_address": "VNC_bind_address",
200 38d7239a Iustin Pop
      "serial_no": "SerialNo",
201 d8052456 Iustin Pop
      }
202 137161c9 Michael Hanselmann
  else:
203 137161c9 Michael Hanselmann
    headers = None
204 137161c9 Michael Hanselmann
205 137161c9 Michael Hanselmann
  if opts.human_readable:
206 644eeef9 Iustin Pop
    unitfields = ["admin_ram", "oper_ram", "sda_size", "sdb_size"]
207 137161c9 Michael Hanselmann
  else:
208 137161c9 Michael Hanselmann
    unitfields = None
209 137161c9 Michael Hanselmann
210 38d7239a Iustin Pop
  numfields = ["admin_ram", "oper_ram", "sda_size", "sdb_size", "vcpus",
211 38d7239a Iustin Pop
               "serial_no"]
212 137161c9 Michael Hanselmann
213 130a6a6f Iustin Pop
  list_type_fields = ("tags",)
214 8a23d2d3 Iustin Pop
  # change raw values to nicer strings
215 8a23d2d3 Iustin Pop
  for row in output:
216 8a23d2d3 Iustin Pop
    for idx, field in enumerate(selected_fields):
217 8a23d2d3 Iustin Pop
      val = row[idx]
218 8a23d2d3 Iustin Pop
      if field == "snodes":
219 8a23d2d3 Iustin Pop
        val = ",".join(val) or "-"
220 8a23d2d3 Iustin Pop
      elif field == "admin_state":
221 8a23d2d3 Iustin Pop
        if val:
222 8a23d2d3 Iustin Pop
          val = "yes"
223 8a23d2d3 Iustin Pop
        else:
224 8a23d2d3 Iustin Pop
          val = "no"
225 8a23d2d3 Iustin Pop
      elif field == "oper_state":
226 8a23d2d3 Iustin Pop
        if val is None:
227 8a23d2d3 Iustin Pop
          val = "(node down)"
228 8a23d2d3 Iustin Pop
        elif val: # True
229 8a23d2d3 Iustin Pop
          val = "running"
230 8a23d2d3 Iustin Pop
        else:
231 8a23d2d3 Iustin Pop
          val = "stopped"
232 8a23d2d3 Iustin Pop
      elif field == "oper_ram":
233 8a23d2d3 Iustin Pop
        if val is None:
234 8a23d2d3 Iustin Pop
          val = "(node down)"
235 8a23d2d3 Iustin Pop
      elif field == "sda_size" or field == "sdb_size":
236 8a23d2d3 Iustin Pop
        if val is None:
237 8a23d2d3 Iustin Pop
          val = "N/A"
238 130a6a6f Iustin Pop
      elif field in list_type_fields:
239 130a6a6f Iustin Pop
        val = ",".join(val)
240 8a23d2d3 Iustin Pop
      row[idx] = str(val)
241 8a23d2d3 Iustin Pop
242 16be8703 Iustin Pop
  data = GenerateTable(separator=opts.separator, headers=headers,
243 16be8703 Iustin Pop
                       fields=selected_fields, unitfields=unitfields,
244 16be8703 Iustin Pop
                       numfields=numfields, data=output)
245 16be8703 Iustin Pop
246 16be8703 Iustin Pop
  for line in data:
247 16be8703 Iustin Pop
    logger.ToStdout(line)
248 a8083063 Iustin Pop
249 a8083063 Iustin Pop
  return 0
250 a8083063 Iustin Pop
251 a8083063 Iustin Pop
252 a8083063 Iustin Pop
def AddInstance(opts, args):
253 a8083063 Iustin Pop
  """Add an instance to the cluster.
254 a8083063 Iustin Pop
255 a8083063 Iustin Pop
  Args:
256 a8083063 Iustin Pop
    opts - class with options as members
257 a8083063 Iustin Pop
    args - list with a single element, the instance name
258 a8083063 Iustin Pop
  Opts used:
259 a8083063 Iustin Pop
    mem - amount of memory to allocate to instance (MiB)
260 a8083063 Iustin Pop
    size - amount of disk space to allocate to instance (MiB)
261 a8083063 Iustin Pop
    os - which OS to run on instance
262 a8083063 Iustin Pop
    node - node to run new instance on
263 a8083063 Iustin Pop
264 a8083063 Iustin Pop
  """
265 a8083063 Iustin Pop
  instance = args[0]
266 a8083063 Iustin Pop
267 60d49723 Michael Hanselmann
  (pnode, snode) = SplitNodeOption(opts.node)
268 60d49723 Michael Hanselmann
269 3b6d8c9b Iustin Pop
  kernel_path = _TransformPath(opts.kernel_path)
270 3b6d8c9b Iustin Pop
  initrd_path = _TransformPath(opts.initrd_path)
271 3b6d8c9b Iustin Pop
272 31a853d2 Iustin Pop
  hvm_acpi = opts.hvm_acpi == _VALUE_TRUE
273 31a853d2 Iustin Pop
  hvm_pae = opts.hvm_pae == _VALUE_TRUE
274 31a853d2 Iustin Pop
275 31a853d2 Iustin Pop
  if ((opts.hvm_cdrom_image_path is not None) and
276 31a853d2 Iustin Pop
      (opts.hvm_cdrom_image_path.lower() == constants.VALUE_NONE)):
277 31a853d2 Iustin Pop
    hvm_cdrom_image_path = None
278 31a853d2 Iustin Pop
  else:
279 31a853d2 Iustin Pop
    hvm_cdrom_image_path = opts.hvm_cdrom_image_path
280 31a853d2 Iustin Pop
281 a8083063 Iustin Pop
  op = opcodes.OpCreateInstance(instance_name=instance, mem_size=opts.mem,
282 a8083063 Iustin Pop
                                disk_size=opts.size, swap_size=opts.swap,
283 a8083063 Iustin Pop
                                disk_template=opts.disk_template,
284 a8083063 Iustin Pop
                                mode=constants.INSTANCE_CREATE,
285 60d49723 Michael Hanselmann
                                os_type=opts.os, pnode=pnode,
286 60d49723 Michael Hanselmann
                                snode=snode, vcpus=opts.vcpus,
287 bdd55f71 Iustin Pop
                                ip=opts.ip, bridge=opts.bridge,
288 bdd55f71 Iustin Pop
                                start=opts.start, ip_check=opts.ip_check,
289 3b6d8c9b Iustin Pop
                                wait_for_sync=opts.wait_for_sync,
290 3b6d8c9b Iustin Pop
                                mac=opts.mac,
291 3b6d8c9b Iustin Pop
                                kernel_path=kernel_path,
292 25c5878d Alexander Schreiber
                                initrd_path=initrd_path,
293 31a853d2 Iustin Pop
                                iallocator=opts.iallocator,
294 45873083 Manuel Franceschini
                                hvm_boot_order=opts.hvm_boot_order,
295 35a0d128 Iustin Pop
                                file_storage_dir=opts.file_storage_dir,
296 35a0d128 Iustin Pop
                                file_driver=opts.file_driver,
297 31a853d2 Iustin Pop
                                hvm_acpi=hvm_acpi, hvm_pae=hvm_pae,
298 31a853d2 Iustin Pop
                                hvm_cdrom_image_path=hvm_cdrom_image_path,
299 ad2023a3 Alexander Schreiber
                                vnc_bind_address=opts.vnc_bind_address,
300 ad2023a3 Alexander Schreiber
                                hvm_nic_type=opts.hvm_nic_type,
301 ad2023a3 Alexander Schreiber
                                hvm_disk_type=opts.hvm_disk_type)
302 31a853d2 Iustin Pop
303 6340bb0a Iustin Pop
  SubmitOrSend(op, opts)
304 a8083063 Iustin Pop
  return 0
305 a8083063 Iustin Pop
306 a8083063 Iustin Pop
307 fe7b0351 Michael Hanselmann
def ReinstallInstance(opts, args):
308 fe7b0351 Michael Hanselmann
  """Reinstall an instance.
309 fe7b0351 Michael Hanselmann
310 fe7b0351 Michael Hanselmann
  Args:
311 fe7b0351 Michael Hanselmann
    opts - class with options as members
312 fe7b0351 Michael Hanselmann
    args - list containing a single element, the instance name
313 fe7b0351 Michael Hanselmann
314 fe7b0351 Michael Hanselmann
  """
315 fe7b0351 Michael Hanselmann
  instance_name = args[0]
316 fe7b0351 Michael Hanselmann
317 20e23543 Alexander Schreiber
  if opts.select_os is True:
318 20e23543 Alexander Schreiber
    op = opcodes.OpDiagnoseOS(output_fields=["name", "valid"], names=[])
319 20e23543 Alexander Schreiber
    result = SubmitOpCode(op)
320 20e23543 Alexander Schreiber
321 20e23543 Alexander Schreiber
    if not result:
322 20e23543 Alexander Schreiber
      logger.ToStdout("Can't get the OS list")
323 20e23543 Alexander Schreiber
      return 1
324 20e23543 Alexander Schreiber
325 20e23543 Alexander Schreiber
    logger.ToStdout("Available OS templates:")
326 20e23543 Alexander Schreiber
    number = 0
327 20e23543 Alexander Schreiber
    choices = []
328 20e23543 Alexander Schreiber
    for entry in result:
329 20e23543 Alexander Schreiber
      logger.ToStdout("%3s: %s" % (number, entry[0]))
330 20e23543 Alexander Schreiber
      choices.append(("%s" % number, entry[0], entry[0]))
331 20e23543 Alexander Schreiber
      number = number + 1
332 20e23543 Alexander Schreiber
333 20e23543 Alexander Schreiber
    choices.append(('x', 'exit', 'Exit gnt-instance reinstall'))
334 20e23543 Alexander Schreiber
    selected = AskUser("Enter OS template name or number (or x to abort):",
335 20e23543 Alexander Schreiber
                       choices)
336 20e23543 Alexander Schreiber
337 20e23543 Alexander Schreiber
    if selected == 'exit':
338 20e23543 Alexander Schreiber
      logger.ToStdout("User aborted reinstall, exiting")
339 20e23543 Alexander Schreiber
      return 1
340 20e23543 Alexander Schreiber
341 20e23543 Alexander Schreiber
    os = selected
342 20e23543 Alexander Schreiber
  else:
343 20e23543 Alexander Schreiber
    os = opts.os
344 20e23543 Alexander Schreiber
345 fe7b0351 Michael Hanselmann
  if not opts.force:
346 f4bc1f2c Michael Hanselmann
    usertext = ("This will reinstall the instance %s and remove"
347 f4bc1f2c Michael Hanselmann
                " all data. Continue?") % instance_name
348 47988778 Iustin Pop
    if not AskUser(usertext):
349 fe7b0351 Michael Hanselmann
      return 1
350 fe7b0351 Michael Hanselmann
351 d0834de3 Michael Hanselmann
  op = opcodes.OpReinstallInstance(instance_name=instance_name,
352 20e23543 Alexander Schreiber
                                   os_type=os)
353 6340bb0a Iustin Pop
  SubmitOrSend(op, opts)
354 fe7b0351 Michael Hanselmann
355 fe7b0351 Michael Hanselmann
  return 0
356 fe7b0351 Michael Hanselmann
357 fe7b0351 Michael Hanselmann
358 a8083063 Iustin Pop
def RemoveInstance(opts, args):
359 a8083063 Iustin Pop
  """Remove an instance.
360 a8083063 Iustin Pop
361 a8083063 Iustin Pop
  Args:
362 a8083063 Iustin Pop
    opts - class with options as members
363 a8083063 Iustin Pop
    args - list containing a single element, the instance name
364 a8083063 Iustin Pop
365 a8083063 Iustin Pop
  """
366 a8083063 Iustin Pop
  instance_name = args[0]
367 a8083063 Iustin Pop
  force = opts.force
368 a8083063 Iustin Pop
369 a8083063 Iustin Pop
  if not force:
370 a8083063 Iustin Pop
    usertext = ("This will remove the volumes of the instance %s"
371 a8083063 Iustin Pop
                " (including mirrors), thus removing all the data"
372 a8083063 Iustin Pop
                " of the instance. Continue?") % instance_name
373 47988778 Iustin Pop
    if not AskUser(usertext):
374 a8083063 Iustin Pop
      return 1
375 a8083063 Iustin Pop
376 1d67656e Iustin Pop
  op = opcodes.OpRemoveInstance(instance_name=instance_name,
377 1d67656e Iustin Pop
                                ignore_failures=opts.ignore_failures)
378 6340bb0a Iustin Pop
  SubmitOrSend(op, opts)
379 a8083063 Iustin Pop
  return 0
380 a8083063 Iustin Pop
381 a8083063 Iustin Pop
382 decd5f45 Iustin Pop
def RenameInstance(opts, args):
383 4ab0b9e3 Guido Trotter
  """Rename an instance.
384 decd5f45 Iustin Pop
385 decd5f45 Iustin Pop
  Args:
386 decd5f45 Iustin Pop
    opts - class with options as members
387 decd5f45 Iustin Pop
    args - list containing two elements, the instance name and the new name
388 decd5f45 Iustin Pop
389 decd5f45 Iustin Pop
  """
390 decd5f45 Iustin Pop
  op = opcodes.OpRenameInstance(instance_name=args[0],
391 decd5f45 Iustin Pop
                                new_name=args[1],
392 decd5f45 Iustin Pop
                                ignore_ip=opts.ignore_ip)
393 6340bb0a Iustin Pop
  SubmitOrSend(op, opts)
394 decd5f45 Iustin Pop
  return 0
395 decd5f45 Iustin Pop
396 decd5f45 Iustin Pop
397 a8083063 Iustin Pop
def ActivateDisks(opts, args):
398 a8083063 Iustin Pop
  """Activate an instance's disks.
399 a8083063 Iustin Pop
400 a8083063 Iustin Pop
  This serves two purposes:
401 a8083063 Iustin Pop
    - it allows one (as long as the instance is not running) to mount
402 a8083063 Iustin Pop
    the disks and modify them from the node
403 a8083063 Iustin Pop
    - it repairs inactive secondary drbds
404 a8083063 Iustin Pop
405 a8083063 Iustin Pop
  """
406 a8083063 Iustin Pop
  instance_name = args[0]
407 a8083063 Iustin Pop
  op = opcodes.OpActivateInstanceDisks(instance_name=instance_name)
408 6340bb0a Iustin Pop
  disks_info = SubmitOrSend(op, opts)
409 a8083063 Iustin Pop
  for host, iname, nname in disks_info:
410 a8083063 Iustin Pop
    print "%s:%s:%s" % (host, iname, nname)
411 a8083063 Iustin Pop
  return 0
412 a8083063 Iustin Pop
413 a8083063 Iustin Pop
414 a8083063 Iustin Pop
def DeactivateDisks(opts, args):
415 a8083063 Iustin Pop
  """Command-line interface for _ShutdownInstanceBlockDevices.
416 a8083063 Iustin Pop
417 a8083063 Iustin Pop
  This function takes the instance name, looks for its primary node
418 a8083063 Iustin Pop
  and the tries to shutdown its block devices on that node.
419 a8083063 Iustin Pop
420 a8083063 Iustin Pop
  """
421 a8083063 Iustin Pop
  instance_name = args[0]
422 a8083063 Iustin Pop
  op = opcodes.OpDeactivateInstanceDisks(instance_name=instance_name)
423 6340bb0a Iustin Pop
  SubmitOrSend(op, opts)
424 a8083063 Iustin Pop
  return 0
425 a8083063 Iustin Pop
426 a8083063 Iustin Pop
427 c6e911bc Iustin Pop
def GrowDisk(opts, args):
428 c6e911bc Iustin Pop
  """Command-line interface for _ShutdownInstanceBlockDevices.
429 c6e911bc Iustin Pop
430 c6e911bc Iustin Pop
  This function takes the instance name, looks for its primary node
431 c6e911bc Iustin Pop
  and the tries to shutdown its block devices on that node.
432 c6e911bc Iustin Pop
433 c6e911bc Iustin Pop
  """
434 c6e911bc Iustin Pop
  instance = args[0]
435 c6e911bc Iustin Pop
  disk = args[1]
436 c6e911bc Iustin Pop
  amount = utils.ParseUnit(args[2])
437 c6e911bc Iustin Pop
  op = opcodes.OpGrowDisk(instance_name=instance, disk=disk, amount=amount)
438 6340bb0a Iustin Pop
  SubmitOrSend(op, opts)
439 c6e911bc Iustin Pop
  return 0
440 c6e911bc Iustin Pop
441 c6e911bc Iustin Pop
442 a8083063 Iustin Pop
def StartupInstance(opts, args):
443 decd5f45 Iustin Pop
  """Startup an instance.
444 a8083063 Iustin Pop
445 a8083063 Iustin Pop
  Args:
446 a8083063 Iustin Pop
    opts - class with options as members
447 a8083063 Iustin Pop
    args - list containing a single element, the instance name
448 a8083063 Iustin Pop
449 a8083063 Iustin Pop
  """
450 312ac745 Iustin Pop
  if opts.multi_mode is None:
451 312ac745 Iustin Pop
    opts.multi_mode = _SHUTDOWN_INSTANCES
452 312ac745 Iustin Pop
  inames = _ExpandMultiNames(opts.multi_mode, args)
453 bcee9cb4 Iustin Pop
  if not inames:
454 bcee9cb4 Iustin Pop
    raise errors.OpPrereqError("Selection filter does not match any instances")
455 804a1e8e Iustin Pop
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
456 804a1e8e Iustin Pop
  if not (opts.force_multi or not multi_on
457 804a1e8e Iustin Pop
          or _ConfirmOperation(inames, "startup")):
458 804a1e8e Iustin Pop
    return 1
459 312ac745 Iustin Pop
  for name in inames:
460 312ac745 Iustin Pop
    op = opcodes.OpStartupInstance(instance_name=name,
461 312ac745 Iustin Pop
                                   force=opts.force,
462 312ac745 Iustin Pop
                                   extra_args=opts.extra_args)
463 312ac745 Iustin Pop
    if multi_on:
464 312ac745 Iustin Pop
      logger.ToStdout("Starting up %s" % name)
465 aefef4f4 Iustin Pop
    try:
466 aefef4f4 Iustin Pop
      SubmitOrSend(op, opts)
467 aefef4f4 Iustin Pop
    except JobSubmittedException, err:
468 aefef4f4 Iustin Pop
      _, txt = FormatError(err)
469 aefef4f4 Iustin Pop
      logger.ToStdout("%s" % txt)
470 a8083063 Iustin Pop
  return 0
471 a8083063 Iustin Pop
472 7c0d6283 Michael Hanselmann
473 579d4337 Alexander Schreiber
def RebootInstance(opts, args):
474 579d4337 Alexander Schreiber
  """Reboot an instance
475 579d4337 Alexander Schreiber
476 579d4337 Alexander Schreiber
  Args:
477 579d4337 Alexander Schreiber
    opts - class with options as members
478 579d4337 Alexander Schreiber
    args - list containing a single element, the instance name
479 579d4337 Alexander Schreiber
480 579d4337 Alexander Schreiber
  """
481 579d4337 Alexander Schreiber
  if opts.multi_mode is None:
482 579d4337 Alexander Schreiber
    opts.multi_mode = _SHUTDOWN_INSTANCES
483 579d4337 Alexander Schreiber
  inames = _ExpandMultiNames(opts.multi_mode, args)
484 bcee9cb4 Iustin Pop
  if not inames:
485 bcee9cb4 Iustin Pop
    raise errors.OpPrereqError("Selection filter does not match any instances")
486 579d4337 Alexander Schreiber
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
487 579d4337 Alexander Schreiber
  if not (opts.force_multi or not multi_on
488 579d4337 Alexander Schreiber
          or _ConfirmOperation(inames, "reboot")):
489 579d4337 Alexander Schreiber
    return 1
490 579d4337 Alexander Schreiber
  for name in inames:
491 579d4337 Alexander Schreiber
    op = opcodes.OpRebootInstance(instance_name=name,
492 579d4337 Alexander Schreiber
                                  reboot_type=opts.reboot_type,
493 579d4337 Alexander Schreiber
                                  ignore_secondaries=opts.ignore_secondaries)
494 579d4337 Alexander Schreiber
495 6340bb0a Iustin Pop
    SubmitOrSend(op, opts)
496 579d4337 Alexander Schreiber
  return 0
497 a8083063 Iustin Pop
498 7c0d6283 Michael Hanselmann
499 a8083063 Iustin Pop
def ShutdownInstance(opts, args):
500 a8083063 Iustin Pop
  """Shutdown an instance.
501 a8083063 Iustin Pop
502 a8083063 Iustin Pop
  Args:
503 a8083063 Iustin Pop
    opts - class with options as members
504 a8083063 Iustin Pop
    args - list containing a single element, the instance name
505 a8083063 Iustin Pop
506 a8083063 Iustin Pop
  """
507 312ac745 Iustin Pop
  if opts.multi_mode is None:
508 312ac745 Iustin Pop
    opts.multi_mode = _SHUTDOWN_INSTANCES
509 312ac745 Iustin Pop
  inames = _ExpandMultiNames(opts.multi_mode, args)
510 bcee9cb4 Iustin Pop
  if not inames:
511 bcee9cb4 Iustin Pop
    raise errors.OpPrereqError("Selection filter does not match any instances")
512 804a1e8e Iustin Pop
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
513 804a1e8e Iustin Pop
  if not (opts.force_multi or not multi_on
514 804a1e8e Iustin Pop
          or _ConfirmOperation(inames, "shutdown")):
515 804a1e8e Iustin Pop
    return 1
516 312ac745 Iustin Pop
  for name in inames:
517 312ac745 Iustin Pop
    op = opcodes.OpShutdownInstance(instance_name=name)
518 312ac745 Iustin Pop
    if multi_on:
519 312ac745 Iustin Pop
      logger.ToStdout("Shutting down %s" % name)
520 aefef4f4 Iustin Pop
    try:
521 aefef4f4 Iustin Pop
      SubmitOrSend(op, opts)
522 aefef4f4 Iustin Pop
    except JobSubmittedException, err:
523 aefef4f4 Iustin Pop
      _, txt = FormatError(err)
524 aefef4f4 Iustin Pop
      logger.ToStdout("%s" % txt)
525 a8083063 Iustin Pop
  return 0
526 a8083063 Iustin Pop
527 a8083063 Iustin Pop
528 a8083063 Iustin Pop
def ReplaceDisks(opts, args):
529 a8083063 Iustin Pop
  """Replace the disks of an instance
530 a8083063 Iustin Pop
531 a8083063 Iustin Pop
  Args:
532 a8083063 Iustin Pop
    opts - class with options as members
533 a8083063 Iustin Pop
    args - list with a single element, the instance name
534 a8083063 Iustin Pop
535 a8083063 Iustin Pop
  """
536 a8083063 Iustin Pop
  instance_name = args[0]
537 a9e0c397 Iustin Pop
  new_2ndary = opts.new_secondary
538 b6e82a65 Iustin Pop
  iallocator = opts.iallocator
539 a9e0c397 Iustin Pop
  if opts.disks is None:
540 a9e0c397 Iustin Pop
    disks = ["sda", "sdb"]
541 a9e0c397 Iustin Pop
  else:
542 a9e0c397 Iustin Pop
    disks = opts.disks.split(",")
543 a9e0c397 Iustin Pop
  if opts.on_primary == opts.on_secondary: # no -p or -s passed, or both passed
544 a9e0c397 Iustin Pop
    mode = constants.REPLACE_DISK_ALL
545 a9e0c397 Iustin Pop
  elif opts.on_primary: # only on primary:
546 a9e0c397 Iustin Pop
    mode = constants.REPLACE_DISK_PRI
547 b6e82a65 Iustin Pop
    if new_2ndary is not None or iallocator is not None:
548 a9e0c397 Iustin Pop
      raise errors.OpPrereqError("Can't change secondary node on primary disk"
549 a9e0c397 Iustin Pop
                                 " replacement")
550 b6e82a65 Iustin Pop
  elif opts.on_secondary is not None or iallocator is not None:
551 b6e82a65 Iustin Pop
    # only on secondary
552 a9e0c397 Iustin Pop
    mode = constants.REPLACE_DISK_SEC
553 a9e0c397 Iustin Pop
554 a9e0c397 Iustin Pop
  op = opcodes.OpReplaceDisks(instance_name=args[0], disks=disks,
555 b6e82a65 Iustin Pop
                              remote_node=new_2ndary, mode=mode,
556 b6e82a65 Iustin Pop
                              iallocator=iallocator)
557 6340bb0a Iustin Pop
  SubmitOrSend(op, opts)
558 a8083063 Iustin Pop
  return 0
559 a8083063 Iustin Pop
560 a8083063 Iustin Pop
561 a8083063 Iustin Pop
def FailoverInstance(opts, args):
562 a8083063 Iustin Pop
  """Failover an instance.
563 a8083063 Iustin Pop
564 a8083063 Iustin Pop
  The failover is done by shutting it down on its present node and
565 a8083063 Iustin Pop
  starting it on the secondary.
566 a8083063 Iustin Pop
567 a8083063 Iustin Pop
  Args:
568 a8083063 Iustin Pop
    opts - class with options as members
569 a8083063 Iustin Pop
    args - list with a single element, the instance name
570 a8083063 Iustin Pop
  Opts used:
571 a8083063 Iustin Pop
    force - whether to failover without asking questions.
572 a8083063 Iustin Pop
573 a8083063 Iustin Pop
  """
574 80de0e3f Iustin Pop
  instance_name = args[0]
575 80de0e3f Iustin Pop
  force = opts.force
576 a8083063 Iustin Pop
577 80de0e3f Iustin Pop
  if not force:
578 80de0e3f Iustin Pop
    usertext = ("Failover will happen to image %s."
579 80de0e3f Iustin Pop
                " This requires a shutdown of the instance. Continue?" %
580 80de0e3f Iustin Pop
                (instance_name,))
581 80de0e3f Iustin Pop
    if not AskUser(usertext):
582 80de0e3f Iustin Pop
      return 1
583 a8083063 Iustin Pop
584 80de0e3f Iustin Pop
  op = opcodes.OpFailoverInstance(instance_name=instance_name,
585 80de0e3f Iustin Pop
                                  ignore_consistency=opts.ignore_consistency)
586 6340bb0a Iustin Pop
  SubmitOrSend(op, opts)
587 80de0e3f Iustin Pop
  return 0
588 a8083063 Iustin Pop
589 a8083063 Iustin Pop
590 a8083063 Iustin Pop
def ConnectToInstanceConsole(opts, args):
591 a8083063 Iustin Pop
  """Connect to the console of an instance.
592 a8083063 Iustin Pop
593 a8083063 Iustin Pop
  Args:
594 a8083063 Iustin Pop
    opts - class with options as members
595 a8083063 Iustin Pop
    args - list with a single element, the instance name
596 a8083063 Iustin Pop
597 a8083063 Iustin Pop
  """
598 a8083063 Iustin Pop
  instance_name = args[0]
599 a8083063 Iustin Pop
600 a8083063 Iustin Pop
  op = opcodes.OpConnectConsole(instance_name=instance_name)
601 0a80a26f Michael Hanselmann
  cmd = SubmitOpCode(op)
602 51c6e7b5 Michael Hanselmann
603 51c6e7b5 Michael Hanselmann
  if opts.show_command:
604 51c6e7b5 Michael Hanselmann
    print utils.ShellQuoteArgs(cmd)
605 51c6e7b5 Michael Hanselmann
  else:
606 51c6e7b5 Michael Hanselmann
    try:
607 51c6e7b5 Michael Hanselmann
      os.execvp(cmd[0], cmd)
608 51c6e7b5 Michael Hanselmann
    finally:
609 51c6e7b5 Michael Hanselmann
      sys.stderr.write("Can't run console command %s with arguments:\n'%s'" %
610 51c6e7b5 Michael Hanselmann
                       (cmd, " ".join(argv)))
611 51c6e7b5 Michael Hanselmann
      os._exit(1)
612 a8083063 Iustin Pop
613 a8083063 Iustin Pop
614 a8083063 Iustin Pop
def _FormatBlockDevInfo(buf, dev, indent_level):
615 a8083063 Iustin Pop
  """Show block device information.
616 a8083063 Iustin Pop
617 a8083063 Iustin Pop
  This is only used by ShowInstanceConfig(), but it's too big to be
618 a8083063 Iustin Pop
  left for an inline definition.
619 a8083063 Iustin Pop
620 a8083063 Iustin Pop
  """
621 a8083063 Iustin Pop
  def helper(buf, dtype, status):
622 f2e9e0e8 Alexander Schreiber
    """Format one line for physical device status."""
623 a8083063 Iustin Pop
    if not status:
624 a8083063 Iustin Pop
      buf.write("not active\n")
625 a8083063 Iustin Pop
    else:
626 0834c866 Iustin Pop
      (path, major, minor, syncp, estt, degr, ldisk) = status
627 fd38ef95 Manuel Franceschini
      if major is None:
628 fd38ef95 Manuel Franceschini
        major_string = "N/A"
629 fd38ef95 Manuel Franceschini
      else:
630 fd38ef95 Manuel Franceschini
        major_string = str(major)
631 fd38ef95 Manuel Franceschini
632 fd38ef95 Manuel Franceschini
      if minor is None:
633 fd38ef95 Manuel Franceschini
        minor_string = "N/A"
634 fd38ef95 Manuel Franceschini
      else:
635 fd38ef95 Manuel Franceschini
        minor_string = str(minor)
636 fd38ef95 Manuel Franceschini
637 fd38ef95 Manuel Franceschini
      buf.write("%s (%s:%s)" % (path, major_string, minor_string))
638 00fb8246 Michael Hanselmann
      if dtype in (constants.LD_DRBD8, ):
639 a8083063 Iustin Pop
        if syncp is not None:
640 a8083063 Iustin Pop
          sync_text = "*RECOVERING* %5.2f%%," % syncp
641 a8083063 Iustin Pop
          if estt:
642 a8083063 Iustin Pop
            sync_text += " ETA %ds" % estt
643 a8083063 Iustin Pop
          else:
644 a8083063 Iustin Pop
            sync_text += " ETA unknown"
645 a8083063 Iustin Pop
        else:
646 a8083063 Iustin Pop
          sync_text = "in sync"
647 a8083063 Iustin Pop
        if degr:
648 a8083063 Iustin Pop
          degr_text = "*DEGRADED*"
649 a8083063 Iustin Pop
        else:
650 a8083063 Iustin Pop
          degr_text = "ok"
651 0834c866 Iustin Pop
        if ldisk:
652 0834c866 Iustin Pop
          ldisk_text = " *MISSING DISK*"
653 0834c866 Iustin Pop
        else:
654 0834c866 Iustin Pop
          ldisk_text = ""
655 0834c866 Iustin Pop
        buf.write(" %s, status %s%s" % (sync_text, degr_text, ldisk_text))
656 9db6dbce Iustin Pop
      elif dtype == constants.LD_LV:
657 0834c866 Iustin Pop
        if ldisk:
658 0834c866 Iustin Pop
          ldisk_text = " *FAILED* (failed drive?)"
659 9db6dbce Iustin Pop
        else:
660 0834c866 Iustin Pop
          ldisk_text = ""
661 0834c866 Iustin Pop
        buf.write(ldisk_text)
662 a8083063 Iustin Pop
      buf.write("\n")
663 a8083063 Iustin Pop
664 a8083063 Iustin Pop
  if dev["iv_name"] is not None:
665 a8083063 Iustin Pop
    data = "  - %s, " % dev["iv_name"]
666 a8083063 Iustin Pop
  else:
667 a8083063 Iustin Pop
    data = "  - "
668 a8083063 Iustin Pop
  data += "type: %s" % dev["dev_type"]
669 a8083063 Iustin Pop
  if dev["logical_id"] is not None:
670 a8083063 Iustin Pop
    data += ", logical_id: %s" % (dev["logical_id"],)
671 a8083063 Iustin Pop
  elif dev["physical_id"] is not None:
672 a8083063 Iustin Pop
    data += ", physical_id: %s" % (dev["physical_id"],)
673 a8083063 Iustin Pop
  buf.write("%*s%s\n" % (2*indent_level, "", data))
674 a8083063 Iustin Pop
  buf.write("%*s    primary:   " % (2*indent_level, ""))
675 a8083063 Iustin Pop
  helper(buf, dev["dev_type"], dev["pstatus"])
676 a8083063 Iustin Pop
677 a8083063 Iustin Pop
  if dev["sstatus"]:
678 a8083063 Iustin Pop
    buf.write("%*s    secondary: " % (2*indent_level, ""))
679 a8083063 Iustin Pop
    helper(buf, dev["dev_type"], dev["sstatus"])
680 a8083063 Iustin Pop
681 a8083063 Iustin Pop
  if dev["children"]:
682 a8083063 Iustin Pop
    for child in dev["children"]:
683 a8083063 Iustin Pop
      _FormatBlockDevInfo(buf, child, indent_level+1)
684 a8083063 Iustin Pop
685 a8083063 Iustin Pop
686 a8083063 Iustin Pop
def ShowInstanceConfig(opts, args):
687 a8083063 Iustin Pop
  """Compute instance run-time status.
688 a8083063 Iustin Pop
689 a8083063 Iustin Pop
  """
690 a8083063 Iustin Pop
  retcode = 0
691 a8083063 Iustin Pop
  op = opcodes.OpQueryInstanceData(instances=args)
692 a8083063 Iustin Pop
  result = SubmitOpCode(op)
693 a8340917 Iustin Pop
  hvm_parameters = ("hvm_acpi", "hvm_pae", "hvm_cdrom_image_path",
694 ad2023a3 Alexander Schreiber
                    "hvm_boot_order", "hvm_nic_type", "hvm_disk_type")
695 a8340917 Iustin Pop
696 a8340917 Iustin Pop
  pvm_parameters = ("kernel_path", "initrd_path")
697 a8083063 Iustin Pop
698 a8083063 Iustin Pop
  if not result:
699 a8083063 Iustin Pop
    logger.ToStdout("No instances.")
700 a8083063 Iustin Pop
    return 1
701 a8083063 Iustin Pop
702 a8083063 Iustin Pop
  buf = StringIO()
703 a8083063 Iustin Pop
  retcode = 0
704 a8083063 Iustin Pop
  for instance_name in result:
705 a8083063 Iustin Pop
    instance = result[instance_name]
706 a8083063 Iustin Pop
    buf.write("Instance name: %s\n" % instance["name"])
707 a8083063 Iustin Pop
    buf.write("State: configured to be %s, actual state is %s\n" %
708 a8083063 Iustin Pop
              (instance["config_state"], instance["run_state"]))
709 a8083063 Iustin Pop
    buf.write("  Nodes:\n")
710 a8083063 Iustin Pop
    buf.write("    - primary: %s\n" % instance["pnode"])
711 a8083063 Iustin Pop
    buf.write("    - secondaries: %s\n" % ", ".join(instance["snodes"]))
712 a8083063 Iustin Pop
    buf.write("  Operating system: %s\n" % instance["os"])
713 a8340917 Iustin Pop
    if instance.has_key("network_port"):
714 a8340917 Iustin Pop
      buf.write("  Allocated network port: %s\n" % instance["network_port"])
715 a8340917 Iustin Pop
    if False not in map(instance.has_key, pvm_parameters):
716 a8340917 Iustin Pop
      if instance["kernel_path"] in (None, constants.VALUE_DEFAULT):
717 a8340917 Iustin Pop
        kpath = "(default: %s)" % constants.XEN_KERNEL
718 a8340917 Iustin Pop
      else:
719 a8340917 Iustin Pop
        kpath = instance["kernel_path"]
720 a8340917 Iustin Pop
      buf.write("  Kernel path: %s\n" % kpath)
721 a8340917 Iustin Pop
      if instance["initrd_path"] in (None, constants.VALUE_DEFAULT):
722 a8340917 Iustin Pop
        initrd = "(default: %s)" % constants.XEN_INITRD
723 a8340917 Iustin Pop
      elif instance["initrd_path"] == constants.VALUE_NONE:
724 a8340917 Iustin Pop
        initrd = "(none)"
725 a8340917 Iustin Pop
      else:
726 a8340917 Iustin Pop
        initrd = instance["initrd_path"]
727 a8340917 Iustin Pop
      buf.write("       initrd: %s\n" % initrd)
728 a8340917 Iustin Pop
    if False not in map(instance.has_key, hvm_parameters):
729 a8340917 Iustin Pop
      buf.write("  HVM:\n")
730 a8340917 Iustin Pop
      buf.write("    - boot order: %s\n" % instance["hvm_boot_order"])
731 a8340917 Iustin Pop
      buf.write("    - ACPI support: %s\n" % instance["hvm_acpi"])
732 a8340917 Iustin Pop
      buf.write("    - PAE support: %s\n" % instance["hvm_pae"])
733 a8340917 Iustin Pop
      buf.write("    - virtual CDROM: %s\n" % instance["hvm_cdrom_image_path"])
734 ad2023a3 Alexander Schreiber
      buf.write("    - virtual NIC type: %s\n" %  instance["hvm_nic_type"])
735 ad2023a3 Alexander Schreiber
      buf.write("    - virtual disk type: %s\n" %  instance["hvm_disk_type"])
736 a8340917 Iustin Pop
    if instance.has_key("vnc_bind_address"):
737 a8340917 Iustin Pop
      buf.write("  VNC bind address: %s\n" % instance["vnc_bind_address"])
738 34b6ab97 Alexander Schreiber
      buf.write("  VNC console port: %s\n" % instance["vnc_console_port"])
739 a8083063 Iustin Pop
    buf.write("  Hardware:\n")
740 f55ff7ec Iustin Pop
    buf.write("    - VCPUs: %d\n" % instance["vcpus"])
741 a8083063 Iustin Pop
    buf.write("    - memory: %dMiB\n" % instance["memory"])
742 a8083063 Iustin Pop
    buf.write("    - NICs: %s\n" %
743 a8083063 Iustin Pop
        ", ".join(["{MAC: %s, IP: %s, bridge: %s}" %
744 a8083063 Iustin Pop
                   (mac, ip, bridge)
745 a8083063 Iustin Pop
                     for mac, ip, bridge in instance["nics"]]))
746 a8083063 Iustin Pop
    buf.write("  Block devices:\n")
747 a8083063 Iustin Pop
748 a8083063 Iustin Pop
    for device in instance["disks"]:
749 a8083063 Iustin Pop
      _FormatBlockDevInfo(buf, device, 1)
750 a8083063 Iustin Pop
751 a8083063 Iustin Pop
  logger.ToStdout(buf.getvalue().rstrip('\n'))
752 a8083063 Iustin Pop
  return retcode
753 a8083063 Iustin Pop
754 a8083063 Iustin Pop
755 7767bbf5 Manuel Franceschini
def SetInstanceParams(opts, args):
756 a8083063 Iustin Pop
  """Modifies an instance.
757 a8083063 Iustin Pop
758 a8083063 Iustin Pop
  All parameters take effect only at the next restart of the instance.
759 a8083063 Iustin Pop
760 a8083063 Iustin Pop
  Args:
761 a8083063 Iustin Pop
    opts - class with options as members
762 a8083063 Iustin Pop
    args - list with a single element, the instance name
763 a8083063 Iustin Pop
  Opts used:
764 a8083063 Iustin Pop
    memory - the new memory size
765 a8083063 Iustin Pop
    vcpus - the new number of cpus
766 1862d460 Alexander Schreiber
    mac - the new MAC address of the instance
767 a8083063 Iustin Pop
768 a8083063 Iustin Pop
  """
769 973d7867 Iustin Pop
  if not (opts.mem or opts.vcpus or opts.ip or opts.bridge or opts.mac or
770 31a853d2 Iustin Pop
          opts.kernel_path or opts.initrd_path or opts.hvm_boot_order or
771 917f91d5 Iustin Pop
          opts.hvm_acpi or opts.hvm_pae or opts.hvm_cdrom_image_path or
772 ad2023a3 Alexander Schreiber
          opts.vnc_bind_address or opts.hvm_nic_type or opts.hvm_disk_type):
773 a8083063 Iustin Pop
    logger.ToStdout("Please give at least one of the parameters.")
774 a8083063 Iustin Pop
    return 1
775 a8083063 Iustin Pop
776 973d7867 Iustin Pop
  kernel_path = _TransformPath(opts.kernel_path)
777 973d7867 Iustin Pop
  initrd_path = _TransformPath(opts.initrd_path)
778 40cece88 Iustin Pop
  if opts.hvm_boot_order == 'default':
779 25c5878d Alexander Schreiber
    hvm_boot_order = constants.VALUE_DEFAULT
780 25c5878d Alexander Schreiber
  else:
781 25c5878d Alexander Schreiber
    hvm_boot_order = opts.hvm_boot_order
782 973d7867 Iustin Pop
783 3fc175f0 Alexander Schreiber
  if opts.hvm_acpi is None:
784 3fc175f0 Alexander Schreiber
    hvm_acpi = opts.hvm_acpi
785 3fc175f0 Alexander Schreiber
  else:
786 3fc175f0 Alexander Schreiber
    hvm_acpi = opts.hvm_acpi == _VALUE_TRUE
787 31a853d2 Iustin Pop
788 3fc175f0 Alexander Schreiber
  if opts.hvm_pae is None:
789 3fc175f0 Alexander Schreiber
    hvm_pae = opts.hvm_pae
790 31a853d2 Iustin Pop
  else:
791 3fc175f0 Alexander Schreiber
    hvm_pae = opts.hvm_pae == _VALUE_TRUE
792 31a853d2 Iustin Pop
793 ad2023a3 Alexander Schreiber
  if opts.hvm_nic_type == constants.VALUE_NONE:
794 ad2023a3 Alexander Schreiber
    hvm_nic_type = None
795 ad2023a3 Alexander Schreiber
  else:
796 ad2023a3 Alexander Schreiber
    hvm_nic_type = opts.hvm_nic_type
797 ad2023a3 Alexander Schreiber
798 ad2023a3 Alexander Schreiber
  if opts.hvm_disk_type == constants.VALUE_NONE:
799 ad2023a3 Alexander Schreiber
    hvm_disk_type = None
800 ad2023a3 Alexander Schreiber
  else:
801 ad2023a3 Alexander Schreiber
    hvm_disk_type = opts.hvm_disk_type
802 ad2023a3 Alexander Schreiber
803 7767bbf5 Manuel Franceschini
  op = opcodes.OpSetInstanceParams(instance_name=args[0], mem=opts.mem,
804 7767bbf5 Manuel Franceschini
                                   vcpus=opts.vcpus, ip=opts.ip,
805 7767bbf5 Manuel Franceschini
                                   bridge=opts.bridge, mac=opts.mac,
806 7767bbf5 Manuel Franceschini
                                   kernel_path=opts.kernel_path,
807 7767bbf5 Manuel Franceschini
                                   initrd_path=opts.initrd_path,
808 31a853d2 Iustin Pop
                                   hvm_boot_order=hvm_boot_order,
809 31a853d2 Iustin Pop
                                   hvm_acpi=hvm_acpi, hvm_pae=hvm_pae,
810 3fc175f0 Alexander Schreiber
                                   hvm_cdrom_image_path=
811 3fc175f0 Alexander Schreiber
                                   opts.hvm_cdrom_image_path,
812 ad2023a3 Alexander Schreiber
                                   vnc_bind_address=opts.vnc_bind_address,
813 ad2023a3 Alexander Schreiber
                                   hvm_nic_type=hvm_nic_type,
814 4300c4b6 Guido Trotter
                                   hvm_disk_type=hvm_disk_type,
815 4300c4b6 Guido Trotter
                                   force=opts.force)
816 31a853d2 Iustin Pop
817 6340bb0a Iustin Pop
  # even if here we process the result, we allow submit only
818 6340bb0a Iustin Pop
  result = SubmitOrSend(op, opts)
819 a8083063 Iustin Pop
820 a8083063 Iustin Pop
  if result:
821 a8083063 Iustin Pop
    logger.ToStdout("Modified instance %s" % args[0])
822 a8083063 Iustin Pop
    for param, data in result:
823 a8083063 Iustin Pop
      logger.ToStdout(" - %-5s -> %s" % (param, data))
824 a8083063 Iustin Pop
    logger.ToStdout("Please don't forget that these parameters take effect"
825 a8083063 Iustin Pop
                    " only at the next start of the instance.")
826 a8083063 Iustin Pop
  return 0
827 a8083063 Iustin Pop
828 a8083063 Iustin Pop
829 a8083063 Iustin Pop
# options used in more than one cmd
830 a8083063 Iustin Pop
node_opt = make_option("-n", "--node", dest="node", help="Target node",
831 a8083063 Iustin Pop
                       metavar="<node>")
832 a8083063 Iustin Pop
833 d0834de3 Michael Hanselmann
os_opt = cli_option("-o", "--os-type", dest="os", help="What OS to run",
834 739ecd56 Michael Hanselmann
                    metavar="<os>")
835 d0834de3 Michael Hanselmann
836 312ac745 Iustin Pop
# multi-instance selection options
837 804a1e8e Iustin Pop
m_force_multi = make_option("--force-multiple", dest="force_multi",
838 804a1e8e Iustin Pop
                            help="Do not ask for confirmation when more than"
839 804a1e8e Iustin Pop
                            " one instance is affected",
840 804a1e8e Iustin Pop
                            action="store_true", default=False)
841 804a1e8e Iustin Pop
842 312ac745 Iustin Pop
m_pri_node_opt = make_option("--primary", dest="multi_mode",
843 312ac745 Iustin Pop
                             help="Filter by nodes (primary only)",
844 312ac745 Iustin Pop
                             const=_SHUTDOWN_NODES_PRI, action="store_const")
845 312ac745 Iustin Pop
846 312ac745 Iustin Pop
m_sec_node_opt = make_option("--secondary", dest="multi_mode",
847 312ac745 Iustin Pop
                             help="Filter by nodes (secondary only)",
848 312ac745 Iustin Pop
                             const=_SHUTDOWN_NODES_SEC, action="store_const")
849 312ac745 Iustin Pop
850 312ac745 Iustin Pop
m_node_opt = make_option("--node", dest="multi_mode",
851 312ac745 Iustin Pop
                         help="Filter by nodes (primary and secondary)",
852 312ac745 Iustin Pop
                         const=_SHUTDOWN_NODES_BOTH, action="store_const")
853 312ac745 Iustin Pop
854 312ac745 Iustin Pop
m_clust_opt = make_option("--all", dest="multi_mode",
855 312ac745 Iustin Pop
                          help="Select all instances in the cluster",
856 312ac745 Iustin Pop
                          const=_SHUTDOWN_CLUSTER, action="store_const")
857 312ac745 Iustin Pop
858 312ac745 Iustin Pop
m_inst_opt = make_option("--instance", dest="multi_mode",
859 312ac745 Iustin Pop
                         help="Filter by instance name [default]",
860 312ac745 Iustin Pop
                         const=_SHUTDOWN_INSTANCES, action="store_const")
861 312ac745 Iustin Pop
862 312ac745 Iustin Pop
863 a8083063 Iustin Pop
# this is defined separately due to readability only
864 a8083063 Iustin Pop
add_opts = [
865 a8083063 Iustin Pop
  DEBUG_OPT,
866 60d49723 Michael Hanselmann
  make_option("-n", "--node", dest="node",
867 60d49723 Michael Hanselmann
              help="Target node and optional secondary node",
868 60d49723 Michael Hanselmann
              metavar="<pnode>[:<snode>]"),
869 b9ac33e9 Iustin Pop
  cli_option("-s", "--os-size", dest="size", help="Disk size, in MiB unless"
870 b9ac33e9 Iustin Pop
             " a suffix is used",
871 a8083063 Iustin Pop
             default=20 * 1024, type="unit", metavar="<size>"),
872 b9ac33e9 Iustin Pop
  cli_option("--swap-size", dest="swap", help="Swap size, in MiB unless a"
873 b9ac33e9 Iustin Pop
             " suffix is used",
874 a8083063 Iustin Pop
             default=4 * 1024, type="unit", metavar="<size>"),
875 d0834de3 Michael Hanselmann
  os_opt,
876 b9ac33e9 Iustin Pop
  cli_option("-m", "--memory", dest="mem", help="Memory size (in MiB)",
877 a8083063 Iustin Pop
              default=128, type="unit", metavar="<mem>"),
878 a8083063 Iustin Pop
  make_option("-p", "--cpu", dest="vcpus", help="Number of virtual CPUs",
879 a8083063 Iustin Pop
              default=1, type="int", metavar="<PROC>"),
880 a8083063 Iustin Pop
  make_option("-t", "--disk-template", dest="disk_template",
881 45873083 Manuel Franceschini
              help="Custom disk setup (diskless, file, plain or drbd)",
882 f9193417 Iustin Pop
              default=None, metavar="TEMPL"),
883 a8083063 Iustin Pop
  make_option("-i", "--ip", dest="ip",
884 a8083063 Iustin Pop
              help="IP address ('none' [default], 'auto', or specify address)",
885 a8083063 Iustin Pop
              default='none', type="string", metavar="<ADDRESS>"),
886 1862d460 Alexander Schreiber
  make_option("--mac", dest="mac",
887 1862d460 Alexander Schreiber
              help="MAC address ('auto' [default], or specify address)",
888 1862d460 Alexander Schreiber
              default='auto', type="string", metavar="<MACADDRESS>"),
889 a8083063 Iustin Pop
  make_option("--no-wait-for-sync", dest="wait_for_sync", default=True,
890 a8083063 Iustin Pop
              action="store_false", help="Don't wait for sync (DANGEROUS!)"),
891 a8083063 Iustin Pop
  make_option("-b", "--bridge", dest="bridge",
892 a8083063 Iustin Pop
              help="Bridge to connect this instance to",
893 bdd55f71 Iustin Pop
              default=None, metavar="<bridge>"),
894 bdd55f71 Iustin Pop
  make_option("--no-start", dest="start", default=True,
895 bdd55f71 Iustin Pop
              action="store_false", help="Don't start the instance after"
896 bdd55f71 Iustin Pop
              " creation"),
897 bdd55f71 Iustin Pop
  make_option("--no-ip-check", dest="ip_check", default=True,
898 bdd55f71 Iustin Pop
              action="store_false", help="Don't check that the instance's IP"
899 bdd55f71 Iustin Pop
              " is alive (only valid with --no-start)"),
900 3b6d8c9b Iustin Pop
  make_option("--kernel", dest="kernel_path",
901 3b6d8c9b Iustin Pop
              help="Path to the instances' kernel (or 'default')",
902 3b6d8c9b Iustin Pop
              default=None,
903 3b6d8c9b Iustin Pop
              type="string", metavar="<FILENAME>"),
904 3b6d8c9b Iustin Pop
  make_option("--initrd", dest="initrd_path",
905 3b6d8c9b Iustin Pop
              help="Path to the instances' initrd (or 'none', or 'default')",
906 3b6d8c9b Iustin Pop
              default=None,
907 3b6d8c9b Iustin Pop
              type="string", metavar="<FILENAME>"),
908 25c5878d Alexander Schreiber
  make_option("--hvm-boot-order", dest="hvm_boot_order",
909 538475ca Iustin Pop
              help="Boot device order for HVM (one or more of [acdn])",
910 25c5878d Alexander Schreiber
              default=None, type="string", metavar="<BOOTORDER>"),
911 45873083 Manuel Franceschini
  make_option("--file-storage-dir", dest="file_storage_dir",
912 45873083 Manuel Franceschini
              help="Relative path under default cluster-wide file storage dir"
913 45873083 Manuel Franceschini
              " to store file-based disks", default=None,
914 45873083 Manuel Franceschini
              metavar="<DIR>"),
915 45873083 Manuel Franceschini
  make_option("--file-driver", dest="file_driver", help="Driver to use"
916 538475ca Iustin Pop
              " for image files", default="loop", metavar="<DRIVER>"),
917 538475ca Iustin Pop
  make_option("--iallocator", metavar="<NAME>",
918 538475ca Iustin Pop
              help="Select nodes for the instance automatically using the"
919 538475ca Iustin Pop
              " <NAME> iallocator plugin", default=None, type="string"),
920 31a853d2 Iustin Pop
  make_option("--hvm-acpi", dest="hvm_acpi",
921 31a853d2 Iustin Pop
              help="ACPI support for HVM (true|false)",
922 31a853d2 Iustin Pop
              metavar="<BOOL>", choices=["true", "false"]),
923 ad2023a3 Alexander Schreiber
  make_option("--hvm-nic-type", dest="hvm_nic_type",
924 ad2023a3 Alexander Schreiber
              help="Type of virtual NIC for HVM "
925 ad2023a3 Alexander Schreiber
              "(rtl8139,ne2k_pci,ne2k_isa,paravirtual)",
926 ad2023a3 Alexander Schreiber
              metavar="NICTYPE", choices=[constants.HT_HVM_NIC_RTL8139,
927 ad2023a3 Alexander Schreiber
                                          constants.HT_HVM_NIC_NE2K_PCI,
928 ad2023a3 Alexander Schreiber
                                          constants.HT_HVM_NIC_NE2K_ISA,
929 ad2023a3 Alexander Schreiber
                                          constants.HT_HVM_DEV_PARAVIRTUAL],
930 ad2023a3 Alexander Schreiber
              default=constants.HT_HVM_NIC_RTL8139),
931 ad2023a3 Alexander Schreiber
  make_option("--hvm-disk-type", dest="hvm_disk_type",
932 ad2023a3 Alexander Schreiber
              help="Type of virtual disks for HVM (ioemu,paravirtual)",
933 ad2023a3 Alexander Schreiber
              metavar="DISKTYPE", choices=[constants.HT_HVM_DEV_IOEMU,
934 ad2023a3 Alexander Schreiber
                                           constants.HT_HVM_DEV_PARAVIRTUAL],
935 ad2023a3 Alexander Schreiber
              default=constants.HT_HVM_DEV_IOEMU,),
936 31a853d2 Iustin Pop
  make_option("--hvm-pae", dest="hvm_pae",
937 31a853d2 Iustin Pop
              help="PAE support for HVM (true|false)",
938 31a853d2 Iustin Pop
              metavar="<BOOL>", choices=["true", "false"]),
939 31a853d2 Iustin Pop
  make_option("--hvm-cdrom-image-path", dest="hvm_cdrom_image_path",
940 31a853d2 Iustin Pop
              help="CDROM image path for HVM (absolute path or None)",
941 31a853d2 Iustin Pop
              default=None, type="string", metavar="<CDROMIMAGE>"),
942 31a853d2 Iustin Pop
  make_option("--vnc-bind-address", dest="vnc_bind_address",
943 31a853d2 Iustin Pop
              help="bind address for VNC (IP address)",
944 31a853d2 Iustin Pop
              default=None, type="string", metavar="<VNCADDRESS>"),
945 6340bb0a Iustin Pop
  SUBMIT_OPT,
946 a8083063 Iustin Pop
  ]
947 a8083063 Iustin Pop
948 a8083063 Iustin Pop
commands = {
949 a8083063 Iustin Pop
  'add': (AddInstance, ARGS_ONE, add_opts,
950 bdb7d4e8 Michael Hanselmann
          "[...] -t disk-type -n node[:secondary-node] -o os-type <name>",
951 a8083063 Iustin Pop
          "Creates and adds a new instance to the cluster"),
952 51c6e7b5 Michael Hanselmann
  'console': (ConnectToInstanceConsole, ARGS_ONE,
953 51c6e7b5 Michael Hanselmann
              [DEBUG_OPT,
954 51c6e7b5 Michael Hanselmann
               make_option("--show-cmd", dest="show_command",
955 51c6e7b5 Michael Hanselmann
                           action="store_true", default=False,
956 51c6e7b5 Michael Hanselmann
                           help=("Show command instead of executing it"))],
957 9a033156 Iustin Pop
              "[--show-cmd] <instance>",
958 a8083063 Iustin Pop
              "Opens a console on the specified instance"),
959 80de0e3f Iustin Pop
  'failover': (FailoverInstance, ARGS_ONE,
960 fe7b0351 Michael Hanselmann
               [DEBUG_OPT, FORCE_OPT,
961 a8083063 Iustin Pop
                make_option("--ignore-consistency", dest="ignore_consistency",
962 a8083063 Iustin Pop
                            action="store_true", default=False,
963 a8083063 Iustin Pop
                            help="Ignore the consistency of the disks on"
964 a8083063 Iustin Pop
                            " the secondary"),
965 6340bb0a Iustin Pop
                SUBMIT_OPT,
966 a8083063 Iustin Pop
                ],
967 9a033156 Iustin Pop
               "[-f] <instance>",
968 a8083063 Iustin Pop
               "Stops the instance and starts it on the backup node, using"
969 99e2be3b Guido Trotter
               " the remote mirror (only for instances of type drbd)"),
970 9a033156 Iustin Pop
  'info': (ShowInstanceConfig, ARGS_ANY, [DEBUG_OPT], "[<instance>...]",
971 a8083063 Iustin Pop
           "Show information on the specified instance"),
972 a8083063 Iustin Pop
  'list': (ListInstances, ARGS_NONE,
973 9a033156 Iustin Pop
           [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], "",
974 d8052456 Iustin Pop
           "Lists the instances and their status. The available fields are"
975 d8052456 Iustin Pop
           " (see the man page for details): status, oper_state, oper_ram,"
976 d8a4b51d Iustin Pop
           " name, os, pnode, snodes, admin_state, admin_ram, disk_template,"
977 38d7239a Iustin Pop
           " ip, mac, bridge, sda_size, sdb_size, vcpus, serial_no."
978 38d7239a Iustin Pop
           " The default field"
979 48c4dfa8 Iustin Pop
           " list is (in order): %s." % ", ".join(_LIST_DEF_FIELDS),
980 48c4dfa8 Iustin Pop
           ),
981 20e23543 Alexander Schreiber
  'reinstall': (ReinstallInstance, ARGS_ONE,
982 20e23543 Alexander Schreiber
                [DEBUG_OPT, FORCE_OPT, os_opt,
983 20e23543 Alexander Schreiber
                 make_option("--select-os", dest="select_os",
984 20e23543 Alexander Schreiber
                             action="store_true", default=False,
985 20e23543 Alexander Schreiber
                             help="Interactive OS reinstall, lists available"
986 6340bb0a Iustin Pop
                             " OS templates for selection"),
987 6340bb0a Iustin Pop
                 SUBMIT_OPT,
988 6340bb0a Iustin Pop
                 ],
989 9a033156 Iustin Pop
                "[-f] <instance>", "Reinstall a stopped instance"),
990 1d67656e Iustin Pop
  'remove': (RemoveInstance, ARGS_ONE,
991 1d67656e Iustin Pop
             [DEBUG_OPT, FORCE_OPT,
992 1d67656e Iustin Pop
              make_option("--ignore-failures", dest="ignore_failures",
993 1d67656e Iustin Pop
                          action="store_true", default=False,
994 1d67656e Iustin Pop
                          help=("Remove the instance from the cluster even"
995 1d67656e Iustin Pop
                                " if there are failures during the removal"
996 1d67656e Iustin Pop
                                " process (shutdown, disk removal, etc.)")),
997 6340bb0a Iustin Pop
              SUBMIT_OPT,
998 1d67656e Iustin Pop
              ],
999 9a033156 Iustin Pop
             "[-f] <instance>", "Shuts down the instance and removes it"),
1000 decd5f45 Iustin Pop
  'rename': (RenameInstance, ARGS_FIXED(2),
1001 decd5f45 Iustin Pop
             [DEBUG_OPT,
1002 decd5f45 Iustin Pop
              make_option("--no-ip-check", dest="ignore_ip",
1003 decd5f45 Iustin Pop
                          help="Do not check that the IP of the new name"
1004 decd5f45 Iustin Pop
                          " is alive",
1005 decd5f45 Iustin Pop
                          default=False, action="store_true"),
1006 6340bb0a Iustin Pop
              SUBMIT_OPT,
1007 decd5f45 Iustin Pop
              ],
1008 9a033156 Iustin Pop
             "<instance> <new_name>", "Rename the instance"),
1009 a8083063 Iustin Pop
  'replace-disks': (ReplaceDisks, ARGS_ONE,
1010 a8083063 Iustin Pop
                    [DEBUG_OPT,
1011 a8083063 Iustin Pop
                     make_option("-n", "--new-secondary", dest="new_secondary",
1012 a9e0c397 Iustin Pop
                                 help=("New secondary node (for secondary"
1013 a9e0c397 Iustin Pop
                                       " node change)"), metavar="NODE"),
1014 a9e0c397 Iustin Pop
                     make_option("-p", "--on-primary", dest="on_primary",
1015 a9e0c397 Iustin Pop
                                 default=False, action="store_true",
1016 a9e0c397 Iustin Pop
                                 help=("Replace the disk(s) on the primary"
1017 12c3449a Michael Hanselmann
                                       " node (only for the drbd template)")),
1018 a9e0c397 Iustin Pop
                     make_option("-s", "--on-secondary", dest="on_secondary",
1019 a9e0c397 Iustin Pop
                                 default=False, action="store_true",
1020 a9e0c397 Iustin Pop
                                 help=("Replace the disk(s) on the secondary"
1021 12c3449a Michael Hanselmann
                                       " node (only for the drbd template)")),
1022 a9e0c397 Iustin Pop
                     make_option("--disks", dest="disks", default=None,
1023 a9e0c397 Iustin Pop
                                 help=("Comma-separated list of disks"
1024 a9e0c397 Iustin Pop
                                       " to replace (e.g. sda) (optional,"
1025 a9e0c397 Iustin Pop
                                       " defaults to all disks")),
1026 b6e82a65 Iustin Pop
                     make_option("--iallocator", metavar="<NAME>",
1027 b6e82a65 Iustin Pop
                                 help="Select new secondary for the instance"
1028 b6e82a65 Iustin Pop
                                 " automatically using the"
1029 b6e82a65 Iustin Pop
                                 " <NAME> iallocator plugin (enables"
1030 b6e82a65 Iustin Pop
                                 " secondary node replacement)",
1031 b6e82a65 Iustin Pop
                                 default=None, type="string"),
1032 6340bb0a Iustin Pop
                     SUBMIT_OPT,
1033 a9e0c397 Iustin Pop
                     ],
1034 9a033156 Iustin Pop
                    "[-s|-p|-n NODE] <instance>",
1035 a8083063 Iustin Pop
                    "Replaces all disks for the instance"),
1036 7767bbf5 Manuel Franceschini
  'modify': (SetInstanceParams, ARGS_ONE,
1037 fe7b0351 Michael Hanselmann
             [DEBUG_OPT, FORCE_OPT,
1038 a8083063 Iustin Pop
              cli_option("-m", "--memory", dest="mem",
1039 a8083063 Iustin Pop
                         help="Memory size",
1040 a8083063 Iustin Pop
                         default=None, type="unit", metavar="<mem>"),
1041 a8083063 Iustin Pop
              make_option("-p", "--cpu", dest="vcpus",
1042 a8083063 Iustin Pop
                          help="Number of virtual CPUs",
1043 a8083063 Iustin Pop
                          default=None, type="int", metavar="<PROC>"),
1044 a8083063 Iustin Pop
              make_option("-i", "--ip", dest="ip",
1045 a8083063 Iustin Pop
                          help="IP address ('none' or numeric IP)",
1046 a8083063 Iustin Pop
                          default=None, type="string", metavar="<ADDRESS>"),
1047 a8083063 Iustin Pop
              make_option("-b", "--bridge", dest="bridge",
1048 a8083063 Iustin Pop
                          help="Bridge to connect this instance to",
1049 decd5f45 Iustin Pop
                          default=None, type="string", metavar="<bridge>"),
1050 1862d460 Alexander Schreiber
              make_option("--mac", dest="mac",
1051 1862d460 Alexander Schreiber
                          help="MAC address", default=None,
1052 1862d460 Alexander Schreiber
                          type="string", metavar="<MACADDRESS>"),
1053 973d7867 Iustin Pop
              make_option("--kernel", dest="kernel_path",
1054 973d7867 Iustin Pop
                          help="Path to the instances' kernel (or"
1055 973d7867 Iustin Pop
                          " 'default')", default=None,
1056 973d7867 Iustin Pop
                          type="string", metavar="<FILENAME>"),
1057 973d7867 Iustin Pop
              make_option("--initrd", dest="initrd_path",
1058 973d7867 Iustin Pop
                          help="Path to the instances' initrd (or 'none', or"
1059 973d7867 Iustin Pop
                          " 'default')", default=None,
1060 973d7867 Iustin Pop
                          type="string", metavar="<FILENAME>"),
1061 25c5878d Alexander Schreiber
              make_option("--hvm-boot-order", dest="hvm_boot_order",
1062 25c5878d Alexander Schreiber
                          help="boot device order for HVM"
1063 25c5878d Alexander Schreiber
                          "(either one or more of [acdn] or 'default')",
1064 25c5878d Alexander Schreiber
                          default=None, type="string", metavar="<BOOTORDER>"),
1065 31a853d2 Iustin Pop
              make_option("--hvm-acpi", dest="hvm_acpi",
1066 31a853d2 Iustin Pop
                          help="ACPI support for HVM (true|false)",
1067 31a853d2 Iustin Pop
                          metavar="<BOOL>", choices=["true", "false"]),
1068 31a853d2 Iustin Pop
              make_option("--hvm-pae", dest="hvm_pae",
1069 31a853d2 Iustin Pop
                          help="PAE support for HVM (true|false)",
1070 31a853d2 Iustin Pop
                          metavar="<BOOL>", choices=["true", "false"]),
1071 31a853d2 Iustin Pop
              make_option("--hvm-cdrom-image-path",
1072 31a853d2 Iustin Pop
                          dest="hvm_cdrom_image_path",
1073 31a853d2 Iustin Pop
                          help="CDROM image path for HVM"
1074 31a853d2 Iustin Pop
                          "(absolute path or None)",
1075 31a853d2 Iustin Pop
                          default=None, type="string", metavar="<CDROMIMAGE>"),
1076 ad2023a3 Alexander Schreiber
              make_option("--hvm-nic-type", dest="hvm_nic_type",
1077 ad2023a3 Alexander Schreiber
                          help="Type of virtual NIC for HVM "
1078 ad2023a3 Alexander Schreiber
                          "(rtl8139,ne2k_pci,ne2k_isa,paravirtual)",
1079 ad2023a3 Alexander Schreiber
                          metavar="NICTYPE",
1080 ad2023a3 Alexander Schreiber
                          choices=[constants.HT_HVM_NIC_RTL8139,
1081 ad2023a3 Alexander Schreiber
                                   constants.HT_HVM_NIC_NE2K_PCI,
1082 ad2023a3 Alexander Schreiber
                                   constants.HT_HVM_NIC_NE2K_ISA,
1083 ad2023a3 Alexander Schreiber
                                   constants.HT_HVM_DEV_PARAVIRTUAL],
1084 ad2023a3 Alexander Schreiber
                          default=None),
1085 ad2023a3 Alexander Schreiber
              make_option("--hvm-disk-type", dest="hvm_disk_type",
1086 ad2023a3 Alexander Schreiber
                          help="Type of virtual disks for HVM "
1087 ad2023a3 Alexander Schreiber
                          "(ioemu,paravirtual)",
1088 ad2023a3 Alexander Schreiber
                          metavar="DISKTYPE",
1089 ad2023a3 Alexander Schreiber
                          choices=[constants.HT_HVM_DEV_IOEMU,
1090 ad2023a3 Alexander Schreiber
                                   constants.HT_HVM_DEV_PARAVIRTUAL],
1091 ad2023a3 Alexander Schreiber
                          default=None),
1092 31a853d2 Iustin Pop
              make_option("--vnc-bind-address", dest="vnc_bind_address",
1093 31a853d2 Iustin Pop
                          help="bind address for VNC (IP address)",
1094 31a853d2 Iustin Pop
                          default=None, type="string", metavar="<VNCADDRESS>"),
1095 6340bb0a Iustin Pop
              SUBMIT_OPT,
1096 a8083063 Iustin Pop
              ],
1097 9a033156 Iustin Pop
             "<instance>", "Alters the parameters of an instance"),
1098 312ac745 Iustin Pop
  'shutdown': (ShutdownInstance, ARGS_ANY,
1099 312ac745 Iustin Pop
               [DEBUG_OPT, m_node_opt, m_pri_node_opt, m_sec_node_opt,
1100 6340bb0a Iustin Pop
                m_clust_opt, m_inst_opt, m_force_multi,
1101 6340bb0a Iustin Pop
                SUBMIT_OPT,
1102 6340bb0a Iustin Pop
                ],
1103 9a033156 Iustin Pop
               "<instance>", "Stops an instance"),
1104 312ac745 Iustin Pop
  'startup': (StartupInstance, ARGS_ANY,
1105 804a1e8e Iustin Pop
              [DEBUG_OPT, FORCE_OPT, m_force_multi,
1106 a8083063 Iustin Pop
               make_option("-e", "--extra", dest="extra_args",
1107 a8083063 Iustin Pop
                           help="Extra arguments for the instance's kernel",
1108 a8083063 Iustin Pop
                           default=None, type="string", metavar="<PARAMS>"),
1109 312ac745 Iustin Pop
               m_node_opt, m_pri_node_opt, m_sec_node_opt,
1110 312ac745 Iustin Pop
               m_clust_opt, m_inst_opt,
1111 6340bb0a Iustin Pop
               SUBMIT_OPT,
1112 a8083063 Iustin Pop
               ],
1113 9a033156 Iustin Pop
            "<instance>", "Starts an instance"),
1114 579d4337 Alexander Schreiber
1115 579d4337 Alexander Schreiber
  'reboot': (RebootInstance, ARGS_ANY,
1116 579d4337 Alexander Schreiber
              [DEBUG_OPT, m_force_multi,
1117 579d4337 Alexander Schreiber
               make_option("-e", "--extra", dest="extra_args",
1118 579d4337 Alexander Schreiber
                           help="Extra arguments for the instance's kernel",
1119 579d4337 Alexander Schreiber
                           default=None, type="string", metavar="<PARAMS>"),
1120 579d4337 Alexander Schreiber
               make_option("-t", "--type", dest="reboot_type",
1121 579d4337 Alexander Schreiber
                           help="Type of reboot: soft/hard/full",
1122 579d4337 Alexander Schreiber
                           default=constants.INSTANCE_REBOOT_SOFT,
1123 579d4337 Alexander Schreiber
                           type="string", metavar="<REBOOT>"),
1124 579d4337 Alexander Schreiber
               make_option("--ignore-secondaries", dest="ignore_secondaries",
1125 579d4337 Alexander Schreiber
                           default=False, action="store_true",
1126 579d4337 Alexander Schreiber
                           help="Ignore errors from secondaries"),
1127 579d4337 Alexander Schreiber
               m_node_opt, m_pri_node_opt, m_sec_node_opt,
1128 579d4337 Alexander Schreiber
               m_clust_opt, m_inst_opt,
1129 6340bb0a Iustin Pop
               SUBMIT_OPT,
1130 579d4337 Alexander Schreiber
               ],
1131 9a033156 Iustin Pop
            "<instance>", "Reboots an instance"),
1132 6340bb0a Iustin Pop
  'activate-disks': (ActivateDisks, ARGS_ONE, [DEBUG_OPT, SUBMIT_OPT],
1133 9a033156 Iustin Pop
                     "<instance>",
1134 a8083063 Iustin Pop
                     "Activate an instance's disks"),
1135 6340bb0a Iustin Pop
  'deactivate-disks': (DeactivateDisks, ARGS_ONE, [DEBUG_OPT, SUBMIT_OPT],
1136 9a033156 Iustin Pop
                       "<instance>",
1137 a8083063 Iustin Pop
                       "Deactivate an instance's disks"),
1138 6340bb0a Iustin Pop
  'grow-disk': (GrowDisk, ARGS_FIXED(3), [DEBUG_OPT, SUBMIT_OPT],
1139 c6e911bc Iustin Pop
                "<instance> <disk> <size>", "Grow an instance's disk"),
1140 846baef9 Iustin Pop
  'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT],
1141 746ea1da Guido Trotter
                "<instance_name>", "List the tags of the given instance"),
1142 810c50b7 Iustin Pop
  'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
1143 746ea1da Guido Trotter
               "<instance_name> tag...", "Add tags to the given instance"),
1144 810c50b7 Iustin Pop
  'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
1145 746ea1da Guido Trotter
                  "<instance_name> tag...", "Remove tags from given instance"),
1146 a8083063 Iustin Pop
  }
1147 a8083063 Iustin Pop
1148 dbfd89dd Guido Trotter
aliases = {
1149 dbfd89dd Guido Trotter
  'activate_block_devs': 'activate-disks',
1150 00ce8b29 Guido Trotter
  'replace_disks': 'replace-disks',
1151 536fda25 Guido Trotter
  'start': 'startup',
1152 536fda25 Guido Trotter
  'stop': 'shutdown',
1153 dbfd89dd Guido Trotter
  }
1154 dbfd89dd Guido Trotter
1155 a8083063 Iustin Pop
if __name__ == '__main__':
1156 dbfd89dd Guido Trotter
  sys.exit(GenericMain(commands, aliases=aliases,
1157 846baef9 Iustin Pop
                       override={"tag_type": constants.TAG_INSTANCE}))