Clients use $HOME/.config/arvados/settings.conf for configuration. (fixes #1943)
[arvados.git] / sdk / ruby / lib / arvados.rb
1 require 'rubygems'
2 require 'google/api_client'
3 require 'active_support/inflector'
4 require 'json'
5
6 ActiveSupport::Inflector.inflections do |inflect|
7   inflect.irregular 'specimen', 'specimens'
8   inflect.irregular 'human', 'humans'
9 end
10
11 module Kernel
12   def suppress_warnings
13     original_verbosity = $VERBOSE
14     $VERBOSE = nil
15     result = yield
16     $VERBOSE = original_verbosity
17     return result
18   end
19 end
20
21 class Arvados
22
23   class TransactionFailedError < StandardError
24   end
25
26   @@debuglevel = 0
27   class << self
28     attr_accessor :debuglevel
29   end
30
31   def initialize(opts={})
32     @application_version ||= 0.0
33     @application_name ||= File.split($0).last
34
35     @config = self.load_config_file
36
37     @arvados_api_version = opts[:api_version] ||
38       @config['ARVADOS_API_VERSION'] ||
39       '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."
46
47     if (opts[:suppress_ssl_warnings] or
48         @config['ARVADOS_API_HOST_INSECURE'])
49       suppress_warnings do
50         OpenSSL::SSL.const_set 'VERIFY_PEER', OpenSSL::SSL::VERIFY_NONE
51       end
52     end
53
54     # Define a class and an Arvados instance method for each Arvados
55     # resource. After this, self.job will return Arvados::Job;
56     # self.job.new() and self.job.find() will do what you want.
57     _arvados = self
58     namespace_class = Arvados.const_set "A#{self.object_id}", Class.new
59     self.arvados_api.schemas.each do |classname, schema|
60       next if classname.match /List$/
61       klass = Class.new(Arvados::Model) do
62         def self.arvados
63           @arvados
64         end
65         def self.api_models_sym
66           @api_models_sym
67         end
68         def self.api_model_sym
69           @api_model_sym
70         end
71       end
72
73       # Define the resource methods (create, get, update, delete, ...)
74       self.
75         arvados_api.
76         send(classname.underscore.split('/').last.pluralize.to_sym).
77         discovered_methods.
78         each do |method|
79         class << klass; self; end.class_eval do
80           define_method method.name do |*params|
81             self.api_exec(method.name.to_sym, *params)
82           end
83         end
84       end
85
86       # Give the new class access to the API
87       klass.instance_eval do
88         @arvados = _arvados
89         # These should be pulled from the discovery document instead:
90         @api_models_sym = classname.underscore.split('/').last.pluralize.to_sym
91         @api_model_sym = classname.underscore.split('/').last.to_sym
92       end
93
94       # Create the new class in namespace_class so it doesn't
95       # interfere with classes created by other Arvados objects. The
96       # result looks like Arvados::A26949680::Job.
97       namespace_class.const_set classname, klass
98
99       self.class.class_eval do
100         define_method classname.underscore do
101           klass
102         end
103       end
104     end
105   end
106
107   class Google::APIClient
108     def discovery_document(api, version)
109       api = api.to_s
110       return @discovery_documents["#{api}:#{version}"] ||=
111         begin
112           response = self.execute!(
113                                    :http_method => :get,
114                                    :uri => self.discovery_uri(api, version),
115                                    :authenticated => false
116                                    )
117           response.body.class == String ? JSON.parse(response.body) : response.body
118         end
119     end
120   end
121
122   def client
123     @client ||= Google::APIClient.
124       new(:host => @arvados_api_host,
125           :application_name => @application_name,
126           :application_version => @application_version.to_s)
127   end
128
129   def arvados_api
130     @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
131   end
132
133   def self.debuglog(message, verbosity=1)
134     $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
135   end
136
137   def load_config_file(config_file_path="~/.config/arvados/settings.conf")
138     # Initialize config settings with environment variables.
139     config = {}
140     config['ARVADOS_API_HOST']          = ENV['ARVADOS_API_HOST']
141     config['ARVADOS_API_TOKEN']         = ENV['ARVADOS_API_TOKEN']
142     config['ARVADOS_API_HOST_INSECURE'] = ENV['ARVADOS_API_HOST_INSECURE']
143     config['ARVADOS_API_VERSION']       = ENV['ARVADOS_API_VERSION']
144
145     # Load settings from the config file.
146     lineno = 0
147     File.open(File.expand_path config_file_path).each do |line|
148       lineno = lineno + 1
149       # skip comments and blank lines
150       next if line.match('^\s*#') or not line.match('\S')
151       var, val = line.chomp.split('=', 2)
152       # allow environment settings to override config files.
153       if var and val
154         config[var] ||= val
155       else
156         warn "#{config_file}: #{lineno}: could not parse `#{line}'"
157       end
158     end
159
160     config
161   end
162
163   class Model
164     def self.arvados_api
165       arvados.arvados_api
166     end
167     def self.client
168       arvados.client
169     end
170     def self.debuglog(*args)
171       arvados.class.debuglog *args
172     end
173     def debuglog(*args)
174       self.class.arvados.class.debuglog *args
175     end
176     def self.api_exec(method, parameters={})
177       parameters = parameters.
178         merge(:api_token => @config['ARVADOS_API_TOKEN'])
179       parameters.each do |k,v|
180         parameters[k] = v.to_json if v.is_a? Array or v.is_a? Hash
181       end
182       result = client.
183         execute(:api_method => arvados_api.send(api_models_sym).send(method),
184                 :authenticated => false,
185                 :parameters => parameters)
186       resp = JSON.parse result.body, :symbolize_names => true
187       if resp[:errors]
188         raise Arvados::TransactionFailedError.new(resp[:errors])
189       elsif resp[:uuid] and resp[:etag]
190         self.new(resp)
191       elsif resp[:items].is_a? Array
192         resp.merge(items: resp[:items].collect do |i|
193                      self.new(i)
194                    end)
195       else
196         resp
197       end
198     end
199
200     def []=(x,y)
201       @attributes_to_update[x] = y
202       @attributes[x] = y
203     end
204     def [](x)
205       if @attributes[x].is_a? Hash or @attributes[x].is_a? Array
206         # We won't be notified via []= if these change, so we'll just
207         # assume they are going to get changed, and submit them if
208         # save() is called.
209         @attributes_to_update[x] = @attributes[x]
210       end
211       @attributes[x]
212     end
213     def save
214       @attributes_to_update.keys.each do |k|
215         @attributes_to_update[k] = @attributes[k]
216       end
217       j = self.class.api_exec :update, {
218         :uuid => @attributes[:uuid],
219         self.class.api_model_sym => @attributes_to_update.to_json
220       }
221       unless j.respond_to? :[] and j[:uuid]
222         debuglog "Failed to save #{self.to_s}: #{j[:errors] rescue nil}", 0
223         nil
224       else
225         @attributes_to_update = {}
226         @attributes = j
227       end
228     end
229
230     protected
231
232     def initialize(j)
233       @attributes_to_update = {}
234       @attributes = j
235     end
236   end
237 end