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