2 require 'google/api_client'
3 require 'active_support/inflector'
8 ActiveSupport::Inflector.inflections do |inflect|
9 inflect.irregular 'specimen', 'specimens'
10 inflect.irregular 'human', 'humans'
15 original_verbosity = $VERBOSE
18 $VERBOSE = original_verbosity
25 class TransactionFailedError < StandardError
31 attr_accessor :debuglevel
34 def initialize(opts={})
35 @application_version ||= 0.0
36 @application_name ||= File.split($0).last
38 @arvados_api_version = opts[:api_version] || 'v1'
40 @arvados_api_host = opts[:api_host] ||
41 config['ARVADOS_API_HOST'] or
42 raise "#{$0}: no :api_host or ENV[ARVADOS_API_HOST] provided."
43 @arvados_api_token = opts[:api_token] ||
44 config['ARVADOS_API_TOKEN'] or
45 raise "#{$0}: no :api_token or ENV[ARVADOS_API_TOKEN] provided."
47 if (opts[:suppress_ssl_warnings] or
48 %w(1 true yes).index(config['ARVADOS_API_HOST_INSECURE'].
51 OpenSSL::SSL.const_set 'VERIFY_PEER', OpenSSL::SSL::VERIFY_NONE
55 # Define a class and an Arvados instance method for each Arvados
56 # resource. After this, self.job will return Arvados::Job;
57 # self.job.new() and self.job.find() will do what you want.
59 namespace_class = Arvados.const_set "A#{self.object_id}", Class.new
60 self.arvados_api.schemas.each do |classname, schema|
61 next if classname.match /List$/
62 klass = Class.new(Arvados::Model) do
66 def self.api_models_sym
69 def self.api_model_sym
74 # Define the resource methods (create, get, update, delete, ...)
77 send(classname.underscore.split('/').last.pluralize.to_sym).
80 class << klass; self; end.class_eval do
81 define_method method.name do |*params|
82 self.api_exec method, *params
87 # Give the new class access to the API
88 klass.instance_eval do
90 # TODO: Pull these from the discovery document instead.
91 @api_models_sym = classname.underscore.split('/').last.pluralize.to_sym
92 @api_model_sym = classname.underscore.split('/').last.to_sym
95 # Create the new class in namespace_class so it doesn't
96 # interfere with classes created by other Arvados objects. The
97 # result looks like Arvados::A26949680::Job.
98 namespace_class.const_set classname, klass
100 self.class.class_eval do
101 define_method classname.underscore do
108 class Google::APIClient
109 def discovery_document(api, version)
111 return @discovery_documents["#{api}:#{version}"] ||=
113 # fetch new API discovery doc if stale
114 cached_doc = File.expand_path '~/.cache/arvados/discovery_uri.json'
115 if not File.exist?(cached_doc) or (Time.now - File.mtime(cached_doc)) > 86400
116 response = self.execute!(:http_method => :get,
117 :uri => self.discovery_uri(api, version),
118 :authenticated => false)
119 FileUtils.makedirs(File.dirname cached_doc)
120 File.open(cached_doc, 'w') do |f|
125 File.open(cached_doc) { |f| JSON.load f }
131 @client ||= Google::APIClient.
132 new(:host => @arvados_api_host,
133 :application_name => @application_name,
134 :application_version => @application_version.to_s)
138 @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
141 def self.debuglog(message, verbosity=1)
142 $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
146 self.class.debuglog *args
149 def config(config_file_path="~/.config/arvados/settings.conf")
150 return @@config if @@config
152 # Initialize config settings with environment variables.
154 config['ARVADOS_API_HOST'] = ENV['ARVADOS_API_HOST']
155 config['ARVADOS_API_TOKEN'] = ENV['ARVADOS_API_TOKEN']
156 config['ARVADOS_API_HOST_INSECURE'] = ENV['ARVADOS_API_HOST_INSECURE']
158 if config['ARVADOS_API_HOST'] and config['ARVADOS_API_TOKEN']
159 # Environment variables take precedence over the config file, so
160 # there is no point reading the config file. If the environment
161 # specifies a _HOST without asking for _INSECURE, we certainly
162 # shouldn't give the config file a chance to create a
163 # system-wide _INSECURE state for this user.
165 # Note: If we start using additional configuration settings from
166 # this file in the future, we might have to read the file anyway
167 # instead of returning here.
168 return (@@config = config)
172 expanded_path = File.expand_path config_file_path
173 if File.exist? expanded_path
174 # Load settings from the config file.
176 File.open(expanded_path).each do |line|
178 # skip comments and blank lines
179 next if line.match('^\s*#') or not line.match('\S')
180 var, val = line.chomp.split('=', 2)
183 # allow environment settings to override config files.
184 if !var.empty? and val
187 debuglog "#{expanded_path}: #{lineno}: could not parse `#{line}'", 0
191 rescue StandardError => e
192 debuglog "Ignoring error reading #{config_file_path}: #{e}", 0
205 def self.debuglog(*args)
206 arvados.class.debuglog *args
209 self.class.arvados.class.debuglog *args
211 def self.api_exec(method, parameters={})
212 api_method = arvados_api.send(api_models_sym).send(method.name.to_sym)
213 parameters.each do |k,v|
214 parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
216 # Look for objects expected by request.properties.(key).$ref and
217 # move them from parameters (query string) to request body.
219 method.discovery_document['request'].
220 andand['properties'].
222 if v.is_a? Hash and v['$ref']
224 body[k] = parameters.delete k.to_sym
228 execute(:api_method => api_method,
229 :authenticated => false,
230 :parameters => parameters,
233 authorization: 'OAuth2 '+arvados.config['ARVADOS_API_TOKEN']
235 resp = JSON.parse result.body, :symbolize_names => true
237 raise Arvados::TransactionFailedError.new(resp[:errors])
238 elsif resp[:uuid] and resp[:etag]
240 elsif resp[:items].is_a? Array
241 resp.merge(items: resp[:items].collect do |i|
250 @attributes_to_update[x] = y
254 if @attributes[x].is_a? Hash or @attributes[x].is_a? Array
255 # We won't be notified via []= if these change, so we'll just
256 # assume they are going to get changed, and submit them if
258 @attributes_to_update[x] = @attributes[x]
263 @attributes_to_update.keys.each do |k|
264 @attributes_to_update[k] = @attributes[k]
266 j = self.class.api_exec :update, {
267 :uuid => @attributes[:uuid],
268 self.class.api_model_sym => @attributes_to_update.to_json
270 unless j.respond_to? :[] and j[:uuid]
271 debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
274 @attributes_to_update = {}
282 @attributes_to_update = {}