Esempio n. 1
0
def BuildInstanceHookEnvByObject(lu,
                                 instance,
                                 secondary_nodes=None,
                                 disks=None,
                                 override=None):
    """Builds instance related env variables for hooks from an object.

  @type lu: L{LogicalUnit}
  @param lu: the logical unit on whose behalf we execute
  @type instance: L{objects.Instance}
  @param instance: the instance for which we should build the
      environment
  @type override: dict
  @param override: dictionary with key/values that will override
      our values
  @rtype: dict
  @return: the hook environment dictionary

  """
    cluster = lu.cfg.GetClusterInfo()
    bep = cluster.FillBE(instance)
    hvp = cluster.FillHV(instance)

    # Override secondary_nodes
    if secondary_nodes is None:
        secondary_nodes = lu.cfg.GetInstanceSecondaryNodes(instance.uuid)

    # Override disks
    if disks is None:
        disks = lu.cfg.GetInstanceDisks(instance.uuid)

    disk_template = utils.GetDiskTemplate(disks)

    args = {
        "name": instance.name,
        "primary_node_name": lu.cfg.GetNodeName(instance.primary_node),
        "secondary_node_names": lu.cfg.GetNodeNames(secondary_nodes),
        "os_type": instance.os,
        "status": instance.admin_state,
        "maxmem": bep[constants.BE_MAXMEM],
        "minmem": bep[constants.BE_MINMEM],
        "vcpus": bep[constants.BE_VCPUS],
        "nics": NICListToTuple(lu, instance.nics),
        "disk_template": disk_template,
        "disks": disks,
        "bep": bep,
        "hvp": hvp,
        "hypervisor_name": instance.hypervisor,
        "tags": instance.tags,
    }
    if override:
        args.update(override)
    return BuildInstanceHookEnv(**args)
    def Exec(self, feedback_fn):
        """Gather and return data"""
        result = {}

        cluster = self.cfg.GetClusterInfo()

        node_uuids = itertools.chain(*(self.cfg.GetInstanceNodes(i.uuid)
                                       for i in self.wanted_instances))
        nodes = dict(self.cfg.GetMultiNodeInfo(node_uuids))

        groups = dict(
            self.cfg.GetMultiNodeGroupInfo(node.group
                                           for node in nodes.values()))

        for instance in self.wanted_instances:
            pnode = nodes[instance.primary_node]
            hvparams = cluster.FillHV(instance, skip_globals=True)

            if self.op.static or pnode.offline:
                remote_state = None
                if pnode.offline:
                    self.LogWarning(
                        "Primary node %s is marked offline, returning static"
                        " information only for instance %s" %
                        (pnode.name, instance.name))
            else:
                remote_info = self.rpc.call_instance_info(
                    instance.primary_node, instance.name, instance.hypervisor,
                    cluster.hvparams[instance.hypervisor])
                remote_info.Raise("Error checking node %s" % pnode.name)
                remote_info = remote_info.payload

                allow_userdown = \
                    cluster.enabled_user_shutdown and \
                    (instance.hypervisor != constants.HT_KVM or
                     hvparams[constants.HV_KVM_USER_SHUTDOWN])

                if remote_info and "state" in remote_info:
                    if hv_base.HvInstanceState.IsShutdown(
                            remote_info["state"]):
                        if allow_userdown:
                            remote_state = "user down"
                        else:
                            remote_state = "down"
                    else:
                        remote_state = "up"
                else:
                    if instance.admin_state == constants.ADMINST_UP:
                        remote_state = "down"
                    elif instance.admin_state == constants.ADMINST_DOWN:
                        if instance.admin_state_source == constants.USER_SOURCE:
                            remote_state = "user down"
                        else:
                            remote_state = "down"
                    else:
                        remote_state = "offline"

            group2name_fn = lambda uuid: groups[uuid].name
            node_uuid2name_fn = lambda uuid: nodes[uuid].name

            disk_objects = self.cfg.GetInstanceDisks(instance.uuid)
            output_disks = map(
                compat.partial(self._ComputeDiskStatus, instance,
                               node_uuid2name_fn), disk_objects)

            secondary_nodes = self.cfg.GetInstanceSecondaryNodes(instance.uuid)
            snodes_group_uuids = [
                nodes[snode_uuid].group for snode_uuid in secondary_nodes
            ]

            result[instance.name] = {
                "name": instance.name,
                "config_state": instance.admin_state,
                "run_state": remote_state,
                "pnode": pnode.name,
                "pnode_group_uuid": pnode.group,
                "pnode_group_name": group2name_fn(pnode.group),
                "snodes": map(node_uuid2name_fn, secondary_nodes),
                "snodes_group_uuids": snodes_group_uuids,
                "snodes_group_names": map(group2name_fn, snodes_group_uuids),
                "os": instance.os,
                # this happens to be the same format used for hooks
                "nics": NICListToTuple(self, instance.nics),
                "disk_template": utils.GetDiskTemplate(disk_objects),
                "disks": output_disks,
                "hypervisor": instance.hypervisor,
                "network_port": instance.network_port,
                "hv_instance": instance.hvparams,
                "hv_actual": hvparams,
                "be_instance": instance.beparams,
                "be_actual": cluster.FillBE(instance),
                "os_instance": instance.osparams,
                "os_actual": cluster.SimpleFillOS(instance.os,
                                                  instance.osparams),
                "serial_no": instance.serial_no,
                "mtime": instance.mtime,
                "ctime": instance.ctime,
                "uuid": instance.uuid,
            }

        return result
Esempio n. 3
0
 def testMixed(self):
     self.assertEqual(utils.GetDiskTemplate([Rbd(), Drbd()]),
                      constants.DT_MIXED)
Esempio n. 4
0
 def testMultiple(self):
     self.assertEqual(utils.GetDiskTemplate([Rbd(), Rbd()]),
                      constants.DT_RBD)
Esempio n. 5
0
 def testDiskless(self):
     self.assertEqual(utils.GetDiskTemplate([]), constants.DT_DISKLESS)
Esempio n. 6
0
 def testUnique(self):
     self.assertEqual(utils.GetDiskTemplate([Rbd()]), constants.DT_RBD)