2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: AGPL-3.0
6 abort 'Error: Ruby >= 1.9.3 required.' if RUBY_VERSION < '1.9.3'
11 log = Logger.new STDERR
12 log.progname = $0.split('/').last
14 opts = Optimist::options do
16 banner "Usage: #{log.progname} " +
17 "{user_uuid_or_email} {user_and_repo_name} {vm_uuid}"
22 opt :openid_prefix, <<-eos, default: 'https://www.google.com/accounts/o8/id'
23 If creating a new user record, require authentication from an OpenID \
24 with this OpenID prefix *and* a matching email address in order to \
27 opt :send_notification_email, <<-eos, default: 'true'
28 Send notification email after successfully setting up the user.
32 log.level = (ENV['DEBUG'] || opts.debug) ? Logger::DEBUG : Logger::WARN
35 Optimist::die "required arguments are missing"
38 user_arg, user_repo_name, vm_uuid = ARGV
41 arv = Arvados.new(api_version: 'v1')
43 # Look up the given user by uuid or, failing that, email address.
45 found_user = arv.user.get(uuid: user_arg)
46 rescue Arvados::TransactionFailedError
47 found = arv.user.list(where: {email: user_arg})[:items]
50 if !user_arg.match(/\w\@\w+\.\w+/)
51 abort "About to create new user, but #{user_arg.inspect} " +
52 "does not look like an email address. Stop."
54 elsif found.count != 1
55 abort "Found #{found.count} users with email. Stop."
57 found_user = found.first
61 # Invoke user setup method
63 user = arv.user.setup uuid: found_user[:uuid], repo_name: user_repo_name,
64 vm_uuid: vm_uuid, openid_prefix: opts.openid_prefix,
65 send_notification_email: opts.send_notification_email
67 user = arv.user.setup user: {email: user_arg}, repo_name: user_repo_name,
68 vm_uuid: vm_uuid, openid_prefix: opts.openid_prefix,
69 send_notification_email: opts.send_notification_email
72 log.info {"user uuid: " + user[:uuid]}