Beispiel #1
0
def fetch(key):
    """Gets instances created by the given instance group manager.

  Args:
    key: ndb.Key for a models.InstanceGroupManager entity.

  Returns:
    A list of instance URLs.
  """
    entity = key.get()
    if not entity:
        logging.warning('InstanceGroupManager does not exist: %s', key)
        return []

    if not entity.url:
        logging.warning('InstanceGroupManager URL unspecified: %s', key)
        return []

    parent = key.parent().get()
    if not parent:
        logging.warning('InstanceTemplateRevision does not exist: %s',
                        key.parent())
        return []

    if not parent.project:
        logging.warning('InstanceTemplateRevision project unspecified: %s',
                        key.parent())
        return []

    api = gce.Project(parent.project)
    result = api.get_instances_in_instance_group(
        instance_group_managers.get_name(entity),
        entity.key.id(),
        max_results=500,
    )
    instance_urls = [
        instance['instance'] for instance in result.get('items', [])
    ]
    while result.get('nextPageToken'):
        result = api.get_instances_in_instance_group(
            instance_group_managers.get_name(entity),
            entity.key.id(),
            max_results=500,
            page_token=result['nextPageToken'],
        )
        instance_urls.extend(
            [instance['instance'] for instance in result['items']])

    return instance_urls
Beispiel #2
0
def _delete(instance_template_revision, instance_group_manager, instance):
  """Deletes the given instance.

  Args:
    instance_template_revision: models.InstanceTemplateRevision.
    instance_group_manager: models.InstanceGroupManager.
    instance: models.Instance
  """
  api = gce.Project(instance_template_revision.project)
  try:
    result = api.delete_instances(
        instance_group_managers.get_name(instance_group_manager),
        instance_group_manager.key.id(),
        [instance.url],
    )
    if result['status'] != 'DONE':
      logging.warning(
          'Instance group manager operation failed: %s\n%s',
          parent.key,
          json.dumps(result, indent=2),
      )
    else:
      metrics.send_machine_event('DELETION_SCHEDULED', instance.key.id())
  except net.Error as e:
    if e.status_code == 400:
      metrics.send_machine_event('DELETION_SUCCEEDED', instance.key.id())
    else:
      raise
Beispiel #3
0
def _delete(instance_template_revision, instance_group_manager, instance):
  """Deletes the given instance.

  Args:
    instance_template_revision: models.InstanceTemplateRevision.
    instance_group_manager: models.InstanceGroupManager.
    instance: models.Instance
  """
  api = gce.Project(instance_template_revision.project)
  try:
    result = api.delete_instances(
        instance_group_managers.get_name(instance_group_manager),
        instance_group_manager.key.id(),
        [instance.url],
    )
    if result['status'] != 'DONE':
      logging.warning(
          'Instance group manager operation failed: %s\n%s',
          parent.key,
          json.dumps(result, indent=2),
      )
  except net.Error as e:
    if e.status_code != 400:
      # If the instance isn't found, assume it's already deleted.
      raise
Beispiel #4
0
def _delete(instance_template_revision, instance_group_manager, instance):
    """Deletes the given instance.

  Args:
    instance_template_revision: models.InstanceTemplateRevision.
    instance_group_manager: models.InstanceGroupManager.
    instance: models.Instance
  """
    api = gce.Project(instance_template_revision.project)
    try:
        result = api.delete_instances(
            instance_group_managers.get_name(instance_group_manager),
            instance_group_manager.key.id(),
            [instance.url],
        )
        if result['status'] != 'DONE':
            logging.warning(
                'Instance group manager operation failed: %s\n%s',
                parent.key,
                json.dumps(result, indent=2),
            )
    except net.Error as e:
        if e.status_code != 400:
            # If the instance isn't found, assume it's already deleted.
            raise
Beispiel #5
0
def fetch(key):
  """Gets instances created by the given instance group manager.

  Args:
    key: ndb.Key for a models.InstanceGroupManager entity.

  Returns:
    A list of instance URLs.
  """
  entity = key.get()
  if not entity:
    logging.warning('InstanceGroupManager does not exist: %s', key)
    return []

  if not entity.url:
    logging.warning('InstanceGroupManager URL unspecified: %s', key)
    return []

  parent = key.parent().get()
  if not parent:
    logging.warning('InstanceTemplateRevision does not exist: %s', key.parent())
    return []

  if not parent.project:
    logging.warning(
        'InstanceTemplateRevision project unspecified: %s', key.parent())
    return []

  api = gce.Project(parent.project)
  result = api.get_instances_in_instance_group(
      instance_group_managers.get_name(entity),
      entity.key.id(),
      max_results=500,
  )
  instance_urls = [instance['instance'] for instance in result.get('items', [])]
  while result.get('nextPageToken'):
    result = api.get_instances_in_instance_group(
        instance_group_managers.get_name(entity),
        entity.key.id(),
        max_results=500,
        page_token=result['nextPageToken'],
    )
    instance_urls.extend([instance['instance'] for instance in result['items']])

  return instance_urls
Beispiel #6
0
def _delete(instance_template_revision, instance_group_manager, instance):
    """Deletes the given instance.

  Args:
    instance_template_revision: models.InstanceTemplateRevision.
    instance_group_manager: models.InstanceGroupManager.
    instance: models.Instance
  """
    # We don't check if there are any pending deletion calls because we don't
    # care. We just want the instance to be deleted, so we make repeated calls
    # until the instance is no longer detected. However, we do care how long
    # it takes, so we only write instance.deletion_ts once.

    api = gce.Project(instance_template_revision.project)
    try:
        now = utils.utcnow()
        result = api.delete_instances(
            instance_group_managers.get_name(instance_group_manager),
            instance_group_manager.key.id(),
            [instance.url],
        )
        if result['status'] != 'DONE':
            # This is not the status of the instance deletion, it's the status of
            # scheduling the instance deletions in the managed instance group. If
            # it's not DONE, the deletions won't even be attempted. If it is DONE,
            # the actual deletions may still fail.
            logging.warning(
                'Instance group manager operation failed: %s\n%s',
                instance_group_manager.key,
                json.dumps(result, indent=2),
            )
        else:
            if not instance.deletion_ts:
                set_deletion_time(instance.key, now)
                metrics.send_machine_event('DELETION_SCHEDULED',
                                           instance.hostname)
    except net.Error as e:
        if e.status_code == 400:
            if not instance.deletion_ts:
                set_deletion_time(instance.key, now)
        else:
            raise