Class: Chef::Provider::Group::Dscl

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

Instance Attribute Summary

Attributes inherited from Chef::Provider::Group

#group_exists

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

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

#chdir_or_tmpdir, #handle_command_failures, #not_if, #only_if, #output_of_command, #run_command, #run_command_with_systems_locale

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #cookbook_name, #initialize, #node, #resource_collection

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

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



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

def create_group
  dscl_create_group
  set_gid
  set_members
end

#dscl(*args) ⇒ Object



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

def dscl(*args)
  host = "."
  stdout_result = ""; stderr_result = ""; cmd = "dscl #{host} -#{args.join(' ')}"
  status = popen4(cmd) do |pid, stdin, stdout, stderr|
    stdout.each { |line| stdout_result << line }
    stderr.each { |line| stderr_result << line }
  end
  return [cmd, status, stdout_result, stderr_result]
end

#dscl_create_groupObject



110
111
112
113
# File 'lib/chef/provider/group/dscl.rb', line 110

def dscl_create_group
  safe_dscl("create /Groups/#{@new_resource.group_name}")
  safe_dscl("create /Groups/#{@new_resource.group_name} Password '*'")
end

#get_free_gid(search_limit = 1000) ⇒ Object

get a free GID greater than 200



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chef/provider/group/dscl.rb', line 49

def get_free_gid(search_limit=1000)
  gid = nil; next_gid_guess = 200
  groups_gids = safe_dscl("list /Groups gid")
  while(next_gid_guess < search_limit + 200)
    if groups_gids =~ Regexp.new("#{Regexp.escape(next_gid_guess.to_s)}\n")
      next_gid_guess += 1
    else
      gid = next_gid_guess
      break
    end
  end
  return gid || raise("gid not found. Exhausted. Searched #{search_limit} times")
end

#gid_used?(gid) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/chef/provider/group/dscl.rb', line 63

def gid_used?(gid)
  return false unless gid
  groups_gids = safe_dscl("list /Groups gid")
  !! ( groups_gids =~ Regexp.new("#{Regexp.escape(gid.to_s)}\n") )
end

#load_current_resourceObject



87
88
89
90
# File 'lib/chef/provider/group/dscl.rb', line 87

def load_current_resource
  super
  raise Chef::Exceptions::Group, "Could not find binary /usr/bin/dscl for #{@new_resource}" unless ::File.exists?("/usr/bin/dscl")
end

#manage_groupObject



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef/provider/group/dscl.rb', line 98

def manage_group
  if @new_resource.group_name && (@current_resource.group_name != @new_resource.group_name)
    dscl_create_group
  end
  if @new_resource.gid && (@current_resource.gid != @new_resource.gid)
    set_gid
  end
  if @new_resource.members && (@current_resource.members != @new_resource.members)
    set_members
  end
end

#remove_groupObject



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

def remove_group
  safe_dscl("delete /Groups/#{@new_resource.group_name}")
end

#safe_dscl(*args) ⇒ Object



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

def safe_dscl(*args)
  result = dscl(*args)
  return "" if ( args.first =~ /^delete/ ) && ( result[1].exitstatus != 0 )
  raise(Chef::Exceptions::Group,"dscl error: #{result.inspect}") unless result[1].exitstatus == 0
  raise(Chef::Exceptions::Group,"dscl error: #{result.inspect}") if result[2] =~ /No such key: /
  return result[2]
end

#set_gidObject



69
70
71
72
73
# File 'lib/chef/provider/group/dscl.rb', line 69

def set_gid
  @new_resource.gid(get_free_gid) if [nil,""].include? @new_resource.gid
  raise(Chef::Exceptions::Group,"gid is already in use") if gid_used?(@new_resource.gid)
  safe_dscl("create /Groups/#{@new_resource.group_name} PrimaryGroupID #{@new_resource.gid}")
end

#set_membersObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chef/provider/group/dscl.rb', line 75

def set_members
  unless @new_resource.append
    Chef::Log.debug("#{@new_resource}: removing group members #{@current_resource.members.join(' ')}") unless @current_resource.members.empty?
    safe_dscl("create /Groups/#{@new_resource.group_name} GroupMembers ''") # clear guid list
    safe_dscl("create /Groups/#{@new_resource.group_name} GroupMembership ''") # clear user list
  end
  unless @new_resource.members.empty?
    Chef::Log.debug("#{@new_resource}: setting group members #{@new_resource.members.join(', ')}")
    safe_dscl("append /Groups/#{@new_resource.group_name} GroupMembership #{@new_resource.members.join(' ')}")
  end
end