rhc-create-app

Documento bajo revisión/actualización

Ayuda en línea

$ rhc-create-app -h
Obtaining list of cartridges (please excuse the delay)...

Usage: /usr/bin/rhc-create-app
Create an OpenShift Express app.

  -a|--app   application     Application name  (alphanumeric - max 32 chars) (required)
  -t|--type  type            Type of app to create (raw-0.1, php-5.3, jbossas-7.0, jenkins-1.4, python-2.6, perl-5.10, ruby-1.8) (required)
  -l|--rhlogin  rhlogin      Red Hat login (RHN or OpenShift login with OpenShift Express access) (Default: ggomix)
  -p|--password  password    RHLogin password  (optional, will prompt)
  -r|--repo  path            Git Repo path (defaults to ./$app_name)
  -n|--nogit                 Only create remote space, don't pull it locally
  -d|--debug                 Print Debug info
  -h|--help                  Show Usage info
  --no-dns                   Skip DNS check. Must be used in combination with --nogit
  --config  path             Path of alternate config file
  --timeout #                Timeout, in seconds, for connection
  --enable-jenkins [name]    Indicates to create a Jenkins application (if not already available)
                             and embed the Jenkins client into this application.  The default 
                             name will be 'jenkins' if not specified. Note that --no-dns is ignored
                             for the creation of the Jenkins application.

Creación aplicación tipo Ruby 1.8

$ rhc-create-app -a ruby18demo -t ruby-1.8 -l ggomix
Password: 
Creating application: ruby18demo
Now your new domain name is being propagated worldwide (this might take a minute)...
    retry # 6 - Waiting for DNS: ruby18demo-gomix.rhcloud.com
Warning: Permanently added 'ruby18demo-gomix.rhcloud.com,204.236.252.187' (RSA) to the list of known hosts.
Confirming application 'ruby18demo' is available:  Success!

ruby18demo published:  http://ruby18demo-gomix.rhcloud.com/
git url:  ssh://e5f597d537d14ecfa037a8af4a9cc500@ruby18demo-gomix.rhcloud.com/~/git/ruby18demo.git/
Successfully created application: ruby18demo

Ya con esto tenemos nuestra app ruby 1.8 (rack) arriba (el pantallazo no es exacto en el url):

En paralelo se ha creado un directorio ruby18demo con su aplicación enlazada a Openshift.

$ cd one
$ ls -al
total 52
drwxr-xr-x  6 gomix gomix  4096 dic 28 09:23 .
drwxrwxr-x 10 gomix gomix  4096 dic 28 09:23 ..
-rw-rw-r--  1 gomix gomix 16872 dic 28 09:23 config.ru
drwxrwxr-x  8 gomix gomix  4096 dic 28 09:23 .git
drwxrwxr-x  4 gomix gomix  4096 dic 28 09:23 .openshift
drwxrwxr-x  2 gomix gomix  4096 dic 28 09:23 public
-rw-rw-r--  1 gomix gomix  2910 dic 28 09:23 README
drwxrwxr-x  2 gomix gomix  4096 dic 28 09:23 tmp

Note que debe usar GIT para subir cambios a su aplicación, igualmente note que también se ha creado un archivo Rackup para usted que puede utilizar localmente para correr su aplicación si lo desea.

config.ru

 1require 'rack/lobster'
 2
 3map '/health' do
 4  health = proc do |env|
 5    [200, { "Content-Type" => "text/html" }, ["1"]]
 6  end
 7  run health
 8end
 9
10map '/lobster' do
11  run Rack::Lobster.new
12end
13
14map '/' do
15  welcome = proc do |env|
16    [200, { "Content-Type" => "text/html" }, ["<!doctype html>
17<html lang=\"en\">
18<head>
19  <meta charset=\"utf-8\">
20  <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">
21  <title>Welcome to OpenShift</title>
22  <style>
23  html { background: black; }
24  body {
25    background: #333;
26    background: -webkit-linear-gradient(top, black, #666);
27    background: -o-linear-gradient(top, black, #666);
28  ...
29  ...
30  ...  
31  ul { margin: 0; padding: 0; }
32  li {
33    list-style-type: none;
34    padding: .5em 0;
35  }
36  </style>
37</head>
38<body>
39  <img
40    alt=\"OpenShift logo\" 
41    src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAABKCAYAAACVbQIxAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz
42AAAWcQAAFnEBAJr2QQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAACAASURB
43VHic7Z15nBxVtce/p7pnsj4IZOkJyQwhPSQhQCQJSIILnbCjsigh74kooIAgKoqiggooCj71RXyK
44...
45...
46...
47W16LN5ZzKl4QD6YYj8FrAbyciMc6Vcry+O6PJxwj8MYzlgSTBZLeyvYTcvwLXqujHu9zS+axGQM+
48jrcq3gYOyVkvcjjwUWA53iD6KaGAhISEvC/xg+QaYFQiHivqXVAh7w7hLxKGhIS8XzkHeCoUj/cv
49/w+9BQu2G5s85QAAAABJRU5ErkJggg==\">
50  <h1>
51    Welcome to OpenShift
52  </h1>
53  <p>
54    Place your application here
55  </p>
56  <p>
57    In order to commit to your new project, go to your projects git repo (created with the rhc-create-app command).  Make your changes, then run:
58  </p>
59  &lt;pre&gt;
60    git commit -a -m 'Some commit message'
61    git push
62  &lt;/pre&gt;
63  &lt;p&gt;
64    Then reload this page.
65  &lt;/p&gt;
66
67  &lt;h2&gt;
68    What's next?
69  &lt;/h2&gt;
70  &lt;ul&gt;
71    &lt;li&gt;
72      Why not visit us at &lt;a href=\"http://openshift.redhat.com\"&gt;http://openshift.redhat.com&lt;/a&gt;, or
73    &lt;/li&gt;
74    &lt;li&gt;
75      You could get help in the &lt;a href=\"http://www.redhat.com/openshift\"&gt;OpenShift forums&lt;/a&gt;, or
76    &lt;/li&gt;
77    &lt;li&gt;
78      You're welcome to come chat with us in our IRC channel at #openshift on freenode.net
79    &lt;/li&gt;
80  &lt;/ul&gt;
81&lt;/body&gt;
82&lt;/html&gt;
83"]]
84  end
85  run welcome
86end

Hagamos un cambio en config.ru y usemos git para empujar el cambio.

[gomix@movix one]$ git commit config.ru
[master bd2ef9c] sin logo
 1 files changed, 133 insertions(+), 317 deletions(-)
 rewrite config.ru (83%)
[gomix@movix one]$ git push
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 304 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Stopping application...
remote: Waiting for stop to finish
remote: Waiting for stop to finish
remote: Waiting for stop to finish
remote: Waiting for stop to finish
remote: Waiting for stop to finish
remote: Waiting for stop to finish
remote: Done
remote: Running .openshift/action_hooks/pre_build
remote: Running .openshift/action_hooks/build
remote: Running .openshift/action_hooks/deploy
remote: Starting application...
remote: Done
remote: Running .openshift/action_hooks/post_deploy
To ssh://3626c2aeac3e4d9baca2684d6722a409@one-gomix.rhcloud.com/~/git/one.git/
   49a17a6..bd2ef9c  master -> master

Rails encima de Rack

Por omisión, hoy día el tipo ruby-1.8 es "rack", de ahí que para instalar Rails, sólo debemos montárselo encima.

$ git mv config.ru config.ru.old
$ git mv README README.openshift
$ git commit -a -m "para preservar estos archivos" 
$ git push
...
$ rails new .                # POR FAVOR NOTE EL PUNTO, SE CREARA EL ESQUELETO RAILS3 EN EL DIRECTORIO ACTUAL
       exist  
      create  README
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/images/rails.png
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/mailers
      create  app/models
      create  app/views/layouts/application.html.erb
      create  app/mailers/.gitkeep
      create  app/models/.gitkeep
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  lib/assets
      create  lib/assets/.gitkeep
      create  log
      create  log/.gitkeep
       exist  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt
      create  script
      create  script/rails
      create  test/fixtures
      create  test/fixtures/.gitkeep
      create  test/functional
      create  test/functional/.gitkeep
      create  test/integration
      create  test/integration/.gitkeep
      create  test/unit
      create  test/unit/.gitkeep
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.gitkeep
      create  vendor/plugins
      create  vendor/plugins/.gitkeep
         run  bundle install
Enter your password to install the bundled RubyGems to your system:     < pass sudo para su usuario
...
Using rack-ssl (1.3.2) 
Installing json (1.6.4) with native extensions 
...
...
...
Installing rdoc (3.12) 
Using thor (0.14.6) 
Using railties (3.1.3) 
Installing coffee-rails (3.1.1) 
Installing jquery-rails (1.0.19) 
Using rails (3.1.3) 
Installing sass (3.1.12) 
Installing sass-rails (3.1.5) 
Using sqlite3 (1.3.5) 
Installing uglifier (1.2.1) 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

Note que Rails 3 ha creado el archivo Gemfile para que pueda usar el bundler.

Dependiendo de la versión Rails en su sistema puede que se haya o no ejecutado bundle install. Si no se ha ejecutado, es momento de hacerlo.

$ bundle install
...
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

Gemfile

 1source 'http://rubygems.org'
 2
 3gem 'rails', '3.0.10'
 4
 5# Bundle edge Rails instead:
 6# gem 'rails', :git => 'git://github.com/rails/rails.git'
 7
 8gem 'sqlite3'
 9
10# Use unicorn as the web server
11# gem 'unicorn'
12
13# Deploy with Capistrano
14# gem 'capistrano'
15
16# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
17# gem 'ruby-debug'
18# gem 'ruby-debug19', :require => 'ruby-debug'
19
20# Bundle the extra gems:
21# gem 'bj'
22# gem 'nokogiri'
23# gem 'sqlite3-ruby', :require => 'sqlite3'
24# gem 'aws-s3', :require => 'aws/s3'
25
26# Bundle gems for the local environment. Make sure to
27# put test-only gems in this group so their generators
28# and rake tasks are available in development mode:
29# group :development, :test do
30#   gem 'webrat'
31# end

Agregue los nuevos archivos al repositorio git de su proyecto:

$ git add .
$ git commit -a -m "Configuración inicial Rails" 
[master 2e2c88e] Configuración inicial Rails
 40 files changed, 10298 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 Gemfile
 create mode 100644 Gemfile.lock
 create mode 100644 README
 create mode 100644 Rakefile
 create mode 100644 app/controllers/application_controller.rb
 create mode 100644 app/helpers/application_helper.rb
 create mode 100644 app/views/layouts/application.html.erb
...
$ git push 
Counting objects: 63, done.
Compressing objects: 100% (48/48), done.
Writing objects: 100% (61/61), 86.29 KiB, done.
Total 61 (delta 2), reused 0 (delta 0)
remote: Stopping application...
remote: Waiting for stop to finish
remote: Waiting for stop to finish
remote: Waiting for stop to finish
remote: Done
remote: Running .openshift/action_hooks/pre_build
remote: Bundling RubyGems based on Gemfile/Gemfile.lock to repo/vendor/bundle with 'bundle install --deployment'
remote: Fetching source index for http://rubygems.org/
remote: which: no sudo in (/usr/libexec/li/cartridges/ruby-1.8/info/bin/:/usr/libexec/li/cartridges/abstract-httpd/info/bin/:/usr/libexec/li/cartridges/abstr
act/info/bin/:/sbin:/usr/sbin:/bin:/usr/bin)
remote: Installing rake (0.9.2.2) 
remote: Installing abstract (1.0.0) 
remote: Installing activesupport (3.0.10) 
remote: Installing builder (2.1.2) 
remote: Installing i18n (0.5.0) 
remote: Installing activemodel (3.0.10) 
remote: Installing erubis (2.6.6) 
remote: Installing rack (1.4.1) 
remote: Installing rack-mount (0.8.3) 
remote: Installing rack-test (0.6.1) 
remote: Installing tzinfo (0.3.31) 
remote: Installing actionpack (3.0.10) 
remote: Installing mime-types (1.17.2) 
remote: Installing polyglot (0.3.3) 
remote: Installing treetop (1.4.10) 
remote: Installing mail (2.3.0) 
remote: Installing actionmailer (3.0.10) 
remote: Installing arel (2.0.10) 
remote: Installing activerecord (3.0.10) 
remote: Installing activeresource (3.0.10) 
remote: Installing json (1.6.5) with native extensions 
remote: Using bundler (1.0.10)                                                                                                                       [24/286]
remote: Installing rdoc (3.12) 
remote: Installing thor (0.14.6) 
remote: Installing railties (3.0.10) 
remote: Installing rails (3.0.10) 
remote: Installing sqlite3 (1.3.5) with native extensions 
remote: Your bundle is complete! It was installed into ./vendor/bundle
remote: Precompiling with 'bundle exec rake assets:precompile'
remote: Running .openshift/action_hooks/build
remote: Running .openshift/action_hooks/deploy
remote: Starting application...
remote: Done
remote: Running .openshift/action_hooks/post_deploy
To ssh://e5f597d537d14ecfa037a8af4a9cc500@ruby18demo-gomix.rhcloud.com/~/git/ruby18demo.git/
   1852ea4..2e2c88e  master -> master

Y con ello ya tenemos nuestro Rails funcionando.

Hola Mundo en nuestra app Rails

Hagamos nuestras primeras modificaciones a nuestro proyecto Rails.

$ rails generate controller home index
      create  app/controllers/home_controller.rb
       route  get "home/index" 
      invoke  erb
      create    app/views/home
      create    app/views/home/index.html.erb
      invoke  test_unit
      create    test/functional/home_controller_test.rb
      invoke  helper
      create    app/helpers/home_helper.rb
      invoke    test_unit
      create      test/unit/helpers/home_helper_test.rb

$ git rm public/index.html
rm 'public/index.html'

Agregue la siguiente ruta en config/routes.rb

1root :to => "home#index" 

Agregue sus cambios al repositorio.

$ git add .
$ git commit -a -m "Mis primeros cambios" 
[master f912583] Mis primeros cambios
 7 files changed, 25 insertions(+), 239 deletions(-)
 create mode 100644 app/controllers/home_controller.rb
 create mode 100644 app/helpers/home_helper.rb
 create mode 100644 app/views/home/index.html.erb
 delete mode 100644 public/index.html
 create mode 100644 test/functional/home_controller_test.rb
 create mode 100644 test/unit/helpers/home_helper_test.rb
$ git push
]$ git push
Counting objects: 28, done.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (19/19), 1.59 KiB, done.
Total 19 (delta 4), reused 0 (delta 0)
remote: Stopping application...
remote: Waiting for stop to finish
remote: Waiting for stop to finish
remote: Waiting for stop to finish
remote: Done
remote: Saving away previously bundled RubyGems
remote: Running .openshift/action_hooks/pre_build
remote: Restoring previously bundled RubyGems (note: you can commit .openshift/markers/force_clean_build at the root of your repo to force a clean bundle)
remote: Bundling RubyGems based on Gemfile/Gemfile.lock to repo/vendor/bundle with 'bundle install --deployment'
remote: Using rake (0.9.2.2) 
remote: Using abstract (1.0.0) 
remote: Using activesupport (3.0.10) 
remote: Using builder (2.1.2) 
remote: Using i18n (0.5.0) 
remote: Using activemodel (3.0.10) 
remote: Using erubis (2.6.6) 
remote: Using rack (1.4.1) 
remote: Using rack-mount (0.8.3) 
remote: Using rack-test (0.6.1) 
remote: Using tzinfo (0.3.31) 
remote: Using actionpack (3.0.10)                                                                                                                     [0/361]
remote: Using mime-types (1.17.2) 
remote: Using polyglot (0.3.3) 
remote: Using treetop (1.4.10) 
remote: Using mail (2.3.0) 
remote: Using actionmailer (3.0.10) 
remote: Using arel (2.0.10) 
remote: Using activerecord (3.0.10) 
remote: Using activeresource (3.0.10) 
remote: Using json (1.6.5) 
remote: Using bundler (1.0.10) 
remote: Using rdoc (3.12) 
remote: Using thor (0.14.6) 
remote: Using railties (3.0.10) 
remote: Using rails (3.0.10) 
remote: Using sqlite3 (1.3.5) 
remote: Your bundle is complete! It was installed into ./vendor/bundle
remote: Precompiling with 'bundle exec rake assets:precompile'
remote: Running .openshift/action_hooks/build
remote: Running .openshift/action_hooks/deploy
remote: Starting application...
remote: Done
remote: Running .openshift/action_hooks/post_deploy
To ssh://e5f597d537d14ecfa037a8af4a9cc500@ruby18demo-gomix.rhcloud.com/~/git/ruby18demo.git/
   2e2c88e..f912583  master -> master

Ahora debería ver la salida de home#index al entrar a la raíz de su aplicación, para que diga "Hola Mundo" ajuste la vista correspondiente.

Soporte de base de datos en Rails

Para agregar el soporte de base de datos necesario, modifique para agregar el siguiente contenido al archivo .openshift/action_hooks/deploy:

pushd ${OPENSHIFT_REPO_DIR} > /dev/null
bundle exec rake db:migrate RAILS_ENV="production" 
popd > /dev/null

Existen dos opciones, SQLite y MySQL.

config/database.yml para caso SQLite.

1...
2production:
3  adapter: sqlite3
4  database: <%=ENV['OPENSHIFT_DATA_DIR']%>production.sqlite3
5  pool: 5
6  timeout: 5000

Para el caso MySQL necesitará agregar el "cartucho" mysql a su app utilizando rhc-ctl-app primero:

$ rhc-ctl-app -a APP -e add-mysql-5.1

config/database.yml para caso MySQL.

 1production:
 2  adapter: mysql
 3  encoding: utf8
 4  database: <%=ENV['OPENSHIFT_APP_NAME']%>
 5  pool: 5
 6  host: <%=ENV['OPENSHIFT_DB_HOST']%>
 7  port: <%=ENV['OPENSHIFT_DB_PORT']%>
 8  username: <%=ENV['OPENSHIFT_DB_USERNAME']%>
 9  password: <%=ENV['OPENSHIFT_DB_PASSWORD']%>
10  socket: <%=ENV['OPENSHIFT_DB_SOCKET']%>

Luego deberá agregar mysql a su lista de dependencias en el archivo Gemfile, agregue:

gem 'mysql'

Luego ejecute bundle install para actualizar su archivo Gemfile.lock y vuelva hacer un despliegue empujando los cambios al repo git en Openshift.

modelo para probar

$ rails generate scaffold Post name:string title:string content:text
...
$ git add ...
$ git commit ...
$ git push ...
...
remote: Running .openshift/action_hooks/build
remote: Running .openshift/action_hooks/deploy
remote: ==  CreatePosts: migrating ====================================================
remote: -- create_table(:posts)
remote:    -> 0.0025s
remote: ==  CreatePosts: migrated (0.0026s) ===========================================
remote: 
remote: Starting application...
remote: Done
remote: Running .openshift/action_hooks/post_deploy
Auto packing the repository for optimum performance.
To ssh://42edf339eaa7454c8ecfce338e7a0882@uno-gomix.rhcloud.com/~/git/uno.git/
   8d76a39..eb68b86  master -> master

¡Por favor note con cuidado que se ha corrido la migración con éxito! Abajo un pantallazo de la creación y listado de dicho modelo en nuestra aplicación.

rack-app-openshift-one.png (45.2 kB) Guillermo Gómez, 12/28/2011 10:47 am

openshift-probando-bd-sqlite.png (22 kB) Guillermo Gómez, 12/28/2011 02:03 pm

openshift-probando-bd-sqlite-2.png (23 kB) Guillermo Gómez, 12/28/2011 02:03 pm