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 discovery_uri = self.discovery_uri(api, version)
112 discovery_uri_hash = Digest::MD5.hexdigest(discovery_uri)
113 return @discovery_documents[discovery_uri_hash] ||=
115 # fetch new API discovery doc if stale
116 cached_doc = File.expand_path "~/.cache/arvados/discovery-#{discovery_uri_hash}.json" rescue nil
117 if cached_doc.nil? or not File.exist?(cached_doc) or (Time.now - File.mtime(cached_doc)) > 86400
118 response = self.execute!(:http_method => :get,
119 :uri => discovery_uri,
120 :authenticated => false)
122 FileUtils.makedirs(File.dirname cached_doc)
123 File.open(cached_doc, 'w') do |f|
127 return JSON.load response.body
131 File.open(cached_doc) { |f| JSON.load f }
137 @client ||= Google::APIClient.
138 new(:host => @arvados_api_host,
139 :application_name => @application_name,
140 :application_version => @application_version.to_s)
144 @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
147 def self.debuglog(message, verbosity=1)
148 $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
152 self.class.debuglog *args
155 def config(config_file_path="~/.config/arvados/settings.conf")
156 return @@config if @@config
158 # Initialize config settings with environment variables.
160 config['ARVADOS_API_HOST'] = ENV['ARVADOS_API_HOST']
161 config['ARVADOS_API_TOKEN'] = ENV['ARVADOS_API_TOKEN']
162 config['ARVADOS_API_HOST_INSECURE'] = ENV['ARVADOS_API_HOST_INSECURE']
164 if config['ARVADOS_API_HOST'] and config['ARVADOS_API_TOKEN']
165 # Environment variables take precedence over the config file, so
166 # there is no point reading the config file. If the environment
167 # specifies a _HOST without asking for _INSECURE, we certainly
168 # shouldn't give the config file a chance to create a
169 # system-wide _INSECURE state for this user.
171 # Note: If we start using additional configuration settings from
172 # this file in the future, we might have to read the file anyway
173 # instead of returning here.
174 return (@@config = config)
178 expanded_path = File.expand_path config_file_path
179 if File.exist? expanded_path
180 # Load settings from the config file.
182 File.open(expanded_path).each do |line|
184 # skip comments and blank lines
185 next if line.match('^\s*#') or not line.match('\S')
186 var, val = line.chomp.split('=', 2)
189 # allow environment settings to override config files.
190 if !var.empty? and val
193 debuglog "#{expanded_path}: #{lineno}: could not parse `#{line}'", 0
197 rescue StandardError => e
198 debuglog "Ignoring error reading #{config_file_path}: #{e}", 0
211 def self.debuglog(*args)
212 arvados.class.debuglog *args
215 self.class.arvados.class.debuglog *args
217 def self.api_exec(method, parameters={})
218 api_method = arvados_api.send(api_models_sym).send(method.name.to_sym)
219 parameters.each do |k,v|
220 parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
222 # Look for objects expected by request.properties.(key).$ref and
223 # move them from parameters (query string) to request body.
225 method.discovery_document['request'].
226 andand['properties'].
228 if v.is_a? Hash and v['$ref']
230 body[k] = parameters.delete k.to_sym
234 execute(:api_method => api_method,
235 :authenticated => false,
236 :parameters => parameters,
239 authorization: 'OAuth2 '+arvados.config['ARVADOS_API_TOKEN']
241 resp = JSON.parse result.body, :symbolize_names => true
243 raise Arvados::TransactionFailedError.new(resp[:errors])
244 elsif resp[:uuid] and resp[:etag]
246 elsif resp[:items].is_a? Array
247 resp.merge(items: resp[:items].collect do |i|
256 @attributes_to_update[x] = y
260 if @attributes[x].is_a? Hash or @attributes[x].is_a? Array
261 # We won't be notified via []= if these change, so we'll just
262 # assume they are going to get changed, and submit them if
264 @attributes_to_update[x] = @attributes[x]
269 @attributes_to_update.keys.each do |k|
270 @attributes_to_update[k] = @attributes[k]
272 j = self.class.api_exec :update, {
273 :uuid => @attributes[:uuid],
274 self.class.api_model_sym => @attributes_to_update.to_json
276 unless j.respond_to? :[] and j[:uuid]
277 debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
280 @attributes_to_update = {}
288 @attributes_to_update = {}