11185: Merge branch 'master' into 11185-wb-disable-reuse
[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 generate_provenance(cr)
8     return if params['tab_pane'] != "Provenance"
9
10     nodes = {}
11     nodes[cr[:uuid]] = cr
12     if cr[:container_uuid]
13       ContainerRequest.where(requesting_container_uuid: cr[:container_uuid]).each do |child|
14         nodes[child[:uuid]] = child
15       end
16     end
17     @svg = ProvenanceHelper::create_provenance_graph nodes,
18                                                      "provenance_svg",
19                                                      {
20                                                        :request => request,
21                                                        :direction => :top_down,
22                                                      }
23   end
24
25   def show_pane_list
26     panes = %w(Status Log Provenance Advanced)
27     if @object.andand.state == 'Uncommitted'
28       panes = %w(Inputs) + panes - %w(Log Provenance)
29     end
30     panes
31   end
32
33   def show
34     generate_provenance(@object)
35     super
36   end
37
38   def cancel
39     @object.update_attributes! priority: 0
40     if params[:return_to]
41       redirect_to params[:return_to]
42     else
43       redirect_to @object
44     end
45   end
46
47   def update
48     @updates ||= params[@object.class.to_s.underscore.singularize.to_sym]
49     input_obj = @updates[:mounts].andand[:"/var/lib/cwl/cwl.input.json"].andand[:content]
50     if input_obj
51       workflow = @object.mounts[:"/var/lib/cwl/workflow.json"][:content]
52       get_cwl_inputs(workflow).each do |input_schema|
53         if not input_obj.include? cwl_shortname(input_schema[:id])
54           next
55         end
56         required, primary_type, param_id = cwl_input_info(input_schema)
57         if input_obj[param_id] == ""
58           input_obj[param_id] = nil
59         elsif primary_type == "boolean"
60           input_obj[param_id] = input_obj[param_id] == "true"
61         elsif ["int", "long"].include? primary_type
62           input_obj[param_id] = input_obj[param_id].to_i
63         elsif ["float", "double"].include? primary_type
64           input_obj[param_id] = input_obj[param_id].to_f
65         elsif ["File", "Directory"].include? primary_type
66           re = CollectionsHelper.match_uuid_with_optional_filepath(input_obj[param_id])
67           if re
68             c = Collection.find(re[1])
69             input_obj[param_id] = {"class" => primary_type,
70                                    "location" => "keep:#{c.portable_data_hash}#{re[4]}",
71                                    "arv:collection" => input_obj[param_id]}
72           end
73         end
74       end
75     end
76     params[:merge] = true
77     begin
78       super
79     rescue => e
80       flash[:error] = e.to_s
81       show
82     end
83   end
84
85   def copy
86     src = @object
87
88     @object = ContainerRequest.new
89
90     # By default the copied CR won't be reusing containers, unless use_existing=true
91     # param is passed.
92     command = src.command
93     if params[:use_existing]
94       @object.use_existing = true
95       # Pass the correct argument to arvados-cwl-runner command.
96       if src.command[0] == 'arvados-cwl-runner'
97         command = src.command - ['--disable-reuse']
98         command.insert(1, '--enable-reuse')
99       end
100     else
101       @object.use_existing = false
102       # Pass the correct argument to arvados-cwl-runner command.
103       if src.command[0] == 'arvados-cwl-runner'
104         command = src.command - ['--enable-reuse']
105         command.insert(1, '--disable-reuse')
106       end
107     end
108
109     @object.command = command
110     @object.container_image = src.container_image
111     @object.cwd = src.cwd
112     @object.description = src.description
113     @object.environment = src.environment
114     @object.mounts = src.mounts
115     @object.name = src.name
116     @object.output_path = src.output_path
117     @object.priority = 1
118     @object.properties[:template_uuid] = src.properties[:template_uuid]
119     @object.runtime_constraints = src.runtime_constraints
120     @object.scheduling_parameters = src.scheduling_parameters
121     @object.state = 'Uncommitted'
122
123     # set owner_uuid to that of source, provided it is a project and writable by current user
124     current_project = Group.find(src.owner_uuid) rescue nil
125     if (current_project && current_project.writable_by.andand.include?(current_user.uuid))
126       @object.owner_uuid = src.owner_uuid
127     end
128
129     super
130   end
131 end