Set up an instance of Mastodon for Kosmos

Refs #19

Use new application cookbook, update our cookbooks
This commit is contained in:
Greg Karékinian
2017-04-06 21:20:51 +02:00
parent a3f5c5f646
commit de11c0d691
345 changed files with 22591 additions and 3473 deletions

View File

@@ -0,0 +1,24 @@
#
# Copyright 2015, 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.
#
module PoiseApplicationRuby
autoload :AppMixin, 'poise_application_ruby/app_mixin'
autoload :Error, 'poise_application_ruby/error'
autoload :Resources, 'poise_application_ruby/resources'
autoload :ServiceMixin, 'poise_application_ruby/service_mixin'
autoload :VERSION, 'poise_application_ruby/version'
end

View File

@@ -0,0 +1,92 @@
#
# Copyright 2015, 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 'poise/backports'
require 'poise/utils'
require 'poise_application/app_mixin'
require 'poise_ruby/ruby_command_mixin'
module PoiseApplicationRuby
# A helper mixin for Ruby application resources and providers.
#
# @since 4.0.0
module AppMixin
include Poise::Utils::ResourceProviderMixin
# A helper mixin for Ruby application resources.
module Resource
include PoiseApplication::AppMixin::Resource
include PoiseRuby::RubyCommandMixin::Resource
# @!attribute parent_ruby
# Override the #parent_ruby from RubyCommandMixin to grok the
# application level parent as a default value.
# @return [PoiseRuby::Resources::RubyRuntime::Resource, nil]
parent_attribute(:ruby, type: :ruby_runtime, optional: true, default: lazy { app_state_ruby.equal?(self) ? nil : app_state_ruby })
# @!attribute parent_bundle
# Parent bundle install context.
# @return [PoiseRuby::Resources::BundleInstall::Resource, nil]
parent_attribute(:bundle, type: :ruby_runtime, optional: true, auto: false, default: lazy { app_state_bundle.equal?(self) ? nil : app_state_bundle })
# @attribute app_state_ruby
# The application-level Ruby parent.
# @return [PoiseRuby::Resources::RubyRuntime::Resource, nil]
def app_state_ruby(ruby=Poise::NOT_PASSED)
unless ruby == Poise::NOT_PASSED
app_state[:ruby] = ruby
end
app_state[:ruby]
end
# @attribute app_state_bundle
# The application-level Bundle parent.
# @return [PoiseRuby::Resources::BundleInstall::Resource, nil]
def app_state_bundle(bundle=Poise::NOT_PASSED)
unless bundle == Poise::NOT_PASSED
app_state[:bundle] = bundle
end
app_state[:bundle]
end
# A merged hash of environment variables for both the application state
# and parent ruby.
#
# @return [Hash<String, String>]
def app_state_environment_ruby
env = app_state_environment
env = env.merge(parent_ruby.ruby_environment) if parent_ruby
env['BUNDLE_GEMFILE'] = parent_bundle.gemfile_path if parent_bundle
env
end
# Update ruby_from_parent to transfer {#parent_bundle} too.
#
# @param resource [Chef::Resource] Resource to inherit from.
# @return [void]
def ruby_from_parent(resource)
super
parent_bundle(resource.parent_bundle) if resource.parent_bundle
end
end
# A helper mixin for Ruby application providers.
module Provider
include PoiseApplication::AppMixin::Provider
end
end
end

View File

@@ -0,0 +1,17 @@
#
# Copyright 2015, 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 'poise_application_ruby/resources'

View File

@@ -0,0 +1,25 @@
#
# Copyright 2015, 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 'poise_application/error'
module PoiseApplicationRuby
# Base exception class for poise-application-ruby errors.
#
# @since 4.0.0
class Error < PoiseApplication::Error
end
end

View File

@@ -0,0 +1,24 @@
#
# Copyright 2015, 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 'poise_application_ruby/resources/bundle_install'
require 'poise_application_ruby/resources/rackup'
require 'poise_application_ruby/resources/rails'
require 'poise_application_ruby/resources/ruby'
require 'poise_application_ruby/resources/ruby_execute'
require 'poise_application_ruby/resources/ruby_gem'
require 'poise_application_ruby/resources/thin'
require 'poise_application_ruby/resources/unicorn'

View File

@@ -0,0 +1,54 @@
#
# Copyright 2015, 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 'poise_ruby/resources/bundle_install'
require 'poise_application_ruby/app_mixin'
module PoiseApplicationRuby
module Resources
# (see BundleInstall::Resource)
# @since 4.0.0
module BundleInstall
# An `application_bundle_install` resource to install a
# [Bundler](http://bundler.io/) Gemfile in a web application.
#
# @note
# This resource is not idempotent itself, it will always run `bundle
# install`.
# @example
# application '/srv/my_app' do
# bundle_install
# end
class Resource < PoiseRuby::Resources::BundleInstall::Resource
include PoiseApplicationRuby::AppMixin
provides(:application_bundle_install)
subclass_providers!
# Set this resource as the app_state's parent bundle.
#
# @api private
def after_created
super.tap do |val|
app_state_bundle(self)
end
end
end
end
end
end

View File

@@ -0,0 +1,70 @@
#
# Copyright 2015, 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 Rackup::Resource)
# @since 4.0.0
module Rackup
class Resource < Chef::Resource
include PoiseApplicationRuby::ServiceMixin
provides(:application_rackup)
# @!attribute port
# TCP port to listen on. Defaults to 80.
# @return [String, Integer]
attribute(:port, kind_of: [String, Integer], default: 80)
end
class Provider < Chef::Provider
include PoiseApplicationRuby::ServiceMixin
provides(:application_rackup)
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 up service options for rackup.
#
# @param resource [PoiseService::Resource] Service resource.
# @return [void]
def service_options(resource)
super
resource.ruby_command("rackup --port #{new_resource.port}")
resource.directory(::File.dirname(configru_path))
# Older versions of rackup ignore all signals.
resource.stop_signal('KILL')
end
end
end
end
end

View File

@@ -0,0 +1,260 @@
#
# Copyright 2015, 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/app_mixin'
module PoiseApplicationRuby
module Resources
# (see Rails::Resource)
module Rails
# An `application_rails` resource to configure Ruby on Rails applications.
#
# @since 4.0.0
# @provides application_rails
# @action deploy
# @example
# application '/srv/myapp' do
# git '...'
# bundle_install
# rails do
# database do
# host node['rails_host']
# end
# end
# unicorn do
# port 8080
# end
# end
class Resource < Chef::Resource
include PoiseApplicationRuby::AppMixin
provides(:application_rails)
actions(:deploy)
# @!attribute database
# Option collector attribute for Rails database configuration.
# @return [Hash]
# @example Setting via block
# database do
# adapter 'postgresql'
# database 'blog'
# end
# @example Setting via URL
# database 'postgresql://localhost/blog'
attribute(:database, option_collector: true, parser: :parse_database_url)
# @!attribute database_config
# Template content attribute for the contents of database.yml.
# @todo Redo this doc to cover the actual attributes created.
# @return [Poise::Helpers::TemplateContent]
attribute(:database_config, template: true, default_source: 'database.yml.erb', default_options: lazy { default_database_options })
# @!attribute migrate
# Run database migrations. This is a bad idea for real apps. Please
# do not use it.
# @return [Boolean]
attribute(:migrate, equal_to: [true, false], default: false)
# @!attribute precompile_assets
# Set to true to run rake assets:precompile. By default will try to
# auto-detect if Sprockets is in use by looking at config/initializers.
# @see #default_precompile_assets
# @return [Boolean]
attribute(:precompile_assets, equal_to: [true, false], default: lazy { default_precompile_assets })
# @!attribute rails_env
# Rails environment name. Defaults to the Chef environment name or
# `production` if none is set.
# @see #default_rails_env
# @return [String]
attribute(:rails_env, kind_of: String, default: lazy { default_rails_env })
# @!attribute secret_token
# 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])
# @!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_mode
# Secrets configuration mode. Set to `:yaml` to generate a Rails 4.2
# secrets.yml. Set to `:initializer` to update
# `config/initializers/secret_token.rb`. If unspecified this is
# auto-detected based on what files exist.
# @return [Symbol]
attribute(:secrets_mode, equal_to: [:yaml, :initializer], default: lazy { default_secrets_mode })
private
# Check if we should run the precompile by default. Looks for the
# assets initializer because that is not present with --skip-sprockets.
#
# @return [Boolean]
def default_precompile_assets
::File.exists?(::File.join(path, 'config', 'initializers', 'assets.rb'))
end
# Check the default environment name.
#
# @return [String]
def default_rails_env
node.chef_environment == '_default' ? 'production' : node.chef_environment
end
# Format a single URL for the database.yml
#
# @return [Hash]
def parse_database_url(url)
{'url' => url}
end
# Default template variables for the database.yml.
#
# @return [Hash<Symbol, Object>]
def default_database_options
db_config = {'encoding' => 'utf8', 'reconnect' => true, 'pool' => 5}.merge(database)
{
config: {
rails_env => db_config
},
}
end
# Check which secrets configuration mode is in use based on files.
#
# @return [Symbol]
def default_secrets_mode
::File.exists?(::File.join(path, 'config', 'initializers', 'secret_token.rb')) ? :initialize : :yaml
end
# Default template variables for the secrets.yml.
#
# @return [Hash<Symbol, Object>]
def default_secrets_options
{
config: {
rails_env => {
'secret_key_base' => secret_token,
}
},
}
end
end
# Provider for `application_rails`.
#
# @since 4.0.0
# @see Resource
# @provides application_rails
class Provider < Chef::Provider
include PoiseApplicationRuby::AppMixin
provides(:application_rails)
# `deploy` action for `application_rails`. Ensure all configuration
# files are created and other deploy tasks resolved.
#
# @return [void]
def action_deploy
set_state
notifying_block do
write_database_yml unless new_resource.database.empty?
write_secrets_config if new_resource.secret_token
precompile_assets if new_resource.precompile_assets
run_migrations if new_resource.migrate
end
end
private
# Set app_state variables for future services et al.
def set_state
new_resource.app_state_environment[:RAILS_ENV] = new_resource.rails_env
new_resource.app_state_environment[:DATABASE_URL] = new_resource.database['url'] if new_resource.database['url']
end
# Create a database.yml config file.
def write_database_yml
file ::File.join(new_resource.path, 'config', 'database.yml') do
user new_resource.parent.owner
group new_resource.parent.group
mode '640'
content new_resource.database_config_content
end
end
# Dispatch to the correct config writer based on the mode.
def write_secrets_config
case new_resource.secrets_mode
when :yaml
write_secrets_yml
when :initializer
write_secrets_initializer
end
end
# Write a Rails 4.2-style secrets.yml.
def write_secrets_yml
file ::File.join(new_resource.path, 'config', 'secrets.yml') do
user new_resource.parent.owner
group new_resource.parent.group
mode '640'
content new_resource.secrets_config_content
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.')
end
# Precompile assets using the rake task.
def precompile_assets
# Currently this will always run so the resource will always update :-/
# Better fix would be to shell_out! and parse the output?
ruby_execute 'rake assets:precompile' do
command %w{rake assets:precompile}
user new_resource.parent.owner
group new_resource.parent.group
cwd new_resource.parent.path
environment new_resource.app_state_environment
ruby_from_parent new_resource
parent_bundle new_resource.parent_bundle if new_resource.parent_bundle
end
end
# Run database migrations using the rake task.
def run_migrations
# Currently this will always run so the resource will always update :-/
# Better fix would be to shell_out! and parse the output?
ruby_execute 'rake db:migrate' do
command %w{rake db:migrate}
user new_resource.parent.owner
group new_resource.parent.group
cwd new_resource.parent.path
environment new_resource.app_state_environment
ruby_from_parent new_resource
parent_bundle new_resource.parent_bundle if new_resource.parent_bundle
end
end
end
end
end
end

View File

@@ -0,0 +1,63 @@
#
# Copyright 2015, 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 'poise_ruby/resources/ruby_runtime'
require 'poise_application_ruby/app_mixin'
module PoiseApplicationRuby
module Resources
# (see Ruby::Resource)
# @since 4.0.0
module Ruby
# An `application_ruby` resource to manage Ruby runtimes
# inside an Application cookbook deployment.
#
# @provides application_ruby
# @provides application_ruby_runtime
# @action install
# @action uninstall
# @example
# application '/app' do
# ruby '2'
# end
class Resource < PoiseRuby::Resources::RubyRuntime::Resource
include PoiseApplicationRuby::AppMixin
provides(:application_ruby)
provides(:application_ruby_runtime)
container_default(false)
subclass_providers!
# Rebind the parent class #gem_binary instead of the one from
# RubyCommandMixin (by way of AppMixin)
def gem_binary(*args, &block)
self.class.superclass.instance_method(:gem_binary).bind(self).call(*args, &block)
end
# Set this resource as the app_state's parent ruby.
#
# @api private
def after_created
super.tap do |val|
app_state_ruby(self)
end
end
end
end
end
end

View File

@@ -0,0 +1,89 @@
#
# Copyright 2015, 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 'poise_ruby/resources/ruby_execute'
require 'poise_application_ruby/app_mixin'
module PoiseApplicationRuby
module Resources
# (see RubyExecute::Resource)
# @since 4.0.0
module RubyExecute
# An `application_ruby_execute` resource to run Ruby commands inside an
# Application cookbook deployment.
#
# @provides application_ruby_execute
# @action run
# @example
# application '/srv/myapp' do
# ruby_execute 'rake build'
# end
class Resource < PoiseRuby::Resources::RubyExecute::Resource
include PoiseApplicationRuby::AppMixin
provides(:application_ruby_execute)
def initialize(*args)
super
# Clear some instance variables so my defaults work.
remove_instance_variable(:@cwd)
remove_instance_variable(:@group)
remove_instance_variable(:@user)
end
# #!attribute cwd
# Override the default directory to be the app path if unspecified.
# @return [String]
attribute(:cwd, kind_of: [String, NilClass, FalseClass], default: lazy { parent && parent.path })
# #!attribute group
# Override the default group to be the app group if unspecified.
# @return [String, Integer]
attribute(:group, kind_of: [String, Integer, NilClass, FalseClass], default: lazy { parent && parent.group })
# #!attribute user
# Override the default user to be the app owner if unspecified.
# @return [String, Integer]
attribute(:user, kind_of: [String, Integer, NilClass, FalseClass], default: lazy { parent && parent.owner })
end
# The default provider for `application_ruby_execute`.
#
# @see Resource
# @provides application_ruby_execute
class Provider < PoiseRuby::Resources::RubyExecute::Provider
provides(:application_ruby_execute)
private
# Override environment to add the application envivonrment instead.
#
# @return [Hash]
def environment
super.tap do |environment|
# Don't use the app_state_environment_ruby because we already have
# those values in place.
environment.update(new_resource.app_state_environment)
# Re-apply the resource environment for correct ordering.
environment.update(new_resource.environment) if new_resource.environment
end
end
end
end
end
end

View File

@@ -0,0 +1,46 @@
#
# Copyright 2015, 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 'poise_ruby/resources/ruby_gem'
require 'poise_application_ruby/app_mixin'
module PoiseApplicationRuby
module Resources
# (see RubyGem::Resource)
# @since 4.0.0
module RubyGem
# An `application_ruby_gem` resource to install Ruby gems inside an
# Application cookbook deployment.
#
# @provides application_ruby_gem
# @action install
# @action upgrade
# @action remove
# @example
# application '/srv/myapp' do
# ruby_gem 'rack'
# end
class Resource < PoiseRuby::Resources::RubyGem::Resource
include PoiseApplicationRuby::AppMixin
provides(:application_ruby_gem)
subclass_providers!
end
end
end
end

View File

@@ -0,0 +1,64 @@
#
# Copyright 2015, 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 Thin::Resource)
# @since 4.0.0
module Thin
class Resource < Chef::Resource
include PoiseApplicationRuby::ServiceMixin
provides(:application_thin)
attribute(:port, kind_of: [String, Integer], default: 80)
attribute(:config_path, kind_of: String)
end
class Provider < Chef::Provider
include PoiseApplicationRuby::ServiceMixin
provides(:application_thin)
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
# (see PoiseApplication::ServiceMixin#service_options)
def service_options(resource)
super
cmd = "thin --rackup #{configru_path} --port #{new_resource.port}"
cmd << " --config #{::File.expand_path(new_resource.config_path, new_resource.path)}" if new_resource.config_path
resource.ruby_command(cmd)
end
end
end
end
end

View File

@@ -0,0 +1,87 @@
#
# Copyright 2015, 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 Unicorn::Resource)
# @since 4.0.0
module Unicorn
# An `application_unicorn` resource to manage a unicorn web application
# server.
#
# @since 4.0.0
# @provides application_unicorn
# @action enable
# @action disable
# @action start
# @action stop
# @action restart
# @action reload
# @example
# application '/srv/myapp' do
# git '...'
# bundle_install
# unicorn do
# port 8080
# end
# end
class Resource < Chef::Resource
include PoiseApplicationRuby::ServiceMixin
provides(:application_unicorn)
# @!attribute port
# Port to bind to.
attribute(:port, kind_of: [String, Integer], default: 80)
end
# Provider for `application_unicorn`.
#
# @since 4.0.0
# @see Resource
# @provides application_unicorn
class Provider < Chef::Provider
include PoiseApplicationRuby::ServiceMixin
provides(:application_unicorn)
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("unicorn --port #{new_resource.port} #{configru_path}")
end
end
end
end
end

View File

@@ -0,0 +1,66 @@
#
# Copyright 2015, 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 'poise/utils'
require 'poise_application/service_mixin'
require 'poise_languages/utils'
require 'poise_ruby/bundler_mixin'
require 'poise_application_ruby/app_mixin'
module PoiseApplicationRuby
# A helper mixin for Ruby service resources and providers.
#
# @since 4.0.0
module ServiceMixin
include Poise::Utils::ResourceProviderMixin
# A helper mixin for Ruby service resources.
module Resource
include PoiseApplication::ServiceMixin::Resource
include PoiseApplicationRuby::AppMixin::Resource
end
# A helper mixin for Ruby service providers.
module Provider
include PoiseApplication::ServiceMixin::Provider
include PoiseApplicationRuby::AppMixin::Provider
include PoiseRuby::BundlerMixin
# Set up the service for running Ruby stuff.
def service_options(resource)
super
# Closure scoping for #ruby_command below.
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']
cmd = if self_.new_resource.parent_bundle
bundle_exec_command(val, path: path)
else
"#{self_.new_resource.ruby} #{PoiseLanguages::Utils.absolute_command(val, path: path)}"
end
resource.command(cmd)
end
# Include env vars as needed.
resource.environment.update(new_resource.parent_ruby.ruby_environment) if new_resource.parent_ruby
resource.environment['BUNDLE_GEMFILE'] = new_resource.parent_bundle.gemfile_path if new_resource.parent_bundle
end
end
end
end

View File

@@ -0,0 +1,19 @@
#
# Copyright 2015, 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.
#
module PoiseApplicationRuby
VERSION = '4.0.1'
end