10111: Merge branch 'master' into 10111-cr-provenance-graph
[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     @svg = ProvenanceHelper::create_provenance_graph nodes,
13                                                      "provenance_svg",
14                                                      {
15                                                        :request => request,
16                                                        :direction => :top_down,
17                                                      }
18   end
19
20   def show_pane_list
21     panes = %w(Status Log Provenance Advanced)
22     if @object.andand.state == 'Uncommitted'
23       panes = %w(Inputs) + panes - %w(Log Provenance)
24     end
25     panes
26   end
27
28   def show
29     generate_provenance(@object)
30     super
31   end
32
33   def cancel
34     @object.update_attributes! priority: 0
35     if params[:return_to]
36       redirect_to params[:return_to]
37     else
38       redirect_to @object
39     end
40   end
41
42   def update
43     @updates ||= params[@object.class.to_s.underscore.singularize.to_sym]
44     input_obj = @updates[:mounts].andand[:"/var/lib/cwl/cwl.input.json"].andand[:content]
45     if input_obj
46       workflow = @object.mounts[:"/var/lib/cwl/workflow.json"][:content]
47       get_cwl_inputs(workflow).each do |input_schema|
48         if not input_obj.include? cwl_shortname(input_schema[:id])
49           next
50         end
51         required, primary_type, param_id = cwl_input_info(input_schema)
52         if input_obj[param_id] == ""
53           input_obj[param_id] = nil
54         elsif primary_type == "boolean"
55           input_obj[param_id] = input_obj[param_id] == "true"
56         elsif ["int", "long"].include? primary_type
57           input_obj[param_id] = input_obj[param_id].to_i
58         elsif ["float", "double"].include? primary_type
59           input_obj[param_id] = input_obj[param_id].to_f
60         elsif ["File", "Directory"].include? primary_type
61           re = CollectionsHelper.match_uuid_with_optional_filepath(input_obj[param_id])
62           if re
63             c = Collection.find(re[1])
64             input_obj[param_id] = {"class" => primary_type,
65                                    "location" => "keep:#{c.portable_data_hash}#{re[4]}",
66                                    "arv:collection" => input_obj[param_id]}
67           end
68         end
69       end
70     end
71     params[:merge] = true
72     begin
73       super
74     rescue => e
75       flash[:error] = e.to_s
76       show
77     end
78   end
79
80   def copy
81     src = @object
82
83     @object = ContainerRequest.new
84
85     @object.command = src.command
86     @object.container_image = src.container_image
87     @object.cwd = src.cwd
88     @object.description = src.description
89     @object.environment = src.environment
90     @object.mounts = src.mounts
91     @object.name = src.name
92     @object.output_path = src.output_path
93     @object.priority = 1
94     @object.properties[:template_uuid] = src.properties[:template_uuid]
95     @object.runtime_constraints = src.runtime_constraints
96     @object.scheduling_parameters = src.scheduling_parameters
97     @object.state = 'Uncommitted'
98     @object.use_existing = false
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