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

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

Instance Attribute Summary

Attributes inherited from Chef::Provider::User

#locked, #user_exists

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #node

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

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, #load_current_resource

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chef/provider/user/useradd.rb', line 44

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 Chef::Exceptions::User, "Cannot determine if #{@new_resource} is locked!"
  end

  @locked
end

#create_userObject



25
26
27
28
29
# File 'lib/chef/provider/user/useradd.rb', line 25

def create_user
  command = "useradd"
  command << set_options
  run_command(:command => command)
end

#lock_userObject



64
65
66
# File 'lib/chef/provider/user/useradd.rb', line 64

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

#manage_userObject



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

def manage_user
  command = "usermod"
  command << set_options
  run_command(:command => command)
end

#remove_userObject



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

def remove_user
  command = "userdel"
  command << " -r" if @new_resource.supports[:manage_home]
  command << " #{@new_resource.username}"
  run_command(:command => command)
end

#set_optionsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chef/provider/user/useradd.rb', line 72

def set_options
  opts = ''
  
  field_list = {
    'comment' => "-c",
    'gid' => "-g",
    'uid' => "-u",
    'shell' => "-s",
    'password' => "-p"
  }
  field_list.sort{ |a,b| a[0] <=> b[0] }.each do |field, option|
    field_symbol = field.to_sym
    if @current_resource.send(field_symbol) != @new_resource.send(field_symbol)
      if @new_resource.send(field_symbol)
        Chef::Log.debug("Setting #{@new_resource} #{field} to #{@new_resource.send(field_symbol)}")
        opts << " #{option} '#{@new_resource.send(field_symbol)}'"
      end
    end
  end
  if @current_resource.home != @new_resource.home && @new_resource.home
    if @new_resource.supports[:manage_home]
      Chef::Log.debug("Managing the home directory for #{@new_resource}")
      opts << " -d '#{@new_resource.home}' -m"
    else
      Chef::Log.debug("Setting #{@new_resource} home to #{@new_resource.home}")
      opts << " -d '#{@new_resource.home}'"
    end
  end
  opts << " -o" if @new_resource.supports[:non_unique]
  opts << " #{@new_resource.username}"
  opts
end

#unlock_userObject



68
69
70
# File 'lib/chef/provider/user/useradd.rb', line 68

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