summaryrefslogtreecommitdiff
path: root/chef-utils/lib/chef-utils/dsl/platform_family.rb
blob: 9d0c878cb738e40c224656857fc7c23878fab4e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#
# Copyright:: Copyright 2018-2019, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require_relative "../internal"

module ChefUtils
  module DSL
    module PlatformFamily
      include Internal

      # Determine if the current node is a member of the 'arch' family.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def arch?(node = __getnode)
        node["platform_family"] == "arch"
      end
      # chef-sugar backcompat method
      alias_method :arch_linux?, :arch?

      # Determine if the current node is a member of the 'aix' platform family.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def aix?(node = __getnode)
        node["platform_family"] == "aix"
      end

      # Determine if the current node is a member of the 'debian' platform family (Debian, Ubuntu and derivatives).
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def debian?(node = __getnode)
        node["platform_family"] == "debian"
      end

      # Determine if the current node is a member of the 'fedora' platform family (Fedora and Arista).
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def fedora?(node = __getnode)
        node["platform_family"] == "fedora"
      end

      # Determine if the current node is a member of the 'mac_os_x' platform family.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def macos?(node = __getnode)
        node["platform_family"] == "mac_os_x"
      end
      # chef-sugar backcompat method
      alias_method :osx?, :macos?
      # chef-sugar backcompat method
      alias_method :mac?, :macos?
      # chef-sugar backcompat method
      alias_method :mac_os_x?, :macos?

      # Determine if the current node is a member of the 'rhel' platform family (Red Hat, CentOS, Oracle or Scientific Linux, but NOT Amazon Linux or Fedora).
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def rhel?(node = __getnode)
        node["platform_family"] == "rhel"
      end
      # chef-sugar backcompat method
      alias_method :el?, :rhel?

      # Determine if the current node is a rhel6 compatible build (Red Hat, CentOS, Oracle or Scientific Linux).
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def rhel6?(node = __getnode)
        node["platform_family"] == "rhel" && node["platform_version"].to_f >= 6.0 && node["platform_version"].to_f < 7.0
      end

      # Determine if the current node is a rhel7 compatible build (Red Hat, CentOS, Oracle or Scientific Linux).
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def rhel7?(node = __getnode)
        node["platform_family"] == "rhel" && node["platform_version"].to_f >= 7.0 && node["platform_version"].to_f < 8.0
      end

      # Determine if the current node is a rhel8 compatible build (Red Hat, CentOS, Oracle or Scientific Linux).
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def rhel8?(node = __getnode)
        node["platform_family"] == "rhel" && node["platform_version"].to_f >= 8.0 && node["platform_version"].to_f < 9.0
      end

      # Determine if the current node is a member of the 'amazon' platform family.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def amazon?(node = __getnode)
        node["platform_family"] == "amazon"
      end
      # chef-sugar backcompat method
      alias_method :amazon_linux?, :amazon?

      # Determine if the current node is a member of the 'solaris2' platform family.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def solaris2?(node = __getnode)
        node["platform_family"] == "solaris2"
      end
      # chef-sugar backcompat method
      alias_method :solaris?, :solaris2?

      # Determine if the current node is a member of the 'smartos' platform family.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def smartos?(node = __getnode)
        node["platform_family"] == "smartos"
      end

      # Determine if the current node is a member of the 'suse' platform family (openSUSE, SLES, and SLED).
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def suse?(node = __getnode)
        node["platform_family"] == "suse"
      end

      # Determine if the current node is a member of the 'gentoo' platform family.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def gentoo?(node = __getnode)
        node["platform_family"] == "gentoo"
      end

      # Determine if the current node is a member of the 'freebsd' platform family.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def freebsd?(node = __getnode)
        node["platform_family"] == "freebsd"
      end

      # Determine if the current node is a member of the 'openbsd' platform family.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def openbsd?(node = __getnode)
        node["platform_family"] == "openbsd"
      end

      # Determine if the current node is a member of the 'netbsd' platform family.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def netbsd?(node = __getnode)
        node["platform_family"] == "netbsd"
      end

      # Determine if the current node is a member of the 'dragonflybsd' platform family.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def dragonflybsd?(node = __getnode)
        node["platform_family"] == "dragonflybsd"
      end

      # Determine if the current node is a member of the 'windows' platform family.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def windows?(node = __getnode(true))
        # This is all somewhat complicated.  We prefer to get the node object so that chefspec can
        # stub the node object.  But we also have to deal with class-parsing time where there is
        # no node object, so we have to fall back to RUBY_PLATFORM based detection.  We cannot pull
        # the node object out of the Chef.run_context.node global object here (which is what the
        # false flag to __getnode is about) because some run-time code also cannot run under chefspec
        # on non-windows where the node is stubbed to windows.
        #
        # As a result of this the `windows?` helper and the `ChefUtils.windows?` helper do not behave
        # the same way in that the latter is not stubbable by chefspec.
        #
        node ? node["platform_family"] == "windows" : windows_ruby?
      end

      # Determine if the Ruby VM is currently running on a Windows node (ChefSpec can never stub
      # this behavior, so this is useful for code which can never be parsed on a non-Windows box).
      #
      # @since 15.5
      #
      # @return [Boolean]
      #
      def windows_ruby?
        !!(RUBY_PLATFORM =~ /mswin|mingw32|windows/)
      end

      #
      # Platform-Family-like Helpers
      #
      # These are meta-helpers which address the issue that platform_family is single valued and cannot
      # be an array while a tree-like Taxonomy is what is called for in some cases.
      #

      # If it uses RPM, it goes in here (rhel, fedora, amazon, suse platform_families).  Deliberately does not
      # include AIX because bff is AIX's primary package manager and adding it here would make this substantially
      # less useful since in no way can AIX trace its lineage back to old redhat distros.  This is most useful for
      # "smells like redhat, including SuSE".
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def rpm_based?(node = __getnode)
        fedora_derived?(node) || node["platform_family"] == "suse"
      end

      # RPM-based distros which are not SuSE and are very loosely similar to fedora, using yum or dnf. The historical
      # lineage of the distro should have forked off from old redhat fedora distros at some point. Currently rhel,
      # fedora and amazon. This is most useful for "smells like redhat, but isn't SuSE".
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def fedora_derived?(node = __getnode)
        redhat_based?(node) || node["platform_family"] == "amazon"
      end

      # RedHat distros -- fedora and rhel platform_families, nothing else. This is most likely not as useful as the
      # "fedora_dervied?" helper.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def redhat_based?(node = __getnode)
        %w{rhel fedora}.include?(node["platform_family"])
      end

      # All of the Solaris-lineage.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def solaris_based?(node = __getnode)
        %w{solaris2 smartos omnios openindiana opensolaris nexentacore}.include?(node["platform"])
      end

      # All of the BSD-lineage.
      #
      # Note that macOS is not included since macOS deviates so significantly from BSD that including it would not be useful.
      #
      # @param [Chef::Node] node the node to check
      # @since 15.5
      #
      # @return [Boolean]
      #
      def bsd_based?(node = __getnode)
        # we could use os, platform_family or platform here equally
        %w{netbsd freebsd openbsd dragonflybsd}.include?(node["platform"])
      end

      extend self
    end
  end
end