Class: EY::DeployBase

Inherits:
Task show all
Includes:
LoggedOutput
Defined in:
lib/ey-deploy/deploy.rb

Direct Known Subclasses

Deploy

Constant Summary

Constants included from Dataflow

Dataflow::UnificationError, Dataflow::VERSION

Instance Attribute Summary

Attributes inherited from Task

#config

Instance Method Summary collapse

Methods included from LoggedOutput

#debug, #info, logfile, logfile=, #logged_system, verbose=, verbose?, #verbose?

Methods inherited from Task

#initialize, #require_custom_tasks, #roles, #run, #sudo

Methods included from Dataflow

#barrier, #by_need, #flow, included, #local, #need_later, #unify

Constructor Details

This class inherits a constructor from EY::Task

Instance Method Details

#bundleObject

task



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ey-deploy/deploy.rb', line 120

def bundle
  if File.exist?("#{c.release_path}/Gemfile")
    info "~> Gemfile detected, bundling gems"
    lockfile = File.join(c.release_path, "Gemfile.lock")

    bundler_installer = if File.exist?(lockfile)
                          get_bundler_installer(lockfile)
                        else
                          warn_about_missing_lockfile
                          BundleInstaller.new(DEFAULT_09_BUNDLER, "--without=development --without=test")
                        end

    sudo "#{$0} _#{VERSION}_ install_bundler #{bundler_installer.version}"

    run "cd #{c.release_path} && bundle _#{bundler_installer.version}_ install #{bundler_installer.options}"
  end
end

#callback(what) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/ey-deploy/deploy.rb', line 220

def callback(what)
  @callbacks_reached ||= true
  if File.exist?("#{c.release_path}/deploy/#{what}.rb")
    eydeploy_path = $0   # invoke others just like we were invoked
    run "#{eydeploy_path} _#{VERSION}_ hook '#{what}' --app '#{config.app}' --release-path #{config.release_path}" do |server, cmd|
      cmd << " --framework-env '#{c.environment}'"
      cmd << " --current-role '#{server.role}'"
      cmd << " --current-name '#{server.name}'" if server.name
      cmd << " --config '#{c[:config]}'" if c.has_key?(:config)
      cmd
    end
  end
end

#cleanup_old_releasesObject

task



139
140
141
142
143
144
# File 'lib/ey-deploy/deploy.rb', line 139

def cleanup_old_releases
  @cleanup_failed = true
  info "~> Cleaning up old releases"
  sudo "ls #{c.release_dir} | head -n -3 | xargs -I{} rm -rf #{c.release_dir}/{}"
  @cleanup_failed = false
end

#conditionally_enable_maintenance_pageObject



70
71
72
73
74
# File 'lib/ey-deploy/deploy.rb', line 70

def conditionally_enable_maintenance_page
  if c.migrate? || c.stack == "nginx_mongrel"
    enable_maintenance_page
  end
end

#copy_repository_cacheObject

task



177
178
179
180
181
182
183
# File 'lib/ey-deploy/deploy.rb', line 177

def copy_repository_cache
  info "~> Copying to #{c.release_path}"
  run("mkdir -p #{c.release_path} && rsync -aq #{c.exclusions} #{c.repository_cache}/ #{c.release_path}")

  info "~> Ensuring proper ownership"
  sudo("chown -R #{c.user}:#{c.group} #{c.deploy_to}")
end

#create_revision_fileObject



185
186
187
# File 'lib/ey-deploy/deploy.rb', line 185

def create_revision_file
  run create_revision_file_command
end

#deployObject

default task



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
36
37
38
39
40
# File 'lib/ey-deploy/deploy.rb', line 11

def deploy
  debug "Starting deploy at #{Time.now.asctime}"
  update_repository_cache
  require_custom_tasks
  push_code

  info "~> Starting full deploy"
  copy_repository_cache

  with_failed_release_cleanup do
    create_revision_file
    bundle
    symlink_configs
    conditionally_enable_maintenance_page
    run_with_callbacks(:migrate)
    callback(:before_symlink)
    symlink
  end

  callback(:after_symlink)
  run_with_callbacks(:restart)
  disable_maintenance_page

  cleanup_old_releases
  debug "Finished deploy at #{Time.now.asctime}"
rescue Exception
  debug "Finished failing to deploy at #{Time.now.asctime}"
  puts_deploy_failure
  raise
end

#disable_maintenance_pageObject



76
77
78
79
80
81
# File 'lib/ey-deploy/deploy.rb', line 76

def disable_maintenance_page
  @maintenance_up = false
  roles :app_master, :app, :solo do
    run "rm -f #{File.join(c.shared_path, "system", "maintenance.html")}"
  end
end

#enable_maintenance_pageObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ey-deploy/deploy.rb', line 42

def enable_maintenance_page
  maintenance_page_candidates = [
    "public/maintenance.html.custom",
    "public/maintenance.html.tmp",
    "public/maintenance.html",
    "public/system/maintenance.html.default",
  ].map do |file|
    File.join(c.latest_release, file)
  end

  # this one is guaranteed to exist
  maintenance_page_candidates <<  File.expand_path(
    "default_maintenance_page.html",
    File.dirname(__FILE__)
    )

  # put in the maintenance page
  maintenance_file = maintenance_page_candidates.detect do |file|
    File.exists?(file)
  end

  @maintenance_up = true
  roles :app_master, :app, :solo do
    visible_maint_page = File.join(c.shared_path, "system", "maintenance.html")
    run "cp '#{maintenance_file}' '#{visible_maint_page}'"
  end
end

#get_bundler_installer(lockfile) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/ey-deploy/deploy.rb', line 288

def get_bundler_installer(lockfile)
  parser = LockfileParser.new(File.read(lockfile))
  case parser.lockfile_version
  when :bundler09
    BundleInstaller.new(
      parser.bundler_version || DEFAULT_09_BUNDLER,
      "--without=development --without=test")
  when :bundler10
    BundleInstaller.new(
      parser.bundler_version || DEFAULT_10_BUNDLER,
      "--deployment --path #{c.shared_path}/bundled_gems --without development test"
      )
  else
    raise "Unknown lockfile version #{parser.lockfile_version}"
  end
end

#migrateObject

task



166
167
168
169
170
171
172
173
174
# File 'lib/ey-deploy/deploy.rb', line 166

def migrate
  return unless c.migrate?
  @migrations_reached = true
  roles :app_master, :solo do
    cmd = "cd #{c.release_path} && #{c.framework_envs} #{c.migration_command}"
    info "~> Migrating: #{cmd}"
    run(cmd)
  end
end

#push_codeObject

task



90
91
92
93
94
95
# File 'lib/ey-deploy/deploy.rb', line 90

def push_code
  info "~> Pushing code to all servers"
  barrier *(EY::Server.all.map do |server|
    need_later { server.push_code }
  end)
end

#restartObject

task



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ey-deploy/deploy.rb', line 98

def restart
  @restart_failed = true
  info "~> Restarting app servers"
  roles :app_master, :app, :solo do
    restart_command = case c.stack
    when "nginx_unicorn"
      pidfile = "/var/run/engineyard/unicorn_#{c.app}.pid"
      condition = "[ -e #{pidfile} ] && [ ! -d /proc/`cat #{pidfile}` ]"
      run("if #{condition}; then rm -f #{pidfile}; fi")
      run("/engineyard/bin/app_#{c.app} deploy")
    when "nginx_mongrel"
      sudo("monit restart all -g #{c.app}")
    when "nginx_passenger"
      run("touch #{c.current_path}/tmp/restart.txt")
    else
      raise "Unknown stack #{c.stack}; restart failed!"
    end
  end
  @restart_failed = false
end

#rollbackObject

task



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ey-deploy/deploy.rb', line 147

def rollback
  if c.all_releases.size > 1
    c.release_path = c.previous_release

    revision = File.read(File.join(c.release_path, 'REVISION')).strip
    info "~> Rolling back to previous release: #{short_log_message(revision)}"

    run_with_callbacks(:symlink, c.previous_release)
    cleanup_current_release
    bundle
    info "~> Restarting with previous release"
    with_maintenance_page { run_with_callbacks(:restart) }
  else
    info "~> Already at oldest release, nothing to roll back to"
    exit(1)
  end
end

#run_with_callbacks(task, *args) ⇒ Object



83
84
85
86
87
# File 'lib/ey-deploy/deploy.rb', line 83

def run_with_callbacks(task, *args)
  callback(:"before_#{task}")
  send(task, *args)
  callback(:"after_#{task}")
end

task



210
211
212
213
214
215
216
217
218
# File 'lib/ey-deploy/deploy.rb', line 210

def symlink(release_to_link=c.release_path)
  info "~> Symlinking code"
  run "rm -f #{c.current_path} && ln -nfs #{release_to_link} #{c.current_path} && chown -R #{c.user}:#{c.group} #{c.current_path}"
  @symlink_changed = true
rescue Exception
  sudo "rm -f #{c.current_path} && ln -nfs #{c.previous_release(release_to_link)} #{c.current_path} && chown -R #{c.user}:#{c.group} #{c.current_path}"
  @symlink_changed = false
  raise
end


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/ey-deploy/deploy.rb', line 189

def symlink_configs(release_to_link=c.release_path)
  info "~> Symlinking configs"
  [ "chmod -R g+w #{release_to_link}",
    "rm -rf #{release_to_link}/log #{release_to_link}/public/system #{release_to_link}/tmp/pids",
    "mkdir -p #{release_to_link}/tmp",
    "ln -nfs #{c.shared_path}/log #{release_to_link}/log",
    "mkdir -p #{release_to_link}/public",
    "mkdir -p #{release_to_link}/config",
    "ln -nfs #{c.shared_path}/system #{release_to_link}/public/system",
    "ln -nfs #{c.shared_path}/pids #{release_to_link}/tmp/pids",
    "ln -nfs #{c.shared_path}/config/database.yml #{release_to_link}/config/database.yml",
    "ln -nfs #{c.shared_path}/config/mongrel_cluster.yml #{release_to_link}/config/mongrel_cluster.yml",
  ].each do |cmd|
    run cmd
  end

  sudo "chown -R #{c.user}:#{c.group} #{release_to_link}"
  run "if [ -f \"#{c.shared_path}/config/newrelic.yml\" ]; then ln -nfs #{c.shared_path}/config/newrelic.yml #{release_to_link}/config/newrelic.yml; fi"
end