Class: Chef::Provider::User::Useradd
Constant Summary
collapse
- UNIVERSAL_OPTIONS =
[[:comment, "-c"], [:gid, "-g"], [:password, "-p"], [:shell, "-s"], [:uid, "-u"]]
Mixin::ShellOut::DEPRECATED_OPTIONS
Instance Attribute Summary
#locked, #user_exists
#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context
Instance Method Summary
collapse
#run_command_compatible_options, #shell_out, #shell_out!
#action_create, #action_lock, #action_manage, #action_modify, #action_remove, #action_unlock, #compare_user, #convert_group_name, #define_resource_requirements, #initialize, #load_current_resource, #whyrun_supported?
#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_and_return_stdout_stderr, #run_command_with_systems_locale
#popen4
#popen4
#action_nothing, #cleanup_after_converge, #converge_by, #define_resource_requirements, #events, #initialize, #load_current_resource, #node, #process_resource_requirements, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, #whyrun_mode?, #whyrun_supported?
#build_resource, #declare_resource, #describe_self_for_error, #evaluate_resource_definition, #has_resource_definition?, #have_resource_class_for?, #method_missing, #resource_class_for
#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Chef::DSL::Recipe
Instance Method Details
#check_lock ⇒ Object
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
88
89
90
91
92
93
94
|
# File 'lib/chef/provider/user/useradd.rb', line 56
def check_lock
passwd_s = shell_out!("passwd", "-S", new_resource.username, :returns => [0,1])
if whyrun_mode? && passwd_s.stdout.empty? && passwd_s.stderr.match(/does not exist/)
return false
end
raise Chef::Exceptions::User, "Cannot determine if #{@new_resource} is locked!" if passwd_s.stdout.empty?
status_line = passwd_s.stdout.split(' ')
case status_line[1]
when /^P/
@locked = false
when /^N/
@locked = false
when /^L/
@locked = true
end
unless passwd_s.exitstatus == 0
raise_lock_error = false
if ['redhat', 'centos'].include?(node[:platform])
passwd_version_check = shell_out!('rpm -q passwd')
passwd_version = passwd_version_check.stdout.chomp
unless passwd_version == 'passwd-0.73-1'
raise_lock_error = true
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
104
105
106
107
108
109
|
# File 'lib/chef/provider/user/useradd.rb', line 104
def compile_command(base_command)
base_command = Array(base_command)
yield base_command
base_command << new_resource.username
base_command
end
|
#create_user ⇒ Object
32
33
34
35
36
37
38
|
# File 'lib/chef/provider/user/useradd.rb', line 32
def create_user
command = compile_command("useradd") do |useradd|
useradd.concat(universal_options)
useradd.concat(useradd_options)
end
shell_out!(*command)
end
|
#lock_user ⇒ Object
96
97
98
|
# File 'lib/chef/provider/user/useradd.rb', line 96
def lock_user
shell_out!("usermod", "-L", new_resource.username)
end
|
#manage_user ⇒ Object
40
41
42
43
44
45
46
47
|
# File 'lib/chef/provider/user/useradd.rb', line 40
def manage_user
unless universal_options.empty?
command = compile_command("usermod") do |u|
u.concat(universal_options)
end
shell_out!(*command)
end
end
|
#managing_home_dir? ⇒ Boolean
157
158
159
|
# File 'lib/chef/provider/user/useradd.rb', line 157
def managing_home_dir?
new_resource.manage_home || new_resource.supports[:manage_home]
end
|
#remove_user ⇒ Object
49
50
51
52
53
54
|
# File 'lib/chef/provider/user/useradd.rb', line 49
def remove_user
command = [ "userdel" ]
command << "-r" if managing_home_dir?
command << new_resource.username
shell_out!(*command)
end
|
#universal_options ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/chef/provider/user/useradd.rb', line 111
def universal_options
@universal_options ||=
begin
opts = []
self.class::UNIVERSAL_OPTIONS.each do |field, option|
update_options(field, option, opts)
end
if updating_home?
if managing_home_dir?
Chef::Log.debug("#{new_resource} managing the users home directory")
opts << "-d" << new_resource.home << "-m"
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
end
|
#unlock_user ⇒ Object
100
101
102
|
# File 'lib/chef/provider/user/useradd.rb', line 100
def unlock_user
shell_out!("usermod", "-U", new_resource.username)
end
|
#update_options(field, option, opts) ⇒ Object
133
134
135
136
137
138
139
140
|
# File 'lib/chef/provider/user/useradd.rb', line 133
def update_options(field, option, opts)
if @current_resource.send(field).to_s != new_resource.send(field).to_s
if new_resource.send(field)
Chef::Log.debug("#{new_resource} setting #{field} to #{new_resource.send(field)}")
opts << option << new_resource.send(field).to_s
end
end
end
|
#updating_home? ⇒ Boolean
148
149
150
151
152
153
154
155
|
# File 'lib/chef/provider/user/useradd.rb', line 148
def updating_home?
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_options ⇒ Object
142
143
144
145
146
|
# File 'lib/chef/provider/user/useradd.rb', line 142
def useradd_options
opts = []
opts << "-r" if new_resource.system
opts
end
|