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