Skip to content

Commit

Permalink
improve error handling around obtaining the user's Group ID
Browse files Browse the repository at this point in the history
During `why_run` mode, the user has not been created yet. As such, calls to `Etc.getpwnam()` result in an `ArgumentError` being thrown.

Add a `user_gid` method that either returns the gid or nil, and use it where the gid is required.
  • Loading branch information
theckman committed Apr 13, 2015
1 parent a738ee2 commit 7d445ed
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions providers/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ def normalize_bool(val)
end
end

def user_gid
# this check is needed as the new user won't exit yet
# in why_run mode, causing an uncaught ArgumentError exception
Etc.getpwnam(new_resource.username).gid
rescue ArgumentError
nil
end

def user_resource(exec_action)
# avoid variable scoping issues in resource block
my_home, my_shell, manage_home, non_unique = @my_home, @my_shell, @manage_home, @non_unique
Expand Down Expand Up @@ -131,7 +139,7 @@ def home_dir_resource(exec_action)
r = directory my_home do
path my_home
owner new_resource.username
group Etc.getpwnam(new_resource.username).gid
group user_gid
mode node['user']['home_dir_mode']
recursive true
action :nothing
Expand All @@ -146,7 +154,7 @@ def home_ssh_dir_resource(exec_action)
r = directory "#{my_home}/.ssh" do
path "#{my_home}/.ssh"
owner new_resource.username
group Etc.getpwnam(new_resource.username).gid
group user_gid
mode '0700'
recursive true
action :nothing
Expand All @@ -166,7 +174,7 @@ def authorized_keys_resource(exec_action)
cookbook 'user'
source 'authorized_keys.erb'
owner new_resource.username
group Etc.getpwnam(new_resource.username).gid
group user_gid
mode '0600'
variables :user => new_resource.username,
:ssh_keys => ssh_keys,
Expand Down

0 comments on commit 7d445ed

Please sign in to comment.