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