Class: Chef::Platform

Inherits:
Object show all
Extended by:
Mixin::ParamsValidate
Defined in:
lib/chef/platform.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Mixin::ParamsValidate

set_or_return, validate

Class Attribute Details

.platformsObject



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
# File 'lib/chef/platform.rb', line 40

def platforms
  @platforms ||= {
    :mac_os_x => {
      :default => {
        :package => Chef::Provider::Package::Macports,
        :service => Chef::Provider::Service::Macosx,
        :user => Chef::Provider::User::Dscl,
        :group => Chef::Provider::Group::Dscl
      }
    },
    :mac_os_x_server => {
      :default => {
        :package => Chef::Provider::Package::Macports,
        :service => Chef::Provider::Service::Macosx,
        :user => Chef::Provider::User::Dscl,
        :group => Chef::Provider::Group::Dscl
      }
    },
    :freebsd => {
      :default => {
        :group   => Chef::Provider::Group::Pw,
        :package => Chef::Provider::Package::Freebsd,
        :service => Chef::Provider::Service::Freebsd,
        :user    => Chef::Provider::User::Pw,
        :cron    => Chef::Provider::Cron
      }
    },
    :ubuntu   => {
      :default => {
        :package => Chef::Provider::Package::Apt,
        :service => Chef::Provider::Service::Debian,
        :cron => Chef::Provider::Cron,
        :mdadm => Chef::Provider::Mdadm
      }
    },
    :linaro   => {
      :default => {
        :package => Chef::Provider::Package::Apt,
        :service => Chef::Provider::Service::Debian,
        :cron => Chef::Provider::Cron,
        :mdadm => Chef::Provider::Mdadm
      }
    },
    :raspbian   => {
      :default => {
        :package => Chef::Provider::Package::Apt,
        :service => Chef::Provider::Service::Debian,
        :cron => Chef::Provider::Cron,
        :mdadm => Chef::Provider::Mdadm
      }
    },
    :linuxmint   => {
      :default => {
        :package => Chef::Provider::Package::Apt,
        :service => Chef::Provider::Service::Upstart,
        :cron => Chef::Provider::Cron,
        :mdadm => Chef::Provider::Mdadm
      }
    },
    :debian => {
      :default => {
        :package => Chef::Provider::Package::Apt,
        :service => Chef::Provider::Service::Debian,
        :cron => Chef::Provider::Cron,
        :mdadm => Chef::Provider::Mdadm
      },
      "6.0" => {
        :service => Chef::Provider::Service::Insserv
      }
    },
    :xenserver   => {
      :default => {
        :service => Chef::Provider::Service::Redhat,
        :cron => Chef::Provider::Cron,
        :package => Chef::Provider::Package::Yum,
        :mdadm => Chef::Provider::Mdadm
      }
    },
    :centos   => {
      :default => {
        :service => Chef::Provider::Service::Redhat,
        :cron => Chef::Provider::Cron,
        :package => Chef::Provider::Package::Yum,
        :mdadm => Chef::Provider::Mdadm
      }
    },
    :amazon   => {
      :default => {
        :service => Chef::Provider::Service::Redhat,
        :cron => Chef::Provider::Cron,
        :package => Chef::Provider::Package::Yum,
        :mdadm => Chef::Provider::Mdadm
      }
    },
    :scientific => {
      :default => {
        :service => Chef::Provider::Service::Redhat,
        :cron => Chef::Provider::Cron,
        :package => Chef::Provider::Package::Yum,
        :mdadm => Chef::Provider::Mdadm
      }
    },
    :fedora   => {
      :default => {
        :service => Chef::Provider::Service::Redhat,
        :cron => Chef::Provider::Cron,
        :package => Chef::Provider::Package::Yum,
        :mdadm => Chef::Provider::Mdadm
      }
    },
    :suse     => {
      :default => {
        :service => Chef::Provider::Service::Redhat,
        :cron => Chef::Provider::Cron,
        :package => Chef::Provider::Package::Zypper,
        :group => Chef::Provider::Group::Suse
      }
    },
    :oracle  => {
      :default => {
        :service => Chef::Provider::Service::Redhat,
        :cron => Chef::Provider::Cron,
        :package => Chef::Provider::Package::Yum,
        :mdadm => Chef::Provider::Mdadm
      }
    },
    :redhat   => {
      :default => {
        :service => Chef::Provider::Service::Redhat,
        :cron => Chef::Provider::Cron,
        :package => Chef::Provider::Package::Yum,
        :mdadm => Chef::Provider::Mdadm
      }
    },
    :gentoo   => {
      :default => {
        :package => Chef::Provider::Package::Portage,
        :service => Chef::Provider::Service::Gentoo,
        :cron => Chef::Provider::Cron,
        :mdadm => Chef::Provider::Mdadm
      }
    },
    :arch   => {
      :default => {
        :package => Chef::Provider::Package::Pacman,
        :service => Chef::Provider::Service::Arch,
        :cron => Chef::Provider::Cron,
        :mdadm => Chef::Provider::Mdadm
      }
    },
    :mswin => {
      :default => {
        :env =>  Chef::Provider::Env::Windows,
        :service => Chef::Provider::Service::Windows,
        :user => Chef::Provider::User::Windows,
        :group => Chef::Provider::Group::Windows,
        :mount => Chef::Provider::Mount::Windows
      }
    },
    :mingw32 => {
      :default => {
        :env =>  Chef::Provider::Env::Windows,
        :service => Chef::Provider::Service::Windows,
        :user => Chef::Provider::User::Windows,
        :group => Chef::Provider::Group::Windows,
        :mount => Chef::Provider::Mount::Windows
      }
    },
    :windows => {
      :default => {
        :env =>  Chef::Provider::Env::Windows,
        :service => Chef::Provider::Service::Windows,
        :user => Chef::Provider::User::Windows,
        :group => Chef::Provider::Group::Windows,
        :mount => Chef::Provider::Mount::Windows
      }
    },
    :solaris  => {},
    :openindiana => {
      :default => {
        :service => Chef::Provider::Service::Solaris,
        :package => Chef::Provider::Package::Ips,
        :cron => Chef::Provider::Cron::Solaris,
        :group => Chef::Provider::Group::Usermod
      }
    },
    :opensolaris => {
      :default => {
        :service => Chef::Provider::Service::Solaris,
        :package => Chef::Provider::Package::Ips,
        :cron => Chef::Provider::Cron::Solaris,
        :group => Chef::Provider::Group::Usermod
      }
    },
    :nexentacore => {
      :default => {
        :service => Chef::Provider::Service::Solaris,
        :package => Chef::Provider::Package::Solaris,
        :cron => Chef::Provider::Cron::Solaris,
        :group => Chef::Provider::Group::Usermod
      }
    },
    :omnios => {
      :default => {
        :service => Chef::Provider::Service::Solaris,
        :package => Chef::Provider::Package::Ips,
        :cron => Chef::Provider::Cron::Solaris,
        :group => Chef::Provider::Group::Usermod
      }
    },
    :solaris2 => {
      :default => {
        :service => Chef::Provider::Service::Solaris,
        :package => Chef::Provider::Package::Ips,
        :cron => Chef::Provider::Cron::Solaris,
        :group => Chef::Provider::Group::Usermod
      },
      "5.9" => {
        :service => Chef::Provider::Service::Solaris,
        :package => Chef::Provider::Package::Solaris,
        :cron => Chef::Provider::Cron::Solaris,
        :group => Chef::Provider::Group::Usermod
      },
      "5.10" => {
        :service => Chef::Provider::Service::Solaris,
        :package => Chef::Provider::Package::Solaris,
        :cron => Chef::Provider::Cron::Solaris,
        :group => Chef::Provider::Group::Usermod
      }
    },
    :smartos => {
      :default => {
        :service => Chef::Provider::Service::Solaris,
        :package => Chef::Provider::Package::SmartOS,
        :cron => Chef::Provider::Cron::Solaris,
        :group => Chef::Provider::Group::Usermod
      }
    },
    :netbsd => {
      :default => {
        :service => Chef::Provider::Service::Freebsd,
        :group => Chef::Provider::Group::Groupmod
      }
    },
    :openbsd => {
      :default => {
        :group => Chef::Provider::Group::Usermod
      }
    },
    :hpux => {
      :default => {
        :group => Chef::Provider::Group::Usermod
      }
    },
    :aix => {
      :default => {
        :group => Chef::Provider::Group::Aix
      }
    },
    :default  => {
      :file => Chef::Provider::File,
      :directory => Chef::Provider::Directory,
      :link => Chef::Provider::Link,
      :template => Chef::Provider::Template,
      :remote_directory => Chef::Provider::RemoteDirectory,
      :execute => Chef::Provider::Execute,
      :mount => Chef::Provider::Mount::Mount,
      :script => Chef::Provider::Script,
      :service => Chef::Provider::Service::Init,
      :perl => Chef::Provider::Script,
      :python => Chef::Provider::Script,
      :ruby => Chef::Provider::Script,
      :bash => Chef::Provider::Script,
      :csh => Chef::Provider::Script,
      :user => Chef::Provider::User::Useradd,
      :group => Chef::Provider::Group::Gpasswd,
      :http_request => Chef::Provider::HttpRequest,
      :route => Chef::Provider::Route,
      :ifconfig => Chef::Provider::Ifconfig,
      :ruby_block => Chef::Provider::RubyBlock,
      :erl_call => Chef::Provider::ErlCall,
      :log => Chef::Provider::Log::ChefLog
    }
  }
end

Class Method Details

.find(name, version) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/chef/platform.rb', line 328

def find(name, version)
  provider_map = platforms[:default].clone

  name_sym = name
  if name.kind_of?(String)
    name.downcase!
    name.gsub!(/\s/, "_")
    name_sym = name.to_sym
  end

  if platforms.has_key?(name_sym)
    if platforms[name_sym].has_key?(version)
      Chef::Log.debug("Platform #{name.to_s} version #{version} found")
      if platforms[name_sym].has_key?(:default)
        provider_map.merge!(platforms[name_sym][:default])
      end
      provider_map.merge!(platforms[name_sym][version])
    elsif platforms[name_sym].has_key?(:default)
      provider_map.merge!(platforms[name_sym][:default])
    end
  else
    Chef::Log.debug("Platform #{name} not found, using all defaults. (Unsupported platform?)")
  end
  provider_map
end

.find_platform_and_version(node) ⇒ Object

Raises:

  • (ArgumentError)


354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/chef/platform.rb', line 354

def find_platform_and_version(node)
  platform = nil
  version = nil

  if node[:platform]
    platform = node[:platform]
  elsif node.attribute?("os")
    platform = node[:os]
  else
    platform = :generic
  end

  raise ArgumentError, "Cannot find a platform for #{node}" unless platform

  if node[:platform_version]
    version = node[:platform_version]
  elsif node[:os_version]
    version = node[:os_version]
  elsif node[:os_release]
    version = node[:os_release]
  else
    version = "0.0.1"
  end

  raise ArgumentError, "Cannot find a version for #{node}" unless version

  return platform, version
end

.find_provider(platform, version, resource_type) ⇒ Object

Raises:

  • (ArgumentError)


464
465
466
467
468
469
470
471
472
473
# File 'lib/chef/platform.rb', line 464

def find_provider(platform, version, resource_type)
  pmap = Chef::Platform.find(platform, version)
  provider_klass = explicit_provider(platform, version, resource_type) ||
                   platform_provider(platform, version, resource_type) ||
                   resource_matching_provider(platform, version, resource_type)

  raise ArgumentError, "Cannot find a provider for #{resource_type} on #{platform} version #{version}" if provider_klass.nil?

  provider_klass
end

.find_provider_for_node(node, resource_type) ⇒ Object



396
397
398
399
# File 'lib/chef/platform.rb', line 396

def find_provider_for_node(node, resource_type)
  platform, version = find_platform_and_version(node)
  provider = find_provider(platform, version, resource_type)
end

.provider_for_node(node, resource_type) ⇒ Object

Raises:

  • (NotImplementedError)


391
392
393
394
# File 'lib/chef/platform.rb', line 391

def provider_for_node(node, resource_type)
  raise NotImplementedError, "#{self.class.name} no longer supports #provider_for_node"
  find_provider_for_node(node, resource_type).new(node, resource_type)
end

.provider_for_resource(resource, action = :nothing) ⇒ Object

Raises:

  • (ArgumentError)


383
384
385
386
387
388
389
# File 'lib/chef/platform.rb', line 383

def provider_for_resource(resource, action=:nothing)
  node = resource.run_context && resource.run_context.node
  raise ArgumentError, "Cannot find the provider for a resource with no run context set" unless node
  provider = find_provider_for_node(node, resource).new(resource, resource.run_context)
  provider.action = action
  provider
end

.set(args) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/chef/platform.rb', line 401

def set(args)
  validate(
    args,
    {
      :platform => {
        :kind_of => Symbol,
        :required => false,
      },
      :version => {
        :kind_of => String,
        :required => false,
      },
      :resource => {
        :kind_of => Symbol,
      },
      :provider => {
        :kind_of => [ String, Symbol, Class ],
      }
    }
  )
  if args.has_key?(:platform)
    if args.has_key?(:version)
      if platforms.has_key?(args[:platform])
        if platforms[args[:platform]].has_key?(args[:version])
          platforms[args[:platform]][args[:version]][args[:resource].to_sym] = args[:provider]
        else
          platforms[args[:platform]][args[:version]] = {
            args[:resource].to_sym => args[:provider]
          }
        end
      else
        platforms[args[:platform]] = {
          args[:version] => {
            args[:resource].to_sym => args[:provider]
          }
        }
      end
    else
      if platforms.has_key?(args[:platform])
        if platforms[args[:platform]].has_key?(:default)
          platforms[args[:platform]][:default][args[:resource].to_sym] = args[:provider]
        else
          platforms[args[:platform]] = { :default => { args[:resource].to_sym => args[:provider] } }
        end
      else
        platforms[args[:platform]] = {
          :default => {
            args[:resource].to_sym => args[:provider]
          }
        }
      end
    end
  else
    if platforms.has_key?(:default)
      platforms[:default][args[:resource].to_sym] = args[:provider]
    else
      platforms[:default] = {
        args[:resource].to_sym => args[:provider]
      }
    end
  end
end

.windows?Boolean

Returns:

  • (Boolean)


475
476
477
478
479
480
481
# File 'lib/chef/platform.rb', line 475

def windows?
  if RUBY_PLATFORM =~ /mswin|mingw|windows/
    true
  else
    false
  end
end

.windows_server_2003?Boolean

Returns:

  • (Boolean)


483
484
485
486
487
488
489
490
# File 'lib/chef/platform.rb', line 483

def windows_server_2003?
  return false unless windows?

  require 'ruby-wmi'

  host = WMI::Win32_OperatingSystem.find(:first)
  (host.version && host.version.start_with?("5.2"))
end