1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
6 require 'active_support/inflector'
12 require 'arvados/google_api_client'
14 ActiveSupport::Inflector.inflections do |inflect|
15 inflect.irregular 'specimen', 'specimens'
16 inflect.irregular 'human', 'humans'
20 class ArvadosClient < Google::APIClient
21 attr_reader :request_id
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
32 if !e.message.match(/.*req-[0-9a-zA-Z]{20}.*/)
33 raise $!, "#{$!} (Request ID: #{@request_id})", $!.backtrace
40 class TransactionFailedError < StandardError
45 attr_accessor :debuglevel
48 def initialize(opts={})
49 @application_version ||= 0.0
50 @application_name ||= File.split($0).last
52 @arvados_api_version = opts[:api_version] || 'v1'
55 [[:api_host, 'ARVADOS_API_HOST'],
56 [:api_token, 'ARVADOS_API_TOKEN']].each do |op, en|
61 raise "#{$0}: no :#{op} or ENV[#{en}] provided."
65 if (opts[:suppress_ssl_warnings] or
66 %w(1 true yes).index(config['ARVADOS_API_HOST_INSECURE'].
69 OpenSSL::SSL.const_set 'VERIFY_PEER', OpenSSL::SSL::VERIFY_NONE
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.
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
84 def self.api_models_sym
87 def self.api_model_sym
92 # Define the resource methods (create, get, update, delete, ...)
95 send(classname.underscore.split('/').last.pluralize.to_sym).
98 class << klass; self; end.class_eval do
99 define_method method.name do |*params|
100 self.api_exec method, *params
105 # Give the new class access to the API
106 klass.instance_eval do
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
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
118 self.define_singleton_method classname.underscore do
125 @client ||= ArvadosClient.
126 new(:host => config["ARVADOS_API_HOST"],
127 :application_name => @application_name,
128 :application_version => @application_version.to_s)
132 @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
135 def self.debuglog(message, verbosity=1)
136 $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
140 self.class.debuglog(*args)
143 def config(config_file_path="~/.config/arvados/settings.conf")
144 return @config if @config
146 # Initialize config settings with environment variables.
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']
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.
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)
166 expanded_path = File.expand_path config_file_path
167 if File.exist? expanded_path
168 # Load settings from the config file.
170 File.open(expanded_path).each do |line|
172 # skip comments and blank lines
173 next if line.match('^\s*#') or not line.match('\S')
174 var, val = line.chomp.split('=', 2)
177 # allow environment settings to override config files.
178 if !var.empty? and val
181 debuglog "#{expanded_path}: #{lineno}: could not parse `#{line}'", 0
185 rescue StandardError => e
186 debuglog "Ignoring error reading #{config_file_path}: #{e}", 0
193 return @cluster_config if @cluster_config
195 uri = URI("https://#{config()["ARVADOS_API_HOST"]}/arvados/v1/config")
196 cc = JSON.parse(Net::HTTP.get(uri))
208 def self.debuglog(*args)
209 arvados.class.debuglog(*args)
212 self.class.arvados.class.debuglog(*args)
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
219 # Look for objects expected by request.properties.(key).$ref and
220 # move them from parameters (query string) to request body.
222 method.discovery_document['request'].
223 andand['properties'].
225 if v.is_a? Hash and v['$ref']
227 body[k] = parameters.delete k.to_sym
231 execute(:api_method => api_method,
232 :authenticated => false,
233 :parameters => parameters,
234 :body_object => body,
236 :authorization => 'Bearer '+arvados.config['ARVADOS_API_TOKEN']
238 resp = JSON.parse result.body, :symbolize_names => true
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})"
243 raise Arvados::TransactionFailedError.new(resp[:errors])
244 elsif resp[:uuid] and resp[:etag]
246 elsif resp[:items].is_a? Array
247 resp.merge(:items => resp[:items].collect do |i|
256 @attributes_to_update[x] = y
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
264 @attributes_to_update[x] = @attributes[x]
269 @attributes_to_update.keys.each do |k|
270 @attributes_to_update[k] = @attributes[k]
272 j = self.class.api_exec :update, {
273 :uuid => @attributes[:uuid],
274 self.class.api_model_sym => @attributes_to_update.to_json
276 unless j.respond_to? :[] and j[:uuid]
277 debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
280 @attributes_to_update = {}
288 @attributes_to_update = {}
295 def suppress_warnings
296 original_verbosity = $VERBOSE
301 $VERBOSE = original_verbosity