9767: need to parse the json string of the workflow yaml
[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.workflow
50         begin
51           wf_json = JSON.parse(JSON.dump(YAML::load(workflow.workflow)))
52         rescue => e
53           logger.error "Error converting workflow yaml to json: #{e.message}"
54           raise ArgumentError, "Error converting workflow yaml to json: #{e.message}"
55         end
56       end
57
58       model_class = ContainerRequest
59
60       attrs['properties'] = {'template_uuid' => template_uuid}
61       attrs['priority'] = 1
62       attrs['state'] = "Uncommitted"
63
64       # required
65       attrs['command'] = ["arvados-cwl-runner", "--local", "--api=containers", "/var/lib/cwl/workflow.json", "/var/lib/cwl/cwl.input.json"]
66       attrs['container_image'] = "arvados/jobs"
67       attrs['cwd'] = "/var/spool/cwl"
68       attrs['output_path'] = "/var/spool/cwl"
69
70       # mounts
71       mounts = {
72         "/var/lib/cwl/cwl.input.json" => {
73           "kind" => "json",
74           "content" => {}
75         },
76         "stdout" => {
77           "kind" => "file",
78           "path" => "/var/spool/cwl/cwl.output.json"
79         },
80         "/var/spool/cwl" => {
81           "kind" => "collection",
82           "writable" => true
83         }
84       }
85       if wf_json
86         mounts["/var/lib/cwl/workflow.json"] = {
87           "kind" => "json",
88           "content" => wf_json
89         }
90       end
91       attrs['mounts'] = mounts
92
93       # runtime constriants
94       runtime_constraints = {
95         "vcpus" => 1,
96         "ram" => 256000000,
97         "API" => true
98       }
99       attrs['runtime_constraints'] = runtime_constraints
100     else
101       raise ArgumentError, "Unsupported template uuid: #{template_uuid}"
102     end
103
104     attrs['owner_uuid'] = params['work_unit']['owner_uuid']
105     @object ||= model_class.new attrs
106
107     if @object.save
108       redirect_to @object
109     else
110       render_error status: 422
111     end
112   end
113 end