10 req_envs = %w(ARVADOS_API_HOST ARVADOS_API_TOKEN ARVADOS_VIRTUAL_MACHINE_UUID)
13 abort "Fatal: These environment vars must be set: #{req_envs}"
22 uids = Hash[Etc.to_enum(:passwd).map { |ent| [ent.name, ent.uid] }]
23 gids = Hash[Etc.to_enum(:group).map { |ent| [ent.name, ent.gid] }]
24 arv = Arvados.new({ :suppress_ssl_warnings => false })
26 vm_uuid = ENV['ARVADOS_VIRTUAL_MACHINE_UUID']
28 logins = arv.virtual_machine.logins(:uuid => vm_uuid)[:items]
29 logins = [] if logins.nil?
30 logins = logins.reject { |l| l[:username].nil? or l[:hostname].nil? or l[:public_key].nil? or l[:virtual_machine_uuid] != vm_uuid }
34 open("/etc/login.defs", encoding: "utf-8") do |login_defs|
35 login_defs.each_line do |line|
36 next unless match = /^UID_MIN\s+(\S+)$/.match(line)
37 if match[1].start_with?("0x")
39 elsif match[1].start_with?("0")
44 new_uid_min = match[1].to_i(base)
45 uid_min = new_uid_min if (new_uid_min > 0)
48 logins.reject! { |l| (uids[l[:username]] || 65535) < uid_min }
54 keys[l[:username]] = Array.new() if not keys.has_key?(l[:username])
56 # Handle putty-style ssh public keys
57 key.sub!(/^(Comment: "r[^\n]*\n)(.*)$/m,'ssh-rsa \2 \1')
58 key.sub!(/^(Comment: "d[^\n]*\n)(.*)$/m,'ssh-dss \2 \1')
62 keys[l[:username]].push(key) if not keys[l[:username]].include?(key)
66 devnull = open("/dev/null", "w")
69 next if seen[l[:username]]
70 seen[l[:username]] = true if not seen.has_key?(l[:username])
72 unless uids[l[:username]]
73 STDERR.puts "Creating account #{l[:username]}"
74 groups = l[:groups] || []
75 # Adding users to the FUSE group has long been hardcoded behavior.
77 groups.select! { |name| gids[name] }
79 next unless system("useradd", "-m",
82 "-G", groups.join(","),
86 # Create .ssh directory if necessary
87 @homedir = Etc.getpwnam(l[:username]).dir
88 userdotssh = File.join(@homedir, ".ssh")
89 Dir.mkdir(userdotssh) if !File.exists?(userdotssh)
90 @key = "#######################################################################################
91 # THIS FILE IS MANAGED BY #{$0} -- CHANGES WILL BE OVERWRITTEN #
92 #######################################################################################\n\n"
93 @key += keys[l[:username]].join("\n") + "\n"
94 userauthkeys = File.join(userdotssh, "authorized_keys")
95 if !File.exists?(userauthkeys) or IO::read(userauthkeys) != @key then
96 f = File.new(userauthkeys, 'w')
100 FileUtils.chown_R(l[:username], nil, userdotssh)
101 File.chmod(0700, userdotssh)
102 File.chmod(0750, @homedir)
103 File.chmod(0600, userauthkeys)
107 rescue Exception => bang
108 puts "Error: " + bang.to_s
109 puts bang.backtrace.join("\n")