implement adding users
This commit is contained in:
parent
055744e7f7
commit
0f9f1a84f9
1 changed files with 29 additions and 0 deletions
29
lib/user.rb
29
lib/user.rb
|
@ -1,8 +1,37 @@
|
||||||
|
|
||||||
module User
|
module User
|
||||||
|
require 'system'
|
||||||
|
|
||||||
def self.install(context)
|
def self.install(context)
|
||||||
puts "Creating #{context.type}: #{context.user_name}"
|
puts "Creating #{context.type}: #{context.user_name}"
|
||||||
|
System.install(["zsh"])
|
||||||
|
|
||||||
|
user_exists = system("getent passwd #{context.user_name} > /dev/null")
|
||||||
|
|
||||||
|
group = case context.type
|
||||||
|
when :user
|
||||||
|
"users"
|
||||||
|
when :service
|
||||||
|
"services"
|
||||||
|
else
|
||||||
|
rise "Can not create user for unknow type: #{context.type}"
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
group_exists = system("getent group #{group} > /dev/null")
|
||||||
|
unless group_exists
|
||||||
|
puts "Group '#{group}' does not exist. Creating it..."
|
||||||
|
system("sudo groupadd #{group}")
|
||||||
|
end
|
||||||
|
|
||||||
|
if user_exists
|
||||||
|
puts "User #{context.user_name} already exists. Updating shell and adding to group '#{group}'."
|
||||||
|
system("sudo usermod -s /usr/bin/zsh #{context.user_name}")
|
||||||
|
system("sudo usermod -g #{group} #{context.user_name}")
|
||||||
|
else
|
||||||
|
puts "User #{context.user_name} does not exist. Creating user..."
|
||||||
|
system("sudo adduser --disabled-login --gecos \"\" --ingroup #{group} --shell /usr/bin/zsh #{context.user_name}")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue