Merge branch 'master' into 5720-ajax-loading-error
[arvados.git] / services / api / app / controllers / arvados / v1 / nodes_controller.rb
1 class Arvados::V1::NodesController < ApplicationController
2   skip_before_filter :require_auth_scope, :only => :ping
3   skip_before_filter :find_object_by_uuid, :only => :ping
4   skip_before_filter :render_404_if_no_object, :only => :ping
5
6   include DbCurrentTime
7
8   def update
9     if resource_attrs[:job_uuid]
10       @object.job_readable = readable_job_uuids(resource_attrs[:job_uuid]).any?
11     end
12     super
13   end
14
15   def self._ping_requires_parameters
16     { ping_secret: {required: true} }
17   end
18
19   def ping
20     act_as_system_user do
21       @object = Node.where(uuid: (params[:id] || params[:uuid])).first
22       if !@object
23         return render_not_found
24       end
25       ping_data = {
26         ip: params[:local_ipv4] || request.env['REMOTE_ADDR'],
27         ec2_instance_id: params[:instance_id]
28       }
29       [:ping_secret, :total_cpu_cores, :total_ram_mb, :total_scratch_mb]
30         .each do |key|
31         ping_data[key] = params[key] if params[key]
32       end
33       @object.ping(ping_data)
34       if @object.info['ping_secret'] == params[:ping_secret]
35         send_json @object.as_api_response(:superuser)
36       else
37         raise "Invalid ping_secret after ping"
38       end
39     end
40   end
41
42   def find_objects_for_index
43     if !current_user.andand.is_admin && current_user.andand.is_active
44       # active non-admin users can list nodes that are (or were
45       # recently) working
46       @objects = model_class.where('last_ping_at >= ?', db_current_time - 1.hours)
47     end
48     super
49     job_uuids = @objects.map { |n| n[:job_uuid] }.compact
50     assoc_jobs = readable_job_uuids(job_uuids)
51     @objects.each do |node|
52       node.job_readable = assoc_jobs.include?(node[:job_uuid])
53     end
54   end
55
56   protected
57
58   def readable_job_uuids(*uuids)
59     Job.readable_by(*@read_users).select(:uuid).where(uuid: uuids).map(&:uuid)
60   end
61 end