Merge branch '10200-cwl-crunch-script' closes #10200
[arvados.git] / apps / workbench / app / controllers / work_units_controller.rb
1 class WorkUnitsController < ApplicationController
2   def find_objects_for_index
3     # If it's not the index rows partial display, just return
4     # The /index request will again be invoked to display the
5     # partial at which time, we will be using the objects found.
6     return if !params[:partial]
7
8     @limit = 20
9     @filters = @filters || []
10
11     # get next page of pipeline_instances
12     filters = @filters + [["uuid", "is_a", ["arvados#pipelineInstance"]]]
13     pipelines = PipelineInstance.limit(@limit).order(["created_at desc"]).filter(filters)
14
15     # get next page of jobs
16     filters = @filters + [["uuid", "is_a", ["arvados#job"]]]
17     jobs = Job.limit(@limit).order(["created_at desc"]).filter(filters)
18
19     # get next page of container_requests
20     filters = @filters + [["uuid", "is_a", ["arvados#containerRequest"]]]
21     crs = ContainerRequest.limit(@limit).order(["created_at desc"]).filter(filters)
22     @objects = (jobs.to_a + pipelines.to_a + crs.to_a).sort_by(&:created_at).reverse.first(@limit)
23
24     if @objects.any?
25       @next_page_filters = next_page_filters('<=')
26       @next_page_href = url_for(partial: :all_processes_rows,
27                                 filters: @next_page_filters.to_json)
28       preload_links_for_objects(@objects.to_a)
29     else
30       @next_page_href = nil
31     end
32   end
33
34   def next_page_href with_params={}
35     @next_page_href
36   end
37
38   def create
39     template_uuid = params['work_unit']['template_uuid']
40
41     attrs = {}
42     rc = resource_class_for_uuid(template_uuid)
43     if rc == PipelineTemplate
44       model_class = PipelineInstance
45       attrs['pipeline_template_uuid'] = template_uuid
46     elsif rc == Workflow
47       # workflow json
48       workflow = Workflow.find? template_uuid
49       if workflow.definition
50         begin
51           wf_json = YAML::load(workflow.definition)
52         rescue => e
53           logger.error "Error converting definition yaml to json: #{e.message}"
54           raise ArgumentError, "Error converting definition yaml to json: #{e.message}"
55         end
56       end
57
58       model_class = ContainerRequest
59
60       attrs['name'] = "#{workflow['name']} container" if workflow['name'].present?
61       attrs['properties'] = {'template_uuid' => template_uuid}
62       attrs['priority'] = 1
63       attrs['state'] = "Uncommitted"
64
65       # required
66       attrs['command'] = ["arvados-cwl-runner", "--local", "--api=containers", "/var/lib/cwl/workflow.json#main", "/var/lib/cwl/cwl.input.json"]
67       attrs['container_image'] = "arvados/jobs"
68       attrs['cwd'] = "/var/spool/cwl"
69       attrs['output_path'] = "/var/spool/cwl"
70
71       # mounts
72       mounts = {
73         "/var/lib/cwl/cwl.input.json" => {
74           "kind" => "json",
75           "content" => {}
76         },
77         "stdout" => {
78           "kind" => "file",
79           "path" => "/var/spool/cwl/cwl.output.json"
80         },
81         "/var/spool/cwl" => {
82           "kind" => "collection",
83           "writable" => true
84         }
85       }
86       if wf_json
87         mounts["/var/lib/cwl/workflow.json"] = {
88           "kind" => "json",
89           "content" => wf_json
90         }
91       end
92       attrs['mounts'] = mounts
93
94       # runtime constriants
95       runtime_constraints = {
96         "vcpus" => 1,
97         "ram" => 256000000,
98         "API" => true
99       }
100       attrs['runtime_constraints'] = runtime_constraints
101     else
102       raise ArgumentError, "Unsupported template uuid: #{template_uuid}"
103     end
104
105     attrs['owner_uuid'] = params['work_unit']['owner_uuid']
106     @object ||= model_class.new attrs
107
108     if @object.save
109       redirect_to @object
110     else
111       render_error status: 422
112     end
113   end
114 end