Merge branch 'master' into 5189-manage-acct-shows-own-repos
[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   def update
7     if resource_attrs[:job_uuid]
8       @object.job_readable = readable_job_uuids(resource_attrs[:job_uuid]).any?
9     end
10     super
11   end
12
13   def self._ping_requires_parameters
14     { ping_secret: {required: true} }
15   end
16
17   def ping
18     act_as_system_user do
19       @object = Node.where(uuid: (params[:id] || params[:uuid])).first
20       if !@object
21         return render_not_found
22       end
23       ping_data = {
24         ip: params[:local_ipv4] || request.env['REMOTE_ADDR'],
25         ec2_instance_id: params[:instance_id]
26       }
27       [:ping_secret, :total_cpu_cores, :total_ram_mb, :total_scratch_mb]
28         .each do |key|
29         ping_data[key] = params[key] if params[key]
30       end
31       @object.ping(ping_data)
32       if @object.info['ping_secret'] == params[:ping_secret]
33         send_json @object.as_api_response(:superuser)
34       else
35         raise "Invalid ping_secret after ping"
36       end
37     end
38   end
39
40   def find_objects_for_index
41     if !current_user.andand.is_admin && current_user.andand.is_active
42       # active non-admin users can list nodes that are (or were
43       # recently) working
44       @objects = model_class.where('last_ping_at >= ?', Time.now - 1.hours)
45     end
46     super
47     job_uuids = @objects.map { |n| n[:job_uuid] }.compact
48     assoc_jobs = readable_job_uuids(job_uuids)
49     @objects.each do |node|
50       node.job_readable = assoc_jobs.include?(node[:job_uuid])
51     end
52   end
53
54   protected
55
56   def readable_job_uuids(*uuids)
57     Job.readable_by(*@read_users).select(:uuid).where(uuid: uuids).map(&:uuid)
58   end
59 end