3 class Arvados::V1::CollectionsController < ApplicationController
5 if resource_attrs[:uuid] and (loc = Keep::Locator.parse(resource_attrs[:uuid]))
6 resource_attrs[:portable_data_hash] = loc.to_s
7 resource_attrs.delete :uuid
12 def find_object_by_uuid
13 if loc = Keep::Locator.parse(params[:id])
15 if c = Collection.readable_by(*@read_users).where({ portable_data_hash: loc.to_s }).limit(1).first
17 uuid: c.portable_data_hash,
18 portable_data_hash: c.portable_data_hash,
19 manifest_text: c.manifest_text,
29 sign_manifests(@object[:manifest_text])
30 if @object.is_a? Collection
31 render json: @object.as_api_response
38 sign_manifests(*@objects.map { |c| c[:manifest_text] })
42 def find_collections(visited, sp, &b)
45 sp.class.columns.each do |c|
46 find_collections(visited, sp[c.name.to_sym], &b) if c.name != "log"
50 find_collections(visited, v, &b)
54 find_collections(visited, v, &b)
57 if m = /[a-f0-9]{32}\+\d+/.match(sp)
59 elsif m = /[0-9a-z]{5}-4zz18-[0-9a-z]{15}/.match(sp)
65 def search_edges(visited, uuid, direction)
66 if uuid.nil? or uuid.empty? or visited[uuid]
70 if loc = Keep::Locator.parse(uuid)
72 return if visited[loc.to_s]
75 logger.debug "visiting #{uuid}"
78 # uuid is a portable_data_hash
79 collections = Collection.readable_by(*@read_users).where(portable_data_hash: loc.to_s)
80 c = collections.limit(2).all
82 visited[loc.to_s] = c[0]
84 name = collections.limit(1).where("name <> ''").first
87 portable_data_hash: c[0].portable_data_hash,
88 name: "#{name.name} + #{collections.count-1} more"
92 portable_data_hash: c[0].portable_data_hash,
98 if direction == :search_up
99 # Search upstream for jobs where this locator is the output of some job
100 Job.readable_by(*@read_users).where(output: loc.to_s).each do |job|
101 search_edges(visited, job.uuid, :search_up)
104 Job.readable_by(*@read_users).where(log: loc.to_s).each do |job|
105 search_edges(visited, job.uuid, :search_up)
107 elsif direction == :search_down
108 if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
109 # Special case, don't follow the empty collection.
113 # Search downstream for jobs where this locator is in script_parameters
114 Job.readable_by(*@read_users).where(["jobs.script_parameters like ?", "%#{loc.to_s}%"]).each do |job|
115 search_edges(visited, job.uuid, :search_down)
118 Job.readable_by(*@read_users).where(["jobs.docker_image_locator = ?", "#{loc.to_s}"]).each do |job|
119 search_edges(visited, job.uuid, :search_down)
123 # uuid is a regular Arvados UUID
124 rsc = ArvadosModel::resource_class_for_uuid uuid
126 Job.readable_by(*@read_users).where(uuid: uuid).each do |job|
127 visited[uuid] = job.as_api_response
128 if direction == :search_up
129 # Follow upstream collections referenced in the script parameters
130 find_collections(visited, job) do |hash, uuid|
131 search_edges(visited, hash, :search_up) if hash
132 search_edges(visited, uuid, :search_up) if uuid
134 elsif direction == :search_down
135 # Follow downstream job output
136 search_edges(visited, job.output, direction)
139 elsif rsc == Collection
140 if c = Collection.readable_by(*@read_users).where(uuid: uuid).limit(1).first
141 search_edges(visited, c.portable_data_hash, direction)
142 visited[c.portable_data_hash] = c.as_api_response
145 rsc.where(uuid: uuid).each do |r|
146 visited[uuid] = r.as_api_response
151 if direction == :search_up
152 # Search for provenance links pointing to the current uuid
153 Link.readable_by(*@read_users).
154 where(head_uuid: uuid, link_class: "provenance").
156 visited[link.uuid] = link.as_api_response
157 search_edges(visited, link.tail_uuid, direction)
159 elsif direction == :search_down
160 # Search for provenance links emanating from the current uuid
161 Link.readable_by(current_user).
162 where(tail_uuid: uuid, link_class: "provenance").
164 visited[link.uuid] = link.as_api_response
165 search_edges(visited, link.head_uuid, direction)
172 search_edges(visited, @object[:portable_data_hash], :search_up)
173 search_edges(visited, @object[:uuid], :search_up)
179 search_edges(visited, @object[:uuid], :search_down)
180 search_edges(visited, @object[:portable_data_hash], :search_down)
187 if action_name == 'index'
188 # Omit manifest_text from index results unless expressly selected.
189 @select ||= model_class.api_accessible_attributes(:user).
190 map { |attr_spec| attr_spec.first.to_s } - ["manifest_text"]
195 def sign_manifests(*manifests)
196 if current_api_client_authorization
198 key: Rails.configuration.blob_signing_key,
199 api_token: current_api_client_authorization.api_token,
200 ttl: Rails.configuration.blob_signing_ttl,
202 manifests.each do |text|
203 Collection.munge_manifest_locators(text) do |loc|
204 Blob.sign_locator(loc.to_s, signing_opts)