#
# Ward Vandewege <ward@clinicalfuture.com>
+require 'fileutils'
+
if RUBY_VERSION < '1.9.3' then
abort <<-EOS
#{$0.gsub(/^\.\//,'')} requires Ruby version 1.9.3 or higher.
EOS
end
-# read authentication data from ~/.config/arvados if present
+# read authentication data from arvados configuration file if present
lineno = 0
-config_file = File.expand_path('~/.config/arvados')
+config_file = File.expand_path('~/.config/arvados/settings.conf')
if File.exist? config_file then
File.open(config_file, 'r').each do |line|
lineno = lineno + 1
when 'keep'
ARGV.shift
@sub = ARGV.shift
- if ['get', 'put'].index @sub then
+ if ['get', 'put', 'ls', 'normalize'].index @sub then
# Native Arvados
exec `which arv-#{@sub}`.strip, *ARGV
- elsif ['ls', 'less', 'check'].index @sub then
+ elsif ['less', 'check'].index @sub then
# wh* shims
exec `which wh#{@sub}`.strip, *ARGV
+ elsif @sub == 'docker'
+ exec `which arv-keepdocker`.strip, *ARGV
else
puts "Usage: \n" +
"#{$0} keep ls\n" +
"#{$0} keep get\n" +
"#{$0} keep put\n" +
"#{$0} keep less\n" +
- "#{$0} keep check\n"
+ "#{$0} keep check\n" +
+ "#{$0} keep docker\n"
end
abort
when 'pipeline'
rescue LoadError
abort <<-EOS
-Please install all required gems:
+Please install all required gems:
gem install activesupport andand curb google-api-client json oj trollop
class Google::APIClient
def discovery_document(api, version)
- api = api.to_s
- return @discovery_documents["#{api}:#{version}"] ||= (begin
- response = self.execute!(
- :http_method => :get,
- :uri => self.discovery_uri(api, version),
- :authenticated => false
- )
- response.body.class == String ? JSON.parse(response.body) : response.body
- end)
+ api = api.to_s
+ return @discovery_documents["#{api}:#{version}"] ||=
+ begin
+ # fetch new API discovery doc if stale
+ cached_doc = File.expand_path '~/.cache/arvados/discovery_uri.json'
+ if not File.exist?(cached_doc) or (Time.now - File.mtime(cached_doc)) > 86400
+ response = self.execute!(:http_method => :get,
+ :uri => self.discovery_uri(api, version),
+ :authenticated => false)
+ FileUtils.makedirs(File.dirname cached_doc)
+ File.open(cached_doc, 'w') do |f|
+ f.puts response.body
+ end
+ end
+
+ File.open(cached_doc) { |f| JSON.load f }
+ end
end
end
end
end
-client = ArvadosClient.new(:host => ENV['ARVADOS_API_HOST'], :application_name => 'arvados-cli', :application_version => '1.0')
-arvados = client.discovered_api('arvados', ENV['ARVADOS_API_VERSION'])
+begin
+ client = ArvadosClient.new(:host => ENV['ARVADOS_API_HOST'], :application_name => 'arvados-cli', :application_version => '1.0')
+ arvados = client.discovered_api('arvados', ENV['ARVADOS_API_VERSION'])
+rescue Exception => e
+ puts "Failed to connect to Arvados API server: #{e}"
+ exit 1
+end
def to_boolean(s)
!!(s =~ /^(true|t|yes|y|1)$/i)
discovery_document["resources"][resource.pluralize]["methods"].
each do |k,v|
description = ''
- description = ' ' + v["description"] if v.include?("description")
+ if v.include? "description"
+ # add only the first line of the discovery doc description
+ description = ' ' + v["description"].split("\n").first.chomp
+ end
banner += " #{sprintf("%20s",k)}#{description}\n"
end
banner += "\n"
STDERR.puts banner
-
- if not method.nil? and method != '--help' then
+
+ if not method.nil? and method != '--help' then
Trollop::die ("Unknown method #{method.inspect} " +
"for resource #{resource.inspect}")
end
banner += "\n\n"
discovery_document["resources"].each do |k,v|
description = ''
- if discovery_document["schemas"].include?(k.singularize.capitalize) and
- discovery_document["schemas"][k.singularize.capitalize].include?('description') then
- description = ' ' + discovery_document["schemas"][k.singularize.capitalize]["description"]
+ resource_info = discovery_document["schemas"][k.singularize.capitalize]
+ if resource_info and resource_info.include?('description')
+ # add only the first line of the discovery doc description
+ description = ' ' + resource_info["description"].split("\n").first.chomp
end
banner += " #{sprintf("%30s",k.singularize)}#{description}\n"
end
banner += "\n"
STDERR.puts banner
- if not resource.nil? and resource != '--help' then
+ if not resource.nil? and resource != '--help' then
Trollop::die "Unknown resource type #{resource.inspect}"
end
exit 255
end
global_opts = Trollop::options do
+ version __FILE__
banner "arv: the Arvados CLI tool"
opt :dry_run, "Don't actually do anything", :short => "-n"
opt :verbose, "Print some things on stderr"
required: is_required,
type: :string
}
- discovered_params[resource.to_sym] = body_object
end
end
exit
end
-request_parameters = {}.merge(method_opts)
+request_parameters = {_profile:true}.merge(method_opts)
resource_body = request_parameters.delete(resource_schema.to_sym)
if resource_body
request_body = {
resource_schema => resource_body
}
else
- request_body = {}
+ request_body = nil
end
case api_method
when
- 'arvados.users.event_stream',
- 'arvados.jobs.log_stream',
'arvados.jobs.log_tail_follow'
# Special case for methods that respond with data streams rather
curl.headers['Accept'] = 'text/plain'
curl.headers['Authorization'] = "OAuth2 #{ENV['ARVADOS_API_TOKEN']}"
if ENV['ARVADOS_API_HOST_INSECURE']
- curl.ssl_verify_peer = false
+ curl.ssl_verify_peer = false
curl.ssl_verify_host = false
end
if global_opts[:verbose]
end
exit 0
else
- request_body[:api_token] = ENV['ARVADOS_API_TOKEN']
- request_body[:_profile] = true
result = client.execute(:api_method => eval(api_method),
:parameters => request_parameters,
:body => request_body,
- :authenticated => false)
+ :authenticated => false,
+ :headers => {
+ authorization: 'OAuth2 '+ENV['ARVADOS_API_TOKEN']
+ })
end
begin
puts results['uuid']
end
end
-
-