10111: Moved the api request related code to the controller.
[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 = {cr[:uuid] => cr}
11     child_crs = []
12     col_uuids = []
13     col_pdhs = []
14     col_uuids << cr[:output_uuid] if cr[:output_uuid]
15     col_pdhs += ProvenanceHelper::cr_input_pdhs(cr)
16
17     # Search for child CRs
18     if cr[:container_uuid]
19       child_crs = ContainerRequest.where(requesting_container_uuid: cr[:container_uuid])
20
21       child_crs.each do |child|
22         nodes[child[:uuid]] = child
23         col_uuids << child[:output_uuid] if child[:output_uuid]
24         col_pdhs += ProvenanceHelper::cr_input_pdhs(child)
25       end
26     end
27
28     output_cols = {} # Indexed by UUID
29     input_cols = {} # Indexed by PDH
30     output_pdhs = []
31
32     # Batch requests to get all related collections
33     # First fetch output collections by UUID.
34     Collection.filter([['uuid', 'in', col_uuids.uniq]]).each do |c|
35       output_cols[c[:uuid]] = c
36       output_pdhs << c[:portable_data_hash]
37     end
38     # Then, get only input collections by PDH. There could be more than one collection
39     # per PDH: the number of collections is used on the collection node label.
40     Collection.filter(
41       [['portable_data_hash', 'in', col_pdhs - output_pdhs]]).each do |c|
42       if input_cols[c[:portable_data_hash]]
43         input_cols[c[:portable_data_hash]] << c
44       else
45         input_cols[c[:portable_data_hash]] = [c]
46       end
47     end
48
49     @svg = ProvenanceHelper::create_provenance_graph(
50       nodes, "provenance_svg",
51       {
52         :request => request,
53         :direction => :top_down,
54         :output_collections => output_cols,
55         :input_collections => input_cols,
56         :cr_children_of => {
57           cr[:uuid] => child_crs.select{|child| child[:uuid]},
58         },
59       })
60   end
61
62   def show_pane_list
63     panes = %w(Status Log Provenance Advanced)
64     if @object.andand.state == 'Uncommitted'
65       panes = %w(Inputs) + panes - %w(Log Provenance)
66     end
67     panes
68   end
69
70   def show
71     generate_provenance(@object)
72     super
73   end
74
75   def cancel
76     @object.update_attributes! priority: 0
77     if params[:return_to]
78       redirect_to params[:return_to]
79     else
80       redirect_to @object
81     end
82   end
83
84   def update
85     @updates ||= params[@object.class.to_s.underscore.singularize.to_sym]
86     input_obj = @updates[:mounts].andand[:"/var/lib/cwl/cwl.input.json"].andand[:content]
87     if input_obj
88       workflow = @object.mounts[:"/var/lib/cwl/workflow.json"][:content]
89       get_cwl_inputs(workflow).each do |input_schema|
90         if not input_obj.include? cwl_shortname(input_schema[:id])
91           next
92         end
93         required, primary_type, param_id = cwl_input_info(input_schema)
94         if input_obj[param_id] == ""
95           input_obj[param_id] = nil
96         elsif primary_type == "boolean"
97           input_obj[param_id] = input_obj[param_id] == "true"
98         elsif ["int", "long"].include? primary_type
99           input_obj[param_id] = input_obj[param_id].to_i
100         elsif ["float", "double"].include? primary_type
101           input_obj[param_id] = input_obj[param_id].to_f
102         elsif ["File", "Directory"].include? primary_type
103           re = CollectionsHelper.match_uuid_with_optional_filepath(input_obj[param_id])
104           if re
105             c = Collection.find(re[1])
106             input_obj[param_id] = {"class" => primary_type,
107                                    "location" => "keep:#{c.portable_data_hash}#{re[4]}",
108                                    "arv:collection" => input_obj[param_id]}
109           end
110         end
111       end
112     end
113     params[:merge] = true
114     begin
115       super
116     rescue => e
117       flash[:error] = e.to_s
118       show
119     end
120   end
121
122   def copy
123     src = @object
124
125     @object = ContainerRequest.new
126
127     # By default the copied CR won't be reusing containers, unless use_existing=true
128     # param is passed.
129     command = src.command
130     if params[:use_existing]
131       @object.use_existing = true
132       # Pass the correct argument to arvados-cwl-runner command.
133       if src.command[0] == 'arvados-cwl-runner'
134         command = src.command - ['--disable-reuse']
135         command.insert(1, '--enable-reuse')
136       end
137     else
138       @object.use_existing = false
139       # Pass the correct argument to arvados-cwl-runner command.
140       if src.command[0] == 'arvados-cwl-runner'
141         command = src.command - ['--enable-reuse']
142         command.insert(1, '--disable-reuse')
143       end
144     end
145
146     @object.command = command
147     @object.container_image = src.container_image
148     @object.cwd = src.cwd
149     @object.description = src.description
150     @object.environment = src.environment
151     @object.mounts = src.mounts
152     @object.name = src.name
153     @object.output_path = src.output_path
154     @object.priority = 1
155     @object.properties[:template_uuid] = src.properties[:template_uuid]
156     @object.runtime_constraints = src.runtime_constraints
157     @object.scheduling_parameters = src.scheduling_parameters
158     @object.state = 'Uncommitted'
159
160     # set owner_uuid to that of source, provided it is a project and writable by current user
161     current_project = Group.find(src.owner_uuid) rescue nil
162     if (current_project && current_project.writable_by.andand.include?(current_user.uuid))
163       @object.owner_uuid = src.owner_uuid
164     end
165
166     super
167   end
168 end