2025-08-06 08:00:01 +02:00
|
|
|
|
|
|
|
module User
|
2025-08-07 16:53:10 +02:00
|
|
|
require 'system'
|
2025-08-06 08:00:01 +02:00
|
|
|
|
2025-08-07 16:29:11 +02:00
|
|
|
def self.install(context)
|
|
|
|
puts "Creating #{context.type}: #{context.user_name}"
|
2025-08-07 16:53:10 +02:00
|
|
|
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
|
2025-08-07 16:29:11 +02:00
|
|
|
end
|
2025-08-06 08:00:01 +02:00
|
|
|
|
2025-08-07 16:29:11 +02:00
|
|
|
end
|