chef-sugar 1.0.0.beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chef/sugar/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'chef-sugar'
8
+ spec.version = Chef::Sugar::VERSION
9
+ spec.authors = ['Seth Vargo']
10
+ spec.email = ['sethvargo@gmail.com']
11
+ spec.description = 'A series of helpful sugar of the Chef core and ' \
12
+ 'other resources to make a cleaner, more lean recipe ' \
13
+ 'DSL, enforce DRY principles, and make writing Chef ' \
14
+ 'recipes an awesome experience!'
15
+ spec.summary = 'A collection of helper methods and modules that ' \
16
+ 'make working with Chef recipes awesome.'
17
+ spec.homepage = 'https://github.com/sethvargo/chef-sugar'
18
+ spec.license = 'Apache 2.0'
19
+
20
+ spec.required_ruby_version = '>= 1.9'
21
+
22
+ spec.files = `git ls-files`.split($/)
23
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
24
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.3'
28
+ spec.add_development_dependency 'rake'
29
+
30
+ spec.add_development_dependency 'chef', '~> 11.0'
31
+ spec.add_development_dependency 'rspec', '~> 2.11'
32
+
33
+ spec.add_development_dependency 'chefspec', '~> 3.0.0.beta'
34
+ spec.add_development_dependency 'foodcritic', '~> 3.0'
35
+ spec.add_development_dependency 'test-kitchen', '~> 1.0.0.beta'
36
+ spec.add_development_dependency 'kitchen-vagrant', '~> 0.11'
37
+ end
data/lib/chef/sugar.rb ADDED
@@ -0,0 +1,38 @@
1
+ #
2
+ # Copyright 2013, Seth Vargo <sethvargo@gmail.com>
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'chef/recipe'
18
+ require 'chef/resource'
19
+ require 'chef/provider'
20
+
21
+ class Chef
22
+ module Sugar
23
+ require_relative 'sugar/architecture'
24
+ require_relative 'sugar/cloud'
25
+ require_relative 'sugar/ip'
26
+ require_relative 'sugar/node'
27
+ require_relative 'sugar/platform'
28
+ require_relative 'sugar/platform_family'
29
+ require_relative 'sugar/ruby'
30
+ require_relative 'sugar/shell'
31
+ require_relative 'sugar/vagrant'
32
+ require_relative 'sugar/version'
33
+ end
34
+ end
35
+
36
+ Chef::Recipe.send(:include, Chef::Sugar::DSL)
37
+ Chef::Resource.send(:include, Chef::Sugar::DSL)
38
+ Chef::Provider.send(:include, Chef::Sugar::DSL)
@@ -0,0 +1,53 @@
1
+ #
2
+ # Copyright 2013, Seth Vargo <sethvargo@gmail.com>
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ class Chef
18
+ module Sugar
19
+ module Architecture
20
+ extend self
21
+
22
+ #
23
+ # Determine if the current architecture is 64-bit
24
+ #
25
+ # @return [Boolean]
26
+ #
27
+ def _64_bit?(node)
28
+ node['kernel']['machine'] == 'x86_64'
29
+ end
30
+
31
+ #
32
+ # Determine if the current architecture is 32-bit
33
+ #
34
+ # @todo Make this more than "not 64-bit"
35
+ #
36
+ # @return [Boolean]
37
+ #
38
+ def _32_bit?(node)
39
+ !_64_bit?(node)
40
+ end
41
+ alias_method :i386?, :_32_bit?
42
+ end
43
+
44
+ module DSL
45
+ # @see Chef::Sugar::Architecture#_64_bit?
46
+ def _64_bit?; Chef::Sugar::Architecture._64_bit?(node); end
47
+
48
+ # @see Chef::Sugar::Architecture#_32_bit?
49
+ def _32_bit?; Chef::Sugar::Architecture._32_bit?(node); end
50
+ alias_method :i386?, :_32_bit?
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,147 @@
1
+ #
2
+ # Copyright 2013, Seth Vargo <sethvargo@gmail.com>
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ class Chef
18
+ module Sugar
19
+ module Cloud
20
+ extend self
21
+
22
+ #
23
+ # Return true if the current current node is in "the cloud".
24
+ #
25
+ # @param [Chef::Node] node
26
+ # the node to check
27
+ #
28
+ # @return [Boolean]
29
+ #
30
+ def cloud?(node)
31
+ node.key?('cloud')
32
+ end
33
+
34
+ #
35
+ # Return true if the current current node is in EC2
36
+ #
37
+ # @param [Chef::Node] node
38
+ # the node to check
39
+ #
40
+ # @return [Boolean]
41
+ #
42
+ def ec2?(node)
43
+ node.key?('ec2')
44
+ end
45
+
46
+ #
47
+ # Return true if the current current node is in GCE
48
+ #
49
+ # @param [Chef::Node] node
50
+ # the node to check
51
+ #
52
+ # @return [Boolean]
53
+ #
54
+ def gce?(node)
55
+ node.key?('gce')
56
+ end
57
+
58
+ #
59
+ # Return true if the current current node is in Rackspace
60
+ #
61
+ # @param [Chef::Node] node
62
+ # the node to check
63
+ #
64
+ # @return [Boolean]
65
+ #
66
+ def rackspace?(node)
67
+ node.key?('rackspace')
68
+ end
69
+
70
+ #
71
+ # Return true if the current current node is in Eucalyptus
72
+ #
73
+ # @param [Chef::Node] node
74
+ # the node to check
75
+ #
76
+ # @return [Boolean]
77
+ #
78
+ def eucalyptus?(node)
79
+ node.key?('eucalyptus')
80
+ end
81
+ alias_method :euca?, :eucalyptus?
82
+
83
+ #
84
+ # Return true if the current current node is in Linode
85
+ #
86
+ # @param [Chef::Node] node
87
+ # the node to check
88
+ #
89
+ # @return [Boolean]
90
+ #
91
+ def linode?(node)
92
+ node.key?('linode')
93
+ end
94
+
95
+ #
96
+ # Return true if the current current node is in Openstack
97
+ #
98
+ # @param [Chef::Node] node
99
+ # the node to check
100
+ #
101
+ # @return [Boolean]
102
+ #
103
+ def openstack?(node)
104
+ node.key?('openstack')
105
+ end
106
+
107
+ #
108
+ # Return true if the current current node is in Azure
109
+ #
110
+ # @param [Chef::Node] node
111
+ # the node to check
112
+ #
113
+ # @return [Boolean]
114
+ #
115
+ def azure?(node)
116
+ node.key?('azure')
117
+ end
118
+ end
119
+
120
+ module DSL
121
+ # @see Chef::Sugar::Cloud#cloud?
122
+ def cloud?; Chef::Sugar::Cloud.cloud?(node); end
123
+
124
+ # @see Chef::Sugar::Cloud#ec2?
125
+ def ec2?; Chef::Sugar::Cloud.ec2?(node); end
126
+
127
+ # @see Chef::Sugar::Cloud#gce?
128
+ def gce?; Chef::Sugar::Cloud.gce?(node); end
129
+
130
+ # @see Chef::Sugar::Cloud#rackspace?
131
+ def rackspace?; Chef::Sugar::Cloud.rackspace?(node); end
132
+
133
+ # @see Chef::Sugar::Cloud#eucalyptus?
134
+ def eucalyptus?; Chef::Sugar::Cloud.eucalyptus?(node); end
135
+ alias_method :euca?, :eucalyptus?
136
+
137
+ # @see Chef::Sugar::Cloud#linode?
138
+ def linode?; Chef::Sugar::Cloud.linode?(node); end
139
+
140
+ # @see Chef::Sugar::Cloud#openstack?
141
+ def openstack?; Chef::Sugar::Cloud.openstack?(node); end
142
+
143
+ # @see Chef::Sugar::Cloud#azure?
144
+ def azure?; Chef::Sugar::Cloud.azure?(node); end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,48 @@
1
+ #
2
+ # Copyright 2013, Seth Vargo <sethvargo@gmail.com>
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ class Chef
18
+ module Sugar
19
+ module IP
20
+ extend self
21
+
22
+ #
23
+ # The best IP address for the given node, in the context of
24
+ # the current node. Useful for choosing a local IP address
25
+ # over a public one to limit bandwidth on cloud providers.
26
+ #
27
+ # @param [Chef::Node] other
28
+ # the node to calculate the best IP address for
29
+ #
30
+ def best_ip_for(node, other)
31
+ if other['cloud']
32
+ if node['cloud'] && other['cloud']['provider'] == node['cloud']['provider']
33
+ other['cloud']['local_ipv4']
34
+ else
35
+ other['cloud']['public_ipv4']
36
+ end
37
+ else
38
+ other['ipaddress']
39
+ end
40
+ end
41
+ end
42
+
43
+ module DSL
44
+ # @see Chef::Sugar::IP#best_ip_for
45
+ def best_ip_for(other); Chef::Sugar::IP.best_ip_for(node, other); end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Copyright 2013, Seth Vargo <sethvargo@gmail.com>
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ class Chef
18
+ class Node
19
+ #
20
+ # Determine if the current node is in the given Chef environment
21
+ # (or matches the given regular expression).
22
+ #
23
+ # @param [String, Regex] environment
24
+ #
25
+ # @return [Boolean]
26
+ #
27
+ def in?(environment)
28
+ environment === chef_environment
29
+ end
30
+
31
+ #
32
+ # Determine if the current node includes the given recipe name.
33
+ #
34
+ # @param [String] recipe_name
35
+ #
36
+ def includes_recipe?(recipe_name)
37
+ run_list.include?(recipe_name)
38
+ end
39
+ alias_method :include_recipe?, :includes_recipe?
40
+ end
41
+ end
@@ -0,0 +1,166 @@
1
+ #
2
+ # Copyright 2013, Seth Vargo <sethvargo@gmail.com>
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ class Chef
18
+ module Sugar
19
+ module Platform
20
+ extend self
21
+
22
+ PLATFORM_VERSIONS = {
23
+ 'debian' => {
24
+ 'squeeze' => '6.0',
25
+ 'wheezy' => '7.0',
26
+ 'jessie' => '8.0',
27
+ },
28
+ 'linuxmint' => {
29
+ 'olivia' => '15',
30
+ 'nadia' => '14',
31
+ 'maya' => '13',
32
+ 'lisa' => '12',
33
+ },
34
+ 'mac_os_x' => {
35
+ 'lion' => '10.7',
36
+ 'mountain_lion' => '10.8',
37
+ 'mavericks' => '10.9',
38
+ },
39
+ 'ubuntu' => {
40
+ 'lucid' => '10.04',
41
+ 'maverick' => '10.10',
42
+ 'natty' => '11.04',
43
+ 'oneiric' => '11.10',
44
+ 'precise' => '12.04',
45
+ 'quantal' => '12.10',
46
+ 'raring' => '13.04',
47
+ 'saucy' => '13.10',
48
+ },
49
+ }
50
+
51
+ COMPARISON_OPERATORS = {
52
+ 'after' => ->(a, b) { a > b },
53
+ 'after_or_at' => ->(a, b) { a >= b },
54
+ '' => ->(a, b) { a == b },
55
+ 'before' => ->(a, b) { a < b },
56
+ 'before_or_at' => ->(a, b) { a <= b },
57
+ }
58
+
59
+ # Dynamically define custom matchers at runtime in a matrix. For each
60
+ # Platform, we create a map of named versions to their numerical
61
+ # equivalents (e.g. debian_before_squeeze?).
62
+ PLATFORM_VERSIONS.each do |platform, versions|
63
+ versions.each do |name, version|
64
+ COMPARISON_OPERATORS.each do |operator, block|
65
+ method_name = "#{platform}_#{operator}_#{name}?".squeeze('_').to_sym
66
+ define_method(method_name) do |node|
67
+ # Calling #to_f will ensure we only check major versions since
68
+ # '10.04.4'.to_f #=> 10.04.
69
+ node['platform'] == platform && block.call(node['platform_version'].to_f, version.to_f)
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ #
76
+ # Determine if the current node is linux mint.
77
+ #
78
+ # @param [Chef::Node] node
79
+ #
80
+ # @return [Boolean]
81
+ #
82
+ def linux_mint?(node)
83
+ node['platform'] == 'linuxmint'
84
+ end
85
+ alias_method :mint?, :linux_mint?
86
+
87
+ #
88
+ # Determine if the current node is ubuntu.
89
+ #
90
+ # @param [Chef::Node] node
91
+ #
92
+ # @return [Boolean]
93
+ #
94
+ def ubuntu?(node)
95
+ node['platform'] == 'ubuntu'
96
+ end
97
+
98
+ #
99
+ # Determine if the current node is amazon linux.
100
+ #
101
+ # @param [Chef::Node] node
102
+ #
103
+ # @return [Boolean]
104
+ #
105
+ def amazon_linux?(node)
106
+ node['platform'] == 'amazon'
107
+ end
108
+ alias_method :amazon?, :amazon_linux?
109
+
110
+ #
111
+ # Determine if the current node is centos.
112
+ #
113
+ # @param [Chef::Node] node
114
+ #
115
+ # @return [Boolean]
116
+ #
117
+ def centos?(node)
118
+ node['platform'] == 'centos'
119
+ end
120
+
121
+ #
122
+ # Determine if the current node is oracle linux.
123
+ #
124
+ # @param [Chef::Node] node
125
+ #
126
+ # @return [Boolean]
127
+ #
128
+ def oracle_linux?(node)
129
+ node['platform'] == 'oracle'
130
+ end
131
+ alias_method :oracle?, :oracle_linux?
132
+
133
+ #
134
+ # Determine if the current node is scientific linux.
135
+ #
136
+ # @param [Chef::Node] node
137
+ #
138
+ # @return [Boolean]
139
+ #
140
+ def scientific_linux?(node)
141
+ node['platform'] == 'scientific'
142
+ end
143
+ alias_method :scientific?, :scientific_linux?
144
+
145
+ #
146
+ # Determine if the current node is redhat enterprise.
147
+ #
148
+ # @param [Chef::Node] node
149
+ #
150
+ # @return [Boolean]
151
+ #
152
+ def redhat_enterprise_linux?(node)
153
+ node['platform'] == 'enterprise'
154
+ end
155
+ alias_method :redhat_enterprise?, :redhat_enterprise_linux?
156
+ end
157
+
158
+ module DSL
159
+ Chef::Sugar::Platform.instance_methods.each do |name|
160
+ define_method(name) do
161
+ Chef::Sugar::Platform.send(name, node)
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end