Merge branch '8784-dir-listings'
[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_filter :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     @object.update_attributes! priority: 0
81     if params[:return_to]
82       redirect_to params[:return_to]
83     else
84       redirect_to @object
85     end
86   end
87
88   def update
89     @updates ||= params[@object.class.to_s.underscore.singularize.to_sym]
90     input_obj = @updates[:mounts].andand[:"/var/lib/cwl/cwl.input.json"].andand[:content]
91     if input_obj
92       workflow = @object.mounts[:"/var/lib/cwl/workflow.json"][:content]
93       get_cwl_inputs(workflow).each do |input_schema|
94         if not input_obj.include? cwl_shortname(input_schema[:id])
95           next
96         end
97         required, primary_type, param_id = cwl_input_info(input_schema)
98         if input_obj[param_id] == ""
99           input_obj[param_id] = nil
100         elsif primary_type == "boolean"
101           input_obj[param_id] = input_obj[param_id] == "true"
102         elsif ["int", "long"].include? primary_type
103           input_obj[param_id] = input_obj[param_id].to_i
104         elsif ["float", "double"].include? primary_type
105           input_obj[param_id] = input_obj[param_id].to_f
106         elsif ["File", "Directory"].include? primary_type
107           re = CollectionsHelper.match_uuid_with_optional_filepath(input_obj[param_id])
108           if re
109             c = Collection.find(re[1])
110             input_obj[param_id] = {"class" => primary_type,
111                                    "location" => "keep:#{c.portable_data_hash}#{re[4]}",
112                                    "arv:collection" => input_obj[param_id]}
113           end
114         end
115       end
116     end
117     params[:merge] = true
118     begin
119       super
120     rescue => e
121       flash[:error] = e.to_s
122       show
123     end
124   end
125
126   def copy
127     src = @object
128
129     @object = ContainerRequest.new
130
131     # By default the copied CR won't be reusing containers, unless use_existing=true
132     # param is passed.
133     command = src.command
134     if params[:use_existing]
135       @object.use_existing = true
136       # Pass the correct argument to arvados-cwl-runner command.
137       if src.command[0] == 'arvados-cwl-runner'
138         command = src.command - ['--disable-reuse']
139         command.insert(1, '--enable-reuse')
140       end
141     else
142       @object.use_existing = false
143       # Pass the correct argument to arvados-cwl-runner command.
144       if src.command[0] == 'arvados-cwl-runner'
145         command = src.command - ['--enable-reuse']
146         command.insert(1, '--disable-reuse')
147       end
148     end
149
150     @object.command = command
151     @object.container_image = src.container_image
152     @object.cwd = src.cwd
153     @object.description = src.description
154     @object.environment = src.environment
155     @object.mounts = src.mounts
156     @object.name = src.name
157     @object.output_path = src.output_path
158     @object.priority = 1
159     @object.properties[:template_uuid] = src.properties[:template_uuid]
160     @object.runtime_constraints = src.runtime_constraints
161     @object.scheduling_parameters = src.scheduling_parameters
162     @object.state = 'Uncommitted'
163
164     # set owner_uuid to that of source, provided it is a project and writable by current user
165     current_project = Group.find(src.owner_uuid) rescue nil
166     if (current_project && current_project.writable_by.andand.include?(current_user.uuid))
167       @object.owner_uuid = src.owner_uuid
168     end
169
170     super
171   end
172
173   def index
174     @limit = 20
175     super
176   end
177
178 end