├── .gitignore ├── .s2i ├── bin │ └── README └── environment ├── .travis.yml ├── Dockerfile ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── app.rb ├── config.ru ├── config ├── database.rb └── database.yml ├── db └── migrate │ └── 20141102191902_create_key_pair.rb ├── models.rb ├── run.sh ├── test └── sample_test.rb └── views └── main.erb /.gitignore: -------------------------------------------------------------------------------- 1 | *schema.rb 2 | -------------------------------------------------------------------------------- /.s2i/bin/README: -------------------------------------------------------------------------------- 1 | This folder can contain scripts that can be used during various points in the 2 | S2I process. For more information on the types of scripts that can be placed 3 | here, and their required details, please see: 4 | https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md#s2i-scripts 5 | -------------------------------------------------------------------------------- /.s2i/environment: -------------------------------------------------------------------------------- 1 | # The default environment for this Ruby application is production 2 | RACK_ENV=production 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | sudo: false 3 | rvm: 4 | - 2.7 5 | before_install: 6 | - cp -p Gemfile.lock Gemfile.lock.org 7 | script: 8 | # Gemfile.lock should not include BUNDLED WITH part 9 | # to prevent old version Bundler to update the file. 10 | # https://github.com/openshift/ruby-hello-world/pull/64#issuecomment-363731682 11 | - | 12 | if grep '^BUNDLED WITH' Gemfile.lock; then 13 | false 14 | else 15 | true 16 | fi 17 | # Check Gemfile.lock is not edited because that causes an error 18 | # with openshift cluster. 19 | - diff Gemfile.lock.org Gemfile.lock 20 | - bundle exec rake test 21 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM registry.access.redhat.com/ubi8/ruby-27 2 | USER default 3 | EXPOSE 8080 4 | ENV RACK_ENV production 5 | ENV RAILS_ENV production 6 | COPY . /opt/app-root/src/ 7 | ENV GEM_HOME ~/.gem 8 | ENV BUNDLE_FROZEN true 9 | RUN bundle install 10 | CMD ["./run.sh"] 11 | 12 | USER root 13 | RUN chmod og+rw /opt/app-root/src/db 14 | USER default 15 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'sinatra', '2.1.0' 4 | gem 'sinatra-activerecord', '2.0.22' 5 | gem 'mysql2', '0.5.5' 6 | gem 'rake', '13.0.3' 7 | gem 'minitest', '5.14.4' 8 | gem 'i18n', '1.8.9' 9 | gem 'webrick', '1.7.0' 10 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | activemodel (6.1.7.6) 5 | activesupport (= 6.1.7.6) 6 | activerecord (6.1.7.6) 7 | activemodel (= 6.1.7.6) 8 | activesupport (= 6.1.7.6) 9 | activesupport (6.1.7.6) 10 | concurrent-ruby (~> 1.0, >= 1.0.2) 11 | i18n (>= 1.6, < 2) 12 | minitest (>= 5.1) 13 | tzinfo (~> 2.0) 14 | zeitwerk (~> 2.3) 15 | concurrent-ruby (1.2.3) 16 | i18n (1.8.9) 17 | concurrent-ruby (~> 1.0) 18 | minitest (5.14.4) 19 | mustermann (1.1.2) 20 | ruby2_keywords (~> 0.0.1) 21 | mysql2 (0.5.5) 22 | rack (2.2.8) 23 | rack-protection (2.1.0) 24 | rack 25 | rake (13.0.3) 26 | ruby2_keywords (0.0.5) 27 | sinatra (2.1.0) 28 | mustermann (~> 1.0) 29 | rack (~> 2.2) 30 | rack-protection (= 2.1.0) 31 | tilt (~> 2.0) 32 | sinatra-activerecord (2.0.22) 33 | activerecord (>= 4.1) 34 | sinatra (>= 1.0) 35 | tilt (2.3.0) 36 | tzinfo (2.0.6) 37 | concurrent-ruby (~> 1.0) 38 | webrick (1.7.0) 39 | zeitwerk (2.6.13) 40 | 41 | PLATFORMS 42 | ruby 43 | 44 | DEPENDENCIES 45 | i18n (= 1.8.9) 46 | minitest (= 5.14.4) 47 | mysql2 (= 0.5.5) 48 | rake (= 13.0.3) 49 | sinatra (= 2.1.0) 50 | sinatra-activerecord (= 2.0.22) 51 | webrick (= 1.7.0) 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is a sample openshift v3 application repository. 2 | 3 | For instructions on how to use it, please see: https://github.com/openshift/origin/blob/master/examples/sample-app/README.md 4 | 5 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler' 2 | 3 | require_relative 'config/database' 4 | require_relative 'models' 5 | 6 | require 'sinatra/activerecord/rake' 7 | require 'rake/testtask' 8 | 9 | Rake::TestTask.new do |t| 10 | t.pattern = "test/*_test.rb" 11 | t.verbose = true 12 | end 13 | -------------------------------------------------------------------------------- /app.rb: -------------------------------------------------------------------------------- 1 | require 'sinatra' 2 | require_relative 'config/database' 3 | require_relative 'models' 4 | 5 | set :bind, '0.0.0.0' 6 | set :port, 8080 7 | 8 | set :app_file, __FILE__ 9 | 10 | def configure_database 11 | if ENV['RACK_ENV']=="production" 12 | while !self.connect_to_database_prod 13 | sleep 0.1 14 | end 15 | else 16 | while !self.connect_to_database_test 17 | sleep 0.1 18 | end 19 | end 20 | puts "Connected to database" 21 | puts "Create database..." 22 | %x"rake db:create" 23 | puts "Run migrations..." 24 | %x"rake db:migrate" 25 | end 26 | 27 | configure do 28 | puts "Run app..." 29 | 30 | unless ENV["DATABASE_SERVICE_HOST"].nil? && ENV["DATABASE_TEST_SERVICE_HOST"].nil? 31 | configure_database 32 | end 33 | end 34 | 35 | get '/' do 36 | puts "Servicing index request..." 37 | erb :main 38 | end 39 | 40 | get '/keys' do 41 | puts "Retrieving all keys" 42 | a={} 43 | KeyPair.all.each do |v| 44 | a[v.key]=v.value 45 | end 46 | a.to_s 47 | end 48 | 49 | get '/keys/:id' do 50 | puts "Retrieving key #{params[:id]}" 51 | if KeyPair.exists?(params[:id]) 52 | KeyPair.find(params[:id]).value 53 | else 54 | not_found "Key not found" 55 | end 56 | end 57 | 58 | post '/keys/:id' do 59 | puts "Updating key #{params[:id]} to #{params['value']}" 60 | if KeyPair.exists?(params[:id]) 61 | KeyPair.update(params[:id], value: params['value']) 62 | "Key updated" 63 | else 64 | KeyPair.create(key:params[:id],value:params['value']).save 65 | "Key created" 66 | end 67 | end 68 | 69 | delete '/keys/:id' do 70 | puts "Deleting key #{params[:id]}" 71 | if KeyPair.exists?(params[:id]) 72 | v=KeyPair.find(params[:id]) 73 | v.destroy 74 | "Key deleted" 75 | else 76 | "Key not found" 77 | end 78 | end 79 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | #\ -p 8080 2 | require './app' 3 | $stdout.sync = true 4 | $stderr.sync = true 5 | run Sinatra::Application 6 | -------------------------------------------------------------------------------- /config/database.rb: -------------------------------------------------------------------------------- 1 | require 'sinatra/activerecord' 2 | 3 | def self.connect_to_database_prod 4 | begin 5 | config = { 6 | :adapter => "mysql2", 7 | :host => "#{ENV["DATABASE_SERVICE_HOST"]}", 8 | :port => "#{ENV["DATABASE_SERVICE_PORT"]}", 9 | :database => "#{ENV["MYSQL_DATABASE"]}" 10 | } 11 | if ENV.key?("MYSQL_ROOT_PASSWORD") 12 | config[:password] = "#{ENV["MYSQL_ROOT_PASSWORD"]}" 13 | else 14 | config[:username] = "#{ENV["MYSQL_USER"]}" 15 | config[:password] = "#{ENV["MYSQL_PASSWORD"]}" 16 | end 17 | 18 | puts "Connecting to production database (#{config[:username]}@#{config[:host]}:#{config[:port]})..." 19 | ActiveRecord::Base.establish_connection(config) 20 | 21 | ActiveRecord::Base.connection.active? 22 | 23 | rescue Exception => e 24 | if not /Can't connect to MySQL server/ =~ e.message 25 | puts e.message 26 | end 27 | return false 28 | end 29 | end 30 | 31 | def self.connect_to_database_test 32 | begin 33 | config = { 34 | :adapter => "mysql2", 35 | :host => "#{ENV["DATABASE_TEST_SERVICE_HOST"]}", 36 | :port => "#{ENV["DATABASE_TEST_SERVICE_PORT"]}", 37 | :database => "#{ENV["MYSQL_DATABASE"]}" 38 | } 39 | if ENV.key?("MYSQL_ROOT_PASSWORD") 40 | config[:password] = ENV["MYSQL_ROOT_PASSWORD"] 41 | else 42 | config[:username] = ENV["MYSQL_USER"] 43 | config[:password] = ENV["MYSQL_PASSWORD"] 44 | end 45 | 46 | puts "Connecting to test database (#{config[:username]}@#{config[:host]}:#{config[:port]})..." 47 | ActiveRecord::Base.establish_connection(config) 48 | 49 | ActiveRecord::Base.connection.active? 50 | 51 | rescue Exception => e 52 | if not /Can't connect to MySQL server/ =~ e.message 53 | puts e.message 54 | end 55 | return false 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /config/database.yml: -------------------------------------------------------------------------------- 1 | <%# Handle old behaviour of official mysql image %> 2 | <% user = ENV.key?("MYSQL_ROOT_PASSWORD") ? "root" : ENV["MYSQL_USER"] %> 3 | <% password = ENV.key?("MYSQL_ROOT_PASSWORD") ? ENV["MYSQL_ROOT_PASSWORD"] : ENV["MYSQL_PASSWORD"] %> 4 | development: 5 | adapter: mysql2 6 | database: <%= ENV["MYSQL_DATABASE"] %> 7 | username: <%= user %> 8 | password: <%= password %> 9 | host: <%= ENV["DATABASE_SERVICE_HOST"] %> 10 | port: <%= ENV["DATABASE_SERVICE_PORT"] %> 11 | 12 | test: 13 | adapter: mysql2 14 | database: <%= ENV["MYSQL_DATABASE"] %> 15 | username: <%= user %> 16 | password: <%= password %> 17 | host: <%= ENV["DATABASE_TEST_SERVICE_HOST"] %> 18 | port: <%= ENV["DATABASE_TEST_SERVICE_PORT"] %> 19 | 20 | production: 21 | adapter: mysql2 22 | database: <%= ENV["MYSQL_DATABASE"] %> 23 | username: <%= user %> 24 | password: <%= password %> 25 | host: <%= ENV["DATABASE_SERVICE_HOST"] %> 26 | port: <%= ENV["DATABASE_SERVICE_PORT"] %> 27 | -------------------------------------------------------------------------------- /db/migrate/20141102191902_create_key_pair.rb: -------------------------------------------------------------------------------- 1 | class CreateKeyPair < ActiveRecord::Migration[5.1] 2 | def up 3 | create_table :key_pairs, :primary_key => :key do |t| 4 | t.string :value 5 | end 6 | change_column :key_pairs, :key, :string 7 | end 8 | 9 | def down 10 | drop_table :key_pairs 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /models.rb: -------------------------------------------------------------------------------- 1 | require 'active_record' 2 | 3 | class KeyPair < ActiveRecord::Base 4 | self.primary_key = 'key' 5 | end 6 | -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | bundle exec ruby app.rb 4 | -------------------------------------------------------------------------------- /test/sample_test.rb: -------------------------------------------------------------------------------- 1 | require 'bundler' 2 | require 'minitest/autorun' 3 | 4 | class TestSample < Minitest::Test 5 | 6 | def test_sample 7 | assert_equal "Foo", "Foo" 8 | end 9 | 10 | end 11 | -------------------------------------------------------------------------------- /views/main.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | Hello from OpenShift v3! 15 | 16 | 17 | 20 | 21 | <% if ENV["DATABASE_SERVICE_HOST"].nil? && ENV["DATABASE_TEST_SERVICE_HOST"].nil? %> 22 |
23 |

(It looks like the database isn't running. This isn't going to be much fun.)

24 |
25 | <% else %> 26 |
27 |

Get, edit or delete key-value pairs, for fun.

28 |
29 |
30 | 31 |
32 | 33 | 34 |
35 | 36 |
37 | 38 | 39 |
40 | 41 |
42 | 43 | 48 |
49 |
50 |
51 | <% end %> 52 | 53 | 54 | 91 | 92 | --------------------------------------------------------------------------------