8. Cloud Storage Buckets#

Google Cloud Storage buckets are a type of object storage service that let you store and manage large amounts of data in a secure, scalable, and durable way. A bucket is a container for files, or objects, and can hold backups, documents, logs, media files, and other data that needs to be accessed reliably from the cloud. Buckets can be used for routine backups, archiving old files, hosting static websites, sharing downloadable assets, and storing data for applications that need to read and write files without managing physical servers.

In this milestone, you will learn how to create and configure a bucket, assign permissions, and use it to store backup data from a Linux system. You’ll create a bucket that contains your VM’s startup script where it can be easily accessed. You’ll also keep a copy of MediaWiki’s source code in the bucket so that your application will work well, even if something happens to MediaWiki’s website.

Labs#

In class today you will see how to do the following tasks:

  1. Use the console for buckets

  2. Use the gcloud tool for buckets

  3. Give your VM’s service account access to the bucket you created

  4. Copy LocalSettings.php to your bucket

  5. Move your startup script to your bucket

    1. Copy the script in this milestone into your bucket

    2. Update your project metadata

Update LocalSettings.php#

When you installed MediaWiki you had to hard-code the IP address of your VM because you don’t have a domain name. We’re going to fix that, but in the meantime you need to update some code in LocalSettings.php to automatically figure out your IP address.

Find a line like this (around Line 35):

## The protocol and server name to use in fully-qualified URLs
$wgServer = "http://136.67.231.190";

Change it to this:

if ( isset( $_SERVER['HTTP_HOST'] ) ) {
    $wgServer = '//' . $_SERVER['HTTP_HOST'];
} else {
    $wgServer = 'http://localhost';
}

Move Your Startup Script#

It’s much more convenient to have a startup script in a cloud storage bucket. Copy the contents of the shell script below into a file called startup.sh and upload the file to your bucket. Once the file is present edit your Compute Engine metadata:

  1. Remove the startup-script key.

  2. Add a new key called startup-script-url with the value gs://your-bucket-name/startup.sh

Warning

You have to modify this script with your bucket name!

#! /usr/bin/bash

set -e

# Install stuff
sudo apt install -y apache2 php php-xml php-intl php-mbstring php-pgsql postgresql-client
sudo systemctl restart apache2 

# Remove index.html
rm -f /var/www/html/index.html

# Install the redirect...
sudo tee /var/www/html/index.php << 'EOF'
<?php
header("Location: mediawiki-1.46.2");
exit;
EOF

# Install MediaWiki
cd /var/www/html
sudo wget https://releases.wikimedia.org/mediawiki/1.46/mediawiki-1.46.2.tar.gz
sudo tar -xvf mediawiki-1.46.2.tar.gz

# Download site settings.
sudo gcloud storage cp gs://your-bucket-name-here-student/LocalSettings.php mediawiki-1.46.2/

Project Documentation#

To meet the project requirements for this week you have to accomplish and document the following tasks:

  1. Create a non-public cloud storage bucket

  2. Copy startup.sh and LocalSettings.php into your bucket

  3. Add the “Storage Admin” role to the service account you created for VMs

  4. Delete the startup-script key and add startup-script-url key in Compute Engine Metadata

Check Your Progress#

If your project is setup correctly you should now be able to create and destroy VMs at will. Each new VM you create will automatically install MediaWiki and have all the settings needed to connect your database. You should verify this by deleting your VM and recreating it. Remember these important settings on your new VM:

  1. Enable HTTP access

  2. Attach it to your cis-91 VPC

  3. Use the non-default service account you created

A VM with the proper settings will “just work” no matter what region it’s in.