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
25 attr_accessor :debuglevel
28 def initialize(opts={})
29 @application_version ||= 0.0
30 @application_name ||= File.split($0).last
32 @arvados_api_version = opts[:api_version] || 'v1'
34 @arvados_api_host = opts[:api_host] ||
35 config['ARVADOS_API_HOST'] or
36 raise "#{$0}: no :api_host or ENV[ARVADOS_API_HOST] provided."
37 @arvados_api_token = opts[:api_token] ||
38 config['ARVADOS_API_TOKEN'] or
39 raise "#{$0}: no :api_token or ENV[ARVADOS_API_TOKEN] provided."
41 if (opts[:suppress_ssl_warnings] or
42 %w(1 true yes).index(config['ARVADOS_API_HOST_INSECURE'].
45 OpenSSL::SSL.const_set 'VERIFY_PEER', OpenSSL::SSL::VERIFY_NONE
49 # Define a class and an Arvados instance method for each Arvados
50 # resource. After this, self.job will return Arvados::Job;
51 # self.job.new() and self.job.find() will do what you want.
53 namespace_class = Arvados.const_set "A#{self.object_id}", Class.new
54 self.arvados_api.schemas.each do |classname, schema|
55 next if classname.match /List$/
56 klass = Class.new(Arvados::Model) do
60 def self.api_models_sym
63 def self.api_model_sym
68 # Define the resource methods (create, get, update, delete, ...)
71 send(classname.underscore.split('/').last.pluralize.to_sym).
74 class << klass; self; end.class_eval do
75 define_method method.name do |*params|
76 self.api_exec method, *params
81 # Give the new class access to the API
82 klass.instance_eval do
84 # TODO: Pull these from the discovery document instead.
85 @api_models_sym = classname.underscore.split('/').last.pluralize.to_sym
86 @api_model_sym = classname.underscore.split('/').last.to_sym
89 # Create the new class in namespace_class so it doesn't
90 # interfere with classes created by other Arvados objects. The
91 # result looks like Arvados::A26949680::Job.
92 namespace_class.const_set classname, klass
94 self.class.class_eval do
95 define_method classname.underscore do
103 @client ||= Google::APIClient.
104 new(:host => @arvados_api_host,
105 :application_name => @application_name,
106 :application_version => @application_version.to_s)
110 @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
113 def self.debuglog(message, verbosity=1)
114 $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
118 self.class.debuglog *args
121 def config(config_file_path="~/.config/arvados/settings.conf")
122 return @@config if @@config
124 # Initialize config settings with environment variables.
126 config['ARVADOS_API_HOST'] = ENV['ARVADOS_API_HOST']
127 config['ARVADOS_API_TOKEN'] = ENV['ARVADOS_API_TOKEN']
128 config['ARVADOS_API_HOST_INSECURE'] = ENV['ARVADOS_API_HOST_INSECURE']
130 if config['ARVADOS_API_HOST'] and config['ARVADOS_API_TOKEN']
131 # Environment variables take precedence over the config file, so
132 # there is no point reading the config file. If the environment
133 # specifies a _HOST without asking for _INSECURE, we certainly
134 # shouldn't give the config file a chance to create a
135 # system-wide _INSECURE state for this user.
137 # Note: If we start using additional configuration settings from
138 # this file in the future, we might have to read the file anyway
139 # instead of returning here.
140 return (@@config = config)
144 expanded_path = File.expand_path config_file_path
145 if File.exist? expanded_path
146 # Load settings from the config file.
148 File.open(expanded_path).each do |line|
150 # skip comments and blank lines
151 next if line.match('^\s*#') or not line.match('\S')
152 var, val = line.chomp.split('=', 2)
155 # allow environment settings to override config files.
156 if !var.empty? and val
159 debuglog "#{expanded_path}: #{lineno}: could not parse `#{line}'", 0
163 rescue StandardError => e
164 debuglog "Ignoring error reading #{config_file_path}: #{e}", 0
177 def self.debuglog(*args)
178 arvados.class.debuglog *args
181 self.class.arvados.class.debuglog *args
183 def self.api_exec(method, parameters={})
184 api_method = arvados_api.send(api_models_sym).send(method.name.to_sym)
185 parameters.each do |k,v|
186 parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
188 # Look for objects expected by request.properties.(key).$ref and
189 # move them from parameters (query string) to request body.
191 method.discovery_document['request'].
192 andand['properties'].
194 if v.is_a? Hash and v['$ref']
196 body[k] = parameters.delete k.to_sym
200 execute(:api_method => api_method,
201 :authenticated => false,
202 :parameters => parameters,
203 :body_object => body,
205 :authorization => 'OAuth2 '+arvados.config['ARVADOS_API_TOKEN']
207 resp = JSON.parse result.body, :symbolize_names => true
209 raise Arvados::TransactionFailedError.new(resp[:errors])
210 elsif resp[:uuid] and resp[:etag]
212 elsif resp[:items].is_a? Array
213 resp.merge(:items => resp[:items].collect do |i|
222 @attributes_to_update[x] = y
226 if @attributes[x].is_a? Hash or @attributes[x].is_a? Array
227 # We won't be notified via []= if these change, so we'll just
228 # assume they are going to get changed, and submit them if
230 @attributes_to_update[x] = @attributes[x]
235 @attributes_to_update.keys.each do |k|
236 @attributes_to_update[k] = @attributes[k]
238 j = self.class.api_exec :update, {
239 :uuid => @attributes[:uuid],
240 self.class.api_model_sym => @attributes_to_update.to_json
242 unless j.respond_to? :[] and j[:uuid]
243 debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
246 @attributes_to_update = {}
254 @attributes_to_update = {}
261 def suppress_warnings
262 original_verbosity = $VERBOSE
267 $VERBOSE = original_verbosity