chef-sugar-ng 4.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.github/lock.yml +3 -0
  3. data/.github/reaction.yml +1 -0
  4. data/.gitignore +26 -0
  5. data/.kitchen.yml +16 -0
  6. data/.travis.yml +17 -0
  7. data/CHANGELOG.md +261 -0
  8. data/CONTRIBUTING.md +20 -0
  9. data/Gemfile +2 -0
  10. data/LICENSE +201 -0
  11. data/README.md +540 -0
  12. data/Rakefile +11 -0
  13. data/chef-sugar-ng.gemspec +33 -0
  14. data/lib/chef/sugar.rb +51 -0
  15. data/lib/chef/sugar/architecture.rb +171 -0
  16. data/lib/chef/sugar/cloud.rb +192 -0
  17. data/lib/chef/sugar/constraints.rb +108 -0
  18. data/lib/chef/sugar/constraints_dsl.rb +83 -0
  19. data/lib/chef/sugar/core_extensions.rb +19 -0
  20. data/lib/chef/sugar/core_extensions/array.rb +34 -0
  21. data/lib/chef/sugar/core_extensions/object.rb +27 -0
  22. data/lib/chef/sugar/core_extensions/string.rb +66 -0
  23. data/lib/chef/sugar/data_bag.rb +146 -0
  24. data/lib/chef/sugar/docker.rb +40 -0
  25. data/lib/chef/sugar/filters.rb +227 -0
  26. data/lib/chef/sugar/init.rb +62 -0
  27. data/lib/chef/sugar/ip.rb +48 -0
  28. data/lib/chef/sugar/kernel.rb +49 -0
  29. data/lib/chef/sugar/kitchen.rb +40 -0
  30. data/lib/chef/sugar/node.rb +213 -0
  31. data/lib/chef/sugar/platform.rb +327 -0
  32. data/lib/chef/sugar/platform_family.rb +179 -0
  33. data/lib/chef/sugar/ruby.rb +51 -0
  34. data/lib/chef/sugar/run_context.rb +41 -0
  35. data/lib/chef/sugar/shell.rb +147 -0
  36. data/lib/chef/sugar/vagrant.rb +77 -0
  37. data/lib/chef/sugar/version.rb +21 -0
  38. data/lib/chef/sugar/virtualization.rb +151 -0
  39. data/libraries/chef-sugar.rb +1 -0
  40. data/metadata.rb +24 -0
  41. data/recipes/default.rb +20 -0
  42. data/spec/spec_helper.rb +25 -0
  43. data/spec/support/shared_examples.rb +20 -0
  44. data/spec/unit/chef/sugar/architecture_spec.rb +129 -0
  45. data/spec/unit/chef/sugar/cloud_spec.rb +149 -0
  46. data/spec/unit/chef/sugar/constraints_spec.rb +45 -0
  47. data/spec/unit/chef/sugar/core_extensions/array_spec.rb +10 -0
  48. data/spec/unit/chef/sugar/core_extensions/object_spec.rb +62 -0
  49. data/spec/unit/chef/sugar/core_extensions/string_spec.rb +48 -0
  50. data/spec/unit/chef/sugar/data_bag_spec.rb +118 -0
  51. data/spec/unit/chef/sugar/docker_spec.rb +39 -0
  52. data/spec/unit/chef/sugar/init_spec.rb +74 -0
  53. data/spec/unit/chef/sugar/ip_spec.rb +53 -0
  54. data/spec/unit/chef/sugar/kernel_spec.rb +16 -0
  55. data/spec/unit/chef/sugar/kitchen_spec.rb +18 -0
  56. data/spec/unit/chef/sugar/node_spec.rb +172 -0
  57. data/spec/unit/chef/sugar/platform_family_spec.rb +166 -0
  58. data/spec/unit/chef/sugar/platform_spec.rb +342 -0
  59. data/spec/unit/chef/sugar/ruby_spec.rb +39 -0
  60. data/spec/unit/chef/sugar/run_context_spec.rb +19 -0
  61. data/spec/unit/chef/sugar/shell_spec.rb +104 -0
  62. data/spec/unit/chef/sugar/vagrant_spec.rb +37 -0
  63. data/spec/unit/chef/sugar/virtualization_spec.rb +135 -0
  64. data/spec/unit/recipes/default_spec.rb +9 -0
  65. metadata +202 -0
@@ -0,0 +1,179 @@
1
+ #
2
+ # Copyright 2013-2015, 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 PlatformFamily
20
+ extend self
21
+
22
+ #
23
+ # Determine if the current node is a member of the arch family.
24
+ #
25
+ # @param [Chef::Node] node
26
+ #
27
+ # @return [Boolean]
28
+ #
29
+ def arch_linux?(node)
30
+ node['platform_family'] == 'arch'
31
+ end
32
+ alias_method :arch?, :arch_linux?
33
+
34
+ #
35
+ # Determine if the current node is a member of the debian family.
36
+ #
37
+ # @param [Chef::Node] node
38
+ #
39
+ # @return [Boolean]
40
+ #
41
+ def debian?(node)
42
+ node['platform_family'] == 'debian'
43
+ end
44
+
45
+ #
46
+ # Determine if the current node is a member of the fedora family.
47
+ #
48
+ # @param [Chef::Node] node
49
+ #
50
+ # @return [Boolean]
51
+ #
52
+ def fedora?(node)
53
+ node['platform_family'] == 'fedora'
54
+ end
55
+
56
+ #
57
+ # Determine if the current node is a member of the freebsd family.
58
+ #
59
+ # @param [Chef::Node] node
60
+ #
61
+ # @return [Boolean]
62
+ #
63
+ def freebsd?(node)
64
+ node['platform_family'] == 'freebsd'
65
+ end
66
+
67
+ #
68
+ # Determine if the current node is a member of the arch family.
69
+ #
70
+ # @param [Chef::Node] node
71
+ #
72
+ # @return [Boolean]
73
+ #
74
+ def gentoo?(node)
75
+ node['platform_family'] == 'gentoo'
76
+ end
77
+
78
+ #
79
+ # Determine if the current node is a member of the OSX family.
80
+ #
81
+ # @param [Chef::Node] node
82
+ #
83
+ # @return [Boolean]
84
+ #
85
+ def mac_os_x?(node)
86
+ node['platform_family'] == 'mac_os_x'
87
+ end
88
+ alias_method :osx?, :mac_os_x?
89
+ alias_method :mac?, :mac_os_x?
90
+
91
+ #
92
+ # Determine if the current node is a member of the openbsd family.
93
+ #
94
+ # @param [Chef::Node] node
95
+ #
96
+ # @return [Boolean]
97
+ #
98
+ def openbsd?(node)
99
+ node['platform_family'] == 'openbsd'
100
+ end
101
+
102
+ #
103
+ # Determine if the current node is a member of the redhat family.
104
+ #
105
+ # @param [Chef::Node] node
106
+ #
107
+ # @return [Boolean]
108
+ #
109
+ def rhel?(node)
110
+ node['platform_family'] == 'rhel'
111
+ end
112
+ alias_method :redhat?, :rhel?
113
+ alias_method :el?, :rhel?
114
+
115
+ #
116
+ # Determine if the current node is a member of the slackware family.
117
+ #
118
+ # @param [Chef::Node] node
119
+ #
120
+ # @return [Boolean]
121
+ #
122
+ def slackware?(node)
123
+ node['platform_family'] == 'slackware'
124
+ end
125
+
126
+ #
127
+ # Determine if the current node is a member of the suse family.
128
+ #
129
+ # @param [Chef::Node] node
130
+ #
131
+ # @return [Boolean]
132
+ #
133
+ def suse?(node)
134
+ node['platform_family'] == 'suse'
135
+ end
136
+
137
+ #
138
+ # Determine if the current node is a member of the windows family.
139
+ #
140
+ # @param [Chef::Node] node
141
+ #
142
+ # @return [Boolean]
143
+ #
144
+ def windows?(node)
145
+ node['platform_family'] == 'windows'
146
+ end
147
+
148
+ #
149
+ # Determine if the current node is a member of the wrlinux family.
150
+ #
151
+ # @param [Chef::Node] node
152
+ #
153
+ # @return [Boolean]
154
+ #
155
+ def wrlinux?(node)
156
+ node['platform_family'] == 'wrlinux'
157
+ end
158
+
159
+ #
160
+ # Determine if the current system is a linux derivative
161
+ #
162
+ # @param [Chef::Node] node
163
+ #
164
+ # @return [Boolean]
165
+ #
166
+ def linux?(node)
167
+ node['os'] == 'linux'
168
+ end
169
+ end
170
+
171
+ module DSL
172
+ Chef::Sugar::PlatformFamily.instance_methods.each do |name|
173
+ define_method(name) do
174
+ Chef::Sugar::PlatformFamily.send(name, node)
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,51 @@
1
+ #
2
+ # Copyright 2013-2015, 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 Ruby
20
+ extend self
21
+
22
+ #
23
+ # Determine if the current Ruby version is 2.0.
24
+ #
25
+ # @return [Boolean]
26
+ #
27
+ def ruby_20?(node)
28
+ version = Gem::Version.new(node['languages']['ruby']['version'])
29
+ Gem::Requirement.new('~> 2.0.0').satisfied_by?(version)
30
+ end
31
+
32
+ #
33
+ # Determine if the current Ruby version is 1.9.
34
+ #
35
+ # @return [Boolean]
36
+ #
37
+ def ruby_19?(node)
38
+ version = Gem::Version.new(node['languages']['ruby']['version'])
39
+ Gem::Requirement.new('~> 1.9.0').satisfied_by?(version)
40
+ end
41
+ end
42
+
43
+ module DSL
44
+ # @see Chef::Sugar::Ruby#ruby_20?
45
+ def ruby_20?; Chef::Sugar::Ruby.ruby_20?(node); end
46
+
47
+ # @see Chef::Sugar::Ruby#ruby_19?
48
+ def ruby_19?; Chef::Sugar::Ruby.ruby_19?(node); end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Copyright 2013-2015, 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 RunContext
20
+ extend self
21
+
22
+ #
23
+ # Determine if the current node includes the given recipe name.
24
+ #
25
+ # @param [String] recipe_name
26
+ #
27
+ def includes_recipe?(node, recipe_name)
28
+ node.recipe?(recipe_name)
29
+ end
30
+ alias_method :include_recipe?, :includes_recipe?
31
+ end
32
+
33
+ module DSL
34
+ # @see Chef::Sugar::IP#best_ip_for
35
+ def includes_recipe?(recipe_name)
36
+ Chef::Sugar::RunContext.includes_recipe?(node, recipe_name)
37
+ end
38
+ alias_method :include_recipe?, :includes_recipe?
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,147 @@
1
+ #
2
+ # Copyright 2013-2015, 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 'mixlib/shellout'
18
+ require 'pathname'
19
+
20
+ class Chef
21
+ module Sugar
22
+ module Shell
23
+ extend self
24
+
25
+ #
26
+ # Finds a command in $PATH
27
+ #
28
+ # @param [String] cmd
29
+ # the command to find
30
+ #
31
+ # @return [String, nil]
32
+ #
33
+ def which(cmd)
34
+ if Pathname.new(cmd).absolute?
35
+ File.executable?(cmd) ? cmd : nil
36
+ else
37
+ paths = ENV['PATH'].split(::File::PATH_SEPARATOR) + %w(/bin /usr/bin /sbin /usr/sbin)
38
+
39
+ paths.each do |path|
40
+ possible = File.join(path, cmd)
41
+ return possible if File.executable?(possible)
42
+ end
43
+
44
+ nil
45
+ end
46
+ end
47
+
48
+ #
49
+ # The platform-specific output path to +/dev/null+.
50
+ #
51
+ # @return [String]
52
+ #
53
+ def dev_null(node)
54
+ Chef::Sugar::PlatformFamily.windows?(node) ? 'NUL' : '/dev/null'
55
+ end
56
+
57
+ #
58
+ # Boolean method to check if a command line utility is installed.
59
+ #
60
+ # @param [String] cmd
61
+ # the command to find
62
+ #
63
+ # @return [Boolean]
64
+ # true if the command is found in the path, false otherwise
65
+ #
66
+ def installed?(cmd)
67
+ !which(cmd).nil?
68
+ end
69
+
70
+ #
71
+ # Checks if the given binary is installed and exists at the given
72
+ # version. Also see {version_for}.
73
+ #
74
+ # @param [String] cmd
75
+ # the command to check
76
+ # @param [String] expected_version
77
+ # the version to check
78
+ # @param [String] flag
79
+ # the flag to use to check the version of the binary
80
+ #
81
+ # @return [Boolean]
82
+ # true if the command exists and is at the given version, false
83
+ # otherwise
84
+ #
85
+ def installed_at_version?(cmd, expected_version, flag = '--version')
86
+ return false if !installed?(cmd)
87
+
88
+ version = version_for(cmd, flag)
89
+ return false if version.nil?
90
+
91
+ if expected_version.is_a?(Regexp)
92
+ !version.match(expected_version).nil?
93
+ else
94
+ version.include?(expected_version)
95
+ end
96
+ end
97
+
98
+ #
99
+ # The version for a given command. This method does NOT check if the
100
+ # command exists! It is assumed the command existence has been
101
+ # checked with +which+ or similar. To simply check if an installed
102
+ # version is acceptable, please see {installed_at_version}.
103
+ #
104
+ # Assumptions:
105
+ # 1. The command exists.
106
+ # 2. The command outputs version information to +$stdout+ or +$stderr+.
107
+ # Did you know that java outputs its version to $stderr?
108
+ #
109
+ #
110
+ # @param [String] cmd
111
+ # the command to find the version for
112
+ # @param [String] flag
113
+ # the flag to use to get the version
114
+ #
115
+ # @return [String]
116
+ # the entire output of the version command (stderr and stdout)
117
+ #
118
+ def version_for(cmd, flag = '--version')
119
+ cmd = Mixlib::ShellOut.new("#{cmd} #{flag}")
120
+ cmd.run_command
121
+ cmd.error!
122
+ [cmd.stdout.strip, cmd.stderr.strip].join("\n")
123
+ end
124
+ end
125
+
126
+ module DSL
127
+ # @see Chef::Sugar::Shell#which
128
+ def which(cmd); Chef::Sugar::Shell.which(cmd); end
129
+
130
+ # @see Chef::Sugar::Shell#dev_null
131
+ def dev_null; Chef::Sugar::Shell.dev_null(node); end
132
+
133
+ # @see Chef::Sugar::Shell#installed?
134
+ def installed?(cmd); Chef::Sugar::Shell.installed?(cmd); end
135
+
136
+ # @see Chef::Sugar::Shell#installed_at_version?
137
+ def installed_at_version?(cmd, version, flag = '--version')
138
+ Chef::Sugar::Shell.installed_at_version?(cmd, version, flag)
139
+ end
140
+
141
+ # @see Chef::Sugar::Shell#version_for
142
+ def version_for(cmd, flag = '--version')
143
+ Chef::Sugar::Shell.version_for(cmd, flag)
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,77 @@
1
+ #
2
+ # Copyright 2013-2015, 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 Vagrant
20
+ extend self
21
+
22
+ #
23
+ # Determine if the current node is running in vagrant mode.
24
+ #
25
+ # @param [Chef::Node] node
26
+ #
27
+ # @return [Boolean]
28
+ # true if the machine is currently running vagrant, false
29
+ # otherwise
30
+ #
31
+ def vagrant?(node)
32
+ vagrant_key?(node) || vagrant_domain?(node) || vagrant_user?(node)
33
+ end
34
+
35
+ private
36
+
37
+ #
38
+ # Check if the +vagrant+ key exists on the +node+ object. This key is no
39
+ # longer populated by vagrant, but it is kept around for legacy purposes.
40
+ #
41
+ # @param (see vagrant?)
42
+ # @return (see vagrant?)
43
+ #
44
+ def vagrant_key?(node)
45
+ node.key?('vagrant')
46
+ end
47
+
48
+ #
49
+ # Check if "vagrantup.com" is included in the node's domain. Technically,
50
+ # this would make Chef Sugar falsely detect +vagrant?+ on any of
51
+ # Hashicorp's servers. But if that edge case becomes a serious problem,
52
+ # @mitchellh has my phone number.
53
+ #
54
+ # @param (see vagrant?)
55
+ # @return (see vagrant?)
56
+ #
57
+ def vagrant_domain?(node)
58
+ node.key?('domain') && !node['domain'].nil? && node['domain'].include?('vagrantup.com')
59
+ end
60
+
61
+ #
62
+ # Check if the system contains a +vagrant+ user.
63
+ #
64
+ # @param (see vagrant?)
65
+ # @return (see vagrant?)
66
+ #
67
+ def vagrant_user?(node)
68
+ node.key?('etc') && node['etc'].key?('passwd') && node['etc']['passwd'].key?('vagrant')
69
+ end
70
+ end
71
+
72
+ module DSL
73
+ # @see Chef::Sugar::Vagrant#vagrant?
74
+ def vagrant?; Chef::Sugar::Vagrant.vagrant?(node); end
75
+ end
76
+ end
77
+ end