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