Class: Chef::Provider::OsxProfile

Inherits:
Chef::Provider show all
Includes:
Mixin::Command
Defined in:
lib/chef/provider/osx_profile.rb

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary

Attributes inherited from Chef::Provider

#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_and_return_stdout_stderr, #run_command_with_systems_locale

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, #check_resource_semantics!, #cleanup_after_converge, #converge_by, #converge_if_changed, #events, include_resource_dsl, include_resource_dsl_module, #initialize, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use_inline_resources, #whyrun_mode?

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited

Methods included from DeprecatedLWRPClass

#const_missing, #register_deprecated_lwrp_class

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Constructor Details

This class inherits a constructor from Chef::Provider

Instance Method Details

#action_installObject



102
103
104
105
106
107
108
109
110
# File 'lib/chef/provider/osx_profile.rb', line 102

def action_install
  unless profile_installed?
    converge_by("install profile #{@new_profile_identifier}") do
      profile_path = write_profile_to_disk
      install_profile(profile_path)
      get_installed_profiles(true)
    end
  end
end

#action_removeObject



112
113
114
115
116
117
118
119
120
# File 'lib/chef/provider/osx_profile.rb', line 112

def action_remove
  # Clean up profile after removing it
  if profile_installed?
    converge_by("remove profile #{@new_profile_identifier}") do
      remove_profile
      get_installed_profiles(true)
    end
  end
end

#cache_cookbook_profile(cookbook_file) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/chef/provider/osx_profile.rb', line 148

def cache_cookbook_profile(cookbook_file)
  Chef::FileCache.create_cache_path(
    ::File.join(
      "profiles",
      @new_resource.cookbook_name,
      ::File.dirname(cookbook_file)
    )
  )
  remote_file = Chef::Resource::CookbookFile.new(
    ::File.join(
      get_cache_dir,
      "#{cookbook_file}.remote"
    ),
    run_context
  )
  remote_file.cookbook_name = @new_resource.cookbook_name
  remote_file.source(cookbook_file)
  remote_file.backup(false)
  remote_file.run_action(:create)
  remote_file.path
end

#config_uuid(profile) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/chef/provider/osx_profile.rb', line 178

def config_uuid(profile)
  # Make a UUID of the profile contents and return as string
  UUIDTools::UUID.sha1_create(
    UUIDTools::UUID_DNS_NAMESPACE,
    profile.to_s
  ).to_s
end

#cookbook_file_available?(cookbook_file) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
139
140
# File 'lib/chef/provider/osx_profile.rb', line 136

def cookbook_file_available?(cookbook_file)
  run_context.has_cookbook_file_in_cookbook?(
    @new_resource.cookbook_name, cookbook_file
  )
end

#define_resource_requirementsObject



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
# File 'lib/chef/provider/osx_profile.rb', line 67

def define_resource_requirements
  requirements.assert(:remove) do |a|
    if @new_profile_identifier
      a.assertion {
        !@new_profile_identifier.nil? and
          !@new_profile_identifier.end_with?(".mobileconfig") and
          /^\w+(?:\.\w+)+$/.match(@new_profile_identifier)
      }
      a.failure_message RuntimeError, "when removing using the identifier attribute, it must match the profile identifier"
    else
      new_profile_name = @new_resource.profile_name
      a.assertion {
        !new_profile_name.end_with?(".mobileconfig") and
          /^\w+(?:\.\w+)+$/.match(new_profile_name)
      }
      a.failure_message RuntimeError, "When removing by resource name, it must match the profile identifier "
    end
  end

  requirements.assert(:install) do |a|
    if @new_profile_hash.is_a?(Hash)
      a.assertion {
        @new_profile_hash.include?("PayloadIdentifier")
      }
      a.failure_message RuntimeError, "The specified profile does not seem to be valid"
    end
    if @new_profile_hash.is_a?(String)
      a.assertion {
        @new_profile_hash.end_with?(".mobileconfig")
      }
      a.failure_message RuntimeError, "#{new_profile_hash}' is not a valid profile"
    end
  end
end

#generate_tempfileObject



227
228
229
# File 'lib/chef/provider/osx_profile.rb', line 227

def generate_tempfile
  tempfile = ::Dir::Tmpname.create("allprofiles.plist") {}
end

#get_cache_dirObject



142
143
144
145
146
# File 'lib/chef/provider/osx_profile.rb', line 142

def get_cache_dir
  cache_dir = Chef::FileCache.create_cache_path(
    "profiles/#{@new_resource.cookbook_name}"
  )
end

#get_installed_profiles(update = nil) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/chef/provider/osx_profile.rb', line 208

def get_installed_profiles(update = nil)
  if update
    node.run_state[:config_profiles] = query_installed_profiles
  else
    node.run_state[:config_profiles] ||= query_installed_profiles
  end
end

#get_profile_hash(new_profile) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/chef/provider/osx_profile.rb', line 170

def get_profile_hash(new_profile)
  if new_profile.is_a?(Hash)
    return new_profile
  elsif new_profile.is_a?(String)
    return load_profile_hash(new_profile)
  end
end

#install_profile(profile_path) ⇒ Object



194
195
196
197
198
199
# File 'lib/chef/provider/osx_profile.rb', line 194

def install_profile(profile_path)
  cmd = "profiles -I -F '#{profile_path}'"
  Chef::Log.debug("cmd: #{cmd}")
  shellout_results = shell_out(cmd)
  shellout_results.exitstatus
end

#load_current_resourceObject



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
# File 'lib/chef/provider/osx_profile.rb', line 36

def load_current_resource
  @current_resource = Chef::Resource::OsxProfile.new(@new_resource.name)
  @current_resource.profile_name(@new_resource.profile_name)

  all_profiles = get_installed_profiles
  @new_resource.profile(
    @new_resource.profile ||
    @new_resource.profile_name
  )

  @new_profile_hash = get_profile_hash(@new_resource.profile)
  @new_profile_hash["PayloadUUID"] =
    config_uuid(@new_profile_hash) if @new_profile_hash

  if @new_profile_hash
    @new_profile_identifier = @new_profile_hash["PayloadIdentifier"]
  else
    @new_profile_identifier = @new_resource.identifier ||
      @new_resource.profile_name
  end

  if all_profiles.empty?
    current_profile = nil
  else
    current_profile = all_profiles["_computerlevel"].find do |item|
      item["ProfileIdentifier"] == @new_profile_identifier
    end
  end
  @current_resource.profile(current_profile)
end

#load_profile_hash(new_profile) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/chef/provider/osx_profile.rb', line 122

def load_profile_hash(new_profile)
  # file must exist in cookbook
  if new_profile.end_with?(".mobileconfig")
    unless cookbook_file_available?(new_profile)
      error_string = "#{self}: '#{new_profile}' not found in cookbook"
      raise Chef::Exceptions::FileNotFound, error_string
    end
    cookbook_profile = cache_cookbook_profile(new_profile)
    return read_plist(cookbook_profile)
  else
    return nil
  end
end

#profile_installed?Boolean

Returns:

  • (Boolean)


240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/chef/provider/osx_profile.rb', line 240

def profile_installed?
  # Profile Identifier and UUID must match a currently installed profile
  if @current_resource.profile.nil? or @current_resource.profile.empty?
    false
  else
    if @new_resource.action.include?(:remove)
      true
    else
      @current_resource.profile["ProfileUUID"] ==
        @new_profile_hash["PayloadUUID"]
    end
  end
end

#query_installed_profilesObject



216
217
218
219
220
221
222
223
224
225
# File 'lib/chef/provider/osx_profile.rb', line 216

def query_installed_profiles
  # Dump all profile metadata to a tempfile
  tempfile = generate_tempfile
  write_installed_profiles(tempfile)
  installed_profiles = read_plist(tempfile)
  Chef::Log.debug("Saved profiles to run_state")
  # Clean up the temp file as we do not need it anymore
  ::File.unlink(tempfile)
  installed_profiles
end

#read_plist(xml_file) ⇒ Object



236
237
238
# File 'lib/chef/provider/osx_profile.rb', line 236

def read_plist(xml_file)
  Plist.parse_xml(xml_file)
end

#remove_profileObject



201
202
203
204
205
206
# File 'lib/chef/provider/osx_profile.rb', line 201

def remove_profile
  cmd = "profiles -R -p '#{@new_profile_identifier}'"
  Chef::Log.debug("cmd: #{cmd}")
  shellout_results = shell_out(cmd)
  shellout_results.exitstatus
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/chef/provider/osx_profile.rb', line 32

def whyrun_supported?
  true
end

#write_installed_profiles(tempfile) ⇒ Object



231
232
233
234
# File 'lib/chef/provider/osx_profile.rb', line 231

def write_installed_profiles(tempfile)
  cmd = "profiles -P -o '#{tempfile}'"
  shell_out!(cmd)
end

#write_profile_to_diskObject



186
187
188
189
190
191
192
# File 'lib/chef/provider/osx_profile.rb', line 186

def write_profile_to_disk
  @new_resource.path(Chef::FileCache.create_cache_path("profiles"))
  tempfile = Chef::FileContentManagement::Tempfile.new(@new_resource).tempfile
  tempfile.write(@new_profile_hash.to_plist)
  tempfile.close
  tempfile.path
end