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