/
deploy.rb
104 lines (85 loc) · 2.93 KB
/
deploy.rb
1
2
3
4
5
6
7
8
9
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
36
37
38
39
40
41
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module Inploy
class Deploy
include Helper
include DSL
attr_accessor :repository, :user, :application, :hosts, :path, :app_folder, :ssh_opts, :branch, :environment,
:port, :skip_steps, :cache_dirs, :sudo
define_callbacks :after_setup, :before_restarting_server
def initialize
self.server = :passenger
@cache_dirs = %w(public/cache)
@branch = 'master'
@environment = 'production'
@user = "deploy"
@path = "/var/local/apps"
@app_folder = nil
configure
end
def template=(template)
load_module "templates/#{template}"
end
def server=(server)
load_module "servers/#{server}"
end
def configure
configure_from configuration_file if configuration_file
end
def configure_from(file)
deploy = self
eval file.read + ';local_variables.each { |variable| deploy.send "#{variable}=", eval(variable.to_s) rescue nil }'
end
def remote_install(opts)
remote_run "bash < <(wget -O- #{opts[:from]})"
end
def remote_setup
remote_run "cd #{path} && #{@sudo}git clone --depth 1 #{repository} #{application} && cd #{directory} #{checkout}#{bundle} && #{@sudo}rake inploy:local:setup environment=#{environment}#{skip_steps_cmd}"
end
def local_setup
create_folders 'tmp/pids', 'db'
copy_sample_files
rake "db:create RAILS_ENV=#{environment}"
run "./init.sh" if file_exists?("init.sh")
after_update_code
callback :after_setup
end
def remote_update
remote_run "cd #{application_path} && #{@sudo}rake inploy:local:update environment=#{environment}#{skip_steps_cmd}"
end
def remote_rake(task)
remote_run "cd #{application_path} && rake #{task} RAILS_ENV=#{environment}"
end
def local_update
update_code
after_update_code
end
def update_code
run "git pull origin #{branch}"
end
private
def directory
app_folder.nil? ? application : "#{application}/#{app_folder}"
end
def checkout
branch.eql?("master") ? "" : "&& $(git branch | grep -vq #{branch}) && git checkout -f -b #{branch} origin/#{branch}"
end
def bundle
" && #{bundle_cmd}" if using_bundler?
end
def after_update_code
run "git submodule update --init"
copy_sample_files
return unless install_gems
migrate_database
update_crontab
clear_cache
run "rm -R -f public/assets" if jammit_is_installed?
rake_if_included "more:parse"
run "compass compile" if file_exists?("config/initializers/compass.rb")
rake_if_included "asset:packager:build_all"
rake_if_included "hoptoad:deploy RAILS_ENV=#{environment} TO=#{environment} REPO=#{repository} REVISION=#{`git log | head -1 | cut -d ' ' -f 2`}"
ruby_if_exists "vendor/plugins/newrelic_rpm/bin/newrelic_cmd", :params => "deployments"
callback :before_restarting_server
restart_server
end
end
end