poise-application-ruby 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.kitchen.travis.yml +9 -0
  4. data/.kitchen.yml +10 -0
  5. data/.travis.yml +21 -0
  6. data/.yardopts +3 -0
  7. data/Berksfile +35 -0
  8. data/CHANGELOG.md +82 -0
  9. data/Gemfile +37 -0
  10. data/LICENSE +201 -0
  11. data/README.md +271 -0
  12. data/Rakefile +17 -0
  13. data/SUPPORTERS.md +81 -0
  14. data/chef/templates/database.yml.erb +3 -0
  15. data/chef/templates/secrets.yml.erb +3 -0
  16. data/lib/poise_application_ruby.rb +24 -0
  17. data/lib/poise_application_ruby/app_mixin.rb +92 -0
  18. data/lib/poise_application_ruby/cheftie.rb +17 -0
  19. data/lib/poise_application_ruby/error.rb +25 -0
  20. data/lib/poise_application_ruby/resources.rb +24 -0
  21. data/lib/poise_application_ruby/resources/bundle_install.rb +54 -0
  22. data/lib/poise_application_ruby/resources/rackup.rb +70 -0
  23. data/lib/poise_application_ruby/resources/rails.rb +260 -0
  24. data/lib/poise_application_ruby/resources/ruby.rb +57 -0
  25. data/lib/poise_application_ruby/resources/ruby_execute.rb +89 -0
  26. data/lib/poise_application_ruby/resources/ruby_gem.rb +46 -0
  27. data/lib/poise_application_ruby/resources/thin.rb +64 -0
  28. data/lib/poise_application_ruby/resources/unicorn.rb +87 -0
  29. data/lib/poise_application_ruby/service_mixin.rb +66 -0
  30. data/lib/poise_application_ruby/version.rb +19 -0
  31. data/poise-application-ruby.gemspec +45 -0
  32. data/test/cookbooks/application_ruby_test/attributes/default.rb +17 -0
  33. data/test/cookbooks/application_ruby_test/metadata.rb +20 -0
  34. data/test/cookbooks/application_ruby_test/recipes/default.rb +66 -0
  35. data/test/cookbooks/application_ruby_test/recipes/rails.rb +41 -0
  36. data/test/cookbooks/application_ruby_test/recipes/sinatra.rb +29 -0
  37. data/test/gemfiles/chef-12.gemfile +19 -0
  38. data/test/gemfiles/master.gemfile +27 -0
  39. data/test/integration/default/serverspec/default_spec.rb +44 -0
  40. data/test/integration/default/serverspec/rails_spec.rb +41 -0
  41. data/test/integration/default/serverspec/sinatra_spec.rb +40 -0
  42. data/test/spec/resources/ruby_execute_spec.rb +46 -0
  43. data/test/spec/spec_helper.rb +20 -0
  44. metadata +202 -0
@@ -0,0 +1,17 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'poise_boiler/rakefile'
@@ -0,0 +1,81 @@
1
+ # Supporters
2
+
3
+ This cookbook wouldn't have been possible without the generous support of my
4
+ Kickstarter backers. Thanks so much to every one of you!
5
+
6
+ * @kcunning
7
+ * Alberto Lorenzo Pulido
8
+ * Alex Gaynor
9
+ * Alexander Myasnikov
10
+ * Brooke Schreier Ganz
11
+ * Bryan McLellan
12
+ * Charles Johnson
13
+ * Chef Software, Inc.
14
+ * Chris Adams
15
+ * Christopher Petrilli
16
+ * David Thornton
17
+ * Derek Murawsky
18
+ * Fast Robot, LLC
19
+ * Fatty McAwesome Pants (Kate Heddleston)
20
+ * Greg Albrecht
21
+ * JD Harrington
22
+ * Jake Plimack
23
+ * Jason Cook
24
+ * Jeff Byrnes
25
+ * Jeff Forcier
26
+ * Jeff Lindsay
27
+ * Jennifer Davis
28
+ * John Fitch
29
+ * Jon Stacks
30
+ * Joshua SS Miller
31
+ * Julian Dunn
32
+ * Julien Phalip
33
+ * Kennon Kwok
34
+ * Kevin Duane
35
+ * Krzysztof Wilczynski
36
+ * Linda Goldstein
37
+ * Manny Toledo
38
+ * Marco A Morales
39
+ * Marcus Morris
40
+ * Mark Luntzel
41
+ * Martin B. Smith
42
+ * Mathieu Sauve-Frankel
43
+ * Matt Good
44
+ * Matt Juszczak
45
+ * Matt Ray
46
+ * Matt Stratton
47
+ * Michael Burns
48
+ * Miguel Landaeta
49
+ * Mike Schueler
50
+ * Nathan L Smith
51
+ * Nathen Harvey
52
+ * Paul Welch
53
+ * Peter Kropf
54
+ * Phil Mocek
55
+ * Practice Fusion Inc
56
+ * Ranjib
57
+ * Richard Jones
58
+ * Robert J. Berger
59
+ * Robin Levin
60
+ * Roland Moriz
61
+ * Ruben Orduz
62
+ * Russell Keith-Magee
63
+ * Ryan Hass
64
+ * Sam Clements
65
+ * Sean OMeara
66
+ * Seva Feldman
67
+ * Soo Choi
68
+ * Steven Danna
69
+ * Symonds & Son
70
+ * Todd Michael Bushnell
71
+ * Tracy Osborn
72
+ * Troy Ready
73
+ * Tyler Ball
74
+ * Vicky Tuite
75
+ * Ying Li
76
+ * Yvo van Doorn
77
+ * Zac Stevens
78
+ * lvh
79
+ * oolongtea
80
+ * smeuser
81
+ * tmonk42
@@ -0,0 +1,3 @@
1
+ # Generated by Chef for <%= @new_resource.to_s %>
2
+
3
+ <%= @config.to_yaml %>
@@ -0,0 +1,3 @@
1
+ # Generated by Chef for <%= @new_resource.to_s %>
2
+
3
+ <%= @config.to_yaml %>
@@ -0,0 +1,24 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+
18
+ module PoiseApplicationRuby
19
+ autoload :AppMixin, 'poise_application_ruby/app_mixin'
20
+ autoload :Error, 'poise_application_ruby/error'
21
+ autoload :Resources, 'poise_application_ruby/resources'
22
+ autoload :ServiceMixin, 'poise_application_ruby/service_mixin'
23
+ autoload :VERSION, 'poise_application_ruby/version'
24
+ end
@@ -0,0 +1,92 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'poise/backports'
18
+ require 'poise/utils'
19
+ require 'poise_application/app_mixin'
20
+ require 'poise_ruby/ruby_command_mixin'
21
+
22
+
23
+ module PoiseApplicationRuby
24
+ # A helper mixin for Ruby application resources and providers.
25
+ #
26
+ # @since 4.0.0
27
+ module AppMixin
28
+ include Poise::Utils::ResourceProviderMixin
29
+
30
+ # A helper mixin for Ruby application resources.
31
+ module Resource
32
+ include PoiseApplication::AppMixin::Resource
33
+ include PoiseRuby::RubyCommandMixin::Resource
34
+
35
+ # @!attribute parent_ruby
36
+ # Override the #parent_ruby from RubyCommandMixin to grok the
37
+ # application level parent as a default value.
38
+ # @return [PoiseRuby::Resources::RubyRuntime::Resource, nil]
39
+ parent_attribute(:ruby, type: :ruby_runtime, optional: true, default: lazy { app_state_ruby.equal?(self) ? nil : app_state_ruby })
40
+
41
+ # @!attribute parent_bundle
42
+ # Parent bundle install context.
43
+ # @return [PoiseRuby::Resources::BundleInstall::Resource, nil]
44
+ parent_attribute(:bundle, type: :ruby_runtime, optional: true, auto: false, default: lazy { app_state_bundle.equal?(self) ? nil : app_state_bundle })
45
+
46
+ # @attribute app_state_ruby
47
+ # The application-level Ruby parent.
48
+ # @return [PoiseRuby::Resources::RubyRuntime::Resource, nil]
49
+ def app_state_ruby(ruby=Poise::NOT_PASSED)
50
+ unless ruby == Poise::NOT_PASSED
51
+ app_state[:ruby] = ruby
52
+ end
53
+ app_state[:ruby]
54
+ end
55
+
56
+ # @attribute app_state_bundle
57
+ # The application-level Bundle parent.
58
+ # @return [PoiseRuby::Resources::BundleInstall::Resource, nil]
59
+ def app_state_bundle(bundle=Poise::NOT_PASSED)
60
+ unless bundle == Poise::NOT_PASSED
61
+ app_state[:bundle] = bundle
62
+ end
63
+ app_state[:bundle]
64
+ end
65
+
66
+ # A merged hash of environment variables for both the application state
67
+ # and parent ruby.
68
+ #
69
+ # @return [Hash<String, String>]
70
+ def app_state_environment_ruby
71
+ env = app_state_environment
72
+ env = env.merge(parent_ruby.ruby_environment) if parent_ruby
73
+ env['BUNDLE_GEMFILE'] = parent_bundle.gemfile_path if parent_bundle
74
+ env
75
+ end
76
+
77
+ # Update ruby_from_parent to transfer {#parent_bundle} too.
78
+ #
79
+ # @param resource [Chef::Resource] Resource to inherit from.
80
+ # @return [void]
81
+ def ruby_from_parent(resource)
82
+ super
83
+ parent_bundle(resource.parent_bundle) if resource.parent_bundle
84
+ end
85
+ end
86
+
87
+ # A helper mixin for Ruby application providers.
88
+ module Provider
89
+ include PoiseApplication::AppMixin::Provider
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,17 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'poise_application_ruby/resources'
@@ -0,0 +1,25 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'poise_application/error'
18
+
19
+ module PoiseApplicationRuby
20
+ # Base exception class for poise-application-ruby errors.
21
+ #
22
+ # @since 4.0.0
23
+ class Error < PoiseApplication::Error
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'poise_application_ruby/resources/bundle_install'
18
+ require 'poise_application_ruby/resources/rackup'
19
+ require 'poise_application_ruby/resources/rails'
20
+ require 'poise_application_ruby/resources/ruby'
21
+ require 'poise_application_ruby/resources/ruby_execute'
22
+ require 'poise_application_ruby/resources/ruby_gem'
23
+ require 'poise_application_ruby/resources/thin'
24
+ require 'poise_application_ruby/resources/unicorn'
@@ -0,0 +1,54 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'poise_ruby/resources/bundle_install'
18
+
19
+ require 'poise_application_ruby/app_mixin'
20
+
21
+
22
+ module PoiseApplicationRuby
23
+ module Resources
24
+ # (see BundleInstall::Resource)
25
+ # @since 4.0.0
26
+ module BundleInstall
27
+ # An `application_bundle_install` resource to install a
28
+ # [Bundler](http://bundler.io/) Gemfile in a web application.
29
+ #
30
+ # @note
31
+ # This resource is not idempotent itself, it will always run `bundle
32
+ # install`.
33
+ # @example
34
+ # application '/srv/my_app' do
35
+ # bundle_install
36
+ # end
37
+ class Resource < PoiseRuby::Resources::BundleInstall::Resource
38
+ include PoiseApplicationRuby::AppMixin
39
+ provides(:application_bundle_install)
40
+ subclass_providers!
41
+
42
+ # Set this resource as the app_state's parent bundle.
43
+ #
44
+ # @api private
45
+ def after_created
46
+ super.tap do |val|
47
+ app_state_bundle(self)
48
+ end
49
+ end
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,70 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'chef/provider'
18
+ require 'chef/resource'
19
+
20
+ require 'poise_application_ruby/service_mixin'
21
+
22
+
23
+ module PoiseApplicationRuby
24
+ module Resources
25
+ # (see Rackup::Resource)
26
+ # @since 4.0.0
27
+ module Rackup
28
+ class Resource < Chef::Resource
29
+ include PoiseApplicationRuby::ServiceMixin
30
+ provides(:application_rackup)
31
+
32
+ # @!attribute port
33
+ # TCP port to listen on. Defaults to 80.
34
+ # @return [String, Integer]
35
+ attribute(:port, kind_of: [String, Integer], default: 80)
36
+ end
37
+
38
+ class Provider < Chef::Provider
39
+ include PoiseApplicationRuby::ServiceMixin
40
+ provides(:application_rackup)
41
+
42
+ private
43
+
44
+ # Find the path to the config.ru. If the resource path was to a
45
+ # directory, apparent /config.ru.
46
+ #
47
+ # @return [String]
48
+ def configru_path
49
+ @configru_path ||= if ::File.directory?(new_resource.path)
50
+ ::File.join(new_resource.path, 'config.ru')
51
+ else
52
+ new_resource.path
53
+ end
54
+ end
55
+
56
+ # Set up service options for rackup.
57
+ #
58
+ # @param resource [PoiseService::Resource] Service resource.
59
+ # @return [void]
60
+ def service_options(resource)
61
+ super
62
+ resource.ruby_command("rackup --port #{new_resource.port}")
63
+ resource.directory(::File.dirname(configru_path))
64
+ # Older versions of rackup ignore all signals.
65
+ resource.stop_signal('KILL')
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,260 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'chef/provider'
18
+ require 'chef/resource'
19
+
20
+ require 'poise_application_ruby/app_mixin'
21
+
22
+
23
+ module PoiseApplicationRuby
24
+ module Resources
25
+ # (see Rails::Resource)
26
+ module Rails
27
+ # An `application_rails` resource to configure Ruby on Rails applications.
28
+ #
29
+ # @since 4.0.0
30
+ # @provides application_rails
31
+ # @action deploy
32
+ # @example
33
+ # application '/srv/myapp' do
34
+ # git '...'
35
+ # bundle_install
36
+ # rails do
37
+ # database do
38
+ # host node['rails_host']
39
+ # end
40
+ # end
41
+ # unicorn do
42
+ # port 8080
43
+ # end
44
+ # end
45
+ class Resource < Chef::Resource
46
+ include PoiseApplicationRuby::AppMixin
47
+ provides(:application_rails)
48
+ actions(:deploy)
49
+
50
+ # @!attribute database
51
+ # Option collector attribute for Rails database configuration.
52
+ # @return [Hash]
53
+ # @example Setting via block
54
+ # database do
55
+ # adapter 'postgresql'
56
+ # database 'blog'
57
+ # end
58
+ # @example Setting via URL
59
+ # database 'postgresql://localhost/blog'
60
+ attribute(:database, option_collector: true, parser: :parse_database_url)
61
+ # @!attribute database_config
62
+ # Template content attribute for the contents of database.yml.
63
+ # @todo Redo this doc to cover the actual attributes created.
64
+ # @return [Poise::Helpers::TemplateContent]
65
+ attribute(:database_config, template: true, default_source: 'database.yml.erb', default_options: lazy { default_database_options })
66
+ # @!attribute migrate
67
+ # Run database migrations. This is a bad idea for real apps. Please
68
+ # do not use it.
69
+ # @return [Boolean]
70
+ attribute(:migrate, equal_to: [true, false], default: false)
71
+ # @!attribute precompile_assets
72
+ # Set to true to run rake assets:precompile. By default will try to
73
+ # auto-detect if Sprockets is in use by looking at config/initializers.
74
+ # @see #default_precompile_assets
75
+ # @return [Boolean]
76
+ attribute(:precompile_assets, equal_to: [true, false], default: lazy { default_precompile_assets })
77
+ # @!attribute rails_env
78
+ # Rails environment name. Defaults to the Chef environment name or
79
+ # `production` if none is set.
80
+ # @see #default_rails_env
81
+ # @return [String]
82
+ attribute(:rails_env, kind_of: String, default: lazy { default_rails_env })
83
+ # @!attribute secret_token
84
+ # Secret token for Rails session verification and other purposes. On
85
+ # Rails 4.2 this will be used for secret_key_base. If not set, no
86
+ # secrets configuration is written.
87
+ # @return [String]
88
+ attribute(:secret_token, kind_of: [String, FalseClass])
89
+ # @!attribute secrets_config
90
+ # Template content attribute for the contents of secrets.yml. Only
91
+ # used when secrets_mode is :yaml.
92
+ # @todo Redo this doc to cover the actual attributes created.
93
+ # @return [Poise::Helpers::TemplateContent]
94
+ attribute(:secrets_config, template: true, default_source: 'secrets.yml.erb', default_options: lazy { default_secrets_options })
95
+ # @!attribute secrets_mode
96
+ # Secrets configuration mode. Set to `:yaml` to generate a Rails 4.2
97
+ # secrets.yml. Set to `:initializer` to update
98
+ # `config/initializers/secret_token.rb`. If unspecified this is
99
+ # auto-detected based on what files exist.
100
+ # @return [Symbol]
101
+ attribute(:secrets_mode, equal_to: [:yaml, :initializer], default: lazy { default_secrets_mode })
102
+
103
+ private
104
+
105
+ # Check if we should run the precompile by default. Looks for the
106
+ # assets initializer because that is not present with --skip-sprockets.
107
+ #
108
+ # @return [Boolean]
109
+ def default_precompile_assets
110
+ ::File.exists?(::File.join(path, 'config', 'initializers', 'assets.rb'))
111
+ end
112
+
113
+ # Check the default environment name.
114
+ #
115
+ # @return [String]
116
+ def default_rails_env
117
+ node.chef_environment == '_default' ? 'production' : node.chef_environment
118
+ end
119
+
120
+ # Format a single URL for the database.yml
121
+ #
122
+ # @return [Hash]
123
+ def parse_database_url(url)
124
+ {'url' => url}
125
+ end
126
+
127
+ # Default template variables for the database.yml.
128
+ #
129
+ # @return [Hash<Symbol, Object>]
130
+ def default_database_options
131
+ db_config = {'encoding' => 'utf8', 'reconnect' => true, 'pool' => 5}.merge(database)
132
+ {
133
+ config: {
134
+ rails_env => db_config
135
+ },
136
+ }
137
+ end
138
+
139
+ # Check which secrets configuration mode is in use based on files.
140
+ #
141
+ # @return [Symbol]
142
+ def default_secrets_mode
143
+ ::File.exists?(::File.join(path, 'config', 'initializers', 'secret_token.rb')) ? :initialize : :yaml
144
+ end
145
+
146
+ # Default template variables for the secrets.yml.
147
+ #
148
+ # @return [Hash<Symbol, Object>]
149
+ def default_secrets_options
150
+ {
151
+ config: {
152
+ rails_env => {
153
+ 'secret_key_base' => secret_token,
154
+ }
155
+ },
156
+ }
157
+ end
158
+ end
159
+
160
+ # Provider for `application_rails`.
161
+ #
162
+ # @since 4.0.0
163
+ # @see Resource
164
+ # @provides application_rails
165
+ class Provider < Chef::Provider
166
+ include PoiseApplicationRuby::AppMixin
167
+ provides(:application_rails)
168
+
169
+ # `deploy` action for `application_rails`. Ensure all configuration
170
+ # files are created and other deploy tasks resolved.
171
+ #
172
+ # @return [void]
173
+ def action_deploy
174
+ set_state
175
+ notifying_block do
176
+ write_database_yml unless new_resource.database.empty?
177
+ write_secrets_config if new_resource.secret_token
178
+ precompile_assets if new_resource.precompile_assets
179
+ run_migrations if new_resource.migrate
180
+ end
181
+ end
182
+
183
+ private
184
+
185
+ # Set app_state variables for future services et al.
186
+ def set_state
187
+ new_resource.app_state_environment[:RAILS_ENV] = new_resource.rails_env
188
+ new_resource.app_state_environment[:DATABASE_URL] = new_resource.database['url'] if new_resource.database['url']
189
+ end
190
+
191
+ # Create a database.yml config file.
192
+ def write_database_yml
193
+ file ::File.join(new_resource.path, 'config', 'database.yml') do
194
+ user new_resource.parent.owner
195
+ group new_resource.parent.group
196
+ mode '640'
197
+ content new_resource.database_config_content
198
+ end
199
+ end
200
+
201
+ # Dispatch to the correct config writer based on the mode.
202
+ def write_secrets_config
203
+ case new_resource.secrets_mode
204
+ when :yaml
205
+ write_secrets_yml
206
+ when :initializer
207
+ write_secrets_initializer
208
+ end
209
+ end
210
+
211
+ # Write a Rails 4.2-style secrets.yml.
212
+ def write_secrets_yml
213
+ file ::File.join(new_resource.path, 'config', 'secrets.yml') do
214
+ user new_resource.parent.owner
215
+ group new_resource.parent.group
216
+ mode '640'
217
+ content new_resource.secrets_config_content
218
+ end
219
+ end
220
+
221
+ # In-place update a config/initializers/secret_token.rb file.
222
+ def write_secrets_initializer
223
+ # @todo Implement initalizer-style secret support.
224
+ raise NotImplementedError.new('Sorry, intializer-style secrets loading is not yet supported.')
225
+ end
226
+
227
+ # Precompile assets using the rake task.
228
+ def precompile_assets
229
+ # Currently this will always run so the resource will always update :-/
230
+ # Better fix would be to shell_out! and parse the output?
231
+ ruby_execute 'rake assets:precompile' do
232
+ command %w{rake assets:precompile}
233
+ user new_resource.parent.owner
234
+ group new_resource.parent.group
235
+ cwd new_resource.parent.path
236
+ environment new_resource.app_state_environment
237
+ ruby_from_parent new_resource
238
+ parent_bundle new_resource.parent_bundle if new_resource.parent_bundle
239
+ end
240
+ end
241
+
242
+ # Run database migrations using the rake task.
243
+ def run_migrations
244
+ # Currently this will always run so the resource will always update :-/
245
+ # Better fix would be to shell_out! and parse the output?
246
+ ruby_execute 'rake db:migrate' do
247
+ command %w{rake db:migrate}
248
+ user new_resource.parent.owner
249
+ group new_resource.parent.group
250
+ cwd new_resource.parent.path
251
+ environment new_resource.app_state_environment
252
+ ruby_from_parent new_resource
253
+ parent_bundle new_resource.parent_bundle if new_resource.parent_bundle
254
+ end
255
+ end
256
+
257
+ end
258
+ end
259
+ end
260
+ end