Merge branch '8784-dir-listings'
[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_filter :require_thread_api_token, if: proc { |ctrl|
7     Rails.configuration.anonymous_user_token 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     # get next page of pipeline_instances
21     if PipelineInstance.api_exists?(:index)
22       filters = @filters + [["uuid", "is_a", ["arvados#pipelineInstance"]]]
23       pipelines = PipelineInstance.limit(@limit).order(["created_at desc"]).filter(filters)
24     end
25
26     # get next page of jobs
27     if Job.api_exists?(:index)
28       filters = @filters + [["uuid", "is_a", ["arvados#job"]]]
29       jobs = Job.limit(@limit).order(["created_at desc"]).filter(filters)
30     end
31
32     # get next page of container_requests
33     filters = @filters + [["uuid", "is_a", ["arvados#containerRequest"]]]
34     crs = ContainerRequest.limit(@limit).order(["created_at desc"]).filter(filters)
35     @objects = (jobs.to_a + pipelines.to_a + crs.to_a).sort_by(&:created_at).reverse.first(@limit)
36
37     if @objects.any?
38       @next_page_filters = next_page_filters('<=')
39       @next_page_href = url_for(partial: :all_processes_rows,
40                                 filters: @next_page_filters.to_json)
41       preload_links_for_objects(@objects.to_a)
42     else
43       @next_page_href = nil
44     end
45   end
46
47   def next_page_href with_params={}
48     @next_page_href
49   end
50
51   def create
52     template_uuid = params['work_unit']['template_uuid']
53
54     attrs = {}
55     rc = resource_class_for_uuid(template_uuid)
56     if rc == PipelineTemplate
57       model_class = PipelineInstance
58       attrs['pipeline_template_uuid'] = template_uuid
59     elsif rc == Workflow
60       # workflow json
61       workflow = Workflow.find? template_uuid
62       if workflow.definition
63         begin
64           wf_json = ActiveSupport::HashWithIndifferentAccess.new YAML::load(workflow.definition)
65         rescue => e
66           logger.error "Error converting definition yaml to json: #{e.message}"
67           raise ArgumentError, "Error converting definition yaml to json: #{e.message}"
68         end
69       end
70
71       model_class = ContainerRequest
72
73       attrs['name'] = "#{workflow['name']} container" if workflow['name'].present?
74       attrs['properties'] = {'template_uuid' => template_uuid}
75       attrs['priority'] = 1
76       attrs['state'] = "Uncommitted"
77
78       # required
79       attrs['command'] = ["arvados-cwl-runner", "--local", "--api=containers", "/var/lib/cwl/workflow.json#main", "/var/lib/cwl/cwl.input.json"]
80       attrs['container_image'] = "arvados/jobs"
81       attrs['cwd'] = "/var/spool/cwl"
82       attrs['output_path'] = "/var/spool/cwl"
83
84       input_defaults = {}
85       if wf_json
86         inputs = get_cwl_inputs(wf_json)
87         inputs.each do |input|
88           if input[:default]
89             input_defaults[cwl_shortname(input[:id])] = input[:default]
90           end
91         end
92       end
93
94       # mounts
95       mounts = {
96         "/var/lib/cwl/cwl.input.json" => {
97           "kind" => "json",
98           "content" => input_defaults
99         },
100         "stdout" => {
101           "kind" => "file",
102           "path" => "/var/spool/cwl/cwl.output.json"
103         },
104         "/var/spool/cwl" => {
105           "kind" => "collection",
106           "writable" => true
107         }
108       }
109       if wf_json
110         mounts["/var/lib/cwl/workflow.json"] = {
111           "kind" => "json",
112           "content" => wf_json
113         }
114       end
115       attrs['mounts'] = mounts
116
117       # runtime constriants
118       runtime_constraints = {
119         "vcpus" => 1,
120         "ram" => 256000000,
121         "API" => true
122       }
123       attrs['runtime_constraints'] = runtime_constraints
124     else
125       raise ArgumentError, "Unsupported template uuid: #{template_uuid}"
126     end
127
128     attrs['owner_uuid'] = params['work_unit']['owner_uuid']
129     @object ||= model_class.new attrs
130
131     if @object.save
132       redirect_to @object
133     else
134       render_error status: 422
135     end
136   end
137
138   def find_object_by_uuid
139     if params['object_type']
140       @object = params['object_type'].constantize.find(params['uuid'])
141     else
142       super
143     end
144   end
145
146   def show_child_component
147     data = JSON.load(params[:action_data])
148
149     current_obj = {}
150     current_obj_uuid = data['current_obj_uuid']
151     current_obj_name = data['current_obj_name']
152     current_obj_type = data['current_obj_type']
153     current_obj_parent = data['current_obj_parent']
154     if current_obj_uuid
155       resource_class = resource_class_for_uuid current_obj_uuid
156       obj = object_for_dataclass(resource_class, current_obj_uuid)
157       current_obj = obj if obj
158     end
159
160     if current_obj.is_a?(Hash) and !current_obj.any?
161       if current_obj_parent
162         resource_class = resource_class_for_uuid current_obj_parent
163         parent = object_for_dataclass(resource_class, current_obj_parent)
164         parent_wu = parent.work_unit
165         children = parent_wu.children
166         if current_obj_uuid
167           wu = children.select {|c| c.uuid == current_obj_uuid}.first
168         else current_obj_name
169           wu = children.select {|c| c.label.to_s == current_obj_name}.first
170         end
171       end
172     else
173       if current_obj_type == JobWorkUnit.to_s
174         wu = JobWorkUnit.new(current_obj, current_obj_name, current_obj_parent)
175       elsif current_obj_type == PipelineInstanceWorkUnit.to_s
176         wu = PipelineInstanceWorkUnit.new(current_obj, current_obj_name, current_obj_parent)
177       elsif current_obj_type == ContainerWorkUnit.to_s
178         wu = ContainerWorkUnit.new(current_obj, current_obj_name, current_obj_parent)
179       end
180     end
181
182     respond_to do |f|
183       f.html { render(partial: "show_component", locals: {wu: wu}) }
184     end
185   end
186 end