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] ||
39 config['ARVADOS_API_VERSION'] ||
41 @arvados_api_host = opts[:api_host] ||
42 config['ARVADOS_API_HOST'] or
43 raise "#{$0}: no :api_host or ENV[ARVADOS_API_HOST] provided."
44 @arvados_api_token = opts[:api_token] ||
45 config['ARVADOS_API_TOKEN'] or
46 raise "#{$0}: no :api_token or ENV[ARVADOS_API_TOKEN] provided."
48 if (opts[:suppress_ssl_warnings] or
49 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
145 def config(config_file_path="~/.config/arvados/settings.conf")
146 return @@config if @@config
148 # Initialize config settings with environment variables.
150 config['ARVADOS_API_HOST'] = ENV['ARVADOS_API_HOST']
151 config['ARVADOS_API_TOKEN'] = ENV['ARVADOS_API_TOKEN']
152 config['ARVADOS_API_HOST_INSECURE'] = ENV['ARVADOS_API_HOST_INSECURE']
153 config['ARVADOS_API_VERSION'] = ENV['ARVADOS_API_VERSION']
155 expanded_path = File.expand_path config_file_path
156 if File.exist? expanded_path
157 # Load settings from the config file.
159 File.open(expanded_path).each do |line|
161 # skip comments and blank lines
162 next if line.match('^\s*#') or not line.match('\S')
163 var, val = line.chomp.split('=', 2)
164 # allow environment settings to override config files.
168 warn "#{expanded_path}: #{lineno}: could not parse `#{line}'"
183 def self.debuglog(*args)
184 arvados.class.debuglog *args
187 self.class.arvados.class.debuglog *args
189 def self.api_exec(method, parameters={})
190 api_method = arvados_api.send(api_models_sym).send(method.name.to_sym)
191 parameters = parameters.
192 merge(:api_token => arvados.config['ARVADOS_API_TOKEN'])
193 parameters.each do |k,v|
194 parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
196 # Look for objects expected by request.properties.(key).$ref and
197 # move them from parameters (query string) to request body.
199 method.discovery_document['request'].
200 andand['properties'].
202 if v.is_a? Hash and v['$ref']
204 body[k] = parameters.delete k.to_sym
208 execute(:api_method => api_method,
209 :authenticated => false,
210 :parameters => parameters,
212 resp = JSON.parse result.body, :symbolize_names => true
214 raise Arvados::TransactionFailedError.new(resp[:errors])
215 elsif resp[:uuid] and resp[:etag]
217 elsif resp[:items].is_a? Array
218 resp.merge(items: resp[:items].collect do |i|
227 @attributes_to_update[x] = y
231 if @attributes[x].is_a? Hash or @attributes[x].is_a? Array
232 # We won't be notified via []= if these change, so we'll just
233 # assume they are going to get changed, and submit them if
235 @attributes_to_update[x] = @attributes[x]
240 @attributes_to_update.keys.each do |k|
241 @attributes_to_update[k] = @attributes[k]
243 j = self.class.api_exec :update, {
244 :uuid => @attributes[:uuid],
245 self.class.api_model_sym => @attributes_to_update.to_json
247 unless j.respond_to? :[] and j[:uuid]
248 debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
251 @attributes_to_update = {}
259 @attributes_to_update = {}