chef-helpers 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/chef-helpers/has_source.rb +98 -0
- data/lib/chef-helpers/version.rb +1 -1
- metadata +4 -3
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'chef/recipe'
|
2
|
+
require 'chef/resource/file'
|
3
|
+
|
4
|
+
module ChefHelpers
|
5
|
+
# Provides means for checking whether a template or cookbook file
|
6
|
+
# exist in a recipe, or directly in a resource definition.
|
7
|
+
module HasSource
|
8
|
+
# Checks for existence of a cookbook file or template source in a cookbook.
|
9
|
+
# @param [String] source name of the desired template or cookbook file source
|
10
|
+
# @param [Symbol] segment `:files` or `:templates`
|
11
|
+
# @param [String, nil] cookbook to look in, defaults to current cookbook
|
12
|
+
# @return [String, nil] full path to the source or `nil` if it doesn't exist
|
13
|
+
# @example
|
14
|
+
# has_source?("foo.erb", :templates)
|
15
|
+
# has_source?("bar.conf", :files, "a_cookbook")
|
16
|
+
def has_source?(source, segment, cookbook=nil)
|
17
|
+
cookbook ||= cookbook_name
|
18
|
+
begin
|
19
|
+
run_context.cookbook_collection[cookbook].
|
20
|
+
preferred_filename_on_disk_location(run_context.node, segment, src)
|
21
|
+
rescue Chef::Exceptions::FileNotFound
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Checks for existence of a template source in a cookbook.
|
27
|
+
# @param [String] tmpl name of the template source
|
28
|
+
# @param [String,nil] cookbook cookbook to look in, defaults to current cookbook
|
29
|
+
# @return [String,nil] full path to the template source or `nil`
|
30
|
+
# @see #has_source?
|
31
|
+
def has_template?(tmpl, cookbook=nil)
|
32
|
+
has_source?(tmpl, :templates, cookbook)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Checks for existence of a cookbook file in a cookbook.
|
36
|
+
# @param [String] cbf name of the cookbook file
|
37
|
+
# @param [String,nil] cookbook cookbook to look in, defaults to current cookbook
|
38
|
+
# @return [String,nil] full path to the template source or `nil`
|
39
|
+
# @see #has_source?
|
40
|
+
def has_cookbook_file?(cbf, cookbook=nil)
|
41
|
+
has_source?(tmpl, :files, cookbook)
|
42
|
+
end
|
43
|
+
|
44
|
+
# For a list of sources, returns first source that exist
|
45
|
+
# @param [Array<String>] sources list of source (template source
|
46
|
+
# or cookbook file) names to look for. Source name can include
|
47
|
+
# a cookbook name, e.g. `"mysql::my.cnf.erb"`
|
48
|
+
#
|
49
|
+
# Last parameter can be a `:templates` or `:files` keword, to
|
50
|
+
# indicate what kind of source to look for. If it is ommitted,
|
51
|
+
# and method is called in a `template` or `cookbook_file`
|
52
|
+
# resource block, it is automatically guessed; otherwise,
|
53
|
+
# `RuntimeError` is raised.
|
54
|
+
def try_sources(*sources)
|
55
|
+
segment =
|
56
|
+
if sources.last.is_a?(Symbol)
|
57
|
+
sources.pop
|
58
|
+
else
|
59
|
+
case self
|
60
|
+
when Chef::Resource::Template
|
61
|
+
:templates
|
62
|
+
when Chef::Resource::CookbookFile
|
63
|
+
:files
|
64
|
+
else
|
65
|
+
raise RuntimeError, "Please provide :templates or :files as last argument"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
sources.find do |source|
|
69
|
+
if source =~ /::/
|
70
|
+
src, ckbk = $`, $'
|
71
|
+
else
|
72
|
+
src, ckbk = source, cookbook_name
|
73
|
+
end
|
74
|
+
has_source?(src, segment, ckbk)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Return the first template source off the list that exists.
|
79
|
+
# @see #try_sources
|
80
|
+
def try_templates(*templates)
|
81
|
+
try_sources(templates, :templates)
|
82
|
+
end
|
83
|
+
|
84
|
+
# Return the first cookbook file off the list that exists.
|
85
|
+
# @see #try_sources
|
86
|
+
def try_files(*files)
|
87
|
+
try_sources(files, :files)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class Chef::Recipe
|
93
|
+
include ChefHelpers::HasSource
|
94
|
+
end
|
95
|
+
|
96
|
+
class Chef::Resource::File
|
97
|
+
include ChefHelpers::HasSource
|
98
|
+
end
|
data/lib/chef-helpers/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chef-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -42,6 +42,7 @@ files:
|
|
42
42
|
- Rakefile
|
43
43
|
- chef-helpers.gemspec
|
44
44
|
- lib/chef-helpers.rb
|
45
|
+
- lib/chef-helpers/has_source.rb
|
45
46
|
- lib/chef-helpers/node.rb
|
46
47
|
- lib/chef-helpers/version.rb
|
47
48
|
homepage: https://github.com/3ofcoins/chef-helpers/
|
@@ -58,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
59
|
version: '0'
|
59
60
|
segments:
|
60
61
|
- 0
|
61
|
-
hash:
|
62
|
+
hash: 841325784782894157
|
62
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
64
|
none: false
|
64
65
|
requirements:
|
@@ -67,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
68
|
version: '0'
|
68
69
|
segments:
|
69
70
|
- 0
|
70
|
-
hash:
|
71
|
+
hash: 841325784782894157
|
71
72
|
requirements: []
|
72
73
|
rubyforge_project:
|
73
74
|
rubygems_version: 1.8.24
|