Project

General

Profile

Feature #16803 » usr-local-arvados-install-arvados-tokens.rb.erb

Ward Vandewege, 09/02/2020 05:13 PM

 
1
#!/usr/bin/env ruby
2

    
3
###################################################################
4
#  THIS FILE IS MANAGED BY PUPPET -- CHANGES WILL BE OVERWRITTEN  #
5
###################################################################
6

    
7
require 'rubygems'
8
require 'pp'
9
require 'arvados'
10
require 'yaml'
11

    
12
# This script installs arvados tokens in shell accounts (on the shell VMs).
13
#
14
# Ward Vandewege <ward@curoverse.com>
15

    
16
# Default is development
17
production = ARGV[0] == "production"
18

    
19
ENV["RAILS_ENV"] = "development"
20
ENV["RAILS_ENV"] = "production" if production
21

    
22
DEBUG = 1
23

    
24
# load and merge in the environment-specific application config info
25
# if present, overriding base config parameters as specified
26
path = File.dirname(__FILE__) + '/config/arvados-clients.yml'
27
if File.exists?(path) then
28
	cp_config = YAML.load_file(path)[ENV['RAILS_ENV']]
29
else
30
	puts "Please create a\n " + File.dirname(__FILE__) + "/config/arvados-clients.yml\n file"
31
	exit 1
32
end
33

    
34
ENV['ARVADOS_API_HOST'] = cp_config['arvados_api_host']
35
ENV['ARVADOS_API_TOKEN'] = cp_config['arvados_api_token']
36

    
37
$error = false
38
$output = ''
39

    
40
def install_tokens(logins,cp_config,arv)
41
  @shell_port = cp_config['arvados_shell_port']
42
  seen = Hash.new()
43
  logins.each do |l|
44
    next if seen[l[:username]]
45
    $output += "Processing user #{l[:username]}\n"
46
    @shell_vm = 'controller@' + l[:hostname] + '.' + cp_config['arvados_api_host']
47
    @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"
48
    @config_test = `#{@command}`
49
    if $? != 0 and not @config_test =~ /Connection timed out/
50
      $error = true
51
      $output += "\nError running command\n  #{@command}\n\n"
52
      $output += @config_test + "\n"
53
    end
54
    if @config_test =~ /does not exist/
55
      # Create a user token
56
      user_token = arv.api_client_authorization.create(api_client_authorization:{owner_uuid:l[:user_uuid],api_client_id:0})[:api_token]
57
      # Write ~#{l[:username]}/.config/arvados
58
      @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\""
59
      @command_output = `#{@command}`
60
      if $? != 0
61
        $error = true
62
        $output += "\nError running command\n  #{@command}\n\n"
63
        $output += @command_output + "\n"
64
      end
65
      @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/\""
66
      @command_output = `#{@command}`
67
      if $? != 0
68
        $error = true
69
        $output += "\nError running command\n  #{@command}\n\n"
70
        $output += @command_output + "\n"
71
      end
72
    end
73
    seen[l[:username]] = true if not seen.has_key?(l[:username])
74
  end
75
end
76

    
77
begin
78

    
79
  arv = Arvados.new( { :suppress_ssl_warnings => false } )
80

    
81
  vms = arv.virtual_machine.list()[:items]
82
  vms = vms.reject { |v| v[:hostname].nil? }
83

    
84
  # Only consider VMs defined in this cluster
85
  cluster_identifier = cp_config['arvados_api_host'].split('.')[0]
86
  vms = vms.reject { |v| /^#{cluster_identifier}/.match(v[:uuid]).nil? }
87

    
88
  vms.each do |v|
89
    $output += "Processing #{v[:hostname]}\n"
90
    logins = arv.virtual_machine.logins(:uuid => v[:uuid])[:items]
91

    
92
    install_tokens(logins,cp_config,arv)
93
  end
94

    
95
  puts $output if $error
96

    
97
rescue Exception => bang
98
  puts $output
99
  puts "Error: " + bang.to_s
100
  puts bang.backtrace.join("\n")
101
  exit 1
102
end
103

    
(1-1/2)