Class: Conjure::Service::RailsServer
- Inherits:
-
Object
- Object
- Conjure::Service::RailsServer
- Defined in:
- lib/conjure/service/rails_server.rb
Instance Method Summary collapse
- #apt_packages_required_for_gems ⇒ Object
- #base_image ⇒ Object
- #console ⇒ Object
- #database_exists ⇒ Object
-
#initialize(host, app_name, rails_environment) ⇒ RailsServer
constructor
A new instance of RailsServer.
- #initialize_database ⇒ Object
- #install_gems ⇒ Object
- #log(options = {}) ⇒ Object
- #migrate_database ⇒ Object
- #rake(command) ⇒ Object
- #restart_server ⇒ Object
- #ruby_version ⇒ Object
- #run ⇒ Object
- #server_image ⇒ Object
- #update_database ⇒ Object
Constructor Details
#initialize(host, app_name, rails_environment) ⇒ RailsServer
Returns a new instance of RailsServer.
4 5 6 7 8 |
# File 'lib/conjure/service/rails_server.rb', line 4 def initialize(host, app_name, rails_environment) @host = host @app_name = app_name @rails_environment = rails_environment end |
Instance Method Details
#apt_packages_required_for_gems ⇒ Object
111 112 113 |
# File 'lib/conjure/service/rails_server.rb', line 111 def apt_packages_required_for_gems ["libpq-dev", "libmysqlclient-dev"] end |
#base_image ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/conjure/service/rails_server.rb', line 10 def base_image @base_image ||= @host.images.create( label: "rails_base", base_image: "ubuntu", setup_commands: [ "apt-get install -y curl git", "curl -L https://get.rvm.io | bash -s stable", "rvm install #{ruby_version}", "bash -c 'source /usr/local/rvm/scripts/rvm; rvm use #{ruby_version}@global --default'", "apt-get install -y #{apt_packages_required_for_gems.join ' '}", "echo 'deb http://us.archive.ubuntu.com/ubuntu/ precise universe' >>/etc/apt/sources.list", "apt-get install -y python-software-properties software-properties-common", "add-apt-repository -y ppa:chris-lea/node.js-legacy", "apt-get update", "apt-get install -y nodejs", ], environment: { PATH:"/usr/local/rvm/gems/ruby-1.9.3-p448@global/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", RAILS_ENV: @rails_environment, GITHUB_TOKEN: ENV["GITHUB_TOKEN"], FRECKLE_SUBDOMAIN: "neomind", }, host_volumes: {"/rails_app" => "/#{@app_name}"}, ) end |
#console ⇒ Object
101 102 103 104 105 |
# File 'lib/conjure/service/rails_server.rb', line 101 def console base_image.command "cd #{@app_name}; bundle exec rails console", :stream_stdin => true do |stdout, stderr| print stdout end end |
#database_exists ⇒ Object
64 65 66 67 |
# File 'lib/conjure/service/rails_server.rb', line 64 def database_exists Conjure.log "[ rails] Checking the database status" base_image.command("cd #{@app_name}; bundle exec rake db:version; true").include? "Current version:" end |
#initialize_database ⇒ Object
74 75 76 77 |
# File 'lib/conjure/service/rails_server.rb', line 74 def initialize_database Conjure.log "[ rails] Setting up the database" base_image.command "cd #{@app_name}; bundle exec rake db:setup" end |
#install_gems ⇒ Object
55 56 57 58 |
# File 'lib/conjure/service/rails_server.rb', line 55 def install_gems Conjure.log "[ rails] Installing gems" base_image.command "cd #{@app_name}; bundle --deployment" end |
#log(options = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/conjure/service/rails_server.rb', line 84 def log( = {}) arguments = [] arguments << "-n #{[:lines]}" if [:lines] arguments << "-f" if [:tail] log_file = "#{@app_name}/log/#{@rails_environment}.log" base_image.command "tail #{arguments.join ' '} #{log_file}" do |stdout, stderr| puts stdout end rescue Interrupt => e end |
#migrate_database ⇒ Object
69 70 71 72 |
# File 'lib/conjure/service/rails_server.rb', line 69 def migrate_database Conjure.log "[ rails] Migrating the database" base_image.command "cd #{@app_name}; bundle exec rake db:migrate" end |
#rake(command) ⇒ Object
95 96 97 98 99 |
# File 'lib/conjure/service/rails_server.rb', line 95 def rake(command) base_image.command "cd #{@app_name}; bundle exec rake #{command}" do |stdout, stderr| print stdout end end |
#restart_server ⇒ Object
79 80 81 82 |
# File 'lib/conjure/service/rails_server.rb', line 79 def restart_server server_image.stop server_image.run "cd #{@app_name}; rm -f tmp/pids/server.pid; bundle exec rails server -p 80" end |
#ruby_version ⇒ Object
107 108 109 |
# File 'lib/conjure/service/rails_server.rb', line 107 def ruby_version Conjure.config.file_contents("../.ruby-version").strip end |
#run ⇒ Object
49 50 51 52 53 |
# File 'lib/conjure/service/rails_server.rb', line 49 def run install_gems update_database restart_server end |
#server_image ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/conjure/service/rails_server.rb', line 37 def server_image @server_image ||= @host.images.create( label: "rails_server", base_image: base_image, ports: [80], environment: { PATH:"/usr/local/rvm/gems/ruby-1.9.3-p448@global/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", }, host_volumes: {"/rails_app" => "/#{@app_name}"}, ) end |
#update_database ⇒ Object
60 61 62 |
# File 'lib/conjure/service/rails_server.rb', line 60 def update_database database_exists ? migrate_database : initialize_database end |