chef-utils 0.0.1 → 15.5.9

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.
@@ -0,0 +1,314 @@
1
+ #
2
+ # Copyright:: Copyright 2018-2019, Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require_relative "../internal"
19
+
20
+ module ChefUtils
21
+ module DSL
22
+ module PlatformFamily
23
+ include Internal
24
+
25
+ # Determine if the current node is arch linux.
26
+ #
27
+ # @param [Chef::Node] node
28
+ #
29
+ # @return [Boolean]
30
+ #
31
+ def arch?(node = __getnode)
32
+ node["platform_family"] == "arch"
33
+ end
34
+ # chef-sugar backcompat methods
35
+ alias_method :arch_linux?, :arch?
36
+
37
+ # Determine if the current node is aix
38
+ #
39
+ # @param [Chef::Node] node
40
+ #
41
+ # @return [Boolean]
42
+ #
43
+ def aix?(node = __getnode)
44
+ node["platform_family"] == "aix"
45
+ end
46
+
47
+ # Determine if the current node is a member of the debian family.
48
+ #
49
+ # @param [Chef::Node] node
50
+ #
51
+ # @return [Boolean]
52
+ #
53
+ def debian?(node = __getnode)
54
+ node["platform_family"] == "debian"
55
+ end
56
+
57
+ # Determine if the current node is a member of the fedora family.
58
+ #
59
+ # @param [Chef::Node] node
60
+ #
61
+ # @return [Boolean]
62
+ #
63
+ def fedora?(node = __getnode)
64
+ node["platform_family"] == "fedora"
65
+ end
66
+
67
+ # Determine if the current node is a member of the OSX family.
68
+ #
69
+ # @param [Chef::Node] node
70
+ #
71
+ # @return [Boolean]
72
+ #
73
+ def macos?(node = __getnode)
74
+ node["platform_family"] == "mac_os_x"
75
+ end
76
+ alias_method :osx?, :macos?
77
+ alias_method :mac?, :macos?
78
+ alias_method :mac_os_x?, :macos?
79
+
80
+ # Determine if the current node is a member of the rhel family (RHEL, CentOS, Oracle or Scientific Linux, no Amazon or Fedora).
81
+ #
82
+ # The platform_versions for these operating systems must match (rhel7 == centos7 == oracle7 == scientfic7), modulo additional packages
83
+ #
84
+ # @param [Chef::Node] node
85
+ #
86
+ # @return [Boolean]
87
+ #
88
+ def rhel?(node = __getnode)
89
+ node["platform_family"] == "rhel"
90
+ end
91
+ alias_method :el?, :rhel?
92
+
93
+ # Determine if the current node is a rhel6 compatible build (RHEL, CentOS, Oracle or Scientific Linux)
94
+ #
95
+ # @param [Chef::Node] node
96
+ #
97
+ # @return [Boolean]
98
+ #
99
+ def rhel6?(node = __getnode)
100
+ node["platform_family"] == "rhel" && node["platform_version"].to_f >= 6.0 && node["platform_version"].to_f < 7.0
101
+ end
102
+
103
+ # Determine if the current node is a rhel7 compatible build (RHEL, CentOS, Oracle or Scientific Linux)
104
+ #
105
+ # @param [Chef::Node] node
106
+ #
107
+ # @return [Boolean]
108
+ #
109
+ def rhel7?(node = __getnode)
110
+ node["platform_family"] == "rhel" && node["platform_version"].to_f >= 7.0 && node["platform_version"].to_f < 8.0
111
+ end
112
+
113
+ # Determine if the current node is a rhel8 compatible build (RHEL, CentOS, Oracle or Scientific Linux)
114
+ #
115
+ # @param [Chef::Node] node
116
+ #
117
+ # @return [Boolean]
118
+ #
119
+ def rhel8?(node = __getnode)
120
+ node["platform_family"] == "rhel" && node["platform_version"].to_f >= 8.0 && node["platform_version"].to_f < 9.0
121
+ end
122
+
123
+ # Determine if the current node is a member of the amazon family.
124
+ #
125
+ # @param [Chef::Node] node
126
+ #
127
+ # @return [Boolean]
128
+ #
129
+ def amazon?(node = __getnode)
130
+ node["platform_family"] == "amazon"
131
+ end
132
+ alias_method :amazon_linux?, :amazon?
133
+
134
+ # Determine if the current node is solaris2
135
+ #
136
+ # @param [Chef::Node] node
137
+ #
138
+ # @return [Boolean]
139
+ #
140
+ def solaris2?(node = __getnode)
141
+ node["platform_family"] == "solaris2"
142
+ end
143
+ # chef-sugar backcompat methods
144
+ alias_method :solaris?, :solaris2?
145
+
146
+ # Determine if the current node is smartos
147
+ #
148
+ # @param [Chef::Node] node
149
+ #
150
+ # @return [Boolean]
151
+ #
152
+ def smartos?(node = __getnode)
153
+ node["platform_family"] == "smartos"
154
+ end
155
+
156
+ # Determine if the current node is a member of the suse family.
157
+ #
158
+ # @param [Chef::Node] node
159
+ #
160
+ # @return [Boolean]
161
+ #
162
+ def suse?(node = __getnode)
163
+ node["platform_family"] == "suse"
164
+ end
165
+
166
+ # Determine if the current node is a member of the gentoo family.
167
+ #
168
+ # @param [Chef::Node] node
169
+ #
170
+ # @return [Boolean]
171
+ #
172
+ def gentoo?(node = __getnode)
173
+ node["platform_family"] == "gentoo"
174
+ end
175
+
176
+ # Determine if the current node is freebsd
177
+ #
178
+ # @param [Chef::Node] node
179
+ #
180
+ # @return [Boolean]
181
+ #
182
+ def freebsd?(node = __getnode)
183
+ node["platform_family"] == "freebsd"
184
+ end
185
+
186
+ # Determine if the current node is openbsd
187
+ #
188
+ # @param [Chef::Node] node
189
+ #
190
+ # @return [Boolean]
191
+ #
192
+ def openbsd?(node = __getnode)
193
+ node["platform_family"] == "openbsd"
194
+ end
195
+
196
+ # Determine if the current node is netbsd
197
+ #
198
+ # @param [Chef::Node] node
199
+ #
200
+ # @return [Boolean]
201
+ #
202
+ def netbsd?(node = __getnode)
203
+ node["platform_family"] == "netbsd"
204
+ end
205
+
206
+ # Determine if the current node is dragonflybsd
207
+ #
208
+ # @param [Chef::Node] node
209
+ #
210
+ # @return [Boolean]
211
+ #
212
+ def dragonflybsd?(node = __getnode)
213
+ node["platform_family"] == "dragonflybsd"
214
+ end
215
+
216
+ # Determine if the current node is a member of the windows family.
217
+ #
218
+ # @param [Chef::Node] node
219
+ #
220
+ # @return [Boolean]
221
+ #
222
+ def windows?(node = __getnode(true))
223
+ # This is all somewhat complicated. We prefer to get the node object so that chefspec can
224
+ # stub the node object. But we also have to deal with class-parsing time where there is
225
+ # no node object, so we have to fall back to RUBY_PLATFORM based detection. We cannot pull
226
+ # the node object out of the Chef.run_context.node global object here (which is what the
227
+ # false flag to __getnode is about) because some run-time code also cannot run under chefspec
228
+ # on non-windows where the node is stubbed to windows.
229
+ #
230
+ # As a result of this the `windows?` helper and the `ChefUtils.windows?` helper do not behave
231
+ # the same way in that the latter is not stubbable by chefspec.
232
+ #
233
+ node ? node["platform_family"] == "windows" : windows_ruby?
234
+ end
235
+
236
+ # Determine if the ruby VM is currently running on a windows node (chefspec can never stub
237
+ # this behavior, so this is useful for code which can never be parsed on a non-windows box).
238
+ #
239
+ # @return [Boolean]
240
+ #
241
+ def windows_ruby?
242
+ !!(RUBY_PLATFORM =~ /mswin|mingw32|windows/)
243
+ end
244
+
245
+ #
246
+ # Platform-Family-like Helpers
247
+ #
248
+ # These are meta-helpers which address the issue that platform_family is single valued and cannot
249
+ # be an array while a tree-like Taxonomy is what is called for in some cases.
250
+ #
251
+
252
+ # If it uses RPM, it goes in here (rhel, fedora, amazon, suse platform_families). Deliberately does not
253
+ # include AIX because bff is AIX's primary package manager and adding it here would make this substantially
254
+ # less useful since in no way can AIX trace its lineage back to old redhat distros. This is most useful for
255
+ # "smells like redhat, including SuSE".
256
+ #
257
+ # @param [Chef::Node] node
258
+ #
259
+ # @return [Boolean]
260
+ #
261
+ def rpm_based?(node = __getnode)
262
+ fedora_derived?(node) || node["platform_family"] == "suse"
263
+ end
264
+
265
+ # RPM-based distros which are not SuSE and are very loosely similar to fedora, using yum or dnf. The historical
266
+ # lineage of the distro should have forked off from old redhat fedora distros at some point. Currently rhel,
267
+ # fedora and amazon. This is most useful for "smells like redhat, but isn't SuSE".
268
+ #
269
+ # @param [Chef::Node] node
270
+ #
271
+ # @return [Boolean]
272
+ #
273
+ def fedora_derived?(node = __getnode)
274
+ redhat_based?(node) || node["platform_family"] == "amazon"
275
+ end
276
+
277
+ # RedHat distros -- fedora and rhel platform_families, nothing else. This is most likely not as useful as the
278
+ # "fedora_dervied?" helper.
279
+ #
280
+ # @param [Chef::Node] node
281
+ #
282
+ # @return [Boolean]
283
+ #
284
+ def redhat_based?(node = __getnode)
285
+ %w{rhel fedora}.include?(node["platform_family"])
286
+ end
287
+
288
+ # All of the Solaris-lineage.
289
+ #
290
+ # @param [Chef::Node] node
291
+ #
292
+ # @return [Boolean]
293
+ #
294
+ def solaris_based?(node = __getnode)
295
+ %w{solaris2 smartos omnios openindiana opensolaris nexentacore}.include?(node["platform"])
296
+ end
297
+
298
+ # All of the BSD-lineage.
299
+ #
300
+ # Note that MacOSX is not included since Mac deviates so significantly from BSD that including it would not be useful.
301
+ #
302
+ # @param [Chef::Node] node
303
+ #
304
+ # @return [Boolean]
305
+ #
306
+ def bsd_based?(node = __getnode)
307
+ # we could use os, platform_family or platform here equally
308
+ %w{netbsd freebsd openbsd dragonflybsd}.include?(node["platform"])
309
+ end
310
+
311
+ extend self
312
+ end
313
+ end
314
+ end
@@ -0,0 +1,91 @@
1
+ #
2
+ # Copyright:: Copyright 2018-2019, Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require_relative "../internal"
19
+ require_relative "train_helpers"
20
+
21
+ module ChefUtils
22
+ module DSL
23
+ # NOTE: these are mixed into the service resource+providers specifically and deliberately not
24
+ # injected into the global namespace
25
+ module Service
26
+ include Internal
27
+ include TrainHelpers
28
+
29
+ # Returns if debian's old rc.d manager is installed (not necessarily the primary init system).
30
+ #
31
+ # @return [Boolean]
32
+ #
33
+ def debianrcd?
34
+ file_exist?("/usr/sbin/update-rc.d")
35
+ end
36
+
37
+ # Returns if debian's old invoke rc.d manager is installed (not necessarily the primary init system).
38
+ #
39
+ # @return [Boolean]
40
+ #
41
+ def invokercd?
42
+ file_exist?("/usr/sbin/invoke-rc.d")
43
+ end
44
+
45
+ # Returns if upstart is installed (not necessarily the primary init system).
46
+ #
47
+ # @return [Boolean]
48
+ #
49
+ def upstart?
50
+ file_exist?("/sbin/initctl")
51
+ end
52
+
53
+ # Returns if insserv is installed (not necessarily the primary init system).
54
+ #
55
+ # @return [Boolean]
56
+ #
57
+ def insserv?
58
+ file_exist?("/sbin/insserv")
59
+ end
60
+
61
+ # Returns if redhat's init system is installed (not necessarily the primary init system).
62
+ #
63
+ # @return [Boolean]
64
+ #
65
+ def redhatrcd?
66
+ file_exist?("/sbin/chkconfig")
67
+ end
68
+
69
+ def service_script_exist?(type, script)
70
+ case type
71
+ when :initd
72
+ file_exist?("/etc/init.d/#{script}")
73
+ when :upstart
74
+ file_exist?("/etc/init/#{script}.conf")
75
+ when :xinetd
76
+ file_exist?("/etc/xinetd.d/#{script}")
77
+ when :etc_rcd
78
+ file_exist?("/etc/rc.d/#{script}")
79
+ when :systemd
80
+ file_exist?("/etc/init.d/#{script}") ||
81
+ ChefUtils::DSL::Introspection.has_systemd_service_unit?(script) ||
82
+ ChefUtils::DSL::Introspection.has_systemd_unit?(script)
83
+ else
84
+ raise ArgumentError, "type of service must be one of :initd, :upstart, :xinetd, :etc_rcd, or :systemd"
85
+ end
86
+ end
87
+
88
+ extend self
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,63 @@
1
+ #--
2
+ # Copyright:: Copyright 2019-2019, Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "stringio" unless defined?(StringIO)
18
+ require_relative "../internal"
19
+
20
+ module ChefUtils
21
+ module DSL
22
+ module TrainHelpers
23
+ include Internal
24
+
25
+ #
26
+ # FIXME: generally these helpers all use the pattern of checking for target_mode?
27
+ # and then if it is we use train. That approach should likely be flipped so that
28
+ # even when we're running without target mode we still use inspec in its local
29
+ # mode.
30
+ #
31
+
32
+ # Train wrapper around File.exist? to make it local mode aware.
33
+ #
34
+ # @param filename filename to check
35
+ # @return [Boolean] if it exists
36
+ #
37
+ def file_exist?(filename)
38
+ if __transport_connection
39
+ __transport_connection.file(filename).exist?
40
+ else
41
+ File.exist?(filename)
42
+ end
43
+ end
44
+
45
+ # XXX: modifications to the StringIO won't get written back
46
+ # FIXME: this is very experimental and may be a bad idea and may break at any time
47
+ # @api private
48
+ #
49
+ def file_open(*args, &block)
50
+ if __transport_connection
51
+ content = __transport_connection.file(args[0]).content
52
+ string_io = StringIO.new content
53
+ yield string_io if block_given?
54
+ string_io
55
+ else
56
+ File.open(*args, &block)
57
+ end
58
+ end
59
+
60
+ extend self
61
+ end
62
+ end
63
+ end