2 require 'google/api_client'
3 require 'active_support/inflector'
6 ActiveSupport::Inflector.inflections do |inflect|
7 inflect.irregular 'specimen', 'specimens'
8 inflect.irregular 'human', 'humans'
13 original_verbosity = $VERBOSE
16 $VERBOSE = original_verbosity
23 class TransactionFailedError < StandardError
28 attr_accessor :debuglevel
31 def initialize(opts={})
32 @application_version ||= 0.0
33 @application_name ||= File.split($0).last
35 @arvados_api_version = opts[:api_version] ||
36 ENV['ARVADOS_API_VERSION'] ||
38 @arvados_api_host = opts[:api_host] ||
39 ENV['ARVADOS_API_HOST'] or
40 raise "#{$0}: no :api_host or ENV[ARVADOS_API_HOST] provided."
41 @arvados_api_token = opts[:api_token] ||
42 ENV['ARVADOS_API_TOKEN'] or
43 raise "#{$0}: no :api_token or ENV[ARVADOS_API_TOKEN] provided."
45 if (opts[:api_host] ? opts[:suppress_ssl_warnings] :
46 ENV['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.name.to_sym, *params)
84 # Give the new class access to the API
85 klass.instance_eval do
87 # These should be pulled 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.class.class_eval do
98 define_method classname.underscore do
105 class Google::APIClient
106 def discovery_document(api, version)
108 return @discovery_documents["#{api}:#{version}"] ||=
110 response = self.execute!(
111 :http_method => :get,
112 :uri => self.discovery_uri(api, version),
113 :authenticated => false
115 response.body.class == String ? JSON.parse(response.body) : response.body
121 @client ||= Google::APIClient.
122 new(:host => @arvados_api_host,
123 :application_name => @application_name,
124 :application_version => @application_version.to_s)
128 @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
131 def self.debuglog(message, verbosity=1)
132 $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
142 def self.debuglog(*args)
143 arvados.class.debuglog *args
146 self.class.arvados.class.debuglog *args
148 def self.api_exec(method, parameters={})
149 parameters = parameters.
150 merge(:api_token => ENV['ARVADOS_API_TOKEN'])
151 parameters.each do |k,v|
152 parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
155 execute(:api_method => arvados_api.send(api_models_sym).send(method),
156 :authenticated => false,
157 :parameters => parameters)
158 resp = JSON.parse result.body, :symbolize_names => true
160 raise Arvados::TransactionFailedError.new(resp[:errors])
161 elsif resp[:uuid] and resp[:etag]
163 elsif resp[:items].is_a? Array
164 resp.merge(items: resp[:items].collect do |i|
173 @attributes_to_update[x] = y
177 if @attributes[x].is_a? Hash or @attributes[x].is_a? Array
178 # We won't be notified via []= if these change, so we'll just
179 # assume they are going to get changed, and submit them if
181 @attributes_to_update[x] = @attributes[x]
186 @attributes_to_update.keys.each do |k|
187 @attributes_to_update[k] = @attributes[k]
189 j = self.class.api_exec :update, {
190 :uuid => @attributes[:uuid],
191 self.class.api_model_sym => @attributes_to_update.to_json
193 unless j.respond_to? :[] and j[:uuid]
194 debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
197 @attributes_to_update = {}
205 @attributes_to_update = {}