17119: bugfix: when "count: none" is specified, the group/contents
[arvados.git] / apps / workbench / app / controllers / container_requests_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class ContainerRequestsController < ApplicationController
6   skip_around_action :require_thread_api_token, if: proc { |ctrl|
7     !Rails.configuration.Users.AnonymousUserToken.empty? and
8     'show' == ctrl.action_name
9   }
10
11   def generate_provenance(cr)
12     return if params['tab_pane'] != "Provenance"
13
14     nodes = {}
15     child_crs = []
16     col_uuids = []
17     col_pdhs = []
18     col_uuids << cr[:output_uuid] if cr[:output_uuid]
19     col_pdhs += ProvenanceHelper::cr_input_pdhs(cr)
20
21     # Search for child CRs
22     if cr[:container_uuid]
23       child_crs = ContainerRequest.where(requesting_container_uuid: cr[:container_uuid]).with_count("none")
24
25       child_crs.each do |child|
26         nodes[child[:uuid]] = child
27         col_uuids << child[:output_uuid] if child[:output_uuid]
28         col_pdhs += ProvenanceHelper::cr_input_pdhs(child)
29       end
30     end
31
32     if nodes.length == 0
33       nodes[cr[:uuid]] = cr
34     end
35
36     pdh_to_col = {} # Indexed by PDH
37     output_pdhs = []
38
39     # Batch requests to get all related collections
40     # First fetch output collections by UUID.
41     Collection.filter([['uuid', 'in', col_uuids.uniq]]).with_count("none").each do |c|
42       output_pdhs << c[:portable_data_hash]
43       pdh_to_col[c[:portable_data_hash]] = c
44       nodes[c[:uuid]] = c
45     end
46     # Next, get input collections by PDH.
47     Collection.filter(
48       [['portable_data_hash', 'in', col_pdhs - output_pdhs]]).with_count("none").each do |c|
49       nodes[c[:portable_data_hash]] = c
50     end
51
52     @svg = ProvenanceHelper::create_provenance_graph(
53       nodes, "provenance_svg",
54       {
55         :request => request,
56         :pdh_to_uuid => pdh_to_col,
57       }
58     )
59   end
60
61   def show_pane_list
62     panes = %w(Status Log Provenance Advanced)
63     if @object.andand.state == 'Uncommitted'
64       panes = %w(Inputs) + panes - %w(Log Provenance)
65     end
66     panes
67   end
68
69   def show
70     generate_provenance(@object)
71     super
72   end
73
74   def cancel
75     if @object.container_uuid
76       c = Container.select(['state']).where(uuid: @object.container_uuid).with_count("none").first
77       if c && c.state != 'Running'
78         # If the container hasn't started yet, setting priority=0
79         # leaves our request in "Committed" state and doesn't cancel
80         # the container (even if no other requests are giving it
81         # priority). To avoid showing this container request as "on
82         # hold" after hitting the Cancel button, set state=Final too.
83         @object.state = 'Final'
84       end
85     end
86     @object.update_attributes! priority: 0
87     if params[:return_to]
88       redirect_to params[:return_to]
89     else
90       redirect_to @object
91     end
92   end
93
94   def update
95     @updates ||= params[@object.class.to_s.underscore.singularize.to_sym]
96     input_obj = @updates[:mounts].andand[:"/var/lib/cwl/cwl.input.json"].andand[:content]
97     if input_obj
98       workflow = @object.mounts[:"/var/lib/cwl/workflow.json"][:content]
99       get_cwl_inputs(workflow).each do |input_schema|
100         if not input_obj.include? cwl_shortname(input_schema[:id])
101           next
102         end
103         required, primary_type, param_id = cwl_input_info(input_schema)
104         if input_obj[param_id] == ""
105           input_obj[param_id] = nil
106         elsif primary_type == "boolean"
107           input_obj[param_id] = input_obj[param_id] == "true"
108         elsif ["int", "long"].include? primary_type
109           input_obj[param_id] = input_obj[param_id].to_i
110         elsif ["float", "double"].include? primary_type
111           input_obj[param_id] = input_obj[param_id].to_f
112         elsif ["File", "Directory"].include? primary_type
113           re = CollectionsHelper.match_uuid_with_optional_filepath(input_obj[param_id])
114           if re
115             c = Collection.find(re[1])
116             input_obj[param_id] = {"class" => primary_type,
117                                    "location" => "keep:#{c.portable_data_hash}#{re[4]}",
118                                    "http://arvados.org/cwl#collectionUUID" => re[1]}
119           end
120         end
121       end
122     end
123     params[:merge] = true
124
125     if !@updates[:reuse_steps].nil?
126       if @updates[:reuse_steps] == "false"
127         @updates[:reuse_steps] = false
128       end
129       @updates[:command] ||= @object.command
130       @updates[:command] -= ["--disable-reuse", "--enable-reuse"]
131       if @updates[:reuse_steps]
132         @updates[:command].insert(1, "--enable-reuse")
133       else
134         @updates[:command].insert(1, "--disable-reuse")
135       end
136       @updates.delete(:reuse_steps)
137     end
138
139     begin
140       super
141     rescue => e
142       flash[:error] = e.to_s
143       show
144     end
145   end
146
147   def copy
148     src = @object
149
150     @object = ContainerRequest.new
151
152     # set owner_uuid to that of source, provided it is a project and writable by current user
153     if params[:work_unit].andand[:owner_uuid]
154       @object.owner_uuid = src.owner_uuid = params[:work_unit][:owner_uuid]
155     else
156       current_project = Group.find(src.owner_uuid) rescue nil
157       if (current_project && current_project.writable_by.andand.include?(current_user.uuid))
158         @object.owner_uuid = src.owner_uuid
159       end
160     end
161
162     command = src.command
163     if command[0] == 'arvados-cwl-runner'
164       command.each_with_index do |arg, i|
165         if arg.start_with? "--project-uuid="
166           command[i] = "--project-uuid=#{@object.owner_uuid}"
167         end
168       end
169       command -= ["--disable-reuse", "--enable-reuse"]
170       command.insert(1, '--enable-reuse')
171     end
172
173     if params[:use_existing] == "false"
174       params[:use_existing] = false
175     elsif params[:use_existing] == "true"
176       params[:use_existing] = true
177     end
178
179     if params[:use_existing] || params[:use_existing].nil?
180       # If nil, reuse workflow steps but not the workflow runner.
181       @object.use_existing = !!params[:use_existing]
182
183       # Pass the correct argument to arvados-cwl-runner command.
184       if command[0] == 'arvados-cwl-runner'
185         command -= ["--disable-reuse", "--enable-reuse"]
186         command.insert(1, '--enable-reuse')
187       end
188     else
189       @object.use_existing = false
190       # Pass the correct argument to arvados-cwl-runner command.
191       if command[0] == 'arvados-cwl-runner'
192         command -= ["--disable-reuse", "--enable-reuse"]
193         command.insert(1, '--disable-reuse')
194       end
195     end
196
197     @object.command = command
198     @object.container_image = src.container_image
199     @object.cwd = src.cwd
200     @object.description = src.description
201     @object.environment = src.environment
202     @object.mounts = src.mounts
203     @object.name = src.name
204     @object.output_path = src.output_path
205     @object.priority = 1
206     @object.properties[:template_uuid] = src.properties[:template_uuid]
207     @object.runtime_constraints = src.runtime_constraints
208     @object.scheduling_parameters = src.scheduling_parameters
209     @object.state = 'Uncommitted'
210
211     super
212   end
213
214   def index
215     @limit = 20
216     super
217   end
218
219 end