Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib.py @ 9440aeab

History | View | Annotate | Download (143.1 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 880478f8 Iustin Pop
"""Module implementing the master-side code."""
23 a8083063 Iustin Pop
24 a8083063 Iustin Pop
# pylint: disable-msg=W0613,W0201
25 a8083063 Iustin Pop
26 a8083063 Iustin Pop
import os
27 a8083063 Iustin Pop
import os.path
28 a8083063 Iustin Pop
import sha
29 a8083063 Iustin Pop
import time
30 a8083063 Iustin Pop
import tempfile
31 a8083063 Iustin Pop
import re
32 a8083063 Iustin Pop
import platform
33 a8083063 Iustin Pop
34 a8083063 Iustin Pop
from ganeti import rpc
35 a8083063 Iustin Pop
from ganeti import ssh
36 a8083063 Iustin Pop
from ganeti import logger
37 a8083063 Iustin Pop
from ganeti import utils
38 a8083063 Iustin Pop
from ganeti import errors
39 a8083063 Iustin Pop
from ganeti import hypervisor
40 a8083063 Iustin Pop
from ganeti import config
41 a8083063 Iustin Pop
from ganeti import constants
42 a8083063 Iustin Pop
from ganeti import objects
43 a8083063 Iustin Pop
from ganeti import opcodes
44 a8083063 Iustin Pop
from ganeti import ssconf
45 a8083063 Iustin Pop
46 a8083063 Iustin Pop
class LogicalUnit(object):
47 396e1b78 Michael Hanselmann
  """Logical Unit base class.
48 a8083063 Iustin Pop

49 a8083063 Iustin Pop
  Subclasses must follow these rules:
50 a8083063 Iustin Pop
    - implement CheckPrereq which also fills in the opcode instance
51 a8083063 Iustin Pop
      with all the fields (even if as None)
52 a8083063 Iustin Pop
    - implement Exec
53 a8083063 Iustin Pop
    - implement BuildHooksEnv
54 a8083063 Iustin Pop
    - redefine HPATH and HTYPE
55 a8083063 Iustin Pop
    - optionally redefine their run requirements (REQ_CLUSTER,
56 a8083063 Iustin Pop
      REQ_MASTER); note that all commands require root permissions
57 a8083063 Iustin Pop

58 a8083063 Iustin Pop
  """
59 a8083063 Iustin Pop
  HPATH = None
60 a8083063 Iustin Pop
  HTYPE = None
61 a8083063 Iustin Pop
  _OP_REQP = []
62 a8083063 Iustin Pop
  REQ_CLUSTER = True
63 a8083063 Iustin Pop
  REQ_MASTER = True
64 a8083063 Iustin Pop
65 a8083063 Iustin Pop
  def __init__(self, processor, op, cfg, sstore):
66 a8083063 Iustin Pop
    """Constructor for LogicalUnit.
67 a8083063 Iustin Pop

68 a8083063 Iustin Pop
    This needs to be overriden in derived classes in order to check op
69 a8083063 Iustin Pop
    validity.
70 a8083063 Iustin Pop

71 a8083063 Iustin Pop
    """
72 5bfac263 Iustin Pop
    self.proc = processor
73 a8083063 Iustin Pop
    self.op = op
74 a8083063 Iustin Pop
    self.cfg = cfg
75 a8083063 Iustin Pop
    self.sstore = sstore
76 a8083063 Iustin Pop
    for attr_name in self._OP_REQP:
77 a8083063 Iustin Pop
      attr_val = getattr(op, attr_name, None)
78 a8083063 Iustin Pop
      if attr_val is None:
79 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Required parameter '%s' missing" %
80 3ecf6786 Iustin Pop
                                   attr_name)
81 a8083063 Iustin Pop
    if self.REQ_CLUSTER:
82 a8083063 Iustin Pop
      if not cfg.IsCluster():
83 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Cluster not initialized yet,"
84 3ecf6786 Iustin Pop
                                   " use 'gnt-cluster init' first.")
85 a8083063 Iustin Pop
      if self.REQ_MASTER:
86 880478f8 Iustin Pop
        master = sstore.GetMasterNode()
87 89e1fc26 Iustin Pop
        if master != utils.HostInfo().name:
88 3ecf6786 Iustin Pop
          raise errors.OpPrereqError("Commands must be run on the master"
89 3ecf6786 Iustin Pop
                                     " node %s" % master)
90 a8083063 Iustin Pop
91 a8083063 Iustin Pop
  def CheckPrereq(self):
92 a8083063 Iustin Pop
    """Check prerequisites for this LU.
93 a8083063 Iustin Pop

94 a8083063 Iustin Pop
    This method should check that the prerequisites for the execution
95 a8083063 Iustin Pop
    of this LU are fulfilled. It can do internode communication, but
96 a8083063 Iustin Pop
    it should be idempotent - no cluster or system changes are
97 a8083063 Iustin Pop
    allowed.
98 a8083063 Iustin Pop

99 a8083063 Iustin Pop
    The method should raise errors.OpPrereqError in case something is
100 a8083063 Iustin Pop
    not fulfilled. Its return value is ignored.
101 a8083063 Iustin Pop

102 a8083063 Iustin Pop
    This method should also update all the parameters of the opcode to
103 a8083063 Iustin Pop
    their canonical form; e.g. a short node name must be fully
104 a8083063 Iustin Pop
    expanded after this method has successfully completed (so that
105 a8083063 Iustin Pop
    hooks, logging, etc. work correctly).
106 a8083063 Iustin Pop

107 a8083063 Iustin Pop
    """
108 a8083063 Iustin Pop
    raise NotImplementedError
109 a8083063 Iustin Pop
110 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
111 a8083063 Iustin Pop
    """Execute the LU.
112 a8083063 Iustin Pop

113 a8083063 Iustin Pop
    This method should implement the actual work. It should raise
114 a8083063 Iustin Pop
    errors.OpExecError for failures that are somewhat dealt with in
115 a8083063 Iustin Pop
    code, or expected.
116 a8083063 Iustin Pop

117 a8083063 Iustin Pop
    """
118 a8083063 Iustin Pop
    raise NotImplementedError
119 a8083063 Iustin Pop
120 a8083063 Iustin Pop
  def BuildHooksEnv(self):
121 a8083063 Iustin Pop
    """Build hooks environment for this LU.
122 a8083063 Iustin Pop

123 a8083063 Iustin Pop
    This method should return a three-node tuple consisting of: a dict
124 a8083063 Iustin Pop
    containing the environment that will be used for running the
125 a8083063 Iustin Pop
    specific hook for this LU, a list of node names on which the hook
126 a8083063 Iustin Pop
    should run before the execution, and a list of node names on which
127 a8083063 Iustin Pop
    the hook should run after the execution.
128 a8083063 Iustin Pop

129 a8083063 Iustin Pop
    The keys of the dict must not have 'GANETI_' prefixed as this will
130 a8083063 Iustin Pop
    be handled in the hooks runner. Also note additional keys will be
131 a8083063 Iustin Pop
    added by the hooks runner. If the LU doesn't define any
132 a8083063 Iustin Pop
    environment, an empty dict (and not None) should be returned.
133 a8083063 Iustin Pop

134 a8083063 Iustin Pop
    As for the node lists, the master should not be included in the
135 a8083063 Iustin Pop
    them, as it will be added by the hooks runner in case this LU
136 a8083063 Iustin Pop
    requires a cluster to run on (otherwise we don't have a node
137 a8083063 Iustin Pop
    list). No nodes should be returned as an empty list (and not
138 a8083063 Iustin Pop
    None).
139 a8083063 Iustin Pop

140 a8083063 Iustin Pop
    Note that if the HPATH for a LU class is None, this function will
141 a8083063 Iustin Pop
    not be called.
142 a8083063 Iustin Pop

143 a8083063 Iustin Pop
    """
144 a8083063 Iustin Pop
    raise NotImplementedError
145 a8083063 Iustin Pop
146 a8083063 Iustin Pop
147 a8083063 Iustin Pop
class NoHooksLU(LogicalUnit):
148 a8083063 Iustin Pop
  """Simple LU which runs no hooks.
149 a8083063 Iustin Pop

150 a8083063 Iustin Pop
  This LU is intended as a parent for other LogicalUnits which will
151 a8083063 Iustin Pop
  run no hooks, in order to reduce duplicate code.
152 a8083063 Iustin Pop

153 a8083063 Iustin Pop
  """
154 a8083063 Iustin Pop
  HPATH = None
155 a8083063 Iustin Pop
  HTYPE = None
156 a8083063 Iustin Pop
157 a8083063 Iustin Pop
  def BuildHooksEnv(self):
158 a8083063 Iustin Pop
    """Build hooks env.
159 a8083063 Iustin Pop

160 a8083063 Iustin Pop
    This is a no-op, since we don't run hooks.
161 a8083063 Iustin Pop

162 a8083063 Iustin Pop
    """
163 0e137c28 Iustin Pop
    return {}, [], []
164 a8083063 Iustin Pop
165 a8083063 Iustin Pop
166 9440aeab Michael Hanselmann
def _AddHostToEtcHosts(hostname):
167 9440aeab Michael Hanselmann
  """Wrapper around utils.SetEtcHostsEntry.
168 9440aeab Michael Hanselmann

169 9440aeab Michael Hanselmann
  """
170 9440aeab Michael Hanselmann
  hi = utils.HostInfo(name=hostname)
171 9440aeab Michael Hanselmann
  utils.SetEtcHostsEntry(constants.ETC_HOSTS, hi.ip, hi.name, [hi.ShortName()])
172 9440aeab Michael Hanselmann
173 9440aeab Michael Hanselmann
174 c8a0948f Michael Hanselmann
def _RemoveHostFromEtcHosts(hostname):
175 9440aeab Michael Hanselmann
  """Wrapper around utils.RemoveEtcHostsEntry.
176 c8a0948f Michael Hanselmann

177 c8a0948f Michael Hanselmann
  """
178 c8a0948f Michael Hanselmann
  hi = utils.HostInfo(name=hostname)
179 c8a0948f Michael Hanselmann
  utils.RemoveEtcHostsEntry(constants.ETC_HOSTS, hi.name)
180 c8a0948f Michael Hanselmann
  utils.RemoveEtcHostsEntry(constants.ETC_HOSTS, hi.ShortName())
181 c8a0948f Michael Hanselmann
182 c8a0948f Michael Hanselmann
183 dcb93971 Michael Hanselmann
def _GetWantedNodes(lu, nodes):
184 a7ba5e53 Iustin Pop
  """Returns list of checked and expanded node names.
185 83120a01 Michael Hanselmann

186 83120a01 Michael Hanselmann
  Args:
187 83120a01 Michael Hanselmann
    nodes: List of nodes (strings) or None for all
188 83120a01 Michael Hanselmann

189 83120a01 Michael Hanselmann
  """
190 3312b702 Iustin Pop
  if not isinstance(nodes, list):
191 3ecf6786 Iustin Pop
    raise errors.OpPrereqError("Invalid argument type 'nodes'")
192 dcb93971 Michael Hanselmann
193 dcb93971 Michael Hanselmann
  if nodes:
194 3312b702 Iustin Pop
    wanted = []
195 dcb93971 Michael Hanselmann
196 dcb93971 Michael Hanselmann
    for name in nodes:
197 a7ba5e53 Iustin Pop
      node = lu.cfg.ExpandNodeName(name)
198 dcb93971 Michael Hanselmann
      if node is None:
199 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("No such node name '%s'" % name)
200 3312b702 Iustin Pop
      wanted.append(node)
201 dcb93971 Michael Hanselmann
202 dcb93971 Michael Hanselmann
  else:
203 a7ba5e53 Iustin Pop
    wanted = lu.cfg.GetNodeList()
204 a7ba5e53 Iustin Pop
  return utils.NiceSort(wanted)
205 3312b702 Iustin Pop
206 3312b702 Iustin Pop
207 3312b702 Iustin Pop
def _GetWantedInstances(lu, instances):
208 a7ba5e53 Iustin Pop
  """Returns list of checked and expanded instance names.
209 3312b702 Iustin Pop

210 3312b702 Iustin Pop
  Args:
211 3312b702 Iustin Pop
    instances: List of instances (strings) or None for all
212 3312b702 Iustin Pop

213 3312b702 Iustin Pop
  """
214 3312b702 Iustin Pop
  if not isinstance(instances, list):
215 3312b702 Iustin Pop
    raise errors.OpPrereqError("Invalid argument type 'instances'")
216 3312b702 Iustin Pop
217 3312b702 Iustin Pop
  if instances:
218 3312b702 Iustin Pop
    wanted = []
219 3312b702 Iustin Pop
220 3312b702 Iustin Pop
    for name in instances:
221 a7ba5e53 Iustin Pop
      instance = lu.cfg.ExpandInstanceName(name)
222 3312b702 Iustin Pop
      if instance is None:
223 3312b702 Iustin Pop
        raise errors.OpPrereqError("No such instance name '%s'" % name)
224 3312b702 Iustin Pop
      wanted.append(instance)
225 3312b702 Iustin Pop
226 3312b702 Iustin Pop
  else:
227 a7ba5e53 Iustin Pop
    wanted = lu.cfg.GetInstanceList()
228 a7ba5e53 Iustin Pop
  return utils.NiceSort(wanted)
229 dcb93971 Michael Hanselmann
230 dcb93971 Michael Hanselmann
231 dcb93971 Michael Hanselmann
def _CheckOutputFields(static, dynamic, selected):
232 83120a01 Michael Hanselmann
  """Checks whether all selected fields are valid.
233 83120a01 Michael Hanselmann

234 83120a01 Michael Hanselmann
  Args:
235 83120a01 Michael Hanselmann
    static: Static fields
236 83120a01 Michael Hanselmann
    dynamic: Dynamic fields
237 83120a01 Michael Hanselmann

238 83120a01 Michael Hanselmann
  """
239 83120a01 Michael Hanselmann
  static_fields = frozenset(static)
240 83120a01 Michael Hanselmann
  dynamic_fields = frozenset(dynamic)
241 dcb93971 Michael Hanselmann
242 83120a01 Michael Hanselmann
  all_fields = static_fields | dynamic_fields
243 dcb93971 Michael Hanselmann
244 83120a01 Michael Hanselmann
  if not all_fields.issuperset(selected):
245 3ecf6786 Iustin Pop
    raise errors.OpPrereqError("Unknown output fields selected: %s"
246 3ecf6786 Iustin Pop
                               % ",".join(frozenset(selected).
247 3ecf6786 Iustin Pop
                                          difference(all_fields)))
248 dcb93971 Michael Hanselmann
249 dcb93971 Michael Hanselmann
250 ecb215b5 Michael Hanselmann
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
251 396e1b78 Michael Hanselmann
                          memory, vcpus, nics):
252 ecb215b5 Michael Hanselmann
  """Builds instance related env variables for hooks from single variables.
253 ecb215b5 Michael Hanselmann

254 ecb215b5 Michael Hanselmann
  Args:
255 ecb215b5 Michael Hanselmann
    secondary_nodes: List of secondary nodes as strings
256 396e1b78 Michael Hanselmann
  """
257 396e1b78 Michael Hanselmann
  env = {
258 0e137c28 Iustin Pop
    "OP_TARGET": name,
259 396e1b78 Michael Hanselmann
    "INSTANCE_NAME": name,
260 396e1b78 Michael Hanselmann
    "INSTANCE_PRIMARY": primary_node,
261 396e1b78 Michael Hanselmann
    "INSTANCE_SECONDARIES": " ".join(secondary_nodes),
262 ecb215b5 Michael Hanselmann
    "INSTANCE_OS_TYPE": os_type,
263 396e1b78 Michael Hanselmann
    "INSTANCE_STATUS": status,
264 396e1b78 Michael Hanselmann
    "INSTANCE_MEMORY": memory,
265 396e1b78 Michael Hanselmann
    "INSTANCE_VCPUS": vcpus,
266 396e1b78 Michael Hanselmann
  }
267 396e1b78 Michael Hanselmann
268 396e1b78 Michael Hanselmann
  if nics:
269 396e1b78 Michael Hanselmann
    nic_count = len(nics)
270 396e1b78 Michael Hanselmann
    for idx, (ip, bridge) in enumerate(nics):
271 396e1b78 Michael Hanselmann
      if ip is None:
272 396e1b78 Michael Hanselmann
        ip = ""
273 396e1b78 Michael Hanselmann
      env["INSTANCE_NIC%d_IP" % idx] = ip
274 396e1b78 Michael Hanselmann
      env["INSTANCE_NIC%d_BRIDGE" % idx] = bridge
275 396e1b78 Michael Hanselmann
  else:
276 396e1b78 Michael Hanselmann
    nic_count = 0
277 396e1b78 Michael Hanselmann
278 396e1b78 Michael Hanselmann
  env["INSTANCE_NIC_COUNT"] = nic_count
279 396e1b78 Michael Hanselmann
280 396e1b78 Michael Hanselmann
  return env
281 396e1b78 Michael Hanselmann
282 396e1b78 Michael Hanselmann
283 396e1b78 Michael Hanselmann
def _BuildInstanceHookEnvByObject(instance, override=None):
284 ecb215b5 Michael Hanselmann
  """Builds instance related env variables for hooks from an object.
285 ecb215b5 Michael Hanselmann

286 ecb215b5 Michael Hanselmann
  Args:
287 ecb215b5 Michael Hanselmann
    instance: objects.Instance object of instance
288 ecb215b5 Michael Hanselmann
    override: dict of values to override
289 ecb215b5 Michael Hanselmann
  """
290 396e1b78 Michael Hanselmann
  args = {
291 396e1b78 Michael Hanselmann
    'name': instance.name,
292 396e1b78 Michael Hanselmann
    'primary_node': instance.primary_node,
293 396e1b78 Michael Hanselmann
    'secondary_nodes': instance.secondary_nodes,
294 ecb215b5 Michael Hanselmann
    'os_type': instance.os,
295 396e1b78 Michael Hanselmann
    'status': instance.os,
296 396e1b78 Michael Hanselmann
    'memory': instance.memory,
297 396e1b78 Michael Hanselmann
    'vcpus': instance.vcpus,
298 396e1b78 Michael Hanselmann
    'nics': [(nic.ip, nic.bridge) for nic in instance.nics],
299 396e1b78 Michael Hanselmann
  }
300 396e1b78 Michael Hanselmann
  if override:
301 396e1b78 Michael Hanselmann
    args.update(override)
302 396e1b78 Michael Hanselmann
  return _BuildInstanceHookEnv(**args)
303 396e1b78 Michael Hanselmann
304 396e1b78 Michael Hanselmann
305 a8083063 Iustin Pop
def _UpdateKnownHosts(fullnode, ip, pubkey):
306 a8083063 Iustin Pop
  """Ensure a node has a correct known_hosts entry.
307 a8083063 Iustin Pop

308 a8083063 Iustin Pop
  Args:
309 a8083063 Iustin Pop
    fullnode - Fully qualified domain name of host. (str)
310 a8083063 Iustin Pop
    ip       - IPv4 address of host (str)
311 a8083063 Iustin Pop
    pubkey   - the public key of the cluster
312 a8083063 Iustin Pop

313 a8083063 Iustin Pop
  """
314 82122173 Iustin Pop
  if os.path.exists(constants.SSH_KNOWN_HOSTS_FILE):
315 82122173 Iustin Pop
    f = open(constants.SSH_KNOWN_HOSTS_FILE, 'r+')
316 a8083063 Iustin Pop
  else:
317 82122173 Iustin Pop
    f = open(constants.SSH_KNOWN_HOSTS_FILE, 'w+')
318 a8083063 Iustin Pop
319 a8083063 Iustin Pop
  inthere = False
320 a8083063 Iustin Pop
321 a8083063 Iustin Pop
  save_lines = []
322 a8083063 Iustin Pop
  add_lines = []
323 a8083063 Iustin Pop
  removed = False
324 a8083063 Iustin Pop
325 4cc2a728 Michael Hanselmann
  for rawline in f:
326 a8083063 Iustin Pop
    logger.Debug('read %s' % (repr(rawline),))
327 a8083063 Iustin Pop
328 4cc2a728 Michael Hanselmann
    parts = rawline.rstrip('\r\n').split()
329 4cc2a728 Michael Hanselmann
330 4cc2a728 Michael Hanselmann
    # Ignore unwanted lines
331 4cc2a728 Michael Hanselmann
    if len(parts) >= 3 and not rawline.lstrip()[0] == '#':
332 4cc2a728 Michael Hanselmann
      fields = parts[0].split(',')
333 4cc2a728 Michael Hanselmann
      key = parts[2]
334 4cc2a728 Michael Hanselmann
335 4cc2a728 Michael Hanselmann
      haveall = True
336 4cc2a728 Michael Hanselmann
      havesome = False
337 4cc2a728 Michael Hanselmann
      for spec in [ ip, fullnode ]:
338 4cc2a728 Michael Hanselmann
        if spec not in fields:
339 4cc2a728 Michael Hanselmann
          haveall = False
340 4cc2a728 Michael Hanselmann
        if spec in fields:
341 4cc2a728 Michael Hanselmann
          havesome = True
342 4cc2a728 Michael Hanselmann
343 4cc2a728 Michael Hanselmann
      logger.Debug("key, pubkey = %s." % (repr((key, pubkey)),))
344 4cc2a728 Michael Hanselmann
      if haveall and key == pubkey:
345 4cc2a728 Michael Hanselmann
        inthere = True
346 4cc2a728 Michael Hanselmann
        save_lines.append(rawline)
347 4cc2a728 Michael Hanselmann
        logger.Debug("Keeping known_hosts '%s'." % (repr(rawline),))
348 4cc2a728 Michael Hanselmann
        continue
349 4cc2a728 Michael Hanselmann
350 4cc2a728 Michael Hanselmann
      if havesome and (not haveall or key != pubkey):
351 4cc2a728 Michael Hanselmann
        removed = True
352 4cc2a728 Michael Hanselmann
        logger.Debug("Discarding known_hosts '%s'." % (repr(rawline),))
353 4cc2a728 Michael Hanselmann
        continue
354 a8083063 Iustin Pop
355 a8083063 Iustin Pop
    save_lines.append(rawline)
356 a8083063 Iustin Pop
357 a8083063 Iustin Pop
  if not inthere:
358 a8083063 Iustin Pop
    add_lines.append('%s,%s ssh-rsa %s\n' % (fullnode, ip, pubkey))
359 a8083063 Iustin Pop
    logger.Debug("Adding known_hosts '%s'." % (repr(add_lines[-1]),))
360 a8083063 Iustin Pop
361 a8083063 Iustin Pop
  if removed:
362 a8083063 Iustin Pop
    save_lines = save_lines + add_lines
363 a8083063 Iustin Pop
364 a8083063 Iustin Pop
    # Write a new file and replace old.
365 82122173 Iustin Pop
    fd, tmpname = tempfile.mkstemp('.tmp', 'known_hosts.',
366 82122173 Iustin Pop
                                   constants.DATA_DIR)
367 a8083063 Iustin Pop
    newfile = os.fdopen(fd, 'w')
368 82122173 Iustin Pop
    try:
369 82122173 Iustin Pop
      newfile.write(''.join(save_lines))
370 82122173 Iustin Pop
    finally:
371 82122173 Iustin Pop
      newfile.close()
372 a8083063 Iustin Pop
    logger.Debug("Wrote new known_hosts.")
373 82122173 Iustin Pop
    os.rename(tmpname, constants.SSH_KNOWN_HOSTS_FILE)
374 a8083063 Iustin Pop
375 a8083063 Iustin Pop
  elif add_lines:
376 a8083063 Iustin Pop
    # Simply appending a new line will do the trick.
377 a8083063 Iustin Pop
    f.seek(0, 2)
378 a8083063 Iustin Pop
    for add in add_lines:
379 a8083063 Iustin Pop
      f.write(add)
380 a8083063 Iustin Pop
381 a8083063 Iustin Pop
  f.close()
382 a8083063 Iustin Pop
383 a8083063 Iustin Pop
384 a8083063 Iustin Pop
def _HasValidVG(vglist, vgname):
385 a8083063 Iustin Pop
  """Checks if the volume group list is valid.
386 a8083063 Iustin Pop

387 a8083063 Iustin Pop
  A non-None return value means there's an error, and the return value
388 a8083063 Iustin Pop
  is the error message.
389 a8083063 Iustin Pop

390 a8083063 Iustin Pop
  """
391 a8083063 Iustin Pop
  vgsize = vglist.get(vgname, None)
392 a8083063 Iustin Pop
  if vgsize is None:
393 a8083063 Iustin Pop
    return "volume group '%s' missing" % vgname
394 a8083063 Iustin Pop
  elif vgsize < 20480:
395 191a8385 Guido Trotter
    return ("volume group '%s' too small (20480MiB required, %dMib found)" %
396 191a8385 Guido Trotter
            (vgname, vgsize))
397 a8083063 Iustin Pop
  return None
398 a8083063 Iustin Pop
399 a8083063 Iustin Pop
400 a8083063 Iustin Pop
def _InitSSHSetup(node):
401 a8083063 Iustin Pop
  """Setup the SSH configuration for the cluster.
402 a8083063 Iustin Pop

403 a8083063 Iustin Pop

404 a8083063 Iustin Pop
  This generates a dsa keypair for root, adds the pub key to the
405 a8083063 Iustin Pop
  permitted hosts and adds the hostkey to its own known hosts.
406 a8083063 Iustin Pop

407 a8083063 Iustin Pop
  Args:
408 a8083063 Iustin Pop
    node: the name of this host as a fqdn
409 a8083063 Iustin Pop

410 a8083063 Iustin Pop
  """
411 70d9e3d8 Iustin Pop
  priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS)
412 a8083063 Iustin Pop
413 70d9e3d8 Iustin Pop
  for name in priv_key, pub_key:
414 70d9e3d8 Iustin Pop
    if os.path.exists(name):
415 70d9e3d8 Iustin Pop
      utils.CreateBackup(name)
416 70d9e3d8 Iustin Pop
    utils.RemoveFile(name)
417 a8083063 Iustin Pop
418 a8083063 Iustin Pop
  result = utils.RunCmd(["ssh-keygen", "-t", "dsa",
419 70d9e3d8 Iustin Pop
                         "-f", priv_key,
420 a8083063 Iustin Pop
                         "-q", "-N", ""])
421 a8083063 Iustin Pop
  if result.failed:
422 3ecf6786 Iustin Pop
    raise errors.OpExecError("Could not generate ssh keypair, error %s" %
423 3ecf6786 Iustin Pop
                             result.output)
424 a8083063 Iustin Pop
425 70d9e3d8 Iustin Pop
  f = open(pub_key, 'r')
426 a8083063 Iustin Pop
  try:
427 70d9e3d8 Iustin Pop
    utils.AddAuthorizedKey(auth_keys, f.read(8192))
428 a8083063 Iustin Pop
  finally:
429 a8083063 Iustin Pop
    f.close()
430 a8083063 Iustin Pop
431 a8083063 Iustin Pop
432 a8083063 Iustin Pop
def _InitGanetiServerSetup(ss):
433 a8083063 Iustin Pop
  """Setup the necessary configuration for the initial node daemon.
434 a8083063 Iustin Pop

435 a8083063 Iustin Pop
  This creates the nodepass file containing the shared password for
436 a8083063 Iustin Pop
  the cluster and also generates the SSL certificate.
437 a8083063 Iustin Pop

438 a8083063 Iustin Pop
  """
439 a8083063 Iustin Pop
  # Create pseudo random password
440 a8083063 Iustin Pop
  randpass = sha.new(os.urandom(64)).hexdigest()
441 a8083063 Iustin Pop
  # and write it into sstore
442 a8083063 Iustin Pop
  ss.SetKey(ss.SS_NODED_PASS, randpass)
443 a8083063 Iustin Pop
444 a8083063 Iustin Pop
  result = utils.RunCmd(["openssl", "req", "-new", "-newkey", "rsa:1024",
445 a8083063 Iustin Pop
                         "-days", str(365*5), "-nodes", "-x509",
446 a8083063 Iustin Pop
                         "-keyout", constants.SSL_CERT_FILE,
447 a8083063 Iustin Pop
                         "-out", constants.SSL_CERT_FILE, "-batch"])
448 a8083063 Iustin Pop
  if result.failed:
449 3ecf6786 Iustin Pop
    raise errors.OpExecError("could not generate server ssl cert, command"
450 3ecf6786 Iustin Pop
                             " %s had exitcode %s and error message %s" %
451 3ecf6786 Iustin Pop
                             (result.cmd, result.exit_code, result.output))
452 a8083063 Iustin Pop
453 a8083063 Iustin Pop
  os.chmod(constants.SSL_CERT_FILE, 0400)
454 a8083063 Iustin Pop
455 a8083063 Iustin Pop
  result = utils.RunCmd([constants.NODE_INITD_SCRIPT, "restart"])
456 a8083063 Iustin Pop
457 a8083063 Iustin Pop
  if result.failed:
458 3ecf6786 Iustin Pop
    raise errors.OpExecError("Could not start the node daemon, command %s"
459 3ecf6786 Iustin Pop
                             " had exitcode %s and error %s" %
460 3ecf6786 Iustin Pop
                             (result.cmd, result.exit_code, result.output))
461 a8083063 Iustin Pop
462 a8083063 Iustin Pop
463 bf6929a2 Alexander Schreiber
def _CheckInstanceBridgesExist(instance):
464 bf6929a2 Alexander Schreiber
  """Check that the brigdes needed by an instance exist.
465 bf6929a2 Alexander Schreiber

466 bf6929a2 Alexander Schreiber
  """
467 bf6929a2 Alexander Schreiber
  # check bridges existance
468 bf6929a2 Alexander Schreiber
  brlist = [nic.bridge for nic in instance.nics]
469 bf6929a2 Alexander Schreiber
  if not rpc.call_bridges_exist(instance.primary_node, brlist):
470 bf6929a2 Alexander Schreiber
    raise errors.OpPrereqError("one or more target bridges %s does not"
471 bf6929a2 Alexander Schreiber
                               " exist on destination node '%s'" %
472 bf6929a2 Alexander Schreiber
                               (brlist, instance.primary_node))
473 bf6929a2 Alexander Schreiber
474 bf6929a2 Alexander Schreiber
475 a8083063 Iustin Pop
class LUInitCluster(LogicalUnit):
476 a8083063 Iustin Pop
  """Initialise the cluster.
477 a8083063 Iustin Pop

478 a8083063 Iustin Pop
  """
479 a8083063 Iustin Pop
  HPATH = "cluster-init"
480 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_CLUSTER
481 a8083063 Iustin Pop
  _OP_REQP = ["cluster_name", "hypervisor_type", "vg_name", "mac_prefix",
482 880478f8 Iustin Pop
              "def_bridge", "master_netdev"]
483 a8083063 Iustin Pop
  REQ_CLUSTER = False
484 a8083063 Iustin Pop
485 a8083063 Iustin Pop
  def BuildHooksEnv(self):
486 a8083063 Iustin Pop
    """Build hooks env.
487 a8083063 Iustin Pop

488 a8083063 Iustin Pop
    Notes: Since we don't require a cluster, we must manually add
489 a8083063 Iustin Pop
    ourselves in the post-run node list.
490 a8083063 Iustin Pop

491 a8083063 Iustin Pop
    """
492 0e137c28 Iustin Pop
    env = {"OP_TARGET": self.op.cluster_name}
493 0e137c28 Iustin Pop
    return env, [], [self.hostname.name]
494 a8083063 Iustin Pop
495 a8083063 Iustin Pop
  def CheckPrereq(self):
496 a8083063 Iustin Pop
    """Verify that the passed name is a valid one.
497 a8083063 Iustin Pop

498 a8083063 Iustin Pop
    """
499 a8083063 Iustin Pop
    if config.ConfigWriter.IsCluster():
500 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Cluster is already initialised")
501 a8083063 Iustin Pop
502 89e1fc26 Iustin Pop
    self.hostname = hostname = utils.HostInfo()
503 ff98055b Iustin Pop
504 bcf043c9 Iustin Pop
    if hostname.ip.startswith("127."):
505 130e907e Iustin Pop
      raise errors.OpPrereqError("This host's IP resolves to the private"
506 130e907e Iustin Pop
                                 " range (%s). Please fix DNS or /etc/hosts." %
507 bcf043c9 Iustin Pop
                                 (hostname.ip,))
508 130e907e Iustin Pop
509 89e1fc26 Iustin Pop
    self.clustername = clustername = utils.HostInfo(self.op.cluster_name)
510 a8083063 Iustin Pop
511 16abfbc2 Alexander Schreiber
    if not utils.TcpPing(constants.LOCALHOST_IP_ADDRESS, hostname.ip,
512 16abfbc2 Alexander Schreiber
                         constants.DEFAULT_NODED_PORT):
513 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Inconsistency: this host's name resolves"
514 3ecf6786 Iustin Pop
                                 " to %s,\nbut this ip address does not"
515 3ecf6786 Iustin Pop
                                 " belong to this host."
516 bcf043c9 Iustin Pop
                                 " Aborting." % hostname.ip)
517 a8083063 Iustin Pop
518 a8083063 Iustin Pop
    secondary_ip = getattr(self.op, "secondary_ip", None)
519 a8083063 Iustin Pop
    if secondary_ip and not utils.IsValidIP(secondary_ip):
520 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Invalid secondary ip given")
521 16abfbc2 Alexander Schreiber
    if (secondary_ip and
522 16abfbc2 Alexander Schreiber
        secondary_ip != hostname.ip and
523 16abfbc2 Alexander Schreiber
        (not utils.TcpPing(constants.LOCALHOST_IP_ADDRESS, secondary_ip,
524 16abfbc2 Alexander Schreiber
                           constants.DEFAULT_NODED_PORT))):
525 16abfbc2 Alexander Schreiber
      raise errors.OpPrereqError("You gave %s as secondary IP,\n"
526 16abfbc2 Alexander Schreiber
                                 "but it does not belong to this host." %
527 16abfbc2 Alexander Schreiber
                                 secondary_ip)
528 a8083063 Iustin Pop
    self.secondary_ip = secondary_ip
529 a8083063 Iustin Pop
530 a8083063 Iustin Pop
    # checks presence of the volume group given
531 a8083063 Iustin Pop
    vgstatus = _HasValidVG(utils.ListVolumeGroups(), self.op.vg_name)
532 a8083063 Iustin Pop
533 a8083063 Iustin Pop
    if vgstatus:
534 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Error: %s" % vgstatus)
535 a8083063 Iustin Pop
536 a8083063 Iustin Pop
    if not re.match("^[0-9a-z]{2}:[0-9a-z]{2}:[0-9a-z]{2}$",
537 a8083063 Iustin Pop
                    self.op.mac_prefix):
538 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Invalid mac prefix given '%s'" %
539 3ecf6786 Iustin Pop
                                 self.op.mac_prefix)
540 a8083063 Iustin Pop
541 a8083063 Iustin Pop
    if self.op.hypervisor_type not in hypervisor.VALID_HTYPES:
542 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Invalid hypervisor type given '%s'" %
543 3ecf6786 Iustin Pop
                                 self.op.hypervisor_type)
544 a8083063 Iustin Pop
545 880478f8 Iustin Pop
    result = utils.RunCmd(["ip", "link", "show", "dev", self.op.master_netdev])
546 880478f8 Iustin Pop
    if result.failed:
547 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Invalid master netdev given (%s): '%s'" %
548 8925faaa Iustin Pop
                                 (self.op.master_netdev,
549 8925faaa Iustin Pop
                                  result.output.strip()))
550 880478f8 Iustin Pop
551 7dd30006 Michael Hanselmann
    if not (os.path.isfile(constants.NODE_INITD_SCRIPT) and
552 7dd30006 Michael Hanselmann
            os.access(constants.NODE_INITD_SCRIPT, os.X_OK)):
553 7dd30006 Michael Hanselmann
      raise errors.OpPrereqError("Init.d script '%s' missing or not "
554 7dd30006 Michael Hanselmann
                                 "executable." % constants.NODE_INITD_SCRIPT)
555 c7b46d59 Iustin Pop
556 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
557 a8083063 Iustin Pop
    """Initialize the cluster.
558 a8083063 Iustin Pop

559 a8083063 Iustin Pop
    """
560 a8083063 Iustin Pop
    clustername = self.clustername
561 a8083063 Iustin Pop
    hostname = self.hostname
562 a8083063 Iustin Pop
563 a8083063 Iustin Pop
    # set up the simple store
564 4167825b Iustin Pop
    self.sstore = ss = ssconf.SimpleStore()
565 a8083063 Iustin Pop
    ss.SetKey(ss.SS_HYPERVISOR, self.op.hypervisor_type)
566 bcf043c9 Iustin Pop
    ss.SetKey(ss.SS_MASTER_NODE, hostname.name)
567 bcf043c9 Iustin Pop
    ss.SetKey(ss.SS_MASTER_IP, clustername.ip)
568 880478f8 Iustin Pop
    ss.SetKey(ss.SS_MASTER_NETDEV, self.op.master_netdev)
569 bcf043c9 Iustin Pop
    ss.SetKey(ss.SS_CLUSTER_NAME, clustername.name)
570 a8083063 Iustin Pop
571 a8083063 Iustin Pop
    # set up the inter-node password and certificate
572 a8083063 Iustin Pop
    _InitGanetiServerSetup(ss)
573 a8083063 Iustin Pop
574 a8083063 Iustin Pop
    # start the master ip
575 bcf043c9 Iustin Pop
    rpc.call_node_start_master(hostname.name)
576 a8083063 Iustin Pop
577 a8083063 Iustin Pop
    # set up ssh config and /etc/hosts
578 70d9e3d8 Iustin Pop
    f = open(constants.SSH_HOST_RSA_PUB, 'r')
579 a8083063 Iustin Pop
    try:
580 a8083063 Iustin Pop
      sshline = f.read()
581 a8083063 Iustin Pop
    finally:
582 a8083063 Iustin Pop
      f.close()
583 a8083063 Iustin Pop
    sshkey = sshline.split(" ")[1]
584 a8083063 Iustin Pop
585 9440aeab Michael Hanselmann
    _AddHostToEtcHosts(hostname.name)
586 a8083063 Iustin Pop
587 bcf043c9 Iustin Pop
    _UpdateKnownHosts(hostname.name, hostname.ip, sshkey)
588 a8083063 Iustin Pop
589 bcf043c9 Iustin Pop
    _InitSSHSetup(hostname.name)
590 a8083063 Iustin Pop
591 a8083063 Iustin Pop
    # init of cluster config file
592 4167825b Iustin Pop
    self.cfg = cfgw = config.ConfigWriter()
593 bcf043c9 Iustin Pop
    cfgw.InitConfig(hostname.name, hostname.ip, self.secondary_ip,
594 5fcdc80d Iustin Pop
                    sshkey, self.op.mac_prefix,
595 a8083063 Iustin Pop
                    self.op.vg_name, self.op.def_bridge)
596 a8083063 Iustin Pop
597 a8083063 Iustin Pop
598 a8083063 Iustin Pop
class LUDestroyCluster(NoHooksLU):
599 a8083063 Iustin Pop
  """Logical unit for destroying the cluster.
600 a8083063 Iustin Pop

601 a8083063 Iustin Pop
  """
602 a8083063 Iustin Pop
  _OP_REQP = []
603 a8083063 Iustin Pop
604 a8083063 Iustin Pop
  def CheckPrereq(self):
605 a8083063 Iustin Pop
    """Check prerequisites.
606 a8083063 Iustin Pop

607 a8083063 Iustin Pop
    This checks whether the cluster is empty.
608 a8083063 Iustin Pop

609 a8083063 Iustin Pop
    Any errors are signalled by raising errors.OpPrereqError.
610 a8083063 Iustin Pop

611 a8083063 Iustin Pop
    """
612 880478f8 Iustin Pop
    master = self.sstore.GetMasterNode()
613 a8083063 Iustin Pop
614 a8083063 Iustin Pop
    nodelist = self.cfg.GetNodeList()
615 db915bd1 Michael Hanselmann
    if len(nodelist) != 1 or nodelist[0] != master:
616 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("There are still %d node(s) in"
617 3ecf6786 Iustin Pop
                                 " this cluster." % (len(nodelist) - 1))
618 db915bd1 Michael Hanselmann
    instancelist = self.cfg.GetInstanceList()
619 db915bd1 Michael Hanselmann
    if instancelist:
620 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("There are still %d instance(s) in"
621 3ecf6786 Iustin Pop
                                 " this cluster." % len(instancelist))
622 a8083063 Iustin Pop
623 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
624 a8083063 Iustin Pop
    """Destroys the cluster.
625 a8083063 Iustin Pop

626 a8083063 Iustin Pop
    """
627 c8a0948f Michael Hanselmann
    master = self.sstore.GetMasterNode()
628 70d9e3d8 Iustin Pop
    priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
629 70d9e3d8 Iustin Pop
    utils.CreateBackup(priv_key)
630 70d9e3d8 Iustin Pop
    utils.CreateBackup(pub_key)
631 c8a0948f Michael Hanselmann
    rpc.call_node_leave_cluster(master)
632 c8a0948f Michael Hanselmann
    _RemoveHostFromEtcHosts(master)
633 a8083063 Iustin Pop
634 a8083063 Iustin Pop
635 a8083063 Iustin Pop
class LUVerifyCluster(NoHooksLU):
636 a8083063 Iustin Pop
  """Verifies the cluster status.
637 a8083063 Iustin Pop

638 a8083063 Iustin Pop
  """
639 a8083063 Iustin Pop
  _OP_REQP = []
640 a8083063 Iustin Pop
641 a8083063 Iustin Pop
  def _VerifyNode(self, node, file_list, local_cksum, vglist, node_result,
642 a8083063 Iustin Pop
                  remote_version, feedback_fn):
643 a8083063 Iustin Pop
    """Run multiple tests against a node.
644 a8083063 Iustin Pop

645 a8083063 Iustin Pop
    Test list:
646 a8083063 Iustin Pop
      - compares ganeti version
647 a8083063 Iustin Pop
      - checks vg existance and size > 20G
648 a8083063 Iustin Pop
      - checks config file checksum
649 a8083063 Iustin Pop
      - checks ssh to other nodes
650 a8083063 Iustin Pop

651 a8083063 Iustin Pop
    Args:
652 a8083063 Iustin Pop
      node: name of the node to check
653 a8083063 Iustin Pop
      file_list: required list of files
654 a8083063 Iustin Pop
      local_cksum: dictionary of local files and their checksums
655 098c0958 Michael Hanselmann

656 a8083063 Iustin Pop
    """
657 a8083063 Iustin Pop
    # compares ganeti version
658 a8083063 Iustin Pop
    local_version = constants.PROTOCOL_VERSION
659 a8083063 Iustin Pop
    if not remote_version:
660 a8083063 Iustin Pop
      feedback_fn(" - ERROR: connection to %s failed" % (node))
661 a8083063 Iustin Pop
      return True
662 a8083063 Iustin Pop
663 a8083063 Iustin Pop
    if local_version != remote_version:
664 a8083063 Iustin Pop
      feedback_fn("  - ERROR: sw version mismatch: master %s, node(%s) %s" %
665 a8083063 Iustin Pop
                      (local_version, node, remote_version))
666 a8083063 Iustin Pop
      return True
667 a8083063 Iustin Pop
668 a8083063 Iustin Pop
    # checks vg existance and size > 20G
669 a8083063 Iustin Pop
670 a8083063 Iustin Pop
    bad = False
671 a8083063 Iustin Pop
    if not vglist:
672 a8083063 Iustin Pop
      feedback_fn("  - ERROR: unable to check volume groups on node %s." %
673 a8083063 Iustin Pop
                      (node,))
674 a8083063 Iustin Pop
      bad = True
675 a8083063 Iustin Pop
    else:
676 a8083063 Iustin Pop
      vgstatus = _HasValidVG(vglist, self.cfg.GetVGName())
677 a8083063 Iustin Pop
      if vgstatus:
678 a8083063 Iustin Pop
        feedback_fn("  - ERROR: %s on node %s" % (vgstatus, node))
679 a8083063 Iustin Pop
        bad = True
680 a8083063 Iustin Pop
681 a8083063 Iustin Pop
    # checks config file checksum
682 a8083063 Iustin Pop
    # checks ssh to any
683 a8083063 Iustin Pop
684 a8083063 Iustin Pop
    if 'filelist' not in node_result:
685 a8083063 Iustin Pop
      bad = True
686 a8083063 Iustin Pop
      feedback_fn("  - ERROR: node hasn't returned file checksum data")
687 a8083063 Iustin Pop
    else:
688 a8083063 Iustin Pop
      remote_cksum = node_result['filelist']
689 a8083063 Iustin Pop
      for file_name in file_list:
690 a8083063 Iustin Pop
        if file_name not in remote_cksum:
691 a8083063 Iustin Pop
          bad = True
692 a8083063 Iustin Pop
          feedback_fn("  - ERROR: file '%s' missing" % file_name)
693 a8083063 Iustin Pop
        elif remote_cksum[file_name] != local_cksum[file_name]:
694 a8083063 Iustin Pop
          bad = True
695 a8083063 Iustin Pop
          feedback_fn("  - ERROR: file '%s' has wrong checksum" % file_name)
696 a8083063 Iustin Pop
697 a8083063 Iustin Pop
    if 'nodelist' not in node_result:
698 a8083063 Iustin Pop
      bad = True
699 a8083063 Iustin Pop
      feedback_fn("  - ERROR: node hasn't returned node connectivity data")
700 a8083063 Iustin Pop
    else:
701 a8083063 Iustin Pop
      if node_result['nodelist']:
702 a8083063 Iustin Pop
        bad = True
703 a8083063 Iustin Pop
        for node in node_result['nodelist']:
704 a8083063 Iustin Pop
          feedback_fn("  - ERROR: communication with node '%s': %s" %
705 a8083063 Iustin Pop
                          (node, node_result['nodelist'][node]))
706 a8083063 Iustin Pop
    hyp_result = node_result.get('hypervisor', None)
707 a8083063 Iustin Pop
    if hyp_result is not None:
708 a8083063 Iustin Pop
      feedback_fn("  - ERROR: hypervisor verify failure: '%s'" % hyp_result)
709 a8083063 Iustin Pop
    return bad
710 a8083063 Iustin Pop
711 a8083063 Iustin Pop
  def _VerifyInstance(self, instance, node_vol_is, node_instance, feedback_fn):
712 a8083063 Iustin Pop
    """Verify an instance.
713 a8083063 Iustin Pop

714 a8083063 Iustin Pop
    This function checks to see if the required block devices are
715 a8083063 Iustin Pop
    available on the instance's node.
716 a8083063 Iustin Pop

717 a8083063 Iustin Pop
    """
718 a8083063 Iustin Pop
    bad = False
719 a8083063 Iustin Pop
720 a8083063 Iustin Pop
    instancelist = self.cfg.GetInstanceList()
721 a8083063 Iustin Pop
    if not instance in instancelist:
722 a8083063 Iustin Pop
      feedback_fn("  - ERROR: instance %s not in instance list %s" %
723 a8083063 Iustin Pop
                      (instance, instancelist))
724 a8083063 Iustin Pop
      bad = True
725 a8083063 Iustin Pop
726 a8083063 Iustin Pop
    instanceconfig = self.cfg.GetInstanceInfo(instance)
727 a8083063 Iustin Pop
    node_current = instanceconfig.primary_node
728 a8083063 Iustin Pop
729 a8083063 Iustin Pop
    node_vol_should = {}
730 a8083063 Iustin Pop
    instanceconfig.MapLVsByNode(node_vol_should)
731 a8083063 Iustin Pop
732 a8083063 Iustin Pop
    for node in node_vol_should:
733 a8083063 Iustin Pop
      for volume in node_vol_should[node]:
734 a8083063 Iustin Pop
        if node not in node_vol_is or volume not in node_vol_is[node]:
735 a8083063 Iustin Pop
          feedback_fn("  - ERROR: volume %s missing on node %s" %
736 a8083063 Iustin Pop
                          (volume, node))
737 a8083063 Iustin Pop
          bad = True
738 a8083063 Iustin Pop
739 a8083063 Iustin Pop
    if not instanceconfig.status == 'down':
740 a8083063 Iustin Pop
      if not instance in node_instance[node_current]:
741 a8083063 Iustin Pop
        feedback_fn("  - ERROR: instance %s not running on node %s" %
742 a8083063 Iustin Pop
                        (instance, node_current))
743 a8083063 Iustin Pop
        bad = True
744 a8083063 Iustin Pop
745 a8083063 Iustin Pop
    for node in node_instance:
746 a8083063 Iustin Pop
      if (not node == node_current):
747 a8083063 Iustin Pop
        if instance in node_instance[node]:
748 a8083063 Iustin Pop
          feedback_fn("  - ERROR: instance %s should not run on node %s" %
749 a8083063 Iustin Pop
                          (instance, node))
750 a8083063 Iustin Pop
          bad = True
751 a8083063 Iustin Pop
752 6a438c98 Michael Hanselmann
    return bad
753 a8083063 Iustin Pop
754 a8083063 Iustin Pop
  def _VerifyOrphanVolumes(self, node_vol_should, node_vol_is, feedback_fn):
755 a8083063 Iustin Pop
    """Verify if there are any unknown volumes in the cluster.
756 a8083063 Iustin Pop

757 a8083063 Iustin Pop
    The .os, .swap and backup volumes are ignored. All other volumes are
758 a8083063 Iustin Pop
    reported as unknown.
759 a8083063 Iustin Pop

760 a8083063 Iustin Pop
    """
761 a8083063 Iustin Pop
    bad = False
762 a8083063 Iustin Pop
763 a8083063 Iustin Pop
    for node in node_vol_is:
764 a8083063 Iustin Pop
      for volume in node_vol_is[node]:
765 a8083063 Iustin Pop
        if node not in node_vol_should or volume not in node_vol_should[node]:
766 a8083063 Iustin Pop
          feedback_fn("  - ERROR: volume %s on node %s should not exist" %
767 a8083063 Iustin Pop
                      (volume, node))
768 a8083063 Iustin Pop
          bad = True
769 a8083063 Iustin Pop
    return bad
770 a8083063 Iustin Pop
771 a8083063 Iustin Pop
  def _VerifyOrphanInstances(self, instancelist, node_instance, feedback_fn):
772 a8083063 Iustin Pop
    """Verify the list of running instances.
773 a8083063 Iustin Pop

774 a8083063 Iustin Pop
    This checks what instances are running but unknown to the cluster.
775 a8083063 Iustin Pop

776 a8083063 Iustin Pop
    """
777 a8083063 Iustin Pop
    bad = False
778 a8083063 Iustin Pop
    for node in node_instance:
779 a8083063 Iustin Pop
      for runninginstance in node_instance[node]:
780 a8083063 Iustin Pop
        if runninginstance not in instancelist:
781 a8083063 Iustin Pop
          feedback_fn("  - ERROR: instance %s on node %s should not exist" %
782 a8083063 Iustin Pop
                          (runninginstance, node))
783 a8083063 Iustin Pop
          bad = True
784 a8083063 Iustin Pop
    return bad
785 a8083063 Iustin Pop
786 a8083063 Iustin Pop
  def CheckPrereq(self):
787 a8083063 Iustin Pop
    """Check prerequisites.
788 a8083063 Iustin Pop

789 a8083063 Iustin Pop
    This has no prerequisites.
790 a8083063 Iustin Pop

791 a8083063 Iustin Pop
    """
792 a8083063 Iustin Pop
    pass
793 a8083063 Iustin Pop
794 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
795 a8083063 Iustin Pop
    """Verify integrity of cluster, performing various test on nodes.
796 a8083063 Iustin Pop

797 a8083063 Iustin Pop
    """
798 a8083063 Iustin Pop
    bad = False
799 a8083063 Iustin Pop
    feedback_fn("* Verifying global settings")
800 a8083063 Iustin Pop
    self.cfg.VerifyConfig()
801 a8083063 Iustin Pop
802 a8083063 Iustin Pop
    vg_name = self.cfg.GetVGName()
803 a8083063 Iustin Pop
    nodelist = utils.NiceSort(self.cfg.GetNodeList())
804 a8083063 Iustin Pop
    instancelist = utils.NiceSort(self.cfg.GetInstanceList())
805 a8083063 Iustin Pop
    node_volume = {}
806 a8083063 Iustin Pop
    node_instance = {}
807 a8083063 Iustin Pop
808 a8083063 Iustin Pop
    # FIXME: verify OS list
809 a8083063 Iustin Pop
    # do local checksums
810 cb91d46e Iustin Pop
    file_names = list(self.sstore.GetFileList())
811 cb91d46e Iustin Pop
    file_names.append(constants.SSL_CERT_FILE)
812 cb91d46e Iustin Pop
    file_names.append(constants.CLUSTER_CONF_FILE)
813 a8083063 Iustin Pop
    local_checksums = utils.FingerprintFiles(file_names)
814 a8083063 Iustin Pop
815 a8083063 Iustin Pop
    feedback_fn("* Gathering data (%d nodes)" % len(nodelist))
816 a8083063 Iustin Pop
    all_volumeinfo = rpc.call_volume_list(nodelist, vg_name)
817 a8083063 Iustin Pop
    all_instanceinfo = rpc.call_instance_list(nodelist)
818 a8083063 Iustin Pop
    all_vglist = rpc.call_vg_list(nodelist)
819 a8083063 Iustin Pop
    node_verify_param = {
820 a8083063 Iustin Pop
      'filelist': file_names,
821 a8083063 Iustin Pop
      'nodelist': nodelist,
822 a8083063 Iustin Pop
      'hypervisor': None,
823 a8083063 Iustin Pop
      }
824 a8083063 Iustin Pop
    all_nvinfo = rpc.call_node_verify(nodelist, node_verify_param)
825 a8083063 Iustin Pop
    all_rversion = rpc.call_version(nodelist)
826 a8083063 Iustin Pop
827 a8083063 Iustin Pop
    for node in nodelist:
828 a8083063 Iustin Pop
      feedback_fn("* Verifying node %s" % node)
829 a8083063 Iustin Pop
      result = self._VerifyNode(node, file_names, local_checksums,
830 a8083063 Iustin Pop
                                all_vglist[node], all_nvinfo[node],
831 a8083063 Iustin Pop
                                all_rversion[node], feedback_fn)
832 a8083063 Iustin Pop
      bad = bad or result
833 a8083063 Iustin Pop
834 a8083063 Iustin Pop
      # node_volume
835 a8083063 Iustin Pop
      volumeinfo = all_volumeinfo[node]
836 a8083063 Iustin Pop
837 a8083063 Iustin Pop
      if type(volumeinfo) != dict:
838 a8083063 Iustin Pop
        feedback_fn("  - ERROR: connection to %s failed" % (node,))
839 a8083063 Iustin Pop
        bad = True
840 a8083063 Iustin Pop
        continue
841 a8083063 Iustin Pop
842 a8083063 Iustin Pop
      node_volume[node] = volumeinfo
843 a8083063 Iustin Pop
844 a8083063 Iustin Pop
      # node_instance
845 a8083063 Iustin Pop
      nodeinstance = all_instanceinfo[node]
846 a8083063 Iustin Pop
      if type(nodeinstance) != list:
847 a8083063 Iustin Pop
        feedback_fn("  - ERROR: connection to %s failed" % (node,))
848 a8083063 Iustin Pop
        bad = True
849 a8083063 Iustin Pop
        continue
850 a8083063 Iustin Pop
851 a8083063 Iustin Pop
      node_instance[node] = nodeinstance
852 a8083063 Iustin Pop
853 a8083063 Iustin Pop
    node_vol_should = {}
854 a8083063 Iustin Pop
855 a8083063 Iustin Pop
    for instance in instancelist:
856 a8083063 Iustin Pop
      feedback_fn("* Verifying instance %s" % instance)
857 a8083063 Iustin Pop
      result =  self._VerifyInstance(instance, node_volume, node_instance,
858 a8083063 Iustin Pop
                                     feedback_fn)
859 a8083063 Iustin Pop
      bad = bad or result
860 a8083063 Iustin Pop
861 a8083063 Iustin Pop
      inst_config = self.cfg.GetInstanceInfo(instance)
862 a8083063 Iustin Pop
863 a8083063 Iustin Pop
      inst_config.MapLVsByNode(node_vol_should)
864 a8083063 Iustin Pop
865 a8083063 Iustin Pop
    feedback_fn("* Verifying orphan volumes")
866 a8083063 Iustin Pop
    result = self._VerifyOrphanVolumes(node_vol_should, node_volume,
867 a8083063 Iustin Pop
                                       feedback_fn)
868 a8083063 Iustin Pop
    bad = bad or result
869 a8083063 Iustin Pop
870 a8083063 Iustin Pop
    feedback_fn("* Verifying remaining instances")
871 a8083063 Iustin Pop
    result = self._VerifyOrphanInstances(instancelist, node_instance,
872 a8083063 Iustin Pop
                                         feedback_fn)
873 a8083063 Iustin Pop
    bad = bad or result
874 a8083063 Iustin Pop
875 a8083063 Iustin Pop
    return int(bad)
876 a8083063 Iustin Pop
877 a8083063 Iustin Pop
878 07bd8a51 Iustin Pop
class LURenameCluster(LogicalUnit):
879 07bd8a51 Iustin Pop
  """Rename the cluster.
880 07bd8a51 Iustin Pop

881 07bd8a51 Iustin Pop
  """
882 07bd8a51 Iustin Pop
  HPATH = "cluster-rename"
883 07bd8a51 Iustin Pop
  HTYPE = constants.HTYPE_CLUSTER
884 07bd8a51 Iustin Pop
  _OP_REQP = ["name"]
885 07bd8a51 Iustin Pop
886 07bd8a51 Iustin Pop
  def BuildHooksEnv(self):
887 07bd8a51 Iustin Pop
    """Build hooks env.
888 07bd8a51 Iustin Pop

889 07bd8a51 Iustin Pop
    """
890 07bd8a51 Iustin Pop
    env = {
891 0e137c28 Iustin Pop
      "OP_TARGET": self.op.sstore.GetClusterName(),
892 07bd8a51 Iustin Pop
      "NEW_NAME": self.op.name,
893 07bd8a51 Iustin Pop
      }
894 07bd8a51 Iustin Pop
    mn = self.sstore.GetMasterNode()
895 07bd8a51 Iustin Pop
    return env, [mn], [mn]
896 07bd8a51 Iustin Pop
897 07bd8a51 Iustin Pop
  def CheckPrereq(self):
898 07bd8a51 Iustin Pop
    """Verify that the passed name is a valid one.
899 07bd8a51 Iustin Pop

900 07bd8a51 Iustin Pop
    """
901 89e1fc26 Iustin Pop
    hostname = utils.HostInfo(self.op.name)
902 07bd8a51 Iustin Pop
903 bcf043c9 Iustin Pop
    new_name = hostname.name
904 bcf043c9 Iustin Pop
    self.ip = new_ip = hostname.ip
905 07bd8a51 Iustin Pop
    old_name = self.sstore.GetClusterName()
906 07bd8a51 Iustin Pop
    old_ip = self.sstore.GetMasterIP()
907 07bd8a51 Iustin Pop
    if new_name == old_name and new_ip == old_ip:
908 07bd8a51 Iustin Pop
      raise errors.OpPrereqError("Neither the name nor the IP address of the"
909 07bd8a51 Iustin Pop
                                 " cluster has changed")
910 07bd8a51 Iustin Pop
    if new_ip != old_ip:
911 07bd8a51 Iustin Pop
      result = utils.RunCmd(["fping", "-q", new_ip])
912 07bd8a51 Iustin Pop
      if not result.failed:
913 07bd8a51 Iustin Pop
        raise errors.OpPrereqError("The given cluster IP address (%s) is"
914 07bd8a51 Iustin Pop
                                   " reachable on the network. Aborting." %
915 07bd8a51 Iustin Pop
                                   new_ip)
916 07bd8a51 Iustin Pop
917 07bd8a51 Iustin Pop
    self.op.name = new_name
918 07bd8a51 Iustin Pop
919 07bd8a51 Iustin Pop
  def Exec(self, feedback_fn):
920 07bd8a51 Iustin Pop
    """Rename the cluster.
921 07bd8a51 Iustin Pop

922 07bd8a51 Iustin Pop
    """
923 07bd8a51 Iustin Pop
    clustername = self.op.name
924 07bd8a51 Iustin Pop
    ip = self.ip
925 07bd8a51 Iustin Pop
    ss = self.sstore
926 07bd8a51 Iustin Pop
927 07bd8a51 Iustin Pop
    # shutdown the master IP
928 07bd8a51 Iustin Pop
    master = ss.GetMasterNode()
929 07bd8a51 Iustin Pop
    if not rpc.call_node_stop_master(master):
930 07bd8a51 Iustin Pop
      raise errors.OpExecError("Could not disable the master role")
931 07bd8a51 Iustin Pop
932 07bd8a51 Iustin Pop
    try:
933 07bd8a51 Iustin Pop
      # modify the sstore
934 07bd8a51 Iustin Pop
      ss.SetKey(ss.SS_MASTER_IP, ip)
935 07bd8a51 Iustin Pop
      ss.SetKey(ss.SS_CLUSTER_NAME, clustername)
936 07bd8a51 Iustin Pop
937 07bd8a51 Iustin Pop
      # Distribute updated ss config to all nodes
938 07bd8a51 Iustin Pop
      myself = self.cfg.GetNodeInfo(master)
939 07bd8a51 Iustin Pop
      dist_nodes = self.cfg.GetNodeList()
940 07bd8a51 Iustin Pop
      if myself.name in dist_nodes:
941 07bd8a51 Iustin Pop
        dist_nodes.remove(myself.name)
942 07bd8a51 Iustin Pop
943 07bd8a51 Iustin Pop
      logger.Debug("Copying updated ssconf data to all nodes")
944 07bd8a51 Iustin Pop
      for keyname in [ss.SS_CLUSTER_NAME, ss.SS_MASTER_IP]:
945 07bd8a51 Iustin Pop
        fname = ss.KeyToFilename(keyname)
946 07bd8a51 Iustin Pop
        result = rpc.call_upload_file(dist_nodes, fname)
947 07bd8a51 Iustin Pop
        for to_node in dist_nodes:
948 07bd8a51 Iustin Pop
          if not result[to_node]:
949 07bd8a51 Iustin Pop
            logger.Error("copy of file %s to node %s failed" %
950 07bd8a51 Iustin Pop
                         (fname, to_node))
951 07bd8a51 Iustin Pop
    finally:
952 07bd8a51 Iustin Pop
      if not rpc.call_node_start_master(master):
953 07bd8a51 Iustin Pop
        logger.Error("Could not re-enable the master role on the master,\n"
954 07bd8a51 Iustin Pop
                     "please restart manually.")
955 07bd8a51 Iustin Pop
956 07bd8a51 Iustin Pop
957 5bfac263 Iustin Pop
def _WaitForSync(cfgw, instance, proc, oneshot=False, unlock=False):
958 a8083063 Iustin Pop
  """Sleep and poll for an instance's disk to sync.
959 a8083063 Iustin Pop

960 a8083063 Iustin Pop
  """
961 a8083063 Iustin Pop
  if not instance.disks:
962 a8083063 Iustin Pop
    return True
963 a8083063 Iustin Pop
964 a8083063 Iustin Pop
  if not oneshot:
965 5bfac263 Iustin Pop
    proc.LogInfo("Waiting for instance %s to sync disks." % instance.name)
966 a8083063 Iustin Pop
967 a8083063 Iustin Pop
  node = instance.primary_node
968 a8083063 Iustin Pop
969 a8083063 Iustin Pop
  for dev in instance.disks:
970 a8083063 Iustin Pop
    cfgw.SetDiskID(dev, node)
971 a8083063 Iustin Pop
972 a8083063 Iustin Pop
  retries = 0
973 a8083063 Iustin Pop
  while True:
974 a8083063 Iustin Pop
    max_time = 0
975 a8083063 Iustin Pop
    done = True
976 a8083063 Iustin Pop
    cumul_degraded = False
977 a8083063 Iustin Pop
    rstats = rpc.call_blockdev_getmirrorstatus(node, instance.disks)
978 a8083063 Iustin Pop
    if not rstats:
979 5bfac263 Iustin Pop
      proc.LogWarning("Can't get any data from node %s" % node)
980 a8083063 Iustin Pop
      retries += 1
981 a8083063 Iustin Pop
      if retries >= 10:
982 3ecf6786 Iustin Pop
        raise errors.RemoteError("Can't contact node %s for mirror data,"
983 3ecf6786 Iustin Pop
                                 " aborting." % node)
984 a8083063 Iustin Pop
      time.sleep(6)
985 a8083063 Iustin Pop
      continue
986 a8083063 Iustin Pop
    retries = 0
987 a8083063 Iustin Pop
    for i in range(len(rstats)):
988 a8083063 Iustin Pop
      mstat = rstats[i]
989 a8083063 Iustin Pop
      if mstat is None:
990 5bfac263 Iustin Pop
        proc.LogWarning("Can't compute data for node %s/%s" %
991 a8083063 Iustin Pop
                        (node, instance.disks[i].iv_name))
992 a8083063 Iustin Pop
        continue
993 0834c866 Iustin Pop
      # we ignore the ldisk parameter
994 0834c866 Iustin Pop
      perc_done, est_time, is_degraded, _ = mstat
995 a8083063 Iustin Pop
      cumul_degraded = cumul_degraded or (is_degraded and perc_done is None)
996 a8083063 Iustin Pop
      if perc_done is not None:
997 a8083063 Iustin Pop
        done = False
998 a8083063 Iustin Pop
        if est_time is not None:
999 a8083063 Iustin Pop
          rem_time = "%d estimated seconds remaining" % est_time
1000 a8083063 Iustin Pop
          max_time = est_time
1001 a8083063 Iustin Pop
        else:
1002 a8083063 Iustin Pop
          rem_time = "no time estimate"
1003 5bfac263 Iustin Pop
        proc.LogInfo("- device %s: %5.2f%% done, %s" %
1004 5bfac263 Iustin Pop
                     (instance.disks[i].iv_name, perc_done, rem_time))
1005 a8083063 Iustin Pop
    if done or oneshot:
1006 a8083063 Iustin Pop
      break
1007 a8083063 Iustin Pop
1008 a8083063 Iustin Pop
    if unlock:
1009 a8083063 Iustin Pop
      utils.Unlock('cmd')
1010 a8083063 Iustin Pop
    try:
1011 a8083063 Iustin Pop
      time.sleep(min(60, max_time))
1012 a8083063 Iustin Pop
    finally:
1013 a8083063 Iustin Pop
      if unlock:
1014 a8083063 Iustin Pop
        utils.Lock('cmd')
1015 a8083063 Iustin Pop
1016 a8083063 Iustin Pop
  if done:
1017 5bfac263 Iustin Pop
    proc.LogInfo("Instance %s's disks are in sync." % instance.name)
1018 a8083063 Iustin Pop
  return not cumul_degraded
1019 a8083063 Iustin Pop
1020 a8083063 Iustin Pop
1021 0834c866 Iustin Pop
def _CheckDiskConsistency(cfgw, dev, node, on_primary, ldisk=False):
1022 a8083063 Iustin Pop
  """Check that mirrors are not degraded.
1023 a8083063 Iustin Pop

1024 0834c866 Iustin Pop
  The ldisk parameter, if True, will change the test from the
1025 0834c866 Iustin Pop
  is_degraded attribute (which represents overall non-ok status for
1026 0834c866 Iustin Pop
  the device(s)) to the ldisk (representing the local storage status).
1027 0834c866 Iustin Pop

1028 a8083063 Iustin Pop
  """
1029 a8083063 Iustin Pop
  cfgw.SetDiskID(dev, node)
1030 0834c866 Iustin Pop
  if ldisk:
1031 0834c866 Iustin Pop
    idx = 6
1032 0834c866 Iustin Pop
  else:
1033 0834c866 Iustin Pop
    idx = 5
1034 a8083063 Iustin Pop
1035 a8083063 Iustin Pop
  result = True
1036 a8083063 Iustin Pop
  if on_primary or dev.AssembleOnSecondary():
1037 a8083063 Iustin Pop
    rstats = rpc.call_blockdev_find(node, dev)
1038 a8083063 Iustin Pop
    if not rstats:
1039 a8083063 Iustin Pop
      logger.ToStderr("Can't get any data from node %s" % node)
1040 a8083063 Iustin Pop
      result = False
1041 a8083063 Iustin Pop
    else:
1042 0834c866 Iustin Pop
      result = result and (not rstats[idx])
1043 a8083063 Iustin Pop
  if dev.children:
1044 a8083063 Iustin Pop
    for child in dev.children:
1045 a8083063 Iustin Pop
      result = result and _CheckDiskConsistency(cfgw, child, node, on_primary)
1046 a8083063 Iustin Pop
1047 a8083063 Iustin Pop
  return result
1048 a8083063 Iustin Pop
1049 a8083063 Iustin Pop
1050 a8083063 Iustin Pop
class LUDiagnoseOS(NoHooksLU):
1051 a8083063 Iustin Pop
  """Logical unit for OS diagnose/query.
1052 a8083063 Iustin Pop

1053 a8083063 Iustin Pop
  """
1054 a8083063 Iustin Pop
  _OP_REQP = []
1055 a8083063 Iustin Pop
1056 a8083063 Iustin Pop
  def CheckPrereq(self):
1057 a8083063 Iustin Pop
    """Check prerequisites.
1058 a8083063 Iustin Pop

1059 a8083063 Iustin Pop
    This always succeeds, since this is a pure query LU.
1060 a8083063 Iustin Pop

1061 a8083063 Iustin Pop
    """
1062 a8083063 Iustin Pop
    return
1063 a8083063 Iustin Pop
1064 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1065 a8083063 Iustin Pop
    """Compute the list of OSes.
1066 a8083063 Iustin Pop

1067 a8083063 Iustin Pop
    """
1068 a8083063 Iustin Pop
    node_list = self.cfg.GetNodeList()
1069 a8083063 Iustin Pop
    node_data = rpc.call_os_diagnose(node_list)
1070 a8083063 Iustin Pop
    if node_data == False:
1071 3ecf6786 Iustin Pop
      raise errors.OpExecError("Can't gather the list of OSes")
1072 a8083063 Iustin Pop
    return node_data
1073 a8083063 Iustin Pop
1074 a8083063 Iustin Pop
1075 a8083063 Iustin Pop
class LURemoveNode(LogicalUnit):
1076 a8083063 Iustin Pop
  """Logical unit for removing a node.
1077 a8083063 Iustin Pop

1078 a8083063 Iustin Pop
  """
1079 a8083063 Iustin Pop
  HPATH = "node-remove"
1080 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_NODE
1081 a8083063 Iustin Pop
  _OP_REQP = ["node_name"]
1082 a8083063 Iustin Pop
1083 a8083063 Iustin Pop
  def BuildHooksEnv(self):
1084 a8083063 Iustin Pop
    """Build hooks env.
1085 a8083063 Iustin Pop

1086 a8083063 Iustin Pop
    This doesn't run on the target node in the pre phase as a failed
1087 a8083063 Iustin Pop
    node would not allows itself to run.
1088 a8083063 Iustin Pop

1089 a8083063 Iustin Pop
    """
1090 396e1b78 Michael Hanselmann
    env = {
1091 0e137c28 Iustin Pop
      "OP_TARGET": self.op.node_name,
1092 396e1b78 Michael Hanselmann
      "NODE_NAME": self.op.node_name,
1093 396e1b78 Michael Hanselmann
      }
1094 a8083063 Iustin Pop
    all_nodes = self.cfg.GetNodeList()
1095 a8083063 Iustin Pop
    all_nodes.remove(self.op.node_name)
1096 396e1b78 Michael Hanselmann
    return env, all_nodes, all_nodes
1097 a8083063 Iustin Pop
1098 a8083063 Iustin Pop
  def CheckPrereq(self):
1099 a8083063 Iustin Pop
    """Check prerequisites.
1100 a8083063 Iustin Pop

1101 a8083063 Iustin Pop
    This checks:
1102 a8083063 Iustin Pop
     - the node exists in the configuration
1103 a8083063 Iustin Pop
     - it does not have primary or secondary instances
1104 a8083063 Iustin Pop
     - it's not the master
1105 a8083063 Iustin Pop

1106 a8083063 Iustin Pop
    Any errors are signalled by raising errors.OpPrereqError.
1107 a8083063 Iustin Pop

1108 a8083063 Iustin Pop
    """
1109 a8083063 Iustin Pop
    node = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.node_name))
1110 a8083063 Iustin Pop
    if node is None:
1111 a02bc76e Iustin Pop
      raise errors.OpPrereqError, ("Node '%s' is unknown." % self.op.node_name)
1112 a8083063 Iustin Pop
1113 a8083063 Iustin Pop
    instance_list = self.cfg.GetInstanceList()
1114 a8083063 Iustin Pop
1115 880478f8 Iustin Pop
    masternode = self.sstore.GetMasterNode()
1116 a8083063 Iustin Pop
    if node.name == masternode:
1117 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Node is the master node,"
1118 3ecf6786 Iustin Pop
                                 " you need to failover first.")
1119 a8083063 Iustin Pop
1120 a8083063 Iustin Pop
    for instance_name in instance_list:
1121 a8083063 Iustin Pop
      instance = self.cfg.GetInstanceInfo(instance_name)
1122 a8083063 Iustin Pop
      if node.name == instance.primary_node:
1123 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Instance %s still running on the node,"
1124 3ecf6786 Iustin Pop
                                   " please remove first." % instance_name)
1125 a8083063 Iustin Pop
      if node.name in instance.secondary_nodes:
1126 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Instance %s has node as a secondary,"
1127 3ecf6786 Iustin Pop
                                   " please remove first." % instance_name)
1128 a8083063 Iustin Pop
    self.op.node_name = node.name
1129 a8083063 Iustin Pop
    self.node = node
1130 a8083063 Iustin Pop
1131 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1132 a8083063 Iustin Pop
    """Removes the node from the cluster.
1133 a8083063 Iustin Pop

1134 a8083063 Iustin Pop
    """
1135 a8083063 Iustin Pop
    node = self.node
1136 a8083063 Iustin Pop
    logger.Info("stopping the node daemon and removing configs from node %s" %
1137 a8083063 Iustin Pop
                node.name)
1138 a8083063 Iustin Pop
1139 a8083063 Iustin Pop
    rpc.call_node_leave_cluster(node.name)
1140 a8083063 Iustin Pop
1141 a8083063 Iustin Pop
    ssh.SSHCall(node.name, 'root', "%s stop" % constants.NODE_INITD_SCRIPT)
1142 a8083063 Iustin Pop
1143 a8083063 Iustin Pop
    logger.Info("Removing node %s from config" % node.name)
1144 a8083063 Iustin Pop
1145 a8083063 Iustin Pop
    self.cfg.RemoveNode(node.name)
1146 a8083063 Iustin Pop
1147 c8a0948f Michael Hanselmann
    _RemoveHostFromEtcHosts(node.name)
1148 c8a0948f Michael Hanselmann
1149 a8083063 Iustin Pop
1150 a8083063 Iustin Pop
class LUQueryNodes(NoHooksLU):
1151 a8083063 Iustin Pop
  """Logical unit for querying nodes.
1152 a8083063 Iustin Pop

1153 a8083063 Iustin Pop
  """
1154 246e180a Iustin Pop
  _OP_REQP = ["output_fields", "names"]
1155 a8083063 Iustin Pop
1156 a8083063 Iustin Pop
  def CheckPrereq(self):
1157 a8083063 Iustin Pop
    """Check prerequisites.
1158 a8083063 Iustin Pop

1159 a8083063 Iustin Pop
    This checks that the fields required are valid output fields.
1160 a8083063 Iustin Pop

1161 a8083063 Iustin Pop
    """
1162 a8083063 Iustin Pop
    self.dynamic_fields = frozenset(["dtotal", "dfree",
1163 3ef10550 Michael Hanselmann
                                     "mtotal", "mnode", "mfree",
1164 3ef10550 Michael Hanselmann
                                     "bootid"])
1165 a8083063 Iustin Pop
1166 ec223efb Iustin Pop
    _CheckOutputFields(static=["name", "pinst_cnt", "sinst_cnt",
1167 ec223efb Iustin Pop
                               "pinst_list", "sinst_list",
1168 ec223efb Iustin Pop
                               "pip", "sip"],
1169 dcb93971 Michael Hanselmann
                       dynamic=self.dynamic_fields,
1170 dcb93971 Michael Hanselmann
                       selected=self.op.output_fields)
1171 a8083063 Iustin Pop
1172 246e180a Iustin Pop
    self.wanted = _GetWantedNodes(self, self.op.names)
1173 a8083063 Iustin Pop
1174 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1175 a8083063 Iustin Pop
    """Computes the list of nodes and their attributes.
1176 a8083063 Iustin Pop

1177 a8083063 Iustin Pop
    """
1178 246e180a Iustin Pop
    nodenames = self.wanted
1179 a8083063 Iustin Pop
    nodelist = [self.cfg.GetNodeInfo(name) for name in nodenames]
1180 a8083063 Iustin Pop
1181 a8083063 Iustin Pop
    # begin data gathering
1182 a8083063 Iustin Pop
1183 a8083063 Iustin Pop
    if self.dynamic_fields.intersection(self.op.output_fields):
1184 a8083063 Iustin Pop
      live_data = {}
1185 a8083063 Iustin Pop
      node_data = rpc.call_node_info(nodenames, self.cfg.GetVGName())
1186 a8083063 Iustin Pop
      for name in nodenames:
1187 a8083063 Iustin Pop
        nodeinfo = node_data.get(name, None)
1188 a8083063 Iustin Pop
        if nodeinfo:
1189 a8083063 Iustin Pop
          live_data[name] = {
1190 a8083063 Iustin Pop
            "mtotal": utils.TryConvert(int, nodeinfo['memory_total']),
1191 a8083063 Iustin Pop
            "mnode": utils.TryConvert(int, nodeinfo['memory_dom0']),
1192 a8083063 Iustin Pop
            "mfree": utils.TryConvert(int, nodeinfo['memory_free']),
1193 a8083063 Iustin Pop
            "dtotal": utils.TryConvert(int, nodeinfo['vg_size']),
1194 a8083063 Iustin Pop
            "dfree": utils.TryConvert(int, nodeinfo['vg_free']),
1195 3ef10550 Michael Hanselmann
            "bootid": nodeinfo['bootid'],
1196 a8083063 Iustin Pop
            }
1197 a8083063 Iustin Pop
        else:
1198 a8083063 Iustin Pop
          live_data[name] = {}
1199 a8083063 Iustin Pop
    else:
1200 a8083063 Iustin Pop
      live_data = dict.fromkeys(nodenames, {})
1201 a8083063 Iustin Pop
1202 ec223efb Iustin Pop
    node_to_primary = dict([(name, set()) for name in nodenames])
1203 ec223efb Iustin Pop
    node_to_secondary = dict([(name, set()) for name in nodenames])
1204 a8083063 Iustin Pop
1205 ec223efb Iustin Pop
    inst_fields = frozenset(("pinst_cnt", "pinst_list",
1206 ec223efb Iustin Pop
                             "sinst_cnt", "sinst_list"))
1207 ec223efb Iustin Pop
    if inst_fields & frozenset(self.op.output_fields):
1208 a8083063 Iustin Pop
      instancelist = self.cfg.GetInstanceList()
1209 a8083063 Iustin Pop
1210 ec223efb Iustin Pop
      for instance_name in instancelist:
1211 ec223efb Iustin Pop
        inst = self.cfg.GetInstanceInfo(instance_name)
1212 ec223efb Iustin Pop
        if inst.primary_node in node_to_primary:
1213 ec223efb Iustin Pop
          node_to_primary[inst.primary_node].add(inst.name)
1214 ec223efb Iustin Pop
        for secnode in inst.secondary_nodes:
1215 ec223efb Iustin Pop
          if secnode in node_to_secondary:
1216 ec223efb Iustin Pop
            node_to_secondary[secnode].add(inst.name)
1217 a8083063 Iustin Pop
1218 a8083063 Iustin Pop
    # end data gathering
1219 a8083063 Iustin Pop
1220 a8083063 Iustin Pop
    output = []
1221 a8083063 Iustin Pop
    for node in nodelist:
1222 a8083063 Iustin Pop
      node_output = []
1223 a8083063 Iustin Pop
      for field in self.op.output_fields:
1224 a8083063 Iustin Pop
        if field == "name":
1225 a8083063 Iustin Pop
          val = node.name
1226 ec223efb Iustin Pop
        elif field == "pinst_list":
1227 ec223efb Iustin Pop
          val = list(node_to_primary[node.name])
1228 ec223efb Iustin Pop
        elif field == "sinst_list":
1229 ec223efb Iustin Pop
          val = list(node_to_secondary[node.name])
1230 ec223efb Iustin Pop
        elif field == "pinst_cnt":
1231 ec223efb Iustin Pop
          val = len(node_to_primary[node.name])
1232 ec223efb Iustin Pop
        elif field == "sinst_cnt":
1233 ec223efb Iustin Pop
          val = len(node_to_secondary[node.name])
1234 a8083063 Iustin Pop
        elif field == "pip":
1235 a8083063 Iustin Pop
          val = node.primary_ip
1236 a8083063 Iustin Pop
        elif field == "sip":
1237 a8083063 Iustin Pop
          val = node.secondary_ip
1238 a8083063 Iustin Pop
        elif field in self.dynamic_fields:
1239 ec223efb Iustin Pop
          val = live_data[node.name].get(field, None)
1240 a8083063 Iustin Pop
        else:
1241 3ecf6786 Iustin Pop
          raise errors.ParameterError(field)
1242 a8083063 Iustin Pop
        node_output.append(val)
1243 a8083063 Iustin Pop
      output.append(node_output)
1244 a8083063 Iustin Pop
1245 a8083063 Iustin Pop
    return output
1246 a8083063 Iustin Pop
1247 a8083063 Iustin Pop
1248 dcb93971 Michael Hanselmann
class LUQueryNodeVolumes(NoHooksLU):
1249 dcb93971 Michael Hanselmann
  """Logical unit for getting volumes on node(s).
1250 dcb93971 Michael Hanselmann

1251 dcb93971 Michael Hanselmann
  """
1252 dcb93971 Michael Hanselmann
  _OP_REQP = ["nodes", "output_fields"]
1253 dcb93971 Michael Hanselmann
1254 dcb93971 Michael Hanselmann
  def CheckPrereq(self):
1255 dcb93971 Michael Hanselmann
    """Check prerequisites.
1256 dcb93971 Michael Hanselmann

1257 dcb93971 Michael Hanselmann
    This checks that the fields required are valid output fields.
1258 dcb93971 Michael Hanselmann

1259 dcb93971 Michael Hanselmann
    """
1260 dcb93971 Michael Hanselmann
    self.nodes = _GetWantedNodes(self, self.op.nodes)
1261 dcb93971 Michael Hanselmann
1262 dcb93971 Michael Hanselmann
    _CheckOutputFields(static=["node"],
1263 dcb93971 Michael Hanselmann
                       dynamic=["phys", "vg", "name", "size", "instance"],
1264 dcb93971 Michael Hanselmann
                       selected=self.op.output_fields)
1265 dcb93971 Michael Hanselmann
1266 dcb93971 Michael Hanselmann
1267 dcb93971 Michael Hanselmann
  def Exec(self, feedback_fn):
1268 dcb93971 Michael Hanselmann
    """Computes the list of nodes and their attributes.
1269 dcb93971 Michael Hanselmann

1270 dcb93971 Michael Hanselmann
    """
1271 a7ba5e53 Iustin Pop
    nodenames = self.nodes
1272 dcb93971 Michael Hanselmann
    volumes = rpc.call_node_volumes(nodenames)
1273 dcb93971 Michael Hanselmann
1274 dcb93971 Michael Hanselmann
    ilist = [self.cfg.GetInstanceInfo(iname) for iname
1275 dcb93971 Michael Hanselmann
             in self.cfg.GetInstanceList()]
1276 dcb93971 Michael Hanselmann
1277 dcb93971 Michael Hanselmann
    lv_by_node = dict([(inst, inst.MapLVsByNode()) for inst in ilist])
1278 dcb93971 Michael Hanselmann
1279 dcb93971 Michael Hanselmann
    output = []
1280 dcb93971 Michael Hanselmann
    for node in nodenames:
1281 37d19eb2 Michael Hanselmann
      if node not in volumes or not volumes[node]:
1282 37d19eb2 Michael Hanselmann
        continue
1283 37d19eb2 Michael Hanselmann
1284 dcb93971 Michael Hanselmann
      node_vols = volumes[node][:]
1285 dcb93971 Michael Hanselmann
      node_vols.sort(key=lambda vol: vol['dev'])
1286 dcb93971 Michael Hanselmann
1287 dcb93971 Michael Hanselmann
      for vol in node_vols:
1288 dcb93971 Michael Hanselmann
        node_output = []
1289 dcb93971 Michael Hanselmann
        for field in self.op.output_fields:
1290 dcb93971 Michael Hanselmann
          if field == "node":
1291 dcb93971 Michael Hanselmann
            val = node
1292 dcb93971 Michael Hanselmann
          elif field == "phys":
1293 dcb93971 Michael Hanselmann
            val = vol['dev']
1294 dcb93971 Michael Hanselmann
          elif field == "vg":
1295 dcb93971 Michael Hanselmann
            val = vol['vg']
1296 dcb93971 Michael Hanselmann
          elif field == "name":
1297 dcb93971 Michael Hanselmann
            val = vol['name']
1298 dcb93971 Michael Hanselmann
          elif field == "size":
1299 dcb93971 Michael Hanselmann
            val = int(float(vol['size']))
1300 dcb93971 Michael Hanselmann
          elif field == "instance":
1301 dcb93971 Michael Hanselmann
            for inst in ilist:
1302 dcb93971 Michael Hanselmann
              if node not in lv_by_node[inst]:
1303 dcb93971 Michael Hanselmann
                continue
1304 dcb93971 Michael Hanselmann
              if vol['name'] in lv_by_node[inst][node]:
1305 dcb93971 Michael Hanselmann
                val = inst.name
1306 dcb93971 Michael Hanselmann
                break
1307 dcb93971 Michael Hanselmann
            else:
1308 dcb93971 Michael Hanselmann
              val = '-'
1309 dcb93971 Michael Hanselmann
          else:
1310 3ecf6786 Iustin Pop
            raise errors.ParameterError(field)
1311 dcb93971 Michael Hanselmann
          node_output.append(str(val))
1312 dcb93971 Michael Hanselmann
1313 dcb93971 Michael Hanselmann
        output.append(node_output)
1314 dcb93971 Michael Hanselmann
1315 dcb93971 Michael Hanselmann
    return output
1316 dcb93971 Michael Hanselmann
1317 dcb93971 Michael Hanselmann
1318 a8083063 Iustin Pop
class LUAddNode(LogicalUnit):
1319 a8083063 Iustin Pop
  """Logical unit for adding node to the cluster.
1320 a8083063 Iustin Pop

1321 a8083063 Iustin Pop
  """
1322 a8083063 Iustin Pop
  HPATH = "node-add"
1323 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_NODE
1324 a8083063 Iustin Pop
  _OP_REQP = ["node_name"]
1325 a8083063 Iustin Pop
1326 a8083063 Iustin Pop
  def BuildHooksEnv(self):
1327 a8083063 Iustin Pop
    """Build hooks env.
1328 a8083063 Iustin Pop

1329 a8083063 Iustin Pop
    This will run on all nodes before, and on all nodes + the new node after.
1330 a8083063 Iustin Pop

1331 a8083063 Iustin Pop
    """
1332 a8083063 Iustin Pop
    env = {
1333 0e137c28 Iustin Pop
      "OP_TARGET": self.op.node_name,
1334 a8083063 Iustin Pop
      "NODE_NAME": self.op.node_name,
1335 a8083063 Iustin Pop
      "NODE_PIP": self.op.primary_ip,
1336 a8083063 Iustin Pop
      "NODE_SIP": self.op.secondary_ip,
1337 a8083063 Iustin Pop
      }
1338 a8083063 Iustin Pop
    nodes_0 = self.cfg.GetNodeList()
1339 a8083063 Iustin Pop
    nodes_1 = nodes_0 + [self.op.node_name, ]
1340 a8083063 Iustin Pop
    return env, nodes_0, nodes_1
1341 a8083063 Iustin Pop
1342 a8083063 Iustin Pop
  def CheckPrereq(self):
1343 a8083063 Iustin Pop
    """Check prerequisites.
1344 a8083063 Iustin Pop

1345 a8083063 Iustin Pop
    This checks:
1346 a8083063 Iustin Pop
     - the new node is not already in the config
1347 a8083063 Iustin Pop
     - it is resolvable
1348 a8083063 Iustin Pop
     - its parameters (single/dual homed) matches the cluster
1349 a8083063 Iustin Pop

1350 a8083063 Iustin Pop
    Any errors are signalled by raising errors.OpPrereqError.
1351 a8083063 Iustin Pop

1352 a8083063 Iustin Pop
    """
1353 a8083063 Iustin Pop
    node_name = self.op.node_name
1354 a8083063 Iustin Pop
    cfg = self.cfg
1355 a8083063 Iustin Pop
1356 89e1fc26 Iustin Pop
    dns_data = utils.HostInfo(node_name)
1357 a8083063 Iustin Pop
1358 bcf043c9 Iustin Pop
    node = dns_data.name
1359 bcf043c9 Iustin Pop
    primary_ip = self.op.primary_ip = dns_data.ip
1360 a8083063 Iustin Pop
    secondary_ip = getattr(self.op, "secondary_ip", None)
1361 a8083063 Iustin Pop
    if secondary_ip is None:
1362 a8083063 Iustin Pop
      secondary_ip = primary_ip
1363 a8083063 Iustin Pop
    if not utils.IsValidIP(secondary_ip):
1364 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Invalid secondary IP given")
1365 a8083063 Iustin Pop
    self.op.secondary_ip = secondary_ip
1366 a8083063 Iustin Pop
    node_list = cfg.GetNodeList()
1367 a8083063 Iustin Pop
    if node in node_list:
1368 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Node %s is already in the configuration"
1369 3ecf6786 Iustin Pop
                                 % node)
1370 a8083063 Iustin Pop
1371 a8083063 Iustin Pop
    for existing_node_name in node_list:
1372 a8083063 Iustin Pop
      existing_node = cfg.GetNodeInfo(existing_node_name)
1373 a8083063 Iustin Pop
      if (existing_node.primary_ip == primary_ip or
1374 a8083063 Iustin Pop
          existing_node.secondary_ip == primary_ip or
1375 a8083063 Iustin Pop
          existing_node.primary_ip == secondary_ip or
1376 a8083063 Iustin Pop
          existing_node.secondary_ip == secondary_ip):
1377 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("New node ip address(es) conflict with"
1378 3ecf6786 Iustin Pop
                                   " existing node %s" % existing_node.name)
1379 a8083063 Iustin Pop
1380 a8083063 Iustin Pop
    # check that the type of the node (single versus dual homed) is the
1381 a8083063 Iustin Pop
    # same as for the master
1382 880478f8 Iustin Pop
    myself = cfg.GetNodeInfo(self.sstore.GetMasterNode())
1383 a8083063 Iustin Pop
    master_singlehomed = myself.secondary_ip == myself.primary_ip
1384 a8083063 Iustin Pop
    newbie_singlehomed = secondary_ip == primary_ip
1385 a8083063 Iustin Pop
    if master_singlehomed != newbie_singlehomed:
1386 a8083063 Iustin Pop
      if master_singlehomed:
1387 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("The master has no private ip but the"
1388 3ecf6786 Iustin Pop
                                   " new node has one")
1389 a8083063 Iustin Pop
      else:
1390 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("The master has a private ip but the"
1391 3ecf6786 Iustin Pop
                                   " new node doesn't have one")
1392 a8083063 Iustin Pop
1393 a8083063 Iustin Pop
    # checks reachablity
1394 16abfbc2 Alexander Schreiber
    if not utils.TcpPing(utils.HostInfo().name,
1395 16abfbc2 Alexander Schreiber
                         primary_ip,
1396 16abfbc2 Alexander Schreiber
                         constants.DEFAULT_NODED_PORT):
1397 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Node not reachable by ping")
1398 a8083063 Iustin Pop
1399 a8083063 Iustin Pop
    if not newbie_singlehomed:
1400 a8083063 Iustin Pop
      # check reachability from my secondary ip to newbie's secondary ip
1401 16abfbc2 Alexander Schreiber
      if not utils.TcpPing(myself.secondary_ip,
1402 16abfbc2 Alexander Schreiber
                           secondary_ip,
1403 16abfbc2 Alexander Schreiber
                           constants.DEFAULT_NODED_PORT):
1404 16abfbc2 Alexander Schreiber
        raise errors.OpPrereqError(
1405 16abfbc2 Alexander Schreiber
          "Node secondary ip not reachable by TCP based ping to noded port")
1406 a8083063 Iustin Pop
1407 a8083063 Iustin Pop
    self.new_node = objects.Node(name=node,
1408 a8083063 Iustin Pop
                                 primary_ip=primary_ip,
1409 a8083063 Iustin Pop
                                 secondary_ip=secondary_ip)
1410 a8083063 Iustin Pop
1411 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1412 a8083063 Iustin Pop
    """Adds the new node to the cluster.
1413 a8083063 Iustin Pop

1414 a8083063 Iustin Pop
    """
1415 a8083063 Iustin Pop
    new_node = self.new_node
1416 a8083063 Iustin Pop
    node = new_node.name
1417 a8083063 Iustin Pop
1418 a8083063 Iustin Pop
    # set up inter-node password and certificate and restarts the node daemon
1419 a8083063 Iustin Pop
    gntpass = self.sstore.GetNodeDaemonPassword()
1420 a8083063 Iustin Pop
    if not re.match('^[a-zA-Z0-9.]{1,64}$', gntpass):
1421 3ecf6786 Iustin Pop
      raise errors.OpExecError("ganeti password corruption detected")
1422 a8083063 Iustin Pop
    f = open(constants.SSL_CERT_FILE)
1423 a8083063 Iustin Pop
    try:
1424 a8083063 Iustin Pop
      gntpem = f.read(8192)
1425 a8083063 Iustin Pop
    finally:
1426 a8083063 Iustin Pop
      f.close()
1427 a8083063 Iustin Pop
    # in the base64 pem encoding, neither '!' nor '.' are valid chars,
1428 a8083063 Iustin Pop
    # so we use this to detect an invalid certificate; as long as the
1429 a8083063 Iustin Pop
    # cert doesn't contain this, the here-document will be correctly
1430 a8083063 Iustin Pop
    # parsed by the shell sequence below
1431 a8083063 Iustin Pop
    if re.search('^!EOF\.', gntpem, re.MULTILINE):
1432 3ecf6786 Iustin Pop
      raise errors.OpExecError("invalid PEM encoding in the SSL certificate")
1433 a8083063 Iustin Pop
    if not gntpem.endswith("\n"):
1434 3ecf6786 Iustin Pop
      raise errors.OpExecError("PEM must end with newline")
1435 a8083063 Iustin Pop
    logger.Info("copy cluster pass to %s and starting the node daemon" % node)
1436 a8083063 Iustin Pop
1437 a8083063 Iustin Pop
    # and then connect with ssh to set password and start ganeti-noded
1438 a8083063 Iustin Pop
    # note that all the below variables are sanitized at this point,
1439 a8083063 Iustin Pop
    # either by being constants or by the checks above
1440 a8083063 Iustin Pop
    ss = self.sstore
1441 a8083063 Iustin Pop
    mycommand = ("umask 077 && "
1442 a8083063 Iustin Pop
                 "echo '%s' > '%s' && "
1443 a8083063 Iustin Pop
                 "cat > '%s' << '!EOF.' && \n"
1444 a8083063 Iustin Pop
                 "%s!EOF.\n%s restart" %
1445 a8083063 Iustin Pop
                 (gntpass, ss.KeyToFilename(ss.SS_NODED_PASS),
1446 a8083063 Iustin Pop
                  constants.SSL_CERT_FILE, gntpem,
1447 a8083063 Iustin Pop
                  constants.NODE_INITD_SCRIPT))
1448 a8083063 Iustin Pop
1449 a8083063 Iustin Pop
    result = ssh.SSHCall(node, 'root', mycommand, batch=False, ask_key=True)
1450 a8083063 Iustin Pop
    if result.failed:
1451 3ecf6786 Iustin Pop
      raise errors.OpExecError("Remote command on node %s, error: %s,"
1452 3ecf6786 Iustin Pop
                               " output: %s" %
1453 3ecf6786 Iustin Pop
                               (node, result.fail_reason, result.output))
1454 a8083063 Iustin Pop
1455 a8083063 Iustin Pop
    # check connectivity
1456 a8083063 Iustin Pop
    time.sleep(4)
1457 a8083063 Iustin Pop
1458 a8083063 Iustin Pop
    result = rpc.call_version([node])[node]
1459 a8083063 Iustin Pop
    if result:
1460 a8083063 Iustin Pop
      if constants.PROTOCOL_VERSION == result:
1461 a8083063 Iustin Pop
        logger.Info("communication to node %s fine, sw version %s match" %
1462 a8083063 Iustin Pop
                    (node, result))
1463 a8083063 Iustin Pop
      else:
1464 3ecf6786 Iustin Pop
        raise errors.OpExecError("Version mismatch master version %s,"
1465 3ecf6786 Iustin Pop
                                 " node version %s" %
1466 3ecf6786 Iustin Pop
                                 (constants.PROTOCOL_VERSION, result))
1467 a8083063 Iustin Pop
    else:
1468 3ecf6786 Iustin Pop
      raise errors.OpExecError("Cannot get version from the new node")
1469 a8083063 Iustin Pop
1470 a8083063 Iustin Pop
    # setup ssh on node
1471 a8083063 Iustin Pop
    logger.Info("copy ssh key to node %s" % node)
1472 70d9e3d8 Iustin Pop
    priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS)
1473 a8083063 Iustin Pop
    keyarray = []
1474 70d9e3d8 Iustin Pop
    keyfiles = [constants.SSH_HOST_DSA_PRIV, constants.SSH_HOST_DSA_PUB,
1475 70d9e3d8 Iustin Pop
                constants.SSH_HOST_RSA_PRIV, constants.SSH_HOST_RSA_PUB,
1476 70d9e3d8 Iustin Pop
                priv_key, pub_key]
1477 a8083063 Iustin Pop
1478 a8083063 Iustin Pop
    for i in keyfiles:
1479 a8083063 Iustin Pop
      f = open(i, 'r')
1480 a8083063 Iustin Pop
      try:
1481 a8083063 Iustin Pop
        keyarray.append(f.read())
1482 a8083063 Iustin Pop
      finally:
1483 a8083063 Iustin Pop
        f.close()
1484 a8083063 Iustin Pop
1485 a8083063 Iustin Pop
    result = rpc.call_node_add(node, keyarray[0], keyarray[1], keyarray[2],
1486 a8083063 Iustin Pop
                               keyarray[3], keyarray[4], keyarray[5])
1487 a8083063 Iustin Pop
1488 a8083063 Iustin Pop
    if not result:
1489 3ecf6786 Iustin Pop
      raise errors.OpExecError("Cannot transfer ssh keys to the new node")
1490 a8083063 Iustin Pop
1491 a8083063 Iustin Pop
    # Add node to our /etc/hosts, and add key to known_hosts
1492 9440aeab Michael Hanselmann
    _AddHostToEtcHosts(new_node.name)
1493 c8a0948f Michael Hanselmann
1494 a8083063 Iustin Pop
    _UpdateKnownHosts(new_node.name, new_node.primary_ip,
1495 a8083063 Iustin Pop
                      self.cfg.GetHostKey())
1496 a8083063 Iustin Pop
1497 a8083063 Iustin Pop
    if new_node.secondary_ip != new_node.primary_ip:
1498 16abfbc2 Alexander Schreiber
      if not rpc.call_node_tcp_ping(new_node.name,
1499 16abfbc2 Alexander Schreiber
                                    constants.LOCALHOST_IP_ADDRESS,
1500 16abfbc2 Alexander Schreiber
                                    new_node.secondary_ip,
1501 16abfbc2 Alexander Schreiber
                                    constants.DEFAULT_NODED_PORT,
1502 16abfbc2 Alexander Schreiber
                                    10, False):
1503 3ecf6786 Iustin Pop
        raise errors.OpExecError("Node claims it doesn't have the"
1504 3ecf6786 Iustin Pop
                                 " secondary ip you gave (%s).\n"
1505 3ecf6786 Iustin Pop
                                 "Please fix and re-run this command." %
1506 3ecf6786 Iustin Pop
                                 new_node.secondary_ip)
1507 a8083063 Iustin Pop
1508 ff98055b Iustin Pop
    success, msg = ssh.VerifyNodeHostname(node)
1509 ff98055b Iustin Pop
    if not success:
1510 ff98055b Iustin Pop
      raise errors.OpExecError("Node '%s' claims it has a different hostname"
1511 ff98055b Iustin Pop
                               " than the one the resolver gives: %s.\n"
1512 ff98055b Iustin Pop
                               "Please fix and re-run this command." %
1513 ff98055b Iustin Pop
                               (node, msg))
1514 ff98055b Iustin Pop
1515 a8083063 Iustin Pop
    # Distribute updated /etc/hosts and known_hosts to all nodes,
1516 a8083063 Iustin Pop
    # including the node just added
1517 880478f8 Iustin Pop
    myself = self.cfg.GetNodeInfo(self.sstore.GetMasterNode())
1518 a8083063 Iustin Pop
    dist_nodes = self.cfg.GetNodeList() + [node]
1519 a8083063 Iustin Pop
    if myself.name in dist_nodes:
1520 a8083063 Iustin Pop
      dist_nodes.remove(myself.name)
1521 a8083063 Iustin Pop
1522 a8083063 Iustin Pop
    logger.Debug("Copying hosts and known_hosts to all nodes")
1523 82122173 Iustin Pop
    for fname in ("/etc/hosts", constants.SSH_KNOWN_HOSTS_FILE):
1524 a8083063 Iustin Pop
      result = rpc.call_upload_file(dist_nodes, fname)
1525 a8083063 Iustin Pop
      for to_node in dist_nodes:
1526 a8083063 Iustin Pop
        if not result[to_node]:
1527 a8083063 Iustin Pop
          logger.Error("copy of file %s to node %s failed" %
1528 a8083063 Iustin Pop
                       (fname, to_node))
1529 a8083063 Iustin Pop
1530 cb91d46e Iustin Pop
    to_copy = ss.GetFileList()
1531 a8083063 Iustin Pop
    for fname in to_copy:
1532 a8083063 Iustin Pop
      if not ssh.CopyFileToNode(node, fname):
1533 a8083063 Iustin Pop
        logger.Error("could not copy file %s to node %s" % (fname, node))
1534 a8083063 Iustin Pop
1535 a8083063 Iustin Pop
    logger.Info("adding node %s to cluster.conf" % node)
1536 a8083063 Iustin Pop
    self.cfg.AddNode(new_node)
1537 a8083063 Iustin Pop
1538 a8083063 Iustin Pop
1539 a8083063 Iustin Pop
class LUMasterFailover(LogicalUnit):
1540 a8083063 Iustin Pop
  """Failover the master node to the current node.
1541 a8083063 Iustin Pop

1542 a8083063 Iustin Pop
  This is a special LU in that it must run on a non-master node.
1543 a8083063 Iustin Pop

1544 a8083063 Iustin Pop
  """
1545 a8083063 Iustin Pop
  HPATH = "master-failover"
1546 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_CLUSTER
1547 a8083063 Iustin Pop
  REQ_MASTER = False
1548 a8083063 Iustin Pop
  _OP_REQP = []
1549 a8083063 Iustin Pop
1550 a8083063 Iustin Pop
  def BuildHooksEnv(self):
1551 a8083063 Iustin Pop
    """Build hooks env.
1552 a8083063 Iustin Pop

1553 a8083063 Iustin Pop
    This will run on the new master only in the pre phase, and on all
1554 a8083063 Iustin Pop
    the nodes in the post phase.
1555 a8083063 Iustin Pop

1556 a8083063 Iustin Pop
    """
1557 a8083063 Iustin Pop
    env = {
1558 0e137c28 Iustin Pop
      "OP_TARGET": self.new_master,
1559 a8083063 Iustin Pop
      "NEW_MASTER": self.new_master,
1560 a8083063 Iustin Pop
      "OLD_MASTER": self.old_master,
1561 a8083063 Iustin Pop
      }
1562 a8083063 Iustin Pop
    return env, [self.new_master], self.cfg.GetNodeList()
1563 a8083063 Iustin Pop
1564 a8083063 Iustin Pop
  def CheckPrereq(self):
1565 a8083063 Iustin Pop
    """Check prerequisites.
1566 a8083063 Iustin Pop

1567 a8083063 Iustin Pop
    This checks that we are not already the master.
1568 a8083063 Iustin Pop

1569 a8083063 Iustin Pop
    """
1570 89e1fc26 Iustin Pop
    self.new_master = utils.HostInfo().name
1571 880478f8 Iustin Pop
    self.old_master = self.sstore.GetMasterNode()
1572 a8083063 Iustin Pop
1573 a8083063 Iustin Pop
    if self.old_master == self.new_master:
1574 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("This commands must be run on the node"
1575 3ecf6786 Iustin Pop
                                 " where you want the new master to be.\n"
1576 3ecf6786 Iustin Pop
                                 "%s is already the master" %
1577 3ecf6786 Iustin Pop
                                 self.old_master)
1578 a8083063 Iustin Pop
1579 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1580 a8083063 Iustin Pop
    """Failover the master node.
1581 a8083063 Iustin Pop

1582 a8083063 Iustin Pop
    This command, when run on a non-master node, will cause the current
1583 a8083063 Iustin Pop
    master to cease being master, and the non-master to become new
1584 a8083063 Iustin Pop
    master.
1585 a8083063 Iustin Pop

1586 a8083063 Iustin Pop
    """
1587 a8083063 Iustin Pop
    #TODO: do not rely on gethostname returning the FQDN
1588 a8083063 Iustin Pop
    logger.Info("setting master to %s, old master: %s" %
1589 a8083063 Iustin Pop
                (self.new_master, self.old_master))
1590 a8083063 Iustin Pop
1591 a8083063 Iustin Pop
    if not rpc.call_node_stop_master(self.old_master):
1592 a8083063 Iustin Pop
      logger.Error("could disable the master role on the old master"
1593 a8083063 Iustin Pop
                   " %s, please disable manually" % self.old_master)
1594 a8083063 Iustin Pop
1595 880478f8 Iustin Pop
    ss = self.sstore
1596 880478f8 Iustin Pop
    ss.SetKey(ss.SS_MASTER_NODE, self.new_master)
1597 880478f8 Iustin Pop
    if not rpc.call_upload_file(self.cfg.GetNodeList(),
1598 880478f8 Iustin Pop
                                ss.KeyToFilename(ss.SS_MASTER_NODE)):
1599 880478f8 Iustin Pop
      logger.Error("could not distribute the new simple store master file"
1600 880478f8 Iustin Pop
                   " to the other nodes, please check.")
1601 880478f8 Iustin Pop
1602 a8083063 Iustin Pop
    if not rpc.call_node_start_master(self.new_master):
1603 a8083063 Iustin Pop
      logger.Error("could not start the master role on the new master"
1604 a8083063 Iustin Pop
                   " %s, please check" % self.new_master)
1605 880478f8 Iustin Pop
      feedback_fn("Error in activating the master IP on the new master,\n"
1606 880478f8 Iustin Pop
                  "please fix manually.")
1607 a8083063 Iustin Pop
1608 a8083063 Iustin Pop
1609 a8083063 Iustin Pop
1610 a8083063 Iustin Pop
class LUQueryClusterInfo(NoHooksLU):
1611 a8083063 Iustin Pop
  """Query cluster configuration.
1612 a8083063 Iustin Pop

1613 a8083063 Iustin Pop
  """
1614 a8083063 Iustin Pop
  _OP_REQP = []
1615 59322403 Iustin Pop
  REQ_MASTER = False
1616 a8083063 Iustin Pop
1617 a8083063 Iustin Pop
  def CheckPrereq(self):
1618 a8083063 Iustin Pop
    """No prerequsites needed for this LU.
1619 a8083063 Iustin Pop

1620 a8083063 Iustin Pop
    """
1621 a8083063 Iustin Pop
    pass
1622 a8083063 Iustin Pop
1623 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1624 a8083063 Iustin Pop
    """Return cluster config.
1625 a8083063 Iustin Pop

1626 a8083063 Iustin Pop
    """
1627 a8083063 Iustin Pop
    result = {
1628 5fcdc80d Iustin Pop
      "name": self.sstore.GetClusterName(),
1629 a8083063 Iustin Pop
      "software_version": constants.RELEASE_VERSION,
1630 a8083063 Iustin Pop
      "protocol_version": constants.PROTOCOL_VERSION,
1631 a8083063 Iustin Pop
      "config_version": constants.CONFIG_VERSION,
1632 a8083063 Iustin Pop
      "os_api_version": constants.OS_API_VERSION,
1633 a8083063 Iustin Pop
      "export_version": constants.EXPORT_VERSION,
1634 880478f8 Iustin Pop
      "master": self.sstore.GetMasterNode(),
1635 a8083063 Iustin Pop
      "architecture": (platform.architecture()[0], platform.machine()),
1636 a8083063 Iustin Pop
      }
1637 a8083063 Iustin Pop
1638 a8083063 Iustin Pop
    return result
1639 a8083063 Iustin Pop
1640 a8083063 Iustin Pop
1641 a8083063 Iustin Pop
class LUClusterCopyFile(NoHooksLU):
1642 a8083063 Iustin Pop
  """Copy file to cluster.
1643 a8083063 Iustin Pop

1644 a8083063 Iustin Pop
  """
1645 a8083063 Iustin Pop
  _OP_REQP = ["nodes", "filename"]
1646 a8083063 Iustin Pop
1647 a8083063 Iustin Pop
  def CheckPrereq(self):
1648 a8083063 Iustin Pop
    """Check prerequisites.
1649 a8083063 Iustin Pop

1650 a8083063 Iustin Pop
    It should check that the named file exists and that the given list
1651 a8083063 Iustin Pop
    of nodes is valid.
1652 a8083063 Iustin Pop

1653 a8083063 Iustin Pop
    """
1654 a8083063 Iustin Pop
    if not os.path.exists(self.op.filename):
1655 a8083063 Iustin Pop
      raise errors.OpPrereqError("No such filename '%s'" % self.op.filename)
1656 dcb93971 Michael Hanselmann
1657 dcb93971 Michael Hanselmann
    self.nodes = _GetWantedNodes(self, self.op.nodes)
1658 a8083063 Iustin Pop
1659 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1660 a8083063 Iustin Pop
    """Copy a file from master to some nodes.
1661 a8083063 Iustin Pop

1662 a8083063 Iustin Pop
    Args:
1663 a8083063 Iustin Pop
      opts - class with options as members
1664 a8083063 Iustin Pop
      args - list containing a single element, the file name
1665 a8083063 Iustin Pop
    Opts used:
1666 a8083063 Iustin Pop
      nodes - list containing the name of target nodes; if empty, all nodes
1667 a8083063 Iustin Pop

1668 a8083063 Iustin Pop
    """
1669 a8083063 Iustin Pop
    filename = self.op.filename
1670 a8083063 Iustin Pop
1671 89e1fc26 Iustin Pop
    myname = utils.HostInfo().name
1672 a8083063 Iustin Pop
1673 a7ba5e53 Iustin Pop
    for node in self.nodes:
1674 a8083063 Iustin Pop
      if node == myname:
1675 a8083063 Iustin Pop
        continue
1676 a8083063 Iustin Pop
      if not ssh.CopyFileToNode(node, filename):
1677 a8083063 Iustin Pop
        logger.Error("Copy of file %s to node %s failed" % (filename, node))
1678 a8083063 Iustin Pop
1679 a8083063 Iustin Pop
1680 a8083063 Iustin Pop
class LUDumpClusterConfig(NoHooksLU):
1681 a8083063 Iustin Pop
  """Return a text-representation of the cluster-config.
1682 a8083063 Iustin Pop

1683 a8083063 Iustin Pop
  """
1684 a8083063 Iustin Pop
  _OP_REQP = []
1685 a8083063 Iustin Pop
1686 a8083063 Iustin Pop
  def CheckPrereq(self):
1687 a8083063 Iustin Pop
    """No prerequisites.
1688 a8083063 Iustin Pop

1689 a8083063 Iustin Pop
    """
1690 a8083063 Iustin Pop
    pass
1691 a8083063 Iustin Pop
1692 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1693 a8083063 Iustin Pop
    """Dump a representation of the cluster config to the standard output.
1694 a8083063 Iustin Pop

1695 a8083063 Iustin Pop
    """
1696 a8083063 Iustin Pop
    return self.cfg.DumpConfig()
1697 a8083063 Iustin Pop
1698 a8083063 Iustin Pop
1699 a8083063 Iustin Pop
class LURunClusterCommand(NoHooksLU):
1700 a8083063 Iustin Pop
  """Run a command on some nodes.
1701 a8083063 Iustin Pop

1702 a8083063 Iustin Pop
  """
1703 a8083063 Iustin Pop
  _OP_REQP = ["command", "nodes"]
1704 a8083063 Iustin Pop
1705 a8083063 Iustin Pop
  def CheckPrereq(self):
1706 a8083063 Iustin Pop
    """Check prerequisites.
1707 a8083063 Iustin Pop

1708 a8083063 Iustin Pop
    It checks that the given list of nodes is valid.
1709 a8083063 Iustin Pop

1710 a8083063 Iustin Pop
    """
1711 dcb93971 Michael Hanselmann
    self.nodes = _GetWantedNodes(self, self.op.nodes)
1712 a8083063 Iustin Pop
1713 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1714 a8083063 Iustin Pop
    """Run a command on some nodes.
1715 a8083063 Iustin Pop

1716 a8083063 Iustin Pop
    """
1717 a8083063 Iustin Pop
    data = []
1718 a8083063 Iustin Pop
    for node in self.nodes:
1719 a7ba5e53 Iustin Pop
      result = ssh.SSHCall(node, "root", self.op.command)
1720 a7ba5e53 Iustin Pop
      data.append((node, result.output, result.exit_code))
1721 a8083063 Iustin Pop
1722 a8083063 Iustin Pop
    return data
1723 a8083063 Iustin Pop
1724 a8083063 Iustin Pop
1725 a8083063 Iustin Pop
class LUActivateInstanceDisks(NoHooksLU):
1726 a8083063 Iustin Pop
  """Bring up an instance's disks.
1727 a8083063 Iustin Pop

1728 a8083063 Iustin Pop
  """
1729 a8083063 Iustin Pop
  _OP_REQP = ["instance_name"]
1730 a8083063 Iustin Pop
1731 a8083063 Iustin Pop
  def CheckPrereq(self):
1732 a8083063 Iustin Pop
    """Check prerequisites.
1733 a8083063 Iustin Pop

1734 a8083063 Iustin Pop
    This checks that the instance is in the cluster.
1735 a8083063 Iustin Pop

1736 a8083063 Iustin Pop
    """
1737 a8083063 Iustin Pop
    instance = self.cfg.GetInstanceInfo(
1738 a8083063 Iustin Pop
      self.cfg.ExpandInstanceName(self.op.instance_name))
1739 a8083063 Iustin Pop
    if instance is None:
1740 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' not known" %
1741 3ecf6786 Iustin Pop
                                 self.op.instance_name)
1742 a8083063 Iustin Pop
    self.instance = instance
1743 a8083063 Iustin Pop
1744 a8083063 Iustin Pop
1745 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1746 a8083063 Iustin Pop
    """Activate the disks.
1747 a8083063 Iustin Pop

1748 a8083063 Iustin Pop
    """
1749 a8083063 Iustin Pop
    disks_ok, disks_info = _AssembleInstanceDisks(self.instance, self.cfg)
1750 a8083063 Iustin Pop
    if not disks_ok:
1751 3ecf6786 Iustin Pop
      raise errors.OpExecError("Cannot activate block devices")
1752 a8083063 Iustin Pop
1753 a8083063 Iustin Pop
    return disks_info
1754 a8083063 Iustin Pop
1755 a8083063 Iustin Pop
1756 a8083063 Iustin Pop
def _AssembleInstanceDisks(instance, cfg, ignore_secondaries=False):
1757 a8083063 Iustin Pop
  """Prepare the block devices for an instance.
1758 a8083063 Iustin Pop

1759 a8083063 Iustin Pop
  This sets up the block devices on all nodes.
1760 a8083063 Iustin Pop

1761 a8083063 Iustin Pop
  Args:
1762 a8083063 Iustin Pop
    instance: a ganeti.objects.Instance object
1763 a8083063 Iustin Pop
    ignore_secondaries: if true, errors on secondary nodes won't result
1764 a8083063 Iustin Pop
                        in an error return from the function
1765 a8083063 Iustin Pop

1766 a8083063 Iustin Pop
  Returns:
1767 a8083063 Iustin Pop
    false if the operation failed
1768 a8083063 Iustin Pop
    list of (host, instance_visible_name, node_visible_name) if the operation
1769 a8083063 Iustin Pop
         suceeded with the mapping from node devices to instance devices
1770 a8083063 Iustin Pop
  """
1771 a8083063 Iustin Pop
  device_info = []
1772 a8083063 Iustin Pop
  disks_ok = True
1773 a8083063 Iustin Pop
  for inst_disk in instance.disks:
1774 a8083063 Iustin Pop
    master_result = None
1775 a8083063 Iustin Pop
    for node, node_disk in inst_disk.ComputeNodeTree(instance.primary_node):
1776 a8083063 Iustin Pop
      cfg.SetDiskID(node_disk, node)
1777 a8083063 Iustin Pop
      is_primary = node == instance.primary_node
1778 3f78eef2 Iustin Pop
      result = rpc.call_blockdev_assemble(node, node_disk,
1779 3f78eef2 Iustin Pop
                                          instance.name, is_primary)
1780 a8083063 Iustin Pop
      if not result:
1781 a8083063 Iustin Pop
        logger.Error("could not prepare block device %s on node %s (is_pri"
1782 a8083063 Iustin Pop
                     "mary=%s)" % (inst_disk.iv_name, node, is_primary))
1783 a8083063 Iustin Pop
        if is_primary or not ignore_secondaries:
1784 a8083063 Iustin Pop
          disks_ok = False
1785 a8083063 Iustin Pop
      if is_primary:
1786 a8083063 Iustin Pop
        master_result = result
1787 a8083063 Iustin Pop
    device_info.append((instance.primary_node, inst_disk.iv_name,
1788 a8083063 Iustin Pop
                        master_result))
1789 a8083063 Iustin Pop
1790 b352ab5b Iustin Pop
  # leave the disks configured for the primary node
1791 b352ab5b Iustin Pop
  # this is a workaround that would be fixed better by
1792 b352ab5b Iustin Pop
  # improving the logical/physical id handling
1793 b352ab5b Iustin Pop
  for disk in instance.disks:
1794 b352ab5b Iustin Pop
    cfg.SetDiskID(disk, instance.primary_node)
1795 b352ab5b Iustin Pop
1796 a8083063 Iustin Pop
  return disks_ok, device_info
1797 a8083063 Iustin Pop
1798 a8083063 Iustin Pop
1799 fe7b0351 Michael Hanselmann
def _StartInstanceDisks(cfg, instance, force):
1800 3ecf6786 Iustin Pop
  """Start the disks of an instance.
1801 3ecf6786 Iustin Pop

1802 3ecf6786 Iustin Pop
  """
1803 fe7b0351 Michael Hanselmann
  disks_ok, dummy = _AssembleInstanceDisks(instance, cfg,
1804 fe7b0351 Michael Hanselmann
                                           ignore_secondaries=force)
1805 fe7b0351 Michael Hanselmann
  if not disks_ok:
1806 fe7b0351 Michael Hanselmann
    _ShutdownInstanceDisks(instance, cfg)
1807 fe7b0351 Michael Hanselmann
    if force is not None and not force:
1808 fe7b0351 Michael Hanselmann
      logger.Error("If the message above refers to a secondary node,"
1809 fe7b0351 Michael Hanselmann
                   " you can retry the operation using '--force'.")
1810 3ecf6786 Iustin Pop
    raise errors.OpExecError("Disk consistency error")
1811 fe7b0351 Michael Hanselmann
1812 fe7b0351 Michael Hanselmann
1813 a8083063 Iustin Pop
class LUDeactivateInstanceDisks(NoHooksLU):
1814 a8083063 Iustin Pop
  """Shutdown an instance's disks.
1815 a8083063 Iustin Pop

1816 a8083063 Iustin Pop
  """
1817 a8083063 Iustin Pop
  _OP_REQP = ["instance_name"]
1818 a8083063 Iustin Pop
1819 a8083063 Iustin Pop
  def CheckPrereq(self):
1820 a8083063 Iustin Pop
    """Check prerequisites.
1821 a8083063 Iustin Pop

1822 a8083063 Iustin Pop
    This checks that the instance is in the cluster.
1823 a8083063 Iustin Pop

1824 a8083063 Iustin Pop
    """
1825 a8083063 Iustin Pop
    instance = self.cfg.GetInstanceInfo(
1826 a8083063 Iustin Pop
      self.cfg.ExpandInstanceName(self.op.instance_name))
1827 a8083063 Iustin Pop
    if instance is None:
1828 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' not known" %
1829 3ecf6786 Iustin Pop
                                 self.op.instance_name)
1830 a8083063 Iustin Pop
    self.instance = instance
1831 a8083063 Iustin Pop
1832 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1833 a8083063 Iustin Pop
    """Deactivate the disks
1834 a8083063 Iustin Pop

1835 a8083063 Iustin Pop
    """
1836 a8083063 Iustin Pop
    instance = self.instance
1837 a8083063 Iustin Pop
    ins_l = rpc.call_instance_list([instance.primary_node])
1838 a8083063 Iustin Pop
    ins_l = ins_l[instance.primary_node]
1839 a8083063 Iustin Pop
    if not type(ins_l) is list:
1840 3ecf6786 Iustin Pop
      raise errors.OpExecError("Can't contact node '%s'" %
1841 3ecf6786 Iustin Pop
                               instance.primary_node)
1842 a8083063 Iustin Pop
1843 a8083063 Iustin Pop
    if self.instance.name in ins_l:
1844 3ecf6786 Iustin Pop
      raise errors.OpExecError("Instance is running, can't shutdown"
1845 3ecf6786 Iustin Pop
                               " block devices.")
1846 a8083063 Iustin Pop
1847 a8083063 Iustin Pop
    _ShutdownInstanceDisks(instance, self.cfg)
1848 a8083063 Iustin Pop
1849 a8083063 Iustin Pop
1850 a8083063 Iustin Pop
def _ShutdownInstanceDisks(instance, cfg, ignore_primary=False):
1851 a8083063 Iustin Pop
  """Shutdown block devices of an instance.
1852 a8083063 Iustin Pop

1853 a8083063 Iustin Pop
  This does the shutdown on all nodes of the instance.
1854 a8083063 Iustin Pop

1855 a8083063 Iustin Pop
  If the ignore_primary is false, errors on the primary node are
1856 a8083063 Iustin Pop
  ignored.
1857 a8083063 Iustin Pop

1858 a8083063 Iustin Pop
  """
1859 a8083063 Iustin Pop
  result = True
1860 a8083063 Iustin Pop
  for disk in instance.disks:
1861 a8083063 Iustin Pop
    for node, top_disk in disk.ComputeNodeTree(instance.primary_node):
1862 a8083063 Iustin Pop
      cfg.SetDiskID(top_disk, node)
1863 a8083063 Iustin Pop
      if not rpc.call_blockdev_shutdown(node, top_disk):
1864 a8083063 Iustin Pop
        logger.Error("could not shutdown block device %s on node %s" %
1865 a8083063 Iustin Pop
                     (disk.iv_name, node))
1866 a8083063 Iustin Pop
        if not ignore_primary or node != instance.primary_node:
1867 a8083063 Iustin Pop
          result = False
1868 a8083063 Iustin Pop
  return result
1869 a8083063 Iustin Pop
1870 a8083063 Iustin Pop
1871 a8083063 Iustin Pop
class LUStartupInstance(LogicalUnit):
1872 a8083063 Iustin Pop
  """Starts an instance.
1873 a8083063 Iustin Pop

1874 a8083063 Iustin Pop
  """
1875 a8083063 Iustin Pop
  HPATH = "instance-start"
1876 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_INSTANCE
1877 a8083063 Iustin Pop
  _OP_REQP = ["instance_name", "force"]
1878 a8083063 Iustin Pop
1879 a8083063 Iustin Pop
  def BuildHooksEnv(self):
1880 a8083063 Iustin Pop
    """Build hooks env.
1881 a8083063 Iustin Pop

1882 a8083063 Iustin Pop
    This runs on master, primary and secondary nodes of the instance.
1883 a8083063 Iustin Pop

1884 a8083063 Iustin Pop
    """
1885 a8083063 Iustin Pop
    env = {
1886 a8083063 Iustin Pop
      "FORCE": self.op.force,
1887 a8083063 Iustin Pop
      }
1888 396e1b78 Michael Hanselmann
    env.update(_BuildInstanceHookEnvByObject(self.instance))
1889 880478f8 Iustin Pop
    nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
1890 a8083063 Iustin Pop
          list(self.instance.secondary_nodes))
1891 a8083063 Iustin Pop
    return env, nl, nl
1892 a8083063 Iustin Pop
1893 a8083063 Iustin Pop
  def CheckPrereq(self):
1894 a8083063 Iustin Pop
    """Check prerequisites.
1895 a8083063 Iustin Pop

1896 a8083063 Iustin Pop
    This checks that the instance is in the cluster.
1897 a8083063 Iustin Pop

1898 a8083063 Iustin Pop
    """
1899 a8083063 Iustin Pop
    instance = self.cfg.GetInstanceInfo(
1900 a8083063 Iustin Pop
      self.cfg.ExpandInstanceName(self.op.instance_name))
1901 a8083063 Iustin Pop
    if instance is None:
1902 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' not known" %
1903 3ecf6786 Iustin Pop
                                 self.op.instance_name)
1904 a8083063 Iustin Pop
1905 a8083063 Iustin Pop
    # check bridges existance
1906 bf6929a2 Alexander Schreiber
    _CheckInstanceBridgesExist(instance)
1907 a8083063 Iustin Pop
1908 a8083063 Iustin Pop
    self.instance = instance
1909 a8083063 Iustin Pop
    self.op.instance_name = instance.name
1910 a8083063 Iustin Pop
1911 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
1912 a8083063 Iustin Pop
    """Start the instance.
1913 a8083063 Iustin Pop

1914 a8083063 Iustin Pop
    """
1915 a8083063 Iustin Pop
    instance = self.instance
1916 a8083063 Iustin Pop
    force = self.op.force
1917 a8083063 Iustin Pop
    extra_args = getattr(self.op, "extra_args", "")
1918 a8083063 Iustin Pop
1919 a8083063 Iustin Pop
    node_current = instance.primary_node
1920 a8083063 Iustin Pop
1921 a8083063 Iustin Pop
    nodeinfo = rpc.call_node_info([node_current], self.cfg.GetVGName())
1922 a8083063 Iustin Pop
    if not nodeinfo:
1923 3ecf6786 Iustin Pop
      raise errors.OpExecError("Could not contact node %s for infos" %
1924 3ecf6786 Iustin Pop
                               (node_current))
1925 a8083063 Iustin Pop
1926 a8083063 Iustin Pop
    freememory = nodeinfo[node_current]['memory_free']
1927 a8083063 Iustin Pop
    memory = instance.memory
1928 a8083063 Iustin Pop
    if memory > freememory:
1929 3ecf6786 Iustin Pop
      raise errors.OpExecError("Not enough memory to start instance"
1930 3ecf6786 Iustin Pop
                               " %s on node %s"
1931 3ecf6786 Iustin Pop
                               " needed %s MiB, available %s MiB" %
1932 3ecf6786 Iustin Pop
                               (instance.name, node_current, memory,
1933 3ecf6786 Iustin Pop
                                freememory))
1934 a8083063 Iustin Pop
1935 fe7b0351 Michael Hanselmann
    _StartInstanceDisks(self.cfg, instance, force)
1936 a8083063 Iustin Pop
1937 a8083063 Iustin Pop
    if not rpc.call_instance_start(node_current, instance, extra_args):
1938 a8083063 Iustin Pop
      _ShutdownInstanceDisks(instance, self.cfg)
1939 3ecf6786 Iustin Pop
      raise errors.OpExecError("Could not start instance")
1940 a8083063 Iustin Pop
1941 a8083063 Iustin Pop
    self.cfg.MarkInstanceUp(instance.name)
1942 a8083063 Iustin Pop
1943 a8083063 Iustin Pop
1944 bf6929a2 Alexander Schreiber
class LURebootInstance(LogicalUnit):
1945 bf6929a2 Alexander Schreiber
  """Reboot an instance.
1946 bf6929a2 Alexander Schreiber

1947 bf6929a2 Alexander Schreiber
  """
1948 bf6929a2 Alexander Schreiber
  HPATH = "instance-reboot"
1949 bf6929a2 Alexander Schreiber
  HTYPE = constants.HTYPE_INSTANCE
1950 bf6929a2 Alexander Schreiber
  _OP_REQP = ["instance_name", "ignore_secondaries", "reboot_type"]
1951 bf6929a2 Alexander Schreiber
1952 bf6929a2 Alexander Schreiber
  def BuildHooksEnv(self):
1953 bf6929a2 Alexander Schreiber
    """Build hooks env.
1954 bf6929a2 Alexander Schreiber

1955 bf6929a2 Alexander Schreiber
    This runs on master, primary and secondary nodes of the instance.
1956 bf6929a2 Alexander Schreiber

1957 bf6929a2 Alexander Schreiber
    """
1958 bf6929a2 Alexander Schreiber
    env = {
1959 bf6929a2 Alexander Schreiber
      "IGNORE_SECONDARIES": self.op.ignore_secondaries,
1960 bf6929a2 Alexander Schreiber
      }
1961 bf6929a2 Alexander Schreiber
    env.update(_BuildInstanceHookEnvByObject(self.instance))
1962 bf6929a2 Alexander Schreiber
    nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
1963 bf6929a2 Alexander Schreiber
          list(self.instance.secondary_nodes))
1964 bf6929a2 Alexander Schreiber
    return env, nl, nl
1965 bf6929a2 Alexander Schreiber
1966 bf6929a2 Alexander Schreiber
  def CheckPrereq(self):
1967 bf6929a2 Alexander Schreiber
    """Check prerequisites.
1968 bf6929a2 Alexander Schreiber

1969 bf6929a2 Alexander Schreiber
    This checks that the instance is in the cluster.
1970 bf6929a2 Alexander Schreiber

1971 bf6929a2 Alexander Schreiber
    """
1972 bf6929a2 Alexander Schreiber
    instance = self.cfg.GetInstanceInfo(
1973 bf6929a2 Alexander Schreiber
      self.cfg.ExpandInstanceName(self.op.instance_name))
1974 bf6929a2 Alexander Schreiber
    if instance is None:
1975 bf6929a2 Alexander Schreiber
      raise errors.OpPrereqError("Instance '%s' not known" %
1976 bf6929a2 Alexander Schreiber
                                 self.op.instance_name)
1977 bf6929a2 Alexander Schreiber
1978 bf6929a2 Alexander Schreiber
    # check bridges existance
1979 bf6929a2 Alexander Schreiber
    _CheckInstanceBridgesExist(instance)
1980 bf6929a2 Alexander Schreiber
1981 bf6929a2 Alexander Schreiber
    self.instance = instance
1982 bf6929a2 Alexander Schreiber
    self.op.instance_name = instance.name
1983 bf6929a2 Alexander Schreiber
1984 bf6929a2 Alexander Schreiber
  def Exec(self, feedback_fn):
1985 bf6929a2 Alexander Schreiber
    """Reboot the instance.
1986 bf6929a2 Alexander Schreiber

1987 bf6929a2 Alexander Schreiber
    """
1988 bf6929a2 Alexander Schreiber
    instance = self.instance
1989 bf6929a2 Alexander Schreiber
    ignore_secondaries = self.op.ignore_secondaries
1990 bf6929a2 Alexander Schreiber
    reboot_type = self.op.reboot_type
1991 bf6929a2 Alexander Schreiber
    extra_args = getattr(self.op, "extra_args", "")
1992 bf6929a2 Alexander Schreiber
1993 bf6929a2 Alexander Schreiber
    node_current = instance.primary_node
1994 bf6929a2 Alexander Schreiber
1995 bf6929a2 Alexander Schreiber
    if reboot_type not in [constants.INSTANCE_REBOOT_SOFT,
1996 bf6929a2 Alexander Schreiber
                           constants.INSTANCE_REBOOT_HARD,
1997 bf6929a2 Alexander Schreiber
                           constants.INSTANCE_REBOOT_FULL]:
1998 bf6929a2 Alexander Schreiber
      raise errors.ParameterError("reboot type not in [%s, %s, %s]" %
1999 bf6929a2 Alexander Schreiber
                                  (constants.INSTANCE_REBOOT_SOFT,
2000 bf6929a2 Alexander Schreiber
                                   constants.INSTANCE_REBOOT_HARD,
2001 bf6929a2 Alexander Schreiber
                                   constants.INSTANCE_REBOOT_FULL))
2002 bf6929a2 Alexander Schreiber
2003 bf6929a2 Alexander Schreiber
    if reboot_type in [constants.INSTANCE_REBOOT_SOFT,
2004 bf6929a2 Alexander Schreiber
                       constants.INSTANCE_REBOOT_HARD]:
2005 bf6929a2 Alexander Schreiber
      if not rpc.call_instance_reboot(node_current, instance,
2006 bf6929a2 Alexander Schreiber
                                      reboot_type, extra_args):
2007 bf6929a2 Alexander Schreiber
        raise errors.OpExecError("Could not reboot instance")
2008 bf6929a2 Alexander Schreiber
    else:
2009 bf6929a2 Alexander Schreiber
      if not rpc.call_instance_shutdown(node_current, instance):
2010 bf6929a2 Alexander Schreiber
        raise errors.OpExecError("could not shutdown instance for full reboot")
2011 bf6929a2 Alexander Schreiber
      _ShutdownInstanceDisks(instance, self.cfg)
2012 bf6929a2 Alexander Schreiber
      _StartInstanceDisks(self.cfg, instance, ignore_secondaries)
2013 bf6929a2 Alexander Schreiber
      if not rpc.call_instance_start(node_current, instance, extra_args):
2014 bf6929a2 Alexander Schreiber
        _ShutdownInstanceDisks(instance, self.cfg)
2015 bf6929a2 Alexander Schreiber
        raise errors.OpExecError("Could not start instance for full reboot")
2016 bf6929a2 Alexander Schreiber
2017 bf6929a2 Alexander Schreiber
    self.cfg.MarkInstanceUp(instance.name)
2018 bf6929a2 Alexander Schreiber
2019 bf6929a2 Alexander Schreiber
2020 a8083063 Iustin Pop
class LUShutdownInstance(LogicalUnit):
2021 a8083063 Iustin Pop
  """Shutdown an instance.
2022 a8083063 Iustin Pop

2023 a8083063 Iustin Pop
  """
2024 a8083063 Iustin Pop
  HPATH = "instance-stop"
2025 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_INSTANCE
2026 a8083063 Iustin Pop
  _OP_REQP = ["instance_name"]
2027 a8083063 Iustin Pop
2028 a8083063 Iustin Pop
  def BuildHooksEnv(self):
2029 a8083063 Iustin Pop
    """Build hooks env.
2030 a8083063 Iustin Pop

2031 a8083063 Iustin Pop
    This runs on master, primary and secondary nodes of the instance.
2032 a8083063 Iustin Pop

2033 a8083063 Iustin Pop
    """
2034 396e1b78 Michael Hanselmann
    env = _BuildInstanceHookEnvByObject(self.instance)
2035 880478f8 Iustin Pop
    nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
2036 a8083063 Iustin Pop
          list(self.instance.secondary_nodes))
2037 a8083063 Iustin Pop
    return env, nl, nl
2038 a8083063 Iustin Pop
2039 a8083063 Iustin Pop
  def CheckPrereq(self):
2040 a8083063 Iustin Pop
    """Check prerequisites.
2041 a8083063 Iustin Pop

2042 a8083063 Iustin Pop
    This checks that the instance is in the cluster.
2043 a8083063 Iustin Pop

2044 a8083063 Iustin Pop
    """
2045 a8083063 Iustin Pop
    instance = self.cfg.GetInstanceInfo(
2046 a8083063 Iustin Pop
      self.cfg.ExpandInstanceName(self.op.instance_name))
2047 a8083063 Iustin Pop
    if instance is None:
2048 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' not known" %
2049 3ecf6786 Iustin Pop
                                 self.op.instance_name)
2050 a8083063 Iustin Pop
    self.instance = instance
2051 a8083063 Iustin Pop
2052 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
2053 a8083063 Iustin Pop
    """Shutdown the instance.
2054 a8083063 Iustin Pop

2055 a8083063 Iustin Pop
    """
2056 a8083063 Iustin Pop
    instance = self.instance
2057 a8083063 Iustin Pop
    node_current = instance.primary_node
2058 a8083063 Iustin Pop
    if not rpc.call_instance_shutdown(node_current, instance):
2059 a8083063 Iustin Pop
      logger.Error("could not shutdown instance")
2060 a8083063 Iustin Pop
2061 a8083063 Iustin Pop
    self.cfg.MarkInstanceDown(instance.name)
2062 a8083063 Iustin Pop
    _ShutdownInstanceDisks(instance, self.cfg)
2063 a8083063 Iustin Pop
2064 a8083063 Iustin Pop
2065 fe7b0351 Michael Hanselmann
class LUReinstallInstance(LogicalUnit):
2066 fe7b0351 Michael Hanselmann
  """Reinstall an instance.
2067 fe7b0351 Michael Hanselmann

2068 fe7b0351 Michael Hanselmann
  """
2069 fe7b0351 Michael Hanselmann
  HPATH = "instance-reinstall"
2070 fe7b0351 Michael Hanselmann
  HTYPE = constants.HTYPE_INSTANCE
2071 fe7b0351 Michael Hanselmann
  _OP_REQP = ["instance_name"]
2072 fe7b0351 Michael Hanselmann
2073 fe7b0351 Michael Hanselmann
  def BuildHooksEnv(self):
2074 fe7b0351 Michael Hanselmann
    """Build hooks env.
2075 fe7b0351 Michael Hanselmann

2076 fe7b0351 Michael Hanselmann
    This runs on master, primary and secondary nodes of the instance.
2077 fe7b0351 Michael Hanselmann

2078 fe7b0351 Michael Hanselmann
    """
2079 396e1b78 Michael Hanselmann
    env = _BuildInstanceHookEnvByObject(self.instance)
2080 fe7b0351 Michael Hanselmann
    nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
2081 fe7b0351 Michael Hanselmann
          list(self.instance.secondary_nodes))
2082 fe7b0351 Michael Hanselmann
    return env, nl, nl
2083 fe7b0351 Michael Hanselmann
2084 fe7b0351 Michael Hanselmann
  def CheckPrereq(self):
2085 fe7b0351 Michael Hanselmann
    """Check prerequisites.
2086 fe7b0351 Michael Hanselmann

2087 fe7b0351 Michael Hanselmann
    This checks that the instance is in the cluster and is not running.
2088 fe7b0351 Michael Hanselmann

2089 fe7b0351 Michael Hanselmann
    """
2090 fe7b0351 Michael Hanselmann
    instance = self.cfg.GetInstanceInfo(
2091 fe7b0351 Michael Hanselmann
      self.cfg.ExpandInstanceName(self.op.instance_name))
2092 fe7b0351 Michael Hanselmann
    if instance is None:
2093 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' not known" %
2094 3ecf6786 Iustin Pop
                                 self.op.instance_name)
2095 fe7b0351 Michael Hanselmann
    if instance.disk_template == constants.DT_DISKLESS:
2096 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' has no disks" %
2097 3ecf6786 Iustin Pop
                                 self.op.instance_name)
2098 fe7b0351 Michael Hanselmann
    if instance.status != "down":
2099 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' is marked to be up" %
2100 3ecf6786 Iustin Pop
                                 self.op.instance_name)
2101 fe7b0351 Michael Hanselmann
    remote_info = rpc.call_instance_info(instance.primary_node, instance.name)
2102 fe7b0351 Michael Hanselmann
    if remote_info:
2103 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
2104 3ecf6786 Iustin Pop
                                 (self.op.instance_name,
2105 3ecf6786 Iustin Pop
                                  instance.primary_node))
2106 d0834de3 Michael Hanselmann
2107 d0834de3 Michael Hanselmann
    self.op.os_type = getattr(self.op, "os_type", None)
2108 d0834de3 Michael Hanselmann
    if self.op.os_type is not None:
2109 d0834de3 Michael Hanselmann
      # OS verification
2110 d0834de3 Michael Hanselmann
      pnode = self.cfg.GetNodeInfo(
2111 d0834de3 Michael Hanselmann
        self.cfg.ExpandNodeName(instance.primary_node))
2112 d0834de3 Michael Hanselmann
      if pnode is None:
2113 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Primary node '%s' is unknown" %
2114 3ecf6786 Iustin Pop
                                   self.op.pnode)
2115 00fe9e38 Guido Trotter
      os_obj = rpc.call_os_get(pnode.name, self.op.os_type)
2116 dfa96ded Guido Trotter
      if not os_obj:
2117 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("OS '%s' not in supported OS list for"
2118 3ecf6786 Iustin Pop
                                   " primary node"  % self.op.os_type)
2119 d0834de3 Michael Hanselmann
2120 fe7b0351 Michael Hanselmann
    self.instance = instance
2121 fe7b0351 Michael Hanselmann
2122 fe7b0351 Michael Hanselmann
  def Exec(self, feedback_fn):
2123 fe7b0351 Michael Hanselmann
    """Reinstall the instance.
2124 fe7b0351 Michael Hanselmann

2125 fe7b0351 Michael Hanselmann
    """
2126 fe7b0351 Michael Hanselmann
    inst = self.instance
2127 fe7b0351 Michael Hanselmann
2128 d0834de3 Michael Hanselmann
    if self.op.os_type is not None:
2129 d0834de3 Michael Hanselmann
      feedback_fn("Changing OS to '%s'..." % self.op.os_type)
2130 d0834de3 Michael Hanselmann
      inst.os = self.op.os_type
2131 d0834de3 Michael Hanselmann
      self.cfg.AddInstance(inst)
2132 d0834de3 Michael Hanselmann
2133 fe7b0351 Michael Hanselmann
    _StartInstanceDisks(self.cfg, inst, None)
2134 fe7b0351 Michael Hanselmann
    try:
2135 fe7b0351 Michael Hanselmann
      feedback_fn("Running the instance OS create scripts...")
2136 fe7b0351 Michael Hanselmann
      if not rpc.call_instance_os_add(inst.primary_node, inst, "sda", "sdb"):
2137 3ecf6786 Iustin Pop
        raise errors.OpExecError("Could not install OS for instance %s "
2138 3ecf6786 Iustin Pop
                                 "on node %s" %
2139 3ecf6786 Iustin Pop
                                 (inst.name, inst.primary_node))
2140 fe7b0351 Michael Hanselmann
    finally:
2141 fe7b0351 Michael Hanselmann
      _ShutdownInstanceDisks(inst, self.cfg)
2142 fe7b0351 Michael Hanselmann
2143 fe7b0351 Michael Hanselmann
2144 decd5f45 Iustin Pop
class LURenameInstance(LogicalUnit):
2145 decd5f45 Iustin Pop
  """Rename an instance.
2146 decd5f45 Iustin Pop

2147 decd5f45 Iustin Pop
  """
2148 decd5f45 Iustin Pop
  HPATH = "instance-rename"
2149 decd5f45 Iustin Pop
  HTYPE = constants.HTYPE_INSTANCE
2150 decd5f45 Iustin Pop
  _OP_REQP = ["instance_name", "new_name"]
2151 decd5f45 Iustin Pop
2152 decd5f45 Iustin Pop
  def BuildHooksEnv(self):
2153 decd5f45 Iustin Pop
    """Build hooks env.
2154 decd5f45 Iustin Pop

2155 decd5f45 Iustin Pop
    This runs on master, primary and secondary nodes of the instance.
2156 decd5f45 Iustin Pop

2157 decd5f45 Iustin Pop
    """
2158 decd5f45 Iustin Pop
    env = _BuildInstanceHookEnvByObject(self.instance)
2159 decd5f45 Iustin Pop
    env["INSTANCE_NEW_NAME"] = self.op.new_name
2160 decd5f45 Iustin Pop
    nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
2161 decd5f45 Iustin Pop
          list(self.instance.secondary_nodes))
2162 decd5f45 Iustin Pop
    return env, nl, nl
2163 decd5f45 Iustin Pop
2164 decd5f45 Iustin Pop
  def CheckPrereq(self):
2165 decd5f45 Iustin Pop
    """Check prerequisites.
2166 decd5f45 Iustin Pop

2167 decd5f45 Iustin Pop
    This checks that the instance is in the cluster and is not running.
2168 decd5f45 Iustin Pop

2169 decd5f45 Iustin Pop
    """
2170 decd5f45 Iustin Pop
    instance = self.cfg.GetInstanceInfo(
2171 decd5f45 Iustin Pop
      self.cfg.ExpandInstanceName(self.op.instance_name))
2172 decd5f45 Iustin Pop
    if instance is None:
2173 decd5f45 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' not known" %
2174 decd5f45 Iustin Pop
                                 self.op.instance_name)
2175 decd5f45 Iustin Pop
    if instance.status != "down":
2176 decd5f45 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' is marked to be up" %
2177 decd5f45 Iustin Pop
                                 self.op.instance_name)
2178 decd5f45 Iustin Pop
    remote_info = rpc.call_instance_info(instance.primary_node, instance.name)
2179 decd5f45 Iustin Pop
    if remote_info:
2180 decd5f45 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
2181 decd5f45 Iustin Pop
                                 (self.op.instance_name,
2182 decd5f45 Iustin Pop
                                  instance.primary_node))
2183 decd5f45 Iustin Pop
    self.instance = instance
2184 decd5f45 Iustin Pop
2185 decd5f45 Iustin Pop
    # new name verification
2186 89e1fc26 Iustin Pop
    name_info = utils.HostInfo(self.op.new_name)
2187 decd5f45 Iustin Pop
2188 89e1fc26 Iustin Pop
    self.op.new_name = new_name = name_info.name
2189 decd5f45 Iustin Pop
    if not getattr(self.op, "ignore_ip", False):
2190 89e1fc26 Iustin Pop
      command = ["fping", "-q", name_info.ip]
2191 decd5f45 Iustin Pop
      result = utils.RunCmd(command)
2192 decd5f45 Iustin Pop
      if not result.failed:
2193 decd5f45 Iustin Pop
        raise errors.OpPrereqError("IP %s of instance %s already in use" %
2194 89e1fc26 Iustin Pop
                                   (name_info.ip, new_name))
2195 decd5f45 Iustin Pop
2196 decd5f45 Iustin Pop
2197 decd5f45 Iustin Pop
  def Exec(self, feedback_fn):
2198 decd5f45 Iustin Pop
    """Reinstall the instance.
2199 decd5f45 Iustin Pop

2200 decd5f45 Iustin Pop
    """
2201 decd5f45 Iustin Pop
    inst = self.instance
2202 decd5f45 Iustin Pop
    old_name = inst.name
2203 decd5f45 Iustin Pop
2204 decd5f45 Iustin Pop
    self.cfg.RenameInstance(inst.name, self.op.new_name)
2205 decd5f45 Iustin Pop
2206 decd5f45 Iustin Pop
    # re-read the instance from the configuration after rename
2207 decd5f45 Iustin Pop
    inst = self.cfg.GetInstanceInfo(self.op.new_name)
2208 decd5f45 Iustin Pop
2209 decd5f45 Iustin Pop
    _StartInstanceDisks(self.cfg, inst, None)
2210 decd5f45 Iustin Pop
    try:
2211 decd5f45 Iustin Pop
      if not rpc.call_instance_run_rename(inst.primary_node, inst, old_name,
2212 decd5f45 Iustin Pop
                                          "sda", "sdb"):
2213 decd5f45 Iustin Pop
        msg = ("Could run OS rename script for instance %s\n"
2214 decd5f45 Iustin Pop
               "on node %s\n"
2215 decd5f45 Iustin Pop
               "(but the instance has been renamed in Ganeti)" %
2216 decd5f45 Iustin Pop
               (inst.name, inst.primary_node))
2217 decd5f45 Iustin Pop
        logger.Error(msg)
2218 decd5f45 Iustin Pop
    finally:
2219 decd5f45 Iustin Pop
      _ShutdownInstanceDisks(inst, self.cfg)
2220 decd5f45 Iustin Pop
2221 decd5f45 Iustin Pop
2222 a8083063 Iustin Pop
class LURemoveInstance(LogicalUnit):
2223 a8083063 Iustin Pop
  """Remove an instance.
2224 a8083063 Iustin Pop

2225 a8083063 Iustin Pop
  """
2226 a8083063 Iustin Pop
  HPATH = "instance-remove"
2227 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_INSTANCE
2228 a8083063 Iustin Pop
  _OP_REQP = ["instance_name"]
2229 a8083063 Iustin Pop
2230 a8083063 Iustin Pop
  def BuildHooksEnv(self):
2231 a8083063 Iustin Pop
    """Build hooks env.
2232 a8083063 Iustin Pop

2233 a8083063 Iustin Pop
    This runs on master, primary and secondary nodes of the instance.
2234 a8083063 Iustin Pop

2235 a8083063 Iustin Pop
    """
2236 396e1b78 Michael Hanselmann
    env = _BuildInstanceHookEnvByObject(self.instance)
2237 1d67656e Iustin Pop
    nl = [self.sstore.GetMasterNode()]
2238 a8083063 Iustin Pop
    return env, nl, nl
2239 a8083063 Iustin Pop
2240 a8083063 Iustin Pop
  def CheckPrereq(self):
2241 a8083063 Iustin Pop
    """Check prerequisites.
2242 a8083063 Iustin Pop

2243 a8083063 Iustin Pop
    This checks that the instance is in the cluster.
2244 a8083063 Iustin Pop

2245 a8083063 Iustin Pop
    """
2246 a8083063 Iustin Pop
    instance = self.cfg.GetInstanceInfo(
2247 a8083063 Iustin Pop
      self.cfg.ExpandInstanceName(self.op.instance_name))
2248 a8083063 Iustin Pop
    if instance is None:
2249 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' not known" %
2250 3ecf6786 Iustin Pop
                                 self.op.instance_name)
2251 a8083063 Iustin Pop
    self.instance = instance
2252 a8083063 Iustin Pop
2253 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
2254 a8083063 Iustin Pop
    """Remove the instance.
2255 a8083063 Iustin Pop

2256 a8083063 Iustin Pop
    """
2257 a8083063 Iustin Pop
    instance = self.instance
2258 a8083063 Iustin Pop
    logger.Info("shutting down instance %s on node %s" %
2259 a8083063 Iustin Pop
                (instance.name, instance.primary_node))
2260 a8083063 Iustin Pop
2261 a8083063 Iustin Pop
    if not rpc.call_instance_shutdown(instance.primary_node, instance):
2262 1d67656e Iustin Pop
      if self.op.ignore_failures:
2263 1d67656e Iustin Pop
        feedback_fn("Warning: can't shutdown instance")
2264 1d67656e Iustin Pop
      else:
2265 1d67656e Iustin Pop
        raise errors.OpExecError("Could not shutdown instance %s on node %s" %
2266 1d67656e Iustin Pop
                                 (instance.name, instance.primary_node))
2267 a8083063 Iustin Pop
2268 a8083063 Iustin Pop
    logger.Info("removing block devices for instance %s" % instance.name)
2269 a8083063 Iustin Pop
2270 1d67656e Iustin Pop
    if not _RemoveDisks(instance, self.cfg):
2271 1d67656e Iustin Pop
      if self.op.ignore_failures:
2272 1d67656e Iustin Pop
        feedback_fn("Warning: can't remove instance's disks")
2273 1d67656e Iustin Pop
      else:
2274 1d67656e Iustin Pop
        raise errors.OpExecError("Can't remove instance's disks")
2275 a8083063 Iustin Pop
2276 a8083063 Iustin Pop
    logger.Info("removing instance %s out of cluster config" % instance.name)
2277 a8083063 Iustin Pop
2278 a8083063 Iustin Pop
    self.cfg.RemoveInstance(instance.name)
2279 a8083063 Iustin Pop
2280 a8083063 Iustin Pop
2281 a8083063 Iustin Pop
class LUQueryInstances(NoHooksLU):
2282 a8083063 Iustin Pop
  """Logical unit for querying instances.
2283 a8083063 Iustin Pop

2284 a8083063 Iustin Pop
  """
2285 069dcc86 Iustin Pop
  _OP_REQP = ["output_fields", "names"]
2286 a8083063 Iustin Pop
2287 a8083063 Iustin Pop
  def CheckPrereq(self):
2288 a8083063 Iustin Pop
    """Check prerequisites.
2289 a8083063 Iustin Pop

2290 a8083063 Iustin Pop
    This checks that the fields required are valid output fields.
2291 a8083063 Iustin Pop

2292 a8083063 Iustin Pop
    """
2293 a8083063 Iustin Pop
    self.dynamic_fields = frozenset(["oper_state", "oper_ram"])
2294 dcb93971 Michael Hanselmann
    _CheckOutputFields(static=["name", "os", "pnode", "snodes",
2295 dcb93971 Michael Hanselmann
                               "admin_state", "admin_ram",
2296 644eeef9 Iustin Pop
                               "disk_template", "ip", "mac", "bridge",
2297 644eeef9 Iustin Pop
                               "sda_size", "sdb_size"],
2298 dcb93971 Michael Hanselmann
                       dynamic=self.dynamic_fields,
2299 dcb93971 Michael Hanselmann
                       selected=self.op.output_fields)
2300 a8083063 Iustin Pop
2301 069dcc86 Iustin Pop
    self.wanted = _GetWantedInstances(self, self.op.names)
2302 069dcc86 Iustin Pop
2303 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
2304 a8083063 Iustin Pop
    """Computes the list of nodes and their attributes.
2305 a8083063 Iustin Pop

2306 a8083063 Iustin Pop
    """
2307 069dcc86 Iustin Pop
    instance_names = self.wanted
2308 a8083063 Iustin Pop
    instance_list = [self.cfg.GetInstanceInfo(iname) for iname
2309 a8083063 Iustin Pop
                     in instance_names]
2310 a8083063 Iustin Pop
2311 a8083063 Iustin Pop
    # begin data gathering
2312 a8083063 Iustin Pop
2313 a8083063 Iustin Pop
    nodes = frozenset([inst.primary_node for inst in instance_list])
2314 a8083063 Iustin Pop
2315 a8083063 Iustin Pop
    bad_nodes = []
2316 a8083063 Iustin Pop
    if self.dynamic_fields.intersection(self.op.output_fields):
2317 a8083063 Iustin Pop
      live_data = {}
2318 a8083063 Iustin Pop
      node_data = rpc.call_all_instances_info(nodes)
2319 a8083063 Iustin Pop
      for name in nodes:
2320 a8083063 Iustin Pop
        result = node_data[name]
2321 a8083063 Iustin Pop
        if result:
2322 a8083063 Iustin Pop
          live_data.update(result)
2323 a8083063 Iustin Pop
        elif result == False:
2324 a8083063 Iustin Pop
          bad_nodes.append(name)
2325 a8083063 Iustin Pop
        # else no instance is alive
2326 a8083063 Iustin Pop
    else:
2327 a8083063 Iustin Pop
      live_data = dict([(name, {}) for name in instance_names])
2328 a8083063 Iustin Pop
2329 a8083063 Iustin Pop
    # end data gathering
2330 a8083063 Iustin Pop
2331 a8083063 Iustin Pop
    output = []
2332 a8083063 Iustin Pop
    for instance in instance_list:
2333 a8083063 Iustin Pop
      iout = []
2334 a8083063 Iustin Pop
      for field in self.op.output_fields:
2335 a8083063 Iustin Pop
        if field == "name":
2336 a8083063 Iustin Pop
          val = instance.name
2337 a8083063 Iustin Pop
        elif field == "os":
2338 a8083063 Iustin Pop
          val = instance.os
2339 a8083063 Iustin Pop
        elif field == "pnode":
2340 a8083063 Iustin Pop
          val = instance.primary_node
2341 a8083063 Iustin Pop
        elif field == "snodes":
2342 8a23d2d3 Iustin Pop
          val = list(instance.secondary_nodes)
2343 a8083063 Iustin Pop
        elif field == "admin_state":
2344 8a23d2d3 Iustin Pop
          val = (instance.status != "down")
2345 a8083063 Iustin Pop
        elif field == "oper_state":
2346 a8083063 Iustin Pop
          if instance.primary_node in bad_nodes:
2347 8a23d2d3 Iustin Pop
            val = None
2348 a8083063 Iustin Pop
          else:
2349 8a23d2d3 Iustin Pop
            val = bool(live_data.get(instance.name))
2350 a8083063 Iustin Pop
        elif field == "admin_ram":
2351 a8083063 Iustin Pop
          val = instance.memory
2352 a8083063 Iustin Pop
        elif field == "oper_ram":
2353 a8083063 Iustin Pop
          if instance.primary_node in bad_nodes:
2354 8a23d2d3 Iustin Pop
            val = None
2355 a8083063 Iustin Pop
          elif instance.name in live_data:
2356 a8083063 Iustin Pop
            val = live_data[instance.name].get("memory", "?")
2357 a8083063 Iustin Pop
          else:
2358 a8083063 Iustin Pop
            val = "-"
2359 a8083063 Iustin Pop
        elif field == "disk_template":
2360 a8083063 Iustin Pop
          val = instance.disk_template
2361 a8083063 Iustin Pop
        elif field == "ip":
2362 a8083063 Iustin Pop
          val = instance.nics[0].ip
2363 a8083063 Iustin Pop
        elif field == "bridge":
2364 a8083063 Iustin Pop
          val = instance.nics[0].bridge
2365 a8083063 Iustin Pop
        elif field == "mac":
2366 a8083063 Iustin Pop
          val = instance.nics[0].mac
2367 644eeef9 Iustin Pop
        elif field == "sda_size" or field == "sdb_size":
2368 644eeef9 Iustin Pop
          disk = instance.FindDisk(field[:3])
2369 644eeef9 Iustin Pop
          if disk is None:
2370 8a23d2d3 Iustin Pop
            val = None
2371 644eeef9 Iustin Pop
          else:
2372 644eeef9 Iustin Pop
            val = disk.size
2373 a8083063 Iustin Pop
        else:
2374 3ecf6786 Iustin Pop
          raise errors.ParameterError(field)
2375 a8083063 Iustin Pop
        iout.append(val)
2376 a8083063 Iustin Pop
      output.append(iout)
2377 a8083063 Iustin Pop
2378 a8083063 Iustin Pop
    return output
2379 a8083063 Iustin Pop
2380 a8083063 Iustin Pop
2381 a8083063 Iustin Pop
class LUFailoverInstance(LogicalUnit):
2382 a8083063 Iustin Pop
  """Failover an instance.
2383 a8083063 Iustin Pop

2384 a8083063 Iustin Pop
  """
2385 a8083063 Iustin Pop
  HPATH = "instance-failover"
2386 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_INSTANCE
2387 a8083063 Iustin Pop
  _OP_REQP = ["instance_name", "ignore_consistency"]
2388 a8083063 Iustin Pop
2389 a8083063 Iustin Pop
  def BuildHooksEnv(self):
2390 a8083063 Iustin Pop
    """Build hooks env.
2391 a8083063 Iustin Pop

2392 a8083063 Iustin Pop
    This runs on master, primary and secondary nodes of the instance.
2393 a8083063 Iustin Pop

2394 a8083063 Iustin Pop
    """
2395 a8083063 Iustin Pop
    env = {
2396 a8083063 Iustin Pop
      "IGNORE_CONSISTENCY": self.op.ignore_consistency,
2397 a8083063 Iustin Pop
      }
2398 396e1b78 Michael Hanselmann
    env.update(_BuildInstanceHookEnvByObject(self.instance))
2399 880478f8 Iustin Pop
    nl = [self.sstore.GetMasterNode()] + list(self.instance.secondary_nodes)
2400 a8083063 Iustin Pop
    return env, nl, nl
2401 a8083063 Iustin Pop
2402 a8083063 Iustin Pop
  def CheckPrereq(self):
2403 a8083063 Iustin Pop
    """Check prerequisites.
2404 a8083063 Iustin Pop

2405 a8083063 Iustin Pop
    This checks that the instance is in the cluster.
2406 a8083063 Iustin Pop

2407 a8083063 Iustin Pop
    """
2408 a8083063 Iustin Pop
    instance = self.cfg.GetInstanceInfo(
2409 a8083063 Iustin Pop
      self.cfg.ExpandInstanceName(self.op.instance_name))
2410 a8083063 Iustin Pop
    if instance is None:
2411 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' not known" %
2412 3ecf6786 Iustin Pop
                                 self.op.instance_name)
2413 a8083063 Iustin Pop
2414 a1f445d3 Iustin Pop
    if instance.disk_template not in constants.DTS_NET_MIRROR:
2415 2a710df1 Michael Hanselmann
      raise errors.OpPrereqError("Instance's disk layout is not"
2416 a1f445d3 Iustin Pop
                                 " network mirrored, cannot failover.")
2417 2a710df1 Michael Hanselmann
2418 2a710df1 Michael Hanselmann
    secondary_nodes = instance.secondary_nodes
2419 2a710df1 Michael Hanselmann
    if not secondary_nodes:
2420 2a710df1 Michael Hanselmann
      raise errors.ProgrammerError("no secondary node but using "
2421 2a710df1 Michael Hanselmann
                                   "DT_REMOTE_RAID1 template")
2422 2a710df1 Michael Hanselmann
2423 3a7c308e Guido Trotter
    # check memory requirements on the secondary node
2424 2a710df1 Michael Hanselmann
    target_node = secondary_nodes[0]
2425 3a7c308e Guido Trotter
    nodeinfo = rpc.call_node_info([target_node], self.cfg.GetVGName())
2426 3a7c308e Guido Trotter
    info = nodeinfo.get(target_node, None)
2427 3a7c308e Guido Trotter
    if not info:
2428 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Cannot get current information"
2429 3ecf6786 Iustin Pop
                                 " from node '%s'" % nodeinfo)
2430 3a7c308e Guido Trotter
    if instance.memory > info['memory_free']:
2431 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Not enough memory on target node %s."
2432 3ecf6786 Iustin Pop
                                 " %d MB available, %d MB required" %
2433 3ecf6786 Iustin Pop
                                 (target_node, info['memory_free'],
2434 3ecf6786 Iustin Pop
                                  instance.memory))
2435 3a7c308e Guido Trotter
2436 a8083063 Iustin Pop
    # check bridge existance
2437 a8083063 Iustin Pop
    brlist = [nic.bridge for nic in instance.nics]
2438 50ff9a7a Iustin Pop
    if not rpc.call_bridges_exist(target_node, brlist):
2439 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("One or more target bridges %s does not"
2440 3ecf6786 Iustin Pop
                                 " exist on destination node '%s'" %
2441 50ff9a7a Iustin Pop
                                 (brlist, target_node))
2442 a8083063 Iustin Pop
2443 a8083063 Iustin Pop
    self.instance = instance
2444 a8083063 Iustin Pop
2445 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
2446 a8083063 Iustin Pop
    """Failover an instance.
2447 a8083063 Iustin Pop

2448 a8083063 Iustin Pop
    The failover is done by shutting it down on its present node and
2449 a8083063 Iustin Pop
    starting it on the secondary.
2450 a8083063 Iustin Pop

2451 a8083063 Iustin Pop
    """
2452 a8083063 Iustin Pop
    instance = self.instance
2453 a8083063 Iustin Pop
2454 a8083063 Iustin Pop
    source_node = instance.primary_node
2455 a8083063 Iustin Pop
    target_node = instance.secondary_nodes[0]
2456 a8083063 Iustin Pop
2457 a8083063 Iustin Pop
    feedback_fn("* checking disk consistency between source and target")
2458 a8083063 Iustin Pop
    for dev in instance.disks:
2459 a8083063 Iustin Pop
      # for remote_raid1, these are md over drbd
2460 a8083063 Iustin Pop
      if not _CheckDiskConsistency(self.cfg, dev, target_node, False):
2461 a8083063 Iustin Pop
        if not self.op.ignore_consistency:
2462 3ecf6786 Iustin Pop
          raise errors.OpExecError("Disk %s is degraded on target node,"
2463 3ecf6786 Iustin Pop
                                   " aborting failover." % dev.iv_name)
2464 a8083063 Iustin Pop
2465 a8083063 Iustin Pop
    feedback_fn("* checking target node resource availability")
2466 a8083063 Iustin Pop
    nodeinfo = rpc.call_node_info([target_node], self.cfg.GetVGName())
2467 a8083063 Iustin Pop
2468 a8083063 Iustin Pop
    if not nodeinfo:
2469 3ecf6786 Iustin Pop
      raise errors.OpExecError("Could not contact target node %s." %
2470 3ecf6786 Iustin Pop
                               target_node)
2471 a8083063 Iustin Pop
2472 a8083063 Iustin Pop
    free_memory = int(nodeinfo[target_node]['memory_free'])
2473 a8083063 Iustin Pop
    memory = instance.memory
2474 a8083063 Iustin Pop
    if memory > free_memory:
2475 3ecf6786 Iustin Pop
      raise errors.OpExecError("Not enough memory to create instance %s on"
2476 3ecf6786 Iustin Pop
                               " node %s. needed %s MiB, available %s MiB" %
2477 3ecf6786 Iustin Pop
                               (instance.name, target_node, memory,
2478 3ecf6786 Iustin Pop
                                free_memory))
2479 a8083063 Iustin Pop
2480 a8083063 Iustin Pop
    feedback_fn("* shutting down instance on source node")
2481 a8083063 Iustin Pop
    logger.Info("Shutting down instance %s on node %s" %
2482 a8083063 Iustin Pop
                (instance.name, source_node))
2483 a8083063 Iustin Pop
2484 a8083063 Iustin Pop
    if not rpc.call_instance_shutdown(source_node, instance):
2485 24a40d57 Iustin Pop
      if self.op.ignore_consistency:
2486 24a40d57 Iustin Pop
        logger.Error("Could not shutdown instance %s on node %s. Proceeding"
2487 24a40d57 Iustin Pop
                     " anyway. Please make sure node %s is down"  %
2488 24a40d57 Iustin Pop
                     (instance.name, source_node, source_node))
2489 24a40d57 Iustin Pop
      else:
2490 24a40d57 Iustin Pop
        raise errors.OpExecError("Could not shutdown instance %s on node %s" %
2491 24a40d57 Iustin Pop
                                 (instance.name, source_node))
2492 a8083063 Iustin Pop
2493 a8083063 Iustin Pop
    feedback_fn("* deactivating the instance's disks on source node")
2494 a8083063 Iustin Pop
    if not _ShutdownInstanceDisks(instance, self.cfg, ignore_primary=True):
2495 3ecf6786 Iustin Pop
      raise errors.OpExecError("Can't shut down the instance's disks.")
2496 a8083063 Iustin Pop
2497 a8083063 Iustin Pop
    instance.primary_node = target_node
2498 a8083063 Iustin Pop
    # distribute new instance config to the other nodes
2499 a8083063 Iustin Pop
    self.cfg.AddInstance(instance)
2500 a8083063 Iustin Pop
2501 a8083063 Iustin Pop
    feedback_fn("* activating the instance's disks on target node")
2502 a8083063 Iustin Pop
    logger.Info("Starting instance %s on node %s" %
2503 a8083063 Iustin Pop
                (instance.name, target_node))
2504 a8083063 Iustin Pop
2505 a8083063 Iustin Pop
    disks_ok, dummy = _AssembleInstanceDisks(instance, self.cfg,
2506 a8083063 Iustin Pop
                                             ignore_secondaries=True)
2507 a8083063 Iustin Pop
    if not disks_ok:
2508 a8083063 Iustin Pop
      _ShutdownInstanceDisks(instance, self.cfg)
2509 3ecf6786 Iustin Pop
      raise errors.OpExecError("Can't activate the instance's disks")
2510 a8083063 Iustin Pop
2511 a8083063 Iustin Pop
    feedback_fn("* starting the instance on the target node")
2512 a8083063 Iustin Pop
    if not rpc.call_instance_start(target_node, instance, None):
2513 a8083063 Iustin Pop
      _ShutdownInstanceDisks(instance, self.cfg)
2514 a8083063 Iustin Pop
      raise errors.OpExecError("Could not start instance %s on node %s." %
2515 d0b3526f Michael Hanselmann
                               (instance.name, target_node))
2516 a8083063 Iustin Pop
2517 a8083063 Iustin Pop
2518 3f78eef2 Iustin Pop
def _CreateBlockDevOnPrimary(cfg, node, instance, device, info):
2519 a8083063 Iustin Pop
  """Create a tree of block devices on the primary node.
2520 a8083063 Iustin Pop

2521 a8083063 Iustin Pop
  This always creates all devices.
2522 a8083063 Iustin Pop

2523 a8083063 Iustin Pop
  """
2524 a8083063 Iustin Pop
  if device.children:
2525 a8083063 Iustin Pop
    for child in device.children:
2526 3f78eef2 Iustin Pop
      if not _CreateBlockDevOnPrimary(cfg, node, instance, child, info):
2527 a8083063 Iustin Pop
        return False
2528 a8083063 Iustin Pop
2529 a8083063 Iustin Pop
  cfg.SetDiskID(device, node)
2530 3f78eef2 Iustin Pop
  new_id = rpc.call_blockdev_create(node, device, device.size,
2531 3f78eef2 Iustin Pop
                                    instance.name, True, info)
2532 a8083063 Iustin Pop
  if not new_id:
2533 a8083063 Iustin Pop
    return False
2534 a8083063 Iustin Pop
  if device.physical_id is None:
2535 a8083063 Iustin Pop
    device.physical_id = new_id
2536 a8083063 Iustin Pop
  return True
2537 a8083063 Iustin Pop
2538 a8083063 Iustin Pop
2539 3f78eef2 Iustin Pop
def _CreateBlockDevOnSecondary(cfg, node, instance, device, force, info):
2540 a8083063 Iustin Pop
  """Create a tree of block devices on a secondary node.
2541 a8083063 Iustin Pop

2542 a8083063 Iustin Pop
  If this device type has to be created on secondaries, create it and
2543 a8083063 Iustin Pop
  all its children.
2544 a8083063 Iustin Pop

2545 a8083063 Iustin Pop
  If not, just recurse to children keeping the same 'force' value.
2546 a8083063 Iustin Pop

2547 a8083063 Iustin Pop
  """
2548 a8083063 Iustin Pop
  if device.CreateOnSecondary():
2549 a8083063 Iustin Pop
    force = True
2550 a8083063 Iustin Pop
  if device.children:
2551 a8083063 Iustin Pop
    for child in device.children:
2552 3f78eef2 Iustin Pop
      if not _CreateBlockDevOnSecondary(cfg, node, instance,
2553 3f78eef2 Iustin Pop
                                        child, force, info):
2554 a8083063 Iustin Pop
        return False
2555 a8083063 Iustin Pop
2556 a8083063 Iustin Pop
  if not force:
2557 a8083063 Iustin Pop
    return True
2558 a8083063 Iustin Pop
  cfg.SetDiskID(device, node)
2559 3f78eef2 Iustin Pop
  new_id = rpc.call_blockdev_create(node, device, device.size,
2560 3f78eef2 Iustin Pop
                                    instance.name, False, info)
2561 a8083063 Iustin Pop
  if not new_id:
2562 a8083063 Iustin Pop
    return False
2563 a8083063 Iustin Pop
  if device.physical_id is None:
2564 a8083063 Iustin Pop
    device.physical_id = new_id
2565 a8083063 Iustin Pop
  return True
2566 a8083063 Iustin Pop
2567 a8083063 Iustin Pop
2568 923b1523 Iustin Pop
def _GenerateUniqueNames(cfg, exts):
2569 923b1523 Iustin Pop
  """Generate a suitable LV name.
2570 923b1523 Iustin Pop

2571 923b1523 Iustin Pop
  This will generate a logical volume name for the given instance.
2572 923b1523 Iustin Pop

2573 923b1523 Iustin Pop
  """
2574 923b1523 Iustin Pop
  results = []
2575 923b1523 Iustin Pop
  for val in exts:
2576 923b1523 Iustin Pop
    new_id = cfg.GenerateUniqueID()
2577 923b1523 Iustin Pop
    results.append("%s%s" % (new_id, val))
2578 923b1523 Iustin Pop
  return results
2579 923b1523 Iustin Pop
2580 923b1523 Iustin Pop
2581 923b1523 Iustin Pop
def _GenerateMDDRBDBranch(cfg, primary, secondary, size, names):
2582 a8083063 Iustin Pop
  """Generate a drbd device complete with its children.
2583 a8083063 Iustin Pop

2584 a8083063 Iustin Pop
  """
2585 a8083063 Iustin Pop
  port = cfg.AllocatePort()
2586 923b1523 Iustin Pop
  vgname = cfg.GetVGName()
2587 fe96220b Iustin Pop
  dev_data = objects.Disk(dev_type=constants.LD_LV, size=size,
2588 923b1523 Iustin Pop
                          logical_id=(vgname, names[0]))
2589 fe96220b Iustin Pop
  dev_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
2590 923b1523 Iustin Pop
                          logical_id=(vgname, names[1]))
2591 fe96220b Iustin Pop
  drbd_dev = objects.Disk(dev_type=constants.LD_DRBD7, size=size,
2592 a8083063 Iustin Pop
                          logical_id = (primary, secondary, port),
2593 a8083063 Iustin Pop
                          children = [dev_data, dev_meta])
2594 a8083063 Iustin Pop
  return drbd_dev
2595 a8083063 Iustin Pop
2596 a8083063 Iustin Pop
2597 a1f445d3 Iustin Pop
def _GenerateDRBD8Branch(cfg, primary, secondary, size, names, iv_name):
2598 a1f445d3 Iustin Pop
  """Generate a drbd8 device complete with its children.
2599 a1f445d3 Iustin Pop

2600 a1f445d3 Iustin Pop
  """
2601 a1f445d3 Iustin Pop
  port = cfg.AllocatePort()
2602 a1f445d3 Iustin Pop
  vgname = cfg.GetVGName()
2603 a1f445d3 Iustin Pop
  dev_data = objects.Disk(dev_type=constants.LD_LV, size=size,
2604 a1f445d3 Iustin Pop
                          logical_id=(vgname, names[0]))
2605 a1f445d3 Iustin Pop
  dev_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
2606 a1f445d3 Iustin Pop
                          logical_id=(vgname, names[1]))
2607 a1f445d3 Iustin Pop
  drbd_dev = objects.Disk(dev_type=constants.LD_DRBD8, size=size,
2608 a1f445d3 Iustin Pop
                          logical_id = (primary, secondary, port),
2609 a1f445d3 Iustin Pop
                          children = [dev_data, dev_meta],
2610 a1f445d3 Iustin Pop
                          iv_name=iv_name)
2611 a1f445d3 Iustin Pop
  return drbd_dev
2612 a1f445d3 Iustin Pop
2613 923b1523 Iustin Pop
def _GenerateDiskTemplate(cfg, template_name,
2614 a8083063 Iustin Pop
                          instance_name, primary_node,
2615 a8083063 Iustin Pop
                          secondary_nodes, disk_sz, swap_sz):
2616 a8083063 Iustin Pop
  """Generate the entire disk layout for a given template type.
2617 a8083063 Iustin Pop

2618 a8083063 Iustin Pop
  """
2619 a8083063 Iustin Pop
  #TODO: compute space requirements
2620 a8083063 Iustin Pop
2621 923b1523 Iustin Pop
  vgname = cfg.GetVGName()
2622 a8083063 Iustin Pop
  if template_name == "diskless":
2623 a8083063 Iustin Pop
    disks = []
2624 a8083063 Iustin Pop
  elif template_name == "plain":
2625 a8083063 Iustin Pop
    if len(secondary_nodes) != 0:
2626 a8083063 Iustin Pop
      raise errors.ProgrammerError("Wrong template configuration")
2627 923b1523 Iustin Pop
2628 923b1523 Iustin Pop
    names = _GenerateUniqueNames(cfg, [".sda", ".sdb"])
2629 fe96220b Iustin Pop
    sda_dev = objects.Disk(dev_type=constants.LD_LV, size=disk_sz,
2630 923b1523 Iustin Pop
                           logical_id=(vgname, names[0]),
2631 a8083063 Iustin Pop
                           iv_name = "sda")
2632 fe96220b Iustin Pop
    sdb_dev = objects.Disk(dev_type=constants.LD_LV, size=swap_sz,
2633 923b1523 Iustin Pop
                           logical_id=(vgname, names[1]),
2634 a8083063 Iustin Pop
                           iv_name = "sdb")
2635 a8083063 Iustin Pop
    disks = [sda_dev, sdb_dev]
2636 a8083063 Iustin Pop
  elif template_name == "local_raid1":
2637 a8083063 Iustin Pop
    if len(secondary_nodes) != 0:
2638 a8083063 Iustin Pop
      raise errors.ProgrammerError("Wrong template configuration")
2639 923b1523 Iustin Pop
2640 923b1523 Iustin Pop
2641 923b1523 Iustin Pop
    names = _GenerateUniqueNames(cfg, [".sda_m1", ".sda_m2",
2642 923b1523 Iustin Pop
                                       ".sdb_m1", ".sdb_m2"])
2643 fe96220b Iustin Pop
    sda_dev_m1 = objects.Disk(dev_type=constants.LD_LV, size=disk_sz,
2644 923b1523 Iustin Pop
                              logical_id=(vgname, names[0]))
2645 fe96220b Iustin Pop
    sda_dev_m2 = objects.Disk(dev_type=constants.LD_LV, size=disk_sz,
2646 923b1523 Iustin Pop
                              logical_id=(vgname, names[1]))
2647 fe96220b Iustin Pop
    md_sda_dev = objects.Disk(dev_type=constants.LD_MD_R1, iv_name = "sda",
2648 a8083063 Iustin Pop
                              size=disk_sz,
2649 a8083063 Iustin Pop
                              children = [sda_dev_m1, sda_dev_m2])
2650 fe96220b Iustin Pop
    sdb_dev_m1 = objects.Disk(dev_type=constants.LD_LV, size=swap_sz,
2651 923b1523 Iustin Pop
                              logical_id=(vgname, names[2]))
2652 fe96220b Iustin Pop
    sdb_dev_m2 = objects.Disk(dev_type=constants.LD_LV, size=swap_sz,
2653 923b1523 Iustin Pop
                              logical_id=(vgname, names[3]))
2654 fe96220b Iustin Pop
    md_sdb_dev = objects.Disk(dev_type=constants.LD_MD_R1, iv_name = "sdb",
2655 a8083063 Iustin Pop
                              size=swap_sz,
2656 a8083063 Iustin Pop
                              children = [sdb_dev_m1, sdb_dev_m2])
2657 a8083063 Iustin Pop
    disks = [md_sda_dev, md_sdb_dev]
2658 2a710df1 Michael Hanselmann
  elif template_name == constants.DT_REMOTE_RAID1:
2659 a8083063 Iustin Pop
    if len(secondary_nodes) != 1:
2660 a8083063 Iustin Pop
      raise errors.ProgrammerError("Wrong template configuration")
2661 a8083063 Iustin Pop
    remote_node = secondary_nodes[0]
2662 923b1523 Iustin Pop
    names = _GenerateUniqueNames(cfg, [".sda_data", ".sda_meta",
2663 923b1523 Iustin Pop
                                       ".sdb_data", ".sdb_meta"])
2664 923b1523 Iustin Pop
    drbd_sda_dev = _GenerateMDDRBDBranch(cfg, primary_node, remote_node,
2665 923b1523 Iustin Pop
                                         disk_sz, names[0:2])
2666 fe96220b Iustin Pop
    md_sda_dev = objects.Disk(dev_type=constants.LD_MD_R1, iv_name="sda",
2667 a8083063 Iustin Pop
                              children = [drbd_sda_dev], size=disk_sz)
2668 923b1523 Iustin Pop
    drbd_sdb_dev = _GenerateMDDRBDBranch(cfg, primary_node, remote_node,
2669 923b1523 Iustin Pop
                                         swap_sz, names[2:4])
2670 fe96220b Iustin Pop
    md_sdb_dev = objects.Disk(dev_type=constants.LD_MD_R1, iv_name="sdb",
2671 a8083063 Iustin Pop
                              children = [drbd_sdb_dev], size=swap_sz)
2672 a8083063 Iustin Pop
    disks = [md_sda_dev, md_sdb_dev]
2673 a1f445d3 Iustin Pop
  elif template_name == constants.DT_DRBD8:
2674 a1f445d3 Iustin Pop
    if len(secondary_nodes) != 1:
2675 a1f445d3 Iustin Pop
      raise errors.ProgrammerError("Wrong template configuration")
2676 a1f445d3 Iustin Pop
    remote_node = secondary_nodes[0]
2677 a1f445d3 Iustin Pop
    names = _GenerateUniqueNames(cfg, [".sda_data", ".sda_meta",
2678 a1f445d3 Iustin Pop
                                       ".sdb_data", ".sdb_meta"])
2679 a1f445d3 Iustin Pop
    drbd_sda_dev = _GenerateDRBD8Branch(cfg, primary_node, remote_node,
2680 a1f445d3 Iustin Pop
                                         disk_sz, names[0:2], "sda")
2681 a1f445d3 Iustin Pop
    drbd_sdb_dev = _GenerateDRBD8Branch(cfg, primary_node, remote_node,
2682 a1f445d3 Iustin Pop
                                         swap_sz, names[2:4], "sdb")
2683 a1f445d3 Iustin Pop
    disks = [drbd_sda_dev, drbd_sdb_dev]
2684 a8083063 Iustin Pop
  else:
2685 a8083063 Iustin Pop
    raise errors.ProgrammerError("Invalid disk template '%s'" % template_name)
2686 a8083063 Iustin Pop
  return disks
2687 a8083063 Iustin Pop
2688 a8083063 Iustin Pop
2689 a0c3fea1 Michael Hanselmann
def _GetInstanceInfoText(instance):
2690 3ecf6786 Iustin Pop
  """Compute that text that should be added to the disk's metadata.
2691 3ecf6786 Iustin Pop

2692 3ecf6786 Iustin Pop
  """
2693 a0c3fea1 Michael Hanselmann
  return "originstname+%s" % instance.name
2694 a0c3fea1 Michael Hanselmann
2695 a0c3fea1 Michael Hanselmann
2696 a8083063 Iustin Pop
def _CreateDisks(cfg, instance):
2697 a8083063 Iustin Pop
  """Create all disks for an instance.
2698 a8083063 Iustin Pop

2699 a8083063 Iustin Pop
  This abstracts away some work from AddInstance.
2700 a8083063 Iustin Pop

2701 a8083063 Iustin Pop
  Args:
2702 a8083063 Iustin Pop
    instance: the instance object
2703 a8083063 Iustin Pop

2704 a8083063 Iustin Pop
  Returns:
2705 a8083063 Iustin Pop
    True or False showing the success of the creation process
2706 a8083063 Iustin Pop

2707 a8083063 Iustin Pop
  """
2708 a0c3fea1 Michael Hanselmann
  info = _GetInstanceInfoText(instance)
2709 a0c3fea1 Michael Hanselmann
2710 a8083063 Iustin Pop
  for device in instance.disks:
2711 a8083063 Iustin Pop
    logger.Info("creating volume %s for instance %s" %
2712 a8083063 Iustin Pop
              (device.iv_name, instance.name))
2713 a8083063 Iustin Pop
    #HARDCODE
2714 a8083063 Iustin Pop
    for secondary_node in instance.secondary_nodes:
2715 3f78eef2 Iustin Pop
      if not _CreateBlockDevOnSecondary(cfg, secondary_node, instance,
2716 3f78eef2 Iustin Pop
                                        device, False, info):
2717 a8083063 Iustin Pop
        logger.Error("failed to create volume %s (%s) on secondary node %s!" %
2718 a8083063 Iustin Pop
                     (device.iv_name, device, secondary_node))
2719 a8083063 Iustin Pop
        return False
2720 a8083063 Iustin Pop
    #HARDCODE
2721 3f78eef2 Iustin Pop
    if not _CreateBlockDevOnPrimary(cfg, instance.primary_node,
2722 3f78eef2 Iustin Pop
                                    instance, device, info):
2723 a8083063 Iustin Pop
      logger.Error("failed to create volume %s on primary!" %
2724 a8083063 Iustin Pop
                   device.iv_name)
2725 a8083063 Iustin Pop
      return False
2726 a8083063 Iustin Pop
  return True
2727 a8083063 Iustin Pop
2728 a8083063 Iustin Pop
2729 a8083063 Iustin Pop
def _RemoveDisks(instance, cfg):
2730 a8083063 Iustin Pop
  """Remove all disks for an instance.
2731 a8083063 Iustin Pop

2732 a8083063 Iustin Pop
  This abstracts away some work from `AddInstance()` and
2733 a8083063 Iustin Pop
  `RemoveInstance()`. Note that in case some of the devices couldn't
2734 1d67656e Iustin Pop
  be removed, the removal will continue with the other ones (compare
2735 a8083063 Iustin Pop
  with `_CreateDisks()`).
2736 a8083063 Iustin Pop

2737 a8083063 Iustin Pop
  Args:
2738 a8083063 Iustin Pop
    instance: the instance object
2739 a8083063 Iustin Pop

2740 a8083063 Iustin Pop
  Returns:
2741 a8083063 Iustin Pop
    True or False showing the success of the removal proces
2742 a8083063 Iustin Pop

2743 a8083063 Iustin Pop
  """
2744 a8083063 Iustin Pop
  logger.Info("removing block devices for instance %s" % instance.name)
2745 a8083063 Iustin Pop
2746 a8083063 Iustin Pop
  result = True
2747 a8083063 Iustin Pop
  for device in instance.disks:
2748 a8083063 Iustin Pop
    for node, disk in device.ComputeNodeTree(instance.primary_node):
2749 a8083063 Iustin Pop
      cfg.SetDiskID(disk, node)
2750 a8083063 Iustin Pop
      if not rpc.call_blockdev_remove(node, disk):
2751 a8083063 Iustin Pop
        logger.Error("could not remove block device %s on node %s,"
2752 a8083063 Iustin Pop
                     " continuing anyway" %
2753 a8083063 Iustin Pop
                     (device.iv_name, node))
2754 a8083063 Iustin Pop
        result = False
2755 a8083063 Iustin Pop
  return result
2756 a8083063 Iustin Pop
2757 a8083063 Iustin Pop
2758 a8083063 Iustin Pop
class LUCreateInstance(LogicalUnit):
2759 a8083063 Iustin Pop
  """Create an instance.
2760 a8083063 Iustin Pop

2761 a8083063 Iustin Pop
  """
2762 a8083063 Iustin Pop
  HPATH = "instance-add"
2763 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_INSTANCE
2764 a8083063 Iustin Pop
  _OP_REQP = ["instance_name", "mem_size", "disk_size", "pnode",
2765 a8083063 Iustin Pop
              "disk_template", "swap_size", "mode", "start", "vcpus",
2766 bdd55f71 Iustin Pop
              "wait_for_sync", "ip_check"]
2767 a8083063 Iustin Pop
2768 a8083063 Iustin Pop
  def BuildHooksEnv(self):
2769 a8083063 Iustin Pop
    """Build hooks env.
2770 a8083063 Iustin Pop

2771 a8083063 Iustin Pop
    This runs on master, primary and secondary nodes of the instance.
2772 a8083063 Iustin Pop

2773 a8083063 Iustin Pop
    """
2774 a8083063 Iustin Pop
    env = {
2775 396e1b78 Michael Hanselmann
      "INSTANCE_DISK_TEMPLATE": self.op.disk_template,
2776 396e1b78 Michael Hanselmann
      "INSTANCE_DISK_SIZE": self.op.disk_size,
2777 396e1b78 Michael Hanselmann
      "INSTANCE_SWAP_SIZE": self.op.swap_size,
2778 a8083063 Iustin Pop
      "INSTANCE_ADD_MODE": self.op.mode,
2779 a8083063 Iustin Pop
      }
2780 a8083063 Iustin Pop
    if self.op.mode == constants.INSTANCE_IMPORT:
2781 396e1b78 Michael Hanselmann
      env["INSTANCE_SRC_NODE"] = self.op.src_node
2782 396e1b78 Michael Hanselmann
      env["INSTANCE_SRC_PATH"] = self.op.src_path
2783 396e1b78 Michael Hanselmann
      env["INSTANCE_SRC_IMAGE"] = self.src_image
2784 396e1b78 Michael Hanselmann
2785 396e1b78 Michael Hanselmann
    env.update(_BuildInstanceHookEnv(name=self.op.instance_name,
2786 396e1b78 Michael Hanselmann
      primary_node=self.op.pnode,
2787 396e1b78 Michael Hanselmann
      secondary_nodes=self.secondaries,
2788 396e1b78 Michael Hanselmann
      status=self.instance_status,
2789 ecb215b5 Michael Hanselmann
      os_type=self.op.os_type,
2790 396e1b78 Michael Hanselmann
      memory=self.op.mem_size,
2791 396e1b78 Michael Hanselmann
      vcpus=self.op.vcpus,
2792 396e1b78 Michael Hanselmann
      nics=[(self.inst_ip, self.op.bridge)],
2793 396e1b78 Michael Hanselmann
    ))
2794 a8083063 Iustin Pop
2795 880478f8 Iustin Pop
    nl = ([self.sstore.GetMasterNode(), self.op.pnode] +
2796 a8083063 Iustin Pop
          self.secondaries)
2797 a8083063 Iustin Pop
    return env, nl, nl
2798 a8083063 Iustin Pop
2799 a8083063 Iustin Pop
2800 a8083063 Iustin Pop
  def CheckPrereq(self):
2801 a8083063 Iustin Pop
    """Check prerequisites.
2802 a8083063 Iustin Pop

2803 a8083063 Iustin Pop
    """
2804 a8083063 Iustin Pop
    if self.op.mode not in (constants.INSTANCE_CREATE,
2805 a8083063 Iustin Pop
                            constants.INSTANCE_IMPORT):
2806 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Invalid instance creation mode '%s'" %
2807 3ecf6786 Iustin Pop
                                 self.op.mode)
2808 a8083063 Iustin Pop
2809 a8083063 Iustin Pop
    if self.op.mode == constants.INSTANCE_IMPORT:
2810 a8083063 Iustin Pop
      src_node = getattr(self.op, "src_node", None)
2811 a8083063 Iustin Pop
      src_path = getattr(self.op, "src_path", None)
2812 a8083063 Iustin Pop
      if src_node is None or src_path is None:
2813 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Importing an instance requires source"
2814 3ecf6786 Iustin Pop
                                   " node and path options")
2815 a8083063 Iustin Pop
      src_node_full = self.cfg.ExpandNodeName(src_node)
2816 a8083063 Iustin Pop
      if src_node_full is None:
2817 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Unknown source node '%s'" % src_node)
2818 a8083063 Iustin Pop
      self.op.src_node = src_node = src_node_full
2819 a8083063 Iustin Pop
2820 a8083063 Iustin Pop
      if not os.path.isabs(src_path):
2821 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("The source path must be absolute")
2822 a8083063 Iustin Pop
2823 a8083063 Iustin Pop
      export_info = rpc.call_export_info(src_node, src_path)
2824 a8083063 Iustin Pop
2825 a8083063 Iustin Pop
      if not export_info:
2826 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("No export found in dir %s" % src_path)
2827 a8083063 Iustin Pop
2828 a8083063 Iustin Pop
      if not export_info.has_section(constants.INISECT_EXP):
2829 3ecf6786 Iustin Pop
        raise errors.ProgrammerError("Corrupted export config")
2830 a8083063 Iustin Pop
2831 a8083063 Iustin Pop
      ei_version = export_info.get(constants.INISECT_EXP, 'version')
2832 a8083063 Iustin Pop
      if (int(ei_version) != constants.EXPORT_VERSION):
2833 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Wrong export version %s (wanted %d)" %
2834 3ecf6786 Iustin Pop
                                   (ei_version, constants.EXPORT_VERSION))
2835 a8083063 Iustin Pop
2836 a8083063 Iustin Pop
      if int(export_info.get(constants.INISECT_INS, 'disk_count')) > 1:
2837 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Can't import instance with more than"
2838 3ecf6786 Iustin Pop
                                   " one data disk")
2839 a8083063 Iustin Pop
2840 a8083063 Iustin Pop
      # FIXME: are the old os-es, disk sizes, etc. useful?
2841 a8083063 Iustin Pop
      self.op.os_type = export_info.get(constants.INISECT_EXP, 'os')
2842 a8083063 Iustin Pop
      diskimage = os.path.join(src_path, export_info.get(constants.INISECT_INS,
2843 a8083063 Iustin Pop
                                                         'disk0_dump'))
2844 a8083063 Iustin Pop
      self.src_image = diskimage
2845 a8083063 Iustin Pop
    else: # INSTANCE_CREATE
2846 a8083063 Iustin Pop
      if getattr(self.op, "os_type", None) is None:
2847 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("No guest OS specified")
2848 a8083063 Iustin Pop
2849 a8083063 Iustin Pop
    # check primary node
2850 a8083063 Iustin Pop
    pnode = self.cfg.GetNodeInfo(self.cfg.ExpandNodeName(self.op.pnode))
2851 a8083063 Iustin Pop
    if pnode is None:
2852 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Primary node '%s' is unknown" %
2853 3ecf6786 Iustin Pop
                                 self.op.pnode)
2854 a8083063 Iustin Pop
    self.op.pnode = pnode.name
2855 a8083063 Iustin Pop
    self.pnode = pnode
2856 a8083063 Iustin Pop
    self.secondaries = []
2857 a8083063 Iustin Pop
    # disk template and mirror node verification
2858 a8083063 Iustin Pop
    if self.op.disk_template not in constants.DISK_TEMPLATES:
2859 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Invalid disk template name")
2860 a8083063 Iustin Pop
2861 a1f445d3 Iustin Pop
    if self.op.disk_template in constants.DTS_NET_MIRROR:
2862 a8083063 Iustin Pop
      if getattr(self.op, "snode", None) is None:
2863 a1f445d3 Iustin Pop
        raise errors.OpPrereqError("The networked disk templates need"
2864 3ecf6786 Iustin Pop
                                   " a mirror node")
2865 a8083063 Iustin Pop
2866 a8083063 Iustin Pop
      snode_name = self.cfg.ExpandNodeName(self.op.snode)
2867 a8083063 Iustin Pop
      if snode_name is None:
2868 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Unknown secondary node '%s'" %
2869 3ecf6786 Iustin Pop
                                   self.op.snode)
2870 a8083063 Iustin Pop
      elif snode_name == pnode.name:
2871 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("The secondary node cannot be"
2872 3ecf6786 Iustin Pop
                                   " the primary node.")
2873 a8083063 Iustin Pop
      self.secondaries.append(snode_name)
2874 a8083063 Iustin Pop
2875 ed1ebc60 Guido Trotter
    # Check lv size requirements
2876 ed1ebc60 Guido Trotter
    nodenames = [pnode.name] + self.secondaries
2877 ed1ebc60 Guido Trotter
    nodeinfo = rpc.call_node_info(nodenames, self.cfg.GetVGName())
2878 ed1ebc60 Guido Trotter
2879 ed1ebc60 Guido Trotter
    # Required free disk space as a function of disk and swap space
2880 ed1ebc60 Guido Trotter
    req_size_dict = {
2881 ed1ebc60 Guido Trotter
      constants.DT_DISKLESS: 0,
2882 ed1ebc60 Guido Trotter
      constants.DT_PLAIN: self.op.disk_size + self.op.swap_size,
2883 ed1ebc60 Guido Trotter
      constants.DT_LOCAL_RAID1: (self.op.disk_size + self.op.swap_size) * 2,
2884 ed1ebc60 Guido Trotter
      # 256 MB are added for drbd metadata, 128MB for each drbd device
2885 ed1ebc60 Guido Trotter
      constants.DT_REMOTE_RAID1: self.op.disk_size + self.op.swap_size + 256,
2886 a1f445d3 Iustin Pop
      constants.DT_DRBD8: self.op.disk_size + self.op.swap_size + 256,
2887 ed1ebc60 Guido Trotter
    }
2888 ed1ebc60 Guido Trotter
2889 ed1ebc60 Guido Trotter
    if self.op.disk_template not in req_size_dict:
2890 3ecf6786 Iustin Pop
      raise errors.ProgrammerError("Disk template '%s' size requirement"
2891 3ecf6786 Iustin Pop
                                   " is unknown" %  self.op.disk_template)
2892 ed1ebc60 Guido Trotter
2893 ed1ebc60 Guido Trotter
    req_size = req_size_dict[self.op.disk_template]
2894 ed1ebc60 Guido Trotter
2895 ed1ebc60 Guido Trotter
    for node in nodenames:
2896 ed1ebc60 Guido Trotter
      info = nodeinfo.get(node, None)
2897 ed1ebc60 Guido Trotter
      if not info:
2898 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Cannot get current information"
2899 3ecf6786 Iustin Pop
                                   " from node '%s'" % nodeinfo)
2900 ed1ebc60 Guido Trotter
      if req_size > info['vg_free']:
2901 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Not enough disk space on target node %s."
2902 3ecf6786 Iustin Pop
                                   " %d MB available, %d MB required" %
2903 3ecf6786 Iustin Pop
                                   (node, info['vg_free'], req_size))
2904 ed1ebc60 Guido Trotter
2905 a8083063 Iustin Pop
    # os verification
2906 00fe9e38 Guido Trotter
    os_obj = rpc.call_os_get(pnode.name, self.op.os_type)
2907 dfa96ded Guido Trotter
    if not os_obj:
2908 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("OS '%s' not in supported os list for"
2909 3ecf6786 Iustin Pop
                                 " primary node"  % self.op.os_type)
2910 a8083063 Iustin Pop
2911 a8083063 Iustin Pop
    # instance verification
2912 89e1fc26 Iustin Pop
    hostname1 = utils.HostInfo(self.op.instance_name)
2913 a8083063 Iustin Pop
2914 bcf043c9 Iustin Pop
    self.op.instance_name = instance_name = hostname1.name
2915 a8083063 Iustin Pop
    instance_list = self.cfg.GetInstanceList()
2916 a8083063 Iustin Pop
    if instance_name in instance_list:
2917 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
2918 3ecf6786 Iustin Pop
                                 instance_name)
2919 a8083063 Iustin Pop
2920 a8083063 Iustin Pop
    ip = getattr(self.op, "ip", None)
2921 a8083063 Iustin Pop
    if ip is None or ip.lower() == "none":
2922 a8083063 Iustin Pop
      inst_ip = None
2923 a8083063 Iustin Pop
    elif ip.lower() == "auto":
2924 bcf043c9 Iustin Pop
      inst_ip = hostname1.ip
2925 a8083063 Iustin Pop
    else:
2926 a8083063 Iustin Pop
      if not utils.IsValidIP(ip):
2927 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("given IP address '%s' doesn't look"
2928 3ecf6786 Iustin Pop
                                   " like a valid IP" % ip)
2929 a8083063 Iustin Pop
      inst_ip = ip
2930 a8083063 Iustin Pop
    self.inst_ip = inst_ip
2931 a8083063 Iustin Pop
2932 bdd55f71 Iustin Pop
    if self.op.start and not self.op.ip_check:
2933 bdd55f71 Iustin Pop
      raise errors.OpPrereqError("Cannot ignore IP address conflicts when"
2934 bdd55f71 Iustin Pop
                                 " adding an instance in start mode")
2935 bdd55f71 Iustin Pop
2936 bdd55f71 Iustin Pop
    if self.op.ip_check:
2937 16abfbc2 Alexander Schreiber
      if utils.TcpPing(utils.HostInfo().name, hostname1.ip,
2938 16abfbc2 Alexander Schreiber
                       constants.DEFAULT_NODED_PORT):
2939 16abfbc2 Alexander Schreiber
        raise errors.OpPrereqError("IP %s of instance %s already in use" %
2940 16abfbc2 Alexander Schreiber
                                   (hostname1.ip, instance_name))
2941 a8083063 Iustin Pop
2942 a8083063 Iustin Pop
    # bridge verification
2943 a8083063 Iustin Pop
    bridge = getattr(self.op, "bridge", None)
2944 a8083063 Iustin Pop
    if bridge is None:
2945 a8083063 Iustin Pop
      self.op.bridge = self.cfg.GetDefBridge()
2946 a8083063 Iustin Pop
    else:
2947 a8083063 Iustin Pop
      self.op.bridge = bridge
2948 a8083063 Iustin Pop
2949 a8083063 Iustin Pop
    if not rpc.call_bridges_exist(self.pnode.name, [self.op.bridge]):
2950 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("target bridge '%s' does not exist on"
2951 3ecf6786 Iustin Pop
                                 " destination node '%s'" %
2952 3ecf6786 Iustin Pop
                                 (self.op.bridge, pnode.name))
2953 a8083063 Iustin Pop
2954 a8083063 Iustin Pop
    if self.op.start:
2955 a8083063 Iustin Pop
      self.instance_status = 'up'
2956 a8083063 Iustin Pop
    else:
2957 a8083063 Iustin Pop
      self.instance_status = 'down'
2958 a8083063 Iustin Pop
2959 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
2960 a8083063 Iustin Pop
    """Create and add the instance to the cluster.
2961 a8083063 Iustin Pop

2962 a8083063 Iustin Pop
    """
2963 a8083063 Iustin Pop
    instance = self.op.instance_name
2964 a8083063 Iustin Pop
    pnode_name = self.pnode.name
2965 a8083063 Iustin Pop
2966 a8083063 Iustin Pop
    nic = objects.NIC(bridge=self.op.bridge, mac=self.cfg.GenerateMAC())
2967 a8083063 Iustin Pop
    if self.inst_ip is not None:
2968 a8083063 Iustin Pop
      nic.ip = self.inst_ip
2969 a8083063 Iustin Pop
2970 923b1523 Iustin Pop
    disks = _GenerateDiskTemplate(self.cfg,
2971 a8083063 Iustin Pop
                                  self.op.disk_template,
2972 a8083063 Iustin Pop
                                  instance, pnode_name,
2973 a8083063 Iustin Pop
                                  self.secondaries, self.op.disk_size,
2974 a8083063 Iustin Pop
                                  self.op.swap_size)
2975 a8083063 Iustin Pop
2976 a8083063 Iustin Pop
    iobj = objects.Instance(name=instance, os=self.op.os_type,
2977 a8083063 Iustin Pop
                            primary_node=pnode_name,
2978 a8083063 Iustin Pop
                            memory=self.op.mem_size,
2979 a8083063 Iustin Pop
                            vcpus=self.op.vcpus,
2980 a8083063 Iustin Pop
                            nics=[nic], disks=disks,
2981 a8083063 Iustin Pop
                            disk_template=self.op.disk_template,
2982 a8083063 Iustin Pop
                            status=self.instance_status,
2983 a8083063 Iustin Pop
                            )
2984 a8083063 Iustin Pop
2985 a8083063 Iustin Pop
    feedback_fn("* creating instance disks...")
2986 a8083063 Iustin Pop
    if not _CreateDisks(self.cfg, iobj):
2987 a8083063 Iustin Pop
      _RemoveDisks(iobj, self.cfg)
2988 3ecf6786 Iustin Pop
      raise errors.OpExecError("Device creation failed, reverting...")
2989 a8083063 Iustin Pop
2990 a8083063 Iustin Pop
    feedback_fn("adding instance %s to cluster config" % instance)
2991 a8083063 Iustin Pop
2992 a8083063 Iustin Pop
    self.cfg.AddInstance(iobj)
2993 a8083063 Iustin Pop
2994 a8083063 Iustin Pop
    if self.op.wait_for_sync:
2995 5bfac263 Iustin Pop
      disk_abort = not _WaitForSync(self.cfg, iobj, self.proc)
2996 a1f445d3 Iustin Pop
    elif iobj.disk_template in constants.DTS_NET_MIRROR:
2997 a8083063 Iustin Pop
      # make sure the disks are not degraded (still sync-ing is ok)
2998 a8083063 Iustin Pop
      time.sleep(15)
2999 a8083063 Iustin Pop
      feedback_fn("* checking mirrors status")
3000 5bfac263 Iustin Pop
      disk_abort = not _WaitForSync(self.cfg, iobj, self.proc, oneshot=True)
3001 a8083063 Iustin Pop
    else:
3002 a8083063 Iustin Pop
      disk_abort = False
3003 a8083063 Iustin Pop
3004 a8083063 Iustin Pop
    if disk_abort:
3005 a8083063 Iustin Pop
      _RemoveDisks(iobj, self.cfg)
3006 a8083063 Iustin Pop
      self.cfg.RemoveInstance(iobj.name)
3007 3ecf6786 Iustin Pop
      raise errors.OpExecError("There are some degraded disks for"
3008 3ecf6786 Iustin Pop
                               " this instance")
3009 a8083063 Iustin Pop
3010 a8083063 Iustin Pop
    feedback_fn("creating os for instance %s on node %s" %
3011 a8083063 Iustin Pop
                (instance, pnode_name))
3012 a8083063 Iustin Pop
3013 a8083063 Iustin Pop
    if iobj.disk_template != constants.DT_DISKLESS:
3014 a8083063 Iustin Pop
      if self.op.mode == constants.INSTANCE_CREATE:
3015 a8083063 Iustin Pop
        feedback_fn("* running the instance OS create scripts...")
3016 a8083063 Iustin Pop
        if not rpc.call_instance_os_add(pnode_name, iobj, "sda", "sdb"):
3017 3ecf6786 Iustin Pop
          raise errors.OpExecError("could not add os for instance %s"
3018 3ecf6786 Iustin Pop
                                   " on node %s" %
3019 3ecf6786 Iustin Pop
                                   (instance, pnode_name))
3020 a8083063 Iustin Pop
3021 a8083063 Iustin Pop
      elif self.op.mode == constants.INSTANCE_IMPORT:
3022 a8083063 Iustin Pop
        feedback_fn("* running the instance OS import scripts...")
3023 a8083063 Iustin Pop
        src_node = self.op.src_node
3024 a8083063 Iustin Pop
        src_image = self.src_image
3025 a8083063 Iustin Pop
        if not rpc.call_instance_os_import(pnode_name, iobj, "sda", "sdb",
3026 a8083063 Iustin Pop
                                                src_node, src_image):
3027 3ecf6786 Iustin Pop
          raise errors.OpExecError("Could not import os for instance"
3028 3ecf6786 Iustin Pop
                                   " %s on node %s" %
3029 3ecf6786 Iustin Pop
                                   (instance, pnode_name))
3030 a8083063 Iustin Pop
      else:
3031 a8083063 Iustin Pop
        # also checked in the prereq part
3032 3ecf6786 Iustin Pop
        raise errors.ProgrammerError("Unknown OS initialization mode '%s'"
3033 3ecf6786 Iustin Pop
                                     % self.op.mode)
3034 a8083063 Iustin Pop
3035 a8083063 Iustin Pop
    if self.op.start:
3036 a8083063 Iustin Pop
      logger.Info("starting instance %s on node %s" % (instance, pnode_name))
3037 a8083063 Iustin Pop
      feedback_fn("* starting instance...")
3038 a8083063 Iustin Pop
      if not rpc.call_instance_start(pnode_name, iobj, None):
3039 3ecf6786 Iustin Pop
        raise errors.OpExecError("Could not start instance")
3040 a8083063 Iustin Pop
3041 a8083063 Iustin Pop
3042 a8083063 Iustin Pop
class LUConnectConsole(NoHooksLU):
3043 a8083063 Iustin Pop
  """Connect to an instance's console.
3044 a8083063 Iustin Pop

3045 a8083063 Iustin Pop
  This is somewhat special in that it returns the command line that
3046 a8083063 Iustin Pop
  you need to run on the master node in order to connect to the
3047 a8083063 Iustin Pop
  console.
3048 a8083063 Iustin Pop

3049 a8083063 Iustin Pop
  """
3050 a8083063 Iustin Pop
  _OP_REQP = ["instance_name"]
3051 a8083063 Iustin Pop
3052 a8083063 Iustin Pop
  def CheckPrereq(self):
3053 a8083063 Iustin Pop
    """Check prerequisites.
3054 a8083063 Iustin Pop

3055 a8083063 Iustin Pop
    This checks that the instance is in the cluster.
3056 a8083063 Iustin Pop

3057 a8083063 Iustin Pop
    """
3058 a8083063 Iustin Pop
    instance = self.cfg.GetInstanceInfo(
3059 a8083063 Iustin Pop
      self.cfg.ExpandInstanceName(self.op.instance_name))
3060 a8083063 Iustin Pop
    if instance is None:
3061 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' not known" %
3062 3ecf6786 Iustin Pop
                                 self.op.instance_name)
3063 a8083063 Iustin Pop
    self.instance = instance
3064 a8083063 Iustin Pop
3065 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
3066 a8083063 Iustin Pop
    """Connect to the console of an instance
3067 a8083063 Iustin Pop

3068 a8083063 Iustin Pop
    """
3069 a8083063 Iustin Pop
    instance = self.instance
3070 a8083063 Iustin Pop
    node = instance.primary_node
3071 a8083063 Iustin Pop
3072 a8083063 Iustin Pop
    node_insts = rpc.call_instance_list([node])[node]
3073 a8083063 Iustin Pop
    if node_insts is False:
3074 3ecf6786 Iustin Pop
      raise errors.OpExecError("Can't connect to node %s." % node)
3075 a8083063 Iustin Pop
3076 a8083063 Iustin Pop
    if instance.name not in node_insts:
3077 3ecf6786 Iustin Pop
      raise errors.OpExecError("Instance %s is not running." % instance.name)
3078 a8083063 Iustin Pop
3079 a8083063 Iustin Pop
    logger.Debug("connecting to console of %s on %s" % (instance.name, node))
3080 a8083063 Iustin Pop
3081 a8083063 Iustin Pop
    hyper = hypervisor.GetHypervisor()
3082 a8083063 Iustin Pop
    console_cmd = hyper.GetShellCommandForConsole(instance.name)
3083 82122173 Iustin Pop
    # build ssh cmdline
3084 82122173 Iustin Pop
    argv = ["ssh", "-q", "-t"]
3085 82122173 Iustin Pop
    argv.extend(ssh.KNOWN_HOSTS_OPTS)
3086 82122173 Iustin Pop
    argv.extend(ssh.BATCH_MODE_OPTS)
3087 82122173 Iustin Pop
    argv.append(node)
3088 82122173 Iustin Pop
    argv.append(console_cmd)
3089 82122173 Iustin Pop
    return "ssh", argv
3090 a8083063 Iustin Pop
3091 a8083063 Iustin Pop
3092 a8083063 Iustin Pop
class LUAddMDDRBDComponent(LogicalUnit):
3093 a8083063 Iustin Pop
  """Adda new mirror member to an instance's disk.
3094 a8083063 Iustin Pop

3095 a8083063 Iustin Pop
  """
3096 a8083063 Iustin Pop
  HPATH = "mirror-add"
3097 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_INSTANCE
3098 a8083063 Iustin Pop
  _OP_REQP = ["instance_name", "remote_node", "disk_name"]
3099 a8083063 Iustin Pop
3100 a8083063 Iustin Pop
  def BuildHooksEnv(self):
3101 a8083063 Iustin Pop
    """Build hooks env.
3102 a8083063 Iustin Pop

3103 a8083063 Iustin Pop
    This runs on the master, the primary and all the secondaries.
3104 a8083063 Iustin Pop

3105 a8083063 Iustin Pop
    """
3106 a8083063 Iustin Pop
    env = {
3107 a8083063 Iustin Pop
      "NEW_SECONDARY": self.op.remote_node,
3108 a8083063 Iustin Pop
      "DISK_NAME": self.op.disk_name,
3109 a8083063 Iustin Pop
      }
3110 396e1b78 Michael Hanselmann
    env.update(_BuildInstanceHookEnvByObject(self.instance))
3111 880478f8 Iustin Pop
    nl = [self.sstore.GetMasterNode(), self.instance.primary_node,
3112 a8083063 Iustin Pop
          self.op.remote_node,] + list(self.instance.secondary_nodes)
3113 a8083063 Iustin Pop
    return env, nl, nl
3114 a8083063 Iustin Pop
3115 a8083063 Iustin Pop
  def CheckPrereq(self):
3116 a8083063 Iustin Pop
    """Check prerequisites.
3117 a8083063 Iustin Pop

3118 a8083063 Iustin Pop
    This checks that the instance is in the cluster.
3119 a8083063 Iustin Pop

3120 a8083063 Iustin Pop
    """
3121 a8083063 Iustin Pop
    instance = self.cfg.GetInstanceInfo(
3122 a8083063 Iustin Pop
      self.cfg.ExpandInstanceName(self.op.instance_name))
3123 a8083063 Iustin Pop
    if instance is None:
3124 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' not known" %
3125 3ecf6786 Iustin Pop
                                 self.op.instance_name)
3126 a8083063 Iustin Pop
    self.instance = instance
3127 a8083063 Iustin Pop
3128 a8083063 Iustin Pop
    remote_node = self.cfg.ExpandNodeName(self.op.remote_node)
3129 a8083063 Iustin Pop
    if remote_node is None:
3130 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Node '%s' not known" % self.op.remote_node)
3131 a8083063 Iustin Pop
    self.remote_node = remote_node
3132 a8083063 Iustin Pop
3133 a8083063 Iustin Pop
    if remote_node == instance.primary_node:
3134 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("The specified node is the primary node of"
3135 3ecf6786 Iustin Pop
                                 " the instance.")
3136 a8083063 Iustin Pop
3137 a8083063 Iustin Pop
    if instance.disk_template != constants.DT_REMOTE_RAID1:
3138 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance's disk layout is not"
3139 3ecf6786 Iustin Pop
                                 " remote_raid1.")
3140 a8083063 Iustin Pop
    for disk in instance.disks:
3141 a8083063 Iustin Pop
      if disk.iv_name == self.op.disk_name:
3142 a8083063 Iustin Pop
        break
3143 a8083063 Iustin Pop
    else:
3144 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Can't find this device ('%s') in the"
3145 3ecf6786 Iustin Pop
                                 " instance." % self.op.disk_name)
3146 a8083063 Iustin Pop
    if len(disk.children) > 1:
3147 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("The device already has two slave"
3148 3ecf6786 Iustin Pop
                                 " devices.\n"
3149 3ecf6786 Iustin Pop
                                 "This would create a 3-disk raid1"
3150 3ecf6786 Iustin Pop
                                 " which we don't allow.")
3151 a8083063 Iustin Pop
    self.disk = disk
3152 a8083063 Iustin Pop
3153 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
3154 a8083063 Iustin Pop
    """Add the mirror component
3155 a8083063 Iustin Pop

3156 a8083063 Iustin Pop
    """
3157 a8083063 Iustin Pop
    disk = self.disk
3158 a8083063 Iustin Pop
    instance = self.instance
3159 a8083063 Iustin Pop
3160 a8083063 Iustin Pop
    remote_node = self.remote_node
3161 923b1523 Iustin Pop
    lv_names = [".%s_%s" % (disk.iv_name, suf) for suf in ["data", "meta"]]
3162 923b1523 Iustin Pop
    names = _GenerateUniqueNames(self.cfg, lv_names)
3163 923b1523 Iustin Pop
    new_drbd = _GenerateMDDRBDBranch(self.cfg, instance.primary_node,
3164 923b1523 Iustin Pop
                                     remote_node, disk.size, names)
3165 a8083063 Iustin Pop
3166 a8083063 Iustin Pop
    logger.Info("adding new mirror component on secondary")
3167 a8083063 Iustin Pop
    #HARDCODE
3168 3f78eef2 Iustin Pop
    if not _CreateBlockDevOnSecondary(self.cfg, remote_node, instance,
3169 3f78eef2 Iustin Pop
                                      new_drbd, False,
3170 a0c3fea1 Michael Hanselmann
                                      _GetInstanceInfoText(instance)):
3171 3ecf6786 Iustin Pop
      raise errors.OpExecError("Failed to create new component on secondary"
3172 3ecf6786 Iustin Pop
                               " node %s" % remote_node)
3173 a8083063 Iustin Pop
3174 a8083063 Iustin Pop
    logger.Info("adding new mirror component on primary")
3175 a8083063 Iustin Pop
    #HARDCODE
3176 3f78eef2 Iustin Pop
    if not _CreateBlockDevOnPrimary(self.cfg, instance.primary_node,
3177 3f78eef2 Iustin Pop
                                    instance, new_drbd,
3178 a0c3fea1 Michael Hanselmann
                                    _GetInstanceInfoText(instance)):
3179 a8083063 Iustin Pop
      # remove secondary dev
3180 a8083063 Iustin Pop
      self.cfg.SetDiskID(new_drbd, remote_node)
3181 a8083063 Iustin Pop
      rpc.call_blockdev_remove(remote_node, new_drbd)
3182 3ecf6786 Iustin Pop
      raise errors.OpExecError("Failed to create volume on primary")
3183 a8083063 Iustin Pop
3184 a8083063 Iustin Pop
    # the device exists now
3185 a8083063 Iustin Pop
    # call the primary node to add the mirror to md
3186 a8083063 Iustin Pop
    logger.Info("adding new mirror component to md")
3187 153d9724 Iustin Pop
    if not rpc.call_blockdev_addchildren(instance.primary_node,
3188 153d9724 Iustin Pop
                                         disk, [new_drbd]):
3189 a8083063 Iustin Pop
      logger.Error("Can't add mirror compoment to md!")
3190 a8083063 Iustin Pop
      self.cfg.SetDiskID(new_drbd, remote_node)
3191 a8083063 Iustin Pop
      if not rpc.call_blockdev_remove(remote_node, new_drbd):
3192 a8083063 Iustin Pop
        logger.Error("Can't rollback on secondary")
3193 a8083063 Iustin Pop
      self.cfg.SetDiskID(new_drbd, instance.primary_node)
3194 a8083063 Iustin Pop
      if not rpc.call_blockdev_remove(instance.primary_node, new_drbd):
3195 a8083063 Iustin Pop
        logger.Error("Can't rollback on primary")
3196 3ecf6786 Iustin Pop
      raise errors.OpExecError("Can't add mirror component to md array")
3197 a8083063 Iustin Pop
3198 a8083063 Iustin Pop
    disk.children.append(new_drbd)
3199 a8083063 Iustin Pop
3200 a8083063 Iustin Pop
    self.cfg.AddInstance(instance)
3201 a8083063 Iustin Pop
3202 5bfac263 Iustin Pop
    _WaitForSync(self.cfg, instance, self.proc)
3203 a8083063 Iustin Pop
3204 a8083063 Iustin Pop
    return 0
3205 a8083063 Iustin Pop
3206 a8083063 Iustin Pop
3207 a8083063 Iustin Pop
class LURemoveMDDRBDComponent(LogicalUnit):
3208 a8083063 Iustin Pop
  """Remove a component from a remote_raid1 disk.
3209 a8083063 Iustin Pop

3210 a8083063 Iustin Pop
  """
3211 a8083063 Iustin Pop
  HPATH = "mirror-remove"
3212 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_INSTANCE
3213 a8083063 Iustin Pop
  _OP_REQP = ["instance_name", "disk_name", "disk_id"]
3214 a8083063 Iustin Pop
3215 a8083063 Iustin Pop
  def BuildHooksEnv(self):
3216 a8083063 Iustin Pop
    """Build hooks env.
3217 a8083063 Iustin Pop

3218 a8083063 Iustin Pop
    This runs on the master, the primary and all the secondaries.
3219 a8083063 Iustin Pop

3220 a8083063 Iustin Pop
    """
3221 a8083063 Iustin Pop
    env = {
3222 a8083063 Iustin Pop
      "DISK_NAME": self.op.disk_name,
3223 a8083063 Iustin Pop
      "DISK_ID": self.op.disk_id,
3224 a8083063 Iustin Pop
      "OLD_SECONDARY": self.old_secondary,
3225 a8083063 Iustin Pop
      }
3226 396e1b78 Michael Hanselmann
    env.update(_BuildInstanceHookEnvByObject(self.instance))
3227 880478f8 Iustin Pop
    nl = [self.sstore.GetMasterNode(),
3228 a8083063 Iustin Pop
          self.instance.primary_node] + list(self.instance.secondary_nodes)
3229 a8083063 Iustin Pop
    return env, nl, nl
3230 a8083063 Iustin Pop
3231 a8083063 Iustin Pop
  def CheckPrereq(self):
3232 a8083063 Iustin Pop
    """Check prerequisites.
3233 a8083063 Iustin Pop

3234 a8083063 Iustin Pop
    This checks that the instance is in the cluster.
3235 a8083063 Iustin Pop

3236 a8083063 Iustin Pop
    """
3237 a8083063 Iustin Pop
    instance = self.cfg.GetInstanceInfo(
3238 a8083063 Iustin Pop
      self.cfg.ExpandInstanceName(self.op.instance_name))
3239 a8083063 Iustin Pop
    if instance is None:
3240 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' not known" %
3241 3ecf6786 Iustin Pop
                                 self.op.instance_name)
3242 a8083063 Iustin Pop
    self.instance = instance
3243 a8083063 Iustin Pop
3244 a8083063 Iustin Pop
    if instance.disk_template != constants.DT_REMOTE_RAID1:
3245 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance's disk layout is not"
3246 3ecf6786 Iustin Pop
                                 " remote_raid1.")
3247 a8083063 Iustin Pop
    for disk in instance.disks:
3248 a8083063 Iustin Pop
      if disk.iv_name == self.op.disk_name:
3249 a8083063 Iustin Pop
        break
3250 a8083063 Iustin Pop
    else:
3251 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Can't find this device ('%s') in the"
3252 3ecf6786 Iustin Pop
                                 " instance." % self.op.disk_name)
3253 a8083063 Iustin Pop
    for child in disk.children:
3254 fe96220b Iustin Pop
      if (child.dev_type == constants.LD_DRBD7 and
3255 fe96220b Iustin Pop
          child.logical_id[2] == self.op.disk_id):
3256 a8083063 Iustin Pop
        break
3257 a8083063 Iustin Pop
    else:
3258 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Can't find the device with this port.")
3259 a8083063 Iustin Pop
3260 a8083063 Iustin Pop
    if len(disk.children) < 2:
3261 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Cannot remove the last component from"
3262 3ecf6786 Iustin Pop
                                 " a mirror.")
3263 a8083063 Iustin Pop
    self.disk = disk
3264 a8083063 Iustin Pop
    self.child = child
3265 a8083063 Iustin Pop
    if self.child.logical_id[0] == instance.primary_node:
3266 a8083063 Iustin Pop
      oid = 1
3267 a8083063 Iustin Pop
    else:
3268 a8083063 Iustin Pop
      oid = 0
3269 a8083063 Iustin Pop
    self.old_secondary = self.child.logical_id[oid]
3270 a8083063 Iustin Pop
3271 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
3272 a8083063 Iustin Pop
    """Remove the mirror component
3273 a8083063 Iustin Pop

3274 a8083063 Iustin Pop
    """
3275 a8083063 Iustin Pop
    instance = self.instance
3276 a8083063 Iustin Pop
    disk = self.disk
3277 a8083063 Iustin Pop
    child = self.child
3278 a8083063 Iustin Pop
    logger.Info("remove mirror component")
3279 a8083063 Iustin Pop
    self.cfg.SetDiskID(disk, instance.primary_node)
3280 153d9724 Iustin Pop
    if not rpc.call_blockdev_removechildren(instance.primary_node,
3281 153d9724 Iustin Pop
                                            disk, [child]):
3282 3ecf6786 Iustin Pop
      raise errors.OpExecError("Can't remove child from mirror.")
3283 a8083063 Iustin Pop
3284 a8083063 Iustin Pop
    for node in child.logical_id[:2]:
3285 a8083063 Iustin Pop
      self.cfg.SetDiskID(child, node)
3286 a8083063 Iustin Pop
      if not rpc.call_blockdev_remove(node, child):
3287 a8083063 Iustin Pop
        logger.Error("Warning: failed to remove device from node %s,"
3288 a8083063 Iustin Pop
                     " continuing operation." % node)
3289 a8083063 Iustin Pop
3290 a8083063 Iustin Pop
    disk.children.remove(child)
3291 a8083063 Iustin Pop
    self.cfg.AddInstance(instance)
3292 a8083063 Iustin Pop
3293 a8083063 Iustin Pop
3294 a8083063 Iustin Pop
class LUReplaceDisks(LogicalUnit):
3295 a8083063 Iustin Pop
  """Replace the disks of an instance.
3296 a8083063 Iustin Pop

3297 a8083063 Iustin Pop
  """
3298 a8083063 Iustin Pop
  HPATH = "mirrors-replace"
3299 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_INSTANCE
3300 a9e0c397 Iustin Pop
  _OP_REQP = ["instance_name", "mode", "disks"]
3301 a8083063 Iustin Pop
3302 a8083063 Iustin Pop
  def BuildHooksEnv(self):
3303 a8083063 Iustin Pop
    """Build hooks env.
3304 a8083063 Iustin Pop

3305 a8083063 Iustin Pop
    This runs on the master, the primary and all the secondaries.
3306 a8083063 Iustin Pop

3307 a8083063 Iustin Pop
    """
3308 a8083063 Iustin Pop
    env = {
3309 a9e0c397 Iustin Pop
      "MODE": self.op.mode,
3310 a8083063 Iustin Pop
      "NEW_SECONDARY": self.op.remote_node,
3311 a8083063 Iustin Pop
      "OLD_SECONDARY": self.instance.secondary_nodes[0],
3312 a8083063 Iustin Pop
      }
3313 396e1b78 Michael Hanselmann
    env.update(_BuildInstanceHookEnvByObject(self.instance))
3314 0834c866 Iustin Pop
    nl = [
3315 0834c866 Iustin Pop
      self.sstore.GetMasterNode(),
3316 0834c866 Iustin Pop
      self.instance.primary_node,
3317 0834c866 Iustin Pop
      ]
3318 0834c866 Iustin Pop
    if self.op.remote_node is not None:
3319 0834c866 Iustin Pop
      nl.append(self.op.remote_node)
3320 a8083063 Iustin Pop
    return env, nl, nl
3321 a8083063 Iustin Pop
3322 a8083063 Iustin Pop
  def CheckPrereq(self):
3323 a8083063 Iustin Pop
    """Check prerequisites.
3324 a8083063 Iustin Pop

3325 a8083063 Iustin Pop
    This checks that the instance is in the cluster.
3326 a8083063 Iustin Pop

3327 a8083063 Iustin Pop
    """
3328 a8083063 Iustin Pop
    instance = self.cfg.GetInstanceInfo(
3329 a8083063 Iustin Pop
      self.cfg.ExpandInstanceName(self.op.instance_name))
3330 a8083063 Iustin Pop
    if instance is None:
3331 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' not known" %
3332 3ecf6786 Iustin Pop
                                 self.op.instance_name)
3333 a8083063 Iustin Pop
    self.instance = instance
3334 7df43a76 Iustin Pop
    self.op.instance_name = instance.name
3335 a8083063 Iustin Pop
3336 a9e0c397 Iustin Pop
    if instance.disk_template not in constants.DTS_NET_MIRROR:
3337 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance's disk layout is not"
3338 a9e0c397 Iustin Pop
                                 " network mirrored.")
3339 a8083063 Iustin Pop
3340 a8083063 Iustin Pop
    if len(instance.secondary_nodes) != 1:
3341 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("The instance has a strange layout,"
3342 3ecf6786 Iustin Pop
                                 " expected one secondary but found %d" %
3343 3ecf6786 Iustin Pop
                                 len(instance.secondary_nodes))
3344 a8083063 Iustin Pop
3345 a9e0c397 Iustin Pop
    self.sec_node = instance.secondary_nodes[0]
3346 a9e0c397 Iustin Pop
3347 a8083063 Iustin Pop
    remote_node = getattr(self.op, "remote_node", None)
3348 a9e0c397 Iustin Pop
    if remote_node is not None:
3349 a8083063 Iustin Pop
      remote_node = self.cfg.ExpandNodeName(remote_node)
3350 a8083063 Iustin Pop
      if remote_node is None:
3351 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Node '%s' not known" %
3352 3ecf6786 Iustin Pop
                                   self.op.remote_node)
3353 a9e0c397 Iustin Pop
      self.remote_node_info = self.cfg.GetNodeInfo(remote_node)
3354 a9e0c397 Iustin Pop
    else:
3355 a9e0c397 Iustin Pop
      self.remote_node_info = None
3356 a8083063 Iustin Pop
    if remote_node == instance.primary_node:
3357 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("The specified node is the primary node of"
3358 3ecf6786 Iustin Pop
                                 " the instance.")
3359 a9e0c397 Iustin Pop
    elif remote_node == self.sec_node:
3360 0834c866 Iustin Pop
      if self.op.mode == constants.REPLACE_DISK_SEC:
3361 0834c866 Iustin Pop
        # this is for DRBD8, where we can't execute the same mode of
3362 0834c866 Iustin Pop
        # replacement as for drbd7 (no different port allocated)
3363 0834c866 Iustin Pop
        raise errors.OpPrereqError("Same secondary given, cannot execute"
3364 0834c866 Iustin Pop
                                   " replacement")
3365 a9e0c397 Iustin Pop
      # the user gave the current secondary, switch to
3366 0834c866 Iustin Pop
      # 'no-replace-secondary' mode for drbd7
3367 a9e0c397 Iustin Pop
      remote_node = None
3368 a9e0c397 Iustin Pop
    if (instance.disk_template == constants.DT_REMOTE_RAID1 and
3369 a9e0c397 Iustin Pop
        self.op.mode != constants.REPLACE_DISK_ALL):
3370 a9e0c397 Iustin Pop
      raise errors.OpPrereqError("Template 'remote_raid1' only allows all"
3371 a9e0c397 Iustin Pop
                                 " disks replacement, not individual ones")
3372 a9e0c397 Iustin Pop
    if instance.disk_template == constants.DT_DRBD8:
3373 7df43a76 Iustin Pop
      if (self.op.mode == constants.REPLACE_DISK_ALL and
3374 7df43a76 Iustin Pop
          remote_node is not None):
3375 7df43a76 Iustin Pop
        # switch to replace secondary mode
3376 7df43a76 Iustin Pop
        self.op.mode = constants.REPLACE_DISK_SEC
3377 7df43a76 Iustin Pop
3378 a9e0c397 Iustin Pop
      if self.op.mode == constants.REPLACE_DISK_ALL:
3379 a9e0c397 Iustin Pop
        raise errors.OpPrereqError("Template 'drbd8' only allows primary or"
3380 a9e0c397 Iustin Pop
                                   " secondary disk replacement, not"
3381 a9e0c397 Iustin Pop
                                   " both at once")
3382 a9e0c397 Iustin Pop
      elif self.op.mode == constants.REPLACE_DISK_PRI:
3383 a9e0c397 Iustin Pop
        if remote_node is not None:
3384 a9e0c397 Iustin Pop
          raise errors.OpPrereqError("Template 'drbd8' does not allow changing"
3385 a9e0c397 Iustin Pop
                                     " the secondary while doing a primary"
3386 a9e0c397 Iustin Pop
                                     " node disk replacement")
3387 a9e0c397 Iustin Pop
        self.tgt_node = instance.primary_node
3388 cff90b79 Iustin Pop
        self.oth_node = instance.secondary_nodes[0]
3389 a9e0c397 Iustin Pop
      elif self.op.mode == constants.REPLACE_DISK_SEC:
3390 a9e0c397 Iustin Pop
        self.new_node = remote_node # this can be None, in which case
3391 a9e0c397 Iustin Pop
                                    # we don't change the secondary
3392 a9e0c397 Iustin Pop
        self.tgt_node = instance.secondary_nodes[0]
3393 cff90b79 Iustin Pop
        self.oth_node = instance.primary_node
3394 a9e0c397 Iustin Pop
      else:
3395 a9e0c397 Iustin Pop
        raise errors.ProgrammerError("Unhandled disk replace mode")
3396 a9e0c397 Iustin Pop
3397 a9e0c397 Iustin Pop
    for name in self.op.disks:
3398 a9e0c397 Iustin Pop
      if instance.FindDisk(name) is None:
3399 a9e0c397 Iustin Pop
        raise errors.OpPrereqError("Disk '%s' not found for instance '%s'" %
3400 a9e0c397 Iustin Pop
                                   (name, instance.name))
3401 a8083063 Iustin Pop
    self.op.remote_node = remote_node
3402 a8083063 Iustin Pop
3403 a9e0c397 Iustin Pop
  def _ExecRR1(self, feedback_fn):
3404 a8083063 Iustin Pop
    """Replace the disks of an instance.
3405 a8083063 Iustin Pop

3406 a8083063 Iustin Pop
    """
3407 a8083063 Iustin Pop
    instance = self.instance
3408 a8083063 Iustin Pop
    iv_names = {}
3409 a8083063 Iustin Pop
    # start of work
3410 a9e0c397 Iustin Pop
    if self.op.remote_node is None:
3411 a9e0c397 Iustin Pop
      remote_node = self.sec_node
3412 a9e0c397 Iustin Pop
    else:
3413 a9e0c397 Iustin Pop
      remote_node = self.op.remote_node
3414 a8083063 Iustin Pop
    cfg = self.cfg
3415 a8083063 Iustin Pop
    for dev in instance.disks:
3416 a8083063 Iustin Pop
      size = dev.size
3417 923b1523 Iustin Pop
      lv_names = [".%s_%s" % (dev.iv_name, suf) for suf in ["data", "meta"]]
3418 923b1523 Iustin Pop
      names = _GenerateUniqueNames(cfg, lv_names)
3419 923b1523 Iustin Pop
      new_drbd = _GenerateMDDRBDBranch(cfg, instance.primary_node,
3420 923b1523 Iustin Pop
                                       remote_node, size, names)
3421 a8083063 Iustin Pop
      iv_names[dev.iv_name] = (dev, dev.children[0], new_drbd)
3422 a8083063 Iustin Pop
      logger.Info("adding new mirror component on secondary for %s" %
3423 a8083063 Iustin Pop
                  dev.iv_name)
3424 a8083063 Iustin Pop
      #HARDCODE
3425 3f78eef2 Iustin Pop
      if not _CreateBlockDevOnSecondary(cfg, remote_node, instance,
3426 3f78eef2 Iustin Pop
                                        new_drbd, False,
3427 a0c3fea1 Michael Hanselmann
                                        _GetInstanceInfoText(instance)):
3428 3ecf6786 Iustin Pop
        raise errors.OpExecError("Failed to create new component on"
3429 3ecf6786 Iustin Pop
                                 " secondary node %s\n"
3430 3ecf6786 Iustin Pop
                                 "Full abort, cleanup manually!" %
3431 3ecf6786 Iustin Pop
                                 remote_node)
3432 a8083063 Iustin Pop
3433 a8083063 Iustin Pop
      logger.Info("adding new mirror component on primary")
3434 a8083063 Iustin Pop
      #HARDCODE
3435 3f78eef2 Iustin Pop
      if not _CreateBlockDevOnPrimary(cfg, instance.primary_node,
3436 3f78eef2 Iustin Pop
                                      instance, new_drbd,
3437 a0c3fea1 Michael Hanselmann
                                      _GetInstanceInfoText(instance)):
3438 a8083063 Iustin Pop
        # remove secondary dev
3439 a8083063 Iustin Pop
        cfg.SetDiskID(new_drbd, remote_node)
3440 a8083063 Iustin Pop
        rpc.call_blockdev_remove(remote_node, new_drbd)
3441 a8083063 Iustin Pop
        raise errors.OpExecError("Failed to create volume on primary!\n"
3442 a8083063 Iustin Pop
                                 "Full abort, cleanup manually!!")
3443 a8083063 Iustin Pop
3444 a8083063 Iustin Pop
      # the device exists now
3445 a8083063 Iustin Pop
      # call the primary node to add the mirror to md
3446 a8083063 Iustin Pop
      logger.Info("adding new mirror component to md")
3447 153d9724 Iustin Pop
      if not rpc.call_blockdev_addchildren(instance.primary_node, dev,
3448 153d9724 Iustin Pop
                                           [new_drbd]):
3449 a8083063 Iustin Pop
        logger.Error("Can't add mirror compoment to md!")
3450 a8083063 Iustin Pop
        cfg.SetDiskID(new_drbd, remote_node)
3451 a8083063 Iustin Pop
        if not rpc.call_blockdev_remove(remote_node, new_drbd):
3452 a8083063 Iustin Pop
          logger.Error("Can't rollback on secondary")
3453 a8083063 Iustin Pop
        cfg.SetDiskID(new_drbd, instance.primary_node)
3454 a8083063 Iustin Pop
        if not rpc.call_blockdev_remove(instance.primary_node, new_drbd):
3455 a8083063 Iustin Pop
          logger.Error("Can't rollback on primary")
3456 3ecf6786 Iustin Pop
        raise errors.OpExecError("Full abort, cleanup manually!!")
3457 a8083063 Iustin Pop
3458 a8083063 Iustin Pop
      dev.children.append(new_drbd)
3459 a8083063 Iustin Pop
      cfg.AddInstance(instance)
3460 a8083063 Iustin Pop
3461 a8083063 Iustin Pop
    # this can fail as the old devices are degraded and _WaitForSync
3462 a8083063 Iustin Pop
    # does a combined result over all disks, so we don't check its
3463 a8083063 Iustin Pop
    # return value
3464 5bfac263 Iustin Pop
    _WaitForSync(cfg, instance, self.proc, unlock=True)
3465 a8083063 Iustin Pop
3466 a8083063 Iustin Pop
    # so check manually all the devices
3467 a8083063 Iustin Pop
    for name in iv_names:
3468 a8083063 Iustin Pop
      dev, child, new_drbd = iv_names[name]
3469 a8083063 Iustin Pop
      cfg.SetDiskID(dev, instance.primary_node)
3470 a8083063 Iustin Pop
      is_degr = rpc.call_blockdev_find(instance.primary_node, dev)[5]
3471 a8083063 Iustin Pop
      if is_degr:
3472 3ecf6786 Iustin Pop
        raise errors.OpExecError("MD device %s is degraded!" % name)
3473 a8083063 Iustin Pop
      cfg.SetDiskID(new_drbd, instance.primary_node)
3474 a8083063 Iustin Pop
      is_degr = rpc.call_blockdev_find(instance.primary_node, new_drbd)[5]
3475 a8083063 Iustin Pop
      if is_degr:
3476 3ecf6786 Iustin Pop
        raise errors.OpExecError("New drbd device %s is degraded!" % name)
3477 a8083063 Iustin Pop
3478 a8083063 Iustin Pop
    for name in iv_names:
3479 a8083063 Iustin Pop
      dev, child, new_drbd = iv_names[name]
3480 a8083063 Iustin Pop
      logger.Info("remove mirror %s component" % name)
3481 a8083063 Iustin Pop
      cfg.SetDiskID(dev, instance.primary_node)
3482 153d9724 Iustin Pop
      if not rpc.call_blockdev_removechildren(instance.primary_node,
3483 153d9724 Iustin Pop
                                              dev, [child]):
3484 a8083063 Iustin Pop
        logger.Error("Can't remove child from mirror, aborting"
3485 a8083063 Iustin Pop
                     " *this device cleanup*.\nYou need to cleanup manually!!")
3486 a8083063 Iustin Pop
        continue
3487 a8083063 Iustin Pop
3488 a8083063 Iustin Pop
      for node in child.logical_id[:2]:
3489 a8083063 Iustin Pop
        logger.Info("remove child device on %s" % node)
3490 a8083063 Iustin Pop
        cfg.SetDiskID(child, node)
3491 a8083063 Iustin Pop
        if not rpc.call_blockdev_remove(node, child):
3492 a8083063 Iustin Pop
          logger.Error("Warning: failed to remove device from node %s,"
3493 a8083063 Iustin Pop
                       " continuing operation." % node)
3494 a8083063 Iustin Pop
3495 a8083063 Iustin Pop
      dev.children.remove(child)
3496 a8083063 Iustin Pop
3497 a8083063 Iustin Pop
      cfg.AddInstance(instance)
3498 a8083063 Iustin Pop
3499 a9e0c397 Iustin Pop
  def _ExecD8DiskOnly(self, feedback_fn):
3500 a9e0c397 Iustin Pop
    """Replace a disk on the primary or secondary for dbrd8.
3501 a9e0c397 Iustin Pop

3502 a9e0c397 Iustin Pop
    The algorithm for replace is quite complicated:
3503 a9e0c397 Iustin Pop
      - for each disk to be replaced:
3504 a9e0c397 Iustin Pop
        - create new LVs on the target node with unique names
3505 a9e0c397 Iustin Pop
        - detach old LVs from the drbd device
3506 a9e0c397 Iustin Pop
        - rename old LVs to name_replaced.<time_t>
3507 a9e0c397 Iustin Pop
        - rename new LVs to old LVs
3508 a9e0c397 Iustin Pop
        - attach the new LVs (with the old names now) to the drbd device
3509 a9e0c397 Iustin Pop
      - wait for sync across all devices
3510 a9e0c397 Iustin Pop
      - for each modified disk:
3511 a9e0c397 Iustin Pop
        - remove old LVs (which have the name name_replaces.<time_t>)
3512 a9e0c397 Iustin Pop

3513 a9e0c397 Iustin Pop
    Failures are not very well handled.
3514 cff90b79 Iustin Pop

3515 a9e0c397 Iustin Pop
    """
3516 cff90b79 Iustin Pop
    steps_total = 6
3517 5bfac263 Iustin Pop
    warning, info = (self.proc.LogWarning, self.proc.LogInfo)
3518 a9e0c397 Iustin Pop
    instance = self.instance
3519 a9e0c397 Iustin Pop
    iv_names = {}
3520 a9e0c397 Iustin Pop
    vgname = self.cfg.GetVGName()
3521 a9e0c397 Iustin Pop
    # start of work
3522 a9e0c397 Iustin Pop
    cfg = self.cfg
3523 a9e0c397 Iustin Pop
    tgt_node = self.tgt_node
3524 cff90b79 Iustin Pop
    oth_node = self.oth_node
3525 cff90b79 Iustin Pop
3526 cff90b79 Iustin Pop
    # Step: check device activation
3527 5bfac263 Iustin Pop
    self.proc.LogStep(1, steps_total, "check device existence")
3528 cff90b79 Iustin Pop
    info("checking volume groups")
3529 cff90b79 Iustin Pop
    my_vg = cfg.GetVGName()
3530 cff90b79 Iustin Pop
    results = rpc.call_vg_list([oth_node, tgt_node])
3531 cff90b79 Iustin Pop
    if not results:
3532 cff90b79 Iustin Pop
      raise errors.OpExecError("Can't list volume groups on the nodes")
3533 cff90b79 Iustin Pop
    for node in oth_node, tgt_node:
3534 cff90b79 Iustin Pop
      res = results.get(node, False)
3535 cff90b79 Iustin Pop
      if not res or my_vg not in res:
3536 cff90b79 Iustin Pop
        raise errors.OpExecError("Volume group '%s' not found on %s" %
3537 cff90b79 Iustin Pop
                                 (my_vg, node))
3538 cff90b79 Iustin Pop
    for dev in instance.disks:
3539 cff90b79 Iustin Pop
      if not dev.iv_name in self.op.disks:
3540 cff90b79 Iustin Pop
        continue
3541 cff90b79 Iustin Pop
      for node in tgt_node, oth_node:
3542 cff90b79 Iustin Pop
        info("checking %s on %s" % (dev.iv_name, node))
3543 cff90b79 Iustin Pop
        cfg.SetDiskID(dev, node)
3544 cff90b79 Iustin Pop
        if not rpc.call_blockdev_find(node, dev):
3545 cff90b79 Iustin Pop
          raise errors.OpExecError("Can't find device %s on node %s" %
3546 cff90b79 Iustin Pop
                                   (dev.iv_name, node))
3547 cff90b79 Iustin Pop
3548 cff90b79 Iustin Pop
    # Step: check other node consistency
3549 5bfac263 Iustin Pop
    self.proc.LogStep(2, steps_total, "check peer consistency")
3550 cff90b79 Iustin Pop
    for dev in instance.disks:
3551 cff90b79 Iustin Pop
      if not dev.iv_name in self.op.disks:
3552 cff90b79 Iustin Pop
        continue
3553 cff90b79 Iustin Pop
      info("checking %s consistency on %s" % (dev.iv_name, oth_node))
3554 cff90b79 Iustin Pop
      if not _CheckDiskConsistency(self.cfg, dev, oth_node,
3555 cff90b79 Iustin Pop
                                   oth_node==instance.primary_node):
3556 cff90b79 Iustin Pop
        raise errors.OpExecError("Peer node (%s) has degraded storage, unsafe"
3557 cff90b79 Iustin Pop
                                 " to replace disks on this node (%s)" %
3558 cff90b79 Iustin Pop
                                 (oth_node, tgt_node))
3559 cff90b79 Iustin Pop
3560 cff90b79 Iustin Pop
    # Step: create new storage
3561 5bfac263 Iustin Pop
    self.proc.LogStep(3, steps_total, "allocate new storage")
3562 a9e0c397 Iustin Pop
    for dev in instance.disks:
3563 a9e0c397 Iustin Pop
      if not dev.iv_name in self.op.disks:
3564 a9e0c397 Iustin Pop
        continue
3565 a9e0c397 Iustin Pop
      size = dev.size
3566 a9e0c397 Iustin Pop
      cfg.SetDiskID(dev, tgt_node)
3567 a9e0c397 Iustin Pop
      lv_names = [".%s_%s" % (dev.iv_name, suf) for suf in ["data", "meta"]]
3568 a9e0c397 Iustin Pop
      names = _GenerateUniqueNames(cfg, lv_names)
3569 a9e0c397 Iustin Pop
      lv_data = objects.Disk(dev_type=constants.LD_LV, size=size,
3570 a9e0c397 Iustin Pop
                             logical_id=(vgname, names[0]))
3571 a9e0c397 Iustin Pop
      lv_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
3572 a9e0c397 Iustin Pop
                             logical_id=(vgname, names[1]))
3573 a9e0c397 Iustin Pop
      new_lvs = [lv_data, lv_meta]
3574 a9e0c397 Iustin Pop
      old_lvs = dev.children
3575 a9e0c397 Iustin Pop
      iv_names[dev.iv_name] = (dev, old_lvs, new_lvs)
3576 cff90b79 Iustin Pop
      info("creating new local storage on %s for %s" %
3577 cff90b79 Iustin Pop
           (tgt_node, dev.iv_name))
3578 a9e0c397 Iustin Pop
      # since we *always* want to create this LV, we use the
3579 a9e0c397 Iustin Pop
      # _Create...OnPrimary (which forces the creation), even if we
3580 a9e0c397 Iustin Pop
      # are talking about the secondary node
3581 a9e0c397 Iustin Pop
      for new_lv in new_lvs:
3582 3f78eef2 Iustin Pop
        if not _CreateBlockDevOnPrimary(cfg, tgt_node, instance, new_lv,
3583 a9e0c397 Iustin Pop
                                        _GetInstanceInfoText(instance)):
3584 a9e0c397 Iustin Pop
          raise errors.OpExecError("Failed to create new LV named '%s' on"
3585 a9e0c397 Iustin Pop
                                   " node '%s'" %
3586 a9e0c397 Iustin Pop
                                   (new_lv.logical_id[1], tgt_node))
3587 a9e0c397 Iustin Pop
3588 cff90b79 Iustin Pop
    # Step: for each lv, detach+rename*2+attach
3589 5bfac263 Iustin Pop
    self.proc.LogStep(4, steps_total, "change drbd configuration")
3590 cff90b79 Iustin Pop
    for dev, old_lvs, new_lvs in iv_names.itervalues():
3591 cff90b79 Iustin Pop
      info("detaching %s drbd from local storage" % dev.iv_name)
3592 a9e0c397 Iustin Pop
      if not rpc.call_blockdev_removechildren(tgt_node, dev, old_lvs):
3593 a9e0c397 Iustin Pop
        raise errors.OpExecError("Can't detach drbd from local storage on node"
3594 a9e0c397 Iustin Pop
                                 " %s for device %s" % (tgt_node, dev.iv_name))
3595 cff90b79 Iustin Pop
      #dev.children = []
3596 cff90b79 Iustin Pop
      #cfg.Update(instance)
3597 a9e0c397 Iustin Pop
3598 a9e0c397 Iustin Pop
      # ok, we created the new LVs, so now we know we have the needed
3599 a9e0c397 Iustin Pop
      # storage; as such, we proceed on the target node to rename
3600 a9e0c397 Iustin Pop
      # old_lv to _old, and new_lv to old_lv; note that we rename LVs
3601 a9e0c397 Iustin Pop
      # using the assumption than logical_id == physical_id (which in
3602 a9e0c397 Iustin Pop
      # turn is the unique_id on that node)
3603 cff90b79 Iustin Pop
3604 cff90b79 Iustin Pop
      # FIXME(iustin): use a better name for the replaced LVs
3605 a9e0c397 Iustin Pop
      temp_suffix = int(time.time())
3606 a9e0c397 Iustin Pop
      ren_fn = lambda d, suff: (d.physical_id[0],
3607 a9e0c397 Iustin Pop
                                d.physical_id[1] + "_replaced-%s" % suff)
3608 cff90b79 Iustin Pop
      # build the rename list based on what LVs exist on the node
3609 cff90b79 Iustin Pop
      rlist = []
3610 cff90b79 Iustin Pop
      for to_ren in old_lvs:
3611 cff90b79 Iustin Pop
        find_res = rpc.call_blockdev_find(tgt_node, to_ren)
3612 cff90b79 Iustin Pop
        if find_res is not None: # device exists
3613 cff90b79 Iustin Pop
          rlist.append((to_ren, ren_fn(to_ren, temp_suffix)))
3614 cff90b79 Iustin Pop
3615 cff90b79 Iustin Pop
      info("renaming the old LVs on the target node")
3616 a9e0c397 Iustin Pop
      if not rpc.call_blockdev_rename(tgt_node, rlist):
3617 cff90b79 Iustin Pop
        raise errors.OpExecError("Can't rename old LVs on node %s" % tgt_node)
3618 a9e0c397 Iustin Pop
      # now we rename the new LVs to the old LVs
3619 cff90b79 Iustin Pop
      info("renaming the new LVs on the target node")
3620 a9e0c397 Iustin Pop
      rlist = [(new, old.physical_id) for old, new in zip(old_lvs, new_lvs)]
3621 a9e0c397 Iustin Pop
      if not rpc.call_blockdev_rename(tgt_node, rlist):
3622 cff90b79 Iustin Pop
        raise errors.OpExecError("Can't rename new LVs on node %s" % tgt_node)
3623 cff90b79 Iustin Pop
3624 cff90b79 Iustin Pop
      for old, new in zip(old_lvs, new_lvs):
3625 cff90b79 Iustin Pop
        new.logical_id = old.logical_id
3626 cff90b79 Iustin Pop
        cfg.SetDiskID(new, tgt_node)
3627 a9e0c397 Iustin Pop
3628 cff90b79 Iustin Pop
      for disk in old_lvs:
3629 cff90b79 Iustin Pop
        disk.logical_id = ren_fn(disk, temp_suffix)
3630 cff90b79 Iustin Pop
        cfg.SetDiskID(disk, tgt_node)
3631 a9e0c397 Iustin Pop
3632 a9e0c397 Iustin Pop
      # now that the new lvs have the old name, we can add them to the device
3633 cff90b79 Iustin Pop
      info("adding new mirror component on %s" % tgt_node)
3634 a9e0c397 Iustin Pop
      if not rpc.call_blockdev_addchildren(tgt_node, dev, new_lvs):
3635 a9e0c397 Iustin Pop
        for new_lv in new_lvs:
3636 a9e0c397 Iustin Pop
          if not rpc.call_blockdev_remove(tgt_node, new_lv):
3637 cff90b79 Iustin Pop
            warning("Can't rollback device %s", "manually cleanup unused"
3638 cff90b79 Iustin Pop
                    " logical volumes")
3639 cff90b79 Iustin Pop
        raise errors.OpExecError("Can't add local storage to drbd")
3640 a9e0c397 Iustin Pop
3641 a9e0c397 Iustin Pop
      dev.children = new_lvs
3642 a9e0c397 Iustin Pop
      cfg.Update(instance)
3643 a9e0c397 Iustin Pop
3644 cff90b79 Iustin Pop
    # Step: wait for sync
3645 a9e0c397 Iustin Pop
3646 a9e0c397 Iustin Pop
    # this can fail as the old devices are degraded and _WaitForSync
3647 a9e0c397 Iustin Pop
    # does a combined result over all disks, so we don't check its
3648 a9e0c397 Iustin Pop
    # return value
3649 5bfac263 Iustin Pop
    self.proc.LogStep(5, steps_total, "sync devices")
3650 5bfac263 Iustin Pop
    _WaitForSync(cfg, instance, self.proc, unlock=True)
3651 a9e0c397 Iustin Pop
3652 a9e0c397 Iustin Pop
    # so check manually all the devices
3653 a9e0c397 Iustin Pop
    for name, (dev, old_lvs, new_lvs) in iv_names.iteritems():
3654 a9e0c397 Iustin Pop
      cfg.SetDiskID(dev, instance.primary_node)
3655 a9e0c397 Iustin Pop
      is_degr = rpc.call_blockdev_find(instance.primary_node, dev)[5]
3656 a9e0c397 Iustin Pop
      if is_degr:
3657 a9e0c397 Iustin Pop
        raise errors.OpExecError("DRBD device %s is degraded!" % name)
3658 a9e0c397 Iustin Pop
3659 cff90b79 Iustin Pop
    # Step: remove old storage
3660 5bfac263 Iustin Pop
    self.proc.LogStep(6, steps_total, "removing old storage")
3661 a9e0c397 Iustin Pop
    for name, (dev, old_lvs, new_lvs) in iv_names.iteritems():
3662 cff90b79 Iustin Pop
      info("remove logical volumes for %s" % name)
3663 a9e0c397 Iustin Pop
      for lv in old_lvs:
3664 a9e0c397 Iustin Pop
        cfg.SetDiskID(lv, tgt_node)
3665 a9e0c397 Iustin Pop
        if not rpc.call_blockdev_remove(tgt_node, lv):
3666 cff90b79 Iustin Pop
          warning("Can't remove old LV", "manually remove unused LVs")
3667 a9e0c397 Iustin Pop
          continue
3668 a9e0c397 Iustin Pop
3669 a9e0c397 Iustin Pop
  def _ExecD8Secondary(self, feedback_fn):
3670 a9e0c397 Iustin Pop
    """Replace the secondary node for drbd8.
3671 a9e0c397 Iustin Pop

3672 a9e0c397 Iustin Pop
    The algorithm for replace is quite complicated:
3673 a9e0c397 Iustin Pop
      - for all disks of the instance:
3674 a9e0c397 Iustin Pop
        - create new LVs on the new node with same names
3675 a9e0c397 Iustin Pop
        - shutdown the drbd device on the old secondary
3676 a9e0c397 Iustin Pop
        - disconnect the drbd network on the primary
3677 a9e0c397 Iustin Pop
        - create the drbd device on the new secondary
3678 a9e0c397 Iustin Pop
        - network attach the drbd on the primary, using an artifice:
3679 a9e0c397 Iustin Pop
          the drbd code for Attach() will connect to the network if it
3680 a9e0c397 Iustin Pop
          finds a device which is connected to the good local disks but
3681 a9e0c397 Iustin Pop
          not network enabled
3682 a9e0c397 Iustin Pop
      - wait for sync across all devices
3683 a9e0c397 Iustin Pop
      - remove all disks from the old secondary
3684 a9e0c397 Iustin Pop

3685 a9e0c397 Iustin Pop
    Failures are not very well handled.
3686 0834c866 Iustin Pop

3687 a9e0c397 Iustin Pop
    """
3688 0834c866 Iustin Pop
    steps_total = 6
3689 5bfac263 Iustin Pop
    warning, info = (self.proc.LogWarning, self.proc.LogInfo)
3690 a9e0c397 Iustin Pop
    instance = self.instance
3691 a9e0c397 Iustin Pop
    iv_names = {}
3692 a9e0c397 Iustin Pop
    vgname = self.cfg.GetVGName()
3693 a9e0c397 Iustin Pop
    # start of work
3694 a9e0c397 Iustin Pop
    cfg = self.cfg
3695 a9e0c397 Iustin Pop
    old_node = self.tgt_node
3696 a9e0c397 Iustin Pop
    new_node = self.new_node
3697 a9e0c397 Iustin Pop
    pri_node = instance.primary_node
3698 0834c866 Iustin Pop
3699 0834c866 Iustin Pop
    # Step: check device activation
3700 5bfac263 Iustin Pop
    self.proc.LogStep(1, steps_total, "check device existence")
3701 0834c866 Iustin Pop
    info("checking volume groups")
3702 0834c866 Iustin Pop
    my_vg = cfg.GetVGName()
3703 0834c866 Iustin Pop
    results = rpc.call_vg_list([pri_node, new_node])
3704 0834c866 Iustin Pop
    if not results:
3705 0834c866 Iustin Pop
      raise errors.OpExecError("Can't list volume groups on the nodes")
3706 0834c866 Iustin Pop
    for node in pri_node, new_node:
3707 0834c866 Iustin Pop
      res = results.get(node, False)
3708 0834c866 Iustin Pop
      if not res or my_vg not in res:
3709 0834c866 Iustin Pop
        raise errors.OpExecError("Volume group '%s' not found on %s" %
3710 0834c866 Iustin Pop
                                 (my_vg, node))
3711 0834c866 Iustin Pop
    for dev in instance.disks:
3712 0834c866 Iustin Pop
      if not dev.iv_name in self.op.disks:
3713 0834c866 Iustin Pop
        continue
3714 0834c866 Iustin Pop
      info("checking %s on %s" % (dev.iv_name, pri_node))
3715 0834c866 Iustin Pop
      cfg.SetDiskID(dev, pri_node)
3716 0834c866 Iustin Pop
      if not rpc.call_blockdev_find(pri_node, dev):
3717 0834c866 Iustin Pop
        raise errors.OpExecError("Can't find device %s on node %s" %
3718 0834c866 Iustin Pop
                                 (dev.iv_name, pri_node))
3719 0834c866 Iustin Pop
3720 0834c866 Iustin Pop
    # Step: check other node consistency
3721 5bfac263 Iustin Pop
    self.proc.LogStep(2, steps_total, "check peer consistency")
3722 0834c866 Iustin Pop
    for dev in instance.disks:
3723 0834c866 Iustin Pop
      if not dev.iv_name in self.op.disks:
3724 0834c866 Iustin Pop
        continue
3725 0834c866 Iustin Pop
      info("checking %s consistency on %s" % (dev.iv_name, pri_node))
3726 0834c866 Iustin Pop
      if not _CheckDiskConsistency(self.cfg, dev, pri_node, True, ldisk=True):
3727 0834c866 Iustin Pop
        raise errors.OpExecError("Primary node (%s) has degraded storage,"
3728 0834c866 Iustin Pop
                                 " unsafe to replace the secondary" %
3729 0834c866 Iustin Pop
                                 pri_node)
3730 0834c866 Iustin Pop
3731 0834c866 Iustin Pop
    # Step: create new storage
3732 5bfac263 Iustin Pop
    self.proc.LogStep(3, steps_total, "allocate new storage")
3733 a9e0c397 Iustin Pop
    for dev in instance.disks:
3734 a9e0c397 Iustin Pop
      size = dev.size
3735 0834c866 Iustin Pop
      info("adding new local storage on %s for %s" % (new_node, dev.iv_name))
3736 a9e0c397 Iustin Pop
      # since we *always* want to create this LV, we use the
3737 a9e0c397 Iustin Pop
      # _Create...OnPrimary (which forces the creation), even if we
3738 a9e0c397 Iustin Pop
      # are talking about the secondary node
3739 a9e0c397 Iustin Pop
      for new_lv in dev.children:
3740 3f78eef2 Iustin Pop
        if not _CreateBlockDevOnPrimary(cfg, new_node, instance, new_lv,
3741 a9e0c397 Iustin Pop
                                        _GetInstanceInfoText(instance)):
3742 a9e0c397 Iustin Pop
          raise errors.OpExecError("Failed to create new LV named '%s' on"
3743 a9e0c397 Iustin Pop
                                   " node '%s'" %
3744 a9e0c397 Iustin Pop
                                   (new_lv.logical_id[1], new_node))
3745 a9e0c397 Iustin Pop
3746 0834c866 Iustin Pop
      iv_names[dev.iv_name] = (dev, dev.children)
3747 0834c866 Iustin Pop
3748 5bfac263 Iustin Pop
    self.proc.LogStep(4, steps_total, "changing drbd configuration")
3749 0834c866 Iustin Pop
    for dev in instance.disks:
3750 0834c866 Iustin Pop
      size = dev.size
3751 0834c866 Iustin Pop
      info("activating a new drbd on %s for %s" % (new_node, dev.iv_name))
3752 a9e0c397 Iustin Pop
      # create new devices on new_node
3753 a9e0c397 Iustin Pop
      new_drbd = objects.Disk(dev_type=constants.LD_DRBD8,
3754 a9e0c397 Iustin Pop
                              logical_id=(pri_node, new_node,
3755 a9e0c397 Iustin Pop
                                          dev.logical_id[2]),
3756 a9e0c397 Iustin Pop
                              children=dev.children)
3757 3f78eef2 Iustin Pop
      if not _CreateBlockDevOnSecondary(cfg, new_node, instance,
3758 3f78eef2 Iustin Pop
                                        new_drbd, False,
3759 a9e0c397 Iustin Pop
                                      _GetInstanceInfoText(instance)):
3760 a9e0c397 Iustin Pop
        raise errors.OpExecError("Failed to create new DRBD on"
3761 a9e0c397 Iustin Pop
                                 " node '%s'" % new_node)
3762 a9e0c397 Iustin Pop
3763 0834c866 Iustin Pop
    for dev in instance.disks:
3764 a9e0c397 Iustin Pop
      # we have new devices, shutdown the drbd on the old secondary
3765 0834c866 Iustin Pop
      info("shutting down drbd for %s on old node" % dev.iv_name)
3766 a9e0c397 Iustin Pop
      cfg.SetDiskID(dev, old_node)
3767 a9e0c397 Iustin Pop
      if not rpc.call_blockdev_shutdown(old_node, dev):
3768 0834c866 Iustin Pop
        warning("Failed to shutdown drbd for %s on old node" % dev.iv_name,
3769 0834c866 Iustin Pop
                "Please cleanup this device manuall as soon as possible")
3770 a9e0c397 Iustin Pop
3771 a9e0c397 Iustin Pop
      # we have new storage, we 'rename' the network on the primary
3772 0834c866 Iustin Pop
      info("switching primary drbd for %s to new secondary node" % dev.iv_name)
3773 a9e0c397 Iustin Pop
      cfg.SetDiskID(dev, pri_node)
3774 a9e0c397 Iustin Pop
      # rename to the ip of the new node
3775 a9e0c397 Iustin Pop
      new_uid = list(dev.physical_id)
3776 a9e0c397 Iustin Pop
      new_uid[2] = self.remote_node_info.secondary_ip
3777 a9e0c397 Iustin Pop
      rlist = [(dev, tuple(new_uid))]
3778 a9e0c397 Iustin Pop
      if not rpc.call_blockdev_rename(pri_node, rlist):
3779 0834c866 Iustin Pop
        raise errors.OpExecError("Can't detach & re-attach drbd %s on node"
3780 a9e0c397 Iustin Pop
                                 " %s from %s to %s" %
3781 a9e0c397 Iustin Pop
                                 (dev.iv_name, pri_node, old_node, new_node))
3782 a9e0c397 Iustin Pop
      dev.logical_id = (pri_node, new_node, dev.logical_id[2])
3783 a9e0c397 Iustin Pop
      cfg.SetDiskID(dev, pri_node)
3784 a9e0c397 Iustin Pop
      cfg.Update(instance)
3785 a9e0c397 Iustin Pop
3786 a9e0c397 Iustin Pop
3787 a9e0c397 Iustin Pop
    # this can fail as the old devices are degraded and _WaitForSync
3788 a9e0c397 Iustin Pop
    # does a combined result over all disks, so we don't check its
3789 a9e0c397 Iustin Pop
    # return value
3790 5bfac263 Iustin Pop
    self.proc.LogStep(5, steps_total, "sync devices")
3791 5bfac263 Iustin Pop
    _WaitForSync(cfg, instance, self.proc, unlock=True)
3792 a9e0c397 Iustin Pop
3793 a9e0c397 Iustin Pop
    # so check manually all the devices
3794 a9e0c397 Iustin Pop
    for name, (dev, old_lvs) in iv_names.iteritems():
3795 a9e0c397 Iustin Pop
      cfg.SetDiskID(dev, pri_node)
3796 a9e0c397 Iustin Pop
      is_degr = rpc.call_blockdev_find(pri_node, dev)[5]
3797 a9e0c397 Iustin Pop
      if is_degr:
3798 a9e0c397 Iustin Pop
        raise errors.OpExecError("DRBD device %s is degraded!" % name)
3799 a9e0c397 Iustin Pop
3800 5bfac263 Iustin Pop
    self.proc.LogStep(6, steps_total, "removing old storage")
3801 a9e0c397 Iustin Pop
    for name, (dev, old_lvs) in iv_names.iteritems():
3802 0834c866 Iustin Pop
      info("remove logical volumes for %s" % name)
3803 a9e0c397 Iustin Pop
      for lv in old_lvs:
3804 a9e0c397 Iustin Pop
        cfg.SetDiskID(lv, old_node)
3805 a9e0c397 Iustin Pop
        if not rpc.call_blockdev_remove(old_node, lv):
3806 0834c866 Iustin Pop
          warning("Can't remove LV on old secondary",
3807 0834c866 Iustin Pop
                  "Cleanup stale volumes by hand")
3808 a9e0c397 Iustin Pop
3809 a9e0c397 Iustin Pop
  def Exec(self, feedback_fn):
3810 a9e0c397 Iustin Pop
    """Execute disk replacement.
3811 a9e0c397 Iustin Pop

3812 a9e0c397 Iustin Pop
    This dispatches the disk replacement to the appropriate handler.
3813 a9e0c397 Iustin Pop

3814 a9e0c397 Iustin Pop
    """
3815 a9e0c397 Iustin Pop
    instance = self.instance
3816 a9e0c397 Iustin Pop
    if instance.disk_template == constants.DT_REMOTE_RAID1:
3817 a9e0c397 Iustin Pop
      fn = self._ExecRR1
3818 a9e0c397 Iustin Pop
    elif instance.disk_template == constants.DT_DRBD8:
3819 a9e0c397 Iustin Pop
      if self.op.remote_node is None:
3820 a9e0c397 Iustin Pop
        fn = self._ExecD8DiskOnly
3821 a9e0c397 Iustin Pop
      else:
3822 a9e0c397 Iustin Pop
        fn = self._ExecD8Secondary
3823 a9e0c397 Iustin Pop
    else:
3824 a9e0c397 Iustin Pop
      raise errors.ProgrammerError("Unhandled disk replacement case")
3825 a9e0c397 Iustin Pop
    return fn(feedback_fn)
3826 a9e0c397 Iustin Pop
3827 a8083063 Iustin Pop
3828 a8083063 Iustin Pop
class LUQueryInstanceData(NoHooksLU):
3829 a8083063 Iustin Pop
  """Query runtime instance data.
3830 a8083063 Iustin Pop

3831 a8083063 Iustin Pop
  """
3832 a8083063 Iustin Pop
  _OP_REQP = ["instances"]
3833 a8083063 Iustin Pop
3834 a8083063 Iustin Pop
  def CheckPrereq(self):
3835 a8083063 Iustin Pop
    """Check prerequisites.
3836 a8083063 Iustin Pop

3837 a8083063 Iustin Pop
    This only checks the optional instance list against the existing names.
3838 a8083063 Iustin Pop

3839 a8083063 Iustin Pop
    """
3840 a8083063 Iustin Pop
    if not isinstance(self.op.instances, list):
3841 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Invalid argument type 'instances'")
3842 a8083063 Iustin Pop
    if self.op.instances:
3843 a8083063 Iustin Pop
      self.wanted_instances = []
3844 a8083063 Iustin Pop
      names = self.op.instances
3845 a8083063 Iustin Pop
      for name in names:
3846 a8083063 Iustin Pop
        instance = self.cfg.GetInstanceInfo(self.cfg.ExpandInstanceName(name))
3847 a8083063 Iustin Pop
        if instance is None:
3848 3ecf6786 Iustin Pop
          raise errors.OpPrereqError("No such instance name '%s'" % name)
3849 a8083063 Iustin Pop
      self.wanted_instances.append(instance)
3850 a8083063 Iustin Pop
    else:
3851 a8083063 Iustin Pop
      self.wanted_instances = [self.cfg.GetInstanceInfo(name) for name
3852 a8083063 Iustin Pop
                               in self.cfg.GetInstanceList()]
3853 a8083063 Iustin Pop
    return
3854 a8083063 Iustin Pop
3855 a8083063 Iustin Pop
3856 a8083063 Iustin Pop
  def _ComputeDiskStatus(self, instance, snode, dev):
3857 a8083063 Iustin Pop
    """Compute block device status.
3858 a8083063 Iustin Pop

3859 a8083063 Iustin Pop
    """
3860 a8083063 Iustin Pop
    self.cfg.SetDiskID(dev, instance.primary_node)
3861 a8083063 Iustin Pop
    dev_pstatus = rpc.call_blockdev_find(instance.primary_node, dev)
3862 a1f445d3 Iustin Pop
    if dev.dev_type in constants.LDS_DRBD:
3863 a8083063 Iustin Pop
      # we change the snode then (otherwise we use the one passed in)
3864 a8083063 Iustin Pop
      if dev.logical_id[0] == instance.primary_node:
3865 a8083063 Iustin Pop
        snode = dev.logical_id[1]
3866 a8083063 Iustin Pop
      else:
3867 a8083063 Iustin Pop
        snode = dev.logical_id[0]
3868 a8083063 Iustin Pop
3869 a8083063 Iustin Pop
    if snode:
3870 a8083063 Iustin Pop
      self.cfg.SetDiskID(dev, snode)
3871 a8083063 Iustin Pop
      dev_sstatus = rpc.call_blockdev_find(snode, dev)
3872 a8083063 Iustin Pop
    else:
3873 a8083063 Iustin Pop
      dev_sstatus = None
3874 a8083063 Iustin Pop
3875 a8083063 Iustin Pop
    if dev.children:
3876 a8083063 Iustin Pop
      dev_children = [self._ComputeDiskStatus(instance, snode, child)
3877 a8083063 Iustin Pop
                      for child in dev.children]
3878 a8083063 Iustin Pop
    else:
3879 a8083063 Iustin Pop
      dev_children = []
3880 a8083063 Iustin Pop
3881 a8083063 Iustin Pop
    data = {
3882 a8083063 Iustin Pop
      "iv_name": dev.iv_name,
3883 a8083063 Iustin Pop
      "dev_type": dev.dev_type,
3884 a8083063 Iustin Pop
      "logical_id": dev.logical_id,
3885 a8083063 Iustin Pop
      "physical_id": dev.physical_id,
3886 a8083063 Iustin Pop
      "pstatus": dev_pstatus,
3887 a8083063 Iustin Pop
      "sstatus": dev_sstatus,
3888 a8083063 Iustin Pop
      "children": dev_children,
3889 a8083063 Iustin Pop
      }
3890 a8083063 Iustin Pop
3891 a8083063 Iustin Pop
    return data
3892 a8083063 Iustin Pop
3893 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
3894 a8083063 Iustin Pop
    """Gather and return data"""
3895 a8083063 Iustin Pop
    result = {}
3896 a8083063 Iustin Pop
    for instance in self.wanted_instances:
3897 a8083063 Iustin Pop
      remote_info = rpc.call_instance_info(instance.primary_node,
3898 a8083063 Iustin Pop
                                                instance.name)
3899 a8083063 Iustin Pop
      if remote_info and "state" in remote_info:
3900 a8083063 Iustin Pop
        remote_state = "up"
3901 a8083063 Iustin Pop
      else:
3902 a8083063 Iustin Pop
        remote_state = "down"
3903 a8083063 Iustin Pop
      if instance.status == "down":
3904 a8083063 Iustin Pop
        config_state = "down"
3905 a8083063 Iustin Pop
      else:
3906 a8083063 Iustin Pop
        config_state = "up"
3907 a8083063 Iustin Pop
3908 a8083063 Iustin Pop
      disks = [self._ComputeDiskStatus(instance, None, device)
3909 a8083063 Iustin Pop
               for device in instance.disks]
3910 a8083063 Iustin Pop
3911 a8083063 Iustin Pop
      idict = {
3912 a8083063 Iustin Pop
        "name": instance.name,
3913 a8083063 Iustin Pop
        "config_state": config_state,
3914 a8083063 Iustin Pop
        "run_state": remote_state,
3915 a8083063 Iustin Pop
        "pnode": instance.primary_node,
3916 a8083063 Iustin Pop
        "snodes": instance.secondary_nodes,
3917 a8083063 Iustin Pop
        "os": instance.os,
3918 a8083063 Iustin Pop
        "memory": instance.memory,
3919 a8083063 Iustin Pop
        "nics": [(nic.mac, nic.ip, nic.bridge) for nic in instance.nics],
3920 a8083063 Iustin Pop
        "disks": disks,
3921 f55ff7ec Iustin Pop
        "vcpus": instance.vcpus,
3922 a8083063 Iustin Pop
        }
3923 a8083063 Iustin Pop
3924 a8083063 Iustin Pop
      result[instance.name] = idict
3925 a8083063 Iustin Pop
3926 a8083063 Iustin Pop
    return result
3927 a8083063 Iustin Pop
3928 a8083063 Iustin Pop
3929 a8083063 Iustin Pop
class LUSetInstanceParms(LogicalUnit):
3930 a8083063 Iustin Pop
  """Modifies an instances's parameters.
3931 a8083063 Iustin Pop

3932 a8083063 Iustin Pop
  """
3933 a8083063 Iustin Pop
  HPATH = "instance-modify"
3934 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_INSTANCE
3935 a8083063 Iustin Pop
  _OP_REQP = ["instance_name"]
3936 a8083063 Iustin Pop
3937 a8083063 Iustin Pop
  def BuildHooksEnv(self):
3938 a8083063 Iustin Pop
    """Build hooks env.
3939 a8083063 Iustin Pop

3940 a8083063 Iustin Pop
    This runs on the master, primary and secondaries.
3941 a8083063 Iustin Pop

3942 a8083063 Iustin Pop
    """
3943 396e1b78 Michael Hanselmann
    args = dict()
3944 a8083063 Iustin Pop
    if self.mem:
3945 396e1b78 Michael Hanselmann
      args['memory'] = self.mem
3946 a8083063 Iustin Pop
    if self.vcpus:
3947 396e1b78 Michael Hanselmann
      args['vcpus'] = self.vcpus
3948 396e1b78 Michael Hanselmann
    if self.do_ip or self.do_bridge:
3949 396e1b78 Michael Hanselmann
      if self.do_ip:
3950 396e1b78 Michael Hanselmann
        ip = self.ip
3951 396e1b78 Michael Hanselmann
      else:
3952 396e1b78 Michael Hanselmann
        ip = self.instance.nics[0].ip
3953 396e1b78 Michael Hanselmann
      if self.bridge:
3954 396e1b78 Michael Hanselmann
        bridge = self.bridge
3955 396e1b78 Michael Hanselmann
      else:
3956 396e1b78 Michael Hanselmann
        bridge = self.instance.nics[0].bridge
3957 396e1b78 Michael Hanselmann
      args['nics'] = [(ip, bridge)]
3958 396e1b78 Michael Hanselmann
    env = _BuildInstanceHookEnvByObject(self.instance, override=args)
3959 880478f8 Iustin Pop
    nl = [self.sstore.GetMasterNode(),
3960 a8083063 Iustin Pop
          self.instance.primary_node] + list(self.instance.secondary_nodes)
3961 a8083063 Iustin Pop
    return env, nl, nl
3962 a8083063 Iustin Pop
3963 a8083063 Iustin Pop
  def CheckPrereq(self):
3964 a8083063 Iustin Pop
    """Check prerequisites.
3965 a8083063 Iustin Pop

3966 a8083063 Iustin Pop
    This only checks the instance list against the existing names.
3967 a8083063 Iustin Pop

3968 a8083063 Iustin Pop
    """
3969 a8083063 Iustin Pop
    self.mem = getattr(self.op, "mem", None)
3970 a8083063 Iustin Pop
    self.vcpus = getattr(self.op, "vcpus", None)
3971 a8083063 Iustin Pop
    self.ip = getattr(self.op, "ip", None)
3972 a8083063 Iustin Pop
    self.bridge = getattr(self.op, "bridge", None)
3973 a8083063 Iustin Pop
    if [self.mem, self.vcpus, self.ip, self.bridge].count(None) == 4:
3974 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("No changes submitted")
3975 a8083063 Iustin Pop
    if self.mem is not None:
3976 a8083063 Iustin Pop
      try:
3977 a8083063 Iustin Pop
        self.mem = int(self.mem)
3978 a8083063 Iustin Pop
      except ValueError, err:
3979 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Invalid memory size: %s" % str(err))
3980 a8083063 Iustin Pop
    if self.vcpus is not None:
3981 a8083063 Iustin Pop
      try:
3982 a8083063 Iustin Pop
        self.vcpus = int(self.vcpus)
3983 a8083063 Iustin Pop
      except ValueError, err:
3984 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Invalid vcpus number: %s" % str(err))
3985 a8083063 Iustin Pop
    if self.ip is not None:
3986 a8083063 Iustin Pop
      self.do_ip = True
3987 a8083063 Iustin Pop
      if self.ip.lower() == "none":
3988 a8083063 Iustin Pop
        self.ip = None
3989 a8083063 Iustin Pop
      else:
3990 a8083063 Iustin Pop
        if not utils.IsValidIP(self.ip):
3991 3ecf6786 Iustin Pop
          raise errors.OpPrereqError("Invalid IP address '%s'." % self.ip)
3992 a8083063 Iustin Pop
    else:
3993 a8083063 Iustin Pop
      self.do_ip = False
3994 ecb215b5 Michael Hanselmann
    self.do_bridge = (self.bridge is not None)
3995 a8083063 Iustin Pop
3996 a8083063 Iustin Pop
    instance = self.cfg.GetInstanceInfo(
3997 a8083063 Iustin Pop
      self.cfg.ExpandInstanceName(self.op.instance_name))
3998 a8083063 Iustin Pop
    if instance is None:
3999 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("No such instance name '%s'" %
4000 3ecf6786 Iustin Pop
                                 self.op.instance_name)
4001 a8083063 Iustin Pop
    self.op.instance_name = instance.name
4002 a8083063 Iustin Pop
    self.instance = instance
4003 a8083063 Iustin Pop
    return
4004 a8083063 Iustin Pop
4005 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
4006 a8083063 Iustin Pop
    """Modifies an instance.
4007 a8083063 Iustin Pop

4008 a8083063 Iustin Pop
    All parameters take effect only at the next restart of the instance.
4009 a8083063 Iustin Pop
    """
4010 a8083063 Iustin Pop
    result = []
4011 a8083063 Iustin Pop
    instance = self.instance
4012 a8083063 Iustin Pop
    if self.mem:
4013 a8083063 Iustin Pop
      instance.memory = self.mem
4014 a8083063 Iustin Pop
      result.append(("mem", self.mem))
4015 a8083063 Iustin Pop
    if self.vcpus:
4016 a8083063 Iustin Pop
      instance.vcpus = self.vcpus
4017 a8083063 Iustin Pop
      result.append(("vcpus",  self.vcpus))
4018 a8083063 Iustin Pop
    if self.do_ip:
4019 a8083063 Iustin Pop
      instance.nics[0].ip = self.ip
4020 a8083063 Iustin Pop
      result.append(("ip", self.ip))
4021 a8083063 Iustin Pop
    if self.bridge:
4022 a8083063 Iustin Pop
      instance.nics[0].bridge = self.bridge
4023 a8083063 Iustin Pop
      result.append(("bridge", self.bridge))
4024 a8083063 Iustin Pop
4025 a8083063 Iustin Pop
    self.cfg.AddInstance(instance)
4026 a8083063 Iustin Pop
4027 a8083063 Iustin Pop
    return result
4028 a8083063 Iustin Pop
4029 a8083063 Iustin Pop
4030 a8083063 Iustin Pop
class LUQueryExports(NoHooksLU):
4031 a8083063 Iustin Pop
  """Query the exports list
4032 a8083063 Iustin Pop

4033 a8083063 Iustin Pop
  """
4034 a8083063 Iustin Pop
  _OP_REQP = []
4035 a8083063 Iustin Pop
4036 a8083063 Iustin Pop
  def CheckPrereq(self):
4037 a8083063 Iustin Pop
    """Check that the nodelist contains only existing nodes.
4038 a8083063 Iustin Pop

4039 a8083063 Iustin Pop
    """
4040 dcb93971 Michael Hanselmann
    self.nodes = _GetWantedNodes(self, getattr(self.op, "nodes", None))
4041 a8083063 Iustin Pop
4042 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
4043 a8083063 Iustin Pop
    """Compute the list of all the exported system images.
4044 a8083063 Iustin Pop

4045 a8083063 Iustin Pop
    Returns:
4046 a8083063 Iustin Pop
      a dictionary with the structure node->(export-list)
4047 a8083063 Iustin Pop
      where export-list is a list of the instances exported on
4048 a8083063 Iustin Pop
      that node.
4049 a8083063 Iustin Pop

4050 a8083063 Iustin Pop
    """
4051 a7ba5e53 Iustin Pop
    return rpc.call_export_list(self.nodes)
4052 a8083063 Iustin Pop
4053 a8083063 Iustin Pop
4054 a8083063 Iustin Pop
class LUExportInstance(LogicalUnit):
4055 a8083063 Iustin Pop
  """Export an instance to an image in the cluster.
4056 a8083063 Iustin Pop

4057 a8083063 Iustin Pop
  """
4058 a8083063 Iustin Pop
  HPATH = "instance-export"
4059 a8083063 Iustin Pop
  HTYPE = constants.HTYPE_INSTANCE
4060 a8083063 Iustin Pop
  _OP_REQP = ["instance_name", "target_node", "shutdown"]
4061 a8083063 Iustin Pop
4062 a8083063 Iustin Pop
  def BuildHooksEnv(self):
4063 a8083063 Iustin Pop
    """Build hooks env.
4064 a8083063 Iustin Pop

4065 a8083063 Iustin Pop
    This will run on the master, primary node and target node.
4066 a8083063 Iustin Pop

4067 a8083063 Iustin Pop
    """
4068 a8083063 Iustin Pop
    env = {
4069 a8083063 Iustin Pop
      "EXPORT_NODE": self.op.target_node,
4070 a8083063 Iustin Pop
      "EXPORT_DO_SHUTDOWN": self.op.shutdown,
4071 a8083063 Iustin Pop
      }
4072 396e1b78 Michael Hanselmann
    env.update(_BuildInstanceHookEnvByObject(self.instance))
4073 880478f8 Iustin Pop
    nl = [self.sstore.GetMasterNode(), self.instance.primary_node,
4074 a8083063 Iustin Pop
          self.op.target_node]
4075 a8083063 Iustin Pop
    return env, nl, nl
4076 a8083063 Iustin Pop
4077 a8083063 Iustin Pop
  def CheckPrereq(self):
4078 a8083063 Iustin Pop
    """Check prerequisites.
4079 a8083063 Iustin Pop

4080 a8083063 Iustin Pop
    This checks that the instance name is a valid one.
4081 a8083063 Iustin Pop

4082 a8083063 Iustin Pop
    """
4083 a8083063 Iustin Pop
    instance_name = self.cfg.ExpandInstanceName(self.op.instance_name)
4084 a8083063 Iustin Pop
    self.instance = self.cfg.GetInstanceInfo(instance_name)
4085 a8083063 Iustin Pop
    if self.instance is None:
4086 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Instance '%s' not found" %
4087 3ecf6786 Iustin Pop
                                 self.op.instance_name)
4088 a8083063 Iustin Pop
4089 a8083063 Iustin Pop
    # node verification
4090 a8083063 Iustin Pop
    dst_node_short = self.cfg.ExpandNodeName(self.op.target_node)
4091 a8083063 Iustin Pop
    self.dst_node = self.cfg.GetNodeInfo(dst_node_short)
4092 a8083063 Iustin Pop
4093 a8083063 Iustin Pop
    if self.dst_node is None:
4094 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Destination node '%s' is unknown." %
4095 3ecf6786 Iustin Pop
                                 self.op.target_node)
4096 a8083063 Iustin Pop
    self.op.target_node = self.dst_node.name
4097 a8083063 Iustin Pop
4098 a8083063 Iustin Pop
  def Exec(self, feedback_fn):
4099 a8083063 Iustin Pop
    """Export an instance to an image in the cluster.
4100 a8083063 Iustin Pop

4101 a8083063 Iustin Pop
    """
4102 a8083063 Iustin Pop
    instance = self.instance
4103 a8083063 Iustin Pop
    dst_node = self.dst_node
4104 a8083063 Iustin Pop
    src_node = instance.primary_node
4105 a8083063 Iustin Pop
    # shutdown the instance, unless requested not to do so
4106 a8083063 Iustin Pop
    if self.op.shutdown:
4107 a8083063 Iustin Pop
      op = opcodes.OpShutdownInstance(instance_name=instance.name)
4108 5bfac263 Iustin Pop
      self.proc.ChainOpCode(op)
4109 a8083063 Iustin Pop
4110 a8083063 Iustin Pop
    vgname = self.cfg.GetVGName()
4111 a8083063 Iustin Pop
4112 a8083063 Iustin Pop
    snap_disks = []
4113 a8083063 Iustin Pop
4114 a8083063 Iustin Pop
    try:
4115 a8083063 Iustin Pop
      for disk in instance.disks:
4116 a8083063 Iustin Pop
        if disk.iv_name == "sda":
4117 a8083063 Iustin Pop
          # new_dev_name will be a snapshot of an lvm leaf of the one we passed
4118 a8083063 Iustin Pop
          new_dev_name = rpc.call_blockdev_snapshot(src_node, disk)
4119 a8083063 Iustin Pop
4120 a8083063 Iustin Pop
          if not new_dev_name:
4121 a8083063 Iustin Pop
            logger.Error("could not snapshot block device %s on node %s" %
4122 a8083063 Iustin Pop
                         (disk.logical_id[1], src_node))
4123 a8083063 Iustin Pop
          else:
4124 fe96220b Iustin Pop
            new_dev = objects.Disk(dev_type=constants.LD_LV, size=disk.size,
4125 a8083063 Iustin Pop
                                      logical_id=(vgname, new_dev_name),
4126 a8083063 Iustin Pop
                                      physical_id=(vgname, new_dev_name),
4127 a8083063 Iustin Pop
                                      iv_name=disk.iv_name)
4128 a8083063 Iustin Pop
            snap_disks.append(new_dev)
4129 a8083063 Iustin Pop
4130 a8083063 Iustin Pop
    finally:
4131 a8083063 Iustin Pop
      if self.op.shutdown:
4132 a8083063 Iustin Pop
        op = opcodes.OpStartupInstance(instance_name=instance.name,
4133 a8083063 Iustin Pop
                                       force=False)
4134 5bfac263 Iustin Pop
        self.proc.ChainOpCode(op)
4135 a8083063 Iustin Pop
4136 a8083063 Iustin Pop
    # TODO: check for size
4137 a8083063 Iustin Pop
4138 a8083063 Iustin Pop
    for dev in snap_disks:
4139 a8083063 Iustin Pop
      if not rpc.call_snapshot_export(src_node, dev, dst_node.name,
4140 a8083063 Iustin Pop
                                           instance):
4141 a8083063 Iustin Pop
        logger.Error("could not export block device %s from node"
4142 a8083063 Iustin Pop
                     " %s to node %s" %
4143 a8083063 Iustin Pop
                     (dev.logical_id[1], src_node, dst_node.name))
4144 a8083063 Iustin Pop
      if not rpc.call_blockdev_remove(src_node, dev):
4145 a8083063 Iustin Pop
        logger.Error("could not remove snapshot block device %s from"
4146 a8083063 Iustin Pop
                     " node %s" % (dev.logical_id[1], src_node))
4147 a8083063 Iustin Pop
4148 a8083063 Iustin Pop
    if not rpc.call_finalize_export(dst_node.name, instance, snap_disks):
4149 a8083063 Iustin Pop
      logger.Error("could not finalize export for instance %s on node %s" %
4150 a8083063 Iustin Pop
                   (instance.name, dst_node.name))
4151 a8083063 Iustin Pop
4152 a8083063 Iustin Pop
    nodelist = self.cfg.GetNodeList()
4153 a8083063 Iustin Pop
    nodelist.remove(dst_node.name)
4154 a8083063 Iustin Pop
4155 a8083063 Iustin Pop
    # on one-node clusters nodelist will be empty after the removal
4156 a8083063 Iustin Pop
    # if we proceed the backup would be removed because OpQueryExports
4157 a8083063 Iustin Pop
    # substitutes an empty list with the full cluster node list.
4158 a8083063 Iustin Pop
    if nodelist:
4159 a8083063 Iustin Pop
      op = opcodes.OpQueryExports(nodes=nodelist)
4160 5bfac263 Iustin Pop
      exportlist = self.proc.ChainOpCode(op)
4161 a8083063 Iustin Pop
      for node in exportlist:
4162 a8083063 Iustin Pop
        if instance.name in exportlist[node]:
4163 a8083063 Iustin Pop
          if not rpc.call_export_remove(node, instance.name):
4164 a8083063 Iustin Pop
            logger.Error("could not remove older export for instance %s"
4165 a8083063 Iustin Pop
                         " on node %s" % (instance.name, node))
4166 5c947f38 Iustin Pop
4167 5c947f38 Iustin Pop
4168 5c947f38 Iustin Pop
class TagsLU(NoHooksLU):
4169 5c947f38 Iustin Pop
  """Generic tags LU.
4170 5c947f38 Iustin Pop

4171 5c947f38 Iustin Pop
  This is an abstract class which is the parent of all the other tags LUs.
4172 5c947f38 Iustin Pop

4173 5c947f38 Iustin Pop
  """
4174 5c947f38 Iustin Pop
  def CheckPrereq(self):
4175 5c947f38 Iustin Pop
    """Check prerequisites.
4176 5c947f38 Iustin Pop

4177 5c947f38 Iustin Pop
    """
4178 5c947f38 Iustin Pop
    if self.op.kind == constants.TAG_CLUSTER:
4179 5c947f38 Iustin Pop
      self.target = self.cfg.GetClusterInfo()
4180 5c947f38 Iustin Pop
    elif self.op.kind == constants.TAG_NODE:
4181 5c947f38 Iustin Pop
      name = self.cfg.ExpandNodeName(self.op.name)
4182 5c947f38 Iustin Pop
      if name is None:
4183 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Invalid node name (%s)" %
4184 3ecf6786 Iustin Pop
                                   (self.op.name,))
4185 5c947f38 Iustin Pop
      self.op.name = name
4186 5c947f38 Iustin Pop
      self.target = self.cfg.GetNodeInfo(name)
4187 5c947f38 Iustin Pop
    elif self.op.kind == constants.TAG_INSTANCE:
4188 8f684e16 Iustin Pop
      name = self.cfg.ExpandInstanceName(self.op.name)
4189 5c947f38 Iustin Pop
      if name is None:
4190 3ecf6786 Iustin Pop
        raise errors.OpPrereqError("Invalid instance name (%s)" %
4191 3ecf6786 Iustin Pop
                                   (self.op.name,))
4192 5c947f38 Iustin Pop
      self.op.name = name
4193 5c947f38 Iustin Pop
      self.target = self.cfg.GetInstanceInfo(name)
4194 5c947f38 Iustin Pop
    else:
4195 3ecf6786 Iustin Pop
      raise errors.OpPrereqError("Wrong tag type requested (%s)" %
4196 3ecf6786 Iustin Pop
                                 str(self.op.kind))
4197 5c947f38 Iustin Pop
4198 5c947f38 Iustin Pop
4199 5c947f38 Iustin Pop
class LUGetTags(TagsLU):
4200 5c947f38 Iustin Pop
  """Returns the tags of a given object.
4201 5c947f38 Iustin Pop

4202 5c947f38 Iustin Pop
  """
4203 5c947f38 Iustin Pop
  _OP_REQP = ["kind", "name"]
4204 5c947f38 Iustin Pop
4205 5c947f38 Iustin Pop
  def Exec(self, feedback_fn):
4206 5c947f38 Iustin Pop
    """Returns the tag list.
4207 5c947f38 Iustin Pop

4208 5c947f38 Iustin Pop
    """
4209 5c947f38 Iustin Pop
    return self.target.GetTags()
4210 5c947f38 Iustin Pop
4211 5c947f38 Iustin Pop
4212 73415719 Iustin Pop
class LUSearchTags(NoHooksLU):
4213 73415719 Iustin Pop
  """Searches the tags for a given pattern.
4214 73415719 Iustin Pop

4215 73415719 Iustin Pop
  """
4216 73415719 Iustin Pop
  _OP_REQP = ["pattern"]
4217 73415719 Iustin Pop
4218 73415719 Iustin Pop
  def CheckPrereq(self):
4219 73415719 Iustin Pop
    """Check prerequisites.
4220 73415719 Iustin Pop

4221 73415719 Iustin Pop
    This checks the pattern passed for validity by compiling it.
4222 73415719 Iustin Pop

4223 73415719 Iustin Pop
    """
4224 73415719 Iustin Pop
    try:
4225 73415719 Iustin Pop
      self.re = re.compile(self.op.pattern)
4226 73415719 Iustin Pop
    except re.error, err:
4227 73415719 Iustin Pop
      raise errors.OpPrereqError("Invalid search pattern '%s': %s" %
4228 73415719 Iustin Pop
                                 (self.op.pattern, err))
4229 73415719 Iustin Pop
4230 73415719 Iustin Pop
  def Exec(self, feedback_fn):
4231 73415719 Iustin Pop
    """Returns the tag list.
4232 73415719 Iustin Pop

4233 73415719 Iustin Pop
    """
4234 73415719 Iustin Pop
    cfg = self.cfg
4235 73415719 Iustin Pop
    tgts = [("/cluster", cfg.GetClusterInfo())]
4236 73415719 Iustin Pop
    ilist = [cfg.GetInstanceInfo(name) for name in cfg.GetInstanceList()]
4237 73415719 Iustin Pop
    tgts.extend([("/instances/%s" % i.name, i) for i in ilist])
4238 73415719 Iustin Pop
    nlist = [cfg.GetNodeInfo(name) for name in cfg.GetNodeList()]
4239 73415719 Iustin Pop
    tgts.extend([("/nodes/%s" % n.name, n) for n in nlist])
4240 73415719 Iustin Pop
    results = []
4241 73415719 Iustin Pop
    for path, target in tgts:
4242 73415719 Iustin Pop
      for tag in target.GetTags():
4243 73415719 Iustin Pop
        if self.re.search(tag):
4244 73415719 Iustin Pop
          results.append((path, tag))
4245 73415719 Iustin Pop
    return results
4246 73415719 Iustin Pop
4247 73415719 Iustin Pop
4248 f27302fa Iustin Pop
class LUAddTags(TagsLU):
4249 5c947f38 Iustin Pop
  """Sets a tag on a given object.
4250 5c947f38 Iustin Pop

4251 5c947f38 Iustin Pop
  """
4252 f27302fa Iustin Pop
  _OP_REQP = ["kind", "name", "tags"]
4253 5c947f38 Iustin Pop
4254 5c947f38 Iustin Pop
  def CheckPrereq(self):
4255 5c947f38 Iustin Pop
    """Check prerequisites.
4256 5c947f38 Iustin Pop

4257 5c947f38 Iustin Pop
    This checks the type and length of the tag name and value.
4258 5c947f38 Iustin Pop

4259 5c947f38 Iustin Pop
    """
4260 5c947f38 Iustin Pop
    TagsLU.CheckPrereq(self)
4261 f27302fa Iustin Pop
    for tag in self.op.tags:
4262 f27302fa Iustin Pop
      objects.TaggableObject.ValidateTag(tag)
4263 5c947f38 Iustin Pop
4264 5c947f38 Iustin Pop
  def Exec(self, feedback_fn):
4265 5c947f38 Iustin Pop
    """Sets the tag.
4266 5c947f38 Iustin Pop

4267 5c947f38 Iustin Pop
    """
4268 5c947f38 Iustin Pop
    try:
4269 f27302fa Iustin Pop
      for tag in self.op.tags:
4270 f27302fa Iustin Pop
        self.target.AddTag(tag)
4271 5c947f38 Iustin Pop
    except errors.TagError, err:
4272 3ecf6786 Iustin Pop
      raise errors.OpExecError("Error while setting tag: %s" % str(err))
4273 5c947f38 Iustin Pop
    try:
4274 5c947f38 Iustin Pop
      self.cfg.Update(self.target)
4275 5c947f38 Iustin Pop
    except errors.ConfigurationError:
4276 3ecf6786 Iustin Pop
      raise errors.OpRetryError("There has been a modification to the"
4277 3ecf6786 Iustin Pop
                                " config file and the operation has been"
4278 3ecf6786 Iustin Pop
                                " aborted. Please retry.")
4279 5c947f38 Iustin Pop
4280 5c947f38 Iustin Pop
4281 f27302fa Iustin Pop
class LUDelTags(TagsLU):
4282 f27302fa Iustin Pop
  """Delete a list of tags from a given object.
4283 5c947f38 Iustin Pop

4284 5c947f38 Iustin Pop
  """
4285 f27302fa Iustin Pop
  _OP_REQP = ["kind", "name", "tags"]
4286 5c947f38 Iustin Pop
4287 5c947f38 Iustin Pop
  def CheckPrereq(self):
4288 5c947f38 Iustin Pop
    """Check prerequisites.
4289 5c947f38 Iustin Pop

4290 5c947f38 Iustin Pop
    This checks that we have the given tag.
4291 5c947f38 Iustin Pop

4292 5c947f38 Iustin Pop
    """
4293 5c947f38 Iustin Pop
    TagsLU.CheckPrereq(self)
4294 f27302fa Iustin Pop
    for tag in self.op.tags:
4295 f27302fa Iustin Pop
      objects.TaggableObject.ValidateTag(tag)
4296 f27302fa Iustin Pop
    del_tags = frozenset(self.op.tags)
4297 f27302fa Iustin Pop
    cur_tags = self.target.GetTags()
4298 f27302fa Iustin Pop
    if not del_tags <= cur_tags:
4299 f27302fa Iustin Pop
      diff_tags = del_tags - cur_tags
4300 f27302fa Iustin Pop
      diff_names = ["'%s'" % tag for tag in diff_tags]
4301 f27302fa Iustin Pop
      diff_names.sort()
4302 f27302fa Iustin Pop
      raise errors.OpPrereqError("Tag(s) %s not found" %
4303 f27302fa Iustin Pop
                                 (",".join(diff_names)))
4304 5c947f38 Iustin Pop
4305 5c947f38 Iustin Pop
  def Exec(self, feedback_fn):
4306 5c947f38 Iustin Pop
    """Remove the tag from the object.
4307 5c947f38 Iustin Pop

4308 5c947f38 Iustin Pop
    """
4309 f27302fa Iustin Pop
    for tag in self.op.tags:
4310 f27302fa Iustin Pop
      self.target.RemoveTag(tag)
4311 5c947f38 Iustin Pop
    try:
4312 5c947f38 Iustin Pop
      self.cfg.Update(self.target)
4313 5c947f38 Iustin Pop
    except errors.ConfigurationError:
4314 3ecf6786 Iustin Pop
      raise errors.OpRetryError("There has been a modification to the"
4315 3ecf6786 Iustin Pop
                                " config file and the operation has been"
4316 3ecf6786 Iustin Pop
                                " aborted. Please retry.")