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