Invoking Legacy Code on Google Cloud Platform

You want to migrate your application to Google App Engine, but there's a problem: the server-side code is tied to a specific operating system and can't easily be ported. Perhaps you're using an aging COM object written in C++ or Visual Basic; or, worse, your source code no longer exists, and only the executable binary remains.

This tutorial describes an architectural pattern for invoking legacy code using Google App Engine, Google Compute Engine, and Google Cloud Pub/Sub, and provides an example implementation of the pattern. Though the example implementation focuses on Windows-specific code, the pattern outlined here can be reused for any legacy code.

Objectives

  • Learn about a simple architectural pattern for invoking legacy code on Google Cloud Platform.
  • Download and deploy an example implementation of the architectural pattern.

An architectural pattern for invoking legacy code

The following diagram illustrates an architectural pattern you can use to invoke legacy code from your App Engine application.

Architectural pattern for invoking legacy code
Figure 1: Architectural pattern for invoking legacy code

This architecture works as follows:

  1. The web client sends a request to the App Engine application, which writes the request to a Cloud Pub/Sub topic.
  2. A code wrapper pulls the request from the topic.
  3. The code wrapper invokes the legacy code.
  4. The legacy code returns the result to the wrapper. The wrapper then returns the result to the App Engine application, which returns it to the web client.

An example implementation of the pattern

You can find an implementation of the above pattern at GitHub. This example application takes a string as input, processes the string using the components outlined in the pattern, and returns an all-uppercase version. The application code comprises two main components:

  • The App Engine application, which publishes incoming requests to a Cloud Pub/Sub topic.
  • The code wrapper, which subscribes to the Cloud Pub/Sub topic and, when it receives a request, sends the request to some legacy Windows-based code.

In this implementation, the wrapper is a Windows service written in C#. This wrapper service is configured to run when the instance starts up, and can invoke any script or binary that can be invoked from C#. Both the wrapper service and the invoked binary reside on a Compute Engine instance running Windows Server 2012 R2.

The application's user interface looks like this:

Shout application user interface
Figure 2: Shout application user interface

If you type the word "mars" and click Submit, the result is as follows:

Shout application result
Figure 3: Shout application result

Prerequisites

This example application assumes the following:

Get the source code

Download the example code here and unzip it. If you prefer, you can clone the GitHub project repository instead by running the following command:

C:\> git clone https://github.com/GoogleCloudPlatform/pubsub-shout-csharp.git

Install the dependencies

In your command-line terminal, navigate to the appengine-python-flask folder in your local copy of the pubsub-shout-csharp repository, and then run the following:

C:\> pip install -r requirements.txt -t lib

Create a Compute Engine instance

Create the Compute Engine instance that the code wrapper and legacy code will run on:

  1. In the Cloud Platform Console, go to the Create a new instance page.
  2. In the Boot disk section, click Change.
  3. Select Windows Server 2012 R2 Datacenter Edition from the list of preconfigured images and click Select.
  4. In the Firewalls section, allow HTTP and HTTPS traffic.
  5. In the Project access section, allow full API access to all Google Cloud Platform services within your project.
  6. Click Management, disk, networking, access & security options to reveal advanced options, and then click the Management tab.
  7. In the Tags section, add the tag rdp-tag. Later in the tutorial, you'll create a firewall rule that allows external RDP access to all instances tagged with this tag.
  8. Click Create to begin creating your new instance.

After the instance has been created, add a default Windows user to the instance:

  1. In the Cloud Platform Console, go to the VM instances page.
  2. Click the name of your newly deployed instance.
  3. On the instance page, click the Create or reset Windows password button.
  4. In the Set new Windows password dialog, add your username and click Set to create the user account on your instance.
  5. Make a note of the provided password and close the dialog.

Next, create a firewall rule to enable RDP access to your instance:

  1. In the Cloud Platform Console, go to the Create a firewall rule page.
  2. Fill out the form as follows:

    • Name: rdp-rule
    • Source IP ranges: 0.0.0.0/0
    • Allowed protocols and ports: tcp:3389
    • Target tags: rdp-tag
  3. Click Create to create your firewall rule.

Build and deploy the code wrapper

To build the code wrapper service:

  1. In the Cloud Platform Console, go to the Credentials page.
  2. Click New credentials and select Service account key.
  3. On the Create service account key page, fill out the form as follows:

    • Service account: New service account
    • Name: A name of your choosing.
    • Service account ID: An ID of your choosing.
    • Key type: JSON
  4. Click Create. After a few seconds, a JSON key file downloads to your local computer.

  5. On your local machine, create a new environment variable called GOOGLE_APPLICATION_CREDENTIALS and set it to the absolute path of the JSON key file.
  6. Navigate to the windows-csharp folder in your local copy of the pubsub-shout-csharp repository.
  7. Open ShoutService.sln in Visual Studio 2012 or later.
  8. Edit ShoutLib/Constants.cs, set the variable ProjectId to your https://console.cloud.google.com/ project ID, and save.
  9. In the Build menu, click Build Solution to build the application.

After building the code wrapper service, copy it to your Compute Engine instance:

  1. Establish an remote desktop connection to the instance you created earlier in the tutorial.
  2. On your local machine, in Windows Explorer, navigate to the ShoutService\bin folder under your windows-csharp folder.
  3. Copy the Release folder from Windows Explorer, and then paste it inside your remote connection window to copy the folder to your instance. The Release folder contains the wrapper and the Shout service.

Next, allow the Windows Service Control Manager to start, stop, and otherwise interact with your service by granting appropriate permissions to LOCAL SERVICE, a predefined Windows user account used by the Service Control Manager:

  1. In Windows Explorer, navigate to the Release folder.
  2. Right click the folder to open the context menu, and then click Properties.
  3. In the Release Properties dialog, click the Security tab.
  4. Click Edit...
  5. In the Permissions for Release dialog, click Add...
  6. In the Select Users or Groups dialog, under Enger the object names to select, type LOCAL SERVICE and then click OK.
  7. In the Permissions for Release dialog, click LOCAL SERVICE.
  8. Under Permissions for LOCAL SERVICE, make sure that the following permissions are enabled:

    • Read & execute
    • List folder contents
    • Read
  9. Click OK.

Finally, install the code wrapper service:

  1. On your remote machine, open the command-line terminal as Administrator and navigate to the Release folder.
  2. Install the code wrapper service:

    C:\> C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe ShoutService.exe

Deploy the App Engine application

To deploy the App Engine application:

  1. On your local machine, in your command-line terminal, navigate to the appengine-python-flask folder in your local copy of the pubsub-shout-csharp repository.
  2. Deploy the application by running the following command:

    C:\> appcfg.py -A <your_project_id> -V 1 --oauth2 update .

  3. Authenticate the app when prompted.

  4. Enable the Cloud Pub/Sub API.

    Enable the API

  5. Initialize the app by visiting the following URL in your web browser:

    https://<your_project_id>.appspot.com/init

    When you visit this URL, the App Engine application creates a Cloud Pub/Sub topic and then subscribes the code wrapper service to the topic.

Congratulations! Your application is now live. Visit <your_project_id>.appspot.com to see it in action.

Clean up

After you've finished the tutorial, you can clean up the resources you created on Google Cloud Platform so you won't be billed for them in the future. The following sections describe how to delete or turn off these resources.

Delete your Cloud Platform project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

To delete the project:

  1. In the Cloud Platform Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Delete your Compute Engine instance

To delete a Compute Engine instance:

  1. In the Cloud Platform Console, go to the VM Instances page.

    Go to the VM Instances page

  2. Click the checkbox next to the instance you want to delete.
  3. Click the Delete button at the top of the page to delete the instance.

Next steps

Try other tutorials

Try out other Google Cloud Platform features for yourself. Have a look at our tutorials.

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...