Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 11. Using Ruby on Rails
Ruby on Rails is a web framework written in Ruby. This guide covers using Rails 4 on OpenShift Container Platform.
Go through the whole tutorial to have an overview of all the steps necessary to run your application on the OpenShift Container Platform. If you experience a problem try reading through the entire tutorial and then going back to your issue. It can also be useful to review your previous steps to ensure that all the steps were run correctly.
11.1. Prerequisites Link kopierenLink in die Zwischenablage kopiert!
- Basic Ruby and Rails knowledge.
- Locally installed version of Ruby 2.0.0+, Rubygems, Bundler.
- Basic Git knowledge.
- Running instance of OpenShift Container Platform 4.
-
Make sure that an instance of OpenShift Container Platform is running and is available. Also make sure that your CLI client is installed and the command is accessible from your command shell, so you can use it to log in using your email address and password.
oc
11.2. Setting up the database Link kopierenLink in die Zwischenablage kopiert!
Rails applications are almost always used with a database. For local development use the PostgreSQL database.
Procedure
Install the database:
$ sudo yum install -y postgresql postgresql-server postgresql-develInitialize the database:
$ sudo postgresql-setup initdbThis command creates the
directory, in which the data is stored./var/lib/pgsql/dataStart the database:
$ sudo systemctl start postgresql.serviceWhen the database is running, create your
user:rails$ sudo -u postgres createuser -s railsNote that the user created has no password.
11.3. Writing your application Link kopierenLink in die Zwischenablage kopiert!
If you are starting your Rails application from scratch, you must install the Rails gem first. Then you can proceed with writing your application.
Procedure
Install the Rails gem:
$ gem install railsExample output
Successfully installed rails-4.3.0 1 gem installedAfter you install the Rails gem, create a new application with PostgreSQL as your database:
$ rails new rails-app --database=postgresqlChange into your new application directory:
$ cd rails-appIf you already have an application, make sure the
(postgresql) gem is present in yourpg. If not, edit yourGemfileby adding the gem:Gemfilegem 'pg'Generate a new
with all your dependencies:Gemfile.lock$ bundle installIn addition to using the
database with thepostgresqlgem, you also must ensure that thepgis using theconfig/database.ymladapter.postgresqlMake sure you updated
section in thedefaultfile, so it looks like this:config/database.ymldefault: &default adapter: postgresql encoding: unicode pool: 5 host: localhost username: rails password: <password>Create your application’s development and test databases:
$ rake db:createThis creates
anddevelopmentdatabase in your PostgreSQL server.test
11.3.1. Creating a welcome page Link kopierenLink in die Zwischenablage kopiert!
Since Rails 4 no longer serves a static
public/index.html
To have a custom welcome page must do following steps:
- Create a controller with an index action.
- Create a view page for the welcome controller index action.
- Create a route that serves applications root page with the created controller and view.
Rails offers a generator that completes all necessary steps for you.
Procedure
Run Rails generator:
$ rails generate controller welcome indexAll the necessary files are created.
edit line 2 in
file as follows:config/routes.rbroot 'welcome#index'Run the rails server to verify the page is available:
$ rails serverYou should see your page by visiting http://localhost:3000 in your browser. If you do not see the page, check the logs that are output to your server to debug.
11.3.2. Configuring application for OpenShift Container Platform Link kopierenLink in die Zwischenablage kopiert!
To have your application communicate with the PostgreSQL database service running in OpenShift Container Platform you must edit the
default
config/database.yml
Procedure
Edit the
section in yourdefaultwith pre-defined variables as follows:config/database.ymlSample
config/databaseYAML file<% user = ENV.key?("POSTGRESQL_ADMIN_PASSWORD") ? "root" : ENV["POSTGRESQL_USER"] %> <% password = ENV.key?("POSTGRESQL_ADMIN_PASSWORD") ? ENV["POSTGRESQL_ADMIN_PASSWORD"] : ENV["POSTGRESQL_PASSWORD"] %> <% db_service = ENV.fetch("DATABASE_SERVICE_NAME","").upcase %> default: &default adapter: postgresql encoding: unicode # For details on connection pooling, see rails configuration guide # http://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV["POSTGRESQL_MAX_CONNECTIONS"] || 5 %> username: <%= user %> password: <%= password %> host: <%= ENV["#{db_service}_SERVICE_HOST"] %> port: <%= ENV["#{db_service}_SERVICE_PORT"] %> database: <%= ENV["POSTGRESQL_DATABASE"] %>
11.3.3. Storing your application in Git Link kopierenLink in die Zwischenablage kopiert!
Building an application in OpenShift Container Platform usually requires that the source code be stored in a git repository, so you must install
git
Prerequisites
- Install git.
Procedure
Make sure you are in your Rails application directory by running the
command. The output of the command should look like:ls -1$ ls -1Example output
app bin config config.ru db Gemfile Gemfile.lock lib log public Rakefile README.rdoc test tmp vendorRun the following commands in your Rails app directory to initialize and commit your code to git:
$ git init$ git add .$ git commit -m "initial commit"After your application is committed you must push it to a remote repository. GitHub account, in which you create a new repository.
Set the remote that points to your
repository:git$ git remote add origin git@github.com:<namespace/repository-name>.gitPush your application to your remote git repository.
$ git push
11.4. Deploying your application to OpenShift Container Platform Link kopierenLink in die Zwischenablage kopiert!
You can deploy you application to OpenShift Container Platform.
After creating the
rails-app
Deploying your application in OpenShift Container Platform involves three steps:
- Creating a database service from OpenShift Container Platform’s PostgreSQL image.
- Creating a frontend service from OpenShift Container Platform’s Ruby 2.0 builder image and your Ruby on Rails source code, which are wired with the database service.
- Creating a route for your application.
Procedure
To deploy your Ruby on Rails application, create a new project for the application:
$ oc new-project rails-app --description="My Rails application" --display-name="Rails Application"
11.4.1. Creating the database service Link kopierenLink in die Zwischenablage kopiert!
Your Rails application expects a running database service. For this service use PostgreSQL database image.
To create the database service, use the
oc new-app
- POSTGRESQL_DATABASE
- POSTGRESQL_USER
- POSTGRESQL_PASSWORD
Setting these variables ensures:
- A database exists with the specified name.
- A user exists with the specified name.
- The user can access the specified database with the specified password.
Procedure
Create the database service:
$ oc new-app postgresql -e POSTGRESQL_DATABASE=db_name -e POSTGRESQL_USER=username -e POSTGRESQL_PASSWORD=passwordTo also set the password for the database administrator, append to the previous command with:
-e POSTGRESQL_ADMIN_PASSWORD=admin_pwWatch the progress:
$ oc get pods --watch
11.4.2. Creating the frontend service Link kopierenLink in die Zwischenablage kopiert!
To bring your application to OpenShift Container Platform, you must specify a repository in which your application lives.
Procedure
Create the frontend service and specify database related environment variables that were setup when creating the database service:
$ oc new-app path/to/source/code --name=rails-app -e POSTGRESQL_USER=username -e POSTGRESQL_PASSWORD=password -e POSTGRESQL_DATABASE=db_name -e DATABASE_SERVICE_NAME=postgresqlWith this command, OpenShift Container Platform fetches the source code, sets up the builder, builds your application image, and deploys the newly created image together with the specified environment variables. The application is named
.rails-appVerify the environment variables have been added by viewing the JSON document of the
deployment config:rails-app$ oc get dc rails-app -o jsonYou should see the following section:
Example output
env": [ { "name": "POSTGRESQL_USER", "value": "username" }, { "name": "POSTGRESQL_PASSWORD", "value": "password" }, { "name": "POSTGRESQL_DATABASE", "value": "db_name" }, { "name": "DATABASE_SERVICE_NAME", "value": "postgresql" } ],Check the build process:
$ oc logs -f build/rails-app-1After the build is complete, look at the running pods in OpenShift Container Platform:
$ oc get podsYou should see a line starting with
, and that is your application running in OpenShift Container Platform.myapp-<number>-<hash>Before your application is functional, you must initialize the database by running the database migration script. There are two ways you can do this:
Manually from the running frontend container:
Exec into frontend container with
command:rsh$ oc rsh <frontend_pod_id>Run the migration from inside the container:
$ RAILS_ENV=production bundle exec rake db:migrateIf you are running your Rails application in a
ordevelopmentenvironment you do not have to specify thetestenvironment variable.RAILS_ENV
- By adding pre-deployment lifecycle hooks in your template.
11.4.3. Creating a route for your application Link kopierenLink in die Zwischenablage kopiert!
You can expose a service to create a route for your application.
Procedure
To expose a service by giving it an externally-reachable hostname like
use OpenShift Container Platform route. In your case you need to expose the frontend service by typing:www.example.com$ oc expose service rails-app --hostname=www.example.com
Ensure the hostname you specify resolves into the IP address of the router.