Running GCloud

Run GCloud on Educative using environment keys!

With our tarball uploaded, we simply need to run GCloud using a Terminal widget.

Terminal Widget

Type gcloud version in the Terminal below to see if it has been installed.

Terminal 1
Terminal
Loading...

However, gcloud requires user authentication in order to interact with GCP services. This can be cumbersome for users as they would have to provide their credentials each time.

To circumvent this problem, we can use API keys.

Creating API Keys

To create API keys, head over to the API Keys section on the course editor page.

Add the name of your key and click the Save icon to let it take effect. The Value field can be left empty.

For GCP, you will need to add keys corresponding to your service account’s key file:

  • type
  • project_id
  • private_key_id
  • private_key
  • client_email
  • client_id
  • auth_uri
  • token_uri
  • auth_provider_x509_cert_url
  • client_x509_cert_url

Once these keys have been created, every user can add their own credentials and work with GCP services! To achieve this, we will use a Single Page App (SPA) widget.

Using API Keys

In a SPA widget, select the API keys you want the user to enter values in.

You must also create/upload a JSON file containing these keys. This is where the user’s credentials will automatically be inserted. To add API keys in any code files, use the following template:

{{api_key_name}}

Such a file has been created below:

Press + to interact
{
"type": "{{type}}",
"project_id": "{{project_id}}",
"private_key_id": "{{private_key_id}}",
"private_key": "{{private_key}}",
"client_email": "{{client_email}}",
"client_id": "{{client_id}}",
"auth_uri": "{{auth_uri}}",
"token_uri": "{{token_uri}}",
"auth_provider_x509_cert_url": "{{auth_provider_x509_cert_url}}",
"client_x509_cert_url": "{{client_x509_cert_url}}"
}

Adding a Docker Job

The final step is creating a Docker job that will launch gcloud using the provided credentials. To create a job, head over to the Docker Settings section on the course editor page and click the ‘+’ icon.

The job for running gcloud has been provided below.

  • Type: Live

  • Job Name: This name will be used to select the job in the SPA widget.

  • Input File Name: This can be set to any random value.

  • Run Script: This can be set to any random value.

  • Application Port: This can be set to any random value.

  • HTTPS Strict Mode: False.

  • Force Relaunch: True.

  • Start Script: This is the most important part of the job. Here, you have to enter the commands for running gcloud. We have provided the part of the JSON file we uploaded earlier.

    Note: Files present in a SPA widget are stored in the usercode directory. Hence, we have used that as our path.

Selecting the Docker Job

In the SPA widget, select your Docker job from the dropdown menu.

gcloud in Action!

All that’s left to do is run the SPA widget. Since we do not need to show a running application, we can check the Hide Output option in the SPA widget’s configurations.

Let’s run the final setup below after providing our credentials. You can either enter the values manually or use the Import Values from JSON option to automatically import all the key values, which saves a lot of time.

{
  "type": "{{type}}",
  "project_id": "{{project_id}}",
  "private_key_id": "{{private_key_id}}",
  "private_key": "{{private_key}}",
  "client_email": "{{client_email}}",
  "client_id": "{{client_id}}",
  "auth_uri": "{{auth_uri}}",
  "token_uri": "{{token_uri}}",
  "auth_provider_x509_cert_url": "{{auth_provider_x509_cert_url}}",
  "client_x509_cert_url": "{{client_x509_cert_url}}"
}