10111: Refactored the graph creation code to minimize the amount of API calls neeeded.
[arvados.git] / apps / workbench / app / controllers / container_requests_controller.rb
1 class ContainerRequestsController < ApplicationController
2   skip_around_filter :require_thread_api_token, if: proc { |ctrl|
3     Rails.configuration.anonymous_user_token and
4     'show' == ctrl.action_name
5   }
6
7   def generate_provenance(cr)
8     return if params['tab_pane'] != "Provenance"
9
10     nodes = {cr[:uuid] => cr}
11     @svg = ProvenanceHelper::create_provenance_graph nodes,
12                                                      "provenance_svg",
13                                                      {
14                                                        :request => request,
15                                                        :direction => :top_down,
16                                                      }
17   end
18
19   def show_pane_list
20     panes = %w(Status Log Provenance Advanced)
21     if @object.andand.state == 'Uncommitted'
22       panes = %w(Inputs) + panes - %w(Log Provenance)
23     end
24     panes
25   end
26
27   def show
28     generate_provenance(@object)
29     super
30   end
31
32   def cancel
33     @object.update_attributes! priority: 0
34     if params[:return_to]
35       redirect_to params[:return_to]
36     else
37       redirect_to @object
38     end
39   end
40
41   def update
42     @updates ||= params[@object.class.to_s.underscore.singularize.to_sym]
43     input_obj = @updates[:mounts].andand[:"/var/lib/cwl/cwl.input.json"].andand[:content]
44     if input_obj
45       workflow = @object.mounts[:"/var/lib/cwl/workflow.json"][:content]
46       get_cwl_inputs(workflow).each do |input_schema|
47         if not input_obj.include? cwl_shortname(input_schema[:id])
48           next
49         end
50         required, primary_type, param_id = cwl_input_info(input_schema)
51         if input_obj[param_id] == ""
52           input_obj[param_id] = nil
53         elsif primary_type == "boolean"
54           input_obj[param_id] = input_obj[param_id] == "true"
55         elsif ["int", "long"].include? primary_type
56           input_obj[param_id] = input_obj[param_id].to_i
57         elsif ["float", "double"].include? primary_type
58           input_obj[param_id] = input_obj[param_id].to_f
59         elsif ["File", "Directory"].include? primary_type
60           re = CollectionsHelper.match_uuid_with_optional_filepath(input_obj[param_id])
61           if re
62             c = Collection.find(re[1])
63             input_obj[param_id] = {"class" => primary_type,
64                                    "location" => "keep:#{c.portable_data_hash}#{re[4]}",
65                                    "arv:collection" => input_obj[param_id]}
66           end
67         end
68       end
69     end
70     params[:merge] = true
71     begin
72       super
73     rescue => e
74       flash[:error] = e.to_s
75       show
76     end
77   end
78
79   def copy
80     src = @object
81
82     @object = ContainerRequest.new
83
84     # By default the copied CR won't be reusing containers, unless use_existing=true
85     # param is passed.
86     command = src.command
87     if params[:use_existing]
88       @object.use_existing = true
89       # Pass the correct argument to arvados-cwl-runner command.
90       if src.command[0] == 'arvados-cwl-runner'
91         command = src.command - ['--disable-reuse']
92         command.insert(1, '--enable-reuse')
93       end
94     else
95       @object.use_existing = false
96       # Pass the correct argument to arvados-cwl-runner command.
97       if src.command[0] == 'arvados-cwl-runner'
98         command = src.command - ['--enable-reuse']
99         command.insert(1, '--disable-reuse')
100       end
101     end
102
103     @object.command = command
104     @object.container_image = src.container_image
105     @object.cwd = src.cwd
106     @object.description = src.description
107     @object.environment = src.environment
108     @object.mounts = src.mounts
109     @object.name = src.name
110     @object.output_path = src.output_path
111     @object.priority = 1
112     @object.properties[:template_uuid] = src.properties[:template_uuid]
113     @object.runtime_constraints = src.runtime_constraints
114     @object.scheduling_parameters = src.scheduling_parameters
115     @object.state = 'Uncommitted'
116
117     # set owner_uuid to that of source, provided it is a project and writable by current user
118     current_project = Group.find(src.owner_uuid) rescue nil
119     if (current_project && current_project.writable_by.andand.include?(current_user.uuid))
120       @object.owner_uuid = src.owner_uuid
121     end
122
123     super
124   end
125 end