10079: update show_child_component route and remove the show_component.js.erb file...
[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
115   def show_child_component
116     data = JSON.load(params[:action_data])
117
118     current_obj = data['current_obj']
119     current_obj_type = data['current_obj_type']
120     if current_obj_type == JobWorkUnit.to_s
121       current_obj = arvados_api_client.unpack_api_response current_obj, 'arvados#job'
122       wu = JobWorkUnit.new(current_obj, params['name'])
123     elsif current_obj_type == PipelineInstanceWorkUnit.to_s
124       current_obj = arvados_api_client.unpack_api_response current_obj, 'arvados#pipelineInstance'
125       wu = PipelineInstanceWorkUnit.new(current_obj, params['name'])
126     elsif current_obj_type == ContainerWorkUnit.to_s
127       current_obj = arvados_api_client.unpack_api_response current_obj, 'arvados#containerRequest'
128       wu = ContainerWorkUnit.new(current_obj, params['name'])
129     end
130
131     @object ||= arvados_api_client.unpack_api_response data['main_obj'], data['main_obj_kind']
132
133     respond_to do |f|
134       f.html { render(partial: "show_component", locals: {wu: wu}) }
135     end
136   end
137 end