Deploying a Ruby on Rails App and Binding Services with Predix

by Dzianis DashkevichApril 18, 2016
This tutorial will be helpful to Ruby developers who are new to Predix, an industrial IoT platform from GE, guiding you through deploying an app binded to services.

Prerequisites

To deploy a Ruby on Rails application, you must have the following:

A sample Ruby on Rails application ready to be deployed to GE Predix is available in the predix-rails-starter-app repository on the Altoros GitHub account.

 

First steps in Predix

Most of the time, you’ll interact with the platform through the Cloud Foundry CLI. After installing the package, proceed with logging in to Cloud Foundry by issuing the following command in your terminal:

cf login -a API_ENDPOINT

where API_ENDPOINT is the URL value that was sent to you in the Predix signup e-mail.

Depending on your Predix registration, the value of API_ENDPOINT is one of the following:

  • https://api.system.aws-usw02-pr.ice.predix.io for Predix Basic
  • https://api.system.asv-pr.ice.predix.io for Predix Select

 

Kubernetes Deep Dive DC

Adding services

Before deploying your application, make sure to create necessary service instances for it. If a service is available in Predix and you have the required access to that service, you can use it for your application development.

For viewing services and plans available to you, run the cf marketplace command or open Catalog.

getting-started-with-predix-catalog

 

Creating and binding service instances

To create a service instance for your application, use the cf create-service command:

cf create-service SERVICE PLAN SERVICE_INSTANCE

where:

  • SERVICE is the service name.
  • PLAN is the service plan.
  • SERVICE_INSTANCE is the unique name for the service instance.

Issue the following command in the terminal to create a PostgreSQL service instance—named rails-postgres—for the sample Rails application:

cf create-service postgres shared-nr rails-postgres

You can see a newly created service instance by running the cf services command or opening the default dev space in the Predix console.

getting-started-with-predix-rails-postgres

You have to bind your application to your service instance for integrating the application with the service instance. There are two options on how you can do this:

  • manually by using the cf bind-service command
  • automatically by providing the list of services needed for an application in the manifest.yml file.

To bind your application to the service instance manually, enter the following command (you need to ensure that your application is available in Predix):

cf bind-service APP_NAME SERVICE_INSTANCE

where:

  • APP_NAME is the name of the application you want to bind to your service instance.
  • SERVICE_INSTANCE is the name of the service instance you want to bind your application to.

Because the number of service instances you want to bind your application to can grow, I find it tedious to bind each service instance manually. You can specify the services sub-block in the applications block of the manifest.yml file to bind them automatically. Here’s the excerpt from the manifest.yml file of predix-rails-starter-app:

applications:
- name: predix-rails-starter-app
  ...
  services:
    - rails-postgres

When you bind your application to a service instance, Cloud Foundry provisions its connection details in the JSON encoded VCAP_SERVICES environment variable. So, to get access to the services, you have to parse JSON to get parameters you need. Here’s how it’s done in the config/database.yml file of the Rails starter application:

...
production:
  url: <%= JSON.parse(ENV['VCAP_SERVICES']).dig('postgres', 0, 'credentials', 'uri') if Rails.env.production? %>

 

Deploying the application to Predix

Specify the following terminal command to create a local copy of predix-rails-starter-app:

git clone https://github.com/Altoros/predix-rails-starter-app

The newly created directory contains the manifest.yml file, which assists you with deploying the application.

Go to the application directory and run the following command to deploy the application to Predix:

cf push APP_NAME

where APP_NAME is an optional application name that you can provide to override the application name specified in manifest.yml.

To view logs while deploying the application, you can launch a new terminal window and run the cf logs predix-rails-starter-app command.

 

Verifying the application

Running the cf push command creates a URL route to your application that you can retrieve by issuing the following command:

cf app APP_NAME

where APP_NAME is the application name.

Verify that the application is running by browsing to the URL generated in the output of the previous command. In this example, navigating to predix-rails-starter-app.run.aws-usw02-pr.ice.predix.io verifies that the application is running.

 

Further reading

 


The tutorial was created by Dzianis Dashkevich;
edited and published by Victoria Fedzkovich and Alex Khizhniak.
  •  
  •  
  •