Class: EY::Serverside::DeployBase
- Defined in:
- lib/engineyard-serverside/deploy.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Task
Instance Method Summary collapse
- #bundle ⇒ Object
- #cached_deploy ⇒ Object
- #callback(what) ⇒ Object
- #check_dependencies ⇒ Object
- #check_repository ⇒ Object
-
#clean_release_directory(dir, count = 3) ⇒ Object
Remove all but the most-recent
countreleases from the specified release directory. -
#cleanup_old_releases ⇒ Object
task.
-
#copy_repository_cache ⇒ Object
task.
- #create_revision_file ⇒ Object
- #create_revision_file_command ⇒ Object
- #dependency_manager ⇒ Object
-
#deploy ⇒ Object
default task.
- #disable_maintenance_page ⇒ Object
- #enable_maintenance_page ⇒ Object
- #ensure_git_ssh_wrapper ⇒ Object
- #ensure_ownership(*targets) ⇒ Object
- #gc_repository_cache ⇒ Object
-
#generate_ssh_wrapper ⇒ Object
We specify 'IdentitiesOnly' to avoid failures on systems with > 5 private keys available.
- #install_dependencies ⇒ Object
-
#migrate ⇒ Object
task.
-
#move_symlink(source, link, name) ⇒ Object
Move a symlink as atomically as we can.
-
#push_code ⇒ Object
task.
-
#restart ⇒ Object
task.
- #restart_command ⇒ Object
- #restart_with_maintenance_page ⇒ Object
-
#rollback ⇒ Object
task.
- #run_with_callbacks(task) ⇒ Object
- #setup_services ⇒ Object
- #setup_sqlite3_if_necessary ⇒ Object
- #short_log_message(revision) ⇒ Object
-
#ssh_executable ⇒ Object
create ssh wrapper on all servers.
-
#strategy ⇒ Object
Use [] to access attributes instead of calling methods so that we get nils instead of NoMethodError.
-
#symlink ⇒ Object
task.
- #symlink_configs ⇒ Object
- #symlink_tasks ⇒ Object
- #update_repository_cache ⇒ Object
Methods inherited from Task
#c, #initialize, #load_ey_yml, #paths, #require_custom_tasks, #roles, #run, #sudo
Constructor Details
This class inherits a constructor from EY::Serverside::Task
Instance Method Details
#bundle ⇒ Object
161 162 163 |
# File 'lib/engineyard-serverside/deploy.rb', line 161 def bundle install_dependencies end |
#cached_deploy ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/engineyard-serverside/deploy.rb', line 21 def cached_deploy shell.status "Deploying app from cached copy at #{Time.now.asctime}" load_ey_yml require_custom_tasks push_code shell.status "Starting full deploy" copy_repository_cache check_repository with_failed_release_cleanup do create_revision_file run_with_callbacks(:bundle) setup_services symlink_configs setup_sqlite3_if_necessary run_with_callbacks(:compile_assets) # defined in RailsAssetSupport enable_maintenance_page run_with_callbacks(:migrate) callback(:before_symlink) # We don't use run_with_callbacks for symlink because we need # to clean up manually if it fails. symlink end callback(:after_symlink) run_with_callbacks(:restart) disable_maintenance_page cleanup_old_releases gc_repository_cache shell.status "Finished deploy at #{Time.now.asctime}" rescue Exception => e shell.status "Finished failing to deploy at #{Time.now.asctime}" shell.error "Exception during deploy: #{e.inspect}\n#{e.backtrace}" puts_deploy_failure raise end |
#callback(what) ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/engineyard-serverside/deploy.rb', line 348 def callback(what) @callbacks_reached ||= true if paths.deploy_hook(what).exist? shell.status "Running deploy hook: deploy/#{what}.rb" run Escape.shell_command(base_callback_command_for(what)) do |server, cmd| per_instance_args = [] per_instance_args << '--current-roles' << server.roles.to_a.join(' ') per_instance_args << '--current-name' << server.name.to_s if server.name per_instance_args << '--config' << config.to_json cmd << " " << Escape.shell_command(per_instance_args) end end end |
#check_dependencies ⇒ Object
80 81 82 |
# File 'lib/engineyard-serverside/deploy.rb', line 80 def check_dependencies dependency_manager.check end |
#check_repository ⇒ Object
76 77 78 |
# File 'lib/engineyard-serverside/deploy.rb', line 76 def check_repository check_dependencies end |
#clean_release_directory(dir, count = 3) ⇒ Object
Remove all but the most-recent count releases from the specified
release directory.
IMPORTANT: This expects the release directory naming convention to be
something with a sensible lexical order. Violate that at your peril.
178 179 180 181 182 183 184 |
# File 'lib/engineyard-serverside/deploy.rb', line 178 def clean_release_directory(dir, count = 3) @cleanup_failed = true ordinal = count.succ.to_s shell.status "Cleaning release directory: #{dir}" sudo "ls -r #{dir} | tail -n +#{ordinal} | xargs -I@ rm -rf #{dir}/@" @cleanup_failed = false end |
#cleanup_old_releases ⇒ Object
task
170 171 172 |
# File 'lib/engineyard-serverside/deploy.rb', line 170 def cleanup_old_releases clean_release_directory(paths.releases) end |
#copy_repository_cache ⇒ Object
task
223 224 225 226 227 228 229 230 |
# File 'lib/engineyard-serverside/deploy.rb', line 223 def copy_repository_cache shell.status "Copying to #{paths.active_release}" exclusions = Array(config.copy_exclude).map { |e| %|--exclude="#{e}"| }.join(' ') run("mkdir -p #{paths.active_release} #{paths.shared_config} && rsync -aq #{exclusions} #{paths.repository_cache}/ #{paths.active_release}") shell.status "Ensuring proper ownership." ensure_ownership(paths.active_release) end |
#create_revision_file ⇒ Object
232 233 234 |
# File 'lib/engineyard-serverside/deploy.rb', line 232 def create_revision_file run create_revision_file_command end |
#create_revision_file_command ⇒ Object
68 69 70 |
# File 'lib/engineyard-serverside/deploy.rb', line 68 def create_revision_file_command strategy.create_revision_file_command(paths.active_release) end |
#dependency_manager ⇒ Object
433 434 435 436 |
# File 'lib/engineyard-serverside/deploy.rb', line 433 def dependency_manager ensure_git_ssh_wrapper @dependency_manager ||= DependencyManager.detect(servers, config, shell, self) end |
#deploy ⇒ Object
default task
15 16 17 18 19 |
# File 'lib/engineyard-serverside/deploy.rb', line 15 def deploy shell.status "Starting deploy at #{shell.start_time.asctime}" update_repository_cache cached_deploy end |
#disable_maintenance_page ⇒ Object
96 97 98 |
# File 'lib/engineyard-serverside/deploy.rb', line 96 def disable_maintenance_page maintenance.conditionally_disable end |
#enable_maintenance_page ⇒ Object
92 93 94 |
# File 'lib/engineyard-serverside/deploy.rb', line 92 def enable_maintenance_page maintenance.conditionally_enable end |
#ensure_git_ssh_wrapper ⇒ Object
129 130 131 |
# File 'lib/engineyard-serverside/deploy.rb', line 129 def ensure_git_ssh_wrapper ENV['GIT_SSH'] = ssh_executable.to_s end |
#ensure_ownership(*targets) ⇒ Object
333 334 335 |
# File 'lib/engineyard-serverside/deploy.rb', line 333 def ensure_ownership(*targets) sudo "find #{targets.join(' ')} -not -user #{config.user} -or -not -group #{config.group} -exec chown #{config.user}:#{config.group} {} +" end |
#gc_repository_cache ⇒ Object
64 65 66 |
# File 'lib/engineyard-serverside/deploy.rb', line 64 def gc_repository_cache strategy.gc_repository_cache end |
#generate_ssh_wrapper ⇒ Object
We specify 'IdentitiesOnly' to avoid failures on systems with > 5 private keys available. We set UserKnownHostsFile to /dev/null because StrickHostKeyChecking no doesn't ignore existing entries in known_hosts; we want to actively ignore all such. Learned this at http://lists.mindrot.org/pipermail/openssh-unix-dev/2009-February/027271.html (Thanks Jim L.)
148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/engineyard-serverside/deploy.rb', line 148 def generate_ssh_wrapper path = paths.ssh_wrapper "mkdir -p \#{path.dirname}\n[[ -x \#{path} ]] || cat > \#{path} <<'SSH'\n#!/bin/sh\nunset SSH_AUTH_SOCK\nssh -o CheckHostIP=no -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o LogLevel=INFO -o IdentityFile=\#{paths.deploy_key} -o IdentitiesOnly=yes $*\nSSH\nchmod 0700 \#{path}\n SCRIPT\nend\n" |
#install_dependencies ⇒ Object
165 166 167 |
# File 'lib/engineyard-serverside/deploy.rb', line 165 def install_dependencies dependency_manager.install end |
#migrate ⇒ Object
task
212 213 214 215 216 217 218 219 220 |
# File 'lib/engineyard-serverside/deploy.rb', line 212 def migrate return unless config.migrate? @migrations_reached = true cmd = "cd #{paths.active_release} && PATH=#{paths.binstubs}:$PATH #{config.framework_envs} #{config.migration_command}" roles :app_master, :solo do shell.status "Migrating: #{cmd}" run(cmd) end end |
#move_symlink(source, link, name) ⇒ Object
Move a symlink as atomically as we can.
mv -T renames 'next' to 'current' instead of moving 'next' to current/next' mv -T isn't available on OS X and maybe elsewhere, so fallback to rm && ln
341 342 343 344 345 346 |
# File 'lib/engineyard-serverside/deploy.rb', line 341 def move_symlink(source, link, name) next_link = link.dirname.join(name) mv_t = "ln -nfs #{source} #{next_link} && mv -T #{next_link} #{link} >/dev/null 2>&1" rm_ln = "rm -rf #{next_link} #{link} && ln -nfs #{source} #{link}" "((#{mv_t}) || (#{rm_ln}))" end |
#push_code ⇒ Object
task
107 108 109 110 111 112 113 |
# File 'lib/engineyard-serverside/deploy.rb', line 107 def push_code shell.status "Pushing code to all servers" servers.remote.run_on_each(shell) do |server| cmd = server.sync_directory_command(paths.repository_cache) shell.logged_system(cmd) end end |
#restart ⇒ Object
task
116 117 118 119 120 121 122 123 |
# File 'lib/engineyard-serverside/deploy.rb', line 116 def restart @restart_failed = true shell.status "Restarting app servers" roles :app_master, :app, :solo do run(restart_command) end @restart_failed = false end |
#restart_command ⇒ Object
125 126 127 |
# File 'lib/engineyard-serverside/deploy.rb', line 125 def restart_command %{LANG="en_US.UTF-8" /engineyard/bin/app_#{config.app} deploy} end |
#restart_with_maintenance_page ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/engineyard-serverside/deploy.rb', line 84 def restart_with_maintenance_page load_ey_yml require_custom_tasks enable_maintenance_page restart disable_maintenance_page end |
#rollback ⇒ Object
task
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/engineyard-serverside/deploy.rb', line 187 def rollback if config.rollback_paths! begin rolled_back_release = paths.latest_release shell.status "Rolling back to previous release: #{short_log_message(config.active_revision)}" run_with_callbacks(:symlink) sudo "rm -rf #{rolled_back_release}" bundle shell.status "Restarting with previous release." enable_maintenance_page run_with_callbacks(:restart) disable_maintenance_page shell.status "Finished rollback at #{Time.now.asctime}" rescue Exception shell.status "Failed to rollback at #{Time.now.asctime}" puts_deploy_failure raise end else shell.fatal "Already at oldest release, nothing to roll back to." exit(1) end end |
#run_with_callbacks(task) ⇒ Object
100 101 102 103 104 |
# File 'lib/engineyard-serverside/deploy.rb', line 100 def run_with_callbacks(task) callback("before_#{task}") send(task) callback("after_#{task}") end |
#setup_services ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/engineyard-serverside/deploy.rb', line 236 def setup_services shell.status "Setting up external services." previously_configured_services = config.configured_services begin sudo(config.services_check_command) rescue EY::Serverside::RemoteFailure shell.info "Could not setup services. Upgrade your environment to get services configuration." return end begin sudo(config.services_setup_command) rescue EY::Serverside::RemoteFailure => e if previously_configured_services shell.warning "External services configuration not updated. Using previous version.\nDeploy again if your services configuration appears incomplete or out of date.\n\#{e}\n WARNING\n end\n end\n\n if services = config.configured_services\n shell.status \"Services configured: \#{services.join(', ')}\"\n dependency_manager.check_ey_config\n end\nend\n" |
#setup_sqlite3_if_necessary ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/engineyard-serverside/deploy.rb', line 265 def setup_sqlite3_if_necessary if dependency_manager.uses_sqlite3? shell.status "Setting up SQLite3 Database for compatibility with application's chosen adapter" shell.warning "SQLite3 cannot persist across servers. Please upgrade to a supported database." shell.substatus "Create databases directory if needed" run "mkdir -p #{paths.shared}/databases" shell.substatus "Create SQLite database if needed" run "touch #{paths.shared}/databases/#{config.framework_env}.sqlite3" shell.substatus "Create config directory if needed" run "mkdir -p #{paths.active_release_config}" shell.substatus "Generating SQLite config" run "cat > \#{paths.shared_config}/database.sqlite3.yml<<'YML'\n\#{config.framework_env}:\n adapter: sqlite3\n database: \#{paths.shared}/databases/\#{config.framework_env}.sqlite3\n pool: 5\n timeout: 5000\nYML\n WRAP\n\n shell.substatus \"Symlink database.yml\"\n run \"ln -nfs \#{paths.shared_config}/database.sqlite3.yml \#{paths.active_release_config}/database.yml\"\n end\nend\n" |
#short_log_message(revision) ⇒ Object
72 73 74 |
# File 'lib/engineyard-serverside/deploy.rb', line 72 def (revision) strategy.(revision) end |
#ssh_executable ⇒ Object
create ssh wrapper on all servers
134 135 136 137 138 139 140 141 |
# File 'lib/engineyard-serverside/deploy.rb', line 134 def ssh_executable @ssh_executable ||= begin roles :app_master, :app, :solo, :util do run(generate_ssh_wrapper) end paths.ssh_wrapper end end |
#strategy ⇒ Object
Use [] to access attributes instead of calling methods so that we get nils instead of NoMethodError.
Rollback doesn't know about the repository location (nor should it need to), but it would like to use #short_log_message.
369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/engineyard-serverside/deploy.rb', line 369 def strategy ensure_git_ssh_wrapper @strategy ||= config.strategy_class.new( shell, :verbose => config.verbose, :repository_cache => paths.repository_cache.to_s, :app => config.app, :repo => config[:repo], :ref => config[:branch] ) end |
#symlink ⇒ Object
task
321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/engineyard-serverside/deploy.rb', line 321 def symlink shell.status "Symlinking code." run move_symlink(paths.active_release, paths.current, "deploying") ensure_ownership(paths.current) @symlink_changed = true rescue Exception sudo move_symlink(paths.previous_release(paths.active_release), paths.current, "reverting") ensure_ownership(paths.current) @symlink_changed = false raise end |
#symlink_configs ⇒ Object
295 296 297 298 299 300 301 |
# File 'lib/engineyard-serverside/deploy.rb', line 295 def symlink_configs shell.status "Preparing shared resources for release." symlink_tasks.each do |what, cmd| shell.substatus what run(cmd) end end |
#symlink_tasks ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/engineyard-serverside/deploy.rb', line 303 def symlink_tasks [ ["Set group write permissions", "chmod -R g+w #{paths.active_release}"], ["Remove symlinked shared directories", "rm -rf #{paths.active_log} #{paths.public_system} #{paths.active_release}/tmp/pids"], ["Create tmp directory", "mkdir -p #{paths.active_release}/tmp"], ["Create public directory", "mkdir -p #{paths.public}"], ["Create config directory", "mkdir -p #{paths.active_release_config}"], ["Symlink shared log directory", "ln -nfs #{paths.shared_log} #{paths.active_log}"], ["Symlink public system directory", "ln -nfs #{paths.shared_system} #{paths.public_system}"], ["Symlink shared pids directory", "ln -nfs #{paths.shared}/pids #{paths.active_release}/tmp/pids"], ["Symlink other shared config files", "find #{paths.shared_config} -type f -not -name 'database.yml' -exec ln -s {} #{paths.active_release_config} \\;"], ["Symlink database.yml if needed", "if [ -f \"#{paths.shared_config}/database.yml\" ]; then ln -nfs #{paths.shared_config}/database.yml #{paths.active_release_config}/database.yml; fi"], ["Symlink newrelic.yml if needed", "if [ -f \"#{paths.shared_config}/newrelic.yml\" ]; then ln -nfs #{paths.shared_config}/newrelic.yml #{paths.active_release_config}/newrelic.yml; fi"], ["Symlink mongrel_cluster.yml if needed", "if [ -f \"#{paths.shared_config}/mongrel_cluster.yml\" ]; then ln -nfs #{paths.shared_config}/mongrel_cluster.yml #{paths.active_release_config}/mongrel_cluster.yml; fi"], ] end |
#update_repository_cache ⇒ Object
60 61 62 |
# File 'lib/engineyard-serverside/deploy.rb', line 60 def update_repository_cache strategy.update_repository_cache end |