Update application_ruby cookbook

This commit is contained in:
Greg 2017-06-02 18:30:48 +02:00
parent 1ce7c21c86
commit b7e395f122
24 changed files with 205 additions and 39 deletions

2
Batali
View File

@ -17,7 +17,7 @@ Batali.define do
cookbook 'poise-ruby-build', '~> 1.1.0'
cookbook 'application', '~> 5.2.0'
cookbook 'application_javascript'
cookbook 'application_ruby'
cookbook 'application_ruby', '~> 4.1.0'
cookbook 'application_git', '~> 1.1.0' # 1.2.0 doesn't work with knife-solo
cookbook 'users', '~> 5.0.0'
cookbook 'sudo', '~> 3.4.0'

View File

@ -813,10 +813,6 @@
{
"name": "application_ruby",
"dependencies": [
[
"poise-service",
"~> 1.0"
],
[
"poise",
"~> 2.0"
@ -828,13 +824,17 @@
[
"poise-ruby",
"~> 2.1"
],
[
"poise-service",
"~> 1.0"
]
],
"version": "4.0.1",
"version": "4.1.0",
"source": {
"type": "Batali::Source::Site",
"url": "https://supermarket.chef.io:443/api/v1/cookbooks/application_ruby/versions/4.0.1/download",
"version": "4.0.1"
"url": "https://supermarket.chef.io:443/api/v1/cookbooks/application_ruby/versions/4.1.0/download",
"version": "4.1.0"
}
},
{

View File

@ -1,5 +1,11 @@
# Application_Ruby Changelog
## v4.1.0
* Add an `application_puma` resource to run a Puma application server.
* Add support for `:initializer` style secrets for the `application_rails` resource.
* Chef 13 support.
## v4.0.1
* Correct `gem_binary` results for `application_ruby`.

View File

@ -33,7 +33,7 @@ end
## Requirements
Chef 12 or newer is required.
Chef 12.1 or newer is required.
## Resources
@ -53,6 +53,34 @@ end
All actions and properties are the same as the [`bundle_install` resource](https://github.com/poise/poise-ruby#bundle_install).
### `application_puma`
The `application_puma` resource creates a service for `puma`.
```ruby
application '/srv/myapp' do
puma do
port 8000
end
end
```
#### Actions
* `:enable` Create, enable and start the service. *(default)*
* `:disable` Stop, disable, and destroy the service.
* `:start` Start the service.
* `:stop` Stop the service.
* `:restart` Stop and then start the service.
* `:reload` Send the configured reload signal to the service.
#### Properties
* `path` Base path for the application. *(name attribute)*
* `port` Port to listen on. *(default: 80)*
* `service_name` Name of the service to create. *(default: auto-detect)*
* `user` User to run the service as. *(default: application owner)*
### `application_rackup`
The `application_rackup` resource creates a service for `rackup`.
@ -102,6 +130,8 @@ end
#### Properties
* `path` Base path for the application. *(name attribute)*
* `app_module` Top-level application module. Only needed for the :initializer
style of secret token configuration. *(default: auto-detect)*
* `database` Database settings for Rails. See [the database section
below](#database-parameters) for more information. *(option collector)*
* `migrate` Run database migrations. *(default: false)*
@ -218,7 +248,7 @@ end
* `config_path` Path to a Thin configuration file.
* `port` Port to listen on. *(default: 80)*
* `service_name` Name of the service to create. *(default: auto-detect)*
# `user` User to run the service as. *(default: application owner)*
* `user` User to run the service as. *(default: application owner)*
### `application_unicorn`
@ -246,7 +276,7 @@ end
* `path` Base path for the application. *(name attribute)*
* `port` Port to listen on. *(default: 80)*
* `service_name` Name of the service to create. *(default: auto-detect)*
# `user` User to run the service as. *(default: application owner)*
* `user` User to run the service as. *(default: application owner)*
## Sponsors
@ -256,7 +286,7 @@ The Poise test server infrastructure is sponsored by [Rackspace](https://rackspa
## License
Copyright 2015, Noah Kantrowitz
Copyright 2015-2017, Noah Kantrowitz
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -15,6 +15,7 @@
#
require 'poise_application_ruby/resources/bundle_install'
require 'poise_application_ruby/resources/puma'
require 'poise_application_ruby/resources/rackup'
require 'poise_application_ruby/resources/rails'
require 'poise_application_ruby/resources/ruby'

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -0,0 +1,86 @@
#
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'chef/provider'
require 'chef/resource'
require 'poise_application_ruby/service_mixin'
module PoiseApplicationRuby
module Resources
# (see Puma::Resource)
# @since 4.1.0
module Puma
# An `application_puma` resource to manage a puma web application
# server.
#
# @since 4.1.0
# @provides application_puma
# @action enable
# @action disable
# @action start
# @action stop
# @action restart
# @action reload
# @example
# application '/srv/myapp' do
# git '...'
# bundle_install
# puma do
# port 8080
# end
# end
class Resource < Chef::Resource
include PoiseApplicationRuby::ServiceMixin
provides(:application_puma)
# @!attribute port
# Port to bind to.
attribute(:port, kind_of: [String, Integer], default: 80)
end
# Provider for `application_puma`.
#
# @since 4.1.0
# @see Resource
# @provides application_puma
class Provider < Chef::Provider
include PoiseApplicationRuby::ServiceMixin
provides(:application_puma)
private
# Find the path to the config.ru. If the resource path was to a
# directory, apparent /config.ru.
#
# @return [String]
def configru_path
@configru_path ||= if ::File.directory?(new_resource.path)
::File.join(new_resource.path, 'config.ru')
else
new_resource.path
end
end
# Set service resource options.
def service_options(resource)
super
resource.ruby_command("puma --port #{new_resource.port} #{configru_path}")
end
end
end
end
end

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -18,6 +18,7 @@ require 'chef/provider'
require 'chef/resource'
require 'poise_application_ruby/app_mixin'
require 'poise_application_ruby/error'
module PoiseApplicationRuby
@ -47,6 +48,11 @@ module PoiseApplicationRuby
provides(:application_rails)
actions(:deploy)
# @!attribute app_module
# Top-level application module. Only needed for the :initializer style
# of secret token configuration, and generally auto-detected.
# @return [String, false, nil]
attribute(:app_module, kind_of: [String, FalseClass, NilClass], default: lazy { default_app_module })
# @!attribute database
# Option collector attribute for Rails database configuration.
# @return [Hash]
@ -84,14 +90,20 @@ module PoiseApplicationRuby
# Secret token for Rails session verification and other purposes. On
# Rails 4.2 this will be used for secret_key_base. If not set, no
# secrets configuration is written.
# @return [String]
attribute(:secret_token, kind_of: [String, FalseClass])
# @return [String, false, nil]
attribute(:secret_token, kind_of: [String, FalseClass, NilClass])
# @!attribute secrets_config
# Template content attribute for the contents of secrets.yml. Only
# used when secrets_mode is :yaml.
# @todo Redo this doc to cover the actual attributes created.
# @return [Poise::Helpers::TemplateContent]
attribute(:secrets_config, template: true, default_source: 'secrets.yml.erb', default_options: lazy { default_secrets_options })
# @!attribute secrets_initializer
# Template content attribute for the contents of secret_token.rb. Only
# used when secrets_mode is :initializer.
# @todo Redo this doc to cover the actual attributes created.
# @return [Poise::Helpers::TemplateContent]
attribute(:secrets_initializer, template: true, default_source: 'secret_token.rb.erb', default_options: lazy { default_secrets_options })
# @!attribute secrets_mode
# Secrets configuration mode. Set to `:yaml` to generate a Rails 4.2
# secrets.yml. Set to `:initializer` to update
@ -140,21 +152,33 @@ module PoiseApplicationRuby
#
# @return [Symbol]
def default_secrets_mode
::File.exists?(::File.join(path, 'config', 'initializers', 'secret_token.rb')) ? :initialize : :yaml
::File.exists?(::File.join(path, 'config', 'initializers', 'secret_token.rb')) ? :initializer : :yaml
end
# Default template variables for the secrets.yml.
# Default template variables for the secrets.yml and secret_token.rb.
#
# @return [Hash<Symbol, Object>]
def default_secrets_options
{
config: {
yaml_config: {
rails_env => {
'secret_key_base' => secret_token,
}
},
secret_token: secret_token,
app_module: if secrets_mode == :initializer
raise Error.new("Unable to extract app module for #{self}, please set app_module property") if !app_module || app_module.empty?
app_module
end
}
end
# Default application module name.
#
# @return [String]
def default_app_module
IO.read(::File.join(path, 'config', 'initializers', 'secret_token.rb'))[/(\w+)::Application\.config\.secret_token/, 1]
end
end
# Provider for `application_rails`.
@ -205,6 +229,8 @@ module PoiseApplicationRuby
write_secrets_yml
when :initializer
write_secrets_initializer
else
raise Error.new("Unknown secrets mode #{new_resource.secrets_mode.inspect}")
end
end
@ -215,13 +241,19 @@ module PoiseApplicationRuby
group new_resource.parent.group
mode '640'
content new_resource.secrets_config_content
sensitive true
end
end
# In-place update a config/initializers/secret_token.rb file.
def write_secrets_initializer
# @todo Implement initalizer-style secret support.
raise NotImplementedError.new('Sorry, intializer-style secrets loading is not yet supported.')
file ::File.join(new_resource.path, 'config', 'initializers', 'secret_token.rb') do
user new_resource.parent.owner
group new_resource.parent.group
mode '640'
content new_resource.secrets_initializer_content
sensitive true
end
end
# Precompile assets using the rake task.

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -39,6 +39,7 @@ module PoiseApplicationRuby
module Provider
include PoiseApplication::ServiceMixin::Provider
include PoiseApplicationRuby::AppMixin::Provider
include PoiseRuby::RubyCommandMixin::Provider
include PoiseRuby::BundlerMixin
# Set up the service for running Ruby stuff.
@ -48,10 +49,17 @@ module PoiseApplicationRuby
self_ = self
# Create a new singleton method that fills in Python for you.
resource.define_singleton_method(:ruby_command) do |val|
path = self_.new_resource.app_state_environment_ruby['PATH']
path = self_.new_resource.app_state_environment_ruby['PATH'] || ENV['PATH']
cmd = if self_.new_resource.parent_bundle
bundle_exec_command(val, path: path)
else
# Insert the gem executable directory at the front of the path.
gem_environment = self_.send(:ruby_shell_out!, self_.new_resource.gem_binary, 'environment')
matches = gem_environment.stdout.scan(/EXECUTABLE DIRECTORY: (.*)$/).first
if matches
Chef::Log.debug("[#{new_resource}] Prepending gem executable directory #{matches.first} to existing $PATH (#{path})")
path = "#{matches.first}:#{path}"
end
"#{self_.new_resource.ruby} #{PoiseLanguages::Utils.absolute_command(val, path: path)}"
end
resource.command(cmd)

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -15,5 +15,5 @@
#
module PoiseApplicationRuby
VERSION = '4.0.1'
VERSION = '4.1.0'
end

View File

@ -1,5 +1,5 @@
#
# Copyright 2015, Noah Kantrowitz
# Copyright 2015-2017, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,3 @@
# Generated by Chef for <%= @new_resource.to_s %>
<%= @app_module %>::Application.config.secret_token = <%= @secret_token.inspect %>

View File

@ -1,3 +1,3 @@
# Generated by Chef for <%= @new_resource.to_s %>
<%= @config.to_yaml %>
<%= @yaml_config.to_yaml %>