Hello World on Cloud Foundry v2
It’s been very busy around here and I’ve been lagging behind on the updates to Cloud Foundry v2. If you’re like me, hopefully you will find this a useful update going from way-old vmc on v1 to cf on v2:
I’m going to create a new account on Cloud Foundry and push my good old bacon pancakes ruby sinatra app to my new account.
Step 1: Create a new account. Creating a new account on Cloud Foundry, especially with a promo code, is much easier now. No more temporary passwords that were sometimes hard to read.
Go to cloudfoundry.com
Click on the “free trial” button
Enter email address and Promo code (you can use “preshavedyak”)
Check your email and click on the link in the email from Cloud Foundry
Create a password
Now you have an account and password!
Step 2: Update your gems, target, and then log in.
I followed the steps on http://docs.cloudfoundry.com/docs/dotcom/getting-started.html#install-cf
$ gem install cf
$ cf target api.run.pivotal.io
$ cf login
From the option listed, I chose “1″ (development)
Step 3: Push your app. First I was lazy and tried to see if my existing app would push as is.
$ cf push
Create services for application?>n
Error for the lazy! I got this:
—–> Downloaded app package (4.0K)
/var/vcap/packages/dea_next/buildpacks/lib/buildpack.rb:44:in `build_pack’: Unable to detect a supported application type (RuntimeError)
from /var/vcap/packages/dea_next/buildpacks/lib/buildpack.rb:26:in `block in compile_with_timeout’
from /usr/lib/ruby/1.9.1/timeout.rb:68:in `timeout’
from /var/vcap/packages/dea_next/buildpacks/lib/buildpack.rb:25:in `compile_with_timeout’
from /var/vcap/packages/dea_next/buildpacks/lib/buildpack.rb:16:in `block in stage_application’
from /var/vcap/packages/dea_next/buildpacks/lib/buildpack.rb:12:in `chdir’
from /var/vcap/packages/dea_next/buildpacks/lib/buildpack.rb:12:in `stage_application’
from /var/vcap/packages/dea_next/buildpacks/bin/run:10:in `<main>’
Checking status of app ‘baconpancakestest’…Application failed to stage
Step 4: Set up for your ruby sinatra app.
So I decided to read further on http://docs.cloudfoundry.com/docs/using/deploying-apps/ruby/
It told me to go to http://bundler.io/
I followed these steps:
a) $ gem install bundler
b) Create doc “Gemfile” (no extension to the file) and save this text in the file:
The site says to add the following:
gem ‘rack’, ‘~>1.1′
gem ‘rspec’, :require => ‘spec’
In our case, it is a ruby/sinatra app, so I paste the following in the Gemfile:
Save this file in the folder with my baconpancakesv2.rb app
$ bundle installSkipped $ git add Gemfile Gemfile.lock
Error: I needed to add the ruby version to the Gemfile. In my case, it is ruby ’1.9.3′
Updated Gemfile to say:
$ bundle installThis creates the Gemfile.lock file
c) Update the baconpancakes.rb app
I renamed it baconpancakesv2.rb
I followed the steps on http://www.sinatrarb.com/extensions.html
Change the first line of the app:
Delete: require ‘sinatra’
Replace with: class YakApp < Sinatra::Base
and add “end” in the last line
Here is the code for baconpancakesv2.rb:
class YakApp < Sinatra::Base
get '/' do
youtube_id = $1
# Regex from #
youtube_id = $5
"<h1>Bacon Pancakes from Cloud Foundry</h1>"+youtube_embed('youtu.be/zalYJacOhpo')
d) Create config.ru
Return to http://docs.cloudfoundry.com/docs/using/deploying-apps/ruby/
Follow instructions to create config.ru file.
In my case, for our sinatra app, I include the following:
require './baconpancakesv2' (Make sure this is your own directory name)
map '/' do
Also follow the instructions on http://www.sinatrarb.com/extensions.html
So the config.ru file will include:
map '/' do
Step 5: Try to push the app the right way.
So now you should have a directory that has these 4 files:
- rb app
Now do the cf push from that directory. You should be able to push your app to Cloud Foundry. If you used my app, it should look like this: http://baconpancakesv2.cfapps.io
Of course, this is a limited case. Please consult the Cloud Foundry documentation for further info.
Also check out Andy Piper's github repo for this same app: https://github.com/andypiper/yak/tree/andyp
Thanks to Andy for his help!
Step 6: Manage your account in the browser.
Once you have your account. Don't forget to check out your new account console in the browser. Pretty nice!