11185: When re-run, the default behaviour is to copy reuse disabled. Changed modal...
[arvados.git] / apps / workbench / app / controllers / container_requests_controller.rb
1 class ContainerRequestsController < ApplicationController
2   skip_around_filter :require_thread_api_token, if: proc { |ctrl|
3     Rails.configuration.anonymous_user_token and
4     'show' == ctrl.action_name
5   }
6
7   def show_pane_list
8     panes = %w(Status Log Advanced)
9     if @object.andand.state == 'Uncommitted'
10       panes = %w(Inputs) + panes - %w(Log)
11     end
12     panes
13   end
14
15   def cancel
16     @object.update_attributes! priority: 0
17     if params[:return_to]
18       redirect_to params[:return_to]
19     else
20       redirect_to @object
21     end
22   end
23
24   def update
25     @updates ||= params[@object.class.to_s.underscore.singularize.to_sym]
26     input_obj = @updates[:mounts].andand[:"/var/lib/cwl/cwl.input.json"].andand[:content]
27     if input_obj
28       workflow = @object.mounts[:"/var/lib/cwl/workflow.json"][:content]
29       get_cwl_inputs(workflow).each do |input_schema|
30         if not input_obj.include? cwl_shortname(input_schema[:id])
31           next
32         end
33         required, primary_type, param_id = cwl_input_info(input_schema)
34         if input_obj[param_id] == ""
35           input_obj[param_id] = nil
36         elsif primary_type == "boolean"
37           input_obj[param_id] = input_obj[param_id] == "true"
38         elsif ["int", "long"].include? primary_type
39           input_obj[param_id] = input_obj[param_id].to_i
40         elsif ["float", "double"].include? primary_type
41           input_obj[param_id] = input_obj[param_id].to_f
42         elsif ["File", "Directory"].include? primary_type
43           re = CollectionsHelper.match_uuid_with_optional_filepath(input_obj[param_id])
44           if re
45             c = Collection.find(re[1])
46             input_obj[param_id] = {"class" => primary_type,
47                                    "location" => "keep:#{c.portable_data_hash}#{re[4]}",
48                                    "arv:collection" => input_obj[param_id]}
49           end
50         end
51       end
52     end
53     params[:merge] = true
54     begin
55       super
56     rescue => e
57       flash[:error] = e.to_s
58       show
59     end
60   end
61
62   def copy
63     src = @object
64
65     @object = ContainerRequest.new
66
67     # By default the copied CR won't be reusing jobs, unless use_existing=true param
68     # is passed.
69     command = src.command
70     if params[:use_existing]
71       @object.use_existing = true
72       # Pass the correct argument to arvados-cwl-runner command.
73       if src.command[0] == 'arvados-cwl-runner'
74         command = src.command - ['--disable-reuse']
75         command.insert(1, '--enable-reuse')
76       end
77     else
78       @object.use_existing = false
79       # Pass the correct argument to arvados-cwl-runner command.
80       if src.command[0] == 'arvados-cwl-runner'
81         command = src.command - ['--enable-reuse']
82         command.insert(1, '--disable-reuse')
83       end
84     end
85
86     @object.command = command
87     @object.container_image = src.container_image
88     @object.cwd = src.cwd
89     @object.description = src.description
90     @object.environment = src.environment
91     @object.mounts = src.mounts
92     @object.name = src.name
93     @object.output_path = src.output_path
94     @object.priority = 1
95     @object.properties[:template_uuid] = src.properties[:template_uuid]
96     @object.runtime_constraints = src.runtime_constraints
97     @object.scheduling_parameters = src.scheduling_parameters
98     @object.state = 'Uncommitted'
99
100     # set owner_uuid to that of source, provided it is a project and writable by current user
101     current_project = Group.find(src.owner_uuid) rescue nil
102     if (current_project && current_project.writable_by.andand.include?(current_user.uuid))
103       @object.owner_uuid = src.owner_uuid
104     end
105
106     super
107   end
108 end