Openshift rails

Openshift rails DEFAULT

Rails Sample App on OpenShift

This is a quickstart Rails application for OpenShift v3 that you can use as a starting point to develop your own application and deploy it on an OpenShift cluster.

If you'd like to install it, follow these directions.

The steps in this document assume that you have access to an OpenShift deployment that you can deploy applications on.

OpenShift Considerations

These are some special considerations you may need to keep in mind when running your application on OpenShift.

Assets

Your application is set to precompile the assets every time you push to OpenShift. Any assets you commit to your repo will be preserved alongside those which are generated during the build.

By adding the environment variable value to your BuildConfig, you will disable asset compilation upon application deployment. See the Basic Build Operations documentation on setting environment variables for builds in OpenShift V3.

Security

Since these quickstarts are shared code, we had to take special consideration to ensure that security related configuration variables are unique across applications. To accomplish this, we modified some of the configuration files. Now instead of using the same default values, OpenShift can generate these values using the generate from logic defined within the template.

OpenShift stores these generated values in configuration files that only exist for your deployed application and not in your code anywhere. Each of them will be unique so initialize_secret(:a) will differ from initialize_secret(:b) but they will also be consistent, so any time your application uses them (even across reboots), you know they will be the same.

TLDR: OpenShift can generate and expose environment variables to your application automatically. Look at this quickstart for an example.

Development mode

When you develop your Rails application in OpenShift, you can also enable the 'development' environment by setting the RAILS_ENV environment variable for your deploymentConfiguration, using the client, like:

If you do so, OpenShift will run your application under 'development' mode. In development mode, your application will:

  • Show more detailed errors in the browser
  • Skip static assets (re)compilation

Development environment can help you debug problems in your application in the same way as you do when developing on your local machine. However, we strongly advise you to not run your application in this mode in production.

Installation

These steps assume your OpenShift deployment has the default set of ImageStreams defined. Instructions for installing the default ImageStreams are available here. If you are defining the set of ImageStreams now, remember to pass in the proper cluster-admin credentials and to create the ImageStreams in the 'openshift' namespace.

  1. Fork a copy of rails-ex

  2. Clone your repository to your development machine and cd to the repository directory

  3. Add a Ruby application from the rails template:

  4. Depending on the state of your system, and whether additional items need to be downloaded, it may take around a minute for your build to be started automatically. If you do not want to wait, run

  5. Once the build is running, watch your build progress

  6. Wait for rails-postgresql-example pods to start up (this can take a few minutes):

    Sample output:

  7. Check the IP and port the rails-postgresql-example service is running on:

    Sample output:

In this case, the IP for rails-postgresql-example rails-postgresql-example is 172.30.205.117 and it is on port 8080.
Note: you can also get this information from the web console.

Debugging Unexpected Failures

Review some of the common tips and suggestions here.

Adding Webhooks and Making Code Changes

Since OpenShift V3 does not provide a git repository out of the box, you can configure your github repository to make a webhook call whenever you push your code.

  1. From the Web Console homepage, navigate to your project
  2. Click on Browse > Builds
  3. Click the link with your BuildConfig name
  4. Click the Configuration tab
  5. Click the "Copy to clipboard" icon to the right of the "GitHub webhook URL" field
  6. Navigate to your repository on GitHub and click on repository settings > webhooks > Add webhook
  7. Paste your webhook URL provided by OpenShift
  8. Leave the defaults for the remaining fields - That's it!
  9. After you save your webhook, if you refresh your settings page you can see the status of the ping that Github sent to OpenShift to verify it can reach the server.

Enabling the Blog example

In order to access the example blog application, you have to remove the which serves as the welcome page and rebuild the application. Another option is to make a request directly to which will give you access to the blog.

The username/pw used for authentication in this application are openshift/secret.

Hot Deploy

In order to dynamically pick up changes made in your application source code, you need to set the parameter to the oc new-app command, while performing the installation steps described in this README.

To change your source code in the running container you need to oc rsh into it.

After you oc rsh into the running container, your current directory is set to , where the source code is located.

To set your application back to the environment you need to remove environment variable:

A redeploy will happen automatically due to the trigger.

NOTICE: If the trigger is not set, you need to run the redeploy manually:

Compatibility

This repository is compatible with Ruby 2.3 and higher, excluding any alpha or beta versions.

License

This code is dedicated to the public domain to the maximum extent permitted by applicable law, pursuant to CC0.

Sours: https://github.com/sclorg/rails-ex

Chapter 7. Application Tutorials

This topic group includes information on how to get your application up and running in OpenShift Container Platform and covers different languages and their frameworks.

7.2. Quickstart Templates

A quickstart is a basic example of an application running on OpenShift Container Platform. Quickstarts come in a variety of languages and frameworks, and are defined in a template, which is constructed from a set of services, build configurations, and deployment configurations. This template references the necessary images and source repositories to build and deploy the application.

To explore a quickstart, create an application from a template. Your administrator may have already installed these templates in your OpenShift Container Platform cluster, in which case you can simply select it from the web console. See the template documentation for more information on how to upload, create from, and modify a template.

Quickstarts refer to a source repository that contains the application source code. To customize the quickstart, fork the repository and, when creating an application from the template, substitute the default source repository name with your forked repository. This results in builds that are performed using your source code instead of the provided example source. You can then update the code in your source repository and launch a new build to see the changes reflected in the deployed application.

7.2.2. Web Framework Quickstart Templates

These quickstarts provide a basic application of the indicated framework and language:

Ruby on Rails is a popular web framework written in Ruby. This guide covers using Rails 4 on OpenShift Container Platform.

We strongly advise going 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 executed correctly.

For this guide you will need:

  • Basic Ruby/Rails knowledge
  • Locally installed version of Ruby 2.0.0+, Rubygems, Bundler
  • Basic Git knowledge
  • Running instance of OpenShift Container Platform v3

7.3.2. Local Workstation Setup

First make sure that an instance of OpenShift Container Platform is running and is available. For more info on how to get OpenShift Container Platform up and running check the installation methods. 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.

7.3.2.1. Setting Up the Database

Rails applications are almost always used with a database. For the local development we chose the PostgreSQL database. To install it type:

$ sudo yum install -y postgresql postgresql-server postgresql-devel

Next you need to initialize the database with:

$ sudo postgresql-setup initdb

This command will create the directory, in which the data will be stored.

Start the database by typing:

$ sudo systemctl start postgresql.service

When the database is running, create your user:

$ sudo -u postgres createuser -s rails

Note that the user we created has no password.

7.3.3. Writing Your Application

If you are starting your Rails application from scratch, you need to install the Rails gem first.

$ gem install rails Successfully installed rails-4.2.0 1 gem installed

After you install the Rails gem create a new application, with PostgreSQL as your database:

$ rails new rails-app --database=postgresql

Then change into your new application directory.

$ cd rails-app

If you already have an application, make sure the (postgresql) gem is present in your . If not edit your by adding the gem:

gem 'pg'

To generate a new with all your dependencies run:

$ bundle install

In addition to using the database with the gem, you’ll also need to ensure the is using the adapter.

Make sure you updated section in the file, so it looks like this:

default: &default adapter: postgresql encoding: unicode pool: 5 host: localhost username: rails password:

Create your application’s development and test databases by using this command:

$ rake db:create

This will create and database in your PostgreSQL server.

7.3.3.1. Creating a Welcome Page

Since Rails 4 no longer serves a static page in production, we need to create a new root page.

In order to have a custom welcome page we need to do following steps:

  • Create a controller with an index action
  • Create a view page for the controller action
  • Create a route that will serve applications root page with the created controller and view

Rails offers a generator that will do all this necessary steps for you.

$ rails generate controller welcome index

All the necessary files have been created, now we just need to edit line 2 in file to look like:

root 'welcome#index'

Run the rails server to verify the page is available.

$ rails server

You should see your page by visiting http://localhost:3000 in your browser. If you don’t see the page, check the logs that are output to your server to debug.

7.3.3.2. Configuring the Application for OpenShift Container Platform

In order to have your application communicating with the PostgreSQL database service that will be running in OpenShift Container Platform, you will need to edit the section in your to use environment variables, which you will define later, upon the database service creation.

The section in your edited together with pre-defined variables should look like:

<% 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"] %>

For an example of how the final file should look, see Ruby on Rails example applicationconfig/database.yml.

7.3.3.3. Storing Your Application in Git

OpenShift Container Platform requires git, if you don’t have it installed you will need to install it.

Building an application in OpenShift Container Platform usually requires that the source code be stored in a git repository, so you will need to install if you do not already have it.

Make sure you are in your Rails application directory by running the command. The output of the command should look like:

$ ls -1 app bin config config.ru db Gemfile Gemfile.lock lib log public Rakefile README.rdoc test tmp vendor

Now run these commands in your Rails app directory to initialize and commit your code to git:

$ git init $ git add . $ git commit -m "initial commit"

Once your application is committed you need to push it to a remote repository. For this you would need a GitHub account, in which you create a new repository.

Set the remote that points to your repository:

$ git remote add origin [email protected]:<namespace/repository-name>.git

After that, push your application to your remote git repository.

$ git push

7.3.4. Deploying Your Application to OpenShift Container Platform

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"

After creating the the project, you will be automatically switched to the new project namespace.

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 we wire with the database service
  • Creating a route for your application.

7.3.4.1. Creating the Database Service

Your Rails application expects a running database service. For this service use PostgeSQL database image.

To create the database service you will use the oc new-app command. To this command you will need to pass some necessary environment variables which will be used inside the database container. These environment variables are required to set the username, password, and name of the database. You can change the values of these environment variables to anything you would like. The variables we are going to be setting are as follows:

  • 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

For example:

$ oc new-app postgresql -e POSTGRESQL_DATABASE=db_name -e POSTGRESQL_USER=username -e POSTGRESQL_PASSWORD=password

To also set the password for the database administrator, append to the previous command with:

-e POSTGRESQL_ADMIN_PASSWORD=admin_pw

To watch the progress of this command:

$ oc get pods --watch

7.3.4.2. Creating the Frontend Service

To bring your application to OpenShift Container Platform, you need to specify a repository in which your application lives, using once again the command, in which you will need to specify database related environment variables we setup in the 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=postgresql

With this command, OpenShift Container Platform fetches the source code, sets up the builder image, builds your application image, and deploys the newly created image together with the specified environment variables. The application is named .

You can verify the environment variables have been added by viewing the JSON document of the DeploymentConfig:

$ oc get dc rails-app -o json

You should see the following section:

env": [ { "name": "POSTGRESQL_USER", "value": "username" }, { "name": "POSTGRESQL_PASSWORD", "value": "password" }, { "name": "POSTGRESQL_DATABASE", "value": "db_name" }, { "name": "DATABASE_SERVICE_NAME", "value": "postgresql" } ],

To check the build process:

$ oc logs -f build rails-app-1

Once the build is complete, you can look at the running pods in OpenShift Container Platform.

$ oc get pods

You should see a line starting with , and that is your application running in OpenShift Container Platform.

Before your application will be functional, you need to initialize the database by running the database migration script. There are two ways you can do this:

  • Manually from the running frontend container:

First you need to exec into frontend container with rsh command:

$ oc rsh <FRONTEND_POD_ID>

Run the migration from inside the container:

$ RAILS_ENV=production bundle exec rake db:migrate

If you are running your Rails application in a or environment you don’t have to specify the environment variable.

7.3.4.3. Creating a Route for Your Application

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:

$ oc expose service rails-app --hostname=www.example.com

It’s the user’s responsibility to ensure the hostname they specify resolves into the IP address of the router. For more information, check the OpenShift Container Platform documentation on:

7.4. Setting Up a Nexus Mirror for Maven

While developing your application with Java and Maven, you will most likely be building many times. In order to shorten the build times of your pods, Maven dependencies can be cached in a local Nexus repository. This tutorial will guide you through creating a Nexus repository on your cluster.

This tutorial assumes that you are working with a project that is already set up for use with Maven. If you are interested in using Maven with your Java project, it is highly recommended that you look at their guide.

In addition, be sure to check your application’s image for Maven mirror capabilities. Many images that use Maven have a environment variable that you can use to simplify this process. If it does not have this capability, read the Nexus documentation to configure your build properly.

Furthermore, make sure that you give each pod enough resources to function. You may have to edit the pod template in the Nexus deployment configuration to request more resources.

  1. Download and deploy the official Nexus container image:

    oc new-app sonatype/nexus
  2. Create a route by exposing the newly created Nexus service:

    oc expose svc/nexus
  3. Use oc get routes to find the pod’s new external address.

    oc get routes

    The output should resemble:

    NAME HOST/PORT PATH SERVICES PORT TERMINATION nexus nexus-myproject.192.168.1.173.xip.io nexus 8081-tcp
  4. Confirm that Nexus is running by navigating your browser to the URL under HOST/PORT. To sign in to Nexus, the default administrator username is admin, and the password is admin123.

7.4.2.1. Using Probes to Check for Success

This is a good time to set up readiness and liveness probes. These will periodically check to see that Nexus is running properly.

$ oc set probe dc/nexus \ --liveness \ --failure-threshold 3 \ --initial-delay-seconds 30 \ -- echo ok $ oc set probe dc/nexus \ --readiness \ --failure-threshold 3 \ --initial-delay-seconds 30 \ --get-url=http://:8081/nexus/content/groups/public

7.4.2.2. Adding Persistence to Nexus

If you do not want persistent storage, continue to Connecting to Nexus. However, your cached dependencies and any configuration customization will be lost if the pod is restarted for any reason.

Create a persistent volume claim (PVC) for Nexus, so that the cached dependencies are not lost when the pod running the server terminates. PVCs require available persistent volumes (PV) in the cluster. If there are no PVs available and you do not have administrator access on your cluster, ask your system administrator to create a Read/Write Persistent Volume for you. Otherwise, see Persistent Storage in OpenShift Container Platform for instructions on creating a persistent volume.

Add a PVC to the Nexus deployment configuration.

$ oc volumes dc/nexus --add \ --name 'nexus-volume-1' \ --type 'pvc' \ --mount-path '/sonatype-work/' \ --claim-name 'nexus-pv' \ --claim-size '1G' \ --overwrite

This removes the previous emptyDir volume for the deployment config and adds a claim for one gigabyte of persistent storage mounted at , which is where the dependencies will be stored. Due to the change in configuration, the Nexus pod will be redeployed automatically.

To verify that Nexus is running, refresh the Nexus page in your browser. You can monitor the deployment’s progress using:

$ oc get pods -w

7.4.3. Connecting to Nexus

The next steps demonstrate defining a build that uses the new Nexus repository. The rest of the tutorial uses this example repository with wildfly-100-centos7 as a builder, but these changes should work for any project.

The example builder image supports as part of its environment, so we can use this to point our builder image to our Nexus repository. If your image does not support consuming an environment variable to configure a Maven mirror, you may need to modify the builder image to provide the correct Maven settings to point to the Nexus mirror.

$ oc new-build openshift/wildfly-100-centos7:latest~https://github.com/openshift/jee-ex.git \ -e MAVEN_MIRROR_URL='http://nexus.<Nexus_Project>:8081/nexus/content/groups/public' $ oc logs build/jee-ex-1 --follow

Replace with the project name of the Nexus repository. If it is in the same project as the application that is using it, you can remove the . Learn more about DNS resolution in OpenShift Container Platform.

7.4.4. Confirming Success

In your web browser, navigate to http://<NexusIP>:8081/nexus/content/groups/public to confirm that it has stored your application’s dependencies. You can also check the build logs to see if Maven is using the Nexus mirror. If successful, you should see output referencing the URL `http://nexus:8081.

7.4.5. Additional Resources

Sours: https://access.redhat.com/documentation/en-us/openshift_container_platform/3.5/html/developer_guide/application-tutorials
  1. H11 retrofit projector
  2. Mobilityworks phone number
  3. Hyundai newton nj
  4. Clang type

Chapter 9. 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 executed correctly.

  • 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.

9.2. Setting up the database

Rails applications are almost always used with a database. For the local development use the PostgreSQL database.

Procedure

  1. Install the database:

    $ sudo yum install -y postgresql postgresql-server postgresql-devel
  2. Initialize the database:

    $ sudo postgresql-setup initdb

    This command will create the directory, in which the data will be stored.

  3. Start the database:

    $ sudo systemctl start postgresql.service
  4. When the database is running, create your user:

    $ sudo -u postgres createuser -s rails

    Note that the user created has no password.

9.3. Writing your application

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

  1. Install the Rails gem:

    $ gem install rails Successfully installed rails-4.3.0 1 gem installed
  2. After you install the Rails gem, create a new application with PostgreSQL as your database:

    $ rails new rails-app --database=postgresql
  3. Change into your new application directory:

    $ cd rails-app
  4. If you already have an application, make sure the (postgresql) gem is present in your . If not, edit your by adding the gem:

    gem 'pg'
  5. Generate a new with all your dependencies:

    $ bundle install
  6. In addition to using the database with the gem, you also must ensure that the is using the adapter.

    Make sure you updated section in the file, so it looks like this:

    default: &default adapter: postgresql encoding: unicode pool: 5 host: localhost username: rails password:
  7. Create your application’s development and test databases:

    $ rake db:create

    This will create and database in your PostgreSQL server.

9.3.1. Creating a welcome page

Since Rails 4 no longer serves a static page in production, you must create a new root page.

In order to have a custom welcome page must do following steps:

  • Create a controller with an index action
  • Create a view page for the controller action
  • Create a route that will serve applications root page with the created controller and view

Rails offers a generator that will do all necessary steps for you.

Procedure

  1. Run Rails generator:

    $ rails generate controller welcome index

    All the necessary files are created.

  2. edit line 2 in file as follows:

    root 'welcome#index'
  3. Run the rails server to verify the page is available:

    $ rails server

    You 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.

9.3.2. Configuring application for OpenShift Container Platform

To have your application communicate with the PostgreSQL database service running in OpenShift Container Platform you must edit the section in your to use environment variables, which you will define later, upon the database service creation.

Procedure

  • Edit the section in your with pre-defined variables as follows:

    <% 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"] %>

9.3.3. Storing your application in Git

Building an application in OpenShift Container Platform usually requires that the source code be stored in a git repository, so you must install if you do not already have it.

Prerequisites

Procedure

  1. Make sure you are in your Rails application directory by running the command. The output of the command should look like:

    $ ls -1 app bin config config.ru db Gemfile Gemfile.lock lib log public Rakefile README.rdoc test tmp vendor
  2. Run 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.

  1. Set the remote that points to your repository:

    $ git remote add origin [email protected]:<namespace/repository-name>.git
  2. Push your application to your remote git repository.

    $ git push

9.4. Deploying your application to OpenShift Container Platform

You can deploy you application to OpenShift Container Platform.

After creating the project, you will be automatically switched to the new project namespace.

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"

9.4.1. Creating the database service

Your Rails application expects a running database service. For this service use PostgeSQL database image.

To create the database service you will use the command. To this command you must pass some necessary environment variables which will be used inside the database container. These environment variables are required to set the username, password, and name of the database. You can change the values of these environment variables to anything you would like. The variables are as follows:

  • 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

  1. Create the database service:

    $ oc new-app postgresql -e POSTGRESQL_DATABASE=db_name -e POSTGRESQL_USER=username -e POSTGRESQL_PASSWORD=password

    To also set the password for the database administrator, append to the previous command with:

    -e POSTGRESQL_ADMIN_PASSWORD=admin_pw
  2. Watch the progress:

    $ oc get pods --watch

9.4.2. Creating the frontend service

To bring your application to OpenShift Container Platform, you must specify a repository in which your application lives.

Procedure

  1. 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=postgresql

    With 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 .

  2. Verify the environment variables have been added by viewing the JSON document of the DeploymentConfig:

    $ oc get dc rails-app -o json

    You should see the following section:

    env": [ { "name": "POSTGRESQL_USER", "value": "username" }, { "name": "POSTGRESQL_PASSWORD", "value": "password" }, { "name": "POSTGRESQL_DATABASE", "value": "db_name" }, { "name": "DATABASE_SERVICE_NAME", "value": "postgresql" } ],
  3. Check the build process:

    $ oc logs -f build/rails-app-1
  4. Once the build is complete, look at the running pods in OpenShift Container Platform:

    $ oc get pods

    You should see a line starting with , and that is your application running in OpenShift Container Platform.

  5. Before your application will be 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:

        $ oc rsh <FRONTEND_POD_ID>
      • Run the migration from inside the container:

        $ RAILS_ENV=production bundle exec rake db:migrate

        If you are running your Rails application in a or environment you do not have to specify the environment variable.

    • By adding pre-deployment lifecycle hooks in your template.

9.4.3. Creating a route for your application

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:

    $ oc expose service rails-app --hostname=www.example.com

Ensure the hostname you specify resolves into the IP address of the router.

Sours: https://access.redhat.com/documentation/en-us/openshift_container_platform/4.3/html/images/templates-using-ruby-on-rails
OpenShift Pipelines: Adding CI/CD to your Red Hat OpenShift Deployments
  • Create a Ruby 1.9 application rhc app create -a railsdemo -t ruby-1.9
  • After running the command railsdemo folder will get created in your directory. Run the command shown below. This will generate rails code in the railsdemo folder.It will ask you whether you want to override, say yes. rails new railsdemo
  • Next run commands shown below cd railsdemo bundle install rails generate controller home index rm public/index.html
  • Add the following route to config/routes.rb: root :to => "home#index"
  • Generate Post entity with fields as shown below rails generate scaffold Post name:string title:string content:text
  • Add Database support by uncommenting following lines from .openshift/action_hooks/deploy script pushd ${OPENSHIFT_REPO_DIR} > /dev/null bundle exec rake db:migrate RAILS_ENV="production" popd > /dev/null
  • Add mysql cartridge rhc cartridge add -a railsdemo -c mysql-5.1
  • Update config/database.yml production: adapter: mysql2 encoding: utf8 database: <%=ENV['OPENSHIFT_APP_NAME']%> pool: 5 host: <%=ENV['OPENSHIFT_MYSQL_DB_HOST']%> port: <%=ENV['OPENSHIFT_MYSQL_DB_PORT']%> username: <%=ENV['OPENSHIFT_MYSQL_DB_USERNAME']%> password: <%=ENV['OPENSHIFT_MYSQL_DB_PASSWORD']%> socket: <%=ENV['OPENSHIFT_MYSQL_DB_SOCKET']%>
  • Add gem in Gemfile gem 'mysql2'
  • Add, Commit, and Push the Code git add . git commit -a -m "Initial setup" git push
  • Finally you can view the application running at http://railsdemo-domainname.rhcloud.com and you can create new posts at http://railsdemo-domainname.rhcloud.com/posts
  • Sours: https://shekhargulati.com/2012/12/26/openshift-rails-quickstart/

    Rails openshift

    Creating the Frontend Service

    $ oc new-app path/to/source/code --name=rails-app -e POSTGRESQL_USER=username -e POSTGRESQL_PASSWORD=password -e POSTGRESQL_DATABASE=db_name

    With this command, OpenShift Enterprise fetches the source code, sets up the Builder image, builds your application image, and deploys the newly created image together with the specified environment variables. The application is named .

    You can verify the environment variables have been added by viewing the JSON document of the DeploymentConfig:

    $ oc get dc rails-app -o json

    You should see the following section:

    env": [ { "name": "POSTGRESQL_USER", "value": "username" }, { "name": "POSTGRESQL_PASSWORD", "value": "password" }, { "name": "POSTGRESQL_DATABASE", "value": "db_name" } ],
    $ oc logs -f build rails-app-1

    Once the build is complete, you can look at the running pods in OpenShift Enterprise:

    You should see a line starting with myapp-(#number)-(some hash) and that is your application running in OpenShift Enterprise.

    Before your application will be functional, you need to initialize the database by running the database migration script. There are two ways you can do this:

    • Manually from the running frontend container:

    First you need to exec into frontend container with rsh command:

    $ oc rsh <FRONTEND_POD_ID>

    Run the migration from inside the container:

    $ RAILS_ENV=production bundle exec rake db:migrate

    If you are running your Rails application in a or environment you don’t have to specify the environment variable.

    Creating a Route for Your Application

    To expose a service by giving it an externally-reachable hostname like , use an OpenShift Enterprise route. In your case you need to expose the frontend service by typing:

    $ oc expose service rails-app --hostname=www.example.com

    It’s the user’s responsibility to ensure the hostname they specify resolves into the IP address of the router. For more information, check the OpenShift Enterprise documentation on:

    Sours: https://docs.openshift.com/enterprise/3.1/dev_guide/app_tutorials/ruby_on_rails.html
    Ask an OpenShift Admin (Ep 39): Backup and disaster recovery

    Screenshot 2015-09-13 10.24.03

    OpenShift is Red Hat’s Platform-as-a-Service (PaaS) that allows developers to quickly develop, host, and scale applications in a cloud environment. With OpenShift you have choice of offerings, including online, on-premise, and open source project options.

    With Git, developers can deploy web applications in different languages on the platform. OpenShift makes deployment hassle-free and it is also a free and open source software.

    In this tutorial, I’ll make a Rails app with a static page and deploy it to the OpenShift platform. The purpose of this tutorial is to see how the deployment process works.

    OpenShift Setup

    Create an account for free. A free account gives you 3 small gears with a storage capacity of 1gb per gear. OpenShift Gears are secure containers where applications run within OpenShift Nodes. Nodes are instances of Red Hat Enterprise Linux, which is the foundation of OpenShift. In other words, your application resides on Nodes and runs in secure containers within the nodes called Gears.

    Other pricing options exists if you need something different, you can compare plans to know more.

    Install rhc

    Step one is to install the rhc gem, which contains the OpenShift command line tools:

    This installs the gem rhc and its dependencies. Just like git, rhc has a global configuration that can be completed with:

    (Assuming you are installing rhc for the first time, which I believe you are.)

    You’ll get a prompt asking you to specify your own Openshift server. For this tutorial, we’ll user the server for Openshift online: openshift.redhat.com. Just hit enter to accept the default.

    Next, enter the OpenShift login credentials you used in registering.

    If you type , a token will be created and stored on disk. This token allows you to access the server without using your password.

    Finally, enter to upload your public SSH key to the OpenShift server to allow key-based SSH access.

    NameSpace

    The final step to setting this up involves creating a domain. In Openshift, applications are grouped into domains. To create an application, you must first create a domain. Each domain has a unique name, called a namespace.

    An OpenShift Online namespace forms part of an application’s URL and is unique to your account. The syntax for an application URL is . Each username supports a single namespace, but you can create multiple applications within the namespace. If you need multiple namespaces, you need to create multiple accounts using different usernames. Note, you must create a namespace before you can create an application.

    OpenShift Online uses a blacklist to restrict the namespaces available to you. The blacklist is maintained on the server. A message warns you that you have chosen a blacklisted name and asks you to choose a different namespace if you try to create or alter a namespace using a blacklisted name. Namespaces may contain a maximum of 16 alphanumeric characters and may not have spaces or symbols.

    Enter a unique name after the prompt and you are done. That is all, rhc is now configured.

    Generate a Rails App

    We need something to deploy. Let’s create a little Rails app to play with on OpenShift. I presume you have the gem installed, if not, will get you going.

    Generate the Rails app:

    This creates a with a method and view. Edit the new page:

    Change the route root to:

    We want to use Puma as the web server and PostgreSQL for the database. Puma is one of the web servers OpenShift supports. You can choose to use other web servers, such as Unicorn, Thin, or Passenger.

    Now go to the Gemfile and add the following:

    Run after that.

    OpenShifting the Rails App

    At this point, we want to create a new remote for our app. You need to run this command from the directory above your Rails app (~/parent-directory/rails-app). This is to prevent the creation of a new directory in your app:

    Note: ‘tent’ is the name of the app.

    Edit the production configuration in database.yml as shown below:

    Preparing to Deploy

    Now let’s initialize our local repo:

    Check the details of your app, you’ll need it for deployment:

    tentapp

    Get the git URL and replace with it in the following command:

    Let’s merge the remote:

    You should get a conflict in config.ru, we need to ensure it contains the default content. Open your config.ru and paste in the following:

    Then, commit your changes:

    I have created an .openshift directory as an example that you can find here. Extract it and override the .openshift directory in your app. The structure of this directory looks like:

    openshift-directory

    The .openshift directory, located in the home directory of your app is a hidden directory where a user can create action hooks, set markers, and create cron jobs. Action hooks are scripts that are executed directly and can be written in Python, PHP, Ruby, bash, etc.

    The example repository above also has a config folder with a file. This file configures Puma and has the following content:

    Copy this file to your local config directory.

    Deploy the App

    Push the app to the remote server:

    Note: If you get an error while pushing, repeat the pull request and push again.

    Now your application is online. Just visit the application URL from your browser, which is http://app-nerdslab.rhcloud.com. For my ‘tent’ app, it’s:

    http://tent-nerdslab.rhcloud.com

    Migrating the Database

    To run a database migration, we need to ssh into our server and set it up:

    If you experience any issues, you can always ssh into your server to fix it using these same commands. Remember that tent in the command above should be replaced with the name of your application.

    Checking the Logs

    If you ever encounter errors, you’ll want to access the application logs. To check the logs:

    Changing the Web Server

    The OpenShift ruby cartridge supports, by default, only Passenger running on Apache. But the Advanced Ruby cartridge allows you to use other popular servers, such as Puma, Unicorn, Thin, Rainbows, and, Passenger. In order to change it, we will use a feature of which allows us to change environment variables. The name of your chosen web server lives in the environment variable.

    To select Puma:

    To take effect, you have to restart the application:

    You can check which server is running by using this command:

    If you are using a Gemfile, ensure you have added the appropriate gem of your selected web server. In this case, it looks like:

    Conclusion

    There are lots of options for hosting a Rails app besides OpenShift. There is a post on SitePoint about Shelly Cloud by Jesse Herrick. Here’s another post on deploying a Rails application to Amazon Web Services (AWS) . Of course, there’s always Heroku. Now, I can recommend OpenShift as a hosting provider for easy deployment.

    Sours: https://www.sitepoint.com/deploy-your-rails-to-openshift/

    You will also like:

    Put Your App Online With OpenShift

    Created by Katie Miller, @codemiller

    Get OpenShift

    OpenShift is a cloud computing Platform as a Service (PaaS) that makes it easy to deploy apps online. It is open source and written in Ruby.

    To get started create an OpenShift Online account, which allows you to put three apps online for free. Once you are signed up, install the OpenShift RHC Client Tools by running these commands in a terminal and following the prompts:

    The above instructions assume you installed Ruby using RVM or RailsInstaller. If you used another approach, there is more info about installing RHC for different set-ups in this guide (you may need to do ).

    COACH: Talk about the benefits of deploying to a PaaS such as OpenShift, as opposed to traditional servers. Discuss SSH and why we need to upload a public key to communicate securely.

    Preparing your app

    Create OpenShift application

    We are going to create an OpenShift Ruby application with a PostgreSQL database, using a sample OpenShift Rails application as our starting point. Before we do that, in your terminal change to the parent directory of the one containing your code, probably called . The command below will take you there if you are currently in your directory; if not, substitute another command.

    The output from the or ‘present working directory’ command should show you are now in the directory (or whatever your parent directory was called). To create the OpenShift app in the cloud and make a local copy of its contents, run the following command in your terminal.

    NB: This command is for those using Ruby 2.x and Rails 4. If you have installed Ruby 1.9.x, replace in the command with . For Rails 3, change the URL to .

    If you see a message like , type and press enter.

    The terminal output should include a URL; open a browser window and go to the application URL to view the sample Rails application (the URL will have the form http://openshiftapp-yourdomain.rhcloud.com).

    COACH: Explain what Git is and why we use version control systems.

    Add version control

    We now have a sample app running in the cloud, but we actually need only a few pieces from its codebase. Before we copy across the bits we need, we should put our Rails Girls app under version control with Git.

    Change back to your app directory and initialize it as a Git repository with the following commands:

    We don’t want the pictures uploaded during app development to be part of our repository, so run the following command to instruct Git to ignore them:

    Add and commit all your app files to the Git repository with the following commands:

    COACH: Explain the Git commands used and .gitignore.

    Copy sample app code

    We need the directory and file from the sample application for our Rails app to run on OpenShift. Copy these from the directory to the directory. You can use Windows Explorer or another graphical file system tool to do this if you like, or alternatively run the following commands from the directory in your terminal:

    Check that the copying has worked by looking in the app directory. There should now be a subdirectory called . Open the file ; it should now contain OpenShift environment variables such as . If your file does not contain variables like this, try opening or in your editor and copying across the contents of the file.

    Add and commit the new and changed files in Git with the below commands.

    Change database

    The next step is to change our Rails Girls app database from SQLite to PostgreSQL. Open your application’s and replace:

    with

    Do a bundle to set up your dependencies:

    On some platforms, this may generate platform-specific versions of your Gems that cause issues when you push your app to the cloud. To prevent this, open your file and check the versions of the ‘sqlite3’ and ‘pg’ Gems. If they have a platform-specific suffix, such as , remove this (eg. change to and to ). Save and close the file, and run the above bundle command again before continuing.

    Add and commit your changes in Git:

    COACH: Talk about relational databases and the differences between SQLite and PostgreSQL.

    Deploy app to OpenShift

    We are now ready to deploy the Rails Girls app to OpenShift. We need to tell our Git repository where to push the code. To get the location of your OpenShift code repository, run the following command, and copy the Git URL from the output.

    Now run the following commands, replacing the SSH string with your Git URL. We are using ‘-f’ for force here because we are happy to wipe away the history of the current OpenShift repository, which contains the sample Rails app. When you are pushing future changes, you can just use ‘git push’.

    Refresh the app in your browser to see the result.

    COACH: Talk about Git remotes.

    Congratulations - your Rails application is now online for the whole world to admire. The following sections explain optional further steps you can take to improve and share your app.

    Persist uploaded images

    The app should be looking pretty good now, but there is an issue lurking because of the ephemeral nature of the deployment. When we push a new version of the application, anything stored within OpenShift’s copy of the repo will be wiped to make way for the new files. This includes the images uploaded by users. To fix this, we can store these files in a persistent directory on OpenShift instead. The filepath of the location we need is stored in an environment variable.

    COACH: Explain the motivation for using environment variables.

    The directory where uploaded pictures are currently stored is within the app repository, so it will be deleted when we rebuild. To switch the uploads directory to one that will persist, open and replace

    with

    Now uploaded images will be stored in a persistent directory, but they will still be available through the same URL as what we were using previously. To make this work, we also need to add a symbolic link on the filesystem from the repository location to the real storage location. To do this, open and add the following code:

    This action hook code will run every time the OpenShift app is built, so the link between the directories will always be there when it’s needed.

    Commit your changes and push them to the cloud:

    The images you uploaded before making this change will no longer display, but anything uploaded now will stick around between app rebuilds.

    COACH: Explain symbolic links.

    Push code to GitHub

    Now that your application is under source control with Git, you may also wish to share a copy with others on a Git repository website such as GitHub. To push your code to a GitHub repository, create a repository on GitHub and copy the HTTPS string (something like https://github.com/username/reponame.git).

    Navigate to your OpenShift app repository in the terminal and enter the following commands, replacing the HTTPS location with the string you copied:

    The ‘master’ branch of the local copy of your repository will be pushed to GitHub. Go to the GitHub website to check it out.

    COACH: Talk about Git branches and the benefits of open source code.

    Conclusion

    Your Rails app is now running in the cloud on OpenShift. You can push whatever other changes you like and share the URL to show off your app to your friends.


    Other Guides

    Sours: https://guides.railsgirls.com/openshift


    594 595 596 597 598