1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
6 require 'active_support/inflector'
11 require 'arvados/google_api_client'
13 ActiveSupport::Inflector.inflections do |inflect|
14 inflect.irregular 'specimen', 'specimens'
15 inflect.irregular 'human', 'humans'
19 class ArvadosClient < Google::APIClient
20 attr_reader :request_id
23 @request_id = "req-" + Random::DEFAULT.rand(2**128).to_s(36)[0..19]
24 if args.last.is_a? Hash
25 args.last[:headers] ||= {}
26 args.last[:headers]['X-Request-Id'] = @request_id
31 if !e.message.match(/.*req-[0-9a-zA-Z]{20}.*/)
32 raise $!, "#{$!} (Request ID: #{@request_id})", $!.backtrace
39 class TransactionFailedError < StandardError
44 attr_accessor :debuglevel
47 def initialize(opts={})
48 @application_version ||= 0.0
49 @application_name ||= File.split($0).last
51 @arvados_api_version = opts[:api_version] || 'v1'
54 [[:api_host, 'ARVADOS_API_HOST'],
55 [:api_token, 'ARVADOS_API_TOKEN']].each do |op, en|
60 raise "#{$0}: no :#{op} or ENV[#{en}] provided."
64 if (opts[:suppress_ssl_warnings] or
65 %w(1 true yes).index(config['ARVADOS_API_HOST_INSECURE'].
68 OpenSSL::SSL.const_set 'VERIFY_PEER', OpenSSL::SSL::VERIFY_NONE
72 # Define a class and an Arvados instance method for each Arvados
73 # resource. After this, self.job will return Arvados::Job;
74 # self.job.new() and self.job.find() will do what you want.
76 namespace_class = Arvados.const_set "A#{self.object_id}", Class.new
77 self.arvados_api.schemas.each do |classname, schema|
78 next if classname.match /List$/
79 klass = Class.new(Arvados::Model) do
83 def self.api_models_sym
86 def self.api_model_sym
91 # Define the resource methods (create, get, update, delete, ...)
94 send(classname.underscore.split('/').last.pluralize.to_sym).
97 class << klass; self; end.class_eval do
98 define_method method.name do |*params|
99 self.api_exec method, *params
104 # Give the new class access to the API
105 klass.instance_eval do
107 # TODO: Pull these from the discovery document instead.
108 @api_models_sym = classname.underscore.split('/').last.pluralize.to_sym
109 @api_model_sym = classname.underscore.split('/').last.to_sym
112 # Create the new class in namespace_class so it doesn't
113 # interfere with classes created by other Arvados objects. The
114 # result looks like Arvados::A26949680::Job.
115 namespace_class.const_set classname, klass
117 self.define_singleton_method classname.underscore do
124 @client ||= ArvadosClient.
125 new(:host => config["ARVADOS_API_HOST"],
126 :application_name => @application_name,
127 :application_version => @application_version.to_s)
131 @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
134 def self.debuglog(message, verbosity=1)
135 $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
139 self.class.debuglog *args
142 def config(config_file_path="~/.config/arvados/settings.conf")
143 return @config if @config
145 # Initialize config settings with environment variables.
147 config['ARVADOS_API_HOST'] = ENV['ARVADOS_API_HOST']
148 config['ARVADOS_API_TOKEN'] = ENV['ARVADOS_API_TOKEN']
149 config['ARVADOS_API_HOST_INSECURE'] = ENV['ARVADOS_API_HOST_INSECURE']
151 if config['ARVADOS_API_HOST'] and config['ARVADOS_API_TOKEN']
152 # Environment variables take precedence over the config file, so
153 # there is no point reading the config file. If the environment
154 # specifies a _HOST without asking for _INSECURE, we certainly
155 # shouldn't give the config file a chance to create a
156 # system-wide _INSECURE state for this user.
158 # Note: If we start using additional configuration settings from
159 # this file in the future, we might have to read the file anyway
160 # instead of returning here.
161 return (@config = config)
165 expanded_path = File.expand_path config_file_path
166 if File.exist? expanded_path
167 # Load settings from the config file.
169 File.open(expanded_path).each do |line|
171 # skip comments and blank lines
172 next if line.match('^\s*#') or not line.match('\S')
173 var, val = line.chomp.split('=', 2)
176 # allow environment settings to override config files.
177 if !var.empty? and val
180 debuglog "#{expanded_path}: #{lineno}: could not parse `#{line}'", 0
184 rescue StandardError => e
185 debuglog "Ignoring error reading #{config_file_path}: #{e}", 0
198 def self.debuglog(*args)
199 arvados.class.debuglog *args
202 self.class.arvados.class.debuglog *args
204 def self.api_exec(method, parameters={})
205 api_method = arvados_api.send(api_models_sym).send(method.name.to_sym)
206 parameters.each do |k,v|
207 parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
209 # Look for objects expected by request.properties.(key).$ref and
210 # move them from parameters (query string) to request body.
212 method.discovery_document['request'].
213 andand['properties'].
215 if v.is_a? Hash and v['$ref']
217 body[k] = parameters.delete k.to_sym
221 execute(:api_method => api_method,
222 :authenticated => false,
223 :parameters => parameters,
224 :body_object => body,
226 :authorization => 'Bearer '+arvados.config['ARVADOS_API_TOKEN']
228 resp = JSON.parse result.body, :symbolize_names => true
230 if !resp[:errors][0].match(/.*req-[0-9a-zA-Z]{20}.*/)
231 resp[:errors][0] += " (#{result.headers['X-Request-Id'] or client.request_id})"
233 raise Arvados::TransactionFailedError.new(resp[:errors])
234 elsif resp[:uuid] and resp[:etag]
236 elsif resp[:items].is_a? Array
237 resp.merge(:items => resp[:items].collect do |i|
246 @attributes_to_update[x] = y
250 if @attributes[x].is_a? Hash or @attributes[x].is_a? Array
251 # We won't be notified via []= if these change, so we'll just
252 # assume they are going to get changed, and submit them if
254 @attributes_to_update[x] = @attributes[x]
259 @attributes_to_update.keys.each do |k|
260 @attributes_to_update[k] = @attributes[k]
262 j = self.class.api_exec :update, {
263 :uuid => @attributes[:uuid],
264 self.class.api_model_sym => @attributes_to_update.to_json
266 unless j.respond_to? :[] and j[:uuid]
267 debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
270 @attributes_to_update = {}
278 @attributes_to_update = {}
285 def suppress_warnings
286 original_verbosity = $VERBOSE
291 $VERBOSE = original_verbosity