Merge branch 'master' into 14988-wb-rails5-upgrade
[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.anonymous_user_token and
8     'show' == ctrl.action_name
9   }
10
11   def generate_provenance(cr)
12     return if params['tab_pane'] != "Provenance"
13
14     nodes = {cr[:uuid] => cr}
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])
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     output_cols = {} # Indexed by UUID
33     input_cols = {} # Indexed by PDH
34     output_pdhs = []
35
36     # Batch requests to get all related collections
37     # First fetch output collections by UUID.
38     Collection.filter([['uuid', 'in', col_uuids.uniq]]).each do |c|
39       output_cols[c[:uuid]] = c
40       output_pdhs << c[:portable_data_hash]
41     end
42     # Then, get only input collections by PDH. There could be more than one collection
43     # per PDH: the number of collections is used on the collection node label.
44     Collection.filter(
45       [['portable_data_hash', 'in', col_pdhs - output_pdhs]]).each do |c|
46       if input_cols[c[:portable_data_hash]]
47         input_cols[c[:portable_data_hash]] << c
48       else
49         input_cols[c[:portable_data_hash]] = [c]
50       end
51     end
52
53     @svg = ProvenanceHelper::create_provenance_graph(
54       nodes, "provenance_svg",
55       {
56         :request => request,
57         :direction => :top_down,
58         :output_collections => output_cols,
59         :input_collections => input_cols,
60         :cr_children_of => {
61           cr[:uuid] => child_crs.select{|child| child[:uuid]},
62         },
63       })
64   end
65
66   def show_pane_list
67     panes = %w(Status Log Provenance Advanced)
68     if @object.andand.state == 'Uncommitted'
69       panes = %w(Inputs) + panes - %w(Log Provenance)
70     end
71     panes
72   end
73
74   def show
75     generate_provenance(@object)
76     super
77   end
78
79   def cancel
80     if @object.container_uuid
81       c = Container.select(['state']).where(uuid: @object.container_uuid).first
82       if c && c.state != 'Running'
83         # If the container hasn't started yet, setting priority=0
84         # leaves our request in "Committed" state and doesn't cancel
85         # the container (even if no other requests are giving it
86         # priority). To avoid showing this container request as "on
87         # hold" after hitting the Cancel button, set state=Final too.
88         @object.state = 'Final'
89       end
90     end
91     @object.update_attributes! priority: 0
92     if params[:return_to]
93       redirect_to params[:return_to]
94     else
95       redirect_to @object
96     end
97   end
98
99   def update
100     @updates ||= params[@object.class.to_s.underscore.singularize.to_sym]
101     input_obj = @updates[:mounts].andand[:"/var/lib/cwl/cwl.input.json"].andand[:content]
102     if input_obj
103       workflow = @object.mounts[:"/var/lib/cwl/workflow.json"][:content]
104       get_cwl_inputs(workflow).each do |input_schema|
105         if not input_obj.include? cwl_shortname(input_schema[:id])
106           next
107         end
108         required, primary_type, param_id = cwl_input_info(input_schema)
109         if input_obj[param_id] == ""
110           input_obj[param_id] = nil
111         elsif primary_type == "boolean"
112           input_obj[param_id] = input_obj[param_id] == "true"
113         elsif ["int", "long"].include? primary_type
114           input_obj[param_id] = input_obj[param_id].to_i
115         elsif ["float", "double"].include? primary_type
116           input_obj[param_id] = input_obj[param_id].to_f
117         elsif ["File", "Directory"].include? primary_type
118           re = CollectionsHelper.match_uuid_with_optional_filepath(input_obj[param_id])
119           if re
120             c = Collection.find(re[1])
121             input_obj[param_id] = {"class" => primary_type,
122                                    "location" => "keep:#{c.portable_data_hash}#{re[4]}",
123                                    "http://arvados.org/cwl#collectionUUID" => re[1]}
124           end
125         end
126       end
127     end
128     params[:merge] = true
129     begin
130       super
131     rescue => e
132       flash[:error] = e.to_s
133       show
134     end
135   end
136
137   def copy
138     src = @object
139
140     @object = ContainerRequest.new
141
142     # By default the copied CR won't be reusing containers, unless use_existing=true
143     # param is passed.
144     command = src.command
145     if params[:use_existing]
146       @object.use_existing = true
147       # Pass the correct argument to arvados-cwl-runner command.
148       if src.command[0] == 'arvados-cwl-runner'
149         command = src.command - ['--disable-reuse']
150         command.insert(1, '--enable-reuse')
151       end
152     else
153       @object.use_existing = false
154       # Pass the correct argument to arvados-cwl-runner command.
155       if src.command[0] == 'arvados-cwl-runner'
156         command = src.command - ['--enable-reuse']
157         command.insert(1, '--disable-reuse')
158       end
159     end
160
161     @object.command = command
162     @object.container_image = src.container_image
163     @object.cwd = src.cwd
164     @object.description = src.description
165     @object.environment = src.environment
166     @object.mounts = src.mounts
167     @object.name = src.name
168     @object.output_path = src.output_path
169     @object.priority = 1
170     @object.properties[:template_uuid] = src.properties[:template_uuid]
171     @object.runtime_constraints = src.runtime_constraints
172     @object.scheduling_parameters = src.scheduling_parameters
173     @object.state = 'Uncommitted'
174
175     # set owner_uuid to that of source, provided it is a project and writable by current user
176     current_project = Group.find(src.owner_uuid) rescue nil
177     if (current_project && current_project.writable_by.andand.include?(current_user.uuid))
178       @object.owner_uuid = src.owner_uuid
179     end
180
181     super
182   end
183
184   def index
185     @limit = 20
186     super
187   end
188
189 end