Class: Chef::Provider::Group

Inherits:
Chef::Provider show all
Includes:
Mixin::Command
Defined in:
lib/chef/provider/group.rb,
lib/chef/provider/group/pw.rb,
lib/chef/provider/group/dscl.rb,
lib/chef/provider/group/gpasswd.rb,
lib/chef/provider/group/usermod.rb,
lib/chef/provider/group/groupadd.rb

Direct Known Subclasses

Dscl, Groupadd, Pw

Defined Under Namespace

Classes: Dscl, Gpasswd, Groupadd, Pw, Usermod

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #node

Instance Method Summary collapse

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

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

#initialize(node, new_resource, collection = nil, definitions = nil, cookbook_loader = nil) ⇒ Group

Returns a new instance of Group.



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

def initialize(node, new_resource, collection=nil, definitions=nil, cookbook_loader=nil)
  super(node, new_resource, collection, definitions, cookbook_loader)
  @group_exists = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Attribute Details

#group_existsObject

Returns the value of attribute group_exists.



28
29
30
# File 'lib/chef/provider/group.rb', line 28

def group_exists
  @group_exists
end

Instance Method Details

#action_createObject



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

def action_create
  case @group_exists
  when false
    create_group
    Chef::Log.info("Created #{@new_resource}")
    @new_resource.updated = true
  else 
    if compare_group
      manage_group
      Chef::Log.info("Altered #{@new_resource}")
      @new_resource.updated = true
    end
  end
end

#action_manageObject



99
100
101
102
103
104
105
# File 'lib/chef/provider/group.rb', line 99

def action_manage
  if @group_exists && compare_group
    manage_group 
    @new_resource.updated = true
    Chef::Log.info("Managed #{@new_resource}")
  end
end

#action_modifyObject



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/chef/provider/group.rb', line 107

def action_modify
  if @group_exists 
    if compare_group
      manage_group
      @new_resource.updated = true
      Chef::Log.info("Modified #{@new_resource}")
    end
  else
    raise Chef::Exceptions::Group, "Cannot modify #{@new_resource} - group does not exist!"
  end
end

#action_removeObject



91
92
93
94
95
96
97
# File 'lib/chef/provider/group.rb', line 91

def action_remove
  if @group_exists
    remove_group
    @new_resource.updated = true
    Chef::Log.info("Removed #{@new_resource}")
  end
end

#compare_groupObject

Check to see if a group needs any changes

Returns

<true>

If a change is required

<false>

If a change is not required



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/chef/provider/group.rb', line 61

def compare_group
  return true if @new_resource.gid != @current_resource.gid

  if(@new_resource.append)
    @new_resource.members.each do |member|
      next if @current_resource.members.include?(member)
      return true
    end
  else
    return true if @new_resource.members != @current_resource.members
  end

  return false
end

#load_current_resourceObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef/provider/group.rb', line 35

def load_current_resource
  @current_resource = Chef::Resource::Group.new(@new_resource.name)
  @current_resource.group_name(@new_resource.group_name)
  
  group_info = nil
  begin
    group_info = Etc.getgrnam(@new_resource.group_name)
  rescue ArgumentError => e
    @group_exists = false
    Chef::Log.debug("#{@new_resource}: group does not exist")
  end
  
  if group_info
    @new_resource.gid(group_info.gid)
    @current_resource.gid(group_info.gid)
    @current_resource.members(group_info.mem)
  end
  
  @current_resource
end