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 TransactionFailedError < StandardError
24 attr_accessor :debuglevel
27 def initialize(opts={})
28 @application_version ||= 0.0
29 @application_name ||= File.split($0).last
31 @arvados_api_version = opts[:api_version] || 'v1'
34 [[:api_host, 'ARVADOS_API_HOST'],
35 [:api_token, 'ARVADOS_API_TOKEN']].each do |op, en|
40 raise "#{$0}: no :#{op} or ENV[#{en}] provided."
44 if (opts[:suppress_ssl_warnings] or
45 %w(1 true yes).index(config['ARVADOS_API_HOST_INSECURE'].
48 OpenSSL::SSL.const_set 'VERIFY_PEER', OpenSSL::SSL::VERIFY_NONE
52 # Define a class and an Arvados instance method for each Arvados
53 # resource. After this, self.job will return Arvados::Job;
54 # self.job.new() and self.job.find() will do what you want.
56 namespace_class = Arvados.const_set "A#{self.object_id}", Class.new
57 self.arvados_api.schemas.each do |classname, schema|
58 next if classname.match /List$/
59 klass = Class.new(Arvados::Model) do
63 def self.api_models_sym
66 def self.api_model_sym
71 # Define the resource methods (create, get, update, delete, ...)
74 send(classname.underscore.split('/').last.pluralize.to_sym).
77 class << klass; self; end.class_eval do
78 define_method method.name do |*params|
79 self.api_exec method, *params
84 # Give the new class access to the API
85 klass.instance_eval do
87 # TODO: Pull these from the discovery document instead.
88 @api_models_sym = classname.underscore.split('/').last.pluralize.to_sym
89 @api_model_sym = classname.underscore.split('/').last.to_sym
92 # Create the new class in namespace_class so it doesn't
93 # interfere with classes created by other Arvados objects. The
94 # result looks like Arvados::A26949680::Job.
95 namespace_class.const_set classname, klass
97 self.define_singleton_method classname.underscore do
104 @client ||= Google::APIClient.
105 new(:host => config["ARVADOS_API_HOST"],
106 :application_name => @application_name,
107 :application_version => @application_version.to_s)
111 @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
114 def self.debuglog(message, verbosity=1)
115 $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
119 self.class.debuglog *args
122 def config(config_file_path="~/.config/arvados/settings.conf")
123 return @config if @config
125 # Initialize config settings with environment variables.
127 config['ARVADOS_API_HOST'] = ENV['ARVADOS_API_HOST']
128 config['ARVADOS_API_TOKEN'] = ENV['ARVADOS_API_TOKEN']
129 config['ARVADOS_API_HOST_INSECURE'] = ENV['ARVADOS_API_HOST_INSECURE']
131 if config['ARVADOS_API_HOST'] and config['ARVADOS_API_TOKEN']
132 # Environment variables take precedence over the config file, so
133 # there is no point reading the config file. If the environment
134 # specifies a _HOST without asking for _INSECURE, we certainly
135 # shouldn't give the config file a chance to create a
136 # system-wide _INSECURE state for this user.
138 # Note: If we start using additional configuration settings from
139 # this file in the future, we might have to read the file anyway
140 # instead of returning here.
141 return (@config = config)
145 expanded_path = File.expand_path config_file_path
146 if File.exist? expanded_path
147 # Load settings from the config file.
149 File.open(expanded_path).each do |line|
151 # skip comments and blank lines
152 next if line.match('^\s*#') or not line.match('\S')
153 var, val = line.chomp.split('=', 2)
156 # allow environment settings to override config files.
157 if !var.empty? and val
160 debuglog "#{expanded_path}: #{lineno}: could not parse `#{line}'", 0
164 rescue StandardError => e
165 debuglog "Ignoring error reading #{config_file_path}: #{e}", 0
178 def self.debuglog(*args)
179 arvados.class.debuglog *args
182 self.class.arvados.class.debuglog *args
184 def self.api_exec(method, parameters={})
185 api_method = arvados_api.send(api_models_sym).send(method.name.to_sym)
186 parameters.each do |k,v|
187 parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
189 # Look for objects expected by request.properties.(key).$ref and
190 # move them from parameters (query string) to request body.
192 method.discovery_document['request'].
193 andand['properties'].
195 if v.is_a? Hash and v['$ref']
197 body[k] = parameters.delete k.to_sym
201 execute(:api_method => api_method,
202 :authenticated => false,
203 :parameters => parameters,
204 :body_object => body,
206 :authorization => 'Bearer '+arvados.config['ARVADOS_API_TOKEN']
208 resp = JSON.parse result.body, :symbolize_names => true
210 raise Arvados::TransactionFailedError.new(resp[:errors])
211 elsif resp[:uuid] and resp[:etag]
213 elsif resp[:items].is_a? Array
214 resp.merge(:items => resp[:items].collect do |i|
223 @attributes_to_update[x] = y
227 if @attributes[x].is_a? Hash or @attributes[x].is_a? Array
228 # We won't be notified via []= if these change, so we'll just
229 # assume they are going to get changed, and submit them if
231 @attributes_to_update[x] = @attributes[x]
236 @attributes_to_update.keys.each do |k|
237 @attributes_to_update[k] = @attributes[k]
239 j = self.class.api_exec :update, {
240 :uuid => @attributes[:uuid],
241 self.class.api_model_sym => @attributes_to_update.to_json
243 unless j.respond_to? :[] and j[:uuid]
244 debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
247 @attributes_to_update = {}
255 @attributes_to_update = {}
262 def suppress_warnings
263 original_verbosity = $VERBOSE
268 $VERBOSE = original_verbosity