|
#!/usr/bin/env ruby
|
|
|
|
###################################################################
|
|
# THIS FILE IS MANAGED BY PUPPET -- CHANGES WILL BE OVERWRITTEN #
|
|
###################################################################
|
|
|
|
require 'rubygems'
|
|
require 'pp'
|
|
require 'arvados'
|
|
require 'yaml'
|
|
|
|
# This script installs arvados tokens in shell accounts (on the shell VMs).
|
|
#
|
|
# Ward Vandewege <ward@curoverse.com>
|
|
|
|
# Default is development
|
|
production = ARGV[0] == "production"
|
|
|
|
ENV["RAILS_ENV"] = "development"
|
|
ENV["RAILS_ENV"] = "production" if production
|
|
|
|
DEBUG = 1
|
|
|
|
# load and merge in the environment-specific application config info
|
|
# if present, overriding base config parameters as specified
|
|
path = File.dirname(__FILE__) + '/config/arvados-clients.yml'
|
|
if File.exists?(path) then
|
|
cp_config = YAML.load_file(path)[ENV['RAILS_ENV']]
|
|
else
|
|
puts "Please create a\n " + File.dirname(__FILE__) + "/config/arvados-clients.yml\n file"
|
|
exit 1
|
|
end
|
|
|
|
ENV['ARVADOS_API_HOST'] = cp_config['arvados_api_host']
|
|
ENV['ARVADOS_API_TOKEN'] = cp_config['arvados_api_token']
|
|
|
|
$error = false
|
|
$output = ''
|
|
|
|
def install_tokens(logins,cp_config,arv)
|
|
@shell_port = cp_config['arvados_shell_port']
|
|
seen = Hash.new()
|
|
logins.each do |l|
|
|
next if seen[l[:username]]
|
|
$output += "Processing user #{l[:username]}\n"
|
|
@shell_vm = 'controller@' + l[:hostname] + '.' + cp_config['arvados_api_host']
|
|
@command = "ssh -o 'StrictHostKeyChecking no' -o 'ConnectTimeout 1' -p#{@shell_port} #{@shell_vm} sudo /usr/local/arvados/ensure-.config-dir.sh #{l[:username]} 2>&1"
|
|
@config_test = `#{@command}`
|
|
if $? != 0 and not @config_test =~ /Connection timed out/
|
|
$error = true
|
|
$output += "\nError running command\n #{@command}\n\n"
|
|
$output += @config_test + "\n"
|
|
end
|
|
if @config_test =~ /does not exist/
|
|
# Create a user token
|
|
user_token = arv.api_client_authorization.create(api_client_authorization:{owner_uuid:l[:user_uuid],api_client_id:0})[:api_token]
|
|
# Write ~#{l[:username]}/.config/arvados
|
|
@command = "ssh -o 'StrictHostKeyChecking no' -p#{@shell_port} #{@shell_vm} \"echo 'ARVADOS_API_HOST=#{cp_config['arvados_api_host']}\nARVADOS_API_TOKEN=#{user_token}' |sudo tee ~#{l[:username]}/.config/arvados/settings.conf\""
|
|
@command_output = `#{@command}`
|
|
if $? != 0
|
|
$error = true
|
|
$output += "\nError running command\n #{@command}\n\n"
|
|
$output += @command_output + "\n"
|
|
end
|
|
@command = "ssh -o 'StrictHostKeyChecking no' -p#{@shell_port} #{@shell_vm} \"sudo chmod 700 ~#{l[:username]}/.config/; sudo chmod 700 ~#{l[:username]}/.config/arvados/; sudo chmod 600 ~#{l[:username]}/.config/arvados/settings.conf; sudo chown -R #{l[:username]}:#{l[:username]} ~#{l[:username]}/.config/\""
|
|
@command_output = `#{@command}`
|
|
if $? != 0
|
|
$error = true
|
|
$output += "\nError running command\n #{@command}\n\n"
|
|
$output += @command_output + "\n"
|
|
end
|
|
end
|
|
seen[l[:username]] = true if not seen.has_key?(l[:username])
|
|
end
|
|
end
|
|
|
|
begin
|
|
|
|
arv = Arvados.new( { :suppress_ssl_warnings => false } )
|
|
|
|
vms = arv.virtual_machine.list()[:items]
|
|
vms = vms.reject { |v| v[:hostname].nil? }
|
|
|
|
# Only consider VMs defined in this cluster
|
|
cluster_identifier = cp_config['arvados_api_host'].split('.')[0]
|
|
vms = vms.reject { |v| /^#{cluster_identifier}/.match(v[:uuid]).nil? }
|
|
|
|
vms.each do |v|
|
|
$output += "Processing #{v[:hostname]}\n"
|
|
logins = arv.virtual_machine.logins(:uuid => v[:uuid])[:items]
|
|
|
|
install_tokens(logins,cp_config,arv)
|
|
end
|
|
|
|
puts $output if $error
|
|
|
|
rescue Exception => bang
|
|
puts $output
|
|
puts "Error: " + bang.to_s
|
|
puts bang.backtrace.join("\n")
|
|
exit 1
|
|
end
|
|
|