Exemplo n.º 1
0
 def _create_gce(self):
     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]
     return gce.GceProject(oauth_decorator.credentials,
                           project_id=gce_project_id,
                           zone_name=gce_zone_name)
Exemplo n.º 2
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')
    def get(self):
        """Get and return the list of instances with names containing the tag."""

        gce_project_id = data_handler.stored_user_data[
            user_data.GCE_PROJECT_ID]
        gce_project = gce.GceProject(oauth_decorator.credentials,
                                     project_id=gce_project_id)
        gce_appengine.GceAppEngine().list_demo_instances(
            self, gce_project, DEMO_NAME)
    def post(self):
        """Stop instances with names containing the tag."""

        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)
        gce_appengine.GceAppEngine().delete_demo_instances(
            self, gce_project, DEMO_NAME)
Exemplo n.º 5
0
    def get(self):
        """List instances using the gce_appengine helper class.

    Return the results as JSON mapping instance name to status.
    """

        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]
        gce_project = gce.GceProject(oauth_decorator.credentials,
                                     project_id=gce_project_id,
                                     zone_name=gce_zone_name)
        gce_appengine.GceAppEngine().list_demo_instances(
            self, gce_project, DEMO_NAME)
Exemplo n.º 6
0
    def post(self):
        """Stop 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)
        gce_appengine.GceAppEngine().delete_demo_instances(
            self, gce_project, DEMO_NAME)

        # Record reset objective in datastore so we can recover work in progress.
        updateObjective(gce_project_id, 0)
    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')