Merge branch 'master' into 9998-unsigned_manifest
[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 = 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       # mounts
81       mounts = {
82         "/var/lib/cwl/cwl.input.json" => {
83           "kind" => "json",
84           "content" => {}
85         },
86         "stdout" => {
87           "kind" => "file",
88           "path" => "/var/spool/cwl/cwl.output.json"
89         },
90         "/var/spool/cwl" => {
91           "kind" => "collection",
92           "writable" => true
93         }
94       }
95       if wf_json
96         mounts["/var/lib/cwl/workflow.json"] = {
97           "kind" => "json",
98           "content" => wf_json
99         }
100       end
101       attrs['mounts'] = mounts
102
103       # runtime constriants
104       runtime_constraints = {
105         "vcpus" => 1,
106         "ram" => 256000000,
107         "API" => true
108       }
109       attrs['runtime_constraints'] = runtime_constraints
110     else
111       raise ArgumentError, "Unsupported template uuid: #{template_uuid}"
112     end
113
114     attrs['owner_uuid'] = params['work_unit']['owner_uuid']
115     @object ||= model_class.new attrs
116
117     if @object.save
118       redirect_to @object
119     else
120       render_error status: 422
121     end
122   end
123
124   def find_object_by_uuid
125     if params['object_type']
126       @object = params['object_type'].constantize.find(params['uuid'])
127     else
128       super
129     end
130   end
131
132   def show_child_component
133     data = JSON.load(params[:action_data])
134
135     current_obj = {}
136     current_obj_uuid = data['current_obj_uuid']
137     current_obj_name = data['current_obj_name']
138     current_obj_type = data['current_obj_type']
139     current_obj_parent = data['current_obj_parent']
140     if current_obj_uuid
141       resource_class = resource_class_for_uuid current_obj_uuid
142       obj = object_for_dataclass(resource_class, current_obj_uuid)
143       current_obj = obj if obj
144     end
145
146     if current_obj.is_a?(Hash) and !current_obj.any?
147       if current_obj_parent
148         resource_class = resource_class_for_uuid current_obj_parent
149         parent = object_for_dataclass(resource_class, current_obj_parent)
150         parent_wu = parent.work_unit
151         children = parent_wu.children
152         if current_obj_uuid
153           wu = children.select {|c| c.uuid == current_obj_uuid}.first
154         else current_obj_name
155           wu = children.select {|c| c.label.to_s == current_obj_name}.first
156         end
157       end
158     else
159       if current_obj_type == JobWorkUnit.to_s
160         wu = JobWorkUnit.new(current_obj, current_obj_name, current_obj_parent)
161       elsif current_obj_type == PipelineInstanceWorkUnit.to_s
162         wu = PipelineInstanceWorkUnit.new(current_obj, current_obj_name, current_obj_parent)
163       elsif current_obj_type == ContainerWorkUnit.to_s
164         wu = ContainerWorkUnit.new(current_obj, current_obj_name, current_obj_parent)
165       end
166     end
167
168     respond_to do |f|
169       f.html { render(partial: "show_component", locals: {wu: wu}) }
170     end
171   end
172 end