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