Use a custom Go buildpack with IBM Bluemix

29 June 2014
PDF (230 KB)
Share:

IBM® Bluemix™ provides out-of-the-box support for various fit-for-purpose web development frameworks, programming languages, and runtimes. When you deploy an application to Bluemix that you've written in a supported language, the platform detects the runtime and automatically provisions it as a service to run your application.

But what if Bluemix doesn't support your preferred language and web development framework? Bluemix, like some other PaaS environments, implements a pluggable model that adds support for additional runtimes. A buildpack is collection of scripts that implements the actions needed to examine the application you're deploying, and download and configure the needed dependencies.

So, the answer is: Develop a buildpack yourself for your preferred runtime, or find a third-party or community-developed buildpack on the web. Luckily, plenty of community buildpacks are available. I'll demonstrate the easy process of using a community buildpack for the Go programming language to run a simple web application on Bluemix.

What you need to build and run the app

 

Note: To fork the code for this exercise, click the EDIT CODE button in the upper right-hand corner (enter your DevOps Services credentials if you're not already logged in) and click the FORK button on the menu to create a new project. Alternatively, you can export the code by selecting the root folder (mcrudele/Goweb), then selecting File > Export from the left navigation.

READ:Cloud Foundry community buildpacks

READ:Go getting started page

Step 1. Write the code

 

You'll stand up a web server in Go for the sample app, which performs a couple of simple math calculations.

  1. Import the packages:
    package main
    
    import (
      "net/http"
      "fmt"
      ...
    )
  2. Write an HTTP handler:
    func primeFactorsHandler(w http.ResponseWriter, r *http.Request) {
      // accept only POST
      if r.Method != "POST" {
        w.WriteHeader(http.StatusMethodNotAllowed)
        return
      }
    
      // get the form value and convert to a number
      num_s := r.FormValue("number")
      num, _ := strconv.Atoi(num_s)
    
      // calculate prime factors of the number
      factors, err := PrimeFactors(uint64(num))
    
      // Send results to the client
      ss := prettyPrintPrimeFactors(uint64(num), factors)
      fmt.Printf(w, ss)
    }
  3. Write the web server:
    func main() {
    
      // set the handler to calculate prime factors of a number
      http.HandleFunc("/primefactors", primeFactorsHandler)
    
      log.Printf("Starting GoWeb on port %+v\n", port)
      http.ListenAndServe(":" + port, nil)
    }
  4. Stand up the server locally. From your Go console type:
    go run goweb.go

    Now your server is listening on port 4001, and you can test the app by pointing your browser to http://localhost:4001.

Step 2. Select a Cloud Foundry buildpack for Go

 

Though other choices might be available, I've selected the Heroku buildpack for Go, modified by Michal Jemala to work with Cloud Foundry-based clouds. It's an experimental buildpack, but it does its job well on Bluemix.

Step 3. Create the deployment artifacts

 

You need to create a couple of files on the root folder of your app before deploying it in the cloud:

  • .godir — This file should contain, in its entirety, the desired name of the final Go library. For example:
    example.com/goweb
  • Procfile — This file contains the command to start the web app. For example:
    web:goweb

Step 4. Deploy the app

 

Deploying the app to Bluemix is pretty much the same as deploying any other app, with the exception of a command-line flag to set the custom buildpack the platform should use to provision the runtime.

Log in to your Bluemix account and run this command from the root folder of your application, where appname represents a unique name for your Bluemix-hosted app:

cf push appname -b https://github.com/michaljemala/cloudfoundry-buildpack-go

You'll see the output similar to mine:

Creating app goweb in org your.name-org / space dev as your.name@your.mail...
OK

Using route goweb.mybluemix.net
Binding goweb.mybluemix.net to goweb...
OK

Uploading goweb...
Uploading from: C:\projects\goweb\src
21.7K, 10 files
OK

Starting app goweb in org your.name-org / space dev as your.name@your.mail...
-----> Downloaded app package (16K)
OK
-----> Downloaded app buildpack cache (38M)
Initialized empty Git repository in /tmp/buildpacks/cloudfoundry-buildpack-go/.git/
-----> Using Go 1.2
-----> Installing Virtualenv... done
-----> Installing Mercurial... done
-----> Installing Bazaar... done
-----> Running 'go get -tags cf ./...'... done in 1s
-----> Uploading droplet (2.1M)

0 of 1 instances running, 1 down
0 of 1 instances running, 1 starting
1 of 1 instances running

App started

Showing health and status for app goweb in org your.name-org / space dev as your.name@your.mail...
OK

requested state: started
instances: 1/1
usage: 256M x 1 instances
urls: goweb.mybluemix.net

     state     since                    cpu    memory         disk
#0   running   2014-02-04 06:18:38 PM   0.0%   1.5M of 256M   8.7M of 1G

Step 5. Connect to your app

 

Point your browser to http://appname.mybluemix.net to bring up the UI and start testing the app:

Screen capture of the Go Sample App UI

Conclusion

 

In developing the sample web app, I used the standard packages that come with the Go programming language binary distributions. Now that you're sure that Bluemix will support your efforts, you might want to challenge yourself by modifying the app to use a REST-style web services framework for Go; you'll find some by surfing the web. And you could try Godev, an IDE hosted in a web interface, which is freely available on DevOps Services.

Acknowledgment

A big thanks to Michal Jemala, who made this exercise possible by building the Cloud Foundry buildpack for Go. You might want to take a look at his work.


BLUEMIX SERVICE USED IN THIS TUTORIAL:Installing the cf command line interface gives directions for installing the CLI.

Add a comment

Note: HTML elements are not supported within comments.


1000 characters left

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Cloud computing
ArticleID=963846
ArticleTitle=Use a custom Go buildpack with IBM Bluemix
publish-date=06292014