示例#1
0
 def _build_first_history(self,
                          status_name,
                          size,
                          start_date,
                          end_date=None,
                          first_update=False,
                          activity=None):
     #FIXME: Move this call so that it happens inside InstanceStatusHistory to avoid circ.dep.
     from core.models import InstanceStatusHistory
     if not first_update and status_name not in [
             'build', 'pending', 'running'
     ]:
         logger.info("First Update Unknown - Status name on instance \
                     %s: %s - %s" % (self.provider_alias, status_name))
         # Instance state is 'unknown' from start of instance until now
         # NOTE: This is needed to prevent over-charging accounts
         status_name = 'unknown'
         activity = None
     first_history = InstanceStatusHistory.create_history(
         status_name,
         self,
         size,
         start_date=start_date,
         end_date=end_date,
         activity=activity)
     first_history.save()
     return first_history
示例#2
0
 def first_transaction(self):
     history = InstanceStatusHistory.create_history(self.status_name,
                                                    self.instance,
                                                    self.size,
                                                    self.start_date)
     history.save()
     return history
def _resolve_history_conflict(
    identity, core_running_instance, bad_history, reset_time=None
):
    """
    NOTE 1: This is a 'band-aid' fix until we are 100% that Transaction will
            not create conflicting un-end-dated objects.

    NOTE 2: It is EXPECTED that this instance has the 'esh' attribute
            Failure to add the 'esh' attribute will generate a ValueError!
    """
    if not getattr(core_running_instance, 'esh'):
        raise ValueError("Esh is missing from %s" % core_running_instance)
    esh_instance = core_running_instance.esh

    # Check for temporary status and fetch that
    tmp_status = esh_instance.extra.get('metadata', {}).get("tmp_status")
    new_status = tmp_status or esh_instance.extra['status']

    esh_driver = get_cached_driver(identity=identity)
    new_size = _esh_instance_size_to_core(
        esh_driver, esh_instance, identity.provider.uuid
    )
    if not reset_time:
        reset_time = timezone.now()
    for history in bad_history:
        history.end_date = reset_time
        history.save()
    new_history = InstanceStatusHistory.create_history(
        new_status, core_running_instance, new_size, reset_time
    )
    return new_history
示例#4
0
def _resolve_history_conflict(identity,
                              core_running_instance,
                              bad_history,
                              reset_time=None):
    """
    NOTE 1: This is a 'band-aid' fix until we are 100% that Transaction will
            not create conflicting un-end-dated objects.

    NOTE 2: It is EXPECTED that this instance has the 'esh' attribute
            Failure to add the 'esh' attribute will generate a ValueError!
    """
    if not getattr(core_running_instance, 'esh'):
        raise ValueError("Esh is missing from %s" % core_running_instance)
    esh_instance = core_running_instance.esh

    # Check for temporary status and fetch that
    tmp_status = esh_instance.extra.get('metadata', {}).get("tmp_status")
    new_status = tmp_status or esh_instance.extra['status']

    esh_driver = get_cached_driver(identity=identity)
    new_size = _esh_instance_size_to_core(esh_driver, esh_instance,
                                          identity.provider.uuid)
    if not reset_time:
        reset_time = timezone.now()
    for history in bad_history:
        history.end_date = reset_time
        history.save()
    new_history = InstanceStatusHistory.create_history(new_status,
                                                       core_running_instance,
                                                       new_size, reset_time)
    return new_history
 def _build_first_history(
     self,
     status_name,
     size,
     start_date,
     end_date=None,
     first_update=False,
     activity=None
 ):
     # FIXME: Move this call so that it happens inside InstanceStatusHistory to avoid circ.dep.
     from core.models import InstanceStatusHistory
     if not first_update and status_name not in [
         'build', 'pending', 'running'
     ]:
         logger.info(
             "First Update Unknown - Status name on instance %s: %s",
             self.provider_alias, status_name
         )
         # Instance state is 'unknown' from start of instance until now
         # NOTE: This is needed to prevent over-charging accounts
         status_name = 'unknown'
         activity = None
     first_history = InstanceStatusHistory.create_history(
         status_name,
         self,
         size,
         start_date=start_date,
         end_date=end_date,
         activity=activity
     )
     first_history.save()
     return first_history
示例#6
0
 def new_transaction(self):
     return InstanceStatusHistory.transaction(
         self.status_name,
         "dummy-activity",
         self.instance,
         self.size,
         start_time=self.start_date
     )
示例#7
0
 def new_transaction(self):
     return InstanceStatusHistory.transaction(self.status_name,
                                              self.instance, self.size,
                                              self.start_date)
示例#8
0
 def update_history(self,
                    status_name,
                    size,
                    task=None,
                    tmp_status=None,
                    first_update=False):
     """
     Given the status name and size, look up the previous history object
     If nothing has changed: return (False, last_history)
     else: end date previous history object, start new history object.
           return (True, new_history)
     """
     #FIXME: Move this call so that it happens inside InstanceStatusHistory to avoid circ.dep.
     from core.models import InstanceStatusHistory
     import traceback
     # 1. Get status name
     status_name = _get_status_name_for_provider(self.source.provider,
                                                 status_name, task,
                                                 tmp_status)
     activity = self.esh_activity()
     # 2. Get the last history (or Build a new one if no other exists)
     last_history = self.get_last_history()
     if not last_history:
         last_history = InstanceStatusHistory.create_history(
             status_name,
             self,
             size,
             start_date=self.start_date,
             activity=activity)
         last_history.save()
         logger.debug(
             "STATUSUPDATE - FIRST - Instance:%s Old Status: %s - %s New\
             Status: %s Tmp Status: %s" %
             (self.provider_alias, self.esh_status(), self.esh_activity(),
              status_name, tmp_status))
         logger.debug("STATUSUPDATE - Traceback: %s" %
                      traceback.format_stack())
     # 2. Size and name must match to continue using last history
     if last_history.status.name == status_name \
             and last_history.size.id == size.id:
         # logger.info("status_name matches last history:%s " %
         #        last_history.status.name)
         return (False, last_history)
     logger.debug(
         "STATUSUPDATE - Instance:%s Old Status: %s - %s New Status: %s\
         Tmp Status: %s" % (self.provider_alias, self.esh_status(),
                            self.esh_activity(), status_name, tmp_status))
     logger.debug("STATUSUPDATE - Traceback: %s" % traceback.format_stack())
     # 3. ASSERT: A new history item is required due to a State or Size
     # Change
     now_time = timezone.now()
     try:
         new_history = InstanceStatusHistory.transaction(
             status_name,
             activity,
             self,
             size,
             start_time=now_time,
             last_history=last_history)
         return (True, new_history)
     except ValueError:
         logger.exception("Bad transaction")
         return (False, last_history)
示例#9
0
 def new_transaction(self):
     return InstanceStatusHistory.transaction(
         self.status_name, self.instance, self.size, self.start_date)
示例#10
0
 def first_transaction(self):
     history = InstanceStatusHistory.create_history(
         self.status_name, self.instance, self.size, self.start_date)
     history.save()
     return history
    def update_history(
        self,
        status_name,
        size,
        task=None,
        tmp_status=None,
        fault=None,
        deploy_fault_message=None,
        deploy_fault_trace=None,
        first_update=False
    ):
        """
        Given the status name and size, look up the previous history object
        If nothing has changed: return (False, last_history)
        else: end date previous history object, start new history object.
              return (True, new_history)
        """
        # FIXME: Move this call so that it happens inside InstanceStatusHistory to avoid circ.dep.
        from core.models import InstanceStatusHistory
        import traceback
        # 1. Get status name
        status_name = _get_status_name_for_provider(
            self.source.provider, status_name, task, tmp_status
        )
        activity = self.esh_activity()
        status = self.esh_status()
        extra = InstanceStatusHistory._build_extra(
            status_name=status_name,
            fault=fault,
            deploy_fault_message=deploy_fault_message,
            deploy_fault_trace=deploy_fault_trace
        )

        # 2. Get the last history (or Build a new one if no other exists)
        has_history = self.instancestatushistory_set.all().count()
        if not has_history:
            last_history = InstanceStatusHistory.create_history(
                status_name,
                self,
                size,
                start_date=self.start_date,
                activity=activity,
                extra=extra
            )
            last_history.save()
            logger.debug(
                "STATUSUPDATE - FIRST - Instance:%s Old Status: %s - %s New\
                Status: %s Tmp Status: %s" % (
                    self.provider_alias, status, activity, status_name,
                    tmp_status
                )
            )
            logger.debug(
                "STATUSUPDATE - Traceback: %s" % traceback.format_stack()
            )
        last_history = self.get_last_history()
        # 2. Size and name must match to continue using last history
        if last_history.status.name == status_name \
                and last_history.size.id == size.id:
            # logger.info("status_name matches last history:%s " %
            #        last_history.status.name)
            return (False, last_history)
        logger.debug(
            "STATUSUPDATE - Instance:%s Old Status: %s - %s New Status: %s\
            Tmp Status: %s" %
            (self.provider_alias, status, activity, status_name, tmp_status)
        )
        logger.debug("STATUSUPDATE - Traceback: %s" % traceback.format_stack())
        # 3. ASSERT: A new history item is required due to a State or Size
        # Change
        now_time = timezone.now()
        try:
            new_history = InstanceStatusHistory.transaction(
                status_name,
                activity,
                self,
                size,
                extra=extra,
                start_time=now_time,
                last_history=last_history
            )
            return (True, new_history)
        except ValueError:
            logger.exception("Bad transaction")
            return (False, last_history)