+ def debuglog *args
+ self.class.debuglog(*args)
+ end
+
+ 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']
+
+ if config['ARVADOS_API_HOST'] and config['ARVADOS_API_TOKEN']
+ # Environment variables take precedence over the config file, so
+ # there is no point reading the config file. If the environment
+ # specifies a _HOST without asking for _INSECURE, we certainly
+ # shouldn't give the config file a chance to create a
+ # system-wide _INSECURE state for this user.
+ #
+ # Note: If we start using additional configuration settings from
+ # this file in the future, we might have to read the file anyway
+ # instead of returning here.
+ return (@config = config)
+ end
+
+ begin
+ 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)
+ var.strip!
+ val.strip!
+ # allow environment settings to override config files.
+ if !var.empty? and val
+ config[var] ||= val
+ else
+ debuglog "#{expanded_path}: #{lineno}: could not parse `#{line}'", 0
+ end
+ end
+ end
+ rescue StandardError => e
+ debuglog "Ignoring error reading #{config_file_path}: #{e}", 0
+ end
+
+ @config = config
+ end
+
+ def cluster_config
+ return @cluster_config if @cluster_config
+
+ uri = URI("https://#{config()["ARVADOS_API_HOST"]}/arvados/v1/config")
+ cc = JSON.parse(Net::HTTP.get(uri))
+
+ @cluster_config = cc
+ end
+