Class: Chef::Provider::User::Useradd

Inherits:
Chef::Provider::User show all
Defined in:
lib/chef/provider/user/useradd.rb

Constant Summary collapse

UNIVERSAL_OPTIONS =
[[:comment, "-c"], [:gid, "-g"], [:password, "-p"], [:shell, "-s"], [:uid, "-u"]]

Instance Attribute Summary

Attributes inherited from Chef::Provider::User

#locked, #user_exists

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods inherited from Chef::Provider::User

#action_create, #action_lock, #action_manage, #action_modify, #action_remove, #action_unlock, #compare_user, #convert_group_name, #initialize, #load_current_resource

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::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #cookbook_name, #initialize, #load_current_resource, #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::EnforceOwnershipAndPermissions

#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::User

Dynamic Method Handling

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

Instance Method Details

#check_lockObject



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
# File 'lib/chef/provider/user/useradd.rb', line 48

def check_lock
  status = popen4("passwd -S #{@new_resource.username}") do |pid, stdin, stdout, stderr|
    status_line = stdout.gets.split(' ')
    case status_line[1]
    when /^P/
      @locked = false
    when /^N/
      @locked = false
    when /^L/
      @locked = true
    end
  end

  unless status.exitstatus == 0
    raise_lock_error = false
    # we can get an exit code of 1 even when it's successful on rhel/centos (redhat bug 578534)
    if status.exitstatus == 1 && ['redhat', 'centos'].include?(node[:platform])
      passwd_version_status = popen4('rpm -q passwd') do |pid, stdin, stdout, stderr|
        passwd_version = stdout.gets.chomp

        unless passwd_version == 'passwd-0.73-1'
          raise_lock_error = true
        end
      end
    else
      raise_lock_error = true
    end

    raise Chef::Exceptions::User, "Cannot determine if #{@new_resource} is locked!" if raise_lock_error
  end

  @locked
end

#compile_command(base_command) {|base_command| ... } ⇒ Object

Yields:

  • (base_command)


90
91
92
93
94
# File 'lib/chef/provider/user/useradd.rb', line 90

def compile_command(base_command)
  yield base_command
  base_command << " #{@new_resource.username}"
  base_command
end

#create_userObject



28
29
30
31
32
33
34
# File 'lib/chef/provider/user/useradd.rb', line 28

def create_user
  command = compile_command("useradd") do |useradd|
    useradd << universal_options
    useradd << useradd_options
  end
  run_command(:command => command)
end

#lock_userObject



82
83
84
# File 'lib/chef/provider/user/useradd.rb', line 82

def lock_user
  run_command(:command => "usermod -L #{@new_resource.username}")
end

#manage_userObject



36
37
38
39
# File 'lib/chef/provider/user/useradd.rb', line 36

def manage_user
  command = compile_command("usermod") { |u| u << universal_options }
  run_command(:command => command)
end

#managing_home_dir?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/chef/provider/user/useradd.rb', line 136

def managing_home_dir?
  @new_resource.manage_home || @new_resource.supports[:manage_home]
end

#remove_userObject



41
42
43
44
45
46
# File 'lib/chef/provider/user/useradd.rb', line 41

def remove_user
  command = "userdel"
  command << " -r" if managing_home_dir?
  command << " #{@new_resource.username}"
  run_command(:command => command)
end

#universal_optionsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/provider/user/useradd.rb', line 96

def universal_options
  opts = ''
  
  UNIVERSAL_OPTIONS.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 #{@new_resource.send(field)}")
        opts << " #{option} '#{@new_resource.send(field)}'"
      end
    end
  end
  if updating_home?
    if managing_home_dir?
      Chef::Log.debug("#{@new_resource} managing the users home directory")
      opts << " -d '#{@new_resource.home}'"
    else
      Chef::Log.debug("#{@new_resource} setting home to #{@new_resource.home}")
      opts << " -d '#{@new_resource.home}'"
    end
  end
  opts << " -o" if @new_resource.non_unique || @new_resource.supports[:non_unique]
  opts
end

#unlock_userObject



86
87
88
# File 'lib/chef/provider/user/useradd.rb', line 86

def unlock_user
  run_command(:command => "usermod -U #{@new_resource.username}")
end

#updating_home?Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
# File 'lib/chef/provider/user/useradd.rb', line 127

def updating_home?
  # will return false if paths are equivalent
  # Pathname#cleanpath does a better job than ::File::expand_path (on both unix and windows)
  # ::File.expand_path("///tmp") == ::File.expand_path("/tmp") => false
  # ::File.expand_path("\\tmp") => "C:/tmp"
  return true if @current_resource.home.nil? && @new_resource.home
  @new_resource.home and Pathname.new(@current_resource.home).cleanpath != Pathname.new(@new_resource.home).cleanpath
end

#useradd_optionsObject



120
121
122
123
124
125
# File 'lib/chef/provider/user/useradd.rb', line 120

def useradd_options
  opts = ''
  opts << " -m" if updating_home? && managing_home_dir?
  opts << " -r" if @new_resource.system
  opts
end