Ejemplo n.º 1
0
    def post(self):
        """Start instances using the gce_appengine helper class."""

        gce_project_id = data_handler.stored_user_data[
            user_data.GCE_PROJECT_ID]
        gce_zone_name = data_handler.stored_user_data[user_data.GCE_ZONE_NAME]
        user_id = users.get_current_user().user_id()
        credentials = oauth2client.StorageByKeyName(
            oauth2client.CredentialsModel, user_id, 'credentials').get()
        gce_project = gce.GceProject(credentials,
                                     project_id=gce_project_id,
                                     zone_name=gce_zone_name)

        num_instances = int(self.request.get('num_instances'))
        instances = [
            gce.Instance('%s-%d' % (DEMO_NAME, i))
            for i in range(num_instances)
        ]
        response = gce_appengine.GceAppEngine().run_gce_request(
            self,
            gce_project.bulk_insert,
            'Error inserting instances: ',
            resources=instances)

        # Record objective in datastore so we can recover work in progress.
        updateObjective(gce_project_id, num_instances)

        if response:
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.out.write('starting cluster')
Ejemplo n.º 2
0
    def _get_instance_list(self, gce_project, num_instances, image, disks):
        """Get a list of instances to start.

    Args:
      gce_project: An instance of gce.GceProject.
      num_instances: The number of instances to start.
      image: tuple with (project_name, image_name) for the image to use.
      disks: A dictionary of disk_name -> disk resources

    Returns:
      A list of gce.Instances.
    """

        instance_names = []
        for i in range(num_instances):
            instance_names.append('%s-%02d' % (self.instance_prefix(), i))

        instance_list = []
        for instance_name in instance_names:
            disk_name = 'boot-%s' % instance_name
            disk_mounts = [
                gce.DiskMount(init_disk_name=disk_name,
                              boot=True,
                              auto_delete=True)
            ]

            gce_zone_name = data_handler.stored_user_data[
                user_data.GCE_ZONE_NAME]
            # Define a network interfaces list here that requests an ephemeral
            # external IP address. We will apply this configuration to all VMs
            # started by the fractal app.
            network = gce.Network('default')
            network.gce_project = gce_project
            ext_net = [{
                'network':
                network.url,
                'accessConfigs': [{
                    'name': 'External IP access config',
                    'type': 'ONE_TO_ONE_NAT'
                }]
            }]
            instance = gce.Instance(
                name=instance_name,
                machine_type_name=MACHINE_TYPE,
                zone_name=gce_zone_name,
                network_interfaces=ext_net,
                disk_mounts=disk_mounts,
                tags=[DEMO_NAME, self.instance_prefix()],
                metadata=self._get_instance_metadata(gce_project,
                                                     instance_names),
                service_accounts=gce_project.settings['cloud_service_account'])
            instance_list.append(instance)
        return instance_list
  def _get_instance_list(self, gce_project, num_instances, image, disks):
    """Get a list of instances to start.

    Args:
      gce_project: An instance of gce.GceProject.
      num_instances: The number of instances to start.
      image: tuple with (project_name, image_name) for the image to use.
      disks: A dictionary of disk_name -> disk resources

    Returns:
      A list of gce.Instances.
    """


    instance_names = []
    for i in range(num_instances):
      instance_names.append('%s-%02d' % (self.instance_prefix(), i))

    instance_list = []
    for instance_name in instance_names:
      disk_name = 'boot-%s' % instance_name
      disk = disks.get(disk_name, None)
      disk_mounts = []
      image_project_id = None
      image_name = None
      kernel = None
      if disk:
        dm = gce.DiskMount(disk=disk, boot=True)
        kernel = gce_project.settings['compute']['kernel']
        disk_mounts.append(dm)
      else:
        image_project_id, image_name = image


      instance = gce.Instance(
          name=instance_name,
          machine_type_name=MACHINE_TYPE,
          image_name=image_name,
          image_project_id=image_project_id,
          disk_mounts=disk_mounts,
          kernel=kernel,
          tags=[DEMO_NAME, self.instance_prefix()],
          metadata=self._get_instance_metadata(gce_project, instance_names),
          service_accounts=gce_project.settings['cloud_service_account'])
      instance_list.append(instance)
    return instance_list
    def post(self):
        """Insert instances with a startup script, metadata, and scopes.

    Startup script is randomly chosen to either rotate images left or right.
    Metadata includes the image to rotate, the demo name tag, and the machine
    number. Service account scopes include Compute and storage.
    """

        user = users.get_current_user()
        credentials = oauth2client.StorageByKeyName(
            oauth2client.CredentialsModel, user.user_id(),
            'credentials').get()
        gce_project_id = data_handler.stored_user_data[
            user_data.GCE_PROJECT_ID]
        gce_project = gce.GceProject(credentials, project_id=gce_project_id)

        # Get the bucket info for the instance metadata.
        gcs_bucket = data_handler.stored_user_data[user_data.GCS_BUCKET]
        gcs_directory = data_handler.stored_user_data.get(
            user_data.GCS_DIRECTORY, None)
        gcs_path = None
        if gcs_directory:
            gcs_path = '%s/%s' % (gcs_bucket, gcs_directory)
        else:
            gcs_path = gcs_bucket

        # Figure out the image.  Use custom image if it exists.
        (image_project, image_name) = self._get_image_name(gce_project)

        # Create a list of instances to insert.
        instances = []
        num_instances = int(self.request.get('num_instances'))
        for i in range(num_instances):
            startup_script = os.path.join(os.path.dirname(__file__),
                                          'startup.sh')
            instances.append(
                gce.Instance(name='%s-%d' % (DEMO_NAME, i),
                             image_project_id=image_project,
                             image_name=image_name,
                             service_accounts=gce_project.
                             settings['cloud_service_account'],
                             metadata=[{
                                 'key':
                                 'startup-script',
                                 'value':
                                 open(startup_script, 'r').read()
                             }, {
                                 'key': 'image',
                                 'value': random.choice(IMAGES)
                             }, {
                                 'key': 'seq',
                                 'value': random.choice(SEQUENCES)
                             }, {
                                 'key': 'machine-num',
                                 'value': i
                             }, {
                                 'key': 'tag',
                                 'value': DEMO_NAME
                             }, {
                                 'key': 'gcs-path',
                                 'value': gcs_path
                             }]))

        response = gce_appengine.GceAppEngine().run_gce_request(
            self,
            gce_project.bulk_insert,
            'Error inserting instances: ',
            resources=instances)

        if response:
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.out.write('starting cluster')
    def post(self):
        """Start instances using the gce_appengine helper class."""
        user_info = getUserDemoInfo(users.get_current_user())

        gce_project_id = data_handler.stored_user_data[
            user_data.GCE_PROJECT_ID]
        gce_zone_name = data_handler.stored_user_data[user_data.GCE_ZONE_NAME]
        user_id = users.get_current_user().user_id()
        credentials = oauth2client.StorageByKeyName(
            oauth2client.CredentialsModel, user_id, 'credentials').get()
        gce_project = gce.GceProject(credentials,
                                     project_id=gce_project_id,
                                     zone_name=gce_zone_name)

        # Create a user specific route. We will apply this route to all
        # instances without an IP address so their requests are routed
        # through the first instance acting as a proxy.
        # gce_project.list_routes()
        proxy_instance = gce.Instance(name='%s-0' % user_info['demo_id'],
                                      zone_name=gce_zone_name)
        proxy_instance.gce_project = gce_project
        route_name = '%s-0' % user_info['demo_id']
        gce_route = gce.Route(name=route_name,
                              network_name='default',
                              destination_range='0.0.0.0/0',
                              next_hop_instance=proxy_instance,
                              priority=200,
                              tags=['qs-%s' % user_info['ldap']])
        response = gce_appengine.GceAppEngine().run_gce_request(
            self,
            gce_project.insert,
            'Error inserting route: ',
            resource=gce_route)

        # Define a network interfaces list here that requests an ephemeral
        # external IP address. We will apply this configuration to the first
        # VM started by quick start. All other VMs will take the default
        # network configuration, which requests no external IP address.
        network = gce.Network('default')
        network.gce_project = gce_project
        ext_net = [{
            'network':
            network.url,
            'accessConfigs': [{
                'name': 'External IP access config',
                'type': 'ONE_TO_ONE_NAT'
            }]
        }]
        num_instances = int(self.request.get('num_instances'))
        instances = [
            gce.Instance(
                '%s-%d' % (user_info['demo_id'], i),
                zone_name=gce_zone_name,
                network_interfaces=(ext_net if i == 0 else None),
                metadata=([{
                    'key': 'startup-script',
                    'value': user_data.STARTUP_SCRIPT % 'false'
                }] if i == 0 else [{
                    'key': 'startup-script',
                    'value': user_data.STARTUP_SCRIPT % 'true'
                }]),
                service_accounts=[{
                    'email':
                    'default',
                    'scopes': ['https://www.googleapis.com/auth/compute']
                }],
                disk_mounts=[
                    gce.DiskMount(init_disk_name='%s-%d' %
                                  (user_info['demo_id'], i),
                                  boot=True)
                ],
                can_ip_forward=(True if i == 0 else False),
                tags=(['qs-proxy']
                      if i == 0 else ['qs-%s' % user_info['ldap']]))
            for i in range(num_instances)
        ]
        response = gce_appengine.GceAppEngine().run_gce_request(
            self,
            gce_project.bulk_insert,
            'Error inserting instances: ',
            resources=instances)

        # Record objective in datastore so we can recover work in progress.
        updateObjective(user_info['project_id'], num_instances)

        if response:
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.out.write('starting cluster')