Class: Chef::Provider::Group::Pw

Inherits:
Chef::Provider::Group show all
Defined in:
lib/chef/provider/group/pw.rb

Instance Attribute Summary

Attributes inherited from Chef::Provider::Group

#change_desc, #group_exists

Attributes inherited from Chef::Provider

#action, #current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods inherited from Chef::Provider::Group

#action_create, #action_manage, #action_modify, #action_remove, #compare_group, #initialize, #whyrun_supported?

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #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, #cleanup_after_converge, #cookbook_name, #events, #initialize, #node, #process_resource_requirements, #requirements, #resource_collection, #run_action, #set_updated_status, #whyrun_mode?, #whyrun_supported?

Methods included from DSL::Recipe

#method_missing

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Provider::Group

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::DSL::Recipe

Instance Method Details

#create_groupObject

Create the group



39
40
41
42
43
44
# File 'lib/chef/provider/group/pw.rb', line 39

def create_group
  command = "pw groupadd"
  command << set_options
  command << set_members_option
  run_command(:command => command)
end

#define_resource_requirementsObject



28
29
30
31
32
33
34
35
36
# File 'lib/chef/provider/group/pw.rb', line 28

def define_resource_requirements
  super

  requirements.assert(:all_actions) do |a| 
    a.assertion { ::File.exists?("/usr/sbin/pw") } 
    a.failure_message Chef::Exceptions::Group, "Could not find binary /usr/sbin/pw for #{@new_resource}"
    # No whyrun alternative: this component should be available in the base install of any given system that uses it
  end
end

#load_current_resourceObject



24
25
26
# File 'lib/chef/provider/group/pw.rb', line 24

def load_current_resource
  super
end

#manage_groupObject

Manage the group when it already exists



47
48
49
50
51
52
# File 'lib/chef/provider/group/pw.rb', line 47

def manage_group
  command = "pw groupmod"
  command << set_options
  command << set_members_option
  run_command(:command => command)
end

#remove_groupObject

Remove the group



55
56
57
# File 'lib/chef/provider/group/pw.rb', line 55

def remove_group
  run_command(:command => "pw groupdel #{@new_resource.group_name}")
end

#set_members_optionObject

Set the membership option depending on the current resource states



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef/provider/group/pw.rb', line 73

def set_members_option
  opt = ""
  unless @new_resource.members.empty?
    opt << " -M #{@new_resource.members.join(',')}"
    Chef::Log.debug("#{@new_resource} setting group members to #{@new_resource.members.join(', ')}")
  else
    # New member list is empty so we should delete any old group members
    unless @current_resource.members.empty?
      opt << " -d #{@current_resource.members.join(',')}"
      Chef::Log.debug("#{@new_resource} removing group members #{@current_resource.members.join(', ')}")
    else
      Chef::Log.debug("#{@new_resource} not changing group members, the group has no members")
    end
  end
  opt
end

#set_optionsObject

Little bit of magic as per Adam’s useradd provider to pull and assign the command line flags

Returns

<string>

A string containing the option and then the quoted value



63
64
65
66
67
68
69
70
# File 'lib/chef/provider/group/pw.rb', line 63

def set_options
  opts = " #{@new_resource.group_name}"
  if @new_resource.gid && (@current_resource.gid != @new_resource.gid)
    Chef::Log.debug("#{@new_resource}: current gid (#{@current_resource.gid}) doesnt match target gid (#{@new_resource.gid}), changing it")
    opts << " -g '#{@new_resource.gid}'"
  end
  opts
end