5 # Ward Vandewege <ward@clinicalfuture.com>
7 if RUBY_VERSION < '1.9.3' then
9 #{$0.gsub(/^\.\//,'')} requires Ruby version 1.9.3 or higher.
15 cmd = 'arv-' + ARGV.shift
16 exec `which #{cmd}`.strip, *ARGV
21 if ['ls', 'get', 'put', 'less', 'check'].index @sub then
22 exec `which wh#{@sub}`.strip, *ARGV
35 if ['run'].index @sub then
36 exec `which arv-run-pipeline-instance`.strip, *ARGV
39 "#{$0} pipeline run [...]\n" +
40 "(see arv-run-pipeline-instance --help for details)\n"
45 ENV['ARVADOS_API_VERSION'] ||= 'v1'
47 if not ENV.include?('ARVADOS_API_HOST') or not ENV.include?('ARVADOS_API_TOKEN') then
49 ARVADOS_API_HOST and ARVADOS_API_TOKEN need to be defined as environment variables.
55 require 'google/api_client'
61 require 'active_support/inflector'
65 Please install all required gems:
67 gem install google-api-client json trollop andand oj activesupport
72 ActiveSupport::Inflector.inflections do |inflect|
73 inflect.irregular 'specimen', 'specimens'
74 inflect.irregular 'human', 'humans'
79 original_verbosity = $VERBOSE
82 $VERBOSE = original_verbosity
87 # do this if you're testing with a dev server and you don't care about SSL certificate checks:
88 if ENV['ARVADOS_API_HOST_INSECURE']
89 suppress_warnings { OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE }
92 class Google::APIClient
93 def discovery_document(api, version)
95 return @discovery_documents["#{api}:#{version}"] ||= (begin
96 response = self.execute!(
98 :uri => self.discovery_uri(api, version),
99 :authenticated => false
101 response.body.class == String ? JSON.parse(response.body) : response.body
106 class ArvadosClient < Google::APIClient
108 if args.last.is_a? Hash
109 args.last[:headers] ||= {}
110 args.last[:headers]['Accept'] ||= 'application/json'
116 client = ArvadosClient.new(:host => ENV['ARVADOS_API_HOST'], :application_name => 'arvados-cli', :application_version => '1.0')
117 arvados = client.discovered_api('arvados', ENV['ARVADOS_API_VERSION'])
120 !!(s =~ /^(true|t|yes|y|1)$/i)
123 def parse_arguments(discovery_document)
124 resource_types = Array.new()
125 resource_types << '--help'
126 discovery_document["resources"].each do |k,v|
127 resource_types << k.singularize
130 global_opts = Trollop::options do
131 banner "arvados cli client"
132 opt :dry_run, "Don't actually do anything", :short => "-n"
133 opt :verbose, "Print some things on stderr", :short => "-v"
134 opt :uuid, "Return the UUIDs of the objects in the response, one per line (default)", :short => nil
135 opt :json, "Return the entire response received from the API server, as a JSON object", :short => "-j"
136 opt :human, "Return the response received from the API server, as a JSON object with whitespace added for human consumption", :short => "-h"
137 opt :pretty, "Synonym of --human", :short => nil
138 stop_on resource_types
142 resource_arg = ARGV.shift
143 if resource_types.include?(resource_arg) and resource_arg != '--help' then
145 # Now see if the method supplied exists
147 if discovery_document["resources"][resource_arg.pluralize]["methods"].include?(method) then
148 # method exists. Collect arguments.
149 discovered_params = discovery_document["resources"][resource_arg.pluralize]["methods"][method]["parameters"]
150 method_opts = Trollop::options do
151 discovered_params.each do |k,v|
153 opts[:type] = v["type"].to_sym if v.include?("type")
154 if [:datetime, :text, :object, :array].index opts[:type]
155 opts[:type] = :string # else trollop bork
157 opts[:default] = v["default"] if v.include?("default")
158 opts[:default] = v["default"].to_i if opts[:type] == :integer
159 opts[:default] = to_boolean(v["default"]) if opts[:type] == :boolean
160 opts[:required] = true if v.include?("required") and v["required"]
162 description = ' ' + v["description"] if v.include?("description")
163 opt k.to_sym, description, opts
165 body_object = discovery_document["resources"][resource_arg.pluralize]["methods"][method]["request"]
166 if body_object and discovered_params[resource_arg].nil?
168 if body_object["required"] == false
171 opt resource_arg.to_sym, "#{resource_arg} (request body)", required: is_required, type: :string
174 discovered_params.each do |k,v|
175 if ['object', 'array'].index(v["type"]) and method_opts.has_key? k
176 method_opts[k] = JSON.parse method_opts[k]
180 banner = "\nThis resource type supports the following methods:\n\n"
181 discovery_document["resources"][resource_arg.pluralize]["methods"].each do |k,v|
183 description = ' ' + v["description"] if v.include?("description")
184 banner += " #{sprintf("%20s",k)}#{description}\n"
190 if not method.nil? and method != '--help' then
191 Trollop::die "Unknown method #{method.to_s} for command #{resource_arg.to_s}"
199 banner = "\nThis Arvados instance supports the following resource types:\n\n"
200 discovery_document["resources"].each do |k,v|
202 if discovery_document["schemas"].include?(k.singularize.capitalize) and
203 discovery_document["schemas"][k.singularize.capitalize].include?('description') then
204 description = ' ' + discovery_document["schemas"][k.singularize.capitalize]["description"]
206 banner += " #{sprintf("%30s",k.singularize)}#{description}\n"
212 if not resource_arg.nil? and resource_arg != '--help' then
213 Trollop::die "Unknown resource type #{resource_arg.inspect}"
219 return resource_arg, method, method_opts, global_opts, ARGV
222 resource_schema, method, method_opts, global_opts, remaining_opts = parse_arguments(arvados.discovery_document)
223 controller = resource_schema.pluralize
225 api_method = 'arvados.' + controller + '.' + method
227 if global_opts[:dry_run]
228 if global_opts[:verbose]
229 $stderr.puts "#{api_method} #{method_opts.inspect}"
234 request_parameters = {}.merge(method_opts)
235 resource_body = request_parameters.delete(resource_schema.to_sym)
238 resource_schema => JSON.parse(resource_body)
243 request_body[:api_token] = ENV['ARVADOS_API_TOKEN']
244 result = client.execute(:api_method => eval(api_method),
245 :parameters => request_parameters,
246 :body => request_body,
247 :authenticated => false)
248 results = JSON.parse result.body
250 if results["errors"] then
251 abort "Error: #{results["errors"][0]}"
254 if global_opts[:human] or global_opts[:pretty] then
255 puts Oj.dump(results, :indent => 1)
256 elsif global_opts[:json] then
257 puts Oj.dump(results)
258 elsif results["items"] and results["kind"].match /list$/i
259 results['items'].each do |i| puts i['uuid'] end