2 require 'active_support/inflector'
7 require 'arvados/google_api_client'
9 ActiveSupport::Inflector.inflections do |inflect|
10 inflect.irregular 'specimen', 'specimens'
11 inflect.irregular 'human', 'humans'
15 class TransactionFailedError < StandardError
21 attr_accessor :debuglevel
24 def initialize(opts={})
25 @application_version ||= 0.0
26 @application_name ||= File.split($0).last
28 @arvados_api_version = opts[:api_version] || 'v1'
30 @arvados_api_host = opts[:api_host] ||
31 config['ARVADOS_API_HOST'] or
32 raise "#{$0}: no :api_host or ENV[ARVADOS_API_HOST] provided."
33 @arvados_api_token = opts[:api_token] ||
34 config['ARVADOS_API_TOKEN'] or
35 raise "#{$0}: no :api_token or ENV[ARVADOS_API_TOKEN] provided."
37 if (opts[:suppress_ssl_warnings] or
38 %w(1 true yes).index(config['ARVADOS_API_HOST_INSECURE'].
41 OpenSSL::SSL.const_set 'VERIFY_PEER', OpenSSL::SSL::VERIFY_NONE
45 # Define a class and an Arvados instance method for each Arvados
46 # resource. After this, self.job will return Arvados::Job;
47 # self.job.new() and self.job.find() will do what you want.
49 namespace_class = Arvados.const_set "A#{self.object_id}", Class.new
50 self.arvados_api.schemas.each do |classname, schema|
51 next if classname.match /List$/
52 klass = Class.new(Arvados::Model) do
56 def self.api_models_sym
59 def self.api_model_sym
64 # Define the resource methods (create, get, update, delete, ...)
67 send(classname.underscore.split('/').last.pluralize.to_sym).
70 class << klass; self; end.class_eval do
71 define_method method.name do |*params|
72 self.api_exec method, *params
77 # Give the new class access to the API
78 klass.instance_eval do
80 # TODO: Pull these from the discovery document instead.
81 @api_models_sym = classname.underscore.split('/').last.pluralize.to_sym
82 @api_model_sym = classname.underscore.split('/').last.to_sym
85 # Create the new class in namespace_class so it doesn't
86 # interfere with classes created by other Arvados objects. The
87 # result looks like Arvados::A26949680::Job.
88 namespace_class.const_set classname, klass
90 self.class.class_eval do
91 define_method classname.underscore do
99 @client ||= Google::APIClient.
100 new(:host => @arvados_api_host,
101 :application_name => @application_name,
102 :application_version => @application_version.to_s)
106 @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
109 def self.debuglog(message, verbosity=1)
110 $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
114 self.class.debuglog *args
117 def config(config_file_path="~/.config/arvados/settings.conf")
118 return @@config if @@config
120 # Initialize config settings with environment variables.
122 config['ARVADOS_API_HOST'] = ENV['ARVADOS_API_HOST']
123 config['ARVADOS_API_TOKEN'] = ENV['ARVADOS_API_TOKEN']
124 config['ARVADOS_API_HOST_INSECURE'] = ENV['ARVADOS_API_HOST_INSECURE']
126 if config['ARVADOS_API_HOST'] and config['ARVADOS_API_TOKEN']
127 # Environment variables take precedence over the config file, so
128 # there is no point reading the config file. If the environment
129 # specifies a _HOST without asking for _INSECURE, we certainly
130 # shouldn't give the config file a chance to create a
131 # system-wide _INSECURE state for this user.
133 # Note: If we start using additional configuration settings from
134 # this file in the future, we might have to read the file anyway
135 # instead of returning here.
136 return (@@config = config)
140 expanded_path = File.expand_path config_file_path
141 if File.exist? expanded_path
142 # Load settings from the config file.
144 File.open(expanded_path).each do |line|
146 # skip comments and blank lines
147 next if line.match('^\s*#') or not line.match('\S')
148 var, val = line.chomp.split('=', 2)
151 # allow environment settings to override config files.
152 if !var.empty? and val
155 debuglog "#{expanded_path}: #{lineno}: could not parse `#{line}'", 0
159 rescue StandardError => e
160 debuglog "Ignoring error reading #{config_file_path}: #{e}", 0
173 def self.debuglog(*args)
174 arvados.class.debuglog *args
177 self.class.arvados.class.debuglog *args
179 def self.api_exec(method, parameters={})
180 api_method = arvados_api.send(api_models_sym).send(method.name.to_sym)
181 parameters.each do |k,v|
182 parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
184 # Look for objects expected by request.properties.(key).$ref and
185 # move them from parameters (query string) to request body.
187 method.discovery_document['request'].
188 andand['properties'].
190 if v.is_a? Hash and v['$ref']
192 body[k] = parameters.delete k.to_sym
196 execute(:api_method => api_method,
197 :authenticated => false,
198 :parameters => parameters,
199 :body_object => body,
201 :authorization => 'OAuth2 '+arvados.config['ARVADOS_API_TOKEN']
203 resp = JSON.parse result.body, :symbolize_names => true
205 raise Arvados::TransactionFailedError.new(resp[:errors])
206 elsif resp[:uuid] and resp[:etag]
208 elsif resp[:items].is_a? Array
209 resp.merge(:items => resp[:items].collect do |i|
218 @attributes_to_update[x] = y
222 if @attributes[x].is_a? Hash or @attributes[x].is_a? Array
223 # We won't be notified via []= if these change, so we'll just
224 # assume they are going to get changed, and submit them if
226 @attributes_to_update[x] = @attributes[x]
231 @attributes_to_update.keys.each do |k|
232 @attributes_to_update[k] = @attributes[k]
234 j = self.class.api_exec :update, {
235 :uuid => @attributes[:uuid],
236 self.class.api_model_sym => @attributes_to_update.to_json
238 unless j.respond_to? :[] and j[:uuid]
239 debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
242 @attributes_to_update = {}
250 @attributes_to_update = {}
257 def suppress_warnings
258 original_verbosity = $VERBOSE
263 $VERBOSE = original_verbosity