Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / apps / workbench / app / controllers / work_units_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class WorkUnitsController < ApplicationController
6   skip_around_action :require_thread_api_token, if: proc { |ctrl|
7     !Rails.configuration.Users.AnonymousUserToken.empty? and
8     'show_child_component' == ctrl.action_name
9   }
10
11   def find_objects_for_index
12     # If it's not the index rows partial display, just return
13     # The /index request will again be invoked to display the
14     # partial at which time, we will be using the objects found.
15     return if !params[:partial]
16
17     @limit = 20
18     @filters = @filters || []
19
20     pipelines = []
21     jobs = []
22
23     # get next page of pipeline_instances
24     if PipelineInstance.api_exists?(:index)
25       filters = @filters + [["uuid", "is_a", ["arvados#pipelineInstance"]]]
26       pipelines = PipelineInstance.limit(@limit).order(["created_at desc"]).filter(filters).with_count("none")
27     end
28
29     if params[:show_children]
30       # get next page of jobs
31       if Job.api_exists?(:index)
32         filters = @filters + [["uuid", "is_a", ["arvados#job"]]]
33         jobs = Job.limit(@limit).order(["created_at desc"]).filter(filters).with_count("none")
34       end
35     end
36
37     # get next page of container_requests
38     filters = @filters + [["uuid", "is_a", ["arvados#containerRequest"]]]
39     if !params[:show_children]
40      filters << ["requesting_container_uuid", "=", nil]
41     end
42     crs = ContainerRequest.limit(@limit).order(["created_at desc"]).filter(filters).with_count("none")
43     @objects = (jobs.to_a + pipelines.to_a + crs.to_a).sort_by(&:created_at).reverse.first(@limit)
44
45     if @objects.any?
46       @next_page_filters = next_page_filters('<=')
47       @next_page_href = url_for(partial: :all_processes_rows,
48                                 filters: @next_page_filters.to_json,
49                                 show_children: params[:show_children])
50       preload_links_for_objects(@objects.to_a)
51     else
52       @next_page_href = nil
53     end
54   end
55
56   def next_page_href with_params={}
57     @next_page_href
58   end
59
60   def create
61     template_uuid = params['work_unit']['template_uuid']
62
63     attrs = {}
64     rc = resource_class_for_uuid(template_uuid)
65     if rc == PipelineTemplate
66       model_class = PipelineInstance
67       attrs['pipeline_template_uuid'] = template_uuid
68     elsif rc == Workflow
69       # workflow json
70       workflow = Workflow.find? template_uuid
71       if workflow.definition
72         begin
73           wf_json = ActiveSupport::HashWithIndifferentAccess.new YAML::load(workflow.definition)
74         rescue => e
75           logger.error "Error converting definition yaml to json: #{e.message}"
76           raise ArgumentError, "Error converting definition yaml to json: #{e.message}"
77         end
78       end
79
80       model_class = ContainerRequest
81
82       attrs['name'] = "#{workflow['name']} container" if workflow['name'].present?
83       attrs['properties'] = {'template_uuid' => template_uuid}
84       attrs['priority'] = 1
85       attrs['state'] = "Uncommitted"
86       attrs['use_existing'] = false
87
88       # required
89       attrs['container_image'] = "arvados/jobs"
90       attrs['cwd'] = "/var/spool/cwl"
91       attrs['output_path'] = "/var/spool/cwl"
92
93       # runtime constriants
94       runtime_constraints = {
95         "vcpus" => 1,
96         "ram" => 1024 * 1024 * 1024,
97         "API" => true
98       }
99
100       keep_cache = 256
101       input_defaults = {}
102       if wf_json
103         main = get_cwl_main(wf_json)
104         main[:inputs].each do |input|
105           if input[:default]
106             input_defaults[cwl_shortname(input[:id])] = input[:default]
107           end
108         end
109         if main[:hints]
110           main[:hints].each do |hint|
111             if hint[:class] == "http://arvados.org/cwl#WorkflowRunnerResources"
112               if hint[:coresMin]
113                 runtime_constraints["vcpus"] = hint[:coresMin]
114               end
115               if hint[:ramMin]
116                 runtime_constraints["ram"] = hint[:ramMin] * 1024 * 1024
117               end
118               if hint[:keep_cache]
119                 keep_cache = hint[:keep_cache]
120               end
121               if hint[:acrContainerImage]
122                 attrs['container_image'] = hint[:acrContainerImage]
123               end
124             end
125           end
126         end
127       end
128
129       attrs['command'] = ["arvados-cwl-runner",
130                           "--enable-reuse",
131                           "--local",
132                           "--api=containers",
133                           "--project-uuid=#{params['work_unit']['owner_uuid']}",
134                           "--collection-cache-size=#{keep_cache}",
135                           "/var/lib/cwl/workflow.json#main",
136                           "/var/lib/cwl/cwl.input.json"]
137
138       # mounts
139       mounts = {
140         "/var/lib/cwl/cwl.input.json" => {
141           "kind" => "json",
142           "content" => input_defaults
143         },
144         "stdout" => {
145           "kind" => "file",
146           "path" => "/var/spool/cwl/cwl.output.json"
147         },
148         "/var/spool/cwl" => {
149           "kind" => "collection",
150           "writable" => true
151         }
152       }
153       if wf_json
154         mounts["/var/lib/cwl/workflow.json"] = {
155           "kind" => "json",
156           "content" => wf_json
157         }
158       end
159       attrs['mounts'] = mounts
160
161       attrs['runtime_constraints'] = runtime_constraints
162     else
163       raise ArgumentError, "Unsupported template uuid: #{template_uuid}"
164     end
165
166     attrs['owner_uuid'] = params['work_unit']['owner_uuid']
167     @object ||= model_class.new attrs
168
169     if @object.save
170       redirect_to @object
171     else
172       render_error status: 422
173     end
174   end
175
176   def find_object_by_uuid
177     if params['object_type']
178       @object = params['object_type'].constantize.find(params['uuid'])
179     else
180       super
181     end
182   end
183
184   def show_child_component
185     data = JSON.load(params[:action_data])
186
187     current_obj = {}
188     current_obj_uuid = data['current_obj_uuid']
189     current_obj_name = data['current_obj_name']
190     current_obj_type = data['current_obj_type']
191     current_obj_parent = data['current_obj_parent']
192     if current_obj_uuid
193       resource_class = resource_class_for_uuid current_obj_uuid
194       obj = object_for_dataclass(resource_class, current_obj_uuid)
195       current_obj = obj if obj
196     end
197
198     if current_obj.is_a?(Hash) and !current_obj.any?
199       if current_obj_parent
200         resource_class = resource_class_for_uuid current_obj_parent
201         parent = object_for_dataclass(resource_class, current_obj_parent)
202         parent_wu = parent.work_unit
203         children = parent_wu.children
204         if current_obj_uuid
205           wu = children.select {|c| c.uuid == current_obj_uuid}.first
206         else current_obj_name
207           wu = children.select {|c| c.label.to_s == current_obj_name}.first
208         end
209       end
210     else
211       if current_obj_type == JobWorkUnit.to_s
212         wu = JobWorkUnit.new(current_obj, current_obj_name, current_obj_parent)
213       elsif current_obj_type == PipelineInstanceWorkUnit.to_s
214         wu = PipelineInstanceWorkUnit.new(current_obj, current_obj_name, current_obj_parent)
215       elsif current_obj_type == ContainerWorkUnit.to_s
216         wu = ContainerWorkUnit.new(current_obj, current_obj_name, current_obj_parent)
217       end
218     end
219
220     respond_to do |f|
221       f.html { render(partial: "show_component", locals: {wu: wu}) }
222     end
223   end
224 end