Class: Shuttle::Rails

Inherits:
Strategy show all
Includes:
Support::Bundler, Support::Thin
Defined in:
lib/shuttle/deployment/rails.rb

Constant Summary

Constants included from Helpers

Helpers::LEVEL_COLORS

Instance Attribute Summary

Attributes inherited from Deploy

#config, #environment, #ssh, #target, #version

Instance Method Summary collapse

Methods included from Support::Thin

#thin_config, #thin_env, #thin_host, #thin_options, #thin_port, #thin_restart, #thin_servers, #thin_start, #thin_stop

Methods included from Support::Bundler

#bundle_install, #bundle_path, #bundler_installed?, #bundler_version, #install_bundler

Methods inherited from Strategy

#changes_at?, #checkout_code, #cleanup_release, #cleanup_releases, #connect, #debug?, #deploy_running?, #disable_history, #execute_commands, #execute_hook, #export_environment, #keep_releases, #link_release, #release_lock, #rollback, #setup, #update_code, #update_code_svn, #write_lock, #write_release, #write_revision

Methods inherited from Deploy

#available_releases, #initialize, #last_version

Methods included from PathHelpers

#current_path, #deploy_path, #release_path, #scm_path, #shared_path, #version_path

Methods included from Helpers

#deployer_hostname, #error, #git_installed?, #git_remote, #log, #release_exists?, #stream_output, #svn_installed?, #warn

Constructor Details

This class inherits a constructor from Shuttle::Deploy

Instance Method Details

#cache_assets?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/shuttle/deployment/rails.rb', line 22

def cache_assets?
  config.rails && config.rails.cache_assets == true
end

#deployObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/shuttle/deployment/rails.rb', line 84

def deploy
  ssh.export('RACK_ENV', rails_env)
  ssh.export('RAILS_ENV', rails_env)

  log "Rails environment is set to #{rails_env}"

  setup
  setup_bundler
  update_code
  checkout_code
  bundle_install
  migrate_database
  precompile_assets
  link_shared_paths
  
  if start_server?
    thin_restart
  end

  link_release
  cleanup_releases
end


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/shuttle/deployment/rails.rb', line 131

def link_shared_paths
  ssh.run("mkdir -p #{release_path('tmp')}")
  ssh.run("rm -rf #{release_path}/log")
  ssh.run("ln -s #{shared_path('pids')} #{release_path('tmp/pids')}")
  ssh.run("ln -s #{shared_path('log')} #{release_path('log')}")

  if config.rails
    if config.rails.shared_paths
      config.rails.shared_paths.each_pair do |name, path|
        if ssh.directory_exists?("#{shared_path}/#{name}")
          log "Linking shared path: #{name}"
          ssh.run("ln -s #{shared_path}/#{name} #{release_path}/#{path}")
        else
          error "Shared path does not exist: #{shared_path}/#{name}"
        end
      end
    end
  end
end

#migrate_databaseObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/shuttle/deployment/rails.rb', line 107

def migrate_database
  return if !ssh.file_exists?(release_path('db/schema.rb'))

  migrate     = true # Will migrate by default
  schema      = ssh.read_file(release_path('db/schema.rb'))
  schema_file = shared_path('schema')
  checksum    = Digest::SHA1.hexdigest(schema)

  if ssh.file_exists?(schema_file)
    old_checksum = ssh.read_file(schema_file).strip
    if old_checksum == checksum
      migrate = false
    end
  end

  if migrate == true
    log "Migrating database"
    rake("db:migrate", true)
    ssh.run("echo #{checksum} > #{schema_file}")
  else
    log "Database migration skipped"
  end
end

#precompile_assetsObject

Precompile rails assets. If no changes detected between last and current releases, precompile task will be skipped.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/shuttle/deployment/rails.rb', line 59

def precompile_assets
  if precompile_assets?
    precompile = true

    # Detect if there any change in assets
    if cache_assets? && last_version != version
      old_path = deploy_path("releases/#{last_version}")
      new_path = release_path

      result = ssh.run("diff -arq #{old_path}/app/assets #{new_path}/app/assets")
      if result.success?
        precompile = false
        ssh.run("cp -a #{old_path}/public/assets #{new_path}/public/")
      end
    end

    if precompile
      log "Precompiling assets"
      rake 'assets:precompile'
    else
      log "Asset procompilation skipped"
    end
  end
end

#precompile_assets?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/shuttle/deployment/rails.rb', line 14

def precompile_assets?
  if ENV["ASSETS"]
    true
  else
    config.rails && config.rails.precompile_assets != false
  end
end

#rails_envObject



6
7
8
9
10
11
12
# File 'lib/shuttle/deployment/rails.rb', line 6

def rails_env
  if config.rails && config.rails.environment
    config.rails.environment
  else
    environment
  end
end

#rake(command, print_output = false) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/shuttle/deployment/rails.rb', line 47

def rake(command, print_output = false)
  result = ssh.run("cd #{release_path} && bundle exec rake #{command}")

  if result.success?
    puts result.output if print_output
  else
    error "Unable to run rake command: #{command}. Reason: #{result.output}"
  end
end

#setup_bundlerObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/shuttle/deployment/rails.rb', line 34

def setup_bundler
  if !bundler_installed?
    log "Bundler is missing. Installing"

    res = ssh.run("sudo gem install bundler")
    if res.success?
      log "Bundler v#{bundler_version} installed"
    else
      error "Unable to install bundler: #{res.output}"
    end
  end
end

#start_server?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/shuttle/deployment/rails.rb', line 26

def start_server?
  if config.rails && !config.rails.start_server.nil?
    config.rails.start_server
  else
    true
  end
end