Class: Chef::Provider::Group::Groupmod

Inherits:
Chef::Provider::Group show all
Includes:
Mixin::ShellOut
Defined in:
lib/chef/provider/group/groupmod.rb

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

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 included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!

Methods inherited from Chef::Provider::Group

#action_create, #action_manage, #action_modify, #action_remove, #compare_group, #define_resource_requirements, #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::Unix

#popen4

Methods included from Mixin::Command::Windows

#popen4

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #cleanup_after_converge, #converge, #cookbook_name, #define_resource_requirements, #events, #initialize, #node, #process_resource_requirements, #requirements, #resource_collection, #run_action, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::EnforceOwnershipAndPermissions

#access_controls, #enforce_ownership_and_permissions

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

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

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

#add_group_members(members) ⇒ Object

Adds a list of usernames to the group using `user mod`


95
96
97
98
99
100
# File 'lib/chef/provider/group/groupmod.rb', line 95

def add_group_members(members)
  Chef::Log.debug("#{@new_resource} adding members #{members.join(', ')}") if !members.empty?
  members.each do |user|
    shell_out!("user mod -G #{@new_resource.group_name} #{user}")
  end
end

#create_groupObject

Create the group


36
37
38
39
40
41
42
# File 'lib/chef/provider/group/groupmod.rb', line 36

def create_group
  command = "group add"
  command << set_options
  shell_out!(command)

  add_group_members(@new_resource.members)
end

#load_current_resourceObject


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

def load_current_resource
  super
  [ "group", "user" ].each do |binary|
    raise Chef::Exceptions::Group, "Could not find binary /usr/sbin/#{binary} for #{@new_resource}" unless ::File.exists?("/usr/sbin/#{binary}")
  end
end

#manage_groupObject

Manage the group when it already exists


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef/provider/group/groupmod.rb', line 45

def manage_group
  if @new_resource.append
    to_add = @new_resource.members.dup
    to_add.reject! { |user| @current_resource.members.include?(user) }

    to_delete = Array.new

    Chef::Log.debug("#{@new_resource} not changing group members, the group has no members to add") if to_add.empty?
  else
    to_add = @new_resource.members.dup
    to_add.reject! { |user| @current_resource.members.include?(user) }

    to_delete = @current_resource.members.dup
    to_delete.reject! { |user| @new_resource.members.include?(user) }

    Chef::Log.debug("#{@new_resource} setting group members to: none") if @new_resource.members.empty?
  end

  if to_delete.empty?
    # If we are only adding new members to this group, then
    # call add_group_members with only those users
    add_group_members(to_add)
  else
    Chef::Log.debug("#{@new_resource} removing members #{to_delete.join(', ')}")

    # This is tricky, but works: rename the existing group to
    # "<name>_bak", create a new group with the same GID and
    # "<name>", then set correct members on that group
    rename = "group mod -n #{@new_resource.group_name}_bak #{@new_resource.group_name}"
    shell_out!(rename)

    create = "group add"
    create << set_options(:overwrite_gid => true)
    shell_out!(create)

    # Ignore to_add here, since we're replacing the group we
    # have to add all members who should be in the group.
    add_group_members(@new_resource.members)

    remove = "group del #{@new_resource.group_name}_bak"
    shell_out!(remove)
  end
end

#remove_groupObject

Remove the group


90
91
92
# File 'lib/chef/provider/group/groupmod.rb', line 90

def remove_group
  shell_out!("group del #{@new_resource.group_name}")
end

#set_options(overwrite_gid = false) ⇒ Object

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


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

def set_options(overwrite_gid=false)
  opts = ""
  if overwrite_gid || @new_resource.gid && (@current_resource.gid != @new_resource.gid)
    opts << " -g '#{@new_resource.gid}'"
  end
  if overwrite_gid
    opts << " -o"
  end
  opts << " #{@new_resource.group_name}"
  opts
end