Esempio n. 1
0
def extract_dimensions(instance, instance_template_revision):
    """Extracts Machine Provider dimensions.

  Args:
    instance: models.Instance entity.
    instance_template_revision: models.InstanceTemplateRevision entity.

  Returns:
    A dict of dimensions.
  """
    if instance_template_revision.dimensions:
        dimensions = json.loads(
            protojson.encode_message(instance_template_revision.dimensions))
    else:
        dimensions = {}

    dimensions['backend'] = 'GCE'

    if instance_template_revision.disk_size_gb:
        dimensions['disk_size_gb'] = instance_template_revision.disk_size_gb

    if instance_template_revision.machine_type:
        dimensions['memory_gb'] = gce.machine_type_to_memory(
            instance_template_revision.machine_type)
        dimensions['num_cpus'] = gce.machine_type_to_num_cpus(
            instance_template_revision.machine_type)

    dimensions['hostname'] = instance.key.id()

    return dimensions
Esempio n. 2
0
def extract_dimensions(instance, instance_template_revision):
  """Extracts Machine Provider dimensions.

  Args:
    instance: models.Instance entity.
    instance_template_revision: models.InstanceTemplateRevision entity.

  Returns:
    A dict of dimensions.
  """
  if instance_template_revision.dimensions:
    dimensions = json.loads(protojson.encode_message(
        instance_template_revision.dimensions))
  else:
    dimensions = {}

  dimensions['backend'] = 'GCE'

  if instance_template_revision.disk_size_gb:
    dimensions['disk_size_gb'] = instance_template_revision.disk_size_gb

  if instance_template_revision.machine_type:
    dimensions['memory_gb'] = gce.machine_type_to_memory(
        instance_template_revision.machine_type)
    dimensions['num_cpus'] = gce.machine_type_to_num_cpus(
        instance_template_revision.machine_type)

  dimensions['hostname'] = instance.key.id()

  return dimensions
Esempio n. 3
0
  def get(self):
    api = gce.Project(GCE_PROJECT_ID)
    logging.info('Retrieving instance templates')
    templates = api.get_instance_templates()
    logging.info('Retrieving instance group managers')
    managers = api.get_instance_group_managers(ZONE)

    handlers_pubsub.MachineProviderSubscriptionHandler.ensure_subscribed()

    requests = []

    # TODO(smut): Process instance group managers concurrently with a taskqueue.
    # For each group manager, tell the Machine Provider about its instances.
    for manager_name, manager in managers.iteritems():
      logging.info('Processing instance group manager: %s', manager_name)
      # Extract template name from a link to the template.
      template_name = manager['instanceTemplate'].split('/')[-1]
      # Property-related verification was done by InstanceTemplateProcessor,
      # so we can be sure all the properties we need have been supplied.
      properties = templates[template_name]['properties']
      disk_gb = int(properties['disks'][0]['initializeParams']['diskSizeGb'])
      memory_gb = float(gce.machine_type_to_memory(properties['machineType']))
      num_cpus = gce.machine_type_to_num_cpus(properties['machineType'])
      os_family = machine_provider.OSFamily.lookup_by_name([
          metadatum['value'] for metadatum in properties['metadata']['items']
          if metadatum['key'] == 'os_family'
      ][0])
      dimensions = machine_provider.Dimensions(
          backend=machine_provider.Backend.GCE,
          disk_gb=disk_gb,
          memory_gb=memory_gb,
          num_cpus=num_cpus,
          os_family=os_family,
      )
      instances = api.get_managed_instances(manager_name, ZONE)
      policies = machine_provider.Policies(
          on_reclamation=machine_provider.MachineReclamationPolicy.DELETE,
          pubsub_project=
              handlers_pubsub.MachineProviderSubscriptionHandler.TOPIC_PROJECT,
          pubsub_topic=handlers_pubsub.MachineProviderSubscriptionHandler.TOPIC,
      )

      create_instance_group(manager_name, dimensions, policies, instances)
Esempio n. 4
0
 def test_machine_type_to_num_cpus(self):
     self.assertEqual(8, gce.machine_type_to_num_cpus('n1-standard-8'))
     self.assertEqual(1, gce.machine_type_to_num_cpus('custom-1-2048'))
     with self.assertRaises(AssertionError):
         gce.machine_type_to_num_cpus('incorrect-machine-type')
Esempio n. 5
0
    def get(self):
        pubsub_handler = handlers_pubsub.MachineProviderSubscriptionHandler
        if not pubsub_handler.is_subscribed():
            logging.error(
                'Pub/Sub subscription not created:\n%s',
                pubsub_handler.get_subscription_name(),
            )
            return

        # For each group manager, tell the Machine Provider about its instances.
        for template in models.InstanceTemplate.query():
            logging.info(
                'Retrieving instance template %s from project %s',
                template.template_name,
                template.template_project,
            )
            api = gce.Project(template.template_project)
            try:
                instance_template = api.get_instance_template(
                    template.template_name)
            except net.NotFoundError:
                logging.error(
                    'Instance template does not exist: %s',
                    template.template_name,
                )
                continue
            api = gce.Project(template.instance_group_project)
            properties = instance_template['properties']
            disk_gb = int(
                properties['disks'][0]['initializeParams']['diskSizeGb'])
            memory_gb = float(
                gce.machine_type_to_memory(properties['machineType']))
            num_cpus = gce.machine_type_to_num_cpus(properties['machineType'])
            os_family = machine_provider.OSFamily.lookup_by_name(
                template.os_family)
            dimensions = machine_provider.Dimensions(
                backend=machine_provider.Backend.GCE,
                disk_gb=disk_gb,
                memory_gb=memory_gb,
                num_cpus=num_cpus,
                os_family=os_family,
            )
            try:
                instances = api.get_managed_instances(
                    template.instance_group_name, template.zone)
            except net.NotFoundError:
                logging.warning(
                    'Instance group manager does not exist: %s',
                    template.instance_group_name,
                )
                continue
            policies = machine_provider.Policies(
                backend_attributes=[
                    machine_provider.KeyValuePair(
                        key='group', value=template.instance_group_name),
                ],
                backend_project=handlers_pubsub.
                MachineProviderSubscriptionHandler.TOPIC_PROJECT,
                backend_topic=handlers_pubsub.
                MachineProviderSubscriptionHandler.TOPIC,
                on_reclamation=machine_provider.MachineReclamationPolicy.
                DELETE,
            )

            process_instance_group(
                template.instance_group_name,
                dimensions,
                policies,
                instances,
                template.zone,
                template.instance_group_project,
            )