Class: ChefServerspecHandler

Inherits:
Chef::Handler
  • Object
show all
Defined in:
lib/chef-serverspec-handler.rb

Constant Summary collapse

HERE =
File.absolute_path File.dirname(__FILE__)
SUPPORTED_RESOURCES =
{
  Chef::Resource::File            => [:create, :create_if_missing, :touch],
  Chef::Resource::CookbookFile    => [:create, :create_if_missing],
  Chef::Resource::RemoteFile      => [:create, :create_if_missing],
  Chef::Resource::Directory       => [:create],
  Chef::Resource::RemoteDirectory => [:create, :create_if_missing],
  Chef::Resource::Template        => [:create, :create_if_missing],
  Chef::Resource::Package         => [:install],
  Chef::Resource::Link            => [:create],
  Chef::Resource::User            => [:create],
  Chef::Resource::Group           => [:create],
  Chef::Resource::Service         => [:start, :enable],
  Chef::Resource::Cron            => [:create]
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ChefServerspecHandler

Returns a new instance of ChefServerspecHandler.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chef-serverspec-handler.rb', line 23

def initialize(options={})
  @options = options

  if @options[:output_dir].nil?
    raise ArgumentError, 'option :output_dir is required'
  end

  if !FileTest.directory?(@options[:output_dir])
    raise ArgumentError, ":ouput_dir is not directory"
  end

  @options[:force] ||= false
end

Instance Method Details

#extract_target_resourcesObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/chef-serverspec-handler.rb', line 63

def extract_target_resources
  resources = Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = []}}

  all_resources.each do |resource|
    if SUPPORTED_RESOURCES.keys.include?(resource.class)
      if (SUPPORTED_RESOURCES[resource.class] & regular_actions(resource)).size > 0
        resources[resource.cookbook_name][resource.recipe_name] << resource
      end
    end
  end

  resources
end

#regular_actions(resource) ⇒ Object



77
78
79
# File 'lib/chef-serverspec-handler.rb', line 77

def regular_actions(resource)
  [resource.action].flatten.map {|action| action.to_sym }
end

#reportObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chef-serverspec-handler.rb', line 37

def report
  resources = extract_target_resources

  resources.each do |cookbook, recipes|
    cookbook_dir = File.join(@options[:output_dir], cookbook.to_s)
    FileUtils.mkdir_p cookbook_dir

    recipes.each do |recipe, resources|
      spec_path = File.join(cookbook_dir, "#{recipe}_spec.rb")

      if File.exists?(spec_path) && !@options[:force]
        Chef::Log.warn "ChefServerspecHandler: #{cookbook}/#{recipe}.rb already exists. set :force option to true if you want to override it"
        next
      end

      Chef::Log.info "ChefServerspecHandler: generating #{cookbook}/#{recipe}_spec.rb"

      File.open(spec_path, 'w') do |file|
        template = File.read File.join(HERE, 'chef-serverspec-handler.erb')
        spec = ERB.new(template, nil, '-').result(binding)
        file.write(spec)
      end
    end
  end
end