1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
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
11 def generate_provenance(cr)
12 return if params['tab_pane'] != "Provenance"
14 nodes = {cr[:uuid] => cr}
18 col_uuids << cr[:output_uuid] if cr[:output_uuid]
19 col_pdhs += ProvenanceHelper::cr_input_pdhs(cr)
21 # Search for child CRs
22 if cr[:container_uuid]
23 child_crs = ContainerRequest.where(requesting_container_uuid: cr[:container_uuid])
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)
32 output_cols = {} # Indexed by UUID
33 input_cols = {} # Indexed by PDH
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]
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.
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
49 input_cols[c[:portable_data_hash]] = [c]
53 @svg = ProvenanceHelper::create_provenance_graph(
54 nodes, "provenance_svg",
57 :direction => :top_down,
58 :output_collections => output_cols,
59 :input_collections => input_cols,
61 cr[:uuid] => child_crs.select{|child| child[:uuid]},
67 panes = %w(Status Log Provenance Advanced)
68 if @object.andand.state == 'Uncommitted'
69 panes = %w(Inputs) + panes - %w(Log Provenance)
75 generate_provenance(@object)
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'
91 @object.update_attributes! priority: 0
93 redirect_to params[:return_to]
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]
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])
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])
120 c = Collection.find(re[1])
121 input_obj[param_id] = {"class" => primary_type,
122 "location" => "keep:#{c.portable_data_hash}#{re[4]}",
123 "arv:collection" => input_obj[param_id]}
128 params[:merge] = true
132 flash[:error] = e.to_s
140 @object = ContainerRequest.new
142 # By default the copied CR won't be reusing containers, unless use_existing=true
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')
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')
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
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'
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