code cleanup
This commit is contained in:
parent
291aeb98ed
commit
2c2a29906b
49
collab.rb
49
collab.rb
|
@ -104,7 +104,6 @@ class Project
|
||||||
def add_user(user)
|
def add_user(user)
|
||||||
@users << user
|
@users << user
|
||||||
user.keys.each do |key|
|
user.keys.each do |key|
|
||||||
p key
|
|
||||||
add_key(key)
|
add_key(key)
|
||||||
end
|
end
|
||||||
flush
|
flush
|
||||||
|
@ -119,9 +118,8 @@ class Project
|
||||||
|
|
||||||
def refresh
|
def refresh
|
||||||
@users = File.open("#{@path}/.ssh/users","r") do |f|
|
@users = File.open("#{@path}/.ssh/users","r") do |f|
|
||||||
f.read.split("\n").map{|id| p id; puts $user_id_map; get_user_by_id(id)}
|
f.read.split("\n").map{|id| get_user_by_id(id)}
|
||||||
end
|
end
|
||||||
p @users
|
|
||||||
@keys = extract_ssh_pubkeys("#{@path}/.ssh/authorized_keys")
|
@keys = extract_ssh_pubkeys("#{@path}/.ssh/authorized_keys")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -143,6 +141,9 @@ class Project
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class DuplicateProjectError < StandardError
|
||||||
|
end
|
||||||
|
|
||||||
def get_project(id)
|
def get_project(id)
|
||||||
$project_id_map[id]
|
$project_id_map[id]
|
||||||
end
|
end
|
||||||
|
@ -153,10 +154,16 @@ def add_project(project)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_project(id)
|
def create_project(id)
|
||||||
if not system("useradd -m #{POSIX_NAME_PREFIX}#{id}") then
|
$projects.each do |p|
|
||||||
raise "Project creation failed"
|
if p == id then
|
||||||
|
raise DuplicateProjectError, "Project with id #{id} already exists"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not system("useradd -m #{POSIX_NAME_PREFIX}#{id} -s /usr/bin/eshell") then
|
||||||
|
raise "Project creation failed"
|
||||||
|
end
|
||||||
|
|
||||||
project = Project.new
|
project = Project.new
|
||||||
project.id = id
|
project.id = id
|
||||||
project.path = "/home/#{POSIX_NAME_PREFIX}#{id}"
|
project.path = "/home/#{POSIX_NAME_PREFIX}#{id}"
|
||||||
|
@ -167,14 +174,14 @@ def create_project(id)
|
||||||
FileUtils.mkdir "#{project.path}/.ssh"
|
FileUtils.mkdir "#{project.path}/.ssh"
|
||||||
FileUtils.touch "#{project.path}/.ssh/authorized_keys"
|
FileUtils.touch "#{project.path}/.ssh/authorized_keys"
|
||||||
FileUtils.touch "#{project.path}/.ssh/users"
|
FileUtils.touch "#{project.path}/.ssh/users"
|
||||||
FileUtils.chown_R project.id, project.id, "#{project.path}/.ssh"
|
FileUtils.chown_R project.posixname, project.posixname, "#{project.path}/.ssh"
|
||||||
|
|
||||||
project
|
project
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_project(project)
|
def remove_project(project)
|
||||||
system("userdel -r #{project.posixname}")
|
system("userdel -r #{project.posixname}")
|
||||||
$projects.remove(project)
|
$projects.delete(project)
|
||||||
$project_id_map[id] = nil
|
$project_id_map[id] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -234,34 +241,6 @@ def get_ssh_pubkeys(project)
|
||||||
extract_ssh_pubkeys("#{project.path}/.ssh/authorized_keys")
|
extract_ssh_pubkeys("#{project.path}/.ssh/authorized_keys")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Deprecate
|
|
||||||
def add_ssh_pubkey(project,key)
|
|
||||||
unless valid_pubkey? key
|
|
||||||
raise InvalidSSHPubKey, "Public key not valid"
|
|
||||||
end
|
|
||||||
get_ssh_keyfile(project,"a") do |key_file|
|
|
||||||
key_file << "#{key.to_s}\n"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def reset_pubkeys(project)
|
|
||||||
FileUtils.rm "#{project.path}/.ssh/authorized_keys"
|
|
||||||
FileUtils.touch "#{project.path}/.ssh/authorized_keys"
|
|
||||||
FileUtils.chown_R project.id, project.id, "#{project.path}/.ssh"
|
|
||||||
end
|
|
||||||
|
|
||||||
def remove_ssh_pubkey(project,key)
|
|
||||||
keys = get_ssh_pubkeys(project)
|
|
||||||
reset_pubkeys(project)
|
|
||||||
keys.each{ |k| unless k == key then add_ssh_pubkey(project,k) end}
|
|
||||||
end
|
|
||||||
|
|
||||||
def remove_ssh_pubkey_by_key
|
|
||||||
end
|
|
||||||
|
|
||||||
def remove_ssh_pubkey_by_comment
|
|
||||||
end
|
|
||||||
|
|
||||||
def valid_pubkey?(key)
|
def valid_pubkey?(key)
|
||||||
IO.popen("ssh-keygen -qlf -","r+") do |io|
|
IO.popen("ssh-keygen -qlf -","r+") do |io|
|
||||||
io.write key.to_s
|
io.write key.to_s
|
||||||
|
|
45
prompt.rb
45
prompt.rb
|
@ -51,13 +51,37 @@ def keys_user(user)
|
||||||
end
|
end
|
||||||
|
|
||||||
def projects
|
def projects
|
||||||
choices = $projects
|
choices = choices = {'Create new project' => :create,
|
||||||
project = $prompt.select("Pick a project", choices)
|
'Modify existing project' => :modify}
|
||||||
keys_project(project)
|
case $prompt.select("What do you want to do?", choices)
|
||||||
|
when :create
|
||||||
|
project_create
|
||||||
|
when :modify
|
||||||
|
project_modify
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def keys_project(project)
|
def project_create
|
||||||
choices = {'List users' => :list,
|
id = $prompt.ask("What should be the id of the new project?") do |input|
|
||||||
|
input.required true
|
||||||
|
input.validate /\A\w+\Z/
|
||||||
|
end
|
||||||
|
begin
|
||||||
|
create_project(id)
|
||||||
|
rescue DuplicateProjectError => e
|
||||||
|
puts e.message.colorize(:red)
|
||||||
|
projects
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def project_modify
|
||||||
|
# Currently, it is implied that modify equals changing which users have access
|
||||||
|
project_users($prompt.select("Pick a project", $projects))
|
||||||
|
end
|
||||||
|
|
||||||
|
def project_users(project)
|
||||||
|
choices = {'List users with access' => :list,
|
||||||
'Add user' => :add,
|
'Add user' => :add,
|
||||||
'Select users with access' => :select}
|
'Select users with access' => :select}
|
||||||
case $prompt.select("What do you want to do?", choices)
|
case $prompt.select("What do you want to do?", choices)
|
||||||
|
@ -70,16 +94,23 @@ def keys_project(project)
|
||||||
puts "Added user #{user} to #{project}".colorize(:green)
|
puts "Added user #{user} to #{project}".colorize(:green)
|
||||||
when :select
|
when :select
|
||||||
choices = $users
|
choices = $users
|
||||||
|
|
||||||
# Mark already added users as "default"
|
# Mark already added users as "default"
|
||||||
counter = 1
|
|
||||||
defaults = []
|
defaults = []
|
||||||
|
counter = 1
|
||||||
choices.each do |u|
|
choices.each do |u|
|
||||||
if project.users.include? u then
|
if project.users.include? u then
|
||||||
defaults << counter
|
defaults << counter
|
||||||
end
|
end
|
||||||
counter += 1
|
counter += 1
|
||||||
end
|
end
|
||||||
users = $prompt.multi_select("Select users", choices, default: defaults)
|
|
||||||
|
if defaults != []
|
||||||
|
users = $prompt.multi_select("Select users", choices, default: defaults)
|
||||||
|
else
|
||||||
|
users = $prompt.multi_select("Select users", choices)
|
||||||
|
end
|
||||||
|
|
||||||
project.users = users
|
project.users = users
|
||||||
project.flush
|
project.flush
|
||||||
project.refresh
|
project.refresh
|
||||||
|
|
Loading…
Reference in New Issue
Block a user