Revision decd5f45 lib/cmdlib.py

b/lib/cmdlib.py
2034 2034
      _ShutdownInstanceDisks(inst, self.cfg)
2035 2035

  
2036 2036

  
2037
class LURenameInstance(LogicalUnit):
2038
  """Rename an instance.
2039

  
2040
  """
2041
  HPATH = "instance-rename"
2042
  HTYPE = constants.HTYPE_INSTANCE
2043
  _OP_REQP = ["instance_name", "new_name"]
2044

  
2045
  def BuildHooksEnv(self):
2046
    """Build hooks env.
2047

  
2048
    This runs on master, primary and secondary nodes of the instance.
2049

  
2050
    """
2051
    env = _BuildInstanceHookEnvByObject(self.instance)
2052
    env["INSTANCE_NEW_NAME"] = self.op.new_name
2053
    nl = ([self.sstore.GetMasterNode(), self.instance.primary_node] +
2054
          list(self.instance.secondary_nodes))
2055
    return env, nl, nl
2056

  
2057
  def CheckPrereq(self):
2058
    """Check prerequisites.
2059

  
2060
    This checks that the instance is in the cluster and is not running.
2061

  
2062
    """
2063
    instance = self.cfg.GetInstanceInfo(
2064
      self.cfg.ExpandInstanceName(self.op.instance_name))
2065
    if instance is None:
2066
      raise errors.OpPrereqError("Instance '%s' not known" %
2067
                                 self.op.instance_name)
2068
    if instance.status != "down":
2069
      raise errors.OpPrereqError("Instance '%s' is marked to be up" %
2070
                                 self.op.instance_name)
2071
    remote_info = rpc.call_instance_info(instance.primary_node, instance.name)
2072
    if remote_info:
2073
      raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
2074
                                 (self.op.instance_name,
2075
                                  instance.primary_node))
2076
    self.instance = instance
2077

  
2078
    # new name verification
2079
    hostname1 = utils.LookupHostname(self.op.new_name)
2080
    if not hostname1:
2081
      raise errors.OpPrereqError("New instance name '%s' not found in dns" %
2082
                                 self.op.new_name)
2083

  
2084
    self.op.new_name = new_name = hostname1['hostname']
2085
    if not getattr(self.op, "ignore_ip", False):
2086
      command = ["fping", "-q", hostname1['ip']]
2087
      result = utils.RunCmd(command)
2088
      if not result.failed:
2089
        raise errors.OpPrereqError("IP %s of instance %s already in use" %
2090
                                   (hostname1['ip'], new_name))
2091

  
2092

  
2093
  def Exec(self, feedback_fn):
2094
    """Reinstall the instance.
2095

  
2096
    """
2097
    inst = self.instance
2098
    old_name = inst.name
2099

  
2100
    self.cfg.RenameInstance(inst.name, self.op.new_name)
2101

  
2102
    # re-read the instance from the configuration after rename
2103
    inst = self.cfg.GetInstanceInfo(self.op.new_name)
2104

  
2105
    _StartInstanceDisks(self.cfg, inst, None)
2106
    try:
2107
      if not rpc.call_instance_run_rename(inst.primary_node, inst, old_name,
2108
                                          "sda", "sdb"):
2109
        msg = ("Could run OS rename script for instance %s\n"
2110
               "on node %s\n"
2111
               "(but the instance has been renamed in Ganeti)" %
2112
               (inst.name, inst.primary_node))
2113
        logger.Error(msg)
2114
    finally:
2115
      _ShutdownInstanceDisks(inst, self.cfg)
2116

  
2117

  
2037 2118
class LURemoveInstance(LogicalUnit):
2038 2119
  """Remove an instance.
2039 2120

  

Also available in: Unified diff