5824: Rename keepdl to keep-web.
[arvados.git] / services / api / script / setup-new-user.rb
1 #!/usr/bin/env ruby
2
3 abort 'Error: Ruby >= 1.9.3 required.' if RUBY_VERSION < '1.9.3'
4
5 require 'logger'
6 require 'trollop'
7
8 log = Logger.new STDERR
9 log.progname = $0.split('/').last
10
11 opts = Trollop::options do
12   banner ''
13   banner "Usage: #{log.progname} " +
14     "{user_uuid_or_email} {user_and_repo_name} {vm_uuid}"
15   banner ''
16   opt :debug, <<-eos
17 Show debug messages.
18   eos
19   opt :openid_prefix, <<-eos, default: 'https://www.google.com/accounts/o8/id'
20 If creating a new user record, require authentication from an OpenID \
21 with this OpenID prefix *and* a matching email address in order to \
22 claim the account.
23   eos
24   opt :send_notification_email, <<-eos, default: 'true'
25 Send notification email after successfully setting up the user.
26   eos
27 end
28
29 log.level = (ENV['DEBUG'] || opts.debug) ? Logger::DEBUG : Logger::WARN
30
31 if ARGV.count != 3
32   Trollop::die "required arguments are missing"
33 end
34
35 user_arg, user_repo_name, vm_uuid = ARGV
36
37 require 'arvados'
38 arv = Arvados.new(api_version: 'v1')
39
40 # Look up the given user by uuid or, failing that, email address.
41 begin
42   found_user = arv.user.get(uuid: user_arg)
43 rescue Arvados::TransactionFailedError
44   found = arv.user.list(where: {email: user_arg})[:items]
45
46   if found.count == 0
47     if !user_arg.match(/\w\@\w+\.\w+/)
48       abort "About to create new user, but #{user_arg.inspect} " +
49                "does not look like an email address. Stop."
50     end
51   elsif found.count != 1
52     abort "Found #{found.count} users with email. Stop."
53   else
54     found_user = found.first
55   end
56 end
57
58 # Invoke user setup method
59 if (found_user)
60   user = arv.user.setup uuid: found_user[:uuid], repo_name: user_repo_name,
61           vm_uuid: vm_uuid, openid_prefix: opts.openid_prefix,
62           send_notification_email: opts.send_notification_email
63 else
64   user = arv.user.setup user: {email: user_arg}, repo_name: user_repo_name,
65           vm_uuid: vm_uuid, openid_prefix: opts.openid_prefix,
66           send_notification_email: opts.send_notification_email
67 end
68
69 log.info {"user uuid: " + user[:uuid]}
70
71 puts user.inspect