023a1f829ca9f91e832045dfc1fdf37d72cdf1c2
[arvados.git] / sdk / ruby / lib / arvados.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 require 'rubygems'
6 require 'active_support/inflector'
7 require 'json'
8 require 'fileutils'
9 require 'andand'
10
11 require 'arvados/google_api_client'
12
13 ActiveSupport::Inflector.inflections do |inflect|
14   inflect.irregular 'specimen', 'specimens'
15   inflect.irregular 'human', 'humans'
16 end
17
18 class Arvados
19   class ArvadosClient < Google::APIClient
20     attr_reader :request_id
21
22     def execute(*args)
23       @request_id = "req-" + Random::DEFAULT.rand(2**128).to_s(36)[0..19]
24       if args.last.is_a? Hash
25         args.last[:headers] ||= {}
26         args.last[:headers]['X-Request-Id'] = @request_id
27       end
28       begin
29         super(*args)
30       rescue => e
31         if !e.message.match(/.*req-[0-9a-zA-Z]{20}.*/)
32           e.message += "\nRequest ID: #{e.headers['X-Request-Id'] or @request_id}"
33         end
34         raise e
35       end
36     end
37   end
38
39   class TransactionFailedError < StandardError
40   end
41
42   @@debuglevel = 0
43   class << self
44     attr_accessor :debuglevel
45   end
46
47   def initialize(opts={})
48     @application_version ||= 0.0
49     @application_name ||= File.split($0).last
50
51     @arvados_api_version = opts[:api_version] || 'v1'
52
53     @config = nil
54     [[:api_host, 'ARVADOS_API_HOST'],
55      [:api_token, 'ARVADOS_API_TOKEN']].each do |op, en|
56       if opts[op]
57         config[en] = opts[op]
58       end
59       if !config[en]
60         raise "#{$0}: no :#{op} or ENV[#{en}] provided."
61       end
62     end
63
64     if (opts[:suppress_ssl_warnings] or
65         %w(1 true yes).index(config['ARVADOS_API_HOST_INSECURE'].
66                              andand.downcase))
67       suppress_warnings do
68         OpenSSL::SSL.const_set 'VERIFY_PEER', OpenSSL::SSL::VERIFY_NONE
69       end
70     end
71
72     # Define a class and an Arvados instance method for each Arvados
73     # resource. After this, self.job will return Arvados::Job;
74     # self.job.new() and self.job.find() will do what you want.
75     _arvados = self
76     namespace_class = Arvados.const_set "A#{self.object_id}", Class.new
77     self.arvados_api.schemas.each do |classname, schema|
78       next if classname.match /List$/
79       klass = Class.new(Arvados::Model) do
80         def self.arvados
81           @arvados
82         end
83         def self.api_models_sym
84           @api_models_sym
85         end
86         def self.api_model_sym
87           @api_model_sym
88         end
89       end
90
91       # Define the resource methods (create, get, update, delete, ...)
92       self.
93         arvados_api.
94         send(classname.underscore.split('/').last.pluralize.to_sym).
95         discovered_methods.
96         each do |method|
97         class << klass; self; end.class_eval do
98           define_method method.name do |*params|
99             self.api_exec method, *params
100           end
101         end
102       end
103
104       # Give the new class access to the API
105       klass.instance_eval do
106         @arvados = _arvados
107         # TODO: Pull these from the discovery document instead.
108         @api_models_sym = classname.underscore.split('/').last.pluralize.to_sym
109         @api_model_sym = classname.underscore.split('/').last.to_sym
110       end
111
112       # Create the new class in namespace_class so it doesn't
113       # interfere with classes created by other Arvados objects. The
114       # result looks like Arvados::A26949680::Job.
115       namespace_class.const_set classname, klass
116
117       self.define_singleton_method classname.underscore do
118         klass
119       end
120     end
121   end
122
123   def client
124     @client ||= ArvadosClient.
125       new(:host => config["ARVADOS_API_HOST"],
126           :application_name => @application_name,
127           :application_version => @application_version.to_s)
128   end
129
130   def arvados_api
131     @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
132   end
133
134   def self.debuglog(message, verbosity=1)
135     $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
136   end
137
138   def debuglog *args
139     self.class.debuglog *args
140   end
141
142   def config(config_file_path="~/.config/arvados/settings.conf")
143     return @config if @config
144
145     # Initialize config settings with environment variables.
146     config = {}
147     config['ARVADOS_API_HOST']          = ENV['ARVADOS_API_HOST']
148     config['ARVADOS_API_TOKEN']         = ENV['ARVADOS_API_TOKEN']
149     config['ARVADOS_API_HOST_INSECURE'] = ENV['ARVADOS_API_HOST_INSECURE']
150
151     if config['ARVADOS_API_HOST'] and config['ARVADOS_API_TOKEN']
152       # Environment variables take precedence over the config file, so
153       # there is no point reading the config file. If the environment
154       # specifies a _HOST without asking for _INSECURE, we certainly
155       # shouldn't give the config file a chance to create a
156       # system-wide _INSECURE state for this user.
157       #
158       # Note: If we start using additional configuration settings from
159       # this file in the future, we might have to read the file anyway
160       # instead of returning here.
161       return (@config = config)
162     end
163
164     begin
165       expanded_path = File.expand_path config_file_path
166       if File.exist? expanded_path
167         # Load settings from the config file.
168         lineno = 0
169         File.open(expanded_path).each do |line|
170           lineno = lineno + 1
171           # skip comments and blank lines
172           next if line.match('^\s*#') or not line.match('\S')
173           var, val = line.chomp.split('=', 2)
174           var.strip!
175           val.strip!
176           # allow environment settings to override config files.
177           if !var.empty? and val
178             config[var] ||= val
179           else
180             debuglog "#{expanded_path}: #{lineno}: could not parse `#{line}'", 0
181           end
182         end
183       end
184     rescue StandardError => e
185       debuglog "Ignoring error reading #{config_file_path}: #{e}", 0
186     end
187
188     @config = config
189   end
190
191   class Model
192     def self.arvados_api
193       arvados.arvados_api
194     end
195     def self.client
196       arvados.client
197     end
198     def self.debuglog(*args)
199       arvados.class.debuglog *args
200     end
201     def debuglog(*args)
202       self.class.arvados.class.debuglog *args
203     end
204     def self.api_exec(method, parameters={})
205       api_method = arvados_api.send(api_models_sym).send(method.name.to_sym)
206       parameters.each do |k,v|
207         parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
208       end
209       # Look for objects expected by request.properties.(key).$ref and
210       # move them from parameters (query string) to request body.
211       body = nil
212       method.discovery_document['request'].
213         andand['properties'].
214         andand.each do |k,v|
215         if v.is_a? Hash and v['$ref']
216           body ||= {}
217           body[k] = parameters.delete k.to_sym
218         end
219       end
220       result = client.
221         execute(:api_method => api_method,
222                 :authenticated => false,
223                 :parameters => parameters,
224                 :body_object => body,
225                 :headers => {
226                   :authorization => 'Bearer '+arvados.config['ARVADOS_API_TOKEN']
227                 })
228       resp = JSON.parse result.body, :symbolize_names => true
229       if resp[:errors]
230         if !resp[:errors][0].match(/.*req-[0-9a-zA-Z]{20}.*/)
231           resp[:errors][0] += " (#{result.headers['X-Request-Id'] or client.request_id})"
232         end
233         raise Arvados::TransactionFailedError.new(resp[:errors])
234       elsif resp[:uuid] and resp[:etag]
235         self.new(resp)
236       elsif resp[:items].is_a? Array
237         resp.merge(:items => resp[:items].collect do |i|
238                      self.new(i)
239                    end)
240       else
241         resp
242       end
243     end
244
245     def []=(x,y)
246       @attributes_to_update[x] = y
247       @attributes[x] = y
248     end
249     def [](x)
250       if @attributes[x].is_a? Hash or @attributes[x].is_a? Array
251         # We won't be notified via []= if these change, so we'll just
252         # assume they are going to get changed, and submit them if
253         # save() is called.
254         @attributes_to_update[x] = @attributes[x]
255       end
256       @attributes[x]
257     end
258     def save
259       @attributes_to_update.keys.each do |k|
260         @attributes_to_update[k] = @attributes[k]
261       end
262       j = self.class.api_exec :update, {
263         :uuid => @attributes[:uuid],
264         self.class.api_model_sym => @attributes_to_update.to_json
265       }
266       unless j.respond_to? :[] and j[:uuid]
267         debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
268         nil
269       else
270         @attributes_to_update = {}
271         @attributes = j
272       end
273     end
274
275     protected
276
277     def initialize(j)
278       @attributes_to_update = {}
279       @attributes = j
280     end
281   end
282
283   protected
284
285   def suppress_warnings
286     original_verbosity = $VERBOSE
287     begin
288       $VERBOSE = nil
289       yield
290     ensure
291       $VERBOSE = original_verbosity
292     end
293   end
294 end