Skip to content

Commit

Permalink
Make inline_resource more standard and extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
John Keiser committed Apr 30, 2014
1 parent 7f1d3f2 commit 0448c9d
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 139 deletions.
8 changes: 4 additions & 4 deletions lib/chef/provider/private_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
class Chef::Provider::PrivateKey < Chef::Provider::LWRPBase

action :create do
create_key(false)
create_key(false, :create)
end

action :regenerate do
create_key(true)
create_key(true, :regenerate)
end

action :delete do
Expand All @@ -26,7 +26,7 @@ def whyrun_supported?
true
end

def create_key(regenerate)
def create_key(regenerate, action)
final_private_key = nil
if new_source_key
#
Expand Down Expand Up @@ -92,7 +92,7 @@ def create_key(regenerate)
if new_resource.public_key_path
public_key_path = new_resource.public_key_path
public_key_format = new_resource.public_key_format
Cheffish.inline_resource(self) do
Cheffish.inline_resource(self, action) do
public_key public_key_path do
source_key final_private_key
format public_key_format
Expand Down
6 changes: 3 additions & 3 deletions lib/cheffish.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'chef/run_list/run_list_item'
require 'cheffish/inline_resource'
require 'cheffish/basic_chef_client'

module Cheffish
NAME_REGEX = /^[.\-[:alnum:]_]+$/

def self.inline_resource(provider, &block)
InlineResource.new(provider).instance_eval(&block)
def self.inline_resource(provider, provider_action, &block)
BasicChefClient.inline_resource(provider, provider_action, &block)
end

NOT_PASSED=Object.new
Expand Down
96 changes: 96 additions & 0 deletions lib/cheffish/basic_chef_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
require 'chef/dsl/recipe'
require 'chef/event_dispatch/base'
require 'chef/event_dispatch/dispatcher'
require 'chef/node'
require 'chef/run_context'
require 'chef/runner'
require 'forwardable'

module Cheffish
class BasicChefClient
include Chef::DSL::Recipe

def initialize(node = nil, events = nil)
if !node
node = Chef::Node.new
node.name 'basic_client'
node.automatic[:platform] = 'test'
node.automatic[:platform_version] = 'test'
end

@event_catcher = BasicChefClientEvents.new
dispatcher = Chef::EventDispatch::Dispatcher.new(@event_catcher)
dispatcher.register(events) if events
@run_context = Chef::RunContext.new(node, {}, dispatcher)
@updated = []
@cookbook_name = 'basic_client'
end

extend Forwardable

# Stuff recipes need
attr_reader :run_context
attr_accessor :cookbook_name
attr_accessor :recipe_name
def_delegators :@run_context, :resource_collection, :immediate_notifications, :delayed_notifications

def load_block(&block)
@recipe_name = 'block'
instance_eval(&block)
end

def converge
Chef::Runner.new(self).converge
end

def updates
@event_catcher.updates
end

def updated?
@event_catcher.updates.size > 0
end

def self.inline_resource(provider, provider_action, &block)
events = ProviderEventForwarder.new(provider, provider_action)
client = BasicChefClient.new(provider.node)
client.load_block(&block)
client.converge
client.updated?
end

def self.converge_block(node = nil, events = nil, &block)
client = BasicChefClient.new(node, events)
client.load_block(&block)
client.converge
client.updated?
end

class BasicChefClientEvents < Chef::EventDispatch::Base
def initialize
@updates = []
end

attr_reader :updates

# Called after a resource has been completely converged.
def resource_updated(resource, action)
updates << [ resource, action ]
end
end

class ProviderEventForwarder < Chef::EventDispatch::Base
def initialize(provider, provider_action)
@provider = provider
@provider_action = provider_action
end

attr_reader :provider
attr_reader :provider_action

def resource_update_applied(resource, action, update)
provider.run_context.events.resource_update_applied(provider.new_resource, provider_action, update)
end
end
end
end
88 changes: 0 additions & 88 deletions lib/cheffish/inline_resource.rb

This file was deleted.

89 changes: 45 additions & 44 deletions lib/cheffish/recipe_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,64 @@
require 'chef/client'

class Chef
class Recipe
def with_chef_data_bag(name)
run_context.cheffish.with_data_bag(name, &block)
end
module DSL
module Recipe
def with_chef_data_bag(name)
run_context.cheffish.with_data_bag(name, &block)
end

def with_chef_environment(name, &block)
run_context.cheffish.with_environment(name, &block)
end
def with_chef_environment(name, &block)
run_context.cheffish.with_environment(name, &block)
end

def with_chef_data_bag_item_encryption(encryption_options, &block)
run_context.cheffish.with_data_bag_item_encryption(encryption_options, &block)
end
def with_chef_data_bag_item_encryption(encryption_options, &block)
run_context.cheffish.with_data_bag_item_encryption(encryption_options, &block)
end

def with_chef_server(server_url, options = {}, &block)
run_context.cheffish.with_chef_server({ :chef_server_url => server_url, :options => options }, &block)
end
def with_chef_server(server_url, options = {}, &block)
run_context.cheffish.with_chef_server({ :chef_server_url => server_url, :options => options }, &block)
end

def with_chef_local_server(options, &block)
options[:host] ||= '127.0.0.1'
options[:log_level] ||= Chef::Log.level
options[:port] ||= 8900
def with_chef_local_server(options, &block)
options[:host] ||= '127.0.0.1'
options[:log_level] ||= Chef::Log.level
options[:port] ||= 8900

# Create the data store chef-zero will use
options[:data_store] ||= begin
if !options[:chef_repo_path]
raise "chef_repo_path must be specified to with_chef_local_server"
end
# Create the data store chef-zero will use
options[:data_store] ||= begin
if !options[:chef_repo_path]
raise "chef_repo_path must be specified to with_chef_local_server"
end

# Ensure all paths are given
%w(acl client cookbook container data_bag environment group node role).each do |type|
options["#{type}_path".to_sym] ||= begin
if options[:chef_repo_path].kind_of?(String)
Chef::Config.path_join(options[:chef_repo_path], "#{type}s")
else
options[:chef_repo_path].map { |path| Chef::Config.path_join(path, "#{type}s")}
# Ensure all paths are given
%w(acl client cookbook container data_bag environment group node role).each do |type|
options["#{type}_path".to_sym] ||= begin
if options[:chef_repo_path].kind_of?(String)
Chef::Config.path_join(options[:chef_repo_path], "#{type}s")
else
options[:chef_repo_path].map { |path| Chef::Config.path_join(path, "#{type}s")}
end
end
# Work around issue in earlier versions of ChefFS where it expects strings for these
# instead of symbols
options["#{type}_path"] = options["#{type}_path".to_sym]
end
# Work around issue in earlier versions of ChefFS where it expects strings for these
# instead of symbols
options["#{type}_path"] = options["#{type}_path".to_sym]
end

chef_fs = Chef::ChefFS::Config.new(options).local_fs
chef_fs.write_pretty_json = true
Chef::ChefFS::ChefFSDataStore.new(chef_fs)
end
chef_fs = Chef::ChefFS::Config.new(options).local_fs
chef_fs.write_pretty_json = true
Chef::ChefFS::ChefFSDataStore.new(chef_fs)
end

# Start the chef-zero server
Chef::Log.info("Starting chef-zero on port #{options[:port]} with repository at #{options[:data_store].chef_fs.fs_description}")
chef_zero_server = ChefZero::Server.new(options)
chef_zero_server.start_background
# Start the chef-zero server
Chef::Log.info("Starting chef-zero on port #{options[:port]} with repository at #{options[:data_store].chef_fs.fs_description}")
chef_zero_server = ChefZero::Server.new(options)
chef_zero_server.start_background

run_context.cheffish.local_servers << chef_zero_server
run_context.cheffish.local_servers << chef_zero_server

with_chef_server(chef_zero_server.url, &block)
with_chef_server(chef_zero_server.url, &block)
end
end

end

class RunContext
Expand Down

0 comments on commit 0448c9d

Please sign in to comment.