Module Moonshine::Manifest::Rails::Rails

  1. lib/moonshine/manifest/rails/rails.rb (view online)

Public instance methods

rails_bootstrap ()

Attempt to bootstrap your application. Calls rake moonshine:bootstrap which runs:

rake db:schema:load (if db/schema.rb exists)
rake db:migrate (if db/migrate exists)

We then run a task to load bootstrap fixtures from db/bootstrap (if it exists). These fixtures may be created with the included moonshine:db:bootstrap:dump rake task.

rake moonshine:db:bootstrap

We then run the following task:

rake moonshine:app:bootstrap

The moonshine:app:bootstrap task does nothing by default. If you’d like to have your application preform any logic on it’s first deploy, overwrite this task in your Rakefile:

namespace :moonshine do
  namespace :app do
    desc "Overwrite this task in your app if you have any bootstrap tasks that need to be run"
    task :bootstrap do
      #
    end
  end
end

All of this assumes one things. That your application can run ‘rake environment’ with an empty database. Please ensure your application can do so!

[show source]
    # File lib/moonshine/manifest/rails/rails.rb, line 35
35:   def rails_bootstrap
36:     rake 'moonshine:bootstrap',
37:       :alias => 'rails_bootstrap',
38:       :refreshonly => true,
39:       :before => exec('rake db:migrate')
40:   end
rails_directories ()

Essentially replicates the deploy:setup command from capistrano, but sets up permissions correctly.

[show source]
     # File lib/moonshine/manifest/rails/rails.rb, line 115
115:   def rails_directories
116:     deploy_to_array = configuration[:deploy_to].split('/').split('/')
117:     deploy_to_array.each_with_index do |dir, index|
118:       next if index == 0 || index >= (deploy_to_array.size-1)
119:       file '/'+deploy_to_array[1..index].join('/'), :ensure => :directory
120:     end
121:     dirs = [
122:       "#{configuration[:deploy_to]}",
123:       "#{configuration[:deploy_to]}/shared",
124:       "#{configuration[:deploy_to]}/releases"
125:     ]
126:     if configuration[:shared_children].is_a?(Array)
127:       shared_dirs = configuration[:shared_children].map { |d| "#{configuration[:deploy_to]}/shared/#{d}" }
128:       dirs += shared_dirs
129:     end
130:     if configuration[:app_symlinks].is_a?(Array)
131:       dirs += ["#{configuration[:deploy_to]}/shared/public"]
132:       symlink_dirs = configuration[:app_symlinks].map { |d| "#{configuration[:deploy_to]}/shared/public/#{d}" }
133:       dirs += symlink_dirs
134:     end
135:     dirs.each do |dir|
136:       file dir,
137:       :ensure => :directory,
138:       :owner => configuration[:user],
139:       :group => configuration[:group] || configuration[:user],
140:       :mode => '775'
141:     end
142:   end
rails_gems ()

Automatically install all gems needed specified in the array at configuration[:gems]. This loads gems from config/gems.yml, which can be generated from by running rake moonshine:gems locally.

[show source]
     # File lib/moonshine/manifest/rails/rails.rb, line 84
 84:   def rails_gems
 85:     gemrc = {
 86:       :verbose => true,
 87:       :gem => "--no-ri --no-rdoc",
 88:       :update_sources => true,
 89:       :sources => [
 90:         'http://gems.rubyforge.org',
 91:         'http://gems.github.com',
 92:       ]
 93:     }
 94:     gemrc.merge!(configuration[:rubygems]) if configuration[:rubygems]
 95:     file '/etc/gemrc',
 96:       :ensure   => :present,
 97:       :mode     => '744',
 98:       :owner    => 'root',
 99:       :group    => 'root',
100:       :content  => gemrc.to_yaml
101:     #stub for dependencies
102:     exec 'rails_gems', :command => 'true'
103:     return unless configuration[:gems]
104:     configuration[:gems].each do |gem|
105:       gem.delete(:source) if gem[:source] && gem[:source] =~ /gems.github.com/
106:       gem(gem[:name], {
107:         :version => gem[:version],
108:         :source => gem[:source]
109:       })
110:     end
111:   end
rails_logrotate ()

Rotates the logs for this rails app

[show source]
    # File lib/moonshine/manifest/rails/rails.rb, line 49
49:   def rails_logrotate
50:     configure(:rails_logrotate => {})
51:     logrotate("#{configuration[:deploy_to]}/shared/log/*.log", {
52:       :options => configuration[:rails_logrotate][:options] || %w(daily missingok compress delaycompress sharedscripts),
53:       :postrotate => configuration[:rails_logrotate][:postrotate] || "touch #{configuration[:deploy_to]}/current/tmp/restart.txt"
54:     })
55:     file "/etc/logrotate.d/#{configuration[:deploy_to].gsub('/','')}sharedlog.conf", :ensure => :absent
56:   end
rails_migrations ()

Runs Rails migrations. These are run on each deploy to ensure consistency! No more 500s when you forget to cap deploy:migrations

[show source]
    # File lib/moonshine/manifest/rails/rails.rb, line 44
44:   def rails_migrations
45:     rake 'db:migrate'
46:   end
rails_rake_environment ()

This task ensures Rake is installed and that rake environment executes without error in your rails_root.

[show source]
    # File lib/moonshine/manifest/rails/rails.rb, line 60
60:   def rails_rake_environment
61:     package 'rake', :provider => :gem, :ensure => :installed
62:     file '/var/log/moonshine_rake.log',
63:       :ensure   => :present,
64:       :owner    => configuration[:user],
65:       :group    => configuration[:group] || configuration[:user],
66:       :mode     => '775',
67:       :content  => ' '
68:     exec 'rake tasks',
69:       :command => 'rake environment >> /var/log/moonshine_rake.log 2>&1',
70:       :user => configuration[:user],
71:       :cwd => rails_root,
72:       :environment => "RAILS_ENV=#{ENV['RAILS_ENV']}",
73:       :require => [
74:         exec('rails_gems'),
75:         package('rake'),
76:         file('/var/log/moonshine_rake.log')
77:       ]
78:   end