Class: Chef::Provider::Group::Groupadd

Inherits:
Chef::Provider::Group show all
Defined in:
lib/chef/provider/group/groupadd.rb

Direct Known Subclasses

Gpasswd, Usermod

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



35
36
37
38
39
40
# File 'lib/chef/provider/group/groupadd.rb', line 35

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

#load_current_resourceObject



24
25
26
27
28
29
30
31
32
# File 'lib/chef/provider/group/groupadd.rb', line 24

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

#manage_groupObject

Manage the group when it already exists



43
44
45
46
47
48
# File 'lib/chef/provider/group/groupadd.rb', line 43

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

#modify_group_membersObject



55
56
57
# File 'lib/chef/provider/group/groupadd.rb', line 55

def modify_group_members
  raise Chef::Exceptions::Group, "you must override modify_group_members in #{self.to_s}"
end

#remove_groupObject

Remove the group



51
52
53
# File 'lib/chef/provider/group/groupadd.rb', line 51

def remove_group
  run_command(:command => "groupdel #{@new_resource.group_name}")
end

#set_optionsObject

Little bit of magic as per Adam’s useradd provider to pull the assign the command line flags

Returns

<string>

A string containing the option and then the quoted value



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

def set_options
  opts = ""
  { :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 << " #{@new_resource.group_name}"
end