Class: Arvados

Inherits:
Object
  • Object
show all
Defined in:
lib/arvados.rb

Defined Under Namespace

Classes: Model, TransactionFailedError

Constant Summary collapse

@@config =
nil
@@debuglevel =
0

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Arvados

Returns a new instance of Arvados.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/arvados.rb', line 34

def initialize(opts={})
  @application_version ||= 0.0
  @application_name ||= File.split($0).last

  @arvados_api_version = opts[:api_version] ||
    config['ARVADOS_API_VERSION'] ||
    'v1'
  @arvados_api_host = opts[:api_host] ||
    config['ARVADOS_API_HOST'] or
    raise "#{$0}: no :api_host or ENV[ARVADOS_API_HOST] provided."
  @arvados_api_token = opts[:api_token] ||
    config['ARVADOS_API_TOKEN'] or
    raise "#{$0}: no :api_token or ENV[ARVADOS_API_TOKEN] provided."

  if (opts[:suppress_ssl_warnings] or
      config['ARVADOS_API_HOST_INSECURE'])
    suppress_warnings do
      OpenSSL::SSL.const_set 'VERIFY_PEER', OpenSSL::SSL::VERIFY_NONE
    end
  end

  # Define a class and an Arvados instance method for each Arvados
  # resource. After this, self.job will return Arvados::Job;
  # self.job.new() and self.job.find() will do what you want.
  _arvados = self
  namespace_class = Arvados.const_set "A#{self.object_id}", Class.new
  self.arvados_api.schemas.each do |classname, schema|
    next if classname.match /List$/
    klass = Class.new(Arvados::Model) do
      def self.arvados
        @arvados
      end
      def self.api_models_sym
        @api_models_sym
      end
      def self.api_model_sym
        @api_model_sym
      end
    end

    # Define the resource methods (create, get, update, delete, ...)
    self.
      arvados_api.
      send(classname.underscore.split('/').last.pluralize.to_sym).
      discovered_methods.
      each do |method|
      class << klass; self; end.class_eval do
        define_method method.name do |*params|
          self.api_exec method, *params
        end
      end
    end

    # Give the new class access to the API
    klass.instance_eval do
      @arvados = _arvados
      # TODO: Pull these from the discovery document instead.
      @api_models_sym = classname.underscore.split('/').last.pluralize.to_sym
      @api_model_sym = classname.underscore.split('/').last.to_sym
    end

    # Create the new class in namespace_class so it doesn't
    # interfere with classes created by other Arvados objects. The
    # result looks like Arvados::A26949680::Job.
    namespace_class.const_set classname, klass

    self.class.class_eval do
      define_method classname.underscore do
        klass
      end
    end
  end
end

Class Attribute Details

.debuglevelObject

Returns the value of attribute debuglevel.



31
32
33
# File 'lib/arvados.rb', line 31

def debuglevel
  @debuglevel
end

Class Method Details

.debuglog(message, verbosity = 1) ⇒ Object



141
142
143
# File 'lib/arvados.rb', line 141

def self.debuglog(message, verbosity=1)
  $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if @@debuglevel >= verbosity
end

Instance Method Details

#arvados_apiObject



137
138
139
# File 'lib/arvados.rb', line 137

def arvados_api
  @arvados_api ||= self.client.discovered_api('arvados', @arvados_api_version)
end

#clientObject



130
131
132
133
134
135
# File 'lib/arvados.rb', line 130

def client
  @client ||= Google::APIClient.
    new(:host => @arvados_api_host,
        :application_name => @application_name,
        :application_version => @application_version.to_s)
end

#config(config_file_path = "~/.config/arvados/settings.conf") ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/arvados.rb', line 145

def config(config_file_path="~/.config/arvados/settings.conf")
  return @@config if @@config

  # Initialize config settings with environment variables.
  config = {}
  config['ARVADOS_API_HOST']          = ENV['ARVADOS_API_HOST']
  config['ARVADOS_API_TOKEN']         = ENV['ARVADOS_API_TOKEN']
  config['ARVADOS_API_HOST_INSECURE'] = ENV['ARVADOS_API_HOST_INSECURE']
  config['ARVADOS_API_VERSION']       = ENV['ARVADOS_API_VERSION']

  expanded_path = File.expand_path config_file_path
  if File.exist? expanded_path
    # Load settings from the config file.
    lineno = 0
    File.open(expanded_path).each do |line|
      lineno = lineno + 1
      # skip comments and blank lines
      next if line.match('^\s*#') or not line.match('\S')
      var, val = line.chomp.split('=', 2)
      # allow environment settings to override config files.
      if var and val
        config[var] ||= val
      else
        warn "#{expanded_path}: #{lineno}: could not parse `#{line}'"
      end
    end
  end

  @@config = config
end