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, #node

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

handle_command_failures, not_if, only_if, output_of_command, popen4, run_command, run_command_with_systems_locale

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #initialize

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chef/provider/group/pw.rb', line 68

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
62
63
64
65
# File 'lib/chef/provider/group/pw.rb', line 54

def set_options
  opts = " #{@new_resource.group_name}"
  { :gid => "-g" }.sort { |a,b| a[0] <=> b[0] }.each do |field, option|
    if @current_resource.send(field) != @new_resource.send(field)
      if @new_resource.send(field)
        Chef::Log.debug("#{@new_resource}: setting #{field.to_s} to #{@new_resource.send(field)}")
        opts << " #{option} '#{@new_resource.send(field)}'"
      end
    end
  end
  opts
end