Vendor the external cookbooks
Knife-Zero doesn't include Berkshelf support, so vendoring everything in the repo is convenient again
This commit is contained in:
25
cookbooks/application/files/halite_gem/poise_application.rb
Normal file
25
cookbooks/application/files/halite_gem/poise_application.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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 PoiseApplication
|
||||
autoload :AppMixin, 'poise_application/app_mixin'
|
||||
autoload :Error, 'poise_application/error'
|
||||
autoload :Resources, 'poise_application/resources'
|
||||
autoload :ServiceMixin, 'poise_application/service_mixin'
|
||||
autoload :Utils, 'poise_application/utils'
|
||||
autoload :VERSION, 'poise_application/version'
|
||||
end
|
||||
@@ -0,0 +1,64 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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/app_mixin'
|
||||
|
||||
|
||||
module PoiseApplication
|
||||
# A helper mixin for `file`-like resources to make them take application
|
||||
# resource data. Relative paths are expanded against the application path and
|
||||
# the app owner/group are the default user/group for the resource.
|
||||
#
|
||||
# @api private
|
||||
# @since 5.1.0
|
||||
module AppFileMixin
|
||||
include Poise::Utils::ResourceProviderMixin
|
||||
|
||||
module Resource
|
||||
include PoiseApplication::AppMixin
|
||||
|
||||
def initialize(*)
|
||||
super
|
||||
# So our lazy default below can work. Not needed on 12.7+.
|
||||
remove_instance_variable(:@path) if instance_variable_defined?(:@path)
|
||||
end
|
||||
|
||||
# @!attribute path
|
||||
# Override the default path to be relative to the app path.
|
||||
# @return [String]
|
||||
attribute(:path, kind_of: String, default: lazy { parent ? ::File.expand_path(name, parent.path) : name })
|
||||
|
||||
# @!attribute group
|
||||
# Override the default group to be the app group if unspecified.
|
||||
# @return [String, Integer]
|
||||
attribute(:group, kind_of: [String, Integer, NilClass], default: lazy { parent && parent.group })
|
||||
|
||||
# @!attribute owner
|
||||
# Override the default user to be the app owner if unspecified.
|
||||
# @return [String, Integer]
|
||||
attribute(:owner, kind_of: [String, Integer, NilClass], default: lazy { parent && parent.owner })
|
||||
|
||||
# For the forgetful.
|
||||
alias_method :user, :owner
|
||||
end
|
||||
|
||||
module Provider
|
||||
include PoiseApplication::AppMixin
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,69 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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/mash'
|
||||
require 'poise/provider'
|
||||
require 'poise/resource'
|
||||
require 'poise/utils'
|
||||
|
||||
|
||||
module PoiseApplication
|
||||
# A helper mixin for application resources and providers. These are things
|
||||
# intended to be used as subresources of the `application` resource.
|
||||
#
|
||||
# @since 5.0.0
|
||||
module AppMixin
|
||||
include Poise::Utils::ResourceProviderMixin
|
||||
|
||||
# A helper mixin for application resources.
|
||||
module Resource
|
||||
include Poise::Resource
|
||||
|
||||
# Set the parent type and optional flag.
|
||||
poise_subresource(:application, true)
|
||||
|
||||
# @!attribute path
|
||||
# Base path for the application.
|
||||
# @return [String]
|
||||
attribute(:path, kind_of: String, name_attribute: true)
|
||||
|
||||
# A delegator for accessing the application state. If no application
|
||||
# parent is found, the state will be tracked internally within the
|
||||
# resource.
|
||||
#
|
||||
# @return [Hash<Symbol, Object>]
|
||||
def app_state
|
||||
if parent
|
||||
parent.app_state
|
||||
else
|
||||
# If there isn't a parent, just track within the resource.
|
||||
@local_app_state ||= Mash.new
|
||||
end
|
||||
end
|
||||
|
||||
# Environment variables stored in the application state.
|
||||
#
|
||||
# @return [Hash<String, String>]
|
||||
def app_state_environment
|
||||
app_state[:environment] ||= Mash.new
|
||||
end
|
||||
end
|
||||
|
||||
module Provider
|
||||
include Poise::Provider
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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/resources'
|
||||
@@ -0,0 +1,24 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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 PoiseApplication
|
||||
# Base exception class for poise-application errors.
|
||||
#
|
||||
# @since 5.0.0
|
||||
class Error < Exception
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,30 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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/resources/application'
|
||||
require 'poise_application/resources/application_cookbook_file'
|
||||
require 'poise_application/resources/application_directory'
|
||||
require 'poise_application/resources/application_file'
|
||||
require 'poise_application/resources/application_template'
|
||||
|
||||
|
||||
module PoiseApplication
|
||||
# Chef resources and providers for poise-application.
|
||||
#
|
||||
# @since 5.0.0
|
||||
module Resources
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,259 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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/dsl/recipe' # On 12.4+ this will pull in chef/dsl/resources.
|
||||
require 'chef/resource'
|
||||
require 'chef/provider'
|
||||
require 'poise'
|
||||
|
||||
|
||||
module PoiseApplication
|
||||
module Resources
|
||||
# (see Application::Resource)
|
||||
# @since 5.0.0
|
||||
module Application
|
||||
# An `application` resource to manage application deployment.
|
||||
#
|
||||
# @since 5.0.0
|
||||
# @provides application
|
||||
# @action deploy
|
||||
# @action start
|
||||
# @action stop
|
||||
# @action restart
|
||||
# @action reload
|
||||
# @example
|
||||
# application '/srv/myapp' do
|
||||
# git '...'
|
||||
# poise_service 'myapp' do
|
||||
# command '/srv/myapp/main'
|
||||
# end
|
||||
# end
|
||||
class Resource < Chef::Resource
|
||||
include Poise(container: true, container_namespace: false)
|
||||
provides(:application)
|
||||
actions(:deploy, :start, :stop, :restart, :reload)
|
||||
|
||||
# @!attribute path
|
||||
# Application base path.
|
||||
# @return [String]
|
||||
attribute(:path, kind_of: String, name_attribute: true)
|
||||
# @!attribute environment
|
||||
# Environment variables to set for the whole application.
|
||||
# @return [Hash<String, String>]
|
||||
attribute(:environment, kind_of: Hash, default: lazy { Mash.new })
|
||||
# @!attribute owner
|
||||
# System user that will own the application. This can be overriden in
|
||||
# individual subresources.
|
||||
# @return [String]
|
||||
attribute(:owner, kind_of: String)
|
||||
# @!attribute group
|
||||
# System group that will own the application. This can be overriden in
|
||||
# individual subresources.
|
||||
# @return [String]
|
||||
attribute(:group, kind_of: String)
|
||||
# @!attribute action_on_update
|
||||
# Action to run when any subresource is updated. Defaults to `:restart`.
|
||||
# @return [String, Symbol, nil, false]
|
||||
attribute(:action_on_update, kind_of: [Symbol, String, NilClass, FalseClass], default: :restart)
|
||||
# @!attribute action_on_update_immediately
|
||||
# Run the {#action_on_update} notification with `:immediately`.
|
||||
# @return [Boolean]
|
||||
attribute(:action_on_update_immediately, equal_to: [true, false], default: false)
|
||||
|
||||
# Run the DSL rewire when the resource object is created.
|
||||
# @api private
|
||||
def initialize(*args)
|
||||
super
|
||||
_rewire_dsl! if node
|
||||
end
|
||||
|
||||
# Application-specific state values used as a way to communicate between
|
||||
# subresources.
|
||||
#
|
||||
# @return [Mash]
|
||||
# @example
|
||||
# if new_resource.parent && new_resource.parent.app_state['gemfile_path']
|
||||
def app_state
|
||||
@app_state ||= Mash.new(environment: environment)
|
||||
end
|
||||
|
||||
# Override Container#register_subresource to add our action_on_update.
|
||||
#
|
||||
# @api private
|
||||
def register_subresource(resource)
|
||||
super.tap do |added|
|
||||
if added && action_on_update
|
||||
Chef::Log.debug("[#{self}] Registering #{action_on_update_immediately ? 'immediate ' : ''}#{action_on_update} notification from #{resource}")
|
||||
resource.notifies action_on_update.to_sym, self, (action_on_update_immediately ? :immediately : :delayed)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Find all resources that need to be rewired. This is anything with a
|
||||
# name starting with application_.
|
||||
#
|
||||
# @return [Array<String>]
|
||||
def _rewire_resources
|
||||
if defined?(Chef::DSL::Resources)
|
||||
# Chef >= 12.4.
|
||||
Chef::DSL::Resources.instance_methods
|
||||
else
|
||||
# Chef < 12.4 >= 12.0.
|
||||
Chef::Resource.descendants.map do |klass|
|
||||
klass.node_map.instance_variable_get(:@map).keys + if klass.dsl_name.include?('::')
|
||||
# Probably not valid.
|
||||
# :nocov:
|
||||
[]
|
||||
# :nocov:
|
||||
else
|
||||
# Needed for things that don't call provides().
|
||||
[klass.dsl_name]
|
||||
end
|
||||
end.flatten
|
||||
end.map {|name| name.to_s }.select {|name| name.start_with?('application_') }.uniq
|
||||
end
|
||||
|
||||
# Find all cookbooks that might contain LWRPs matching our name scheme.
|
||||
#
|
||||
# @return [Array<String>]
|
||||
def _rewire_cookbooks
|
||||
# Run context might be unset during test setup.
|
||||
if run_context
|
||||
run_context.cookbook_collection.keys.select {|cookbook_name| cookbook_name.start_with?('application_') }
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
# Build the mapping of new_name => old_name for each resource to rewire.
|
||||
#
|
||||
# @return [Hash<String, String>]
|
||||
def _rewire_map
|
||||
application_cookbooks = _rewire_cookbooks
|
||||
_rewire_resources.inject({}) do |memo, name|
|
||||
# Grab the resource class to check if it is an LWRP.
|
||||
klass = Chef::Resource.resource_for_node(name.to_sym, node)
|
||||
# Find the part to trim. Check for LWRP first, then just application_.
|
||||
trim = if klass < Chef::Resource::LWRPBase
|
||||
application_cookbooks.find {|cookbook_name| name.start_with?(cookbook_name) && name != cookbook_name } || 'application'
|
||||
else
|
||||
# Non-LWRPs are assumed to have a better name.
|
||||
'application'
|
||||
end
|
||||
# Map trimmed to untrimmed.
|
||||
memo[name[trim.length+1..-1]] = name
|
||||
memo
|
||||
end
|
||||
end
|
||||
|
||||
# Build new DSL methods to implement the foo -> application_foo behavior.
|
||||
#
|
||||
# @return [void]
|
||||
def _rewire_dsl!
|
||||
# Generate stub methods for all the rewiring.
|
||||
_rewire_map.each do |new_name, old_name|
|
||||
# This is defined as a singleton method on self so it looks like
|
||||
# the DSL but is scoped to just this context.
|
||||
define_singleton_method(new_name) do |name=nil, *args, &block|
|
||||
# Store the caller to correct the source_line.
|
||||
created_at = caller[0]
|
||||
public_send(old_name, name, *args) do
|
||||
# Set the declared type to be the native name.
|
||||
self.declared_type = self.class.resource_name
|
||||
# Fix the source location. For Chef 12.4 we could do this with the
|
||||
# declared_at parameter on the initial send.
|
||||
self.source_line = created_at
|
||||
# Run the original block.
|
||||
instance_exec(&block) if block
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Provider for `application`.
|
||||
#
|
||||
# @since 5.0.0
|
||||
# @see Resource
|
||||
# @provides application
|
||||
class Provider < Chef::Provider
|
||||
include Poise
|
||||
provides(:application)
|
||||
|
||||
# `deploy` action for `application`. Creates the application base folder.
|
||||
#
|
||||
# @return [void]
|
||||
def action_deploy
|
||||
notifying_block do
|
||||
directory new_resource.path do
|
||||
owner new_resource.owner
|
||||
group new_resource.group
|
||||
mode '755'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# `start` action for `application`. Proxies to subresources.
|
||||
#
|
||||
# @return [void]
|
||||
def action_start
|
||||
proxy_action(:start)
|
||||
end
|
||||
|
||||
# `stop` action for `application`. Proxies to subresources.
|
||||
#
|
||||
# @return [void]
|
||||
def action_stop
|
||||
proxy_action(:stop)
|
||||
end
|
||||
|
||||
# `restart` action for `application`. Proxies to subresources.
|
||||
#
|
||||
# @return [void]
|
||||
def action_restart
|
||||
proxy_action(:restart)
|
||||
end
|
||||
|
||||
# `reload` action for `application`. Proxies to subresources.
|
||||
#
|
||||
# @return [void]
|
||||
def action_reload
|
||||
proxy_action(:reload)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Proxy an action to any subresources that support it.
|
||||
#
|
||||
# @param action [Symbol] Action to proxy.
|
||||
# @return [void]
|
||||
def proxy_action(action)
|
||||
Chef::Log.debug("[#{new_resource} Running proxied #{action} action")
|
||||
new_resource.subresources.each do |r|
|
||||
begin
|
||||
r.run_action(action) if r.allowed_actions.include?(action)
|
||||
rescue Chef::Exceptions::UnsupportedAction
|
||||
# Don't care, just move on.
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,54 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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/app_file_mixin'
|
||||
|
||||
|
||||
module PoiseApplication
|
||||
module Resources
|
||||
# (see ApplicationCookbookFile::Resource)
|
||||
# @since 5.1.0
|
||||
module ApplicationCookbookFile
|
||||
# An `application_cookbook_file` resource to manage Chef cookbook_files inside and
|
||||
# Application cookbook deployment.
|
||||
#
|
||||
# @provides application_cookbook_file
|
||||
# @action create
|
||||
# @action create_if_missing
|
||||
# @action delete
|
||||
# @action touch
|
||||
# @example
|
||||
# application '/srv/myapp' do
|
||||
# cookbook_file 'myapp.conf' do
|
||||
# source 'myapp.conf'
|
||||
# end
|
||||
# end
|
||||
class Resource < Chef::Resource::CookbookFile
|
||||
include PoiseApplication::AppFileMixin
|
||||
provides(:application_cookbook_file)
|
||||
actions(:create, :create_if_missing, :delete, :touch)
|
||||
subclass_providers!
|
||||
|
||||
def initialize(*args)
|
||||
super
|
||||
# For older Chef.
|
||||
@resource_name = :application_cookbook_file
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,50 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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/app_file_mixin'
|
||||
|
||||
|
||||
module PoiseApplication
|
||||
module Resources
|
||||
# (see ApplicationDirectory::Resource)
|
||||
# @since 5.1.0
|
||||
module ApplicationDirectory
|
||||
# An `application_directory` resource to manage Chef files inside and
|
||||
# Application cookbook deployment.
|
||||
#
|
||||
# @provides application_directory
|
||||
# @action create
|
||||
# @action delete
|
||||
# @example
|
||||
# application '/srv/myapp' do
|
||||
# directory 'logs'
|
||||
# end
|
||||
class Resource < Chef::Resource::Directory
|
||||
include PoiseApplication::AppFileMixin
|
||||
provides(:application_directory)
|
||||
actions(:create, :delete)
|
||||
subclass_providers!
|
||||
|
||||
def initialize(*args)
|
||||
super
|
||||
# For older Chef.
|
||||
@resource_name = :application_directory
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,54 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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/app_file_mixin'
|
||||
|
||||
|
||||
module PoiseApplication
|
||||
module Resources
|
||||
# (see ApplicationFile::Resource)
|
||||
# @since 5.1.0
|
||||
module ApplicationFile
|
||||
# An `application_file` resource to manage Chef files inside and
|
||||
# Application cookbook deployment.
|
||||
#
|
||||
# @provides application_file
|
||||
# @action create
|
||||
# @action create_if_missing
|
||||
# @action delete
|
||||
# @action touch
|
||||
# @example
|
||||
# application '/srv/myapp' do
|
||||
# file 'myapp.conf' do
|
||||
# source 'myapp.conf.erb'
|
||||
# end
|
||||
# end
|
||||
class Resource < Chef::Resource::File
|
||||
include PoiseApplication::AppFileMixin
|
||||
provides(:application_file)
|
||||
actions(:create, :create_if_missing, :delete, :touch)
|
||||
subclass_providers!
|
||||
|
||||
def initialize(*args)
|
||||
super
|
||||
# For older Chef.
|
||||
@resource_name = :application_file
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,54 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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/app_file_mixin'
|
||||
|
||||
|
||||
module PoiseApplication
|
||||
module Resources
|
||||
# (see ApplicationTemplate::Resource)
|
||||
# @since 5.1.0
|
||||
module ApplicationTemplate
|
||||
# An `application_template` resource to manage Chef templates inside and
|
||||
# Application cookbook deployment.
|
||||
#
|
||||
# @provides application_template
|
||||
# @action create
|
||||
# @action create_if_missing
|
||||
# @action delete
|
||||
# @action touch
|
||||
# @example
|
||||
# application '/srv/myapp' do
|
||||
# template 'myapp.conf' do
|
||||
# source 'myapp.conf.erb'
|
||||
# end
|
||||
# end
|
||||
class Resource < Chef::Resource::Template
|
||||
include PoiseApplication::AppFileMixin
|
||||
provides(:application_template)
|
||||
actions(:create, :create_if_missing, :delete, :touch)
|
||||
subclass_providers!
|
||||
|
||||
def initialize(*args)
|
||||
super
|
||||
# For older Chef.
|
||||
@resource_name = :application_template
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,116 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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/resource'
|
||||
require 'chef/provider'
|
||||
require 'poise/utils'
|
||||
require 'poise_service/service_mixin'
|
||||
require 'poise_service/utils'
|
||||
|
||||
require 'poise_application/app_mixin'
|
||||
require 'poise_application/utils'
|
||||
|
||||
|
||||
module PoiseApplication
|
||||
# Mixin for application services. This is any resource that will be part of
|
||||
# an application deployment and involves running a persistent service.
|
||||
#
|
||||
# @api public
|
||||
# @since 5.0.0
|
||||
# @example
|
||||
# module MyApp
|
||||
# class Resource < Chef::Resource
|
||||
# include Poise
|
||||
# provides(:my_app)
|
||||
# include PoiseApplication::ServiceMixin
|
||||
# end
|
||||
#
|
||||
# class Provider < Chef::Provider
|
||||
# include Poise
|
||||
# provides(:my_app)
|
||||
# include PoiseApplication::ServiceMixin
|
||||
#
|
||||
# def action_enable
|
||||
# notifying_block do
|
||||
# template '/etc/myapp.conf' do
|
||||
# # ...
|
||||
# end
|
||||
# end
|
||||
# super
|
||||
# end
|
||||
#
|
||||
# def service_options(r)
|
||||
# super
|
||||
# r.command('myapp --serve')
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
module ServiceMixin
|
||||
include Poise::Utils::ResourceProviderMixin
|
||||
|
||||
# Mixin for application service resources.
|
||||
#
|
||||
# @see ServiceMixin
|
||||
module Resource
|
||||
include PoiseService::ServiceMixin::Resource
|
||||
include PoiseApplication::AppMixin::Resource
|
||||
|
||||
module ClassMethods
|
||||
# @api private
|
||||
def included(klass)
|
||||
super
|
||||
klass.extend(ClassMethods)
|
||||
klass.class_exec do
|
||||
attribute(:path, kind_of: String, name_attribute: true)
|
||||
# Redefines from the PoiseService version so we get a better default.
|
||||
attribute(:service_name, kind_of: String, default: lazy { PoiseService::Utils.parse_service_name(path) })
|
||||
attribute(:user, kind_of: [String, Integer], default: lazy { parent ? parent.owner : 'root' })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
extend ClassMethods
|
||||
end
|
||||
|
||||
# Mixin for application service providers.
|
||||
#
|
||||
# @see ServiceMixin
|
||||
module Provider
|
||||
include PoiseService::ServiceMixin::Provider
|
||||
include PoiseApplication::AppMixin::Provider
|
||||
|
||||
private
|
||||
|
||||
# Abstract hook to set parameters on {#service_resource} when it is
|
||||
# created. This is required to set at least `resource.command`.
|
||||
#
|
||||
# @api public
|
||||
# @param resource [Chef::Resource] Resource instance to set parameters on.
|
||||
# @return [void]
|
||||
# @example
|
||||
# def service_options(resource)
|
||||
# super
|
||||
# resource.command('myapp --serve')
|
||||
# end
|
||||
def service_options(resource)
|
||||
super
|
||||
resource.directory(new_resource.path)
|
||||
resource.user(new_resource.user)
|
||||
resource.environment.update(new_resource.app_state_environment) if new_resource.parent
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,51 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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 'etc'
|
||||
|
||||
|
||||
module PoiseApplication
|
||||
# Utility methods for PoiseApplication.
|
||||
#
|
||||
# @api public
|
||||
# @since 5.0.0
|
||||
module Utils
|
||||
# Methods are also available as module-level methods as well as a mixin.
|
||||
extend self
|
||||
|
||||
# Try to find the primary group name for a given user.
|
||||
#
|
||||
# @param user [String, Integer] User to check, if given as an integer this
|
||||
# is used as a UID, otherwise it is the username.
|
||||
# @return [String]
|
||||
# @example
|
||||
# attribute(:group, kind_of: [String, Integer], default: lazy { PoiseApplication::Utils.primary_group_for(user) })
|
||||
def primary_group_for(user)
|
||||
# Force a reload in case any users were created earlier in the run.
|
||||
Etc.endpwent
|
||||
Etc.endgrent
|
||||
user = if user.is_a?(Integer)
|
||||
Etc.getpwuid(user)
|
||||
else
|
||||
Etc.getpwnam(user.to_s)
|
||||
end
|
||||
Etc.getgrgid(user.gid).name
|
||||
rescue ArgumentError
|
||||
# One of the get* calls exploded. ¯\_(ツ)_/¯
|
||||
user.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
#
|
||||
# Copyright 2015-2016, 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 PoiseApplication
|
||||
VERSION = '5.2.0'
|
||||
end
|
||||
Reference in New Issue
Block a user