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

#group_exists

Attributes inherited from Chef::Provider

#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

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, build_from_file, #cookbook_name, #initialize, #node, #resource_collection

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #search, #value_for_platform

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::Mixin::RecipeDefinitionDSLCore

Instance Method Details

#create_groupObject

Create the group



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

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

#load_current_resourceObject



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

def load_current_resource
  super
  raise Chef::Exceptions::Group, "Could not find binary /usr/sbin/pw for #{@new_resource}" unless ::File.exists?("/usr/sbin/pw")
end

#manage_groupObject

Manage the group when it already exists



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

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

#remove_groupObject

Remove the group



46
47
48
# File 'lib/chef/provider/group/pw.rb', line 46

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chef/provider/group/pw.rb', line 64

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



54
55
56
57
58
59
60
61
# File 'lib/chef/provider/group/pw.rb', line 54

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