4084: Merge branch 'master' into 4084-log-pane-refresh-TC
[arvados.git] / services / api / app / controllers / arvados / v1 / collections_controller.rb
1 require "arvados/keep"
2
3 class Arvados::V1::CollectionsController < ApplicationController
4   def create
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
8     end
9     super
10   end
11
12   def find_object_by_uuid
13     if loc = Keep::Locator.parse(params[:id])
14       loc.strip_hints!
15       if c = Collection.readable_by(*@read_users).where({ portable_data_hash: loc.to_s }).limit(1).first
16         @object = {
17           uuid: c.portable_data_hash,
18           portable_data_hash: c.portable_data_hash,
19           manifest_text: c.manifest_text,
20         }
21       end
22     else
23       super
24     end
25     true
26   end
27
28   def show
29     sign_manifests(@object[:manifest_text])
30     if @object.is_a? Collection
31       render json: @object.as_api_response
32     else
33       render json: @object
34     end
35   end
36
37   def index
38     sign_manifests(*@objects.map { |c| c[:manifest_text] })
39     super
40   end
41
42   def find_collections(visited, sp, &b)
43     case sp
44     when ArvadosModel
45       sp.class.columns.each do |c|
46         find_collections(visited, sp[c.name.to_sym], &b) if c.name != "log"
47       end
48     when Hash
49       sp.each do |k, v|
50         find_collections(visited, v, &b)
51       end
52     when Array
53       sp.each do |v|
54         find_collections(visited, v, &b)
55       end
56     when String
57       if m = /[a-f0-9]{32}\+\d+/.match(sp)
58         yield m[0], nil
59       elsif m = /[0-9a-z]{5}-4zz18-[0-9a-z]{15}/.match(sp)
60         yield nil, m[0]
61       end
62     end
63   end
64
65   def search_edges(visited, uuid, direction)
66     if uuid.nil? or uuid.empty? or visited[uuid]
67       return
68     end
69
70     if loc = Keep::Locator.parse(uuid)
71       loc.strip_hints!
72       return if visited[loc.to_s]
73     end
74
75     logger.debug "visiting #{uuid}"
76
77     if loc
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
81       if c.size == 1
82         visited[loc.to_s] = c[0]
83       elsif c.size > 1
84         name = collections.limit(1).where("name <> ''").first
85         if name
86           visited[loc.to_s] = {
87             portable_data_hash: c[0].portable_data_hash,
88             name: "#{name.name} + #{collections.count-1} more"
89           }
90         else
91           visited[loc.to_s] = {
92             portable_data_hash: c[0].portable_data_hash,
93             name: loc.to_s
94           }
95         end
96       end
97
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)
102         end
103
104         Job.readable_by(*@read_users).where(log: loc.to_s).each do |job|
105           search_edges(visited, job.uuid, :search_up)
106         end
107       elsif direction == :search_down
108         if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
109           # Special case, don't follow the empty collection.
110           return
111         end
112
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)
116         end
117
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)
120         end
121       end
122     else
123       # uuid is a regular Arvados UUID
124       rsc = ArvadosModel::resource_class_for_uuid uuid
125       if rsc == Job
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
133             end
134           elsif direction == :search_down
135             # Follow downstream job output
136             search_edges(visited, job.output, direction)
137           end
138         end
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
143         end
144       elsif rsc != nil
145         rsc.where(uuid: uuid).each do |r|
146           visited[uuid] = r.as_api_response
147         end
148       end
149     end
150
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").
155         each do |link|
156         visited[link.uuid] = link.as_api_response
157         search_edges(visited, link.tail_uuid, direction)
158       end
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").
163         each do |link|
164         visited[link.uuid] = link.as_api_response
165         search_edges(visited, link.head_uuid, direction)
166       end
167     end
168   end
169
170   def provenance
171     visited = {}
172     search_edges(visited, @object[:portable_data_hash], :search_up)
173     search_edges(visited, @object[:uuid], :search_up)
174     render json: visited
175   end
176
177   def used_by
178     visited = {}
179     search_edges(visited, @object[:uuid], :search_down)
180     search_edges(visited, @object[:portable_data_hash], :search_down)
181     render json: visited
182   end
183
184   protected
185
186   def apply_filters
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"]
191     end
192     super
193   end
194
195   def sign_manifests(*manifests)
196     if current_api_client_authorization
197       signing_opts = {
198         key: Rails.configuration.blob_signing_key,
199         api_token: current_api_client_authorization.api_token,
200         ttl: Rails.configuration.blob_signing_ttl,
201       }
202       manifests.each do |text|
203         Collection.munge_manifest_locators(text) do |loc|
204           Blob.sign_locator(loc.to_s, signing_opts)
205         end
206       end
207     end
208   end
209 end