-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
opsworks_deploy.rb
194 lines (169 loc) · 5.95 KB
/
opsworks_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
define :opsworks_deploy do
application = params[:app]
deploy = params[:deploy_data]
directory "#{deploy[:deploy_to]}" do
group deploy[:group]
owner deploy[:user]
mode "0775"
action :create
recursive true
end
if deploy[:scm]
ensure_scm_package_installed(deploy[:scm][:scm_type])
prepare_git_checkouts(
:user => deploy[:user],
:group => deploy[:group],
:home => deploy[:home],
:ssh_key => deploy[:scm][:ssh_key]
) if deploy[:scm][:scm_type].to_s == 'git'
prepare_svn_checkouts(
:user => deploy[:user],
:group => deploy[:group],
:home => deploy[:home],
:deploy => deploy,
:application => application
) if deploy[:scm][:scm_type].to_s == 'svn'
if deploy[:scm][:scm_type].to_s == 'archive'
repository = prepare_archive_checkouts(deploy[:scm])
node.set[:deploy][application][:scm] = {
:scm_type => 'git',
:repository => repository
}
elsif deploy[:scm][:scm_type].to_s == 's3'
repository = prepare_s3_checkouts(deploy[:scm])
node.set[:deploy][application][:scm] = {
:scm_type => 'git',
:repository => repository
}
end
end
deploy = node[:deploy][application]
directory "#{deploy[:deploy_to]}/shared/cached-copy" do
recursive true
action :delete
only_if do
deploy[:delete_cached_copy]
end
end
ruby_block "change HOME to #{deploy[:home]} for source checkout" do
block do
ENV['HOME'] = "#{deploy[:home]}"
end
end
# setup deployment & checkout
if deploy[:scm] && deploy[:scm][:scm_type] != 'other'
Chef::Log.debug("Checking out source code of application #{application} with type #{deploy[:application_type]}")
deploy deploy[:deploy_to] do
provider Chef::Provider::Deploy.const_get(deploy[:chef_provider])
if deploy[:keep_releases]
keep_releases deploy[:keep_releases]
end
repository deploy[:scm][:repository]
user deploy[:user]
group deploy[:group]
revision deploy[:scm][:revision]
migrate deploy[:migrate]
migration_command deploy[:migrate_command]
environment deploy[:environment].to_hash
create_dirs_before_symlink( deploy[:create_dirs_before_symlink] )
symlink_before_migrate( deploy[:symlink_before_migrate] )
action deploy[:action]
if deploy[:application_type] == 'rails'
restart_command "sleep #{deploy[:sleep_before_restart]} && #{node[:opsworks][:rails_stack][:restart_command]}"
end
case deploy[:scm][:scm_type].to_s
when 'git'
scm_provider :git
enable_submodules deploy[:enable_submodules]
shallow_clone deploy[:shallow_clone]
when 'svn'
scm_provider :subversion
svn_username deploy[:scm][:user]
svn_password deploy[:scm][:password]
svn_arguments "--no-auth-cache --non-interactive --trust-server-cert"
svn_info_args "--no-auth-cache --non-interactive --trust-server-cert"
else
raise "unsupported SCM type #{deploy[:scm][:scm_type].inspect}"
end
before_migrate do
link_tempfiles_to_current_release
case deploy[:application_type]
when 'rails'
if deploy[:auto_bundle_on_deploy]
OpsWorks::RailsConfiguration.bundle(application, node[:deploy][application], release_path)
end
node.default[:deploy][application][:database][:adapter] = OpsWorks::RailsConfiguration.determine_database_adapter(
application,
deploy,
release_path,
:force => node[:force_database_adapter_detection],
:consult_gemfile => deploy[:auto_bundle_on_deploy]
)
deploy = node[:deploy][application]
rails_configuration "Generate opsworks configration for app #{application.inspect}" do
application application
deploy_to deploy[:deploy_to]
rails_env deploy[:rails_env]
user deploy[:user]
group deploy[:group]
database_data deploy[:database]
memcached_data deploy[:memcached] || {}
end.run_action(:create)
when 'php'
template "#{node[:deploy][application][:deploy_to]}/shared/config/opsworks.php" do
cookbook 'php'
source 'opsworks.php.erb'
mode '0660'
owner node[:deploy][application][:user]
group node[:deploy][application][:group]
variables(
:database => node[:deploy][application][:database],
:memcached => node[:deploy][application][:memcached],
:layers => node[:opsworks][:layers],
:stack_name => node[:opsworks][:stack][:name]
)
only_if do
File.exists?("#{node[:deploy][application][:deploy_to]}/shared/config")
end
end
when 'nodejs'
if deploy[:auto_npm_install_on_deploy]
OpsWorks::NodejsConfiguration.npm_install(application, node[:deploy][application], release_path)
end
end
# run user provided callback file
run_callback_from_file("#{release_path}/deploy/before_migrate.rb")
end
end
end
ruby_block "change HOME back to /root after source checkout" do
block do
ENV['HOME'] = "/root"
end
end
if deploy[:application_type] == 'rails' && node[:opsworks][:instance][:layers].include?('rails-app')
case node[:opsworks][:rails_stack][:name]
when 'apache_passenger'
passenger_web_app do
application application
deploy deploy
end
when 'nginx_unicorn'
unicorn_web_app do
application application
deploy deploy
end
else
raise "Unsupport Rails stack"
end
end
template "/etc/logrotate.d/opsworks_app_#{application}" do
backup false
source "logrotate.erb"
cookbook 'deploy'
owner "root"
group "root"
mode 0644
variables( :log_dirs => ["#{deploy[:deploy_to]}/shared/log" ] )
end
end