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