Skip to the content.

Running your code on GCP

In order to run your code on a cloud linux machine, we’ll do the following:

  1. Create the cloud virtual machine with an appropriate OS image.
  2. SSH to the VM, opening a remote terminal.
  3. Install necessary software to run your project.
  4. Clone your project from GitHub.
  5. Check out your working branch.
  6. Run the shell scripts.

First, make sure you commit and push your working branch to GitHub!

Important: You will now have two copies of your repository, on different machines. Whenever you make changes to one (e.g., on your desktop), you will need to commit and git push your changes, then git pull on the other to get the latest code in the branch.

If you are going to edit code in two places, be very careful, and be sure you know what you’re doing.

1-2: Creating an instance

From a terminal, assuming you have the Cloud SDK installed (refer to the getting started back from Canvas), create the instance:

gcloud compute instaces create lab2 \
  --machine-type=n1-standard-1 \
  --image-project=ubuntu-os-cloud \
  --impage-family=ubuntu-minimal-2004-lts

You can also change the machine type to f1-micro from n1-standard-1, which is in the free tier.

Then, ssh into it:

gcloud compute ssh lab2

At any point, type exit to close the terminal. Repeat the ssh command to re-login.

NOTE: You can do this from the cloud console as well, by going to the Compute/VM instances page, clicking “Create”, and then choosing any of the Ubuntu OS images (as long as it is 20 or above, unless you want to install Go yourself).

Then, from the VM instances page, you can click “SSH” to start a browser-based SSH session.

3: Install software

You need at minimum git and go:

sudo apt-get update
sudo apt-get install -y golang-go git

The first command updates the package manager’s list of packages. The second command does the software install. It will take a minute.

4-5: Clone and check out your branch

Clone your repository (go to your repository’s page on GitHub to get the clone URL):

git clone [url]

Move to the directory and list the repository’s branches:

ls
cd [directory with project]
git branch -a | grep remotes

You should see your branch here.

Now, we need to get that branch locally:

git checkout --track origin/[branch name]

You should see the following message:

On branch [branch name]
Your branch is up to date with 'origin/[branch name'.

nothing to commit, working tree clean

6: Running your lab

Now, you can run all the shell scripts:

./test-wc.sh
./test-ii.sh
./test-wc-distributed.sh
./test-ii-distributed.sh

IMPORTANT: Delete your VM.

Your VM will cost you money! You can stop the instance to reduce its cost with

gcloud compute instances stop lab2

This saves the machine state (which will cost ~pennies) and allow you to restarted it.

gcloud compute instances start lab2

If you want to delete it (and have to redo these steps on subsequent tests), use the following:

gcloud compute instances delete lab2

All of these commands have GUI analogs on the cloud console.