From fef94eb3765464c0b16dcb6fc34fc0d7cc212ab6 Mon Sep 17 00:00:00 2001 From: Maria Nita Date: Mon, 27 Oct 2014 12:12:32 +0100 Subject: [PATCH] Create and show database objects in when running the app --- .gitignore | 1 + Dockerfile | 10 +++--- app.rb | 31 ------------------- sinatra_app/Rakefile | 2 ++ sinatra_app/app.rb | 15 +++++++++ sinatra_app/config/database.yml | 7 +++++ sinatra_app/config/environments.rb | 27 ++++++++++++++++ .../db/migrate/1414364400_create_timestamp.rb | 12 +++++++ sinatra_app/models.rb | 4 +++ sinatra_app/start.rb | 6 ++++ 10 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 .gitignore delete mode 100644 app.rb create mode 100644 sinatra_app/Rakefile create mode 100644 sinatra_app/app.rb create mode 100644 sinatra_app/config/database.yml create mode 100644 sinatra_app/config/environments.rb create mode 100644 sinatra_app/db/migrate/1414364400_create_timestamp.rb create mode 100644 sinatra_app/models.rb create mode 100644 sinatra_app/start.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..f825659db8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*schema.rb diff --git a/Dockerfile b/Dockerfile index 84ea4a9812..0ff08ba5ba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,9 @@ FROM openshift/ruby-20-centos -# Install mysql client and gems to connect app to database. -USER root -RUN yum install -y mysql && gem install sinatra activerecord mysql2 --no-ri --no-rdoc +RUN gem install sinatra sinatra-activerecord mysql2 --no-ri --no-rdoc -USER ruby -ADD app.rb /tmp/app.rb +ADD sinatra_app /tmp/ +WORKDIR /tmp/ EXPOSE 8080 -CMD ["ruby", "/tmp/app.rb"] +CMD ["ruby", "start.rb"] diff --git a/app.rb b/app.rb deleted file mode 100644 index a6f85163d5..0000000000 --- a/app.rb +++ /dev/null @@ -1,31 +0,0 @@ -require 'active_record' -require 'sinatra' - -set :bind, '0.0.0.0' -set :port, 8080 - -if not ("#{ENV["MYSQL_DATABASE"]}".blank? || "#{ENV["MYSQL_ROOT_PASSWORD"]}".blank? || "#{ENV["DATABASE_SERVICE_HOST"]}".blank?) - - while %x"mysqladmin ping -h #{ENV["DATABASE_SERVICE_HOST"]} --port=#{ENV["DATABASE_SERVICE_PORT"]} -uroot -p#{ENV["MYSQL_ROOT_PASSWORD"]}".strip != "mysqld is alive" - puts "Waiting for database connection to #{ENV["DATABASE_SERVICE_HOST"]}:#{ENV["DATABASE_SERVICE_PORT"]} as ...#{ENV["MYSQL_ROOT_PASSWORD"]}\n" - sleep 1 - end - - configure do - ActiveRecord::Base.establish_connection( - :adapter => "mysql2", - :host => "#{ENV["DATABASE_SERVICE_HOST"]}", - :port => "#{ENV["DATABASE_SERVICE_PORT"]}", - :database => "#{ENV["MYSQL_DATABASE"]}", - :password => "#{ENV["MYSQL_ROOT_PASSWORD"]}" - ) - end - puts "Database connection is OK." -end - -get '/' do - "Hello World!\n"+ - # ENV values are generated during template processing - # and then passed to the container when openshift launches it. - "All the environment variables are: #{ENV.map { |k,v| "#{k}=#{v}" }.join("\n")}]\n" -end diff --git a/sinatra_app/Rakefile b/sinatra_app/Rakefile new file mode 100644 index 0000000000..1d016631ac --- /dev/null +++ b/sinatra_app/Rakefile @@ -0,0 +1,2 @@ +require_relative 'app' +require 'sinatra/activerecord/rake' diff --git a/sinatra_app/app.rb b/sinatra_app/app.rb new file mode 100644 index 0000000000..15e7967f06 --- /dev/null +++ b/sinatra_app/app.rb @@ -0,0 +1,15 @@ +require 'sinatra' +require_relative 'config/environments' +require_relative 'models' + +set :bind, '0.0.0.0' +set :port, ENV["FRONTEND_SERVICE_PORT"] + +get '/' do + Timestamp.create(date: Time.now, text: "This is a message from a database query. The last insertion in the database was at") + "Hello World!\n"+ + # ENV values are generated during template processing + # and then passed to the container when openshift launches it. + "All the environment variables are: #{ENV.map { |k,v| "#{k}=#{v}" }.join("\n")}]\n" + + "#{Timestamp.last().text} #{Timestamp.last().date}." +end diff --git a/sinatra_app/config/database.yml b/sinatra_app/config/database.yml new file mode 100644 index 0000000000..2dbb81ccd5 --- /dev/null +++ b/sinatra_app/config/database.yml @@ -0,0 +1,7 @@ +development: + adapter: mysql2 + database: <%= ENV["MYSQL_DATABASE"] %> + username: root + password: <%= ENV['MYSQL_ROOT_PASSWORD'] %> + host: <%= ENV["DATABASE_SERVICE_IP_ADDR"] %> + port: <%= ENV["DATABASE_SERVICE_PORT"] %> diff --git a/sinatra_app/config/environments.rb b/sinatra_app/config/environments.rb new file mode 100644 index 0000000000..575a6f2d4c --- /dev/null +++ b/sinatra_app/config/environments.rb @@ -0,0 +1,27 @@ +require 'sinatra/activerecord' + +def self.connect_to_database + begin + ActiveRecord::Base.establish_connection( + :adapter => "mysql2", + :host => "#{ENV["DATABASE_SERVICE_IP_ADDR"]}", + :port => "#{ENV["DATABASE_SERVICE_PORT"]}", + :database => "#{ENV["MYSQL_DATABASE"]}", + :password => "#{ENV["MYSQL_ROOT_PASSWORD"]}" + ) + + ActiveRecord::Base.connection.active? + + rescue Exception + return false + end +end + +configure :development do + + while !self.connect_to_database + puts "Connecting to database...\n" + sleep 0.1 + end + puts "Connected to database" +end diff --git a/sinatra_app/db/migrate/1414364400_create_timestamp.rb b/sinatra_app/db/migrate/1414364400_create_timestamp.rb new file mode 100644 index 0000000000..0651c6baff --- /dev/null +++ b/sinatra_app/db/migrate/1414364400_create_timestamp.rb @@ -0,0 +1,12 @@ +class CreateTimestamp < ActiveRecord::Migration + def up + create_table :timestamps do |t| + t.date :date + t.text :text + end + end + + def down + drop_table :timestamps + end +end diff --git a/sinatra_app/models.rb b/sinatra_app/models.rb new file mode 100644 index 0000000000..071be3990f --- /dev/null +++ b/sinatra_app/models.rb @@ -0,0 +1,4 @@ +require 'active_record' + +class Timestamp < ActiveRecord::Base +end diff --git a/sinatra_app/start.rb b/sinatra_app/start.rb new file mode 100644 index 0000000000..8f54d0355e --- /dev/null +++ b/sinatra_app/start.rb @@ -0,0 +1,6 @@ +puts "Create database..." +%x"rake db:create" +puts "Run migrations..." +%x"rake db:migrate" +puts "Run app..." +%x"ruby app.rb &>/dev/null &"