Class: Chef::Provider::Group

Inherits:
Chef::Provider show all
Includes:
Mixin::ShellOut
Defined in:
lib/chef/provider/group.rb,
lib/chef/provider/group/pw.rb,
lib/chef/provider/group/aix.rb,
lib/chef/provider/group/dscl.rb,
lib/chef/provider/group/suse.rb,
lib/chef/provider/group/gpasswd.rb,
lib/chef/provider/group/usermod.rb,
lib/chef/provider/group/windows.rb,
lib/chef/provider/group/groupadd.rb,
lib/chef/provider/group/groupmod.rb

Direct Known Subclasses

Dscl, Groupadd, Groupmod, Pw, Windows

Defined Under Namespace

Classes: Aix, Dscl, Gpasswd, Groupadd, Groupmod, Pw, Suse, Usermod, Windows

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

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

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#a_to_s, #clean_array, #shell_out, #shell_out!, #shell_out_compact, #shell_out_compact!, #shell_out_compact_timeout, #shell_out_compact_timeout!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Methods included from Mixin::PathSanity

#enforce_path_sanity, #sanitized_path

Methods inherited from Chef::Provider

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

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

#descendants, #include, #included

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #with_run_context

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 DSL::PlatformIntrospection

#docker?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Constructor Details

#initialize(new_resource, run_context) ⇒ Group

Returns a new instance of Group


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

def initialize(new_resource, run_context)
  super
  @group_exists = true
end

Instance Attribute Details

#change_descObject

Returns the value of attribute change_desc


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

def change_desc
  @change_desc
end

#group_existsObject

Returns the value of attribute group_exists


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

def group_exists
  @group_exists
end

Instance Method Details

#action_createObject


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/chef/provider/group.rb', line 124

def action_create
  case @group_exists
  when false
    converge_by("create group #{new_resource.group_name}") do
      create_group
      Chef::Log.info("#{new_resource} created")
    end
  else
    if compare_group
      converge_by(["alter group #{new_resource.group_name}"] + change_desc) do
        manage_group
        Chef::Log.info("#{new_resource} altered")
      end
    end
  end
end

#action_manageObject


149
150
151
152
153
154
155
# File 'lib/chef/provider/group.rb', line 149

def action_manage
  return unless @group_exists && compare_group
  converge_by(["manage group #{new_resource.group_name}"] + change_desc) do
    manage_group
    Chef::Log.info("#{new_resource} managed")
  end
end

#action_modifyObject


157
158
159
160
161
162
163
# File 'lib/chef/provider/group.rb', line 157

def action_modify
  return unless compare_group
  converge_by(["modify group #{new_resource.group_name}"] + change_desc) do
    manage_group
    Chef::Log.info("#{new_resource} modified")
  end
end

#action_removeObject


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

def action_remove
  return unless @group_exists
  converge_by("remove group #{new_resource.group_name}") do
    remove_group
    Chef::Log.info("#{new_resource} removed")
  end
end

#compare_groupObject

Check to see if a group needs any changes. Populate ==== Returns :: If a change is required :: If a change is not required


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chef/provider/group.rb', line 81

def compare_group
  @change_desc = [ ]
  if new_resource.gid.to_s != current_resource.gid.to_s
    @change_desc << "change gid #{current_resource.gid} to #{new_resource.gid}"
  end

  if new_resource.append
    missing_members = []
    new_resource.members.each do |member|
      next if has_current_group_member?(member)
      validate_member!(member)
      missing_members << member
    end
    unless missing_members.empty?
      @change_desc << "add missing member(s): #{missing_members.join(', ')}"
    end

    members_to_be_removed = []
    new_resource.excluded_members.each do |member|
      if has_current_group_member?(member)
        members_to_be_removed << member
      end
    end
    unless members_to_be_removed.empty?
      @change_desc << "remove existing member(s): #{members_to_be_removed.join(', ')}"
    end
  elsif new_resource.members != current_resource.members
    @change_desc << "replace group members with new list of members"
  end

  !@change_desc.empty?
end

#create_groupObject

Raises:

  • (NotImplementedError)

165
166
167
# File 'lib/chef/provider/group.rb', line 165

def create_group
  raise NotImplementedError, "subclasses of Chef::Provider::Group should define #create_group"
end

#define_resource_requirementsObject


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

def define_resource_requirements
  requirements.assert(:modify) do |a|
    a.assertion { @group_exists }
    a.failure_message(Chef::Exceptions::Group, "Cannot modify #{new_resource} - group does not exist!")
    a.whyrun("Group #{new_resource} does not exist. Unless it would have been created earlier in this run, this attempt to modify it would fail.")
  end

  requirements.assert(:all_actions) do |a|
    # Make sure that the resource doesn't contain any common
    # user names in the members and exclude_members properties.
    if !new_resource.members.nil? && !new_resource.excluded_members.nil?
      common_members = new_resource.members & new_resource.excluded_members
      a.assertion { common_members.empty? }
      a.failure_message(Chef::Exceptions::ConflictingMembersInGroup, "Attempting to both add and remove users from a group: '#{common_members.join(', ')}'")
      # No why-run alternative
    end
  end
end

#has_current_group_member?(member) ⇒ Boolean

Returns:

  • (Boolean)

114
115
116
# File 'lib/chef/provider/group.rb', line 114

def has_current_group_member?(member)
  current_resource.members.include?(member)
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
    @group_exists = false
    Chef::Log.debug("#{new_resource} group does not exist")
  end

  if group_info
    new_resource.gid(group_info.gid) unless new_resource.gid
    current_resource.gid(group_info.gid)
    current_resource.members(group_info.mem)
  end

  current_resource
end

#manage_groupObject

Raises:

  • (NotImplementedError)

169
170
171
# File 'lib/chef/provider/group.rb', line 169

def manage_group
  raise NotImplementedError, "subclasses of Chef::Provider::Group should define #manage_group"
end

#remove_groupObject

Raises:

  • (NotImplementedError)

173
174
175
# File 'lib/chef/provider/group.rb', line 173

def remove_group
  raise NotImplementedError, "subclasses of Chef::Provider::Group should define #remove_group"
end

#validate_member!(member) ⇒ Object


118
119
120
121
122
# File 'lib/chef/provider/group.rb', line 118

def validate_member!(member)
  # Sub-classes can do any validation if needed
  # and raise an error if validation fails
  true
end